From fd7f57e78a08afcbbfd0bf0fae14118909780751 Mon Sep 17 00:00:00 2001 From: willclarktech Date: Thu, 15 Apr 2021 13:18:25 +0200 Subject: [PATCH] proto-signing: Add TxBodyEncodeObject etc --- packages/proto-signing/src/index.ts | 9 ++++-- packages/proto-signing/src/registry.ts | 40 ++++++++++++++++---------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/packages/proto-signing/src/index.ts b/packages/proto-signing/src/index.ts index ad08bfc942..a71e972164 100644 --- a/packages/proto-signing/src/index.ts +++ b/packages/proto-signing/src/index.ts @@ -2,13 +2,16 @@ export { Coin, coin, coins, parseCoins } from "@cosmjs/amino"; export { - isPbjsGeneratedType, - isTsProtoGeneratedType, + DecodeObject, EncodeObject, GeneratedType, + isTxBodyEncodeObject, + isPbjsGeneratedType, + isTsProtoGeneratedType, + PbjsGeneratedType, Registry, TsProtoGeneratedType, - PbjsGeneratedType, + TxBodyEncodeObject, } from "./registry"; export { DirectSecp256k1HdWallet, DirectSecp256k1HdWalletOptions } from "./directsecp256k1hdwallet"; export { DirectSecp256k1Wallet } from "./directsecp256k1wallet"; diff --git a/packages/proto-signing/src/registry.ts b/packages/proto-signing/src/registry.ts index aada6a075a..e0e53afa13 100644 --- a/packages/proto-signing/src/registry.ts +++ b/packages/proto-signing/src/registry.ts @@ -40,17 +40,24 @@ export function isPbjsGeneratedType(type: GeneratedType): type is PbjsGeneratedT return !isTsProtoGeneratedType(type); } -export interface EncodeObject { - readonly typeUrl: string; - readonly value: any; -} +const defaultTypeUrls = { + cosmosCoin: "/cosmos.base.v1beta1.Coin", + cosmosMsgSend: "/cosmos.bank.v1beta1.MsgSend", + cosmosTxBody: "/cosmos.tx.v1beta1.TxBody", + googleAny: "/google.protobuf.Any", +}; export interface DecodeObject { readonly typeUrl: string; readonly value: Uint8Array; } -export interface TxBodyValue { +export interface EncodeObject { + readonly typeUrl: string; + readonly value: any; +} + +interface TxBodyValue { readonly messages: readonly EncodeObject[]; readonly memo?: string; readonly timeoutHeight?: Long; @@ -58,12 +65,14 @@ export interface TxBodyValue { readonly nonCriticalExtensionOptions?: Any[]; } -const defaultTypeUrls = { - cosmosCoin: "/cosmos.base.v1beta1.Coin", - cosmosMsgSend: "/cosmos.bank.v1beta1.MsgSend", - cosmosTxBody: "/cosmos.tx.v1beta1.TxBody", - googleAny: "/google.protobuf.Any", -}; +export interface TxBodyEncodeObject extends EncodeObject { + readonly typeUrl: "/cosmos.tx.v1beta1.TxBody"; + readonly value: TxBodyValue; +} + +export function isTxBodyEncodeObject(encodeObject: EncodeObject): encodeObject is TxBodyEncodeObject { + return (encodeObject as TxBodyEncodeObject).typeUrl === "/cosmos.tx.v1beta1.TxBody"; +} export class Registry { private readonly types: Map; @@ -109,13 +118,14 @@ export class Registry { return type; } - public encode({ typeUrl, value }: EncodeObject): Uint8Array { - if (typeUrl === defaultTypeUrls.cosmosTxBody) { + public encode(encodeObject: EncodeObject): Uint8Array { + const { value, typeUrl } = encodeObject; + if (isTxBodyEncodeObject(encodeObject)) { return this.encodeTxBody(value); } const type = this.lookupTypeWithError(typeUrl); const instance = isTsProtoGeneratedType(type) ? type.fromPartial(value) : type.create(value); - return Uint8Array.from(type.encode(instance).finish()); + return type.encode(instance).finish(); } public encodeTxBody(txBodyFields: TxBodyValue): Uint8Array { @@ -130,7 +140,7 @@ export class Registry { ...txBodyFields, messages: wrappedMessages, }); - return Uint8Array.from(TxBody.encode(txBody).finish()); + return TxBody.encode(txBody).finish(); } public decode({ typeUrl, value }: DecodeObject): any {