Merge pull request #1052 from cosmos/new-error-msg-types

Adding errors to unsupported TypeUrls
This commit is contained in:
Simon Warta 2022-02-21 11:11:17 +01:00 committed by GitHub
commit b6b27bb42d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 4 deletions

View File

@ -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({

View File

@ -63,10 +63,14 @@ function omitDefault<T extends string | number | Long>(input: T): T | undefined
throw new Error(`Got unsupported type '${typeof input}'`);
}
function createDefaultTypes(prefix: string): Record<string, AminoConverter> {
function createDefaultTypes(prefix: string): Record<string, AminoConverter | "not_supported_by_chain"> {
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<string, AminoConverter> {
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<string, AminoConverter>;
}
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<string, AminoConverter>;
private readonly register: Record<string, AminoConverter | "not_supported_by_chain">;
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(