+ 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]!;
+}