+import * as fs from 'fs';
+const source = fs.readFileSync('./ldk.wasm');
+
+const memory = new WebAssembly.Memory({initial: 256});
+const wasmModule = new WebAssembly.Module(source);
+
+const imports: any = {};
+imports.env = {};
+
+imports.env.memoryBase = 0;
+imports.env.memory = memory;
+imports.env.tableBase = 0;
+imports.env.table = new WebAssembly.Table({initial: 4, element: 'anyfunc'});
+
+imports.env["abort"] = function () {
+ console.error("ABORT");
+};
+
+let wasm = null;
+let isWasmInitialized: boolean = false;
+
+
+// WASM CODEC
+
+const nextMultipleOfFour = (value: number) => {
+ return Math.ceil(value / 4) * 4;
+}
+
+const encodeArray = (inputArray) => {
+ const cArrayPointer = wasm.wasm_malloc((inputArray.length + 1) * 4);
+ const arrayMemoryView = new Uint32Array(memory.buffer, cArrayPointer + 4, inputArray.length);
+ arrayMemoryView.set(inputArray, 1);
+ arrayMemoryView[0] = inputArray.length;
+ return cArrayPointer;
+}
+
+const getArrayLength = (arrayPointer) => {
+ const arraySizeViewer = new Uint32Array(
+ memory.buffer, // value
+ arrayPointer, // offset
+ 1 // one int
+ );
+ return arraySizeViewer[0];
+}
+const decodeUint8Array = (arrayPointer, free = true) => {
+ const arraySize = getArrayLength(arrayPointer);
+ const actualArrayViewer = new Uint8Array(
+ memory.buffer, // value
+ arrayPointer + 4, // offset (ignoring length bytes)
+ arraySize // uint8 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 = actualArrayViewer.slice(0, arraySize);
+ if (free) {
+ wasm.free(arrayPointer);
+ }
+ return actualArray;
+}
+const decodeUint32Array = (arrayPointer, free = true) => {
+ const arraySize = getArrayLength(arrayPointer);
+ const actualArrayViewer = new Uint32Array(
+ memory.buffer, // value
+ arrayPointer + 4, // 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 = actualArrayViewer.slice(0, arraySize);
+ if (free) {
+ wasm.free(arrayPointer);
+ }
+ return actualArray;
+}
+
+const encodeString = (string) => {
+ // make malloc count divisible by 4
+ const memoryNeed = nextMultipleOfFour(string.length + 1);
+ const stringPointer = wasm.wasm_malloc(memoryNeed);
+ const stringMemoryView = new Uint8Array(
+ memory.buffer, // value
+ stringPointer, // offset
+ string.length + 1 // length
+ );
+ for (let i = 0; i < string.length; i++) {
+ stringMemoryView[i] = string.charCodeAt(i);
+ }
+ stringMemoryView[string.length] = 0;
+ return stringPointer;
+}
+
+const decodeString = (stringPointer, free = true) => {
+ const memoryView = new Uint8Array(memory.buffer, stringPointer);
+ let cursor = 0;
+ let result = '';
+
+ while (memoryView[cursor] !== 0) {
+ result += String.fromCharCode(memoryView[cursor]);
+ cursor++;
+ }
+
+ if (free) {
+ wasm.wasm_free(stringPointer);
+ }
+
+ return result;
+};