Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update xchain-cardano to work on browser #1366

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

hippocampus-web3
Copy link
Collaborator

This PR aims to make the xchain-cardano package work in browsers by resolving key incompatibilities with some dependencies.

Problem

The main issues preventing xchain-cardano from running in a browser environment are:

  1. @blockfrost/blockfrost-js is not browser-compatible because:
  • It depends on @emurgo/cardano-serialization-lib-nodejs, which is designed for Node.js.
  • It uses got, an HTTP request library meant for server environments only.
  1. @emurgo/cardano-serialization-lib-nodejs is incompatible with browsers due to its reliance on Node.js.
  • Attempts were made to use alternatives such as @emurgo/cardano-serialization-lib-asmjs and @emurgo/cardano-serialization-lib-browser. However, these versions are direct Rust-to-WASM compilations using wasm-pack and wasm2js, which rely on modern JavaScript features that are incompatible with Asgardex's current configuration.

Applied Solution

Since no available solutions were directly compatible with browser, the following measures were taken:

  1. Created a modified version of cardano-serialization-lib-asmjs
  • A custom package (@hippocampus-web3/cardano-serialization-lib-asmjs) was published with a Babel compilation that converts and exports it in CommonJS format, making it easier to load in Asgardex.

  • While this allows Asgardex to start correctly, Webpack still raises the following warning: Critical dependency: the request of a dependency is an expressionSo far, this warning has not caused any runtime issues, but extensive testing is recommended.

📦 NPM Package: @hippocampus-web3/cardano-serialization-lib-asmjs
🔗 Source Code: GitHub

  1. Modified @blockfrost/blockfrost-js to be browser-compatible
  • A forked version was created where: @emurgo/cardano-serialization-lib-nodejs was replaced with @hippocampus-web3/cardano-serialization-lib-asmjs. got was replaced with ky for making HTTP requests in web environments.

📦 NPM Package: @hippocampus-web3/blockfrost-js
🔗 Source Code: GitHub

⚠ Important Note: The reason why @blockfrost/blockfrost-js is officially server-only is that Blockfrost recommends not using the package on the client-side to avoid exposing API keys. This approach should only be applied if the risks of exposing the API key in the client are well understood.

Approaches to Consider

  1. Publishing the modified packages under the @xchainjs scope instead of a my personal namespace. The current approach was taken purely for dev speed.

  2. Avoiding the use of @blockfrost/blockfrost-js entirely and implementing a custom client within xchain-cardano with only the necessary methods, treating it as another provider.

  3. Further investigating potential fixes to load @emurgo/cardano-serialization-lib-asmjs in Asgardex despite its reliance on modern JavaScript features.

  4. Exploring a solution for the Webpack warning Critical dependency: the request of a dependency is an expression, though this seems to be a structural issue caused by wasm-pack-compiled JavaScript.

🔗 Relevant issues on the matter:

wasm-pack issue #822

Summary

This PR introduces modifications to enable xchain-cardano to function in browsers by replacing incompatible dependencies and applying temporary solutions to prevent Asgardex from failing to start. While Webpack still displays a warning, no runtime issues have been detected so far. Further testing is recommended, and alternative approaches should be evaluated to minimize reliance on modified dependencies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant