From debe94935f7f24fa9c1517a0d5dfffb79b79a2a6 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 21 Dec 2020 17:09:38 +0100 Subject: [PATCH] Rename to toAminoMsgType/fromAminoMsgType, improve testing and error messages --- .../src/signingcosmwasmclient.ts | 8 +++---- packages/stargate/src/encoding.spec.ts | 23 +++++++++++++++---- packages/stargate/src/encoding.ts | 22 ++++++++++++------ packages/stargate/src/index.ts | 2 +- .../stargate/src/signingstargateclient.ts | 6 ++--- packages/stargate/types/encoding.d.ts | 4 ++-- packages/stargate/types/index.d.ts | 2 +- 7 files changed, 45 insertions(+), 22 deletions(-) diff --git a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts index 08c1805b..c29ad101 100644 --- a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts @@ -38,10 +38,10 @@ import { BroadcastTxFailure, BroadcastTxResponse, codec, - getMsgType, - getMsgTypeUrl, + fromAminoMsgType, isBroadcastTxFailure, parseRawLog, + toAminoMsgType, } from "@cosmjs/stargate"; import { adaptor34, Client as TendermintClient } from "@cosmjs/tendermint-rpc"; import Long from "long"; @@ -368,14 +368,14 @@ export class SigningCosmWasmClient extends CosmWasmClient { // Amino signer const signMode = SignMode.SIGN_MODE_LEGACY_AMINO_JSON; const msgs = messages.map((msg) => ({ - type: getMsgType(msg.typeUrl), + type: toAminoMsgType(msg.typeUrl), value: msg.value, })); const signDoc = makeSignDocAmino(msgs, fee, chainId, memo, accountNumber, sequence); const { signature, signed } = await this.signer.signAmino(address, signDoc); const signedTxBody = { messages: signed.msgs.map((msg) => ({ - typeUrl: getMsgTypeUrl(msg.type), + typeUrl: fromAminoMsgType(msg.type), value: msg.value, })), memo: signed.memo, diff --git a/packages/stargate/src/encoding.spec.ts b/packages/stargate/src/encoding.spec.ts index d36b1f18..f41db008 100644 --- a/packages/stargate/src/encoding.spec.ts +++ b/packages/stargate/src/encoding.spec.ts @@ -1,15 +1,30 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { getMsgType } from "./encoding"; +import { fromAminoMsgType, toAminoMsgType } from "./encoding"; describe("encoding", () => { - describe("getMsgType", () => { + describe("toAminoMsgType", () => { it("works for known type url", () => { - const msgType = getMsgType("/cosmos.staking.v1beta1.MsgDelegate"); + const msgType = toAminoMsgType("/cosmos.staking.v1beta1.MsgDelegate"); expect(msgType).toEqual("cosmos-sdk/MsgDelegate"); }); it("throws for unknown type url", () => { - expect(() => getMsgType("/xxx.Unknown")).toThrowError(/type url not known/i); + expect(() => toAminoMsgType("/xxx.Unknown")).toThrowError( + /Type URL does not exist in the Amino message type register./i, + ); + }); + }); + + describe("fromAminoMsgType", () => { + it("works for known type url", () => { + const msgUrl = fromAminoMsgType("cosmos-sdk/MsgDelegate"); + expect(msgUrl).toEqual("/cosmos.staking.v1beta1.MsgDelegate"); + }); + + it("throws for unknown type url", () => { + expect(() => fromAminoMsgType("cosmos-sdk/MsgUnknown")).toThrowError( + /Type does not exist in the Amino message type register./i, + ); }); }); }); diff --git a/packages/stargate/src/encoding.ts b/packages/stargate/src/encoding.ts index 71ffe880..353cacc1 100644 --- a/packages/stargate/src/encoding.ts +++ b/packages/stargate/src/encoding.ts @@ -1,4 +1,8 @@ -const typeRegister: Record = { +/** + * A map from Stargate message types as used in the messages's `Any` type + * to Amino types. + */ +const aminoTypeRegister: Record = { "/cosmos.bank.v1beta1.MsgSend": "cosmos-sdk/MsgSend", "/cosmos.bank.v1beta1.MsgMultiSend": "cosmos-sdk/MsgMultiSend", "/cosmos.crisis.v1beta1.MsgVerifyInvariant": "cosmos-sdk/MsgVerifyInvariant", @@ -19,18 +23,22 @@ const typeRegister: Record = { "/cosmos.vesting.v1beta1.MsgCreateVestingAccount": "cosmos-sdk/MsgCreateVestingAccount", }; -export function getMsgType(typeUrl: string): string { - const type = typeRegister[typeUrl]; +export function toAminoMsgType(typeUrl: string): string { + const type = aminoTypeRegister[typeUrl]; if (!type) { - throw new Error("Type URL not known"); + throw new Error( + "Type URL does not exist in the Amino message type register. If you need support for this message, please open an issue at https://github.com/cosmos/cosmjs/issues.", + ); } return type; } -export function getMsgTypeUrl(type: string): string { - const [typeUrl] = Object.entries(typeRegister).find(([_typeUrl, value]) => value === type) ?? []; +export function fromAminoMsgType(type: string): string { + const [typeUrl] = Object.entries(aminoTypeRegister).find(([_typeUrl, value]) => value === type) ?? []; if (!typeUrl) { - throw new Error("Type not known"); + throw new Error( + "Type does not exist in the Amino message type register. If you need support for this message, please open an issue at https://github.com/cosmos/cosmjs/issues.", + ); } return typeUrl; } diff --git a/packages/stargate/src/index.ts b/packages/stargate/src/index.ts index bbc91839..f829efe4 100644 --- a/packages/stargate/src/index.ts +++ b/packages/stargate/src/index.ts @@ -1,5 +1,5 @@ export * as codec from "./codec"; -export { getMsgType, getMsgTypeUrl } from "./encoding"; +export { toAminoMsgType, fromAminoMsgType } from "./encoding"; export { parseRawLog } from "./logs"; export { AuthExtension, diff --git a/packages/stargate/src/signingstargateclient.ts b/packages/stargate/src/signingstargateclient.ts index 67a84ab5..b39e36f7 100644 --- a/packages/stargate/src/signingstargateclient.ts +++ b/packages/stargate/src/signingstargateclient.ts @@ -24,7 +24,7 @@ import { import { adaptor34, Client as TendermintClient } from "@cosmjs/tendermint-rpc"; import { cosmos } from "./codec"; -import { getMsgType, getMsgTypeUrl } from "./encoding"; +import { fromAminoMsgType, toAminoMsgType } from "./encoding"; import { BroadcastTxResponse, StargateClient } from "./stargateclient"; const { TxRaw } = cosmos.tx.v1beta1; @@ -136,14 +136,14 @@ export class SigningStargateClient extends StargateClient { // Amino signer const signMode = cosmos.tx.signing.v1beta1.SignMode.SIGN_MODE_LEGACY_AMINO_JSON; const msgs = messages.map((msg) => ({ - type: getMsgType(msg.typeUrl), + type: toAminoMsgType(msg.typeUrl), value: msg.value, })); const signDoc = makeSignDocAmino(msgs, fee, chainId, memo, accountNumber, sequence); const { signature, signed } = await this.signer.signAmino(address, signDoc); const signedTxBody = { messages: signed.msgs.map((msg) => ({ - typeUrl: getMsgTypeUrl(msg.type), + typeUrl: fromAminoMsgType(msg.type), value: msg.value, })), memo: signed.memo, diff --git a/packages/stargate/types/encoding.d.ts b/packages/stargate/types/encoding.d.ts index af3b9e44..e9f5488d 100644 --- a/packages/stargate/types/encoding.d.ts +++ b/packages/stargate/types/encoding.d.ts @@ -1,2 +1,2 @@ -export declare function getMsgType(typeUrl: string): string; -export declare function getMsgTypeUrl(type: string): string; +export declare function toAminoMsgType(typeUrl: string): string; +export declare function fromAminoMsgType(type: string): string; diff --git a/packages/stargate/types/index.d.ts b/packages/stargate/types/index.d.ts index bbc91839..f829efe4 100644 --- a/packages/stargate/types/index.d.ts +++ b/packages/stargate/types/index.d.ts @@ -1,5 +1,5 @@ export * as codec from "./codec"; -export { getMsgType, getMsgTypeUrl } from "./encoding"; +export { toAminoMsgType, fromAminoMsgType } from "./encoding"; export { parseRawLog } from "./logs"; export { AuthExtension,