UNPKG

@nimiq/core

Version:

Nimiq's Rust-to-WASM web client

1,556 lines (1,492 loc) 163 kB
let wasm; let WASM_VECTOR_LEN = 0; let cachedUint8ArrayMemory0 = null; function getUint8ArrayMemory0() { if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) { cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer); } return cachedUint8ArrayMemory0; } const cachedTextEncoder = (typeof TextEncoder !== 'undefined' ? new TextEncoder('utf-8') : { encode: () => { throw Error('TextEncoder not available') } } ); const encodeString = (typeof cachedTextEncoder.encodeInto === 'function' ? function (arg, view) { return cachedTextEncoder.encodeInto(arg, view); } : function (arg, view) { const buf = cachedTextEncoder.encode(arg); view.set(buf); return { read: arg.length, written: buf.length }; }); function passStringToWasm0(arg, malloc, realloc) { if (realloc === undefined) { const buf = cachedTextEncoder.encode(arg); const ptr = malloc(buf.length, 1) >>> 0; getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf); WASM_VECTOR_LEN = buf.length; return ptr; } let len = arg.length; let ptr = malloc(len, 1) >>> 0; const mem = getUint8ArrayMemory0(); let offset = 0; for (; offset < len; offset++) { const code = arg.charCodeAt(offset); if (code > 0x7F) break; mem[ptr + offset] = code; } if (offset !== len) { if (offset !== 0) { arg = arg.slice(offset); } ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0; const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len); const ret = encodeString(arg, view); offset += ret.written; ptr = realloc(ptr, len, offset, 1) >>> 0; } WASM_VECTOR_LEN = offset; return ptr; } let cachedDataViewMemory0 = null; function getDataViewMemory0() { if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) { cachedDataViewMemory0 = new DataView(wasm.memory.buffer); } return cachedDataViewMemory0; } function addToExternrefTable0(obj) { const idx = wasm.__externref_table_alloc(); wasm.__wbindgen_export_4.set(idx, obj); return idx; } function handleError(f, args) { try { return f.apply(this, args); } catch (e) { const idx = addToExternrefTable0(e); wasm.__wbindgen_exn_store(idx); } } function getArrayU8FromWasm0(ptr, len) { ptr = ptr >>> 0; return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len); } function isLikeNone(x) { return x === undefined || x === null; } function debugString(val) { // primitive types const type = typeof val; if (type == 'number' || type == 'boolean' || val == null) { return `${val}`; } if (type == 'string') { return `"${val}"`; } if (type == 'symbol') { const description = val.description; if (description == null) { return 'Symbol'; } else { return `Symbol(${description})`; } } if (type == 'function') { const name = val.name; if (typeof name == 'string' && name.length > 0) { return `Function(${name})`; } else { return 'Function'; } } // objects if (Array.isArray(val)) { const length = val.length; let debug = '['; if (length > 0) { debug += debugString(val[0]); } for(let i = 1; i < length; i++) { debug += ', ' + debugString(val[i]); } debug += ']'; return debug; } // Test for built-in const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val)); let className; if (builtInMatches && builtInMatches.length > 1) { className = builtInMatches[1]; } else { // Failed to match the standard '[object ClassName]' return toString.call(val); } if (className == 'Object') { // we're a user defined class or Object // JSON.stringify avoids problems with cycles, and is generally much // easier than looping through ownProperties of `val`. try { return 'Object(' + JSON.stringify(val) + ')'; } catch (_) { return 'Object'; } } // errors if (val instanceof Error) { return `${val.name}: ${val.message}\n${val.stack}`; } // TODO we could test for more things here, like `Set`s and `Map`s. return className; } const cachedTextDecoder = (typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }) : { decode: () => { throw Error('TextDecoder not available') } } ); if (typeof TextDecoder !== 'undefined') { cachedTextDecoder.decode(); }; function getStringFromWasm0(ptr, len) { ptr = ptr >>> 0; return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len)); } function _assertClass(instance, klass) { if (!(instance instanceof klass)) { throw new Error(`expected instance of ${klass.name}`); } } function passArray8ToWasm0(arg, malloc) { const ptr = malloc(arg.length * 1, 1) >>> 0; getUint8ArrayMemory0().set(arg, ptr / 1); WASM_VECTOR_LEN = arg.length; return ptr; } function takeFromExternrefTable0(idx) { const value = wasm.__wbindgen_export_4.get(idx); wasm.__externref_table_dealloc(idx); return value; } function passArrayJsValueToWasm0(array, malloc) { const ptr = malloc(array.length * 4, 4) >>> 0; for (let i = 0; i < array.length; i++) { const add = addToExternrefTable0(array[i]); getDataViewMemory0().setUint32(ptr + 4 * i, add, true); } WASM_VECTOR_LEN = array.length; return ptr; } /** * @enum {0 | 1 | 2 | 3} */ export const AccountType = Object.freeze({ Basic: 0, "0": "Basic", Vesting: 1, "1": "Vesting", HTLC: 2, "2": "HTLC", Staking: 3, "3": "Staking", }); /** * A transaction flag signals a special purpose of the transaction. `ContractCreation` must be set * to create new vesting contracts or HTLCs. `Signaling` must be set to interact with the staking * contract for non-value transactions. All other transactions' flag is set to `None`. * @enum {0 | 1 | 2} */ export const TransactionFlag = Object.freeze({ None: 0, "0": "None", ContractCreation: 1, "1": "ContractCreation", Signaling: 2, "2": "Signaling", }); /** * @enum {0 | 1} */ export const TransactionFormat = Object.freeze({ Basic: 0, "0": "Basic", Extended: 1, "1": "Extended", }); const AddressFinalization = (typeof FinalizationRegistry === 'undefined') ? { register: () => {}, unregister: () => {} } : new FinalizationRegistry(ptr => wasm.__wbg_address_free(ptr >>> 0, 1)); /** * An object representing a Nimiq address. * Offers methods to parse and format addresses from and to strings. */ export class Address { static __wrap(ptr) { ptr = ptr >>> 0; const obj = Object.create(Address.prototype); obj.__wbg_ptr = ptr; AddressFinalization.register(obj, obj.__wbg_ptr, obj); return obj; } __destroy_into_raw() { const ptr = this.__wbg_ptr; this.__wbg_ptr = 0; AddressFinalization.unregister(this); return ptr; } free() { const ptr = this.__destroy_into_raw(); wasm.__wbg_address_free(ptr, 0); } /** * @returns {string} */ __getClassname() { let deferred1_0; let deferred1_1; try { const ret = wasm.address___getClassname(this.__wbg_ptr); deferred1_0 = ret[0]; deferred1_1 = ret[1]; return getStringFromWasm0(ret[0], ret[1]); } finally { wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); } } /** * @param {Uint8Array} bytes */ constructor(bytes) { const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.address_new(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } this.__wbg_ptr = ret[0] >>> 0; AddressFinalization.register(this, this.__wbg_ptr, this); return this; } /** * Deserializes an address from a byte array. * @param {Uint8Array} bytes * @returns {Address} */ static deserialize(bytes) { const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.address_deserialize(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return Address.__wrap(ret[0]); } /** * Parses an address from an {@link Address} instance, a hex string representation, or a byte array. * * Throws when an address cannot be parsed from the argument. * @param {Address | string | Uint8Array} addr * @returns {Address} */ static fromAny(addr) { const ret = wasm.address_fromAny(addr); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return Address.__wrap(ret[0]); } /** * Parses an address from a string representation, either user-friendly or hex format. * * Throws when an address cannot be parsed from the string. * @param {string} str * @returns {Address} */ static fromString(str) { const ptr0 = passStringToWasm0(str, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.address_fromString(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return Address.__wrap(ret[0]); } /** * Parses an address from its user-friendly string representation. * * Throws when an address cannot be parsed from the string. * @param {string} str * @returns {Address} */ static fromUserFriendlyAddress(str) { const ptr0 = passStringToWasm0(str, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.address_fromUserFriendlyAddress(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return Address.__wrap(ret[0]); } /** * Computes the multisig address of a list of signer public keys. * @param {(PublicKey | string | Uint8Array)[]} public_keys * @param {number} num_signers * @returns {Address} */ static fromPublicKeys(public_keys, num_signers) { const ret = wasm.address_fromPublicKeys(public_keys, num_signers); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return Address.__wrap(ret[0]); } /** * Formats the address into a plain string format. * @returns {string} */ toPlain() { let deferred1_0; let deferred1_1; try { const ret = wasm.address_toPlain(this.__wbg_ptr); deferred1_0 = ret[0]; deferred1_1 = ret[1]; return getStringFromWasm0(ret[0], ret[1]); } finally { wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); } } /** * Formats the address into user-friendly IBAN format. * @returns {string} */ toUserFriendlyAddress() { let deferred1_0; let deferred1_1; try { const ret = wasm.address_toUserFriendlyAddress(this.__wbg_ptr); deferred1_0 = ret[0]; deferred1_1 = ret[1]; return getStringFromWasm0(ret[0], ret[1]); } finally { wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); } } /** * Formats the address into hex format. * @returns {string} */ toHex() { let deferred1_0; let deferred1_1; try { const ret = wasm.address_toHex(this.__wbg_ptr); deferred1_0 = ret[0]; deferred1_1 = ret[1]; return getStringFromWasm0(ret[0], ret[1]); } finally { wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); } } /** * Returns the byte representation of the address. * @returns {Uint8Array} */ serialize() { const ret = wasm.address_serialize(this.__wbg_ptr); var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v1; } /** * Returns if this address is equal to the other address. * @param {Address} other * @returns {boolean} */ equals(other) { _assertClass(other, Address); const ret = wasm.address_equals(this.__wbg_ptr, other.__wbg_ptr); return ret !== 0; } /** * Compares this address to the other address. * * Returns -1 if this address is smaller than the other address, 0 if they are equal, * and 1 if this address is larger than the other address. * @param {Address} other * @returns {number} */ compare(other) { _assertClass(other, Address); const ret = wasm.address_compare(this.__wbg_ptr, other.__wbg_ptr); return ret; } } const BLSKeyPairFinalization = (typeof FinalizationRegistry === 'undefined') ? { register: () => {}, unregister: () => {} } : new FinalizationRegistry(ptr => wasm.__wbg_blskeypair_free(ptr >>> 0, 1)); /** * A BLS keypair * It is used by validators to vote during Tendermint rounds. * This is just a wrapper around our internal BLS structs */ export class BLSKeyPair { static __wrap(ptr) { ptr = ptr >>> 0; const obj = Object.create(BLSKeyPair.prototype); obj.__wbg_ptr = ptr; BLSKeyPairFinalization.register(obj, obj.__wbg_ptr, obj); return obj; } __destroy_into_raw() { const ptr = this.__wbg_ptr; this.__wbg_ptr = 0; BLSKeyPairFinalization.unregister(this); return ptr; } free() { const ptr = this.__destroy_into_raw(); wasm.__wbg_blskeypair_free(ptr, 0); } /** * Generates a new keypair from secure randomness. * @returns {BLSKeyPair} */ static generate() { const ret = wasm.blskeypair_generate(); return BLSKeyPair.__wrap(ret); } /** * Derives a keypair from an existing private key. * @param {BLSSecretKey} private_key * @returns {BLSKeyPair} */ static derive(private_key) { _assertClass(private_key, BLSSecretKey); const ret = wasm.blskeypair_derive(private_key.__wbg_ptr); return BLSKeyPair.__wrap(ret); } /** * Deserializes a keypair from a byte array. * @param {Uint8Array} bytes * @returns {BLSKeyPair} */ static deserialize(bytes) { const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.blskeypair_deserialize(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return BLSKeyPair.__wrap(ret[0]); } /** * @param {BLSSecretKey} secret_key * @param {BLSPublicKey} public_key */ constructor(secret_key, public_key) { _assertClass(secret_key, BLSSecretKey); _assertClass(public_key, BLSPublicKey); const ret = wasm.blskeypair_new(secret_key.__wbg_ptr, public_key.__wbg_ptr); this.__wbg_ptr = ret >>> 0; BLSKeyPairFinalization.register(this, this.__wbg_ptr, this); return this; } /** * Serializes to a byte array. * @returns {Uint8Array} */ serialize() { const ret = wasm.blskeypair_serialize(this.__wbg_ptr); var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v1; } /** * Gets the keypair's secret key. * @returns {BLSSecretKey} */ get secretKey() { const ret = wasm.blskeypair_secretKey(this.__wbg_ptr); return BLSSecretKey.__wrap(ret); } /** * Gets the keypair's public key. * @returns {BLSPublicKey} */ get publicKey() { const ret = wasm.blskeypair_publicKey(this.__wbg_ptr); return BLSPublicKey.__wrap(ret); } /** * Formats the keypair into a hex string. * @returns {string} */ toHex() { let deferred1_0; let deferred1_1; try { const ret = wasm.blskeypair_toHex(this.__wbg_ptr); deferred1_0 = ret[0]; deferred1_1 = ret[1]; return getStringFromWasm0(ret[0], ret[1]); } finally { wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); } } } const BLSPublicKeyFinalization = (typeof FinalizationRegistry === 'undefined') ? { register: () => {}, unregister: () => {} } : new FinalizationRegistry(ptr => wasm.__wbg_blspublickey_free(ptr >>> 0, 1)); /** * The public part of the BLS keypair. * This is specified in the staking contract to verify votes from Validators. */ export class BLSPublicKey { static __wrap(ptr) { ptr = ptr >>> 0; const obj = Object.create(BLSPublicKey.prototype); obj.__wbg_ptr = ptr; BLSPublicKeyFinalization.register(obj, obj.__wbg_ptr, obj); return obj; } __destroy_into_raw() { const ptr = this.__wbg_ptr; this.__wbg_ptr = 0; BLSPublicKeyFinalization.unregister(this); return ptr; } free() { const ptr = this.__destroy_into_raw(); wasm.__wbg_blspublickey_free(ptr, 0); } /** * Derives a public key from an existing private key. * @param {BLSSecretKey} secret_key * @returns {BLSPublicKey} */ static derive(secret_key) { _assertClass(secret_key, BLSSecretKey); const ret = wasm.blspublickey_derive(secret_key.__wbg_ptr); return BLSPublicKey.__wrap(ret); } /** * Deserializes a public key from a byte array. * @param {Uint8Array} bytes * @returns {BLSPublicKey} */ static deserialize(bytes) { const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.blspublickey_deserialize(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return BLSPublicKey.__wrap(ret[0]); } /** * Creates a new public key from a byte array. * @param {Uint8Array} bytes */ constructor(bytes) { const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.blspublickey_new(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } this.__wbg_ptr = ret[0] >>> 0; BLSPublicKeyFinalization.register(this, this.__wbg_ptr, this); return this; } /** * Serializes the public key to a byte array. * @returns {Uint8Array} */ serialize() { const ret = wasm.blspublickey_serialize(this.__wbg_ptr); var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v1; } /** * Parses a public key from its hex representation. * @param {string} hex * @returns {BLSPublicKey} */ static fromHex(hex) { const ptr0 = passStringToWasm0(hex, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.blspublickey_fromHex(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return BLSPublicKey.__wrap(ret[0]); } /** * Formats the public key into a hex string. * @returns {string} */ toHex() { let deferred1_0; let deferred1_1; try { const ret = wasm.blspublickey_toHex(this.__wbg_ptr); deferred1_0 = ret[0]; deferred1_1 = ret[1]; return getStringFromWasm0(ret[0], ret[1]); } finally { wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); } } } const BLSSecretKeyFinalization = (typeof FinalizationRegistry === 'undefined') ? { register: () => {}, unregister: () => {} } : new FinalizationRegistry(ptr => wasm.__wbg_blssecretkey_free(ptr >>> 0, 1)); /** * The secret part of the BLS keypair. * This is specified in the config file, and is used by Validators to vote. */ export class BLSSecretKey { static __wrap(ptr) { ptr = ptr >>> 0; const obj = Object.create(BLSSecretKey.prototype); obj.__wbg_ptr = ptr; BLSSecretKeyFinalization.register(obj, obj.__wbg_ptr, obj); return obj; } __destroy_into_raw() { const ptr = this.__wbg_ptr; this.__wbg_ptr = 0; BLSSecretKeyFinalization.unregister(this); return ptr; } free() { const ptr = this.__destroy_into_raw(); wasm.__wbg_blssecretkey_free(ptr, 0); } /** * Generates a new private key from secure randomness. * @returns {BLSSecretKey} */ static generate() { const ret = wasm.blssecretkey_generate(); return BLSSecretKey.__wrap(ret); } /** * Deserializes a private key from a byte array. * @param {Uint8Array} bytes * @returns {BLSSecretKey} */ static deserialize(bytes) { const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.blssecretkey_deserialize(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return BLSSecretKey.__wrap(ret[0]); } /** * Creates a new private key from a byte array. * @param {Uint8Array} bytes */ constructor(bytes) { const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.blssecretkey_new(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } this.__wbg_ptr = ret[0] >>> 0; BLSSecretKeyFinalization.register(this, this.__wbg_ptr, this); return this; } /** * Serializes the private key to a byte array. * @returns {Uint8Array} */ serialize() { const ret = wasm.blssecretkey_serialize(this.__wbg_ptr); var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v1; } /** * Parses a private key from its hex representation. * @param {string} hex * @returns {BLSSecretKey} */ static fromHex(hex) { const ptr0 = passStringToWasm0(hex, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.blssecretkey_fromHex(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return BLSSecretKey.__wrap(ret[0]); } /** * Formats the private key into a hex string. * @returns {string} */ toHex() { let deferred1_0; let deferred1_1; try { const ret = wasm.blssecretkey_toHex(this.__wbg_ptr); deferred1_0 = ret[0]; deferred1_1 = ret[1]; return getStringFromWasm0(ret[0], ret[1]); } finally { wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); } } } const ClientConfigurationFinalization = (typeof FinalizationRegistry === 'undefined') ? { register: () => {}, unregister: () => {} } : new FinalizationRegistry(ptr => wasm.__wbg_clientconfiguration_free(ptr >>> 0, 1)); /** * Use this to provide initialization-time configuration to the Client. * This is a simplified version of the configuration that is used for regular nodes, * since not all configuration knobs are available when running inside a browser. */ export class ClientConfiguration { __destroy_into_raw() { const ptr = this.__wbg_ptr; this.__wbg_ptr = 0; ClientConfigurationFinalization.unregister(this); return ptr; } free() { const ptr = this.__destroy_into_raw(); wasm.__wbg_clientconfiguration_free(ptr, 0); } /** * Creates a default client configuration that can be used to change the client's configuration. * * Use its `instantiateClient()` method to launch the client and connect to the network. */ constructor() { const ret = wasm.clientconfiguration_new(); this.__wbg_ptr = ret >>> 0; ClientConfigurationFinalization.register(this, this.__wbg_ptr, this); return this; } /** * Sets the network ID the client should use. Input is case-insensitive. * * Possible values are `'MainAlbatross' | 'TestAlbatross' | 'DevAlbatross'`. * Default is `'MainAlbatross'`. * @param {string} network */ network(network) { const ptr0 = passStringToWasm0(network, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.clientconfiguration_network(this.__wbg_ptr, ptr0, len0); if (ret[1]) { throw takeFromExternrefTable0(ret[0]); } } /** * Sets the list of seed nodes that are used to connect to the Nimiq Albatross network. * * Each array entry must be a proper Multiaddr format string. * @param {any[]} seeds */ seedNodes(seeds) { const ptr0 = passArrayJsValueToWasm0(seeds, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; wasm.clientconfiguration_seedNodes(this.__wbg_ptr, ptr0, len0); } /** * Sets the log level that is used when logging to the console. * * Possible values are `'trace' | 'debug' | 'info' | 'warn' | 'error'`. * Default is `'info'`. * @param {string} log_level */ logLevel(log_level) { const ptr0 = passStringToWasm0(log_level, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; wasm.clientconfiguration_logLevel(this.__wbg_ptr, ptr0, len0); } /** * Sets whether the client should only connect to secure WebSocket connections. * Default is `true`. * @param {boolean} only_secure_ws_connections */ onlySecureWsConnections(only_secure_ws_connections) { wasm.clientconfiguration_onlySecureWsConnections(this.__wbg_ptr, only_secure_ws_connections); } /** * Sets the desired number of peers the client should try to connect to. * Default is `12`. * @param {number} desired_peer_count */ desiredPeerCount(desired_peer_count) { wasm.clientconfiguration_desiredPeerCount(this.__wbg_ptr, desired_peer_count); } /** * Sets the maximum number of peers the client should connect to. * Default is `50`. * @param {number} peer_count_max */ peerCountMax(peer_count_max) { wasm.clientconfiguration_peerCountMax(this.__wbg_ptr, peer_count_max); } /** * Sets the maximum number of peers the client should connect to per IP address. * Default is `10`. * @param {number} peer_count_per_ip_max */ peerCountPerIpMax(peer_count_per_ip_max) { wasm.clientconfiguration_peerCountPerIpMax(this.__wbg_ptr, peer_count_per_ip_max); } /** * Sets the maximum number of peers the client should connect to per subnet. * Default is `10`. * @param {number} peer_count_per_subnet_max */ peerCountPerSubnetMax(peer_count_per_subnet_max) { wasm.clientconfiguration_peerCountPerSubnetMax(this.__wbg_ptr, peer_count_per_subnet_max); } /** * Sets the sync mode that shoud be used. * Only "light" and "pico" are supported for web clients * Default is "light" * @param {string} sync_mode */ syncMode(sync_mode) { const ptr0 = passStringToWasm0(sync_mode, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; wasm.clientconfiguration_syncMode(this.__wbg_ptr, ptr0, len0); } /** * Returns a plain configuration object to be passed to `Client.create`. * @returns {PlainClientConfiguration} */ build() { const ret = wasm.clientconfiguration_build(this.__wbg_ptr); return ret; } } const CommitmentFinalization = (typeof FinalizationRegistry === 'undefined') ? { register: () => {}, unregister: () => {} } : new FinalizationRegistry(ptr => wasm.__wbg_commitment_free(ptr >>> 0, 1)); /** * A cryptographic commitment to a {@link RandomSecret}. The commitment is public, while the secret is, well, secret. */ export class Commitment { static __wrap(ptr) { ptr = ptr >>> 0; const obj = Object.create(Commitment.prototype); obj.__wbg_ptr = ptr; CommitmentFinalization.register(obj, obj.__wbg_ptr, obj); return obj; } __destroy_into_raw() { const ptr = this.__wbg_ptr; this.__wbg_ptr = 0; CommitmentFinalization.unregister(this); return ptr; } free() { const ptr = this.__destroy_into_raw(); wasm.__wbg_commitment_free(ptr, 0); } /** * @returns {string} */ __getClassname() { let deferred1_0; let deferred1_1; try { const ret = wasm.commitment___getClassname(this.__wbg_ptr); deferred1_0 = ret[0]; deferred1_1 = ret[1]; return getStringFromWasm0(ret[0], ret[1]); } finally { wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); } } /** * @returns {number} */ static get SIZE() { const ret = wasm.commitment_size(); return ret >>> 0; } /** * @returns {number} */ get serializedSize() { const ret = wasm.commitment_serialized_size(this.__wbg_ptr); return ret >>> 0; } /** * Derives a commitment from an existing random secret. * @param {RandomSecret} random_secret * @returns {Commitment} */ static derive(random_secret) { _assertClass(random_secret, RandomSecret); const ret = wasm.commitment_derive(random_secret.__wbg_ptr); return Commitment.__wrap(ret); } /** * Sums up multiple commitments into one aggregated commitment. * @param {(Commitment | string | Uint8Array)[]} commitments * @returns {Commitment} */ static sum(commitments) { const ret = wasm.commitment_sum(commitments); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return Commitment.__wrap(ret[0]); } /** * Parses a commitment from a {@link Commitment} instance, a hex string representation, or a byte array. * * Throws when a Commitment cannot be parsed from the argument. * @param {Commitment | string | Uint8Array} commitment * @returns {Commitment} */ static fromAny(commitment) { const ret = wasm.commitment_fromAny(commitment); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return Commitment.__wrap(ret[0]); } /** * Deserializes a commitment from a byte array. * * Throws when the byte array contains less than 32 bytes. * @param {Uint8Array} bytes * @returns {Commitment} */ static deserialize(bytes) { const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.commitment_deserialize(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return Commitment.__wrap(ret[0]); } /** * Creates a new commitment from a byte array. * * Throws when the byte array is not exactly 32 bytes long. * @param {Uint8Array} bytes */ constructor(bytes) { const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.commitment_new(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } this.__wbg_ptr = ret[0] >>> 0; CommitmentFinalization.register(this, this.__wbg_ptr, this); return this; } /** * Serializes the commitment to a byte array. * @returns {Uint8Array} */ serialize() { const ret = wasm.commitment_serialize(this.__wbg_ptr); var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v1; } /** * Parses a commitment from its hex representation. * * Throws when the string is not valid hex format or when it represents less than 32 bytes. * @param {string} hex * @returns {Commitment} */ static fromHex(hex) { const ptr0 = passStringToWasm0(hex, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.commitment_fromHex(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return Commitment.__wrap(ret[0]); } /** * Formats the commitment into a hex string. * @returns {string} */ toHex() { let deferred1_0; let deferred1_1; try { const ret = wasm.commitment_toHex(this.__wbg_ptr); deferred1_0 = ret[0]; deferred1_1 = ret[1]; return getStringFromWasm0(ret[0], ret[1]); } finally { wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); } } /** * Returns if this commitment is equal to the other commitment. * @param {Commitment} other * @returns {boolean} */ equals(other) { _assertClass(other, Commitment); const ret = wasm.commitment_equals(this.__wbg_ptr, other.__wbg_ptr); return ret !== 0; } } const CommitmentPairFinalization = (typeof FinalizationRegistry === 'undefined') ? { register: () => {}, unregister: () => {} } : new FinalizationRegistry(ptr => wasm.__wbg_commitmentpair_free(ptr >>> 0, 1)); /** * A structure holding both a random secret and its corresponding public commitment. * This is similar to a `KeyPair`. */ export class CommitmentPair { static __wrap(ptr) { ptr = ptr >>> 0; const obj = Object.create(CommitmentPair.prototype); obj.__wbg_ptr = ptr; CommitmentPairFinalization.register(obj, obj.__wbg_ptr, obj); return obj; } __destroy_into_raw() { const ptr = this.__wbg_ptr; this.__wbg_ptr = 0; CommitmentPairFinalization.unregister(this); return ptr; } free() { const ptr = this.__destroy_into_raw(); wasm.__wbg_commitmentpair_free(ptr, 0); } /** * @returns {string} */ __getClassname() { let deferred1_0; let deferred1_1; try { const ret = wasm.commitmentpair___getClassname(this.__wbg_ptr); deferred1_0 = ret[0]; deferred1_1 = ret[1]; return getStringFromWasm0(ret[0], ret[1]); } finally { wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); } } /** * @returns {number} */ static get SIZE() { const ret = wasm.commitmentpair_size(); return ret >>> 0; } /** * @returns {number} */ get serializedSize() { const ret = wasm.commitmentpair_serialized_size(this.__wbg_ptr); return ret >>> 0; } /** * Parses a commitment pair from a {@link CommitmentPair} instance, a hex string representation, or a byte array. * * Throws when a CommitmentPair cannot be parsed from the argument. * @param {CommitmentPair | string | Uint8Array} pair * @returns {CommitmentPair} */ static fromAny(pair) { const ret = wasm.commitmentpair_fromAny(pair); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return CommitmentPair.__wrap(ret[0]); } /** * Deserializes a commitment pair from a byte array. * * Throws when the byte array contains less than 32 bytes. * @param {Uint8Array} bytes * @returns {CommitmentPair} */ static deserialize(bytes) { const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.commitmentpair_deserialize(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return CommitmentPair.__wrap(ret[0]); } /** * @returns {CommitmentPair} */ static generate() { const ret = wasm.commitmentpair_generate(); return CommitmentPair.__wrap(ret); } /** * Derives a commitment pair from an existing random secret. * @param {RandomSecret} random_secret * @returns {CommitmentPair} */ static derive(random_secret) { _assertClass(random_secret, RandomSecret); const ret = wasm.commitmentpair_derive(random_secret.__wbg_ptr); return CommitmentPair.__wrap(ret); } /** * @param {RandomSecret} random_secret * @param {Commitment} commitment */ constructor(random_secret, commitment) { _assertClass(random_secret, RandomSecret); _assertClass(commitment, Commitment); const ret = wasm.commitmentpair_new(random_secret.__wbg_ptr, commitment.__wbg_ptr); this.__wbg_ptr = ret >>> 0; CommitmentPairFinalization.register(this, this.__wbg_ptr, this); return this; } /** * Serializes the commitment pair to a byte array. * @returns {Uint8Array} */ serialize() { const ret = wasm.commitmentpair_serialize(this.__wbg_ptr); var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v1; } /** * Parses a commitment pair from its hex representation. * * Throws when the string is not valid hex format or when it represents less than 32 bytes. * @param {string} hex * @returns {CommitmentPair} */ static fromHex(hex) { const ptr0 = passStringToWasm0(hex, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.commitmentpair_fromHex(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return CommitmentPair.__wrap(ret[0]); } /** * Formats the commitment pair into a hex string. * @returns {string} */ toHex() { let deferred1_0; let deferred1_1; try { const ret = wasm.commitmentpair_toHex(this.__wbg_ptr); deferred1_0 = ret[0]; deferred1_1 = ret[1]; return getStringFromWasm0(ret[0], ret[1]); } finally { wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); } } /** * @returns {RandomSecret} */ get secret() { const ret = wasm.commitmentpair_secret(this.__wbg_ptr); return RandomSecret.__wrap(ret); } /** * @returns {Commitment} */ get commitment() { const ret = wasm.commitmentpair_commitment(this.__wbg_ptr); return Commitment.__wrap(ret); } /** * Returns if this commitment pair is equal to the other commitment pair. * @param {CommitmentPair} other * @returns {boolean} */ equals(other) { _assertClass(other, CommitmentPair); const ret = wasm.commitmentpair_equals(this.__wbg_ptr, other.__wbg_ptr); return ret !== 0; } } const CryptoUtilsFinalization = (typeof FinalizationRegistry === 'undefined') ? { register: () => {}, unregister: () => {} } : new FinalizationRegistry(ptr => wasm.__wbg_cryptoutils_free(ptr >>> 0, 1)); export class CryptoUtils { __destroy_into_raw() { const ptr = this.__wbg_ptr; this.__wbg_ptr = 0; CryptoUtilsFinalization.unregister(this); return ptr; } free() { const ptr = this.__destroy_into_raw(); wasm.__wbg_cryptoutils_free(ptr, 0); } /** * Generates a secure random byte array of the given length. * @param {number} length * @returns {Uint8Array} */ static getRandomValues(length) { const ret = wasm.cryptoutils_getRandomValues(length); var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v1; } /** * Computes a 64-byte [HMAC]-SHA512 hash from the input key and data. * * [HMAC]: https://en.wikipedia.org/wiki/HMAC * @param {Uint8Array} key * @param {Uint8Array} data * @returns {Uint8Array} */ static computeHmacSha512(key, data) { const ptr0 = passArray8ToWasm0(key, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ptr1 = passArray8ToWasm0(data, wasm.__wbindgen_malloc); const len1 = WASM_VECTOR_LEN; const ret = wasm.cryptoutils_computeHmacSha512(ptr0, len0, ptr1, len1); var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v3; } /** * Computes a [PBKDF2]-over-SHA512 key from the password with the given parameters. * * [PBKDF2]: https://en.wikipedia.org/wiki/PBKDF2 * @param {Uint8Array} password * @param {Uint8Array} salt * @param {number} iterations * @param {number} derived_key_length * @returns {Uint8Array} */ static computePBKDF2sha512(password, salt, iterations, derived_key_length) { const ptr0 = passArray8ToWasm0(password, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ptr1 = passArray8ToWasm0(salt, wasm.__wbindgen_malloc); const len1 = WASM_VECTOR_LEN; const ret = wasm.cryptoutils_computePBKDF2sha512(ptr0, len0, ptr1, len1, iterations, derived_key_length); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v3; } } const ES256PublicKeyFinalization = (typeof FinalizationRegistry === 'undefined') ? { register: () => {}, unregister: () => {} } : new FinalizationRegistry(ptr => wasm.__wbg_es256publickey_free(ptr >>> 0, 1)); /** * The non-secret (public) part of an ES256 asymmetric key pair that is typically used to digitally verify or encrypt data. */ export class ES256PublicKey { static __wrap(ptr) { ptr = ptr >>> 0; const obj = Object.create(ES256PublicKey.prototype); obj.__wbg_ptr = ptr; ES256PublicKeyFinalization.register(obj, obj.__wbg_ptr, obj); return obj; } __destroy_into_raw() { const ptr = this.__wbg_ptr; this.__wbg_ptr = 0; ES256PublicKeyFinalization.unregister(this); return ptr; } free() { const ptr = this.__destroy_into_raw(); wasm.__wbg_es256publickey_free(ptr, 0); } /** * @returns {string} */ __getClassname() { let deferred1_0; let deferred1_1; try { const ret = wasm.es256publickey___getClassname(this.__wbg_ptr); deferred1_0 = ret[0]; deferred1_1 = ret[1]; return getStringFromWasm0(ret[0], ret[1]); } finally { wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); } } /** * Verifies that a signature is valid for this public key and the provided data. * @param {ES256Signature} signature * @param {Uint8Array} data * @returns {boolean} */ verify(signature, data) { _assertClass(signature, ES256Signature); const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.es256publickey_verify(this.__wbg_ptr, signature.__wbg_ptr, ptr0, len0); return ret !== 0; } /** * Deserializes a public key from a byte array. * * Throws when the byte array contains less than 33 bytes. * @param {Uint8Array} bytes * @returns {ES256PublicKey} */ static deserialize(bytes) { const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.es256publickey_deserialize(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return ES256PublicKey.__wrap(ret[0]); } /** * Deserializes a public key from its SPKI representation. * @param {Uint8Array} spki_bytes * @returns {ES256PublicKey} */ static fromSpki(spki_bytes) { const ptr0 = passArray8ToWasm0(spki_bytes, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.es256publickey_fromSpki(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return ES256PublicKey.__wrap(ret[0]); } /** * Deserializes a public key from its raw representation. * @param {Uint8Array} raw_bytes * @returns {ES256PublicKey} */ static fromRaw(raw_bytes) { const ptr0 = passArray8ToWasm0(raw_bytes, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.es256publickey_fromRaw(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return ES256PublicKey.__wrap(ret[0]); } /** * Creates a new public key from a byte array. * * Compatible with the `-7` COSE algorithm identifier. * * ## Example * * ```javascript * // Create/register a credential with the Webauthn API: * const cred = await navigator.credentials.create({ * publicKey: { * pubKeyCredParams: [{ * type: "public-key", * alg: -7, // ES256 = ECDSA over P-256 with SHA-256 * }], * // ... * }, * }); * * // Then create an instance of ES256PublicKey from the credential response: * const publicKey = new Nimiq.ES256PublicKey(new Uint8Array(cred.response.getPublicKey())); * ``` * @param {Uint8Array} bytes */ constructor(bytes) { const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.es256publickey_new(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } this.__wbg_ptr = ret[0] >>> 0; ES256PublicKeyFinalization.register(this, this.__wbg_ptr, this); return this; } /** * Serializes the public key to a byte array. * @returns {Uint8Array} */ serialize() { const ret = wasm.es256publickey_serialize(this.__wbg_ptr); var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v1; } /** * Parses a public key from its hex representation. * * Throws when the string is not valid hex format or when it represents less than 33 bytes. * @param {string} hex * @returns {ES256PublicKey} */ static fromHex(hex) { const ptr0 = passStringToWasm0(hex, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.es256publickey_fromHex(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return ES256PublicKey.__wrap(ret[0]); } /** * Formats the public key into a hex string. * @returns {string} */ toHex() { let deferred1_0; let deferred1_1; try { const ret = wasm.es256publickey_toHex(this.__wbg_ptr); deferred1_0 = ret[0]; deferred1_1 = ret[1]; return getStringFromWasm0(ret[0], ret[1]); } finally { wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); } } /** * Gets the public key's address. * @returns {Address} */ toAddress() { const ret = wasm.es256publickey_toAddress(this.__wbg_ptr); return Address.__wrap(ret); } /** * Returns if this public key is equal to the other public key. * @param {ES256PublicKey} other * @returns {boolean} */ equals(other) { _assertClass(other, ES256PublicKey); const ret = wasm.es256publickey_equals(this.__wbg_ptr, other.__wbg_ptr); return ret !== 0; } /** * Compares this public key to the other public key. * * Returns -1 if this public key is smaller than the other public key, 0 if they are equal, * and 1 if this public key is larger than the other public key. * @param {ES256PublicKey} other * @returns {number} */ compare(other) { _assertClass(other, ES256PublicKey); const ret = wasm.es256publickey_compare(this.__wbg_ptr, other.__wbg_ptr); return ret; } } const ES256SignatureFinalization = (typeof FinalizationRegistry === 'undefined') ? { register: () => {}, unregister: () => {} } : new FinalizationRegistry(ptr => wasm.__wbg_es256signature_free(ptr >>> 0, 1)); /** * An ES256 Signature represents a cryptographic proof that an ES256 private key signed some data. * It can be verified with the p