hyperledger / iroha-javascript

JavaScript library for Iroha, a Distributed Ledger Technology (blockchain) platform.
https://wiki.hyperledger.org/display/iroha
Apache License 2.0
94 stars 64 forks source link

Different entrypoints in `@iroha2/crypto` & dependency inversion #77

Closed 0x009922 closed 2 years ago

0x009922 commented 2 years ago

The problem

At this point in time deploying Rust and WebAssembly to the web or other locations unfortunately isn't a trivial task to do. wasm-pack docs about deployment

Before this I was sure that web target works fine both in web and nodejs environment, and it was! But unfortunately not in all cases and with some reservations and pitfalls. It is ok, so why not just expose different entrypoints in the same library? Because such approach produces huge problems for the libraries that would depend on this one and be target-agnostic, e.g. @iroha2/client pkg.

The solution

In this PR library contains main "types" entrypoint in /types and target-specific ones in sub-directories. It has the main type - IrohaCryptoInterface, and each target-specific implementation exports the same crypto object that is compatible with this type (it is included into type-check of the monorepo as test). And with this type it is possible to write libraries that depends on it and injects a particular implementation in runtime.

What not to check

wasm-pack outputs at packages/iroha-crypto/(web|node|bundler) dirs. Btw there are index.js and index.d.ts files generated separately from it but also automatically.