merkle-tree-wasm
Version:
<div align="center">
240 lines (207 loc) • 6.84 kB
JavaScript
let wasm;
const { TextDecoder } = require(String.raw`util`);
let WASM_VECTOR_LEN = 0;
let cachegetNodeBufferMemory = null;
function getNodeBufferMemory() {
if (cachegetNodeBufferMemory === null || cachegetNodeBufferMemory.buffer !== wasm.memory.buffer) {
cachegetNodeBufferMemory = Buffer.from(wasm.memory.buffer);
}
return cachegetNodeBufferMemory;
}
function passStringToWasm(arg) {
const len = Buffer.byteLength(arg);
const ptr = wasm.__wbindgen_malloc(len);
getNodeBufferMemory().write(arg, ptr, len);
WASM_VECTOR_LEN = len;
return ptr;
}
let cachegetInt32Memory = null;
function getInt32Memory() {
if (cachegetInt32Memory === null || cachegetInt32Memory.buffer !== wasm.memory.buffer) {
cachegetInt32Memory = new Int32Array(wasm.memory.buffer);
}
return cachegetInt32Memory;
}
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
let cachegetUint8Memory = null;
function getUint8Memory() {
if (cachegetUint8Memory === null || cachegetUint8Memory.buffer !== wasm.memory.buffer) {
cachegetUint8Memory = new Uint8Array(wasm.memory.buffer);
}
return cachegetUint8Memory;
}
function getStringFromWasm(ptr, len) {
return cachedTextDecoder.decode(getUint8Memory().subarray(ptr, ptr + len));
}
let cachegetUint32Memory = null;
function getUint32Memory() {
if (cachegetUint32Memory === null || cachegetUint32Memory.buffer !== wasm.memory.buffer) {
cachegetUint32Memory = new Uint32Array(wasm.memory.buffer);
}
return cachegetUint32Memory;
}
const heap = new Array(32);
heap.fill(undefined);
heap.push(undefined, null, true, false);
let heap_next = heap.length;
function addHeapObject(obj) {
if (heap_next === heap.length) heap.push(heap.length + 1);
const idx = heap_next;
heap_next = heap[idx];
heap[idx] = obj;
return idx;
}
function passArrayJsValueToWasm(array) {
const ptr = wasm.__wbindgen_malloc(array.length * 4);
const mem = getUint32Memory();
for (let i = 0; i < array.length; i++) {
mem[ptr / 4 + i] = addHeapObject(array[i]);
}
WASM_VECTOR_LEN = array.length;
return ptr;
}
/**
*/
module.exports.greet = function() {
wasm.greet();
};
function getObject(idx) { return heap[idx]; }
function dropObject(idx) {
if (idx < 36) return;
heap[idx] = heap_next;
heap_next = idx;
}
function takeObject(idx) {
const ret = getObject(idx);
dropObject(idx);
return ret;
}
/**
*/
class DoubleLayerTree {
static __wrap(ptr) {
const obj = Object.create(DoubleLayerTree.prototype);
obj.ptr = ptr;
return obj;
}
free() {
const ptr = this.ptr;
this.ptr = 0;
wasm.__wbg_doublelayertree_free(ptr);
}
/**
* @param {any[]} double_layer_tree_leaves
* @returns {DoubleLayerTree}
*/
static generate(double_layer_tree_leaves) {
const ret = wasm.doublelayertree_generate(passArrayJsValueToWasm(double_layer_tree_leaves), WASM_VECTOR_LEN);
return DoubleLayerTree.__wrap(ret);
}
/**
* @param {string} address
* @param {number} idx
* @returns {string}
*/
get_inclusion_proof(address, idx) {
const retptr = 8;
const ret = wasm.doublelayertree_get_inclusion_proof(retptr, this.ptr, passStringToWasm(address), WASM_VECTOR_LEN, idx);
const memi32 = getInt32Memory();
const v0 = getStringFromWasm(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1]).slice();
wasm.__wbindgen_free(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1] * 1);
return v0;
}
/**
* @returns {string}
*/
get_root() {
const retptr = 8;
const ret = wasm.doublelayertree_get_root(retptr, this.ptr);
const memi32 = getInt32Memory();
const v0 = getStringFromWasm(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1]).slice();
wasm.__wbindgen_free(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1] * 1);
return v0;
}
/**
* @param {any} leaf_val
* @param {string} inclusion_proof_bytes
* @param {string} root
* @returns {boolean}
*/
static verify(leaf_val, inclusion_proof_bytes, root) {
const ret = wasm.doublelayertree_verify(addHeapObject(leaf_val), passStringToWasm(inclusion_proof_bytes), WASM_VECTOR_LEN, passStringToWasm(root), WASM_VECTOR_LEN);
return ret !== 0;
}
}
module.exports.DoubleLayerTree = DoubleLayerTree;
/**
*/
class DoubleLayerTreeLeaf {
static __wrap(ptr) {
const obj = Object.create(DoubleLayerTreeLeaf.prototype);
obj.ptr = ptr;
return obj;
}
free() {
const ptr = this.ptr;
this.ptr = 0;
wasm.__wbg_doublelayertreeleaf_free(ptr);
}
/**
* @param {string} data
* @param {number} end
* @param {string} address
* @returns {DoubleLayerTreeLeaf}
*/
static new(data, end, address) {
const ret = wasm.doublelayertreeleaf_new(passStringToWasm(data), WASM_VECTOR_LEN, end, passStringToWasm(address), WASM_VECTOR_LEN);
return DoubleLayerTreeLeaf.__wrap(ret);
}
/**
* @returns {string}
*/
get_data() {
const retptr = 8;
const ret = wasm.doublelayertreeleaf_get_data(retptr, this.ptr);
const memi32 = getInt32Memory();
const v0 = getStringFromWasm(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1]).slice();
wasm.__wbindgen_free(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1] * 1);
return v0;
}
/**
* @returns {number}
*/
get_end() {
const ret = wasm.doublelayertreeleaf_get_end(this.ptr);
return ret >>> 0;
}
/**
* @returns {string}
*/
get_address() {
const retptr = 8;
const ret = wasm.doublelayertreeleaf_get_address(retptr, this.ptr);
const memi32 = getInt32Memory();
const v0 = getStringFromWasm(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1]).slice();
wasm.__wbindgen_free(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1] * 1);
return v0;
}
}
module.exports.DoubleLayerTreeLeaf = DoubleLayerTreeLeaf;
module.exports.__wbindgen_object_drop_ref = function(arg0) {
takeObject(arg0);
};
module.exports.__wbg_alert_bad04a56af65f53e = function(arg0, arg1) {
alert(getStringFromWasm(arg0, arg1));
};
module.exports.__wbindgen_json_serialize = function(arg0, arg1) {
const obj = getObject(arg1);
const ret = JSON.stringify(obj === undefined ? null : obj);
const ret0 = passStringToWasm(ret);
const ret1 = WASM_VECTOR_LEN;
getInt32Memory()[arg0 / 4 + 0] = ret0;
getInt32Memory()[arg0 / 4 + 1] = ret1;
};
module.exports.__wbindgen_throw = function(arg0, arg1) {
throw new Error(getStringFromWasm(arg0, arg1));
};
wasm = require('./merkle_tree_wasm_bg');