Skip to content

Commit d350c62

Browse files
committed
wasm: fix wasm example to run with the es6 build, use cjs for esplora
Update the CI and docs accordingly.
1 parent 12f5ac4 commit d350c62

File tree

5 files changed

+72
-58
lines changed

5 files changed

+72
-58
lines changed

.gitlab-ci.yml

+5-4
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,14 @@ build_wally_release_files:
139139
- mv release wallycore-android-jni
140140
- tar czf wally_dist/wallycore-android-jni.tar.gz --remove-files wallycore-android-jni
141141
- source /opt/emsdk/emsdk_env.sh
142-
- tools/build_wasm.sh
142+
- EMCC_OPTIONS="-s EXPORT_ES6=1 -s MODULARIZE=1" tools/build_wasm.sh
143+
- cp contrib/wally_js_example.html dist/wallycore.html
143144
- cd dist
144-
- tar czf wallycore-wasm.tar.gz --remove-files wallycore.html wallycore.js wallycore.wasm
145+
- tar czf ../wally_dist/wallycore-wasm.tar.gz --remove-files wallycore.html wallycore.js wallycore.wasm
145146
- cd ..
146-
- EXPORTED_FUNCTIONS="['_malloc','_free','_wally_init','_wally_asset_value_commitment','_wally_asset_generator_from_bytes']" tools/build_wasm.sh --no-modularize
147+
- EMCC_OPTIONS="-s MODULARIZE=1" - EXPORTED_FUNCTIONS="['_malloc','_free','_wally_init','_wally_asset_value_commitment','_wally_asset_generator_from_bytes']" tools/build_wasm.sh
147148
- cd dist
148-
- tar czf esplora-wasm.tar.gz --remove-files wallycore.html wallycore.js wallycore.wasm
149+
- tar czf ../wally_dist/esplora-wasm.tar.gz --remove-files wallycore.js wallycore.wasm
149150
- cd ..
150151
- sphinx-build -b html -a -c docs/source docs/source docs/build/html
151152
- cd docs/build

README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,21 @@ $ source $HOME/emsdk/emsdk_env.sh
191191
# Optionally set the list of wally functions to export to wasm (default: all)
192192
$ export EXPORTED_FUNCTIONS="['_malloc','_free','_wally_init','_wally_cleanup',...]"
193193
194+
# Optionally set emcc options, e.g. to build as an ES6 module:
195+
$ export EMCC_OPTIONS="-s EXPORT_ES6=1 -s MODULARIZE=1"
196+
194197
# Build
195198
$ ./tools/build_wasm.sh [--disable-elements]
196199
```
197200

198201
Note that emsdk v3.1.27 or later is required.
199202

200-
The script `tools/build_wasm.sh` builds the `wallycore.html` example as well
201-
as the required `wallycore.js` and `wallycore.wasm` files, which can be used
202-
as an example for your own WebAssembly projects.
203+
The file `contrib/wally_js_example.html` is an example page using the
204+
`wallycore.js` and `wallycore.wasm` files, which can be used as an example
205+
for your own WebAssembly projects.
203206

204-
Open `wallycore.html` in a browser via a webserver like [nginx](https://www.nginx.com/)
205-
or `python2 -m SimpleHTTPServer 8000` to run the example.
207+
Open `wally_js_example.html` in a browser via a webserver like [nginx](https://www.nginx.com/)
208+
or `python3 -m http.server` to run the example.
206209

207210
## Cleaning
208211

contrib/shell_minimal.html

-41
This file was deleted.

contrib/wally_js_example.html

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<!doctype html>
2+
<html lang="en-us">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6+
<title>Wallycore JS Example</title>
7+
<style>
8+
textarea { font-family: monospace; width: 80%; padding-right: 0; margin-left: auto; margin-right: auto; display: block; }
9+
</style>
10+
</head>
11+
<link rel="icon" href="data:,">
12+
<body>
13+
<textarea id="output" rows="16"></textarea>
14+
<script type='text/javascript'>
15+
var element = document.getElementById('output');
16+
var wally_example = function() {
17+
const as_utf8 = function(ptr) { return Module.UTF8ToString(Module.getValue(ptr, '*')); };
18+
const b2h = function(buf) { return Array.prototype.map.call(buf, x => ('00' + x.toString(16)).slice(-2)).join(''); };
19+
const wally_init = Module.cwrap("wally_init", ['number'], ['number']);
20+
const wally_secp_randomize = Module.cwrap("wally_secp_randomize", ['number'], ['array', 'number']);
21+
const bip39_mnemonic_from_bytes = Module.cwrap('bip39_mnemonic_from_bytes', ['number'], ['number', 'array', 'number', 'number']);
22+
const wally_free_string = Module.cwrap('wally_free_string', ['number'], ['number']);
23+
24+
// Initialize wally
25+
var text = "init: " + wally_init(0) + '\n';
26+
var entropy = new Uint8Array(32); // WALLY_SECP_RANDOMIZE_LEN
27+
window.crypto.getRandomValues(entropy);
28+
text = text + "secp_randomize: " + wally_secp_randomize(entropy, entropy.length) + '\n';
29+
30+
// Create a BIP39 seed
31+
var seed = new Uint8Array(16); // BIP39_ENTROPY_LEN_128
32+
window.crypto.getRandomValues(seed);
33+
text += "seed: " + b2h(seed) + '\n';
34+
35+
// Generate a mnemonic from it
36+
var ptr = Module._malloc(4); // Holds our output pointer
37+
var ret = bip39_mnemonic_from_bytes(null, seed, seed.length, ptr);
38+
const mnemonic = as_utf8(ptr);
39+
text = text + "mnemonic: " + mnemonic + '\n';
40+
wally_free_string(Module.getValue(ptr, '*'));
41+
Module._free(ptr);
42+
43+
// Display our output
44+
element.value = text;
45+
}
46+
var Module = {
47+
preRun: [],
48+
postRun: [wally_example],
49+
};
50+
</script>
51+
<script type="module">
52+
import InitWally from "./wallycore.js";
53+
InitWally(Module);
54+
</script>
55+
</body>
56+
</html>

tools/build_wasm.sh

+3-8
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ if [ "$1" = "--disable-elements" ]; then
77
DISABLE_ELEMENTS="--disable-elements --disable-elements-abi"
88
shift
99
fi
10-
MODULARIZE="-s MODULARIZE=1"
11-
if [ "$1" = "--no-modularize" ]; then
12-
MODULARIZE=""
13-
fi
1410

1511
num_jobs=4
1612
if [ -f /proc/cpuinfo ]; then
@@ -34,9 +30,9 @@ export CFLAGS="-fno-stack-protector"
3430
emconfigure ./configure --build=$HOST_OS ac_cv_c_bigendian=no --disable-swig-python --disable-swig-java $DISABLE_ELEMENTS --disable-tests --enable-export-all --enable-wasm-interface
3531
emmake make -j $num_jobs
3632

37-
EMCC_OPTIONS="$EMCC_OPTIONS -s EXPORT_ES6=1 ${MODULARIZE} -s EXPORT_NAME=InitWally -s WASM_BIGINT"
33+
EMCC_OPTIONS="$EMCC_OPTIONS -s EXPORT_NAME=InitWally -s WASM_BIGINT"
3834
: ${OPTIMIZATION_LEVEL:=3}
39-
: ${EXPORTED_RUNTIME_METHODS:='cwrap,ccall,malloc,getValue,UTF8ToString'}
35+
: ${EXPORTED_RUNTIME_METHODS:='cwrap,ccall,getValue,UTF8ToString'}
4036
# Get the list of functions to export
4137
source ./tools/wasm_exports.sh
4238

@@ -48,5 +44,4 @@ emcc -O$OPTIMIZATION_LEVEL \
4844
-s FILESYSTEM=0 \
4945
$EMCC_OPTIONS \
5046
./src/.libs/*.o src/secp256k1/src/*.o src/ccan/ccan/*/.libs/*.o src/ccan/ccan/*/*/.libs/*.o \
51-
-o dist/wallycore.html \
52-
--shell-file contrib/shell_minimal.html
47+
-o dist/wallycore.js

0 commit comments

Comments
 (0)