demo-protobuf: Add GeneratedType interface

This commit is contained in:
willclarktech 2020-06-16 10:19:19 +01:00
parent 78a96cf06c
commit 21e29d1f25
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
2 changed files with 15 additions and 17 deletions

View File

@ -2,24 +2,21 @@ import protobuf from "protobufjs";
import { cosmos_sdk as cosmosSdk, google } from "./generated/codecimpl";
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<{}>;
};
interface GeneratedType {
readonly create: (properties?: { [k: string]: any }) => any;
readonly encode: (message: any | { [k: string]: any }, writer?: protobuf.Writer) => protobuf.Writer;
readonly decode: (reader: protobuf.Reader | Uint8Array, length?: number) => any;
}
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],
["/cosmos.Coin", cosmosSdk.v1.Coin],
["/cosmos.bank.MsgSend", cosmosSdk.x.bank.v1.MsgSend],
["/cosmos.tx.TxBody", cosmosSdk.tx.v1.TxBody],
["/google.protobuf.Any", google.protobuf.Any],
...customTypes,
]);
}

View File

@ -1,18 +1,19 @@
import protobuf from "protobufjs";
export declare type GeneratedType = {
readonly create: (properties?: { [k: string]: any }) => protobuf.Message<{}>;
interface GeneratedType {
readonly create: (properties?: { [k: string]: any }) => any;
readonly encode: (
message:
| protobuf.Message<{}>
| any
| {
[k: string]: any;
},
writer?: protobuf.Writer,
) => protobuf.Writer;
readonly decode: (reader: protobuf.Reader | Uint8Array, length?: number) => protobuf.Message<{}>;
};
readonly decode: (reader: protobuf.Reader | Uint8Array, length?: number) => any;
}
export declare class Registry {
private readonly types;
constructor(customTypes?: Iterable<[string, GeneratedType]>);
lookupType(name: string): GeneratedType | undefined;
}
export {};