|
| 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> |
0 commit comments