diff --git a/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts b/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts index 1c6e99db..3ffb995f 100644 --- a/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts +++ b/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts @@ -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", () => { diff --git a/packages/proto-signing/src/directsecp256k1hdwallet.ts b/packages/proto-signing/src/directsecp256k1hdwallet.ts index c6c52d89..f2e3853c 100644 --- a/packages/proto-signing/src/directsecp256k1hdwallet.ts +++ b/packages/proto-signing/src/directsecp256k1hdwallet.ts @@ -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 = {}, ): Promise { + 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 */ diff --git a/packages/proto-signing/src/index.ts b/packages/proto-signing/src/index.ts index ea5182c9..ad08bfc9 100644 --- a/packages/proto-signing/src/index.ts +++ b/packages/proto-signing/src/index.ts @@ -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 {