diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.searchtx.spec.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.searchtx.spec.ts index 36c215d2..0105a81f 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.searchtx.spec.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.searchtx.spec.ts @@ -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(); diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts index b22049f1..b71adff2 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts @@ -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() }; diff --git a/packages/cosmwasm-stargate/src/queries/wasm.spec.ts b/packages/cosmwasm-stargate/src/queries/wasm.spec.ts index 76235aa3..de961f4a 100644 --- a/packages/cosmwasm-stargate/src/queries/wasm.spec.ts +++ b/packages/cosmwasm-stargate/src/queries/wasm.spec.ts @@ -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")]; diff --git a/packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts b/packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts index 422b8645..ea76b5da 100644 --- a/packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts +++ b/packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts @@ -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); diff --git a/packages/cosmwasm-stargate/src/testutils.spec.ts b/packages/cosmwasm-stargate/src/testutils.spec.ts index 9747b2d8..b2b86e3a 100644 --- a/packages/cosmwasm-stargate/src/testutils.spec.ts +++ b/packages/cosmwasm-stargate/src/testutils.spec.ts @@ -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 = {}, ): Promise { + 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 = {}, ): Promise { + 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(