+/* @internal */
+export async function initializeWasmFromUint8Array(wasmBinary: Uint8Array) {
+ for (const fn of fn_list) { imports.env["js_invoke_function_" + fn] = js_invoke; }
+ const { instance: wasmInstance } = await WebAssembly.instantiate(wasmBinary, imports);
+ await finishInitializeWasm(wasmInstance);
+}
+
+/* @internal */
+export async function initializeWasmFetch(uri: string) {
+ for (const fn of fn_list) { imports.env["js_invoke_function_" + fn] = js_invoke; }
+ const stream = fetch(uri);
+ const { instance: wasmInstance } = await WebAssembly.instantiateStreaming(stream, imports);
+ await finishInitializeWasm(wasmInstance);
+}"""
+
+ self.bindings_header += """
+// WASM CODEC
+
+/* @internal */
+export function uint5ArrToBytes(inputArray: Array<UInt5>): Uint8Array {
+ const arr = new Uint8Array(inputArray.length);
+ for (var i = 0; i < inputArray.length; i++) {
+ arr[i] = inputArray[i]!.getVal();
+ }
+ return arr;
+}
+
+/* @internal */
+export function WitnessVersionArrToBytes(inputArray: Array<WitnessVersion>): Uint8Array {
+ const arr = new Uint8Array(inputArray.length);
+ for (var i = 0; i < inputArray.length; i++) {
+ arr[i] = inputArray[i]!.getVal();
+ }
+ return arr;
+}
+
+
+
+/* @internal */
+export function encodeUint128 (inputVal: bigint): number {
+ if (inputVal >= 0x10000000000000000000000000000000n) throw "U128s cannot exceed 128 bits";
+ const cArrayPointer = wasm.TS_malloc(16 + 8);
+ const arrayLengthView = new BigUint64Array(wasm.memory.buffer, cArrayPointer, 1);
+ arrayLengthView[0] = BigInt(16);
+ const arrayMemoryView = new Uint8Array(wasm.memory.buffer, cArrayPointer + 8, 16);
+ for (var i = 0; i < 16; i++) arrayMemoryView[i] = Number((inputVal >> BigInt(i)*8n) & 0xffn);
+ return cArrayPointer;
+}
+/* @internal */
+export function encodeUint8Array (inputArray: Uint8Array|null): number {
+ if (inputArray == null) return 0;
+ const cArrayPointer = wasm.TS_malloc(inputArray.length + 8);
+ const arrayLengthView = new BigUint64Array(wasm.memory.buffer, cArrayPointer, 1);
+ arrayLengthView[0] = BigInt(inputArray.length);
+ const arrayMemoryView = new Uint8Array(wasm.memory.buffer, cArrayPointer + 8, inputArray.length);
+ arrayMemoryView.set(inputArray);
+ return cArrayPointer;
+}
+/* @internal */
+export function encodeUint16Array (inputArray: Uint16Array|Array<number>|null): number {
+ if (inputArray == null) return 0;
+ const cArrayPointer = wasm.TS_malloc((inputArray.length + 4) * 2);
+ const arrayLengthView = new BigUint64Array(wasm.memory.buffer, cArrayPointer, 1);
+ arrayLengthView[0] = BigInt(inputArray.length);
+ const arrayMemoryView = new Uint16Array(wasm.memory.buffer, cArrayPointer + 8, inputArray.length);
+ arrayMemoryView.set(inputArray);
+ return cArrayPointer;
+}
+/* @internal */
+export function encodeUint32Array (inputArray: Uint32Array|Array<number>|null): number {
+ if (inputArray == null) return 0;
+ const cArrayPointer = wasm.TS_malloc((inputArray.length + 2) * 4);
+ const arrayLengthView = new BigUint64Array(wasm.memory.buffer, cArrayPointer, 1);
+ arrayLengthView[0] = BigInt(inputArray.length);
+ const arrayMemoryView = new Uint32Array(wasm.memory.buffer, cArrayPointer + 8, inputArray.length);
+ arrayMemoryView.set(inputArray);
+ return cArrayPointer;
+}
+/* @internal */
+export function encodeUint64Array (inputArray: BigUint64Array|Array<bigint>|null): number {
+ if (inputArray == null) return 0;
+ const cArrayPointer = wasm.TS_malloc((inputArray.length + 1) * 8);
+ const arrayMemoryView = new BigUint64Array(wasm.memory.buffer, cArrayPointer, inputArray.length + 1);
+ arrayMemoryView[0] = BigInt(inputArray.length);
+ arrayMemoryView.set(inputArray, 1);
+ return cArrayPointer;
+}
+
+/* @internal */
+export function check_arr_len(arr: Uint8Array|null, len: number): Uint8Array|null {
+ if (arr !== null && arr.length != len) { throw new Error("Expected array of length " + len + " got " + arr.length); }
+ return arr;
+}
+
+/* @internal */
+export function check_16_arr_len(arr: Uint16Array|null, len: number): Uint16Array|null {
+ if (arr !== null && arr.length != len) { throw new Error("Expected array of length " + len + " got " + arr.length); }
+ return arr;
+}
+
+/* @internal */
+export function getArrayLength(arrayPointer: number): number {
+ const arraySizeViewer = new BigUint64Array(wasm.memory.buffer, arrayPointer, 1);
+ const len = arraySizeViewer[0]!;
+ if (len >= (2n ** 32n)) throw new Error("Bogus Array Size");
+ return Number(len % (2n ** 32n));
+}
+/* @internal */
+export function decodeUint128 (arrayPointer: number, free = true): bigint {
+ const arraySize = getArrayLength(arrayPointer);
+ if (arraySize != 16) throw "Need 16 bytes for a uint128";
+ const actualArrayViewer = new Uint8Array(wasm.memory.buffer, arrayPointer + 8, arraySize);
+ var val = 0n;
+ for (var i = 0; i < 16; i++) {
+ val <<= 8n;
+ val |= BigInt(actualArrayViewer[i]!);
+ }
+ if (free) {
+ wasm.TS_free(arrayPointer);
+ }
+ return val;
+}
+/* @internal */
+export function decodeUint8Array (arrayPointer: number, free = true): Uint8Array {
+ const arraySize = getArrayLength(arrayPointer);
+ const actualArrayViewer = new Uint8Array(wasm.memory.buffer, arrayPointer + 8, arraySize);
+ // Clone the contents, TODO: In the future we should wrap the Viewer in a class that
+ // will free the underlying memory when it becomes unreachable instead of copying here.
+ // Note that doing so may have edge-case interactions with memory resizing (invalidating the buffer).
+ const actualArray = actualArrayViewer.slice(0, arraySize);
+ if (free) {
+ wasm.TS_free(arrayPointer);
+ }
+ return actualArray;
+}
+/* @internal */
+export function decodeUint16Array (arrayPointer: number, free = true): Uint16Array {
+ const arraySize = getArrayLength(arrayPointer);
+ const actualArrayViewer = new Uint16Array(wasm.memory.buffer, arrayPointer + 8, arraySize);
+ // Clone the contents, TODO: In the future we should wrap the Viewer in a class that
+ // will free the underlying memory when it becomes unreachable instead of copying here.
+ // Note that doing so may have edge-case interactions with memory resizing (invalidating the buffer).
+ const actualArray = actualArrayViewer.slice(0, arraySize);
+ if (free) {
+ wasm.TS_free(arrayPointer);
+ }
+ return actualArray;
+}
+/* @internal */
+export function decodeUint64Array (arrayPointer: number, free = true): bigint[] {
+ const arraySize = getArrayLength(arrayPointer);
+ const actualArrayViewer = new BigUint64Array(
+ wasm.memory.buffer, // value
+ arrayPointer + 8, // offset (ignoring length bytes)
+ arraySize // uint32 count
+ );
+ // Clone the contents, TODO: In the future we should wrap the Viewer in a class that
+ // will free the underlying memory when it becomes unreachable instead of copying here.
+ const actualArray = new Array(arraySize);
+ for (var i = 0; i < arraySize; i++) actualArray[i] = actualArrayViewer[i];
+ if (free) {
+ wasm.TS_free(arrayPointer);
+ }
+ return actualArray;
+}
+
+export function freeWasmMemory(pointer: number) { wasm.TS_free(pointer); }
+
+/* @internal */
+export function getU64ArrayElem(arrayPointer: number, idx: number): bigint {
+ const actualArrayViewer = new BigUint64Array(wasm.memory.buffer, arrayPointer + 8, idx + 1);
+ return actualArrayViewer[idx]!;
+}
+
+/* @internal */
+export function getU32ArrayElem(arrayPointer: number, idx: number): number {
+ const actualArrayViewer = new Uint32Array(wasm.memory.buffer, arrayPointer + 8, idx + 1);
+ return actualArrayViewer[idx]!;
+}
+
+/* @internal */
+export function getU8ArrayElem(arrayPointer: number, idx: number): number {
+ const actualArrayViewer = new Uint8Array(wasm.memory.buffer, arrayPointer + 8, idx + 1);
+ return actualArrayViewer[idx]!;
+}
+
+
+/* @internal */
+export function encodeString(str: string): number {
+ const charArray = new TextEncoder().encode(str);
+ return encodeUint8Array(charArray);
+}
+
+/* @internal */
+export function decodeString(stringPointer: number, free = true): string {
+ const arraySize = getArrayLength(stringPointer);
+ const memoryView = new Uint8Array(wasm.memory.buffer, stringPointer + 8, arraySize);
+ const result = new TextDecoder("utf-8").decode(memoryView);
+
+ if (free) {
+ wasm.TS_free(stringPointer);
+ }
+
+ return result;
+}
+"""
+ if DEBUG:
+ self.bindings_header += """
+/* @internal */
+export function getRemainingAllocationCount(): number {
+ return wasm.TS_allocs_remaining();
+}
+/* @internal */
+export function debugPrintRemainingAllocs() {
+ wasm.TS_print_leaks();
+}
+"""
+ else:
+ self.bindings_header += "\n/* @internal */ export function getRemainingAllocationCount(): number { return 0; }\n"
+ self.bindings_header += "/* @internal */ export function debugPrintRemainingAllocs() { }\n"
+
+ with open(outdir + "/index.mts", 'a') as index:
+ index.write("""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/UtilMethods.mjs';
+""")
+
+ self.bindings_version_file = """export function get_ldk_java_bindings_version(): String {
+ return "<git_version_ldk_garbagecollected>";
+}"""