JavaScript/Web Assembly Binding for Indy Crypto
At the behest of the excellent Quorum Control company, I have carried out my first foray into both Rust and Web Assembly (abbr. Wasm), making a JavaScript/Wasm binding of the Hyperledger Indy Crypto library. The reason behind this undertaking is that Quorum Control (and others) need BLS cryptographic signature verification abilities in JavaScript applications (Web and Node.js), and Indy Crypto provides a solid implementation of this although in the Rust language.
Luckily, there are facilities for converting Rust code into Web Assembly, i.e. the wasm-bindgen tool. While still a fledgling project, it allows us to expose a JavaScript API for the BLS module of Indy Crypto. In practice, this is done by writing a set of Rust functions tagged so that they get exported by the binding generated by wasm-bindgen. Additionally, we produce two variants of the binding: One for Node.js and one for ECMAScript modules aware systems (such as the Webpack bundler), in order for it to work both server side and in the browser.
Using the binding from Node.js (i.e. on the server side) proved to be relatively straightforward, but importing it in the browser was trickier than I foresaw. The approach that I found to work was to produce a JavaScript bundle with the help of Webpack, which is able to include the Wasm code correctly, and to import this bundle dynamically as otherwise the Wasm import would fail.
The JavaScript/Wasm binding I contributed can be found in the wrappers/javascript directory of Indy Crypto. There’s API documentation in docs/index.html. You can also find an example of using the library from your client-side JavaScript code in examples/browser, which might prove useful as it turned out to be non-trivial (e.g. having to be dynamically imported).
I hope others may find this write-up helpful in their endeavours combining Rust and JavaScript!