Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions .github/workflows/wasm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: WASM

on:
pull_request:
paths: [wasm/**]
push:
paths: [wasm/**]

jobs:
examples:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive

- uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- uses: pnpm/action-setup@v4
with:
version: 10
run_install: false

- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'pnpm'
cache-dependency-path: '**/pnpm-lock.yaml'

- name: Install wasm-pack
run: which wasm-pack >/dev/null 2>&1 || cargo install wasm-pack

- name: Install ckb-debugger
run: which ckb-debugger >/dev/null 2>&1 || cargo install ckb-debugger

- name: wasm-test
run: cd wasm && make check

- name: wasm-test
run: cd wasm && make test

- name: build-web
run: cd wasm && make build-web
5 changes: 5 additions & 0 deletions wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/target
**/node_modules
/pkg-*
examples/pkg-*
tests/ckb-contract/dist/
194 changes: 194 additions & 0 deletions wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "sparse-merkle-tree-wasm"
version = "0.1.0"
edition = "2024"

[lib]
crate-type = ["cdylib"]

[dependencies]
sparse-merkle-tree = { path = "../", default-features = false, features = [ "std", "with-blake2b-ref" ] }
wasm-bindgen = "0.2.100"
web-sys = { version = "0.3.77", features = [ "console" ] }
js-sys = "0.3.77"
hex = "0.4.3"
blake2b-ref = "0.3.1"
33 changes: 33 additions & 0 deletions wasm/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
default: build-nodejs

check:
cargo fmt
cargo clippy
cargo check

build-nodejs:
wasm-pack build --target nodejs --out-dir pkg-nodejs --dev

build-web:
wasm-pack build --target web --out-dir pkg-web

build-test: build-nodejs
cd tests && pnpm install
cd tests/ckb-contract && pnpm install
cd tests && pnpm add ../pkg-nodejs
cd tests/ckb-contract && pnpm build

test: build-test
cd tests && pnpm jest

run-example: build-web
cp -r pkg-web/ examples/
pnpm dlx http-server ./examples/ -p 8000

install-tools:
which wasm-pack >/dev/null 2>&1 || cargo install wasm-pack
which ckb-debugger >/dev/null 2>&1 || cargo install ckb-debugger

clean:
cargo clean
rm -rf pkg-nodejs pkg-web
30 changes: 30 additions & 0 deletions wasm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Sparse Merkle Tree WASM

This project compiles the Rust implementation of a Sparse Merkle Tree into WebAssembly (WASM), making it accessible for JavaScript/TypeScript applications.

## Build

Currently, both Node.js and web environments are supported. Developers can choose the appropriate build target:

```sh
make build-nodejs
```

or

```sh
make build-web
```

## Run Web Example

```sh
make run-example
```

The demo page does not include a UI. Instead, it automatically runs SMT-related scripts after loading.

## Usage

* **Contract test example**: See line 48 in `tests/tests/smt-ckb.test.ts`
* **Web example**: See line 12 in `examples/index.html`
44 changes: 44 additions & 0 deletions wasm/examples/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<html>

<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<title>Test SMT</title>
</head>

<body>
<script type="module">
import init, { ckb_blake2b_256, CkbSmt, verify_proof } from '../pkg-web/sparse_merkle_tree_wasm.js';

async function run() {
await init();

const smt = new CkbSmt();

const k1 = ckb_blake2b_256("aaa");
const v1 = ckb_blake2b_256("123aa");

const k2 = ckb_blake2b_256("bbb");
const v2 = ckb_blake2b_256(new Uint8Array([0xaa, 0xbb, 0xcc]));

const k3 = ckb_blake2b_256("ccc");
const v3 = ckb_blake2b_256("232cc");

smt.update(k1, v1);

smt.update(k2, v2);
const root2 = smt.root();

smt.update(k3, v3);
const root3 = smt.root();

let proof = smt.get_proof([k1, k3]);

console.log(verify_proof(root2, proof, [[k1, v1], [k3, new Uint8Array(32)]]));
console.log(verify_proof(root3, proof, [[k1, v1], [k3, v3]]));
}

run();
</script>
</body>

</html>
Loading
Loading