diff --git a/packages/demo-protobuf/src/registry.spec.ts b/packages/demo-protobuf/src/registry.spec.ts index bc11581f..bca286d6 100644 --- a/packages/demo-protobuf/src/registry.spec.ts +++ b/packages/demo-protobuf/src/registry.spec.ts @@ -65,7 +65,7 @@ describe("registry demo", () => { }) as unknown) as MsgDemo; const msgDemoBytes = MsgDemo.encode(msgDemo).finish(); const msgDemoWrapped = Any.create({ - type_url: "/demo.MsgDemo", + type_url: typeUrl, value: msgDemoBytes, }); const txBody = TxBody.create({ diff --git a/packages/demo-protobuf/src/registry.ts b/packages/demo-protobuf/src/registry.ts index 302f67ef..844c0907 100644 --- a/packages/demo-protobuf/src/registry.ts +++ b/packages/demo-protobuf/src/registry.ts @@ -2,20 +2,29 @@ import protobuf from "protobufjs"; import { cosmos_sdk as cosmosSdk, google } from "./generated/codecimpl"; -export class Registry { - private readonly types: Map; +export type GeneratedType = { + readonly create: (properties?: { [k: string]: any }) => protobuf.Message<{}>; + readonly encode: ( + message: protobuf.Message<{}> | { [k: string]: any }, + writer?: protobuf.Writer, + ) => protobuf.Writer; + readonly decode: (reader: protobuf.Reader | Uint8Array, length?: number) => protobuf.Message<{}>; +}; - constructor(customTypes: readonly [string, protobuf.Type][] = []) { - this.types = new Map([ - ["/cosmos.Coin", (cosmosSdk.v1.Coin as unknown) as protobuf.Type], - ["/cosmos.bank.MsgSend", (cosmosSdk.x.bank.v1.MsgSend as unknown) as protobuf.Type], - ["/cosmos.tx.TxBody", (cosmosSdk.tx.v1.TxBody as unknown) as protobuf.Type], - ["/google.protobuf.Any", (google.protobuf.Any as unknown) as protobuf.Type], +export class Registry { + private readonly types: Map; + + constructor(customTypes: Iterable<[string, GeneratedType]> = []) { + this.types = new Map([ + ["/cosmos.Coin", (cosmosSdk.v1.Coin as unknown) as GeneratedType], + ["/cosmos.bank.MsgSend", (cosmosSdk.x.bank.v1.MsgSend as unknown) as GeneratedType], + ["/cosmos.tx.TxBody", (cosmosSdk.tx.v1.TxBody as unknown) as GeneratedType], + ["/google.protobuf.Any", (google.protobuf.Any as unknown) as GeneratedType], ...customTypes, ]); } - public lookupType(name: string): protobuf.Type | undefined { + public lookupType(name: string): GeneratedType | undefined { return this.types.get(name); } } diff --git a/packages/demo-protobuf/types/registry.d.ts b/packages/demo-protobuf/types/registry.d.ts index 9b011609..b660a7f7 100644 --- a/packages/demo-protobuf/types/registry.d.ts +++ b/packages/demo-protobuf/types/registry.d.ts @@ -1,6 +1,18 @@ import protobuf from "protobufjs"; +export declare type GeneratedType = { + readonly create: (properties?: { [k: string]: any }) => protobuf.Message<{}>; + readonly encode: ( + message: + | protobuf.Message<{}> + | { + [k: string]: any; + }, + writer?: protobuf.Writer, + ) => protobuf.Writer; + readonly decode: (reader: protobuf.Reader | Uint8Array, length?: number) => protobuf.Message<{}>; +}; export declare class Registry { private readonly types; - constructor(customTypes?: readonly [string, protobuf.Type][]); - lookupType(name: string): protobuf.Type | undefined; + constructor(customTypes?: Iterable<[string, GeneratedType]>); + lookupType(name: string): GeneratedType | undefined; }