@ecash/lib
Version:
Library for eCash transaction building
499 lines (461 loc) • 15.5 kB
JavaScript
let imports = {};
imports['__wbindgen_placeholder__'] = module.exports;
let wasm;
const { TextDecoder } = require(`util`);
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
cachedTextDecoder.decode();
let cachedUint8Memory0 = null;
function getUint8Memory0() {
if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) {
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8Memory0;
}
function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
}
const heap = new Array(128).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;
}
let WASM_VECTOR_LEN = 0;
function passArray8ToWasm0(arg, malloc) {
const ptr = malloc(arg.length * 1, 1) >>> 0;
getUint8Memory0().set(arg, ptr / 1);
WASM_VECTOR_LEN = arg.length;
return ptr;
}
let cachedInt32Memory0 = null;
function getInt32Memory0() {
if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) {
cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
}
return cachedInt32Memory0;
}
function getArrayU8FromWasm0(ptr, len) {
ptr = ptr >>> 0;
return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len);
}
/**
* Calculate SHA512(data).
* @param {Uint8Array} data
* @returns {Uint8Array}
*/
module.exports.sha512 = function(data) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_export_0);
const len0 = WASM_VECTOR_LEN;
wasm.sha512(retptr, ptr0, len0);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var v2 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_export_1(r0, r1 * 1, 1);
return v2;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
};
/**
* Calculate SHA256(SHA256(data)).
* @param {Uint8Array} data
* @returns {Uint8Array}
*/
module.exports.sha256d = function(data) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_export_0);
const len0 = WASM_VECTOR_LEN;
wasm.sha256d(retptr, ptr0, len0);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var v2 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_export_1(r0, r1 * 1, 1);
return v2;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
};
/**
* Calculate SHA256(data).
* @param {Uint8Array} data
* @returns {Uint8Array}
*/
module.exports.sha256 = function(data) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_export_0);
const len0 = WASM_VECTOR_LEN;
wasm.sha256(retptr, ptr0, len0);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var v2 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_export_1(r0, r1 * 1, 1);
return v2;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
};
/**
* Calculate RIPEMD160(SHA256(data)), commonly used as address hash.
* @param {Uint8Array} data
* @returns {Uint8Array}
*/
module.exports.shaRmd160 = function(data) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_export_0);
const len0 = WASM_VECTOR_LEN;
wasm.shaRmd160(retptr, ptr0, len0);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var v2 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_export_1(r0, r1 * 1, 1);
return v2;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
};
function getObject(idx) { return heap[idx]; }
function dropObject(idx) {
if (idx < 132) return;
heap[idx] = heap_next;
heap_next = idx;
}
function takeObject(idx) {
const ret = getObject(idx);
dropObject(idx);
return ret;
}
const EccFinalization = (typeof FinalizationRegistry === 'undefined')
? { register: () => {}, unregister: () => {} }
: new FinalizationRegistry(ptr => wasm.__wbg_ecc_free(ptr >>> 0));
/**
* ECC signatures with libsecp256k1.
*/
class Ecc {
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
EccFinalization.unregister(this);
return ptr;
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_ecc_free(ptr);
}
/**
* Create a new Ecc instance.
*/
constructor() {
const ret = wasm.ecc_new();
this.__wbg_ptr = ret >>> 0;
return this;
}
/**
* Derive a public key from secret key.
* @param {Uint8Array} seckey
* @returns {Uint8Array}
*/
derivePubkey(seckey) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(seckey, wasm.__wbindgen_export_0);
const len0 = WASM_VECTOR_LEN;
wasm.ecc_derivePubkey(retptr, this.__wbg_ptr, ptr0, len0);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
var r3 = getInt32Memory0()[retptr / 4 + 3];
if (r3) {
throw takeObject(r2);
}
var v2 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_export_1(r0, r1 * 1, 1);
return v2;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* Sign an ECDSA signature.
* @param {Uint8Array} seckey
* @param {Uint8Array} msg
* @returns {Uint8Array}
*/
ecdsaSign(seckey, msg) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(seckey, wasm.__wbindgen_export_0);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passArray8ToWasm0(msg, wasm.__wbindgen_export_0);
const len1 = WASM_VECTOR_LEN;
wasm.ecc_ecdsaSign(retptr, this.__wbg_ptr, ptr0, len0, ptr1, len1);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
var r3 = getInt32Memory0()[retptr / 4 + 3];
if (r3) {
throw takeObject(r2);
}
var v3 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_export_1(r0, r1 * 1, 1);
return v3;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* Sign a Schnorr signature.
* @param {Uint8Array} seckey
* @param {Uint8Array} msg
* @returns {Uint8Array}
*/
schnorrSign(seckey, msg) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(seckey, wasm.__wbindgen_export_0);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passArray8ToWasm0(msg, wasm.__wbindgen_export_0);
const len1 = WASM_VECTOR_LEN;
wasm.ecc_schnorrSign(retptr, this.__wbg_ptr, ptr0, len0, ptr1, len1);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
var r3 = getInt32Memory0()[retptr / 4 + 3];
if (r3) {
throw takeObject(r2);
}
var v3 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_export_1(r0, r1 * 1, 1);
return v3;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* Return whether the given secret key is valid, i.e. whether is of correct
* length (32 bytes) and is on the curve.
* @param {Uint8Array} seckey
* @returns {boolean}
*/
isValidSeckey(seckey) {
const ptr0 = passArray8ToWasm0(seckey, wasm.__wbindgen_export_0);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.ecc_isValidSeckey(this.__wbg_ptr, ptr0, len0);
return ret !== 0;
}
/**
* Add a scalar to a secret key.
* @param {Uint8Array} a
* @param {Uint8Array} b
* @returns {Uint8Array}
*/
seckeyAdd(a, b) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(a, wasm.__wbindgen_export_0);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passArray8ToWasm0(b, wasm.__wbindgen_export_0);
const len1 = WASM_VECTOR_LEN;
wasm.ecc_seckeyAdd(retptr, this.__wbg_ptr, ptr0, len0, ptr1, len1);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
var r3 = getInt32Memory0()[retptr / 4 + 3];
if (r3) {
throw takeObject(r2);
}
var v3 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_export_1(r0, r1 * 1, 1);
return v3;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* Add a scalar to a public key (adding G*b).
* @param {Uint8Array} a
* @param {Uint8Array} b
* @returns {Uint8Array}
*/
pubkeyAdd(a, b) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(a, wasm.__wbindgen_export_0);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passArray8ToWasm0(b, wasm.__wbindgen_export_0);
const len1 = WASM_VECTOR_LEN;
wasm.ecc_pubkeyAdd(retptr, this.__wbg_ptr, ptr0, len0, ptr1, len1);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
var r3 = getInt32Memory0()[retptr / 4 + 3];
if (r3) {
throw takeObject(r2);
}
var v3 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_export_1(r0, r1 * 1, 1);
return v3;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
}
module.exports.Ecc = Ecc;
const Sha256HFinalization = (typeof FinalizationRegistry === 'undefined')
? { register: () => {}, unregister: () => {} }
: new FinalizationRegistry(ptr => wasm.__wbg_sha256h_free(ptr >>> 0));
/**
* Instance to calculate SHA256 in a streaming fashion
*/
class Sha256H {
static __wrap(ptr) {
ptr = ptr >>> 0;
const obj = Object.create(Sha256H.prototype);
obj.__wbg_ptr = ptr;
Sha256HFinalization.register(obj, obj.__wbg_ptr, obj);
return obj;
}
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
Sha256HFinalization.unregister(this);
return ptr;
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_sha256h_free(ptr);
}
/**
* Create new hasher instance
*/
constructor() {
const ret = wasm.sha256h_new();
this.__wbg_ptr = ret >>> 0;
return this;
}
/**
* Feed bytes into the hasher
* @param {Uint8Array} data
*/
update(data) {
const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_export_0);
const len0 = WASM_VECTOR_LEN;
wasm.sha256h_update(this.__wbg_ptr, ptr0, len0);
}
/**
* Finalize the hash and return the result
* @returns {Uint8Array}
*/
finalize() {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.sha256h_finalize(retptr, this.__wbg_ptr);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var v1 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_export_1(r0, r1 * 1, 1);
return v1;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* Clone the hasher
* @returns {Sha256H}
*/
clone() {
const ret = wasm.sha256h_clone(this.__wbg_ptr);
return Sha256H.__wrap(ret);
}
}
module.exports.Sha256H = Sha256H;
const Sha512HFinalization = (typeof FinalizationRegistry === 'undefined')
? { register: () => {}, unregister: () => {} }
: new FinalizationRegistry(ptr => wasm.__wbg_sha512h_free(ptr >>> 0));
/**
* Instance to calculate SHA512 in a streaming fashion
*/
class Sha512H {
static __wrap(ptr) {
ptr = ptr >>> 0;
const obj = Object.create(Sha512H.prototype);
obj.__wbg_ptr = ptr;
Sha512HFinalization.register(obj, obj.__wbg_ptr, obj);
return obj;
}
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
Sha512HFinalization.unregister(this);
return ptr;
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_sha512h_free(ptr);
}
/**
* Create new hasher instance
*/
constructor() {
const ret = wasm.sha512h_new();
this.__wbg_ptr = ret >>> 0;
return this;
}
/**
* Feed bytes into the hasher
* @param {Uint8Array} data
*/
update(data) {
const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_export_0);
const len0 = WASM_VECTOR_LEN;
wasm.sha512h_update(this.__wbg_ptr, ptr0, len0);
}
/**
* Finalize the hash and return the result
* @returns {Uint8Array}
*/
finalize() {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.sha512h_finalize(retptr, this.__wbg_ptr);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var v1 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_export_1(r0, r1 * 1, 1);
return v1;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* Clone the hasher
* @returns {Sha512H}
*/
clone() {
const ret = wasm.sha512h_clone(this.__wbg_ptr);
return Sha512H.__wrap(ret);
}
}
module.exports.Sha512H = Sha512H;
module.exports.__wbindgen_string_new = function(arg0, arg1) {
const ret = getStringFromWasm0(arg0, arg1);
return addHeapObject(ret);
};
module.exports.__wbindgen_throw = function(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
};
const path = require('path').join(__dirname, 'ecash_lib_wasm_bg_nodejs.wasm');
const bytes = require('fs').readFileSync(path);
const wasmModule = new WebAssembly.Module(bytes);
const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
wasm = wasmInstance.exports;
module.exports.__wasm = wasm;