Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding errors to unsupported TypeUrls #1039

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .yarn/cache/@confio-ics23-npm-0.6.5-21db74210e-dc658795ec.zip
Git LFS file not shown
3 changes: 0 additions & 3 deletions .yarn/cache/@confio-ics23-npm-0.6.8-c87607eb2c-376d72f644.zip

This file was deleted.

3 changes: 3 additions & 0 deletions .yarn/cache/js-sha512-npm-0.8.0-48a1a122ac-32ca371ebd.zip
Git LFS file not shown
11 changes: 11 additions & 0 deletions packages/stargate/src/aminotypes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 implemented on-chain./i,
);
});

it("throws for unknown type url", () => {
expect(() =>
new AminoTypes({ prefix: "cosmos" }).fromAmino({
Expand Down
28 changes: 24 additions & 4 deletions packages/stargate/src/aminotypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"] => ({
Expand Down Expand Up @@ -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",
};
}

Expand All @@ -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.
Expand All @@ -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);
Expand All @@ -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.`,
);
}
msteiner96 marked this conversation as resolved.
Show resolved Hide resolved
if (!converter) {
throw new Error(
`Type URL '${typeUrl}' does not exist in the Amino message type register. ` +
Expand All @@ -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(
Expand Down