From f48c0e31059977d73d3dc501d7642804ebdb4d41 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 19 Jan 2022 14:33:08 +0100 Subject: [PATCH] Simplify usage and examples for Registry --- .../src/signingcosmwasmclient.ts | 19 +++++++++---------- packages/proto-signing/src/registry.spec.ts | 3 ++- packages/proto-signing/src/registry.ts | 19 +++++++++++++++++++ packages/stargate/CUSTOM_PROTOBUF_CODECS.md | 11 ++++++----- 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts index afc9cefc75..3a80686006 100644 --- a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts @@ -17,7 +17,7 @@ import { AminoTypes, calculateFee, Coin, - defaultRegistryTypes, + defaultRegistryTypes as defaultStargateTypes, DeliverTxResponse, GasPrice, isDeliverTxFailure, @@ -128,15 +128,14 @@ function createDeliverTxResponseErrorMessage(result: DeliverTxResponse): string } function createDefaultRegistry(): Registry { - return new Registry([ - ...defaultRegistryTypes, - ["/cosmwasm.wasm.v1.MsgClearAdmin", MsgClearAdmin], - ["/cosmwasm.wasm.v1.MsgExecuteContract", MsgExecuteContract], - ["/cosmwasm.wasm.v1.MsgMigrateContract", MsgMigrateContract], - ["/cosmwasm.wasm.v1.MsgStoreCode", MsgStoreCode], - ["/cosmwasm.wasm.v1.MsgInstantiateContract", MsgInstantiateContract], - ["/cosmwasm.wasm.v1.MsgUpdateAdmin", MsgUpdateAdmin], - ]); + const registry = new Registry(defaultStargateTypes); + registry.register("/cosmwasm.wasm.v1.MsgClearAdmin", MsgClearAdmin); + registry.register("/cosmwasm.wasm.v1.MsgExecuteContract", MsgExecuteContract); + registry.register("/cosmwasm.wasm.v1.MsgMigrateContract", MsgMigrateContract); + registry.register("/cosmwasm.wasm.v1.MsgStoreCode", MsgStoreCode); + registry.register("/cosmwasm.wasm.v1.MsgInstantiateContract", MsgInstantiateContract); + registry.register("/cosmwasm.wasm.v1.MsgUpdateAdmin", MsgUpdateAdmin); + return registry; } export interface SigningCosmWasmClientOptions { diff --git a/packages/proto-signing/src/registry.spec.ts b/packages/proto-signing/src/registry.spec.ts index ae32381afe..1cdda7de67 100644 --- a/packages/proto-signing/src/registry.spec.ts +++ b/packages/proto-signing/src/registry.spec.ts @@ -64,7 +64,8 @@ describe("registry demo", () => { .add(new Field("body", 3, "string")) .add(new Field("attachment", 4, "bytes")); - const registry = new Registry([[typeUrl, MsgCreatePostOriginal]]); + const registry = new Registry(); + registry.register(typeUrl, MsgCreatePostOriginal); const MsgCreatePost = registry.lookupType(typeUrl); assert(MsgCreatePost); assert(isPbjsGeneratedType(MsgCreatePost)); diff --git a/packages/proto-signing/src/registry.ts b/packages/proto-signing/src/registry.ts index b3ea7d7550..90dbf12708 100644 --- a/packages/proto-signing/src/registry.ts +++ b/packages/proto-signing/src/registry.ts @@ -76,6 +76,25 @@ export function isTxBodyEncodeObject(encodeObject: EncodeObject): encodeObject i export class Registry { private readonly types: Map; + /** + * Creates a new Registry for mapping protobuf type identifiers/type URLs to + * actual implementations. Those implementations are typically generated with ts-proto + * but we also support protobuf.js as a type generator. + * + * By default, a `new Registry()` constains amost no types. `Coin` and `MsgSend` are in there + * for historic reasons but this does not make a lot of sense. + * + * There are currently two methods for adding new types: + * 1. Using the `register()` method + * 2. Passing custom types to the constructor. + * This only creates confusion for users. The reason here is historical. + * Using `register()` is recommended and 2. is deprecated because its behaviour + * will change in https://github.com/cosmos/cosmjs/issues/994. + * + * There is currently no way to unregister/override the default types. We should + * change the `customTypes` argument to override the default types if set. + * See https://github.com/cosmos/cosmjs/issues/994 + */ public constructor(customTypes: Iterable<[string, GeneratedType]> = []) { const { cosmosCoin, cosmosMsgSend } = defaultTypeUrls; this.types = new Map([ diff --git a/packages/stargate/CUSTOM_PROTOBUF_CODECS.md b/packages/stargate/CUSTOM_PROTOBUF_CODECS.md index c2ff44f84a..c53b57d8b8 100644 --- a/packages/stargate/CUSTOM_PROTOBUF_CODECS.md +++ b/packages/stargate/CUSTOM_PROTOBUF_CODECS.md @@ -108,13 +108,14 @@ For example: ```ts import { DirectSecp256k1HdWallet, Registry } from "@cosmjs/proto-signing"; -import { defaultRegistryTypes, SigningStargateClient } from "@cosmjs/stargate"; +import { + defaultRegistryTypes as defaultStargateTypes, + SigningStargateClient, +} from "@cosmjs/stargate"; import { MsgXxx } from "./path/to/generated/codec/my/custom/tx"; // Replace with your own Msg import -const myRegistry = new Registry([ - ...defaultRegistryTypes, - ["/my.custom.MsgXxx", MsgXxx], // Replace with your own type URL and Msg class -]); +const myRegistry = new Registry(defaultStargateTypes); +myRegistry.register("/my.custom.MsgXxx", MsgXxx); // Replace with your own type URL and Msg class const mnemonic = // Replace with your own mnemonic "economy stock theory fatal elder harbor betray wasp final emotion task crumble siren bottom lizard educate guess current outdoor pair theory focus wife stone";