function isAminoConverter( converter, ) { return typeof converter[1] !== "string"; } /** * A map from Stargate message types as used in the messages's `Any` type * to Amino types. */ export class AminoTypes { constructor(types) { this.register = types; } toAmino({ typeUrl, value }) { 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. ` + "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.", ); } if (typeUrl === "/cosmos.authz.v1beta1.MsgGrant" || typeUrl === "/cosmos.authz.v1beta1.MsgRevoke") { return converter.toAmino(value) } return { type: converter.aminoType, value: converter.toAmino(value), }; } fromAmino(data) { if (!data.type) { const typeUrl = data.msg_type_url ? "/cosmos.authz.v1beta1.MsgRevoke" : "/cosmos.authz.v1beta1.MsgGrant" const converter = this.register[typeUrl]; return { typeUrl: typeUrl, value: converter.fromAmino(data), } } const { type, value } = data const matches = Object.entries(this.register) .filter(isAminoConverter) .filter(([_typeUrl, { aminoType }]) => aminoType === type); switch (matches.length) { case 0: { throw new Error( `Amino type identifier '${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.", ); } case 1: { const [typeUrl, converter] = matches[0]; return { typeUrl: typeUrl, value: converter.fromAmino(value), }; } default: throw new Error( `Multiple types are registered with Amino type identifier '${type}': '` + matches .map(([key, _value]) => key) .sort() .join("', '") + "'. Thus fromAmino cannot be performed.", ); } } }