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
9 changes: 5 additions & 4 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,14 @@ build_wally_release_files:
- mv release wallycore-android-jni
- tar czf wally_dist/wallycore-android-jni.tar.gz --remove-files wallycore-android-jni
- source /opt/emsdk/emsdk_env.sh
- tools/build_wasm.sh
- EMCC_OPTIONS="-s EXPORT_ES6=1 -s MODULARIZE=1" tools/build_wasm.sh
- cp contrib/wally_js_example.html dist/wallycore.html
- cd dist
- tar czf wallycore-wasm.tar.gz --remove-files wallycore.html wallycore.js wallycore.wasm
- tar czf ../wally_dist/wallycore-wasm.tar.gz --remove-files wallycore.html wallycore.js wallycore.wasm
- cd ..
- EXPORTED_FUNCTIONS="['_malloc','_free','_wally_init','_wally_asset_value_commitment','_wally_asset_generator_from_bytes']" tools/build_wasm.sh --no-modularize
- EMCC_OPTIONS="-s MODULARIZE=1" EXPORTED_FUNCTIONS="['_malloc','_free','_wally_init','_wally_asset_value_commitment','_wally_asset_generator_from_bytes']" tools/build_wasm.sh
- cd dist
- tar czf esplora-wasm.tar.gz --remove-files wallycore.html wallycore.js wallycore.wasm
- tar czf ../wally_dist/esplora-wasm.tar.gz --remove-files wallycore.js wallycore.wasm
- cd ..
- sphinx-build -b html -a -c docs/source docs/source docs/build/html
- cd docs/build
Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,21 @@ $ source $HOME/emsdk/emsdk_env.sh
# Optionally set the list of wally functions to export to wasm (default: all)
$ export EXPORTED_FUNCTIONS="['_malloc','_free','_wally_init','_wally_cleanup',...]"

# Optionally set emcc options, e.g. to build as an ES6 module:
$ export EMCC_OPTIONS="-s EXPORT_ES6=1 -s MODULARIZE=1"

# Build
$ ./tools/build_wasm.sh [--disable-elements]
```

Note that emsdk v3.1.27 or later is required.

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

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

## Cleaning

Expand Down
41 changes: 0 additions & 41 deletions contrib/shell_minimal.html

This file was deleted.

56 changes: 56 additions & 0 deletions contrib/wally_js_example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Wallycore JS Example</title>
<style>
textarea { font-family: monospace; width: 80%; padding-right: 0; margin-left: auto; margin-right: auto; display: block; }
</style>
</head>
<link rel="icon" href="data:,">
<body>
<textarea id="output" rows="16"></textarea>
<script type='text/javascript'>
var element = document.getElementById('output');
var wally_example = function() {
const as_utf8 = function(ptr) { return Module.UTF8ToString(Module.getValue(ptr, '*')); };
const b2h = function(buf) { return Array.prototype.map.call(buf, x => ('00' + x.toString(16)).slice(-2)).join(''); };
const wally_init = Module.cwrap("wally_init", ['number'], ['number']);
const wally_secp_randomize = Module.cwrap("wally_secp_randomize", ['number'], ['array', 'number']);
const bip39_mnemonic_from_bytes = Module.cwrap('bip39_mnemonic_from_bytes', ['number'], ['number', 'array', 'number', 'number']);
const wally_free_string = Module.cwrap('wally_free_string', ['number'], ['number']);

// Initialize wally
var text = "init: " + wally_init(0) + '\n';
var entropy = new Uint8Array(32); // WALLY_SECP_RANDOMIZE_LEN
window.crypto.getRandomValues(entropy);
text = text + "secp_randomize: " + wally_secp_randomize(entropy, entropy.length) + '\n';

// Create a BIP39 seed
var seed = new Uint8Array(16); // BIP39_ENTROPY_LEN_128
window.crypto.getRandomValues(seed);
text += "seed: " + b2h(seed) + '\n';

// Generate a mnemonic from it
var ptr = Module._malloc(4); // Holds our output pointer
var ret = bip39_mnemonic_from_bytes(null, seed, seed.length, ptr);
const mnemonic = as_utf8(ptr);
text = text + "mnemonic: " + mnemonic + '\n';
wally_free_string(Module.getValue(ptr, '*'));
Module._free(ptr);

// Display our output
element.value = text;
}
var Module = {
preRun: [],
postRun: [wally_example],
};
</script>
<script type="module">
import InitWally from "./wallycore.js";
InitWally(Module);
</script>
</body>
</html>
11 changes: 3 additions & 8 deletions tools/build_wasm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ if [ "$1" = "--disable-elements" ]; then
DISABLE_ELEMENTS="--disable-elements --disable-elements-abi"
shift
fi
MODULARIZE="-s MODULARIZE=1"
if [ "$1" = "--no-modularize" ]; then
MODULARIZE=""
fi

num_jobs=4
if [ -f /proc/cpuinfo ]; then
Expand All @@ -34,9 +30,9 @@ export CFLAGS="-fno-stack-protector"
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
emmake make -j $num_jobs

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

Expand All @@ -48,5 +44,4 @@ emcc -O$OPTIMIZATION_LEVEL \
-s FILESYSTEM=0 \
$EMCC_OPTIONS \
./src/.libs/*.o src/secp256k1/src/*.o src/ccan/ccan/*/.libs/*.o src/ccan/ccan/*/*/.libs/*.o \
-o dist/wallycore.html \
--shell-file contrib/shell_minimal.html
-o dist/wallycore.js
Loading