From 8068f2fc20e9ecebece12c639c24ea5dbba37689 Mon Sep 17 00:00:00 2001 From: Milan Steiner Date: Mon, 21 Feb 2022 10:49:18 +0100 Subject: [PATCH] new message types in defaultRegistry --- packages/stargate/src/aminotypes.spec.ts | 11 ++++++++++ packages/stargate/src/aminotypes.ts | 28 ++++++++++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/packages/stargate/src/aminotypes.spec.ts b/packages/stargate/src/aminotypes.spec.ts index d2fd70e3..82fa6d62 100644 --- a/packages/stargate/src/aminotypes.spec.ts +++ b/packages/stargate/src/aminotypes.spec.ts @@ -1078,6 +1078,17 @@ describe("AminoTypes", () => { }); }); + it("throws for types which are not on chain yet", () => { + expect(() => { + new AminoTypes({ prefix: "cosmos" }).toAmino({ + typeUrl: "/cosmos.feegrant.v1beta1.MsgRevokeAllowance", + value: 0, + }); + }).toThrowError( + /The message type '\/cosmos.feegrant.v1beta1.MsgRevokeAllowance' cannot be signed using the Amino JSON sign mode because this is not supported by chain./i, + ); + }); + it("throws for unknown type url", () => { expect(() => new AminoTypes({ prefix: "cosmos" }).fromAmino({ diff --git a/packages/stargate/src/aminotypes.ts b/packages/stargate/src/aminotypes.ts index efc8e1cd..ac0fafa1 100644 --- a/packages/stargate/src/aminotypes.ts +++ b/packages/stargate/src/aminotypes.ts @@ -63,10 +63,14 @@ function omitDefault(input: T): T | undefined throw new Error(`Got unsupported type '${typeof input}'`); } -function createDefaultTypes(prefix: string): Record { +function createDefaultTypes(prefix: string): Record { return { - // bank + // authz + "/cosmos.authz.v1beta1.MsgGrant": "not_supported_by_chain", + "/cosmos.authz.v1beta1.MsgExec": "not_supported_by_chain", + "/cosmos.authz.v1beta1.MsgRevoke": "not_supported_by_chain", + // bank "/cosmos.bank.v1beta1.MsgSend": { aminoType: "cosmos-sdk/MsgSend", toAmino: ({ fromAddress, toAddress, amount }: MsgSend): AminoMsgSend["value"] => ({ @@ -508,6 +512,8 @@ function createDefaultTypes(prefix: string): Record { timeoutTimestamp: Long.fromString(timeout_timestamp || "0", true), }), }, + "/cosmos.feegrant.v1beta1.MsgGrantAllowance": "not_supported_by_chain", + "/cosmos.feegrant.v1beta1.MsgRevokeAllowance": "not_supported_by_chain", }; } @@ -519,6 +525,12 @@ export interface AminoTypesOptions { readonly additions?: Record; } +function isAminoConverter( + converter: [string, AminoConverter | "not_supported_by_chain"], +): converter is [string, AminoConverter] { + return typeof converter[1] !== "string"; +} + /** * A map from Stargate message types as used in the messages's `Any` type * to Amino types. @@ -528,7 +540,7 @@ export class AminoTypes { // There is no uniqueness guarantee of the Amino type identifier in the type // system or constructor. Instead it's the user's responsibility to ensure // there is no overlap when fromAmino is called. - private readonly register: Record; + private readonly register: Record; public constructor({ prefix, additions = {} }: AminoTypesOptions) { const defaultTypes = createDefaultTypes(prefix); @@ -537,6 +549,11 @@ export class AminoTypes { public toAmino({ typeUrl, value }: EncodeObject): AminoMsg { const converter = this.register[typeUrl]; + if (converter === "not_supported_by_chain") { + throw new Error( + `The message type '${typeUrl}' cannot be signed using the Amino JSON sign mode because this is not supported by chain.`, + ); + } if (!converter) { throw new Error( `Type URL '${typeUrl}' does not exist in the Amino message type register. ` + @@ -551,7 +568,10 @@ export class AminoTypes { } public fromAmino({ type, value }: AminoMsg): EncodeObject { - const matches = Object.entries(this.register).filter(([_typeUrl, { aminoType }]) => aminoType === type); + const matches = Object.entries(this.register) + .filter(isAminoConverter) + .filter(([_typeUrl, { aminoType }]) => aminoType === type); + switch (matches.length) { case 0: { throw new Error(