Update xchain-cardano to work on browser #1366
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
Applied Solution
Since no available solutions were directly compatible with browser, the following measures were taken:
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 expression
So 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
@blockfrost/blockfrost-js
to be browser-compatible@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
Publishing the modified packages under the @xchainjs scope instead of a my personal namespace. The current approach was taken purely for dev speed.
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.
Further investigating potential fixes to load
@emurgo/cardano-serialization-lib-asmjs
in Asgardex despite its reliance on modern JavaScript features.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.