Socket
Socket
Sign inDemoInstall

@cosmjs/proto-signing

Package Overview
Dependencies
Maintainers
2
Versions
92
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cosmjs/proto-signing - npm Package Compare versions

Comparing version 0.25.1 to 0.25.2

build/paths.d.ts

51

build/directsecp256k1hdwallet.d.ts
import { EnglishMnemonic, HdPath } from "@cosmjs/crypto";
import { SignDoc } from "./codec/cosmos/tx/v1beta1/tx";
import { AccountData, DirectSignResponse, OfflineDirectSigner } from "./signer";
import { EncryptionConfiguration, KdfConfiguration } from "./wallet";
/**
* This interface describes a JSON object holding the encrypted wallet and the meta data.
* All fields in here must be JSON types.
*/
export interface DirectSecp256k1HdWalletSerialization {
/** A format+version identifier for this serialization format */
readonly type: string;
/** Information about the key derivation function (i.e. password to encryption key) */
readonly kdf: KdfConfiguration;
/** Information about the symmetric encryption */
readonly encryption: EncryptionConfiguration;
/** An instance of Secp256k1HdWalletData, which is stringified, encrypted and base64 encoded. */
readonly data: string;
}
export declare function extractKdfConfiguration(serialization: string): KdfConfiguration;
export interface DirectSecp256k1HdWalletOptions {

@@ -31,2 +47,20 @@ /** The password to use when deriving a BIP39 seed from a mnemonic. */

static generate(length?: 12 | 15 | 18 | 21 | 24, options?: Partial<DirectSecp256k1HdWalletOptions>): Promise<DirectSecp256k1HdWallet>;
/**
* Restores a wallet from an encrypted serialization.
*
* @param password The user provided password used to generate an encryption key via a KDF.
* This is not normalized internally (see "Unicode normalization" to learn more).
*/
static deserialize(serialization: string, password: string): Promise<DirectSecp256k1HdWallet>;
/**
* Restores a wallet from an encrypted serialization.
*
* This is an advanced alternative to calling `deserialize(serialization, password)` directly, which allows
* you to offload the KDF execution to a non-UI thread (e.g. in a WebWorker).
*
* The caller is responsible for ensuring the key was derived with the given KDF configuration. This can be
* done using `extractKdfConfiguration(serialization)` and `executeKdf(password, kdfConfiguration)` from this package.
*/
static deserializeWithEncryptionKey(serialization: string, encryptionKey: Uint8Array): Promise<DirectSecp256k1HdWallet>;
private static deserializeTypeV1;
/** Base secret */

@@ -42,2 +76,19 @@ private readonly secret;

signDirect(signerAddress: string, signDoc: SignDoc): Promise<DirectSignResponse>;
/**
* Generates an encrypted serialization of this wallet.
*
* @param password The user provided password used to generate an encryption key via a KDF.
* This is not normalized internally (see "Unicode normalization" to learn more).
*/
serialize(password: string): Promise<string>;
/**
* Generates an encrypted serialization of this wallet.
*
* This is an advanced alternative to calling `serialize(password)` directly, which allows you to
* offload the KDF execution to a non-UI thread (e.g. in a WebWorker).
*
* The caller is responsible for ensuring the key was derived with the given KDF options. If this
* is not the case, the wallet cannot be restored with the original password.
*/
serializeWithEncryptionKey(encryptionKey: Uint8Array, kdfConfiguration: KdfConfiguration): Promise<string>;
private getKeyPair;

@@ -44,0 +95,0 @@ private getAccountsWithPrivkeys;

145

build/directsecp256k1hdwallet.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DirectSecp256k1HdWallet = void 0;
exports.DirectSecp256k1HdWallet = exports.extractKdfConfiguration = void 0;
const amino_1 = require("@cosmjs/amino");
const crypto_1 = require("@cosmjs/crypto");
const encoding_1 = require("@cosmjs/encoding");
const build_1 = require("@cosmjs/utils/build");
const signing_1 = require("./signing");
const wallet_1 = require("./wallet");
const serializationTypeV1 = "directsecp256k1hdwallet-v1";
/**
* A KDF configuration that is not very strong but can be used on the main thread.
* It takes about 1 second in Node.js 16.0.0 and should have similar runtimes in other modern Wasm hosts.
*/
const basicPasswordHashingOptions = {
algorithm: "argon2id",
params: {
outputLength: 32,
opsLimit: 24,
memLimitKib: 12 * 1024,
},
};
function isDerivationJson(thing) {
if (!build_1.isNonNullObject(thing))
return false;
if (typeof thing.hdPath !== "string")
return false;
if (typeof thing.prefix !== "string")
return false;
return true;
}
function extractKdfConfigurationV1(doc) {
return doc.kdf;
}
function extractKdfConfiguration(serialization) {
const root = JSON.parse(serialization);
if (!build_1.isNonNullObject(root))
throw new Error("Root document is not an object.");
switch (root.type) {
case serializationTypeV1:
return extractKdfConfigurationV1(root);
default:
throw new Error("Unsupported serialization type");
}
}
exports.extractKdfConfiguration = extractKdfConfiguration;
const defaultOptions = {

@@ -49,2 +88,65 @@ bip39Password: "",

}
/**
* Restores a wallet from an encrypted serialization.
*
* @param password The user provided password used to generate an encryption key via a KDF.
* This is not normalized internally (see "Unicode normalization" to learn more).
*/
static async deserialize(serialization, password) {
const root = JSON.parse(serialization);
if (!build_1.isNonNullObject(root))
throw new Error("Root document is not an object.");
switch (root.type) {
case serializationTypeV1:
return DirectSecp256k1HdWallet.deserializeTypeV1(serialization, password);
default:
throw new Error("Unsupported serialization type");
}
}
/**
* Restores a wallet from an encrypted serialization.
*
* This is an advanced alternative to calling `deserialize(serialization, password)` directly, which allows
* you to offload the KDF execution to a non-UI thread (e.g. in a WebWorker).
*
* The caller is responsible for ensuring the key was derived with the given KDF configuration. This can be
* done using `extractKdfConfiguration(serialization)` and `executeKdf(password, kdfConfiguration)` from this package.
*/
static async deserializeWithEncryptionKey(serialization, encryptionKey) {
const root = JSON.parse(serialization);
if (!build_1.isNonNullObject(root))
throw new Error("Root document is not an object.");
const untypedRoot = root;
switch (untypedRoot.type) {
case serializationTypeV1: {
const decryptedBytes = await wallet_1.decrypt(encoding_1.fromBase64(untypedRoot.data), encryptionKey, untypedRoot.encryption);
const decryptedDocument = JSON.parse(encoding_1.fromUtf8(decryptedBytes));
const { mnemonic, accounts } = decryptedDocument;
build_1.assert(typeof mnemonic === "string");
if (!Array.isArray(accounts))
throw new Error("Property 'accounts' is not an array");
if (!accounts.every((account) => isDerivationJson(account))) {
throw new Error("Account is not in the correct format.");
}
const firstPrefix = accounts[0].prefix;
if (!accounts.every(({ prefix }) => prefix === firstPrefix)) {
throw new Error("Accounts do not all have the same prefix");
}
const hdPaths = accounts.map(({ hdPath }) => crypto_1.stringToPath(hdPath));
return DirectSecp256k1HdWallet.fromMnemonic(mnemonic, {
hdPaths: hdPaths,
prefix: firstPrefix,
});
}
default:
throw new Error("Unsupported serialization type");
}
}
static async deserializeTypeV1(serialization, password) {
const root = JSON.parse(serialization);
if (!build_1.isNonNullObject(root))
throw new Error("Root document is not an object.");
const encryptionKey = await wallet_1.executeKdf(password, root.kdf);
return DirectSecp256k1HdWallet.deserializeWithEncryptionKey(serialization, encryptionKey);
}
get mnemonic() {

@@ -78,2 +180,43 @@ return this.secret.toString();

}
/**
* Generates an encrypted serialization of this wallet.
*
* @param password The user provided password used to generate an encryption key via a KDF.
* This is not normalized internally (see "Unicode normalization" to learn more).
*/
async serialize(password) {
const kdfConfiguration = basicPasswordHashingOptions;
const encryptionKey = await wallet_1.executeKdf(password, kdfConfiguration);
return this.serializeWithEncryptionKey(encryptionKey, kdfConfiguration);
}
/**
* Generates an encrypted serialization of this wallet.
*
* This is an advanced alternative to calling `serialize(password)` directly, which allows you to
* offload the KDF execution to a non-UI thread (e.g. in a WebWorker).
*
* The caller is responsible for ensuring the key was derived with the given KDF options. If this
* is not the case, the wallet cannot be restored with the original password.
*/
async serializeWithEncryptionKey(encryptionKey, kdfConfiguration) {
const dataToEncrypt = {
mnemonic: this.mnemonic,
accounts: this.accounts.map(({ hdPath, prefix }) => ({
hdPath: crypto_1.pathToString(hdPath),
prefix: prefix,
})),
};
const dataToEncryptRaw = encoding_1.toUtf8(JSON.stringify(dataToEncrypt));
const encryptionConfiguration = {
algorithm: wallet_1.supportedAlgorithms.xchacha20poly1305Ietf,
};
const encryptedData = await wallet_1.encrypt(dataToEncryptRaw, encryptionKey, encryptionConfiguration);
const out = {
type: serializationTypeV1,
kdf: kdfConfiguration,
encryption: encryptionConfiguration,
data: encoding_1.toBase64(encryptedData),
};
return JSON.stringify(out);
}
async getKeyPair(hdPath) {

@@ -80,0 +223,0 @@ const { privkey } = crypto_1.Slip10.derivePath(crypto_1.Slip10Curve.Secp256k1, this.seed, hdPath);

@@ -9,2 +9,3 @@ "use strict";

const testutils_spec_1 = require("./testutils.spec");
const wallet_1 = require("./wallet");
describe("DirectSecp256k1HdWallet", () => {

@@ -58,2 +59,154 @@ // m/44'/118'/0'/0/0

});
describe("deserialize", () => {
it("can restore", async () => {
const original = await directsecp256k1hdwallet_1.DirectSecp256k1HdWallet.fromMnemonic(defaultMnemonic);
const password = "123";
const serialized = await original.serialize(password);
const deserialized = await directsecp256k1hdwallet_1.DirectSecp256k1HdWallet.deserialize(serialized, password);
const accounts = await deserialized.getAccounts();
expect(deserialized.mnemonic).toEqual(defaultMnemonic);
expect(accounts).toEqual([
{
algo: "secp256k1",
address: defaultAddress,
pubkey: defaultPubkey,
},
]);
});
it("can restore multiple accounts", async () => {
const mnemonic = "economy stock theory fatal elder harbor betray wasp final emotion task crumble siren bottom lizard educate guess current outdoor pair theory focus wife stone";
const prefix = "wasm";
const accountNumbers = [0, 1, 2, 3, 4];
const hdPaths = accountNumbers.map(amino_1.makeCosmoshubPath);
const original = await directsecp256k1hdwallet_1.DirectSecp256k1HdWallet.fromMnemonic(mnemonic, {
hdPaths: hdPaths,
prefix: prefix,
});
const password = "123";
const serialized = await original.serialize(password);
const deserialized = await directsecp256k1hdwallet_1.DirectSecp256k1HdWallet.deserialize(serialized, password);
const accounts = await deserialized.getAccounts();
expect(deserialized.mnemonic).toEqual(mnemonic);
// These values are taken from the generate_addresses.js script in the scripts/wasmd directory
expect(accounts).toEqual([
{
algo: "secp256k1",
pubkey: encoding_1.fromBase64("A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ"),
address: "wasm1pkptre7fdkl6gfrzlesjjvhxhlc3r4gm32kke3",
},
{
algo: "secp256k1",
pubkey: encoding_1.fromBase64("AiDosfIbBi54XJ1QjCeApumcy/FjdtF+YhywPf3DKTx7"),
address: "wasm10dyr9899g6t0pelew4nvf4j5c3jcgv0r5d3a5l",
},
{
algo: "secp256k1",
pubkey: encoding_1.fromBase64("AzQg33JZqH7vSsm09esZY5bZvmzYwE/SY78cA0iLxpD7"),
address: "wasm1xy4yqngt0nlkdcenxymg8tenrghmek4n3u2lwa",
},
{
algo: "secp256k1",
pubkey: encoding_1.fromBase64("A3gOAlB6aiRTCPvWMQg2+ZbGYNsLd8qlvV28m8p2UhY2"),
address: "wasm142u9fgcjdlycfcez3lw8x6x5h7rfjlnfaallkd",
},
{
algo: "secp256k1",
pubkey: encoding_1.fromBase64("Aum2063ub/ErUnIUB36sK55LktGUStgcbSiaAnL1wadu"),
address: "wasm1hsm76p4ahyhl5yh3ve9ur49r5kemhp2r93f89d",
},
]);
});
});
describe("deserializeWithEncryptionKey", () => {
it("can restore", async () => {
const password = "123";
let serialized;
{
const original = await directsecp256k1hdwallet_1.DirectSecp256k1HdWallet.fromMnemonic(defaultMnemonic);
const anyKdfParams = {
algorithm: "argon2id",
params: {
outputLength: 32,
opsLimit: 4,
memLimitKib: 3 * 1024,
},
};
const encryptionKey = await wallet_1.executeKdf(password, anyKdfParams);
serialized = await original.serializeWithEncryptionKey(encryptionKey, anyKdfParams);
}
{
const kdfConfiguration = directsecp256k1hdwallet_1.extractKdfConfiguration(serialized);
const encryptionKey = await wallet_1.executeKdf(password, kdfConfiguration);
const deserialized = await directsecp256k1hdwallet_1.DirectSecp256k1HdWallet.deserializeWithEncryptionKey(serialized, encryptionKey);
expect(deserialized.mnemonic).toEqual(defaultMnemonic);
expect(await deserialized.getAccounts()).toEqual([
{
algo: "secp256k1",
address: defaultAddress,
pubkey: defaultPubkey,
},
]);
}
});
it("can restore multiple accounts", async () => {
const mnemonic = "economy stock theory fatal elder harbor betray wasp final emotion task crumble siren bottom lizard educate guess current outdoor pair theory focus wife stone";
const prefix = "wasm";
const password = "123";
const accountNumbers = [0, 1, 2, 3, 4];
const hdPaths = accountNumbers.map(amino_1.makeCosmoshubPath);
let serialized;
{
const original = await directsecp256k1hdwallet_1.DirectSecp256k1HdWallet.fromMnemonic(mnemonic, {
prefix: prefix,
hdPaths: hdPaths,
});
const anyKdfParams = {
algorithm: "argon2id",
params: {
outputLength: 32,
opsLimit: 4,
memLimitKib: 3 * 1024,
},
};
const encryptionKey = await wallet_1.executeKdf(password, anyKdfParams);
serialized = await original.serializeWithEncryptionKey(encryptionKey, anyKdfParams);
}
{
const kdfConfiguration = directsecp256k1hdwallet_1.extractKdfConfiguration(serialized);
const encryptionKey = await wallet_1.executeKdf(password, kdfConfiguration);
const deserialized = await directsecp256k1hdwallet_1.DirectSecp256k1HdWallet.deserializeWithEncryptionKey(serialized, encryptionKey);
const accounts = await deserialized.getAccounts();
expect(deserialized.mnemonic).toEqual(mnemonic);
expect(deserialized.mnemonic).toEqual(mnemonic);
// These values are taken from the generate_addresses.js script in the scripts/wasmd directory
expect(accounts).toEqual([
{
algo: "secp256k1",
pubkey: encoding_1.fromBase64("A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ"),
address: "wasm1pkptre7fdkl6gfrzlesjjvhxhlc3r4gm32kke3",
},
{
algo: "secp256k1",
pubkey: encoding_1.fromBase64("AiDosfIbBi54XJ1QjCeApumcy/FjdtF+YhywPf3DKTx7"),
address: "wasm10dyr9899g6t0pelew4nvf4j5c3jcgv0r5d3a5l",
},
{
algo: "secp256k1",
pubkey: encoding_1.fromBase64("AzQg33JZqH7vSsm09esZY5bZvmzYwE/SY78cA0iLxpD7"),
address: "wasm1xy4yqngt0nlkdcenxymg8tenrghmek4n3u2lwa",
},
{
algo: "secp256k1",
pubkey: encoding_1.fromBase64("A3gOAlB6aiRTCPvWMQg2+ZbGYNsLd8qlvV28m8p2UhY2"),
address: "wasm142u9fgcjdlycfcez3lw8x6x5h7rfjlnfaallkd",
},
{
algo: "secp256k1",
pubkey: encoding_1.fromBase64("Aum2063ub/ErUnIUB36sK55LktGUStgcbSiaAnL1wadu"),
address: "wasm1hsm76p4ahyhl5yh3ve9ur49r5kemhp2r93f89d",
},
]);
}
});
});
describe("getAccounts", () => {

@@ -94,3 +247,47 @@ it("resolves to a list of accounts", async () => {

});
describe("serialize", () => {
it("can save with password", async () => {
const wallet = await directsecp256k1hdwallet_1.DirectSecp256k1HdWallet.fromMnemonic(defaultMnemonic);
const serialized = await wallet.serialize("123");
expect(JSON.parse(serialized)).toEqual({
type: "directsecp256k1hdwallet-v1",
kdf: {
algorithm: "argon2id",
params: {
outputLength: 32,
opsLimit: 24,
memLimitKib: 12 * 1024,
},
},
encryption: {
algorithm: "xchacha20poly1305-ietf",
},
data: jasmine.stringMatching(testutils_spec_1.base64Matcher),
});
});
});
describe("serializeWithEncryptionKey", () => {
it("can save with password", async () => {
const wallet = await directsecp256k1hdwallet_1.DirectSecp256k1HdWallet.fromMnemonic(defaultMnemonic);
const key = encoding_1.fromHex("aabb221100aabb332211aabb33221100aabb221100aabb332211aabb33221100");
const customKdfConfiguration = {
algorithm: "argon2id",
params: {
outputLength: 32,
opsLimit: 321,
memLimitKib: 11 * 1024,
},
};
const serialized = await wallet.serializeWithEncryptionKey(key, customKdfConfiguration);
expect(JSON.parse(serialized)).toEqual({
type: "directsecp256k1hdwallet-v1",
kdf: customKdfConfiguration,
encryption: {
algorithm: "xchacha20poly1305-ietf",
},
data: jasmine.stringMatching(testutils_spec_1.base64Matcher),
});
});
});
});
//# sourceMappingURL=directsecp256k1hdwallet.spec.js.map

4

build/index.d.ts
export { Coin, coin, coins, parseCoins } from "@cosmjs/amino";
export { decodeTxRaw, DecodedTxRaw } from "./decode";
export { DecodeObject, EncodeObject, GeneratedType, isTxBodyEncodeObject, isPbjsGeneratedType, isTsProtoGeneratedType, PbjsGeneratedType, Registry, TsProtoGeneratedType, TxBodyEncodeObject, } from "./registry";
export { DirectSecp256k1HdWallet, DirectSecp256k1HdWalletOptions } from "./directsecp256k1hdwallet";
export { extractKdfConfiguration, DirectSecp256k1HdWallet, DirectSecp256k1HdWalletOptions, } from "./directsecp256k1hdwallet";
export { DirectSecp256k1Wallet } from "./directsecp256k1wallet";
export { makeCosmoshubPath } from "./paths";
export { decodePubkey, encodePubkey } from "./pubkey";
export { AccountData, Algo, DirectSignResponse, isOfflineDirectSigner, OfflineDirectSigner, OfflineSigner, } from "./signer";
export { makeAuthInfoBytes, makeSignBytes, makeSignDoc } from "./signing";
export { executeKdf, KdfConfiguration } from "./wallet";
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeSignDoc = exports.makeSignBytes = exports.makeAuthInfoBytes = exports.isOfflineDirectSigner = exports.encodePubkey = exports.decodePubkey = exports.DirectSecp256k1Wallet = exports.DirectSecp256k1HdWallet = exports.Registry = exports.isTsProtoGeneratedType = exports.isPbjsGeneratedType = exports.isTxBodyEncodeObject = exports.decodeTxRaw = exports.parseCoins = exports.coins = exports.coin = void 0;
exports.executeKdf = exports.makeSignDoc = exports.makeSignBytes = exports.makeAuthInfoBytes = exports.isOfflineDirectSigner = exports.encodePubkey = exports.decodePubkey = exports.makeCosmoshubPath = exports.DirectSecp256k1Wallet = exports.DirectSecp256k1HdWallet = exports.extractKdfConfiguration = exports.Registry = exports.isTsProtoGeneratedType = exports.isPbjsGeneratedType = exports.isTxBodyEncodeObject = exports.decodeTxRaw = exports.parseCoins = exports.coins = exports.coin = void 0;
// This type happens to be shared between Amino and Direct sign modes

@@ -17,5 +17,8 @@ var amino_1 = require("@cosmjs/amino");

var directsecp256k1hdwallet_1 = require("./directsecp256k1hdwallet");
Object.defineProperty(exports, "extractKdfConfiguration", { enumerable: true, get: function () { return directsecp256k1hdwallet_1.extractKdfConfiguration; } });
Object.defineProperty(exports, "DirectSecp256k1HdWallet", { enumerable: true, get: function () { return directsecp256k1hdwallet_1.DirectSecp256k1HdWallet; } });
var directsecp256k1wallet_1 = require("./directsecp256k1wallet");
Object.defineProperty(exports, "DirectSecp256k1Wallet", { enumerable: true, get: function () { return directsecp256k1wallet_1.DirectSecp256k1Wallet; } });
var paths_1 = require("./paths");
Object.defineProperty(exports, "makeCosmoshubPath", { enumerable: true, get: function () { return paths_1.makeCosmoshubPath; } });
var pubkey_1 = require("./pubkey");

@@ -30,2 +33,4 @@ Object.defineProperty(exports, "decodePubkey", { enumerable: true, get: function () { return pubkey_1.decodePubkey; } });

Object.defineProperty(exports, "makeSignDoc", { enumerable: true, get: function () { return signing_1.makeSignDoc; } });
var wallet_1 = require("./wallet");
Object.defineProperty(exports, "executeKdf", { enumerable: true, get: function () { return wallet_1.executeKdf; } });
//# sourceMappingURL=index.js.map
{
"name": "@cosmjs/proto-signing",
"version": "0.25.1",
"version": "0.25.2",
"description": "Utilities for protobuf based signing (Cosmos SDK 0.40+)",

@@ -46,3 +46,3 @@ "contributors": [

"dependencies": {
"@cosmjs/amino": "^0.25.1",
"@cosmjs/amino": "^0.25.2",
"long": "^4.0.0",

@@ -52,6 +52,6 @@ "protobufjs": "~6.10.2"

"devDependencies": {
"@cosmjs/encoding": "^0.25.1",
"@cosmjs/utils": "^0.25.1"
"@cosmjs/encoding": "^0.25.2",
"@cosmjs/utils": "^0.25.2"
},
"gitHead": "0dda1b0c4a034c60cb88dad7306c235cfc1ad77f"
"gitHead": "dab008f2f524aab5063fdda3ad510376425dc50a"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc