diff --git a/CHANGELOG.md b/CHANGELOG.md index 875719fe..0536d683 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,8 @@ and this project adheres to - @cosmjs/cosmwasm-stargate: Use `CosmWasmFeeTable` instead of `CosmosFeeTable` in `SigningCosmWasmClientOptions`; export type `CosmWasmFeeTable`. +- @cosmjs/amino, @cosmjs/cli, @cosmjs/ledger-amino, @cosmjs/proto-signing: Fix + runtime error caused by passing explicitly undefined options. ## [0.25.0] - 2021-05-05 diff --git a/packages/amino/src/secp256k1hdwallet.spec.ts b/packages/amino/src/secp256k1hdwallet.spec.ts index 92aa78a9..c55085d1 100644 --- a/packages/amino/src/secp256k1hdwallet.spec.ts +++ b/packages/amino/src/secp256k1hdwallet.spec.ts @@ -33,6 +33,18 @@ describe("Secp256k1HdWallet", () => { expect(account.pubkey).not.toEqual(defaultPubkey); expect(account.address.slice(0, 4)).toEqual("yolo"); }); + + it("works with explicitly undefined options", async () => { + const wallet = await Secp256k1HdWallet.fromMnemonic(defaultMnemonic, { + bip39Password: undefined, + hdPaths: undefined, + prefix: undefined, + }); + expect(wallet.mnemonic).toEqual(defaultMnemonic); + const [account] = await wallet.getAccounts(); + expect(account.pubkey).toEqual(defaultPubkey); + expect(account.address).toEqual(defaultAddress); + }); }); describe("generate", () => { diff --git a/packages/amino/src/secp256k1hdwallet.ts b/packages/amino/src/secp256k1hdwallet.ts index 56a65f23..2c224e31 100644 --- a/packages/amino/src/secp256k1hdwallet.ts +++ b/packages/amino/src/secp256k1hdwallet.ts @@ -246,12 +246,13 @@ export class Secp256k1HdWallet implements OfflineAminoSigner { private readonly accounts: readonly DerivationInfo[]; protected constructor(mnemonic: EnglishMnemonic, options: Secp256k1HdWalletConstructorOptions) { - const { seed, hdPaths, prefix } = { ...defaultOptions, ...options }; + const hdPaths = options.hdPaths ?? defaultOptions.hdPaths; + const prefix = options.prefix ?? defaultOptions.prefix; this.secret = mnemonic; - this.seed = seed; + this.seed = options.seed; this.accounts = hdPaths.map((hdPath) => ({ hdPath: hdPath, - prefix: prefix, + prefix, })); } diff --git a/packages/cli/examples/helpers.ts b/packages/cli/examples/helpers.ts index 8b229e7f..29652aad 100644 --- a/packages/cli/examples/helpers.ts +++ b/packages/cli/examples/helpers.ts @@ -30,7 +30,13 @@ const connect = async ( client: SigningCosmWasmClient; address: string; }> => { - const options: Options = { ...defaultOptions, ...opts }; + const options: Options = { + bech32prefix: opts.bech32prefix ?? defaultOptions.bech32prefix, + feeToken: opts.feeToken ?? defaultOptions.feeToken, + gasPrice: opts.gasPrice ?? defaultOptions.gasPrice, + httpUrl: opts.httpUrl ?? defaultOptions.httpUrl, + networkId: opts.networkId ?? defaultOptions.networkId, + }; const gasPrice = GasPrice.fromString(`${options.gasPrice}${options.feeToken}`); const wallet = await Secp256k1HdWallet.fromMnemonic(mnemonic); const [{ address }] = await wallet.getAccounts(); diff --git a/packages/ledger-amino/src/launchpadledger.ts b/packages/ledger-amino/src/launchpadledger.ts index 01eb512a..0e58bd40 100644 --- a/packages/ledger-amino/src/launchpadledger.ts +++ b/packages/ledger-amino/src/launchpadledger.ts @@ -44,13 +44,10 @@ export class LaunchpadLedger { prefix: cosmosBech32Prefix, testModeAllowed: false, }; - const { hdPaths, prefix, testModeAllowed } = { - ...defaultOptions, - ...options, - }; - this.testModeAllowed = testModeAllowed; - this.hdPaths = hdPaths; - this.prefix = prefix; + + this.testModeAllowed = options.testModeAllowed ?? defaultOptions.testModeAllowed; + this.hdPaths = options.hdPaths ?? defaultOptions.hdPaths; + this.prefix = options.prefix ?? defaultOptions.prefix; this.app = new CosmosApp(transport); } diff --git a/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts b/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts index fe580c00..537b9bc9 100644 --- a/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts +++ b/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts @@ -31,6 +31,18 @@ describe("DirectSecp256k1HdWallet", () => { expect(pubkey).not.toEqual(defaultPubkey); expect(address.slice(0, 4)).toEqual("yolo"); }); + + it("works with explicitly undefined options", async () => { + const wallet = await DirectSecp256k1HdWallet.fromMnemonic(defaultMnemonic, { + bip39Password: undefined, + hdPaths: undefined, + prefix: undefined, + }); + expect(wallet.mnemonic).toEqual(defaultMnemonic); + const [{ pubkey, address }] = await wallet.getAccounts(); + expect(pubkey).toEqual(defaultPubkey); + expect(address).toEqual(defaultAddress); + }); }); describe("generate", () => { diff --git a/packages/proto-signing/src/directsecp256k1hdwallet.ts b/packages/proto-signing/src/directsecp256k1hdwallet.ts index e6c993ab..b1e23a82 100644 --- a/packages/proto-signing/src/directsecp256k1hdwallet.ts +++ b/packages/proto-signing/src/directsecp256k1hdwallet.ts @@ -91,9 +91,10 @@ export class DirectSecp256k1HdWallet implements OfflineDirectSigner { private readonly accounts: readonly Secp256k1Derivation[]; protected constructor(mnemonic: EnglishMnemonic, options: DirectSecp256k1HdWalletConstructorOptions) { - const { seed, hdPaths, prefix } = { ...defaultOptions, ...options }; + const prefix = options.prefix ?? defaultOptions.prefix; + const hdPaths = options.hdPaths ?? defaultOptions.hdPaths; this.secret = mnemonic; - this.seed = seed; + this.seed = options.seed; this.accounts = hdPaths.map((hdPath) => ({ hdPath: hdPath, prefix: prefix,