Merge pull request #755 from cosmos/736-wallet-reorg

Rearrange HD wallet options
This commit is contained in:
Will Clark 2021-04-06 16:42:51 +01:00 committed by GitHub
commit 980e33b8a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 226 additions and 90 deletions

View File

@ -56,6 +56,11 @@ and this project adheres to
- @cosmjs/cosmwasm-stargate: Add `SigningCosmWasmClient.offline` static method
for constructing offline clients without a Tendermint client.
- @cosmjs/stargate: Add `SigningStargateClient.sendIbcTokens` method.
- @cosmjs/amino: Export `Secp256k1HdWalletOptions` interface.
- @cosmjs/amino: Add `bip39Password` option to `Secp256k1HdWallet` options.
- @cosmjs/proto-signing: Export `DirectSecp256k1HdWalletOptions` interface.
- @cosmjs/proto-signing: Add `bip39Password` option to `DirectSecp256k1HdWallet`
options.
### Changed
@ -100,6 +105,10 @@ and this project adheres to
account is not found, instead of returning null.
- @cosmjs/cosmwasm-stargate: `CosmWasmClient.getBalance` now returns a 0 balance
instead of null.
- @cosmjs/amino: Options for `Secp256k1HdWallet.fromMnemonic` are now passed via
a `Secp256k1HdWalletOptions` object.
- @cosmjs/proto-signing: Options for `DirectSecp256k1HdWallet.fromMnemonic` are
now passed via a `DirectSecp256k1HdWalletOptions` object.
### Deprecated

View File

@ -21,7 +21,7 @@ export {
} from "./pubkeys";
export { createMultisigThresholdPubkey } from "./multisig";
export { makeCosmoshubPath } from "./paths";
export { extractKdfConfiguration, Secp256k1HdWallet } from "./secp256k1hdwallet";
export { extractKdfConfiguration, Secp256k1HdWallet, Secp256k1HdWalletOptions } from "./secp256k1hdwallet";
export { Secp256k1Wallet } from "./secp256k1wallet";
export { decodeSignature, encodeSecp256k1Signature, StdSignature } from "./signature";
export { AminoMsg, makeSignDoc, serializeSignDoc, StdFee, StdSignDoc } from "./signdoc";

View File

@ -2,6 +2,7 @@
import { Secp256k1, Secp256k1Signature, sha256 } from "@cosmjs/crypto";
import { fromBase64, fromHex } from "@cosmjs/encoding";
import { makeCosmoshubPath } from "./paths";
import { extractKdfConfiguration, Secp256k1HdWallet } from "./secp256k1hdwallet";
import { serializeSignDoc, StdSignDoc } from "./signdoc";
import { base64Matcher } from "./testutils.spec";
@ -20,6 +21,17 @@ describe("Secp256k1HdWallet", () => {
expect(wallet).toBeTruthy();
expect(wallet.mnemonic).toEqual(defaultMnemonic);
});
it("works with options", async () => {
const wallet = await Secp256k1HdWallet.fromMnemonic(defaultMnemonic, {
bip39Password: "password123",
hdPath: makeCosmoshubPath(123),
prefix: "yolo",
});
expect(wallet.mnemonic).toEqual(defaultMnemonic);
expect((wallet as any).pubkey).not.toEqual(defaultPubkey);
expect((wallet as any).address.slice(0, 4)).toEqual("yolo");
});
});
describe("generate", () => {

View File

@ -107,21 +107,38 @@ interface DerivationInfo {
readonly prefix: string;
}
export interface Secp256k1HdWalletOptions {
/** The password to use when deriving a BIP39 seed from a mnemonic. */
readonly bip39Password: string;
/** The BIP-32/SLIP-10 derivation path. Defaults to the Cosmos Hub/ATOM path `m/44'/118'/0'/0/0`. */
readonly hdPath: HdPath;
/** The bech32 address prefix (human readable part). Defaults to "cosmos". */
readonly prefix: string;
}
const defaultOptions: Secp256k1HdWalletOptions = {
bip39Password: "",
hdPath: makeCosmoshubPath(0),
prefix: "cosmos",
};
export class Secp256k1HdWallet implements OfflineAminoSigner {
/**
* Restores a wallet from the given BIP39 mnemonic.
*
* @param mnemonic Any valid English mnemonic.
* @param hdPath The BIP-32/SLIP-10 derivation path. Defaults to the Cosmos Hub/ATOM path `m/44'/118'/0'/0/0`.
* @param prefix The bech32 address prefix (human readable part). Defaults to "cosmos".
* @param options An optional `Secp256k1HdWalletOptions` object optionally containing a bip39Password, hdPath, and prefix.
*/
public static async fromMnemonic(
mnemonic: string,
hdPath: HdPath = makeCosmoshubPath(0),
prefix = "cosmos",
options: Partial<Secp256k1HdWalletOptions> = {},
): Promise<Secp256k1HdWallet> {
const { bip39Password, hdPath, prefix } = {
...defaultOptions,
...options,
};
const mnemonicChecked = new EnglishMnemonic(mnemonic);
const seed = await Bip39.mnemonicToSeed(mnemonicChecked);
const seed = await Bip39.mnemonicToSeed(mnemonicChecked, bip39Password);
const { privkey } = Slip10.derivePath(Slip10Curve.Secp256k1, seed, hdPath);
const uncompressed = (await Secp256k1.makeKeypair(privkey)).pubkey;
return new Secp256k1HdWallet(
@ -148,7 +165,7 @@ export class Secp256k1HdWallet implements OfflineAminoSigner {
const entropyLength = 4 * Math.floor((11 * length) / 33);
const entropy = Random.getBytes(entropyLength);
const mnemonic = Bip39.encode(entropy);
return Secp256k1HdWallet.fromMnemonic(mnemonic.toString(), hdPath, prefix);
return Secp256k1HdWallet.fromMnemonic(mnemonic.toString(), { hdPath: hdPath, prefix: prefix });
}
/**
@ -198,7 +215,10 @@ export class Secp256k1HdWallet implements OfflineAminoSigner {
if (accounts.length !== 1) throw new Error("Property 'accounts' only supports one entry");
const account = accounts[0];
if (!isDerivationJson(account)) throw new Error("Account is not in the correct format.");
return Secp256k1HdWallet.fromMnemonic(mnemonic, stringToPath(account.hdPath), account.prefix);
return Secp256k1HdWallet.fromMnemonic(mnemonic, {
hdPath: stringToPath(account.hdPath),
prefix: account.prefix,
});
}
default:
throw new Error("Unsupported serialization type");

View File

@ -5,7 +5,7 @@ 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";
for (let i of [0, 1, 2, 3, 4]) {
const wallet = await Secp256k1HdWallet.fromMnemonic(mnemonic, makeCosmoshubPath(i), "cosmos");
const wallet = await Secp256k1HdWallet.fromMnemonic(mnemonic, { hdPath: makeCosmoshubPath(i) });
const [{ address, pubkey }] = await wallet.getAccounts();
console.info(`Address ${i}: ${address}`);
console.info(`Pubkey ${i}: ${toBase64(pubkey)}`);

View File

@ -111,7 +111,7 @@ export async function main(originalArgs: readonly string[]): Promise<void> {
assert(accounts[0].address == "cosmos1kxt5x5q2l57ma2d434pqpafxdm0mgeg9c8cvtx");
const mnemonic = Bip39.encode(Random.getBytes(16)).toString();
const wallet = await Secp256k1HdWallet.fromMnemonic(mnemonic, makeCosmoshubPath(0));
const wallet = await Secp256k1HdWallet.fromMnemonic(mnemonic, { hdPath: makeCosmoshubPath(0) });
const [{ address }] = await wallet.getAccounts();
const data = toAscii("foo bar");
const fee: StdFee = {

View File

@ -247,7 +247,9 @@ describe("Cw3CosmWasmClient", () => {
proposerWallet,
contractAddress,
);
const voterWallet = await Secp256k1HdWallet.fromMnemonic(alice.mnemonic, makeCosmoshubPath(1));
const voterWallet = await Secp256k1HdWallet.fromMnemonic(alice.mnemonic, {
hdPath: makeCosmoshubPath(1),
});
const voter = new Cw3CosmWasmClient(launchpad.endpoint, alice.address1, voterWallet, contractAddress);
const toAddress = makeRandomAddress();
const msg = {
@ -292,9 +294,13 @@ describe("Cw3CosmWasmClient", () => {
proposerWallet,
contractAddress,
);
const voter1Wallet = await Secp256k1HdWallet.fromMnemonic(alice.mnemonic, makeCosmoshubPath(1));
const voter1Wallet = await Secp256k1HdWallet.fromMnemonic(alice.mnemonic, {
hdPath: makeCosmoshubPath(1),
});
const voter1 = new Cw3CosmWasmClient(launchpad.endpoint, alice.address1, voter1Wallet, contractAddress);
const voter2Wallet = await Secp256k1HdWallet.fromMnemonic(alice.mnemonic, makeCosmoshubPath(2));
const voter2Wallet = await Secp256k1HdWallet.fromMnemonic(alice.mnemonic, {
hdPath: makeCosmoshubPath(2),
});
const voter2 = new Cw3CosmWasmClient(launchpad.endpoint, alice.address2, voter2Wallet, contractAddress);
const toAddress = makeRandomAddress();
const msg = {

View File

@ -102,7 +102,7 @@ describe("CosmWasmClient.getTx and .searchTx", () => {
beforeAll(async () => {
if (wasmdEnabled()) {
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const client = await CosmWasmClient.connect(wasmd.endpoint);
const unsuccessfulRecipient = makeRandomAddress();
const successfulRecipient = makeRandomAddress();

View File

@ -168,7 +168,7 @@ describe("CosmWasmClient", () => {
describe("broadcastTx", () => {
it("works", async () => {
pendingWithoutWasmd();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const client = await CosmWasmClient.connect(wasmd.endpoint);
const registry = new Registry();
@ -336,7 +336,7 @@ describe("CosmWasmClient", () => {
beforeAll(async () => {
if (wasmdEnabled()) {
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet);
const { codeId } = await client.upload(alice.address0, getHackatom().data);
const initMsg = { verifier: makeRandomAddress(), beneficiary: makeRandomAddress() };
@ -390,7 +390,7 @@ describe("CosmWasmClient", () => {
beforeAll(async () => {
if (wasmdEnabled()) {
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet);
const { codeId } = await client.upload(alice.address0, getHackatom().data);
const initMsg = { verifier: makeRandomAddress(), beneficiary: makeRandomAddress() };

View File

@ -133,7 +133,7 @@ describe("WasmExtension", () => {
beforeAll(async () => {
if (wasmdEnabled()) {
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, "wasm");
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const result = await uploadContract(wallet, hackatom);
assertIsBroadcastTxSuccess(result);
hackatomCodeId = Number.parseInt(
@ -187,7 +187,7 @@ describe("WasmExtension", () => {
it("works", async () => {
pendingWithoutWasmd();
assert(hackatomCodeId);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, "wasm");
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const client = await makeWasmClient(wasmd.endpoint);
const beneficiaryAddress = makeRandomAddress();
const transferAmount = coins(707707, "ucosm");
@ -248,7 +248,7 @@ describe("WasmExtension", () => {
it("can list contract history", async () => {
pendingWithoutWasmd();
assert(hackatomCodeId);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, "wasm");
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const client = await makeWasmClient(wasmd.endpoint);
const beneficiaryAddress = makeRandomAddress();
const transferAmount = coins(707707, "ucosm");
@ -376,7 +376,7 @@ describe("WasmExtension", () => {
describe("broadcastTx", () => {
it("can upload, instantiate and execute wasm", async () => {
pendingWithoutWasmd();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const client = await makeWasmClient(wasmd.endpoint);
const transferAmount = [coin(1234, "ucosm"), coin(321, "ustake")];

View File

@ -40,7 +40,7 @@ describe("SigningCosmWasmClient", () => {
describe("connectWithSigner", () => {
it("can be constructed", async () => {
pendingWithoutWasmd();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const options = { prefix: wasmd.prefix };
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, options);
expect(client).toBeTruthy();
@ -59,7 +59,7 @@ describe("SigningCosmWasmClient", () => {
it("can be constructed with custom gas price", async () => {
pendingWithoutWasmd();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const options = {
prefix: wasmd.prefix,
gasPrice: GasPrice.fromString("3.14utest"),
@ -112,7 +112,7 @@ describe("SigningCosmWasmClient", () => {
it("can be constructed with custom gas limits", async () => {
pendingWithoutWasmd();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const options = {
prefix: wasmd.prefix,
gasLimits: {
@ -167,7 +167,7 @@ describe("SigningCosmWasmClient", () => {
it("can be constructed with custom gas price and gas limits", async () => {
pendingWithoutWasmd();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const options = {
prefix: wasmd.prefix,
gasPrice: GasPrice.fromString("3.14utest"),
@ -225,7 +225,7 @@ describe("SigningCosmWasmClient", () => {
describe("upload", () => {
it("works", async () => {
pendingWithoutWasmd();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const options = { prefix: wasmd.prefix };
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, options);
const wasm = getHackatom().data;
@ -245,7 +245,7 @@ describe("SigningCosmWasmClient", () => {
it("can set builder and source", async () => {
pendingWithoutWasmd();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const options = { prefix: wasmd.prefix };
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, options);
const hackatom = getHackatom();
@ -263,7 +263,7 @@ describe("SigningCosmWasmClient", () => {
describe("instantiate", () => {
it("works with transfer amount", async () => {
pendingWithoutWasmd();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const options = { prefix: wasmd.prefix };
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, options);
const { codeId } = await client.upload(alice.address0, getHackatom().data);
@ -291,7 +291,7 @@ describe("SigningCosmWasmClient", () => {
it("works with admin", async () => {
pendingWithoutWasmd();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const options = { prefix: wasmd.prefix };
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, options);
const { codeId } = await client.upload(alice.address0, getHackatom().data);
@ -314,7 +314,7 @@ describe("SigningCosmWasmClient", () => {
it("can instantiate one code multiple times", async () => {
pendingWithoutWasmd();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const options = { prefix: wasmd.prefix };
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, options);
const { codeId } = await client.upload(alice.address0, getHackatom().data);
@ -343,7 +343,7 @@ describe("SigningCosmWasmClient", () => {
describe("updateAdmin", () => {
it("can update an admin", async () => {
pendingWithoutWasmd();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const options = { prefix: wasmd.prefix };
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, options);
const { codeId } = await client.upload(alice.address0, getHackatom().data);
@ -375,7 +375,7 @@ describe("SigningCosmWasmClient", () => {
describe("clearAdmin", () => {
it("can clear an admin", async () => {
pendingWithoutWasmd();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const options = { prefix: wasmd.prefix };
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, options);
const { codeId } = await client.upload(alice.address0, getHackatom().data);
@ -407,7 +407,7 @@ describe("SigningCosmWasmClient", () => {
describe("migrate", () => {
it("can can migrate from one code ID to another", async () => {
pendingWithoutWasmd();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const options = { prefix: wasmd.prefix };
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, options);
const { codeId: codeId1 } = await client.upload(alice.address0, getHackatom().data);
@ -444,7 +444,7 @@ describe("SigningCosmWasmClient", () => {
describe("execute", () => {
it("works", async () => {
pendingWithoutWasmd();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const options = { prefix: wasmd.prefix };
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, options);
const { codeId } = await client.upload(alice.address0, getHackatom().data);
@ -488,7 +488,7 @@ describe("SigningCosmWasmClient", () => {
describe("sendTokens", () => {
it("works with direct signer", async () => {
pendingWithoutWasmd();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const options = { prefix: wasmd.prefix };
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, options);
@ -516,7 +516,7 @@ describe("SigningCosmWasmClient", () => {
it("works with legacy Amino signer", async () => {
pendingWithoutWasmd();
const wallet = await Secp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await Secp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const options = { prefix: wasmd.prefix };
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, options);
@ -547,7 +547,7 @@ describe("SigningCosmWasmClient", () => {
describe("direct mode", () => {
it("works", async () => {
pendingWithoutWasmd();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const msgDelegateTypeUrl = "/cosmos.staking.v1beta1.MsgDelegate";
const registry = new Registry();
registry.register(msgDelegateTypeUrl, MsgDelegate);
@ -574,11 +574,9 @@ describe("SigningCosmWasmClient", () => {
it("works with a modifying signer", async () => {
pendingWithoutWasmd();
const wallet = await ModifyingDirectSecp256k1HdWallet.fromMnemonic(
alice.mnemonic,
undefined,
wasmd.prefix,
);
const wallet = await ModifyingDirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, {
prefix: wasmd.prefix,
});
const msgDelegateTypeUrl = "/cosmos.staking.v1beta1.MsgDelegate";
const registry = new Registry();
registry.register(msgDelegateTypeUrl, MsgDelegate);
@ -617,7 +615,7 @@ describe("SigningCosmWasmClient", () => {
describe("legacy Amino mode", () => {
it("works with bank MsgSend", async () => {
pendingWithoutWasmd();
const wallet = await Secp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await Secp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const options = { prefix: wasmd.prefix };
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, options);
@ -641,7 +639,7 @@ describe("SigningCosmWasmClient", () => {
it("works with staking MsgDelegate", async () => {
pendingWithoutWasmd();
const wallet = await Secp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await Secp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const options = { prefix: wasmd.prefix };
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, options);
@ -665,7 +663,7 @@ describe("SigningCosmWasmClient", () => {
it("works with wasm MsgStoreCode", async () => {
pendingWithoutWasmd();
const wallet = await Secp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await Secp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const options = { prefix: wasmd.prefix };
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, options);
const { data, builder, source } = getHackatom();
@ -692,7 +690,7 @@ describe("SigningCosmWasmClient", () => {
it("works with a custom registry and custom message", async () => {
pendingWithoutWasmd();
const wallet = await Secp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, wasmd.prefix);
const wallet = await Secp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const customRegistry = new Registry();
const msgDelegateTypeUrl = "/cosmos.staking.v1beta1.MsgDelegate";
@ -810,7 +808,9 @@ describe("SigningCosmWasmClient", () => {
it("works with a modifying signer", async () => {
pendingWithoutWasmd();
const wallet = await ModifyingSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, "wasm");
const wallet = await ModifyingSecp256k1HdWallet.fromMnemonic(alice.mnemonic, {
prefix: wasmd.prefix,
});
const options = { prefix: wasmd.prefix };
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, options);

View File

@ -1,8 +1,19 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { AminoSignResponse, makeCosmoshubPath, Secp256k1HdWallet, StdSignDoc } from "@cosmjs/amino";
import {
AminoSignResponse,
makeCosmoshubPath,
Secp256k1HdWallet,
Secp256k1HdWalletOptions,
StdSignDoc,
} from "@cosmjs/amino";
import { Bip39, EnglishMnemonic, Random, Secp256k1, Slip10, Slip10Curve } from "@cosmjs/crypto";
import { Bech32, fromBase64 } from "@cosmjs/encoding";
import { DirectSecp256k1HdWallet, DirectSignResponse, makeAuthInfoBytes } from "@cosmjs/proto-signing";
import {
DirectSecp256k1HdWallet,
DirectSecp256k1HdWalletOptions,
DirectSignResponse,
makeAuthInfoBytes,
} from "@cosmjs/proto-signing";
import {
AuthExtension,
BankExtension,
@ -192,17 +203,23 @@ export async function makeWasmClient(
return QueryClient.withExtensions(tmClient, setupAuthExtension, setupBankExtension, setupWasmExtension);
}
const defaultHdWalletOptions = {
bip39Password: "",
hdPath: makeCosmoshubPath(0),
prefix: "cosmos",
};
/**
* A class for testing clients using an Amino signer which modifies the transaction it receives before signing
*/
export class ModifyingSecp256k1HdWallet extends Secp256k1HdWallet {
public static async fromMnemonic(
mnemonic: string,
hdPath = makeCosmoshubPath(0),
prefix = "cosmos",
options: Partial<Secp256k1HdWalletOptions> = {},
): Promise<ModifyingSecp256k1HdWallet> {
const { bip39Password, hdPath, prefix } = { ...defaultHdWalletOptions, ...options };
const mnemonicChecked = new EnglishMnemonic(mnemonic);
const seed = await Bip39.mnemonicToSeed(mnemonicChecked);
const seed = await Bip39.mnemonicToSeed(mnemonicChecked, bip39Password);
const { privkey } = Slip10.derivePath(Slip10Curve.Secp256k1, seed, hdPath);
const uncompressed = (await Secp256k1.makeKeypair(privkey)).pubkey;
return new ModifyingSecp256k1HdWallet(
@ -233,11 +250,11 @@ export class ModifyingSecp256k1HdWallet extends Secp256k1HdWallet {
export class ModifyingDirectSecp256k1HdWallet extends DirectSecp256k1HdWallet {
public static async fromMnemonic(
mnemonic: string,
hdPath = makeCosmoshubPath(0),
prefix = "cosmos",
options: Partial<DirectSecp256k1HdWalletOptions> = {},
): Promise<DirectSecp256k1HdWallet> {
const { bip39Password, hdPath, prefix } = { ...defaultHdWalletOptions, ...options };
const mnemonicChecked = new EnglishMnemonic(mnemonic);
const seed = await Bip39.mnemonicToSeed(mnemonicChecked);
const seed = await Bip39.mnemonicToSeed(mnemonicChecked, bip39Password);
const { privkey } = Slip10.derivePath(Slip10Curve.Secp256k1, seed, hdPath);
const uncompressed = (await Secp256k1.makeKeypair(privkey)).pubkey;
return new ModifyingDirectSecp256k1HdWallet(

View File

@ -19,7 +19,7 @@ export async function createWallets(
const numberOfIdentities = 1 + numberOfDistributors;
for (let i = 0; i < numberOfIdentities; i++) {
const path = makeCosmoshubPath(i);
const wallet = await createWallet(mnemonic, path, addressPrefix);
const wallet = await createWallet(mnemonic, { hdPath: path, prefix: addressPrefix });
const [{ address }] = await wallet.getAccounts();
if (logging) {
const role = i === 0 ? "token holder " : `distributor ${i}`;

View File

@ -547,9 +547,15 @@ describe("LcdClient", () => {
it("can't send transaction with additional signatures", async () => {
pendingWithoutLaunchpad();
const account1 = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(0));
const account2 = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(1));
const account3 = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(2));
const account1 = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, {
hdPath: makeCosmoshubPath(0),
});
const account2 = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, {
hdPath: makeCosmoshubPath(1),
});
const account3 = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, {
hdPath: makeCosmoshubPath(2),
});
const [address1, address2, address3] = await Promise.all(
[account1, account2, account3].map(async (wallet) => {
return (await wallet.getAccounts())[0].address;
@ -605,7 +611,7 @@ describe("LcdClient", () => {
it("can send multiple messages with one signature", async () => {
pendingWithoutLaunchpad();
const wallet = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(0));
const wallet = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, { hdPath: makeCosmoshubPath(0) });
const accounts = await wallet.getAccounts();
const [{ address: walletAddress }] = accounts;
@ -659,8 +665,12 @@ describe("LcdClient", () => {
it("can send multiple messages with multiple signatures", async () => {
pendingWithoutLaunchpad();
const account1 = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(0));
const account2 = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(1));
const account1 = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, {
hdPath: makeCosmoshubPath(0),
});
const account2 = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, {
hdPath: makeCosmoshubPath(1),
});
const [address1, address2] = await Promise.all(
[account1, account2].map(async (wallet) => {
return (await wallet.getAccounts())[0].address;
@ -730,8 +740,12 @@ describe("LcdClient", () => {
it("can't send transaction with wrong signature order (1)", async () => {
pendingWithoutLaunchpad();
const account1 = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(0));
const account2 = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(1));
const account1 = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, {
hdPath: makeCosmoshubPath(0),
});
const account2 = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, {
hdPath: makeCosmoshubPath(1),
});
const [address1, address2] = await Promise.all(
[account1, account2].map(async (wallet) => {
return (await wallet.getAccounts())[0].address;
@ -796,8 +810,12 @@ describe("LcdClient", () => {
it("can't send transaction with wrong signature order (2)", async () => {
pendingWithoutLaunchpad();
const account1 = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(0));
const account2 = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(1));
const account1 = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, {
hdPath: makeCosmoshubPath(0),
});
const account2 = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, {
hdPath: makeCosmoshubPath(1),
});
const [address1, address2] = await Promise.all(
[account1, account2].map(async (wallet) => {
return (await wallet.getAccounts())[0].address;

View File

@ -225,8 +225,8 @@ describe("SigningCosmosClient", () => {
describe("appendSignature", () => {
it("works", async () => {
pendingWithoutLaunchpad();
const wallet0 = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(0));
const wallet1 = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(1));
const wallet0 = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, { hdPath: makeCosmoshubPath(0) });
const wallet1 = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, { hdPath: makeCosmoshubPath(1) });
const client0 = new SigningCosmosClient(launchpad.endpoint, faucet.address0, wallet0);
const client1 = new SigningCosmosClient(launchpad.endpoint, faucet.address1, wallet1);

View File

@ -1,4 +1,4 @@
import { coins } from "@cosmjs/amino";
import { coins, makeCosmoshubPath } from "@cosmjs/amino";
import { Secp256k1, Secp256k1Signature, sha256 } from "@cosmjs/crypto";
import { fromBase64, fromHex } from "@cosmjs/encoding";
@ -19,6 +19,17 @@ describe("DirectSecp256k1HdWallet", () => {
expect(wallet).toBeTruthy();
expect(wallet.mnemonic).toEqual(defaultMnemonic);
});
it("works with options", async () => {
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(defaultMnemonic, {
bip39Password: "password123",
hdPath: makeCosmoshubPath(123),
prefix: "yolo",
});
expect(wallet.mnemonic).toEqual(defaultMnemonic);
expect((wallet as any).pubkey).not.toEqual(defaultPubkey);
expect((wallet as any).address.slice(0, 4)).toEqual("yolo");
});
});
describe("generate", () => {

View File

@ -23,22 +23,39 @@ interface Secp256k1Derivation {
readonly prefix: string;
}
export interface DirectSecp256k1HdWalletOptions {
/** The password to use when deriving a BIP39 seed from a mnemonic. */
readonly bip39Password: string;
/** The BIP-32/SLIP-10 derivation path. Defaults to the Cosmos Hub/ATOM path `m/44'/118'/0'/0/0`. */
readonly hdPath: HdPath;
/** The bech32 address prefix (human readable part). Defaults to "cosmos". */
readonly prefix: string;
}
const defaultOptions: DirectSecp256k1HdWalletOptions = {
bip39Password: "",
hdPath: makeCosmoshubPath(0),
prefix: "cosmos",
};
/** A wallet for protobuf based signing using SIGN_MODE_DIRECT */
export class DirectSecp256k1HdWallet implements OfflineDirectSigner {
/**
* Restores a wallet from the given BIP39 mnemonic.
*
* @param mnemonic Any valid English mnemonic.
* @param hdPath The BIP-32/SLIP-10 derivation path. Defaults to the Cosmos Hub/ATOM path `m/44'/118'/0'/0/0`.
* @param prefix The bech32 address prefix (human readable part). Defaults to "cosmos".
* @param options An optional `DirectSecp256k1HdWalletOptions` object optionally containing a bip39Password, hdPath, and prefix.
*/
public static async fromMnemonic(
mnemonic: string,
hdPath: HdPath = makeCosmoshubPath(0),
prefix = "cosmos",
options: Partial<DirectSecp256k1HdWalletOptions> = {},
): Promise<DirectSecp256k1HdWallet> {
const { bip39Password, hdPath, prefix } = {
...defaultOptions,
...options,
};
const mnemonicChecked = new EnglishMnemonic(mnemonic);
const seed = await Bip39.mnemonicToSeed(mnemonicChecked);
const seed = await Bip39.mnemonicToSeed(mnemonicChecked, bip39Password);
const { privkey } = Slip10.derivePath(Slip10Curve.Secp256k1, seed, hdPath);
const uncompressed = (await Secp256k1.makeKeypair(privkey)).pubkey;
return new DirectSecp256k1HdWallet(
@ -65,7 +82,7 @@ export class DirectSecp256k1HdWallet implements OfflineDirectSigner {
const entropyLength = 4 * Math.floor((11 * length) / 33);
const entropy = Random.getBytes(entropyLength);
const mnemonic = Bip39.encode(entropy);
return DirectSecp256k1HdWallet.fromMnemonic(mnemonic.toString(), hdPath, prefix);
return DirectSecp256k1HdWallet.fromMnemonic(mnemonic.toString(), { hdPath: hdPath, prefix: prefix });
}
/** Base secret */

View File

@ -10,7 +10,7 @@ export {
TsProtoGeneratedType,
PbjsGeneratedType,
} from "./registry";
export { DirectSecp256k1HdWallet } from "./directsecp256k1hdwallet";
export { DirectSecp256k1HdWallet, DirectSecp256k1HdWalletOptions } from "./directsecp256k1hdwallet";
export { DirectSecp256k1Wallet } from "./directsecp256k1wallet";
export { decodePubkey, encodePubkey } from "./pubkey";
export {

View File

@ -214,7 +214,9 @@ describe("multisignature", () => {
] = await Promise.all(
[0, 1, 2, 3, 4].map(async (i) => {
// Signing environment
const wallet = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, makeCosmoshubPath(i));
const wallet = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic, {
hdPath: makeCosmoshubPath(i),
});
const pubkey = encodeSecp256k1Pubkey((await wallet.getAccounts())[0].pubkey);
const address = (await wallet.getAccounts())[0].address;
const signingClient = await SigningStargateClient.offline(wallet);

View File

@ -1,8 +1,20 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { AminoSignResponse, makeCosmoshubPath, Secp256k1HdWallet, StdSignDoc } from "@cosmjs/amino";
import {
AminoSignResponse,
makeCosmoshubPath,
Secp256k1HdWallet,
Secp256k1HdWalletOptions,
StdSignDoc,
} from "@cosmjs/amino";
import { Bip39, EnglishMnemonic, Random, Secp256k1, Slip10, Slip10Curve } from "@cosmjs/crypto";
import { Bech32 } from "@cosmjs/encoding";
import { coins, DirectSecp256k1HdWallet, DirectSignResponse, makeAuthInfoBytes } from "@cosmjs/proto-signing";
import {
coins,
DirectSecp256k1HdWallet,
DirectSecp256k1HdWalletOptions,
DirectSignResponse,
makeAuthInfoBytes,
} from "@cosmjs/proto-signing";
import { SignMode } from "./codec/cosmos/tx/signing/v1beta1/signing";
import { AuthInfo, SignDoc, TxBody } from "./codec/cosmos/tx/v1beta1/tx";
@ -122,17 +134,26 @@ export const nonExistentAddress = "cosmos1p79apjaufyphcmsn4g07cynqf0wyjuezqu84hd
export const nonNegativeIntegerMatcher = /^[0-9]+$/;
export const tendermintIdMatcher = /^[0-9A-F]{64}$/;
const defaultHdWalletOptions = {
bip39Password: "",
hdPath: makeCosmoshubPath(0),
prefix: "cosmos",
};
/**
* A class for testing clients using an Amino signer which modifies the transaction it receives before signing
*/
export class ModifyingSecp256k1HdWallet extends Secp256k1HdWallet {
public static async fromMnemonic(
mnemonic: string,
hdPath = makeCosmoshubPath(0),
prefix = "cosmos",
options: Partial<Secp256k1HdWalletOptions> = {},
): Promise<ModifyingSecp256k1HdWallet> {
const { bip39Password, hdPath, prefix } = {
...defaultHdWalletOptions,
...options,
};
const mnemonicChecked = new EnglishMnemonic(mnemonic);
const seed = await Bip39.mnemonicToSeed(mnemonicChecked);
const seed = await Bip39.mnemonicToSeed(mnemonicChecked, bip39Password);
const { privkey } = Slip10.derivePath(Slip10Curve.Secp256k1, seed, hdPath);
const uncompressed = (await Secp256k1.makeKeypair(privkey)).pubkey;
return new ModifyingSecp256k1HdWallet(
@ -163,11 +184,11 @@ export class ModifyingSecp256k1HdWallet extends Secp256k1HdWallet {
export class ModifyingDirectSecp256k1HdWallet extends DirectSecp256k1HdWallet {
public static async fromMnemonic(
mnemonic: string,
hdPath = makeCosmoshubPath(0),
prefix = "cosmos",
options: Partial<DirectSecp256k1HdWalletOptions> = {},
): Promise<DirectSecp256k1HdWallet> {
const { bip39Password, hdPath, prefix } = { ...defaultHdWalletOptions, ...options };
const mnemonicChecked = new EnglishMnemonic(mnemonic);
const seed = await Bip39.mnemonicToSeed(mnemonicChecked);
const seed = await Bip39.mnemonicToSeed(mnemonicChecked, bip39Password);
const { privkey } = Slip10.derivePath(Slip10Curve.Secp256k1, seed, hdPath);
const uncompressed = (await Secp256k1.makeKeypair(privkey)).pubkey;
return new ModifyingDirectSecp256k1HdWallet(

View File

@ -21,7 +21,7 @@ const codeMeta = {
};
async function main() {
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, "wasm");
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: "wasm" });
const client = await SigningCosmWasmClient.connectWithSigner(endpoint, wallet);
const wasm = fs.readFileSync(__dirname + "/contracts/cw1_subkeys.wasm");

View File

@ -63,7 +63,7 @@ const initData = [
];
async function main() {
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, "wasm");
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: "wasm" });
const client = await SigningCosmWasmClient.connectWithSigner(endpoint, wallet);
const wasm = fs.readFileSync(__dirname + "/contracts/cw3_fixed_multisig.wasm");

View File

@ -48,7 +48,7 @@ const inits = [
];
async function main() {
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, undefined, "wasm");
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: "wasm" });
const client = await SigningCosmWasmClient.connectWithSigner(endpoint, wallet);
const wasm = fs.readFileSync(__dirname + "/contracts/hackatom.wasm");

View File

@ -37,7 +37,10 @@ async function main() {
for (const { mnemonic, accountNumbers } of accountsToCreate) {
const wallets = await Promise.all(
accountNumbers.map((accountNumber) =>
Secp256k1HdWallet.fromMnemonic(mnemonic, makeCosmoshubPath(accountNumber), prefix),
Secp256k1HdWallet.fromMnemonic(mnemonic, {
hdPath: makeCosmoshubPath(accountNumber),
prefix: prefix,
}),
),
);
const accounts = (await Promise.all(wallets.map((wallet) => wallet.getAccounts()))).map(

View File

@ -16,7 +16,7 @@ const faucet = {
};
async function main() {
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic, undefined, prefix);
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic, { prefix: prefix });
const client = await SigningStargateClient.connectWithSigner(rpcUrl, wallet);
const recipient = Bech32.encode(prefix, Random.getBytes(20));
const amount = coins(226644, "ucosm");