WIP: Add WASM/wasm-bindgen support to cdk-ffi #1665

Draft
crodas wants to merge 1 commit from crodas/feature/experiment-wasm-ffi into main
crodas commented 2026-02-21 14:06:38 +00:00 (Migrated from github.com)

Description

This is the evolution of #1660, using the same FFI crate for exporting the wasm data.

Add a WebAssembly target to cdk-ffi alongside the existing UniFFI native bindings. This enables using the CDK wallet directly from browsers via wasm-bindgen.

Key changes:

  • Make UniFFI, SQLite, and tokio multi-thread dependencies optional behind a new uniffi-bindings feature (enabled by default)
  • Add wasm feature with wasm-bindgen, js-sys, web-sys, and related dependencies
  • Implement LocalStorage-backed WalletDatabase for browser persistence
  • Add wasm-bindgen wallet exports wrapping the existing FFI Wallet API
  • Add WASM init function with panic hook and console tracing
  • Gate native-only modules (database, logging, sqlite, wallet_repository) behind uniffi-bindings feature
  • Replace std::time::SystemTime with web_time::SystemTime across all crates to avoid WASM panics
  • Add web-time dependency to cdk-fake-wallet, cdk-ldk-node, and cdk-npubcash
  • Include sample index.html for browser testing
  • Add justfile targets for WASM build

This larger PR will be split into smaller chunks; for instance, https://github.com/lescuer97/cdk/pull/17 and #1662 are the first contributions. The following contribution would be to use the native websocket in #1615 with no external deps. The goal is to have this PR smaller, but here is a rough north picture of wasm support.

Here is a wallet in the browser using the wasm build:

image

Notes to the reviewers


Suggested CHANGELOG Updates

CHANGED

ADDED

REMOVED

FIXED


Checklist

### Description This is the evolution of #1660, using the same FFI crate for exporting the wasm data. Add a WebAssembly target to cdk-ffi alongside the existing UniFFI native bindings. This enables using the CDK wallet directly from browsers via wasm-bindgen. Key changes: - Make UniFFI, SQLite, and tokio multi-thread dependencies optional behind a new `uniffi-bindings` feature (enabled by default) - Add `wasm` feature with wasm-bindgen, js-sys, web-sys, and related dependencies - Implement LocalStorage-backed WalletDatabase for browser persistence - Add wasm-bindgen wallet exports wrapping the existing FFI Wallet API - Add WASM init function with panic hook and console tracing - Gate native-only modules (database, logging, sqlite, wallet_repository) behind `uniffi-bindings` feature - Replace std::time::SystemTime with web_time::SystemTime across all crates to avoid WASM panics - Add web-time dependency to cdk-fake-wallet, cdk-ldk-node, and cdk-npubcash - Include sample index.html for browser testing - Add justfile targets for WASM build This larger PR will be split into smaller chunks; for instance, https://github.com/lescuer97/cdk/pull/17 and #1662 are the first contributions. The following contribution would be to use the native websocket in #1615 with no external deps. The goal is to have this PR smaller, but here is a rough north picture of wasm support. Here is a wallet in the browser using the wasm build: <img width="3762" height="3742" alt="image" src="https://github.com/user-attachments/assets/5ab6b81d-7c80-4902-9753-21ba37352cc7" /> ----- ### Notes to the reviewers <!-- In this section you can include notes directed to the reviewers, like explaining why some parts of the PR were done in a specific way --> ----- ### Suggested [CHANGELOG](https://github.com/cashubtc/cdk/blob/main/CHANGELOG.md) Updates <!-- Please do not edit the actual changelog but note what you changed here. --> #### CHANGED #### ADDED #### REMOVED #### FIXED ---- ### Checklist * [ ] I followed the [code style guidelines](https://github.com/cashubtc/cdk/blob/main/CODE_STYLE.md) * [ ] I ran `just final-check` before committing
thesimplekid (Migrated from github.com) reviewed 2026-02-22 23:16:03 +00:00
thesimplekid (Migrated from github.com) left a comment

This is better then #1660 in that it has less boiler plate. However, it still creates wasm specefic fns and boilerplate. Did you evaluate the use of the uniffi plugin project for js I recommended on that PR (https://github.com/jhugman/uniffi-bindgen-react-native)? Is there a specefic reason we cannot use that so our wasm and kotlin/swift bindings are the same?

I think this is our best model to follow it uses uniffi for all bindings including js, dart and go.

This is better then #1660 in that it has less boiler plate. However, it still creates wasm specefic fns and boilerplate. Did you evaluate the use of the uniffi plugin project for js I recommended on that PR (https://github.com/jhugman/uniffi-bindgen-react-native)? Is there a specefic reason we cannot use that so our wasm and kotlin/swift bindings are the same? I think [this](https://gitlab.com/ark-bitcoin/bark-ffi-bindings) is our best model to follow it uses uniffi for all bindings including js, dart and go.
crodas (Migrated from github.com) reviewed 2026-02-23 15:35:29 +00:00
thesimplekid (Migrated from github.com) reviewed 2026-02-23 15:49:18 +00:00
crodas (Migrated from github.com) reviewed 2026-02-23 15:52:22 +00:00
thesimplekid (Migrated from github.com) reviewed 2026-02-23 16:12:26 +00:00
This pull request has changes conflicting with the target branch.
  • Cargo.lock
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin crodas/feature/experiment-wasm-ffi:crodas/feature/experiment-wasm-ffi
git switch crodas/feature/experiment-wasm-ffi

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch main
git merge --no-ff crodas/feature/experiment-wasm-ffi
git switch crodas/feature/experiment-wasm-ffi
git rebase main
git switch main
git merge --ff-only crodas/feature/experiment-wasm-ffi
git switch crodas/feature/experiment-wasm-ffi
git rebase main
git switch main
git merge --no-ff crodas/feature/experiment-wasm-ffi
git switch main
git merge --squash crodas/feature/experiment-wasm-ffi
git switch main
git merge --ff-only crodas/feature/experiment-wasm-ffi
git switch main
git merge crodas/feature/experiment-wasm-ffi
git push origin main
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cashubtc/cdk!1665
No description provided.