1+ import type * as bbw from "@bufbuild/protobuf/wire" ;
12import { fixUint8Array } from "@cosmjs/encoding" ;
23import { BinaryWriter } from "cosmjs-types/binary" ;
34import { MsgSend } from "cosmjs-types/cosmos/bank/v1beta1/tx" ;
@@ -25,7 +26,7 @@ export interface TelescopeGeneratedType {
2526}
2627
2728/**
28- * A type generated by [ts-proto](https://github.com/stephenh/ts-proto).
29+ * A type generated by [ts-proto](https://github.com/stephenh/ts-proto) version 1 .
2930 */
3031export interface TsProtoGeneratedType {
3132 readonly encode : ( message : any | { [ k : string ] : any } , writer ?: protobuf . Writer ) => protobuf . Writer ;
@@ -34,6 +35,29 @@ export interface TsProtoGeneratedType {
3435 // Methods from ts-proto types we don't need
3536 // readonly fromJSON: (object: any) => any;
3637 // readonly toJSON: (message: any | { [k: string]: any }) => unknown;
38+
39+ // This was added at some point in 2023 and looks pretty compatible to the PbjsGeneratedType constructor.
40+ // but we don't need it as long as Telescope does not have it. `fromPartial` is widespread and works well.
41+ // See https://github.com/stephenh/ts-proto/pull/760
42+ // readonly create: (object?: any) => any;
43+ }
44+
45+ /**
46+ * A type generated by [ts-proto](https://github.com/stephenh/ts-proto) version 2.
47+ * See "ts-proto 2.x Release Notes" at https://github.com/stephenh/ts-proto/blob/v2.10.0/README.markdown#ts-proto-2x-release-notes
48+ */
49+ export interface TsProto2GeneratedType {
50+ readonly encode : ( message : any | { [ k : string ] : any } , writer ?: bbw . BinaryWriter ) => bbw . BinaryWriter ;
51+ readonly decode : ( input : Uint8Array | bbw . BinaryReader , length ?: number ) => any ;
52+ readonly fromPartial : ( object : any ) => any ;
53+ // Methods from ts-proto types we don't need
54+ // readonly fromJSON: (object: any) => any;
55+ // readonly toJSON: (message: any | { [k: string]: any }) => unknown;
56+
57+ // This was added at some point in 2023 and looks pretty compatible to the PbjsGeneratedType constructor.
58+ // but we don't need it as long as Telescope does not have it. `fromPartial` is widespread and works well.
59+ // See https://github.com/stephenh/ts-proto/pull/760
60+ // readonly create: (object?: any) => any;
3761}
3862
3963/**
@@ -48,19 +72,23 @@ export interface PbjsGeneratedType {
4872 readonly decode : ( reader : protobuf . Reader | Uint8Array , length ?: number ) => any ;
4973}
5074
51- export type GeneratedType = TelescopeGeneratedType | TsProtoGeneratedType | PbjsGeneratedType ;
52-
53- export function isTelescopeGeneratedType ( type : GeneratedType ) : type is TelescopeGeneratedType {
54- const casted = type as TelescopeGeneratedType ;
55- return typeof casted . fromPartial === "function" && typeof casted . typeUrl == "string" ;
56- }
57-
58- export function isTsProtoGeneratedType ( type : GeneratedType ) : type is TsProtoGeneratedType {
59- return typeof ( type as TsProtoGeneratedType ) . fromPartial === "function" ;
75+ export type GeneratedType =
76+ | TelescopeGeneratedType
77+ | TsProtoGeneratedType
78+ | TsProto2GeneratedType
79+ | PbjsGeneratedType ;
80+
81+ export function hasFromPartial (
82+ type : GeneratedType ,
83+ ) : type is TelescopeGeneratedType | TsProtoGeneratedType | TsProto2GeneratedType {
84+ return (
85+ typeof ( type as TelescopeGeneratedType | TsProtoGeneratedType | TsProto2GeneratedType ) . fromPartial ===
86+ "function"
87+ ) ;
6088}
6189
62- export function isPbjsGeneratedType ( type : GeneratedType ) : type is PbjsGeneratedType {
63- return ! isTsProtoGeneratedType ( type ) ;
90+ export function hasCreate ( type : GeneratedType ) : type is PbjsGeneratedType {
91+ return typeof ( type as PbjsGeneratedType ) . create === "function" ;
6492}
6593
6694const defaultTypeUrls = {
@@ -137,7 +165,7 @@ export class Registry {
137165 *
138166 * const Coin = registry.lookupType("/cosmos.base.v1beta1.Coin");
139167 * assert(Coin); // Ensures not unset
140- * assert(isTsProtoGeneratedType (Coin)); // Ensures this is the type we expect
168+ * assert(hasFromPartial (Coin)); // Ensures this is the type we expect
141169 *
142170 * // Coin is typed TsProtoGeneratedType now.
143171 * ```
@@ -167,10 +195,7 @@ export class Registry {
167195 return this . encodeTxBody ( value ) ;
168196 }
169197 const type = this . lookupTypeWithError ( typeUrl ) ;
170- const instance =
171- isTelescopeGeneratedType ( type ) || isTsProtoGeneratedType ( type )
172- ? type . fromPartial ( value )
173- : type . create ( value ) ;
198+ const instance = hasFromPartial ( type ) ? type . fromPartial ( value ) : type . create ( value ) ;
174199 return fixUint8Array ( type . encode ( instance ) . finish ( ) ) ;
175200 }
176201
0 commit comments