Skip to content

Commit b74ea1b

Browse files
authored
Merge pull request #55 from joii2020/dev.wasm
Support WASM
2 parents 4b21caa + 6e07fec commit b74ea1b

File tree

21 files changed

+6047
-0
lines changed

21 files changed

+6047
-0
lines changed

.github/workflows/wasm.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: WASM
2+
3+
on:
4+
pull_request:
5+
paths: [wasm/**]
6+
push:
7+
paths: [wasm/**]
8+
9+
jobs:
10+
examples:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
with:
15+
submodules: recursive
16+
17+
- uses: actions/cache@v4
18+
with:
19+
path: |
20+
~/.cargo/bin/
21+
~/.cargo/registry/index/
22+
~/.cargo/registry/cache/
23+
~/.cargo/git/db/
24+
target/
25+
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
26+
27+
- uses: pnpm/action-setup@v4
28+
with:
29+
version: 10
30+
run_install: false
31+
32+
- uses: actions/setup-node@v4
33+
with:
34+
node-version: 20
35+
cache: 'pnpm'
36+
cache-dependency-path: '**/pnpm-lock.yaml'
37+
38+
- name: Install wasm-pack
39+
run: which wasm-pack >/dev/null 2>&1 || cargo install wasm-pack
40+
41+
- name: Install ckb-debugger
42+
run: which ckb-debugger >/dev/null 2>&1 || cargo install ckb-debugger
43+
44+
- name: wasm-test
45+
run: cd wasm && make check
46+
47+
- name: wasm-test
48+
run: cd wasm && make test
49+
50+
- name: build-web
51+
run: cd wasm && make build-web

wasm/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/target
2+
**/node_modules
3+
/pkg-*
4+
examples/pkg-*
5+
tests/ckb-contract/dist/

wasm/Cargo.lock

Lines changed: 194 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wasm/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "sparse-merkle-tree-wasm"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[lib]
7+
crate-type = ["cdylib"]
8+
9+
[dependencies]
10+
sparse-merkle-tree = { path = "../", default-features = false, features = [ "std", "with-blake2b-ref" ] }
11+
wasm-bindgen = "0.2.100"
12+
web-sys = { version = "0.3.77", features = [ "console" ] }
13+
js-sys = "0.3.77"
14+
hex = "0.4.3"
15+
blake2b-ref = "0.3.1"

wasm/Makefile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
default: build-nodejs
2+
3+
check:
4+
cargo fmt
5+
cargo clippy
6+
cargo check
7+
8+
build-nodejs:
9+
wasm-pack build --target nodejs --out-dir pkg-nodejs --dev
10+
11+
build-web:
12+
wasm-pack build --target web --out-dir pkg-web
13+
14+
build-test: build-nodejs
15+
cd tests && pnpm install
16+
cd tests/ckb-contract && pnpm install
17+
cd tests && pnpm add ../pkg-nodejs
18+
cd tests/ckb-contract && pnpm build
19+
20+
test: build-test
21+
cd tests && pnpm jest
22+
23+
run-example: build-web
24+
cp -r pkg-web/ examples/
25+
pnpm dlx http-server ./examples/ -p 8000
26+
27+
install-tools:
28+
which wasm-pack >/dev/null 2>&1 || cargo install wasm-pack
29+
which ckb-debugger >/dev/null 2>&1 || cargo install ckb-debugger
30+
31+
clean:
32+
cargo clean
33+
rm -rf pkg-nodejs pkg-web

wasm/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Sparse Merkle Tree WASM
2+
3+
This project compiles the Rust implementation of a Sparse Merkle Tree into WebAssembly (WASM), making it accessible for JavaScript/TypeScript applications.
4+
5+
## Build
6+
7+
Currently, both Node.js and web environments are supported. Developers can choose the appropriate build target:
8+
9+
```sh
10+
make build-nodejs
11+
```
12+
13+
or
14+
15+
```sh
16+
make build-web
17+
```
18+
19+
## Run Web Example
20+
21+
```sh
22+
make run-example
23+
```
24+
25+
The demo page does not include a UI. Instead, it automatically runs SMT-related scripts after loading.
26+
27+
## Usage
28+
29+
* **Contract test example**: See line 48 in `tests/tests/smt-ckb.test.ts`
30+
* **Web example**: See line 12 in `examples/index.html`

wasm/examples/index.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<html>
2+
3+
<head>
4+
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
5+
<title>Test SMT</title>
6+
</head>
7+
8+
<body>
9+
<script type="module">
10+
import init, { ckb_blake2b_256, CkbSmt, verify_proof } from '../pkg-web/sparse_merkle_tree_wasm.js';
11+
12+
async function run() {
13+
await init();
14+
15+
const smt = new CkbSmt();
16+
17+
const k1 = ckb_blake2b_256("aaa");
18+
const v1 = ckb_blake2b_256("123aa");
19+
20+
const k2 = ckb_blake2b_256("bbb");
21+
const v2 = ckb_blake2b_256(new Uint8Array([0xaa, 0xbb, 0xcc]));
22+
23+
const k3 = ckb_blake2b_256("ccc");
24+
const v3 = ckb_blake2b_256("232cc");
25+
26+
smt.update(k1, v1);
27+
28+
smt.update(k2, v2);
29+
const root2 = smt.root();
30+
31+
smt.update(k3, v3);
32+
const root3 = smt.root();
33+
34+
let proof = smt.get_proof([k1, k3]);
35+
36+
console.log(verify_proof(root2, proof, [[k1, v1], [k3, new Uint8Array(32)]]));
37+
console.log(verify_proof(root3, proof, [[k1, v1], [k3, v3]]));
38+
}
39+
40+
run();
41+
</script>
42+
</body>
43+
44+
</html>

0 commit comments

Comments
 (0)