penumbra-zone / penumbra

Penumbra is a fully private proof-of-stake network and decentralized exchange for the Cosmos ecosystem.
https://penumbra.zone
Apache License 2.0
373 stars 292 forks source link

[wasm] wasm crate should provide useful types #3309

Open turbocrime opened 10 months ago

turbocrime commented 10 months ago

the wasm crate should produce an npm package that exports useful types.

the wasm crate build currently emits a useless declaration asserting everything is "any" type.

the completely separate wasm-ts wrapper package in the web repo is essentially a shim that reintroduces type information. requiring manual type annotation by a separate wrapper package is incorrect.

type declarations should be colocated with the implementation, even if that requires additional tooling or manual annotation inside the rust code.

TalDerei commented 9 months ago

https://dawchihliou.github.io/articles/share-rust-types-with-typescript-for-webassembly-in-30-seconds

turbocrime commented 9 months ago

i've spoken to @grod220 about tsify, but iirc they suggested it was unsuitable for some reason. would be interesting to get some detail on what blocks tsify, vs what might block ts-rs (seems to have serde integration but no wasm bindgen integration) or wasm bindgen's typescript_type and typescript_custom_section which could be used for manual annotation

grod220 commented 9 months ago

This is a very worthwhile thing to explore again. The first attempt in going through this, it became simply easier to do type annotation+validation in wrappers given we need to associate the any's with the automatically generated BufBuild types. But it’s sad we cannot simply inherit all of the correct types (somehow).

grod220 commented 9 months ago

Completely random thought, but what if we defined the wasm crate functions as implementing a grpc service (aka make protobuf definitions)? Then perhaps we could inherit bufbuild type annotations to calls to this service.

This would solve issues with types needing to stay updated (given they would share definitions between the other penumbra protos) and we retain the benefit of all of the BufBuild automatic type/class generation.

The only tricky thing would be writing the transport package that would route a request to a wasm function.

turbocrime commented 9 months ago

keeping wasm crate exports as close as possible to existing types, such as proto types, buf request/response types or service/method impl types, is the goal.

i understand it is frustrating to annotate types in the rust code that essentially duplicate a type that exists outside of the rust code, but it is worse to annotate nothing in the rust code, and then annotate types in a wrapper package that essentially duplicate etc etc

is it possible to use typescript_type independently of typescript_custom_section, or use typescript_custom_section to simply import { ExistingX } from '...'; or define a export type WasmX = ExistingX;?

and again, i assert wasm bindgen is capable of handling and emitting real javascript objects, not just simple json, so we should be able to use buf's constructors. at worst, if that really is impossible, we could emit json with @type annotation and then use buf tooling for reflection.

transport package is already routing to impls in the extension. complying with impl types will only simplify that.