Simplify usage and examples for Registry

This commit is contained in:
Simon Warta 2022-01-19 14:33:08 +01:00
parent 959f7db105
commit f48c0e3105
4 changed files with 36 additions and 16 deletions

View File

@ -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 {

View File

@ -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));

View File

@ -76,6 +76,25 @@ export function isTxBodyEncodeObject(encodeObject: EncodeObject): encodeObject i
export class Registry {
private readonly types: Map<string, GeneratedType>;
/**
* 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<string, GeneratedType>([

View File

@ -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";