cosmos / cosmjs

The Swiss Army knife to power JavaScript based client solutions ranging from Web apps/explorers over browser extensions to server-side clients like faucets/scrapers.
https://cosmos.github.io/cosmjs/
Apache License 2.0
649 stars 333 forks source link

Decentralize message type generation #222

Closed webmaster128 closed 4 years ago

webmaster128 commented 4 years ago

We want to support many different message type, some from the Cosmos SDK and some from custom blockchains that are not standardized. The user of CosmJS should have the most common message types ready to be used shipped with this library. But it should also be possible to add new message types from the caller code.

Some thoughts that might help getting there:

  1. Protojs allow dynamically adding .proto files: https://github.com/protobufjs/protobuf.js/blob/master/README.md#using-proto-files
  2. Maybe we can utilize JSON descriptors or reflection to dynamically add types
  3. The library @node-a-team/ts-amino uses decorators to add encoding information to TypeScript types as shown here: https://github.com/chainapsis/cosmosjs/blob/master/src/common/stdTx.ts. Maybe we can use decorators as well to start with a TypeScript class and annotate it with all the necessary runtime information, like:
@CosmosMessage("cosmos.bank.MsgSend") // adds this type to a registry using the string as the `type_url`
export class MsgSend {
  @Field.Bytes(1 /* protobuf field number goes here*/)
  public fromAddress: Uint8Array;

  @Field.Bytes(2, { fieldName: "to_address" /* runtime field name for protobuf; probably irrelevant since not encoded into the proto bytes*/  })
  public toAddress: Uint8Array;

  @Field.Repeated(3, { type: Type.Defined }) // don't know what this does but if it worked for Amino, it can probably be adapted
  public amount: Coin[];
}
willclarktech commented 4 years ago

Decorators look pretty nice but are currently only a stage 2 proposal for JS and an experimental feature in TS.

willclarktech commented 4 years ago

Good table of tradeoffs here: https://github.com/protobufjs/protobuf.js/blob/7bacfc8/README.md#reflection-vs-static-code

webmaster128 commented 4 years ago

Decorators look pretty nice but are currently only a stage 2 proposal for JS and an experimental feature in TS.

Could you check the browser support for them? The Angular framework uses them for many years now, starting from Angular 2, the first version that uses TypeScript released in 2016. Maybe there is a way to transpile them into older JavaScript?

willclarktech commented 4 years ago

Doesn't look good: https://kangax.github.io/compat-table/esnext/#test-Class_and_Property_Decorators_a_href=_https://github.com/wycats/javascript-decorators_class_decorators_/a

webmaster128 commented 4 years ago

Ah, the support is there for "Type-Script + core-js 3", which is the combination documented in the Angular browser support, where they use the term "ES2015 polyfill" for core-js 3.

I think it is feasable to require the combination Type-Script + core-js 3 when creating custom message types in the application. And for the types shipped with the library we don't use decorators.

willclarktech commented 4 years ago

Closed in #221