]> git.bitcoin.ninja Git - ldk-java/commitdiff
Update auto-generated TypeScript files (now universal Node+Web!)
authorMatt Corallo <git@bluematt.me>
Mon, 17 Jan 2022 22:29:35 +0000 (22:29 +0000)
committerMatt Corallo <git@bluematt.me>
Mon, 17 Jan 2022 23:43:39 +0000 (23:43 +0000)
ts/bindings.mts
ts/index.mts

index af8cbfbe91d9bfb6e1a1f9936626b77ca2346d4a..6e85d1307c38d4b49d9a3d0f4a48ee8bdc541dbd 100644 (file)
@@ -6,6 +6,7 @@ imports.env = {};
 
 var js_objs: Array<WeakRef<object>> = [];
 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) => {
index 8aff2a3d0b5920cd897f8f87ecceaa15b61882fb..9e6b7b101a96177c1c13957bb29b0fbd6df570d1 100644 (file)
@@ -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';