demo-protobuf: Improve registry types

This commit is contained in:
willclarktech 2020-06-16 09:49:00 +01:00
parent 8e83996a3b
commit 78a96cf06c
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
3 changed files with 33 additions and 12 deletions

View File

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

View File

@ -2,20 +2,29 @@ import protobuf from "protobufjs";
import { cosmos_sdk as cosmosSdk, google } from "./generated/codecimpl";
export class Registry {
private readonly types: Map<string, protobuf.Type>;
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<string, protobuf.Type>([
["/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<string, GeneratedType>;
constructor(customTypes: Iterable<[string, GeneratedType]> = []) {
this.types = new Map<string, GeneratedType>([
["/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);
}
}

View File

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