From: Matt Corallo <649246+TheBlueMatt@users.noreply.github.com> Date: Tue, 18 Jan 2022 00:09:37 +0000 (+0000) Subject: Merge pull request #81 from TheBlueMatt/main X-Git-Tag: v0.0.105.0~3 X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=ldk-java;a=commitdiff_plain;h=eec046b940202cb1375e3473d055f06e64e2c9f5;hp=c3594d070a0d4261f311833cc40a78397edac3e3 Merge pull request #81 from TheBlueMatt/main [TS] Unify Web + Node Codebases (with different init functions) --- diff --git a/genbindings.sh b/genbindings.sh index 128a3afb..7fc10bc9 100755 --- a/genbindings.sh +++ b/genbindings.sh @@ -6,6 +6,7 @@ usage() { echo "debug should either be true, false, or leaks" echo "debug of leaks turns on leak tracking on an optimized release bianry" echo "android_web should either be true or false and indicates if we build for android (Java) or web (WASM)" + echo "Note that web currently generates the same results as !web (ie Node.JS)" exit 1 } [ "$1" = "" ] && usage @@ -219,17 +220,13 @@ else mv $F.tmp $F done rm imports.mts.part - if [ "$4" = "true" ]; then - tsc - else - tsc --types node --typeRoots . - cp ../$WASM_FILE liblightningjs.wasm - echo Ready to publish! - if [ -x "$(which node)" ]; then - NODE_V="$(node --version)" - if [ "${NODE_V:1:2}" -gt 14 ]; then - node test/node.mjs - fi + tsc --types node --typeRoots . + cp ../$WASM_FILE liblightningjs.wasm + echo Ready to publish! + if [ -x "$(which node)" ]; then + NODE_V="$(node --version)" + if [ "${NODE_V:1:2}" -gt 14 ]; then + node test/node.mjs fi fi fi diff --git a/ts/bindings.mts b/ts/bindings.mts index af8cbfbe..6e85d130 100644 --- a/ts/bindings.mts +++ b/ts/bindings.mts @@ -6,6 +6,7 @@ imports.env = {}; var js_objs: Array> = []; var js_invoke: Function; +var getRandomValues: Function; imports.wasi_snapshot_preview1 = { "fd_write": (fd: number, iovec_array_ptr: number, iovec_array_len: number) => { @@ -30,7 +31,7 @@ imports.wasi_snapshot_preview1 = { }, "random_get": (buf_ptr: number, buf_len: number) => { const buf = new Uint8Array(wasm.memory.buffer, buf_ptr, buf_len); - crypto.getRandomValues(buf); + getRandomValues(buf); return 0; }, "environ_sizes_get": (environ_var_count_ptr: number, environ_len_ptr: number) => { @@ -55,11 +56,14 @@ imports.wasi_snapshot_preview1 = { var wasm: any = null; let isWasmInitialized: boolean = false; -/* @internal */ -export async function initializeWasm(uri: string) { - const stream = fetch(uri); - imports.env["js_invoke_function"] = js_invoke; - const { instance: wasmInstance } = await WebAssembly.instantiateStreaming(stream, imports); +async function finishInitializeWasm(wasmInstance: WebAssembly.Instance) { + if (typeof crypto === "undefined") { + var crypto_import = (await import('crypto')).webcrypto; + getRandomValues = crypto_import.getRandomValues.bind(crypto_import); + } else { + getRandomValues = crypto.getRandomValues.bind(crypto); + } + wasm = wasmInstance.exports; if (!wasm.test_bigint_pass_deadbeef0badf00d(BigInt("0xdeadbeef0badf00d"))) { throw new Error("Currently need BigInt-as-u64 support, try ----experimental-wasm-bigint"); @@ -79,8 +83,22 @@ export async function initializeWasm(uri: string) { console.log("Loaded LDK-Java Bindings with LDK " + ldk_version + " and LDK-C-Bindings " + c_bindings_version); isWasmInitialized = true; -}; +} +/* @internal */ +export async function initializeWasmFromUint8Array(wasmBinary: Uint8Array) { + imports.env["js_invoke_function"] = js_invoke; + const { instance: wasmInstance } = await WebAssembly.instantiate(wasmBinary, imports); + await finishInitializeWasm(wasmInstance); +} + +/* @internal */ +export async function initializeWasmFetch(uri: string) { + const stream = fetch(uri); + imports.env["js_invoke_function"] = js_invoke; + const { instance: wasmInstance } = await WebAssembly.instantiateStreaming(stream, imports); + await finishInitializeWasm(wasmInstance); +} // WASM CODEC const nextMultipleOfFour = (value: number) => { diff --git a/ts/index.mts b/ts/index.mts index 8aff2a3d..9e6b7b10 100644 --- a/ts/index.mts +++ b/ts/index.mts @@ -1,7 +1,13 @@ -import { initializeWasm as bindingsInit } from './bindings.mjs'; -export function initializeWasm(path: string) { - bindingsInit(path); +import { initializeWasmFetch, initializeWasmFromUint8Array } from './bindings.mjs'; +/** Initializes the WASM backend by calling `fetch()` on the given URI - Browser only */ +export async function initializeWasmWebFetch(uri: string) { + await initializeWasmFetch(uri); } +/** Initializes the WASM backend given a Uint8Array of the .wasm binary file - Browser or Node.JS */ +export async function initializeWasmFromBinary(bin: Uint8Array) { + await initializeWasmFromUint8Array(bin); +} + export * from './structs/TxOut.mjs'; export * from './enums/AccessError.mjs'; export * from './enums/COption_NoneZ.mjs'; diff --git a/ts/package.json b/ts/package.json index ea51b312..1ae0fb50 100644 --- a/ts/package.json +++ b/ts/package.json @@ -1,6 +1,6 @@ { "name": "lightningdevkit", - "version": "0.0.104.1alpha3", + "version": "0.0.104.1alpha4", "description": "Lightning Development Kit", "main": "index.mjs", "type": "module", diff --git a/ts/test/index.html b/ts/test/index.html index 3fc55a6d..60fd523c 100644 --- a/ts/test/index.html +++ b/ts/test/index.html @@ -7,10 +7,10 @@ var test_runner;