0xPolygonMiden / miden-client

Client library that facilitates interaction with the Miden rollup
MIT License
25 stars 22 forks source link

Add WASM Code Approach 2 #397

Closed dagarcia7 closed 6 days ago

dagarcia7 commented 1 week ago

Summary

This PR creates a WASM-compatible version of the miden client so developers can use this tool both in the context of a web browser and as a CLI tool.

Approach

There are 4 main components to this approach -- WebClient, WebStore, WebRpcClient, and a miden-wasm at the root level equipped with a package.json and rollup.config.js to allow easy creation of a WASM, as well as easy publishing as an npm package.

WebClient Implementation of Client

The miden-client exposes a Client struct that represents a generic client capable of interacting with the miden node. Because the Client is exposed in a generic way, allowing its components to be customized for different contexts, I have implemented a version of the Client called WebClient. This WebClient uses a WebStore, and a WebRpcClient. This is similar to how the CLI tool implements a version of the Client that uses a SQLiteStore, and a TonicRpcClient.

WebStore Implementation of Store

The WebStore is a specific implementation of the generic Store trait designed for web environments. It uses IndexedDB as its backend storage method in order to provide robust, efficient, and persistent storage for web-based clients, enabling seamless interaction with the Miden node in web contexts.

The existing sqlite_store utilizes SQLite as its backend, leveraging its robust and efficient database capabilities for data persistence. SQLite is a widely-used, serverless, self-contained SQL database engine that provides reliable storage and efficient querying for applications.

However, SQLite is not compatible with WASM due to its reliance on native file system access and multi-threading, which are not available in the web environment. WASM operates within a restricted sandbox in the browser, lacking direct access to the underlying file system and threading capabilities that SQLite requires.

WebRpcClient Implementation of NodeRpcClient

The WebRpcClient is a specific implementation of the generic NodeRpcClient trait designed for web environments. The existing tonic_client implementation relies heavily on tonic and tokio, utilizing auto-generated code that is not compatible with WASM. This poses a challenge for web-based applications that need to interact with the Miden node.

To address this, the WebRpcClient has been developed to provide the same functionality but in a web-compatible manner. The key difference is that WebRpcClient uses tonic-web-wasm-client to auto-generate the necessary code, ensuring compatibility with WASM.

By using tonic-web-wasm-client, WebRpcClient can efficiently handle gRPC communications in web contexts, enabling seamless interaction with the Miden node while maintaining performance and reliability.

miden-wasm NPM Package Structure

In addition to implementing the necessary traits and the generic client for web contexts, I have also created a miden-wasm folder at the root level. This folder includes a package.json and a rollup.config.js, providing a structured environment for building a WASM based on the miden-client's Cargo.toml file.

This setup allows for seamless compilation of the Rust code to Wasm, leveraging Rollup for bundling and other build optimizations. By organizing the project in this manner, it becomes straightforward to compile the WASM module and package it for distribution.

Moreover, this structure facilitates the easy publication of the entire project as an npm package, enabling developers to integrate the miden-client functionality into their web applications effortlessly. The miden-wasm folder thus streamlines the build and distribution process, enhancing the accessibility and usability of the Wasm-based client.

What's Missing?

While this is a good first step toward allowing this code to execute on the web, there are still several areas that need further development. Missing elements include comprehensive documentation, automated unit and integration tests, general code cleanup, and robust error-handling.

The reason for putting up this PR now, despite these missing components, is that it is becoming increasingly difficult to maintain this code due to the rapid changes in the underlying miden-client, which evolves almost daily. Any modifications to the SQLite store, for example, necessitate corresponding changes to the WebStore using IndexedDB. The same goes for the RPC and higher-level CLI interfaces.

My goal with this PR is to ensure that any future changes to miden-client also consider the WASM implementation, allowing both to progress together. This will prevent the WASM version from falling behind and ensure that it remains in sync with the core functionality of the miden-client.