From 353f2da4a850682cc6a38ce14d57e2f72eb8dbd6 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 26 Jan 2022 16:38:14 +0100 Subject: [PATCH] Make prefix required in AminoTypes constructor --- CHANGELOG.md | 9 +++ .../cosmwasm-stargate/src/aminotypes.spec.ts | 28 +++---- .../src/signingcosmwasmclient.spec.ts | 2 + .../src/signingcosmwasmclient.ts | 4 +- packages/stargate/src/aminotypes.spec.ts | 73 ++++++++++--------- packages/stargate/src/aminotypes.ts | 9 ++- packages/stargate/src/index.ts | 2 +- .../src/signingstargateclient.spec.ts | 2 + .../stargate/src/signingstargateclient.ts | 5 +- 9 files changed, 80 insertions(+), 54 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 836eead899..9a52196cf3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,15 @@ and this project adheres to ## [Unreleased] +### Changed + +- @cosmjs/stargate: The `AminoTypes` now always requires an argument of type + `AminoTypesOptions`. This is an object with a required `prefix` field. Before + the prefix defaulted to "cosmos" but this is almost never the right choice for + CosmJS users that need to add Amino types manually. ([#989]) + +[#989]: https://github.com/cosmos/cosmjs/issues/989 + ### Removed - @cosmjs/crypto: Remove the SHA1 implementation (`Sha1` and `sha1`) as it is diff --git a/packages/cosmwasm-stargate/src/aminotypes.spec.ts b/packages/cosmwasm-stargate/src/aminotypes.spec.ts index 8573935367..6e92eb7263 100644 --- a/packages/cosmwasm-stargate/src/aminotypes.spec.ts +++ b/packages/cosmwasm-stargate/src/aminotypes.spec.ts @@ -29,7 +29,7 @@ describe("AminoTypes", () => { wasmByteCode: fromBase64("WUVMTE9XIFNVQk1BUklORQ=="), instantiatePermission: undefined, }; - const aminoMsg = new AminoTypes({ additions: cosmWasmTypes }).toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos", additions: cosmWasmTypes }).toAmino({ typeUrl: "/cosmwasm.wasm.v1.MsgStoreCode", value: msg, }); @@ -54,7 +54,7 @@ describe("AminoTypes", () => { funds: coins(1234, "ucosm"), admin: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", }; - const aminoMsg = new AminoTypes({ additions: cosmWasmTypes }).toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos", additions: cosmWasmTypes }).toAmino({ typeUrl: "/cosmwasm.wasm.v1.MsgInstantiateContract", value: msg, }); @@ -82,7 +82,7 @@ describe("AminoTypes", () => { funds: coins(1234, "ucosm"), admin: "", }; - const aminoMsg = new AminoTypes({ additions: cosmWasmTypes }).toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos", additions: cosmWasmTypes }).toAmino({ typeUrl: "/cosmwasm.wasm.v1.MsgInstantiateContract", value: msg, }); @@ -107,7 +107,7 @@ describe("AminoTypes", () => { newAdmin: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", contract: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", }; - const aminoMsg = new AminoTypes({ additions: cosmWasmTypes }).toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos", additions: cosmWasmTypes }).toAmino({ typeUrl: "/cosmwasm.wasm.v1.MsgUpdateAdmin", value: msg, }); @@ -127,7 +127,7 @@ describe("AminoTypes", () => { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", contract: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", }; - const aminoMsg = new AminoTypes({ additions: cosmWasmTypes }).toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos", additions: cosmWasmTypes }).toAmino({ typeUrl: "/cosmwasm.wasm.v1.MsgClearAdmin", value: msg, }); @@ -148,7 +148,7 @@ describe("AminoTypes", () => { msg: toUtf8(`{"foo":"bar"}`), funds: coins(1234, "ucosm"), }; - const aminoMsg = new AminoTypes({ additions: cosmWasmTypes }).toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos", additions: cosmWasmTypes }).toAmino({ typeUrl: "/cosmwasm.wasm.v1.MsgExecuteContract", value: msg, }); @@ -171,7 +171,7 @@ describe("AminoTypes", () => { codeId: Long.fromString("98765"), msg: toUtf8(`{"foo":"bar"}`), }; - const aminoMsg = new AminoTypes({ additions: cosmWasmTypes }).toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos", additions: cosmWasmTypes }).toAmino({ typeUrl: "/cosmwasm.wasm.v1.MsgMigrateContract", value: msg, }); @@ -197,7 +197,7 @@ describe("AminoTypes", () => { wasm_byte_code: "WUVMTE9XIFNVQk1BUklORQ==", }, }; - const msg = new AminoTypes({ additions: cosmWasmTypes }).fromAmino(aminoMsg); + const msg = new AminoTypes({ prefix: "cosmos", additions: cosmWasmTypes }).fromAmino(aminoMsg); const expectedValue: MsgStoreCode = { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", wasmByteCode: fromBase64("WUVMTE9XIFNVQk1BUklORQ=="), @@ -223,7 +223,7 @@ describe("AminoTypes", () => { admin: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", }, }; - const msg = new AminoTypes({ additions: cosmWasmTypes }).fromAmino(aminoMsg); + const msg = new AminoTypes({ prefix: "cosmos", additions: cosmWasmTypes }).fromAmino(aminoMsg); const expectedValue: MsgInstantiateContract = { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", codeId: Long.fromString("12345"), @@ -250,7 +250,7 @@ describe("AminoTypes", () => { funds: coins(1234, "ucosm"), }, }; - const msg = new AminoTypes({ additions: cosmWasmTypes }).fromAmino(aminoMsg); + const msg = new AminoTypes({ prefix: "cosmos", additions: cosmWasmTypes }).fromAmino(aminoMsg); const expectedValue: MsgInstantiateContract = { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", codeId: Long.fromString("12345"), @@ -275,7 +275,7 @@ describe("AminoTypes", () => { contract: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", }, }; - const msg = new AminoTypes({ additions: cosmWasmTypes }).fromAmino(aminoMsg); + const msg = new AminoTypes({ prefix: "cosmos", additions: cosmWasmTypes }).fromAmino(aminoMsg); const expectedValue: MsgUpdateAdmin = { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", newAdmin: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", @@ -295,7 +295,7 @@ describe("AminoTypes", () => { contract: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", }, }; - const msg = new AminoTypes({ additions: cosmWasmTypes }).fromAmino(aminoMsg); + const msg = new AminoTypes({ prefix: "cosmos", additions: cosmWasmTypes }).fromAmino(aminoMsg); const expectedValue: MsgClearAdmin = { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", contract: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", @@ -316,7 +316,7 @@ describe("AminoTypes", () => { funds: coins(1234, "ucosm"), }, }; - const msg = new AminoTypes({ additions: cosmWasmTypes }).fromAmino(aminoMsg); + const msg = new AminoTypes({ prefix: "cosmos", additions: cosmWasmTypes }).fromAmino(aminoMsg); const expectedValue: MsgExecuteContract = { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", contract: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", @@ -339,7 +339,7 @@ describe("AminoTypes", () => { msg: { foo: "bar" }, }, }; - const msg = new AminoTypes({ additions: cosmWasmTypes }).fromAmino(aminoMsg); + const msg = new AminoTypes({ prefix: "cosmos", additions: cosmWasmTypes }).fromAmino(aminoMsg); const expectedValue: MsgMigrateContract = { sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", contract: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", diff --git a/packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts b/packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts index 5e86810ccc..295399aabc 100644 --- a/packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts +++ b/packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts @@ -818,6 +818,7 @@ describe("SigningCosmWasmClient", () => { }; customRegistry.register(msgDelegateTypeUrl, CustomMsgDelegate); const customAminoTypes = new AminoTypes({ + prefix: "cosmos", additions: { "/cosmos.staking.v1beta1.MsgDelegate": { aminoType: "cosmos-sdk/MsgDelegate", @@ -1122,6 +1123,7 @@ describe("SigningCosmWasmClient", () => { }; customRegistry.register(msgDelegateTypeUrl, CustomMsgDelegate); const customAminoTypes = new AminoTypes({ + prefix: "cosmos", additions: { "/cosmos.staking.v1beta1.MsgDelegate": { aminoType: "cosmos-sdk/MsgDelegate", diff --git a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts index 3a80686006..05a0e5434b 100644 --- a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts @@ -187,9 +187,11 @@ export class SigningCosmWasmClient extends CosmWasmClient { options: SigningCosmWasmClientOptions, ) { super(tmClient); + // TODO: do we really want to set a default here? Ideally we could get it from the signer such that users only have to set it once. + const prefix = options.prefix ?? "cosmos"; const { registry = createDefaultRegistry(), - aminoTypes = new AminoTypes({ additions: cosmWasmTypes, prefix: options.prefix }), + aminoTypes = new AminoTypes({ prefix, additions: cosmWasmTypes }), } = options; this.registry = registry; this.aminoTypes = aminoTypes; diff --git a/packages/stargate/src/aminotypes.spec.ts b/packages/stargate/src/aminotypes.spec.ts index 6a16e59c8d..158f31e43d 100644 --- a/packages/stargate/src/aminotypes.spec.ts +++ b/packages/stargate/src/aminotypes.spec.ts @@ -50,7 +50,7 @@ describe("AminoTypes", () => { toAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", amount: coins(1234, "ucosm"), }; - const aminoMsg = new AminoTypes().toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ typeUrl: "/cosmos.bank.v1beta1.MsgSend", value: msg, }); @@ -76,7 +76,7 @@ describe("AminoTypes", () => { { address: "cosmos142u9fgcjdlycfcez3lw8x6x5h7rfjlnfhpw2lx", coins: coins(912, "ucosm") }, ], }; - const aminoMsg = new AminoTypes().toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ typeUrl: "/cosmos.bank.v1beta1.MsgMultiSend", value: msg, }); @@ -104,7 +104,7 @@ describe("AminoTypes", () => { depositor: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", proposalId: Long.fromNumber(5), }; - const aminoMsg = new AminoTypes().toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ typeUrl: "/cosmos.gov.v1beta1.MsgDeposit", value: msg, }); @@ -131,7 +131,7 @@ describe("AminoTypes", () => { }).finish(), }, }; - const aminoMsg = new AminoTypes().toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ typeUrl: "/cosmos.gov.v1beta1.MsgSubmitProposal", value: msg, }); @@ -158,7 +158,7 @@ describe("AminoTypes", () => { proposalId: Long.fromNumber(5), voter: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", }; - const aminoMsg = new AminoTypes().toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ typeUrl: "/cosmos.gov.v1beta1.MsgVote", value: msg, }); @@ -180,7 +180,7 @@ describe("AminoTypes", () => { amount: coins(1234, "ucosm"), depositor: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", }; - const aminoMsg = new AminoTypes().toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ typeUrl: "/cosmos.distribution.v1beta1.MsgFundCommunityPool", value: msg, }); @@ -199,7 +199,7 @@ describe("AminoTypes", () => { delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", withdrawAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", }; - const aminoMsg = new AminoTypes().toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ typeUrl: "/cosmos.distribution.v1beta1.MsgSetWithdrawAddress", value: msg, }); @@ -218,7 +218,7 @@ describe("AminoTypes", () => { delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", }; - const aminoMsg = new AminoTypes().toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ typeUrl: "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward", value: msg, }); @@ -236,7 +236,7 @@ describe("AminoTypes", () => { const msg: MsgWithdrawValidatorCommission = { validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", }; - const aminoMsg = new AminoTypes().toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ typeUrl: "/cosmos.distribution.v1beta1.MsgWithdrawValidatorCommission", value: msg, }); @@ -258,7 +258,7 @@ describe("AminoTypes", () => { validatorDstAddress: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", amount: coin(1234, "ucosm"), }; - const aminoMsg = new AminoTypes().toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ typeUrl: "/cosmos.staking.v1beta1.MsgBeginRedelegate", value: msg, }); @@ -297,7 +297,7 @@ describe("AminoTypes", () => { }, value: coin(1234, "ucosm"), }; - const aminoMsg = new AminoTypes().toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ typeUrl: "/cosmos.staking.v1beta1.MsgCreateValidator", value: msg, }); @@ -335,7 +335,7 @@ describe("AminoTypes", () => { validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", amount: coin(1234, "ucosm"), }; - const aminoMsg = new AminoTypes().toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ typeUrl: "/cosmos.staking.v1beta1.MsgDelegate", value: msg, }); @@ -363,7 +363,7 @@ describe("AminoTypes", () => { minSelfDelegation: "123", validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", }; - const aminoMsg = new AminoTypes().toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ typeUrl: "/cosmos.staking.v1beta1.MsgEditValidator", value: msg, }); @@ -391,7 +391,7 @@ describe("AminoTypes", () => { validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", amount: coin(1234, "ucosm"), }; - const aminoMsg = new AminoTypes().toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ typeUrl: "/cosmos.staking.v1beta1.MsgUndelegate", value: msg, }); @@ -421,7 +421,7 @@ describe("AminoTypes", () => { }, timeoutTimestamp: Long.fromString("789", true), }; - const aminoMsg = new AminoTypes().toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ typeUrl: "/ibc.applications.transfer.v1.MsgTransfer", value: msg, }); @@ -456,7 +456,7 @@ describe("AminoTypes", () => { }, timeoutTimestamp: Long.UZERO, }; - const aminoMsg = new AminoTypes().toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ typeUrl: "/ibc.applications.transfer.v1.MsgTransfer", value: msg, }); @@ -488,7 +488,7 @@ describe("AminoTypes", () => { timeoutHeight: undefined, timeoutTimestamp: Long.UZERO, }; - const aminoMsg = new AminoTypes().toAmino({ + const aminoMsg = new AminoTypes({ prefix: "cosmos" }).toAmino({ typeUrl: "/ibc.applications.transfer.v1.MsgTransfer", value: msg, }); @@ -514,6 +514,7 @@ describe("AminoTypes", () => { foo: "bar", }; const aminoMsg = new AminoTypes({ + prefix: "cosmos", additions: { "/my.CustomType": { aminoType: "my-sdk/CustomType", @@ -545,6 +546,7 @@ describe("AminoTypes", () => { amount: coin(1234, "ucosm"), }; const aminoMsg = new AminoTypes({ + prefix: "cosmos", additions: { "/cosmos.staking.v1beta1.MsgDelegate": { aminoType: "my-override/MsgDelegate", @@ -568,9 +570,9 @@ describe("AminoTypes", () => { }); it("throws for unknown type url", () => { - expect(() => new AminoTypes().toAmino({ typeUrl: "/xxx.Unknown", value: { foo: "bar" } })).toThrowError( - /Type URL '\/xxx\.Unknown' does not exist in the Amino message type register./i, - ); + expect(() => + new AminoTypes({ prefix: "cosmos" }).toAmino({ typeUrl: "/xxx.Unknown", value: { foo: "bar" } }), + ).toThrowError(/Type URL '\/xxx\.Unknown' does not exist in the Amino message type register./i); }); }); @@ -586,7 +588,7 @@ describe("AminoTypes", () => { amount: coins(1234, "ucosm"), }, }; - const msg = new AminoTypes().fromAmino(aminoMsg); + const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); const expectedValue: MsgSend = { fromAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", toAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", @@ -612,7 +614,7 @@ describe("AminoTypes", () => { ], }, }; - const msg = new AminoTypes().fromAmino(aminoMsg); + const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); const expectedValue: MsgMultiSend = { inputs: [ { address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", coins: coins(1234, "ucosm") }, @@ -640,7 +642,7 @@ describe("AminoTypes", () => { proposal_id: "5", }, }; - const msg = new AminoTypes().fromAmino(aminoMsg); + const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); const expectedValue: MsgDeposit = { amount: [{ amount: "12300000", denom: "ustake" }], depositor: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", @@ -667,7 +669,7 @@ describe("AminoTypes", () => { }, }, }; - const msg = new AminoTypes().fromAmino(aminoMsg); + const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); const expectedValue: MsgSubmitProposal = { initialDeposit: [{ amount: "12300000", denom: "ustake" }], proposer: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", @@ -694,7 +696,7 @@ describe("AminoTypes", () => { voter: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k", }, }; - const msg = new AminoTypes().fromAmino(aminoMsg); + const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); const expectedValue: MsgVote = { option: VoteOption.VOTE_OPTION_NO_WITH_VETO, proposalId: Long.fromNumber(5), @@ -725,7 +727,7 @@ describe("AminoTypes", () => { amount: coin(1234, "ucosm"), }, }; - const msg = new AminoTypes().fromAmino(aminoMsg); + const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); const expectedValue: MsgBeginRedelegate = { delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", validatorSrcAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", @@ -764,7 +766,7 @@ describe("AminoTypes", () => { value: coin(1234, "ucosm"), }, }; - const msg = new AminoTypes().fromAmino(aminoMsg); + const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); const expectedValue: MsgCreateValidator = { description: { moniker: "validator", @@ -802,7 +804,7 @@ describe("AminoTypes", () => { amount: coin(1234, "ucosm"), }, }; - const msg = new AminoTypes().fromAmino(aminoMsg); + const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); const expectedValue: MsgDelegate = { delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", @@ -830,7 +832,7 @@ describe("AminoTypes", () => { validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", }, }; - const msg = new AminoTypes().fromAmino(aminoMsg); + const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); const expectedValue: MsgEditValidator = { description: { moniker: "validator", @@ -858,7 +860,7 @@ describe("AminoTypes", () => { amount: coin(1234, "ucosm"), }, }; - const msg = new AminoTypes().fromAmino(aminoMsg); + const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); const expectedValue: MsgUndelegate = { delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5", @@ -888,7 +890,7 @@ describe("AminoTypes", () => { timeout_timestamp: "789", }, }; - const msg = new AminoTypes().fromAmino(aminoMsg); + const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); const expectedValue: MsgTransfer = { sourcePort: "testport", sourceChannel: "testchannel", @@ -923,7 +925,7 @@ describe("AminoTypes", () => { // timeout_timestamp omitted }, }; - const msg = new AminoTypes().fromAmino(aminoMsg); + const msg = new AminoTypes({ prefix: "cosmos" }).fromAmino(aminoMsg); const expectedValue: MsgTransfer = { sourcePort: "testport", sourceChannel: "testchannel", @@ -953,6 +955,7 @@ describe("AminoTypes", () => { }, }; const msg = new AminoTypes({ + prefix: "cosmos", additions: { "/my.CustomType": { aminoType: "my-sdk/CustomType", @@ -974,6 +977,7 @@ describe("AminoTypes", () => { it("works with overridden type url", () => { const msg = new AminoTypes({ + prefix: "cosmos", additions: { "/my.OverrideType": { aminoType: "cosmos-sdk/MsgDelegate", @@ -1004,7 +1008,10 @@ describe("AminoTypes", () => { it("throws for unknown type url", () => { expect(() => - new AminoTypes().fromAmino({ type: "cosmos-sdk/MsgUnknown", value: { foo: "bar" } }), + new AminoTypes({ prefix: "cosmos" }).fromAmino({ + type: "cosmos-sdk/MsgUnknown", + value: { foo: "bar" }, + }), ).toThrowError( /Amino type identifier 'cosmos-sdk\/MsgUnknown' does not exist in the Amino message type register./i, ); diff --git a/packages/stargate/src/aminotypes.ts b/packages/stargate/src/aminotypes.ts index 0ae8031162..cf7a545147 100644 --- a/packages/stargate/src/aminotypes.ts +++ b/packages/stargate/src/aminotypes.ts @@ -511,9 +511,12 @@ function createDefaultTypes(prefix: string): Record { }; } -interface AminoTypesOptions { +export interface AminoTypesOptions { + /** + * The Bech32 address prefix of the chain you work with (also called Bech32 human-readable part). + */ + readonly prefix: string; readonly additions?: Record; - readonly prefix?: string; } /** @@ -523,7 +526,7 @@ interface AminoTypesOptions { export class AminoTypes { private readonly register: Record; - public constructor({ additions = {}, prefix = "cosmos" }: AminoTypesOptions = {}) { + public constructor({ prefix, additions = {} }: AminoTypesOptions) { const additionalAminoTypes = Object.values(additions); const filteredDefaultTypes = Object.entries(createDefaultTypes(prefix)).reduce( (acc, [key, value]) => diff --git a/packages/stargate/src/index.ts b/packages/stargate/src/index.ts index c9a633a060..883f04ea39 100644 --- a/packages/stargate/src/index.ts +++ b/packages/stargate/src/index.ts @@ -35,7 +35,7 @@ export { isAminoMsgWithdrawDelegatorReward, isAminoMsgWithdrawValidatorCommission, } from "./aminomsgs"; -export { AminoConverter, AminoTypes } from "./aminotypes"; +export { AminoConverter, AminoTypes, AminoTypesOptions } from "./aminotypes"; export { isMsgDelegateEncodeObject, isMsgDepositEncodeObject, diff --git a/packages/stargate/src/signingstargateclient.spec.ts b/packages/stargate/src/signingstargateclient.spec.ts index 67c0c9730a..9f41535297 100644 --- a/packages/stargate/src/signingstargateclient.spec.ts +++ b/packages/stargate/src/signingstargateclient.spec.ts @@ -484,6 +484,7 @@ describe("SigningStargateClient", () => { }; customRegistry.register(msgDelegateTypeUrl, CustomMsgDelegate); const customAminoTypes = new AminoTypes({ + prefix: "cosmos", additions: { "/cosmos.staking.v1beta1.MsgDelegate": { aminoType: "cosmos-sdk/MsgDelegate", @@ -774,6 +775,7 @@ describe("SigningStargateClient", () => { }; customRegistry.register(msgDelegateTypeUrl, CustomMsgDelegate); const customAminoTypes = new AminoTypes({ + prefix: "cosmos", additions: { "/cosmos.staking.v1beta1.MsgDelegate": { aminoType: "cosmos-sdk/MsgDelegate", diff --git a/packages/stargate/src/signingstargateclient.ts b/packages/stargate/src/signingstargateclient.ts index ff819c7fd8..0d34e1c758 100644 --- a/packages/stargate/src/signingstargateclient.ts +++ b/packages/stargate/src/signingstargateclient.ts @@ -175,8 +175,9 @@ export class SigningStargateClient extends StargateClient { options: SigningStargateClientOptions, ) { super(tmClient); - const { registry = createDefaultRegistry(), aminoTypes = new AminoTypes({ prefix: options.prefix }) } = - options; + // TODO: do we really want to set a default here? Ideally we could get it from the signer such that users only have to set it once. + const prefix = options.prefix ?? "cosmos"; + const { registry = createDefaultRegistry(), aminoTypes = new AminoTypes({ prefix }) } = options; this.registry = registry; this.aminoTypes = aminoTypes; this.signer = signer;