From 017437994bdc69072830326774f2f8332d35bc3d Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 21 Dec 2020 17:53:04 +0100 Subject: [PATCH] Create AminoTypes class and make it extensible --- .../src/signingcosmwasmclient.ts | 8 +-- .../types/signingcosmwasmclient.d.ts | 1 + .../{encoding.spec.ts => aminotypes.spec.ts} | 16 +++--- .../src/{encoding.ts => aminotypes.ts} | 54 +++++++++++-------- packages/stargate/src/index.ts | 2 +- .../stargate/src/signingstargateclient.ts | 7 +-- packages/stargate/types/aminotypes.d.ts | 10 ++++ packages/stargate/types/encoding.d.ts | 2 - packages/stargate/types/index.d.ts | 2 +- .../stargate/types/signingstargateclient.d.ts | 1 + 10 files changed, 63 insertions(+), 40 deletions(-) rename packages/stargate/src/{encoding.spec.ts => aminotypes.spec.ts} (55%) rename packages/stargate/src/{encoding.ts => aminotypes.ts} (53%) create mode 100644 packages/stargate/types/aminotypes.d.ts delete mode 100644 packages/stargate/types/encoding.d.ts diff --git a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts index c29ad101..093db29f 100644 --- a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts @@ -35,13 +35,12 @@ import { Registry, } from "@cosmjs/proto-signing"; import { + AminoTypes, BroadcastTxFailure, BroadcastTxResponse, codec, - fromAminoMsgType, isBroadcastTxFailure, parseRawLog, - toAminoMsgType, } from "@cosmjs/stargate"; import { adaptor34, Client as TendermintClient } from "@cosmjs/tendermint-rpc"; import Long from "long"; @@ -110,6 +109,7 @@ export class SigningCosmWasmClient extends CosmWasmClient { private readonly fees: CosmosFeeTable; private readonly registry: Registry; private readonly signer: OfflineSigner; + private readonly aminoTypes = new AminoTypes(); public static async connectWithWallet( endpoint: string, @@ -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: toAminoMsgType(msg.typeUrl), + type: this.aminoTypes.toAmino(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: fromAminoMsgType(msg.type), + typeUrl: this.aminoTypes.fromAmino(msg.type), value: msg.value, })), memo: signed.memo, diff --git a/packages/cosmwasm-stargate/types/signingcosmwasmclient.d.ts b/packages/cosmwasm-stargate/types/signingcosmwasmclient.d.ts index 9bd1dcf6..df7c1bb1 100644 --- a/packages/cosmwasm-stargate/types/signingcosmwasmclient.d.ts +++ b/packages/cosmwasm-stargate/types/signingcosmwasmclient.d.ts @@ -25,6 +25,7 @@ export declare class SigningCosmWasmClient extends CosmWasmClient { private readonly fees; private readonly registry; private readonly signer; + private readonly aminoTypes; static connectWithWallet( endpoint: string, signer: OfflineSigner, diff --git a/packages/stargate/src/encoding.spec.ts b/packages/stargate/src/aminotypes.spec.ts similarity index 55% rename from packages/stargate/src/encoding.spec.ts rename to packages/stargate/src/aminotypes.spec.ts index f41db008..3053370d 100644 --- a/packages/stargate/src/encoding.spec.ts +++ b/packages/stargate/src/aminotypes.spec.ts @@ -1,28 +1,28 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { fromAminoMsgType, toAminoMsgType } from "./encoding"; +import { AminoTypes } from "./aminotypes"; -describe("encoding", () => { - describe("toAminoMsgType", () => { +describe("AminoTypes", () => { + describe("toAmino", () => { it("works for known type url", () => { - const msgType = toAminoMsgType("/cosmos.staking.v1beta1.MsgDelegate"); + const msgType = new AminoTypes().toAmino("/cosmos.staking.v1beta1.MsgDelegate"); expect(msgType).toEqual("cosmos-sdk/MsgDelegate"); }); it("throws for unknown type url", () => { - expect(() => toAminoMsgType("/xxx.Unknown")).toThrowError( + expect(() => new AminoTypes().toAmino("/xxx.Unknown")).toThrowError( /Type URL does not exist in the Amino message type register./i, ); }); }); - describe("fromAminoMsgType", () => { + describe("fromAmino", () => { it("works for known type url", () => { - const msgUrl = fromAminoMsgType("cosmos-sdk/MsgDelegate"); + const msgUrl = new AminoTypes().fromAmino("cosmos-sdk/MsgDelegate"); expect(msgUrl).toEqual("/cosmos.staking.v1beta1.MsgDelegate"); }); it("throws for unknown type url", () => { - expect(() => fromAminoMsgType("cosmos-sdk/MsgUnknown")).toThrowError( + expect(() => new AminoTypes().fromAmino("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/aminotypes.ts similarity index 53% rename from packages/stargate/src/encoding.ts rename to packages/stargate/src/aminotypes.ts index 353cacc1..0fa3a378 100644 --- a/packages/stargate/src/encoding.ts +++ b/packages/stargate/src/aminotypes.ts @@ -1,8 +1,4 @@ -/** - * A map from Stargate message types as used in the messages's `Any` type - * to Amino types. - */ -const aminoTypeRegister: Record = { +const defaultTypes: Record = { "/cosmos.bank.v1beta1.MsgSend": "cosmos-sdk/MsgSend", "/cosmos.bank.v1beta1.MsgMultiSend": "cosmos-sdk/MsgMultiSend", "/cosmos.crisis.v1beta1.MsgVerifyInvariant": "cosmos-sdk/MsgVerifyInvariant", @@ -23,22 +19,38 @@ const aminoTypeRegister: Record = { "/cosmos.vesting.v1beta1.MsgCreateVestingAccount": "cosmos-sdk/MsgCreateVestingAccount", }; -export function toAminoMsgType(typeUrl: string): string { - const type = aminoTypeRegister[typeUrl]; - if (!type) { - 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; -} +/** + * A map from Stargate message types as used in the messages's `Any` type + * to Amino types. + */ +export class AminoTypes { + private readonly register: Record; -export function fromAminoMsgType(type: string): string { - const [typeUrl] = Object.entries(aminoTypeRegister).find(([_typeUrl, value]) => value === type) ?? []; - if (!typeUrl) { - 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.", - ); + public constructor(additions: Record = {}) { + this.register = { ...defaultTypes, ...additions }; + } + + public toAmino(typeUrl: string): string { + const type = defaultTypes[typeUrl]; + if (!type) { + throw new Error( + "Type URL does not exist in the Amino message type register. " + + "If you need support for this message type, you can pass in additional entries to the AminoTypes constructor. " + + "If you think this message type should be included by default, please open an issue at https://github.com/cosmos/cosmjs/issues.", + ); + } + return type; + } + + public fromAmino(type: string): string { + const [typeUrl] = Object.entries(defaultTypes).find(([_typeUrl, value]) => value === type) ?? []; + if (!typeUrl) { + throw new Error( + "Type does not exist in the Amino message type register. " + + "If you need support for this message type, you can pass in additional entries to the AminoTypes constructor. " + + "If you think this message type should be included by default, please open an issue at https://github.com/cosmos/cosmjs/issues.", + ); + } + return typeUrl; } - return typeUrl; } diff --git a/packages/stargate/src/index.ts b/packages/stargate/src/index.ts index f829efe4..4254360a 100644 --- a/packages/stargate/src/index.ts +++ b/packages/stargate/src/index.ts @@ -1,5 +1,5 @@ export * as codec from "./codec"; -export { toAminoMsgType, fromAminoMsgType } from "./encoding"; +export { AminoTypes } from "./aminotypes"; export { parseRawLog } from "./logs"; export { AuthExtension, diff --git a/packages/stargate/src/signingstargateclient.ts b/packages/stargate/src/signingstargateclient.ts index b39e36f7..dd808cff 100644 --- a/packages/stargate/src/signingstargateclient.ts +++ b/packages/stargate/src/signingstargateclient.ts @@ -23,8 +23,8 @@ import { } from "@cosmjs/proto-signing"; import { adaptor34, Client as TendermintClient } from "@cosmjs/tendermint-rpc"; +import { AminoTypes } from "./aminotypes"; import { cosmos } from "./codec"; -import { fromAminoMsgType, toAminoMsgType } from "./encoding"; import { BroadcastTxResponse, StargateClient } from "./stargateclient"; const { TxRaw } = cosmos.tx.v1beta1; @@ -48,6 +48,7 @@ export class SigningStargateClient extends StargateClient { private readonly fees: CosmosFeeTable; private readonly registry: Registry; private readonly signer: OfflineSigner; + private readonly aminoTypes = new AminoTypes(); public static async connectWithWallet( endpoint: string, @@ -136,14 +137,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: toAminoMsgType(msg.typeUrl), + type: this.aminoTypes.toAmino(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: fromAminoMsgType(msg.type), + typeUrl: this.aminoTypes.fromAmino(msg.type), value: msg.value, })), memo: signed.memo, diff --git a/packages/stargate/types/aminotypes.d.ts b/packages/stargate/types/aminotypes.d.ts new file mode 100644 index 00000000..473b3797 --- /dev/null +++ b/packages/stargate/types/aminotypes.d.ts @@ -0,0 +1,10 @@ +/** + * A map from Stargate message types as used in the messages's `Any` type + * to Amino types. + */ +export declare class AminoTypes { + private readonly register; + constructor(additions?: Record); + toAmino(typeUrl: string): string; + fromAmino(type: string): string; +} diff --git a/packages/stargate/types/encoding.d.ts b/packages/stargate/types/encoding.d.ts deleted file mode 100644 index e9f5488d..00000000 --- a/packages/stargate/types/encoding.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -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 f829efe4..4254360a 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 { toAminoMsgType, fromAminoMsgType } from "./encoding"; +export { AminoTypes } from "./aminotypes"; export { parseRawLog } from "./logs"; export { AuthExtension, diff --git a/packages/stargate/types/signingstargateclient.d.ts b/packages/stargate/types/signingstargateclient.d.ts index e782543b..cc8eb7bb 100644 --- a/packages/stargate/types/signingstargateclient.d.ts +++ b/packages/stargate/types/signingstargateclient.d.ts @@ -15,6 +15,7 @@ export declare class SigningStargateClient extends StargateClient { private readonly fees; private readonly registry; private readonly signer; + private readonly aminoTypes; static connectWithWallet( endpoint: string, signer: OfflineSigner,