UNPKG

@nimiq/core

Version:

Nimiq's Rust-to-WASM web client

1,507 lines (1,496 loc) 182 kB
/** * @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", }); /** * 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); } } /** * 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; } /** * 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]); } /** * 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; } /** * 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]); } /** * 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]); } /** * 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]); } /** * @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; } /** * The all-zeroes burn address. * @returns {Address} */ static get NULL() { const ret = wasm.address_null(); return Address.__wrap(ret); } /** * 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; } /** * 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); } } /** * 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); } } } if (Symbol.dispose) Address.prototype[Symbol.dispose] = Address.prototype.free; /** * 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); } /** * 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]); } /** * Generates a new keypair from secure randomness. * @returns {BLSKeyPair} */ static generate() { const ret = wasm.blskeypair_generate(); return BLSKeyPair.__wrap(ret); } /** * @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; } /** * Gets the keypair's public key. * @returns {BLSPublicKey} */ get publicKey() { const ret = wasm.blskeypair_publicKey(this.__wbg_ptr); return BLSPublicKey.__wrap(ret); } /** * Gets the keypair's secret key. * @returns {BLSSecretKey} */ get secretKey() { const ret = wasm.blskeypair_secretKey(this.__wbg_ptr); return BLSSecretKey.__wrap(ret); } /** * 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; } /** * 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); } } } if (Symbol.dispose) BLSKeyPair.prototype[Symbol.dispose] = BLSKeyPair.prototype.free; /** * 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]); } /** * 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]); } /** * 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; } /** * 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); } } } if (Symbol.dispose) BLSPublicKey.prototype[Symbol.dispose] = BLSPublicKey.prototype.free; /** * 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); } /** * 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]); } /** * 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]); } /** * Generates a new private key from secure randomness. * @returns {BLSSecretKey} */ static generate() { const ret = wasm.blssecretkey_generate(); return BLSSecretKey.__wrap(ret); } /** * 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; } /** * 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); } } } if (Symbol.dispose) BLSSecretKey.prototype[Symbol.dispose] = BLSSecretKey.prototype.free; /** * 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); } /** * Returns a plain configuration object to be passed to `Client.create`. * @returns {PlainClientConfiguration} */ build() { const ret = wasm.clientconfiguration_build(this.__wbg_ptr); return ret; } /** * 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 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 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 maximum network buffer size, which should be greater than 0 * Default is `1024`. * @param {number} network_buffer_size */ networkBufferSize(network_buffer_size) { wasm.clientconfiguration_networkBufferSize(this.__wbg_ptr, network_buffer_size); } /** * 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 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 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 list of seed nodes that are used to connect to the Nimiq Albatross network. * * Each array entry must be a proper Multiaddr format string. * * Throws when an entry cannot be deserialized as a string. * @param {any[]} seeds */ seedNodes(seeds) { const ptr0 = passArrayJsValueToWasm0(seeds, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.clientconfiguration_seedNodes(this.__wbg_ptr, ptr0, len0); if (ret[1]) { throw takeFromExternrefTable0(ret[0]); } } /** * Sets the sync mode that should be used. * Only "light" and "pico" are supported for web clients * Default is "pico" * @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); } } if (Symbol.dispose) ClientConfiguration.prototype[Symbol.dispose] = ClientConfiguration.prototype.free; /** * 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); } } /** * 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); } /** * 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]); } /** * 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; } /** * 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]); } /** * 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]); } /** * 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; } /** * @returns {number} */ get serializedSize() { const ret = wasm.commitment_serialized_size(this.__wbg_ptr); return ret >>> 0; } /** * @returns {number} */ static get SIZE() { const ret = wasm.commitment_size(); return ret >>> 0; } /** * Sums up multiple commitments into one aggregated commitment. * * Attention: This is a simple summation, not a MuSig2 aggregation! For MuSig2 aggregation, use {@link Commitment.sumMuSig2}. * @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]); } /** * Aggregates commitments into one aggregated commitment using the MuSig2 scheme. * * - Each commitment group must correspond to the public key at the same index in the `publicKeys` array. * - The number of commitment groups and public keys must be the same. * - Each commitment group must contain exactly `MUSIG2_PARAMETER_V = 2` commitments. * - The `data` parameter is the same data that will be signed using the aggregated commitment, e.g. the serialized content of a transaction. * * Returns the aggregated commitment. * @param {(PublicKey | string | Uint8Array)[]} public_keys * @param {(Commitment | string | Uint8Array)[][]} commitment_groups * @param {Uint8Array} data * @returns {Commitment} */ static sumMuSig2(public_keys, commitment_groups, data) { const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.commitment_sumMuSig2(public_keys, commitment_groups, 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); } } } if (Symbol.dispose) Commitment.prototype[Symbol.dispose] = Commitment.prototype.free; /** * 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 {Commitment} */ get commitment() { const ret = wasm.commitmentpair_commitment(this.__wbg_ptr); return Commitment.__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); } /** * 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 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; } /** * 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]); } /** * 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]); } /** * @returns {CommitmentPair} */ static generate() { const ret = wasm.commitmentpair_generate(); 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; } /** * @returns {RandomSecret} */ get secret() { const ret = wasm.commitmentpair_secret(this.__wbg_ptr); return RandomSecret.__wrap(ret); } /** * 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; } /** * @returns {number} */ get serializedSize() { const ret = wasm.commitmentpair_serialized_size(this.__wbg_ptr); return ret >>> 0; } /** * @returns {number} */ static get SIZE() { const ret = wasm.commitmentpair_size(); return 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); } } } if (Symbol.dispose) CommitmentPair.prototype[Symbol.dispose] = CommitmentPair.prototype.free; 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); } /** * 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; } /** * 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; } } if (Symbol.dispose) CryptoUtils.prototype[Symbol.dispose] = CryptoUtils.prototype.free; /** * 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); } } /** * 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; } /** * 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]); } /** * 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; } /** * 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]); } /** * 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]); } /** * 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]); } /** * 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; } /** * Gets the public key's address. * @returns {Address} */ toAddress() { const ret = wasm.es256publickey_toAddress(this.__wbg_ptr); return Address.__wrap(ret); } /** * 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); } } /** * 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; } } if (Symbol.dispose) ES256PublicKey.prototype[Symbol.dispose] = ES256PublicKey.prototype.free; /** * An ES256 Signature represents a cryptographic proof that an ES256 private key signed some data. * It can be verified with the private key's public key. */ export class ES256Signature { static __wrap(ptr) { ptr = ptr >>> 0; const obj = Object.create(ES256Signature.prototype); obj.__wbg_ptr = ptr; ES256SignatureFinalization.register(obj, obj.__wbg_ptr, obj); return obj; } __destroy_into_raw() { const ptr = this.__wbg_ptr; this.__wbg_ptr = 0; ES256SignatureFinalization.unregister(this); return ptr; } free() { const ptr = this.__destroy_into_raw(); wasm.__wbg_es256signature_free(ptr, 0); } /** * @returns {string} */ __getClassname() { let deferred1_0; let deferred1_1; try { const ret = wasm.es256signature___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); } } /** * Deserializes an ES256 signature from a byte array. * * Throws when the byte array contains less than 64 bytes. * @param {Uint8Array} bytes * @returns {ES256Signature} */ static deserialize(bytes) { const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.es256signature_deserialize(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return ES256Signature.__wrap(ret[0]); } /** * Parses an ES256 signature from its ASN.1 representation. * @param {Uint8Array} bytes * @returns {ES256Signature} */ static fromAsn1(bytes) { const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.es256signature_fromAsn1(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return ES256Signature.__wrap(ret[0]); } /** * Parses an ES256 signature from its hex representation. * * Throws when the string is not valid hex format or when it represents less than 64 bytes. * @param {string} hex * @returns {ES256Signature} */ static fromHex(hex) { const ptr0 = passStringToWasm0(hex, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.es256signature_fromHex(ptr0, len0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return ES256Signature.__wrap(ret[0]); } /** * Serializes the signature to a byte array. * @returns {Uint8Array} */ serialize() { const ret = wasm.es256signature_serialize(this.__wbg_ptr); var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v1; } /** * Formats the signature into a hex string. * @returns {string} */ toHex() { let deferred1_0; let deferred1_1; try { const ret = wasm.es256signature_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); } } } if (Symbol.dispose) ES256Signature.prototype[Symbol.dispose] = ES256Signature.prototype.free; export class Hash { __destroy_into_raw() { const ptr = this.__wbg_ptr; this.__wbg_ptr = 0; HashFinalization.unregister(this); return ptr; } free() { const ptr = this.__destroy_into_raw(); wasm.__wbg_hash_free(ptr, 0); } /** * Computes a 32-byte [Blake2b] hash from the input data. * * Blake2b is used for example to compute a public key's address. * * [Blake2b]: https://en.wikipedia.org/wiki/BLAKE_(hash_function) * @param {Uint8Array} data * @returns {Uint8Array} */ static computeBlake2b(data) { const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.hash_computeBlake2b(ptr0, len0); var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v2; } /** * Computes an [Argon2d] hash with some Nimiq-specific parameters. * * `iterations` specifies the number of iterations done in the hash * function. It can be used to control the hash computation time. * Increasing this will make it harder for an attacker to brute-force the * password. * * `derived_key_length` specifies the number of bytes that are output. * * [Argon2d]: https://en.wikipedia.org/wiki/Argon2 * @param {Uint8Array} password * @param {Uint8Array} salt * @param {number} iterations * @param {number} derived_key_length * @returns {Uint8Array} */ static computeNimiqArgon2d(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.hash_computeNimiqArgon2d(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; } /** * Computes an [Argon2id] hash with some Nimiq-specific parameters. * * `iterations` specifies the number of iterations done in the hash * function. It can be used to control the hash