Rename to toAminoMsgType/fromAminoMsgType, improve testing and error messages

This commit is contained in:
Simon Warta 2020-12-21 17:09:38 +01:00
parent 858bd2e083
commit debe94935f
7 changed files with 45 additions and 22 deletions

View File

@ -38,10 +38,10 @@ import {
BroadcastTxFailure,
BroadcastTxResponse,
codec,
getMsgType,
getMsgTypeUrl,
fromAminoMsgType,
isBroadcastTxFailure,
parseRawLog,
toAminoMsgType,
} from "@cosmjs/stargate";
import { adaptor34, Client as TendermintClient } from "@cosmjs/tendermint-rpc";
import Long from "long";
@ -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: getMsgType(msg.typeUrl),
type: toAminoMsgType(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: getMsgTypeUrl(msg.type),
typeUrl: fromAminoMsgType(msg.type),
value: msg.value,
})),
memo: signed.memo,

View File

@ -1,15 +1,30 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { getMsgType } from "./encoding";
import { fromAminoMsgType, toAminoMsgType } from "./encoding";
describe("encoding", () => {
describe("getMsgType", () => {
describe("toAminoMsgType", () => {
it("works for known type url", () => {
const msgType = getMsgType("/cosmos.staking.v1beta1.MsgDelegate");
const msgType = toAminoMsgType("/cosmos.staking.v1beta1.MsgDelegate");
expect(msgType).toEqual("cosmos-sdk/MsgDelegate");
});
it("throws for unknown type url", () => {
expect(() => getMsgType("/xxx.Unknown")).toThrowError(/type url not known/i);
expect(() => toAminoMsgType("/xxx.Unknown")).toThrowError(
/Type URL does not exist in the Amino message type register./i,
);
});
});
describe("fromAminoMsgType", () => {
it("works for known type url", () => {
const msgUrl = fromAminoMsgType("cosmos-sdk/MsgDelegate");
expect(msgUrl).toEqual("/cosmos.staking.v1beta1.MsgDelegate");
});
it("throws for unknown type url", () => {
expect(() => fromAminoMsgType("cosmos-sdk/MsgUnknown")).toThrowError(
/Type does not exist in the Amino message type register./i,
);
});
});
});

View File

@ -1,4 +1,8 @@
const typeRegister: Record<string, string> = {
/**
* A map from Stargate message types as used in the messages's `Any` type
* to Amino types.
*/
const aminoTypeRegister: Record<string, string> = {
"/cosmos.bank.v1beta1.MsgSend": "cosmos-sdk/MsgSend",
"/cosmos.bank.v1beta1.MsgMultiSend": "cosmos-sdk/MsgMultiSend",
"/cosmos.crisis.v1beta1.MsgVerifyInvariant": "cosmos-sdk/MsgVerifyInvariant",
@ -19,18 +23,22 @@ const typeRegister: Record<string, string> = {
"/cosmos.vesting.v1beta1.MsgCreateVestingAccount": "cosmos-sdk/MsgCreateVestingAccount",
};
export function getMsgType(typeUrl: string): string {
const type = typeRegister[typeUrl];
export function toAminoMsgType(typeUrl: string): string {
const type = aminoTypeRegister[typeUrl];
if (!type) {
throw new Error("Type URL not known");
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;
}
export function getMsgTypeUrl(type: string): string {
const [typeUrl] = Object.entries(typeRegister).find(([_typeUrl, value]) => value === type) ?? [];
export function fromAminoMsgType(type: string): string {
const [typeUrl] = Object.entries(aminoTypeRegister).find(([_typeUrl, value]) => value === type) ?? [];
if (!typeUrl) {
throw new Error("Type not known");
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.",
);
}
return typeUrl;
}

View File

@ -1,5 +1,5 @@
export * as codec from "./codec";
export { getMsgType, getMsgTypeUrl } from "./encoding";
export { toAminoMsgType, fromAminoMsgType } from "./encoding";
export { parseRawLog } from "./logs";
export {
AuthExtension,

View File

@ -24,7 +24,7 @@ import {
import { adaptor34, Client as TendermintClient } from "@cosmjs/tendermint-rpc";
import { cosmos } from "./codec";
import { getMsgType, getMsgTypeUrl } from "./encoding";
import { fromAminoMsgType, toAminoMsgType } from "./encoding";
import { BroadcastTxResponse, StargateClient } from "./stargateclient";
const { TxRaw } = cosmos.tx.v1beta1;
@ -136,14 +136,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: getMsgType(msg.typeUrl),
type: toAminoMsgType(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: getMsgTypeUrl(msg.type),
typeUrl: fromAminoMsgType(msg.type),
value: msg.value,
})),
memo: signed.memo,

View File

@ -1,2 +1,2 @@
export declare function getMsgType(typeUrl: string): string;
export declare function getMsgTypeUrl(type: string): string;
export declare function toAminoMsgType(typeUrl: string): string;
export declare function fromAminoMsgType(type: string): string;

View File

@ -1,5 +1,5 @@
export * as codec from "./codec";
export { getMsgType, getMsgTypeUrl } from "./encoding";
export { toAminoMsgType, fromAminoMsgType } from "./encoding";
export { parseRawLog } from "./logs";
export {
AuthExtension,