diff --git a/packages/sdk/jest.setup.ts b/packages/sdk/jest.setup.ts index 5fe372a..12d6874 100644 --- a/packages/sdk/jest.setup.ts +++ b/packages/sdk/jest.setup.ts @@ -2,3 +2,10 @@ import fetchMock from 'jest-fetch-mock'; // import 'whatwg-fetch'; globalThis.fetch = fetchMock as any; fetchMock.enableMocks(); + +import { TextDecoder, TextEncoder } from 'util'; + +Object.assign(global, { + TextDecoder, + TextEncoder, +}); diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 70c8230..c4d891a 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -12,13 +12,14 @@ "docs": "typedoc --options typedoc.js" }, "dependencies": { - "@mintlayer/wasm-lib": "^0.1.0" + "@mintlayer/wasm-lib": "workspace:*" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "@types/jest": "^29.5.14", + "@types/node":"^20.4.2", "jest": "^29.7.0", "jest-environment-jsdom": "^29.7.0", "jest-fetch-mock": "^3.0.3", diff --git a/packages/sdk/src/mintlayer-connect-sdk.ts b/packages/sdk/src/mintlayer-connect-sdk.ts index 227ec27..4903529 100644 --- a/packages/sdk/src/mintlayer-connect-sdk.ts +++ b/packages/sdk/src/mintlayer-connect-sdk.ts @@ -2609,8 +2609,10 @@ class Client { const transaction_id = get_transaction_id(transaction, true); if (finalOutputs.some((output) => output.type === 'IssueNft')) { + const block_height = 200000n; // TODO: Get the current block height const token_id = get_token_id( mergeUint8Arrays(BINRepresentation.inputs), + block_height, this.network === 'mainnet' ? Network.Mainnet : Network.Testnet, ); const index = finalOutputs.findIndex((output) => output.type === 'IssueNft'); @@ -2675,14 +2677,17 @@ class Client { .filter(({ input }) => input.input_type === 'AccountCommand' || input.input_type === 'Account') .map(({ input }) => { if (input.command === 'ConcludeOrder') { - return encode_input_for_conclude_order(input.order_id, BigInt(input.nonce.toString()), network); + const block_height = 200000n; // TODO: Get the current block height + return encode_input_for_conclude_order(input.order_id, BigInt(input.nonce.toString()), block_height, network); } if (input.command === 'FillOrder') { + const block_height = 200000n; // TODO: Get the current block height return encode_input_for_fill_order( input.order_id, Amount.from_atoms(input.fill_atoms.toString()), input.destination, BigInt(input.nonce.toString()), + BigInt(block_height), network, ); } @@ -4150,7 +4155,7 @@ class Signer { (input, index) => { let address: string | undefined = undefined; - if(input.input.input_type === 'UTXO') { + if (input.input.input_type === 'UTXO') { const utxoInput = input as UtxoInput; address = utxoInput.utxo.destination; } @@ -4168,7 +4173,7 @@ class Signer { throw new Error(`Address not found for input at index ${index}`); } - const addressPrivateKey = this.getPrivateKey(address) + const addressPrivateKey = this.getPrivateKey(address); if (!addressPrivateKey) { throw new Error(`Private key not found for address: ${address}`); @@ -4176,6 +4181,12 @@ class Signer { const transaction = this.hexToUint8Array(tx.HEXRepresentation_unsigned); + const block_height = 200000n; // TODO: Get the current block height + const additional_info = { + pool_info: {}, + order_info: {} + }; + const witness = encode_witness( SignatureHashType.ALL, addressPrivateKey, @@ -4183,9 +4194,11 @@ class Signer { transaction, optUtxos, index, + additional_info, + block_height, network, - ) - return witness + ); + return witness; }, ) diff --git a/packages/sdk/src/transaction.ts b/packages/sdk/src/transaction.ts index 65cda1a..a0e17a4 100644 --- a/packages/sdk/src/transaction.ts +++ b/packages/sdk/src/transaction.ts @@ -36,7 +36,11 @@ import { Network, TokenUnfreezable, TotalSupply, + decode_signed_transaction_to_js, } from '@mintlayer/wasm-lib'; + +import * as wasmLib from '@mintlayer/wasm-lib'; + import { mergeUint8Arrays, atomsToDecimal, stringToUint8Array } from './utils'; import { UtxoEntry, UtxoInput } from './types/transaction'; @@ -103,6 +107,46 @@ export class Transaction { return this; } + enrichUtxo(knownUtxo: any): this { + const input = this.jsonRepresentation.inputs.find( + ({ input }: any) => + input.index === knownUtxo.outpoint.index && + input.input_type === knownUtxo.outpoint.input_type && + input.source_id === knownUtxo.outpoint.source_id && + input.source_type === knownUtxo.outpoint.source_type, + ); + + if (!input) { + throw new Error(`UTXO input not found: ${knownUtxo.outpoint.source_id}:${knownUtxo.outpoint.index}`); + } + + input.utxo = knownUtxo.utxo; + + this.updateFee(); + + return this; + } + + private updateFee(): void { + const inputAtoms = this.jsonRepresentation.inputs.reduce((total: any, item: any) => { + const atoms = item.utxo?.value?.amount?.atoms; + + return atoms === undefined ? total : total + BigInt(atoms); + }, 0n); + + const outputAtoms = this.jsonRepresentation.outputs.reduce((total: any, output: any) => { + return total + BigInt(output.value.amount.atoms); + }, 0n); + + const feeAtoms = inputAtoms - outputAtoms; + + if (feeAtoms < 0n) { + throw new Error('Transaction fee cannot be negative'); + } + + this.fee = BigInt(feeAtoms); + } + setNetwork(network: 'mainnet' | 'testnet') { this.network = network; return this; @@ -133,9 +177,47 @@ export class Transaction { } fromHEX(hex: string) { + if (typeof hex !== 'string') { + throw new Error('Transaction hex must be a string'); + } + + if (!hex.length) { + throw new Error('Transaction hex cannot be empty'); + } + + if (!/^[0-9a-fA-F]+$/.test(hex)) { + throw new Error('Transaction hex contains invalid characters'); + } + + if (hex.length % 2 !== 0) { + throw new Error('Transaction hex must have an even number of characters'); + } + + const bytes = new Uint8Array(hex.match(/.{1,2}/g)!.map((byte) => parseInt(byte, 16))); + + const network = this.network === 'mainnet' ? 0 : 1; + + const decoded = wasmLib.decode_transaction_to_js(bytes, network); + + this.hexRepresentation = hex; + this.jsonRepresentation = decoded_to_json_representation(decoded); + this.jsonRepresentation.id = get_transaction_id(bytes, false); + this.transactionId = get_transaction_id(bytes, false); + return this; } + static fromHEX( + hex: string, + options: { + network?: 'mainnet' | 'testnet'; + } = {}, + ) { + const transaction = new Transaction(options); + + return transaction.fromHEX(hex); + } + getTransactionId() { return this.transactionId; } @@ -417,7 +499,12 @@ export class Transaction { .filter(({ input }) => input.input_type === 'AccountCommand' || input.input_type === 'Account') .map(({ input }) => { if (input.command === 'ConcludeOrder') { - return encode_input_for_conclude_order(input.order_id, BigInt(input.nonce.toString()), network); + return encode_input_for_conclude_order( + input.order_id, + BigInt(input.nonce.toString()), + BigInt(this.currentBlockHeight), + network, + ); } if (input.command === 'FillOrder') { return encode_input_for_fill_order( @@ -425,6 +512,7 @@ export class Transaction { Amount.from_atoms(input.fill_atoms.toString()), input.destination, BigInt(input.nonce.toString()), + BigInt(this.currentBlockHeight), network, ); } @@ -703,10 +791,10 @@ export class Transaction { type: 'Coin', amount: { atoms: amount, - decimal: amount, + decimal: atomsToDecimal(amount, 11), }, }, - } + }; } transferToken(destination: string, amount: string, token_id: string): Output { @@ -736,7 +824,57 @@ export class Transaction { delegation_id: '', amount: 0, }, - } + }; } - } + +const decoded_to_json_representation = (decoded: any) => { + const tx = decoded.V1; + + return { + inputs: tx.inputs.map((input: any, index: any) => { + if (input.Utxo) { + return { + input: { + index, + input_type: 'UTXO', + source_id: input.Utxo.id.Transaction, + source_type: 'Transaction', + }, + }; + } + + if (input.Account) { + return { + input: { + index, + input_type: 'ACCOUNT', + // TODO: map account input fields if needed + }, + }; + } + + throw new Error(`Unsupported input type at index ${index}`); + }), + + outputs: tx.outputs.map((output: any) => { + if (output.Transfer) { + const [value, destination] = output.Transfer; + + return { + type: 'Transfer', + destination, + value: { + type: Object.keys(value)[0], + amount: { + atoms: value[Object.keys(value)[0]].atoms, + decimal: atomsToDecimal(value[Object.keys(value)[0]].atoms, 11), + }, + }, + }; + } + + throw new Error(`Unsupported output type: ${Object.keys(output)[0]}`); + }), + }; +}; diff --git a/packages/sdk/tests/__mocks__/pkg-node/package.json b/packages/sdk/tests/__mocks__/pkg-node/package.json index 4017c72..f39aeac 100644 --- a/packages/sdk/tests/__mocks__/pkg-node/package.json +++ b/packages/sdk/tests/__mocks__/pkg-node/package.json @@ -1,6 +1,6 @@ { "name": "wasm-wrappers", - "version": "1.0.2", + "version": "1.4.0", "license": "MIT", "files": [ "wasm_wrappers_bg.wasm", @@ -9,4 +9,4 @@ ], "main": "wasm_wrappers.js", "types": "wasm_wrappers.d.ts" -} \ No newline at end of file +} diff --git a/packages/sdk/tests/__mocks__/pkg-node/wasm_wrappers.d.ts b/packages/sdk/tests/__mocks__/pkg-node/wasm_wrappers.d.ts index 01bb6d1..e045f99 100644 --- a/packages/sdk/tests/__mocks__/pkg-node/wasm_wrappers.d.ts +++ b/packages/sdk/tests/__mocks__/pkg-node/wasm_wrappers.d.ts @@ -1,271 +1,497 @@ /* tslint:disable */ /* eslint-disable */ /** - * A utxo can either come from a transaction or a block reward. - * Given a source id, whether from a block reward or transaction, this function - * takes a generic id with it, and returns serialized binary data of the id - * with the given source id. + * Additional information for a pool. */ -export function encode_outpoint_source_id(id: Uint8Array, source: SourceId): Uint8Array; +export interface PoolAdditionalInfo { + staker_balance: SimpleAmount; +} + /** - * Generates a new, random private key from entropy + * Additional information for a transaction. */ -export function make_private_key(): Uint8Array; +export interface TxAdditionalInfo { + pool_info: Record; + order_info: Record; +} + /** - * Create the default account's extended private key for a given mnemonic - * derivation path: 44'/mintlayer_coin_type'/0' + * Additional information for an order. */ -export function make_default_account_privkey(mnemonic: string, network: Network): Uint8Array; +export interface OrderAdditionalInfo { + initially_asked: SimpleCurrencyAmount; + initially_given: SimpleCurrencyAmount; + ask_balance: SimpleAmount; + give_balance: SimpleAmount; +} + /** - * From an extended private key create a receiving private key for a given key index - * derivation path: current_derivation_path/0/key_index + * An alternative representation of `Amount`. */ -export function make_receiving_address(private_key_bytes: Uint8Array, key_index: number): Uint8Array; +export interface SimpleAmount { + atoms: string; +} + /** - * From an extended private key create a change private key for a given key index - * derivation path: current_derivation_path/1/key_index + * An amount of coins or some token, */ -export function make_change_address(private_key_bytes: Uint8Array, key_index: number): Uint8Array; +export type SimpleCurrencyAmount = { coins: SimpleAmount } | { tokens: SimpleTokenAmount }; + /** - * Given a public key (as bytes) and a network type (mainnet, testnet, etc), - * return the address public key hash from that public key as an address + * An amount of some token. */ -export function pubkey_to_pubkeyhash_address(public_key_bytes: Uint8Array, network: Network): string; +export interface SimpleTokenAmount { + token_id: string; + amount: SimpleAmount; +} + + /** - * Given a private key, as bytes, return the bytes of the corresponding public key + * Amount type abstraction. The amount type is stored in a string + * since JavaScript number type cannot fit 128-bit integers. + * The amount is given as an integer in units of "atoms". + * Atoms are the smallest, indivisible amount of a coin or token. */ -export function public_key_from_private_key(private_key: Uint8Array): Uint8Array; +export class Amount { + private constructor(); + free(): void; + [Symbol.dispose](): void; + atoms(): string; + static from_atoms(atoms: string): Amount; +} + /** - * Return the extended public key from an extended private key + * Indicates whether a token can be frozen */ -export function extended_public_key_from_extended_private_key(private_key_bytes: Uint8Array): Uint8Array; +export enum FreezableToken { + No = 0, + Yes = 1, +} + /** - * From an extended public key create a receiving public key for a given key index - * derivation path: current_derivation_path/0/key_index + * The network, for which an operation to be done. Mainnet, testnet, etc. */ -export function make_receiving_address_public_key(extended_public_key_bytes: Uint8Array, key_index: number): Uint8Array; +export enum Network { + Mainnet = 0, + Testnet = 1, + Regtest = 2, + Signet = 3, +} + /** - * From an extended public key create a change public key for a given key index - * derivation path: current_derivation_path/1/key_index + * The part of the transaction that will be committed in the signature. Similar to bitcoin's sighash. */ -export function make_change_address_public_key(extended_public_key_bytes: Uint8Array, key_index: number): Uint8Array; +export enum SignatureHashType { + ALL = 0, + NONE = 1, + SINGLE = 2, + ANYONECANPAY = 3, +} + /** - * Given a message and a private key, sign the message with the given private key - * This kind of signature is to be used when signing spend requests, such as transaction - * input witness. + * A utxo can either come from a transaction or a block reward. This enum signifies that. */ -export function sign_message_for_spending(private_key: Uint8Array, message: Uint8Array): Uint8Array; +export enum SourceId { + Transaction = 0, + BlockReward = 1, +} + /** - * Given a digital signature, a public key and a message. Verify that - * the signature is produced by signing the message with the private key - * that derived the given public key. - * Note that this function is used for verifying messages related to spending, - * such as transaction input witness. + * Indicates whether a token can be unfrozen once frozen */ -export function verify_signature_for_spending(public_key: Uint8Array, signature: Uint8Array, message: Uint8Array): boolean; +export enum TokenUnfreezable { + No = 0, + Yes = 1, +} + /** - * Given a message and a private key, create and sign a challenge with the given private key. - * This kind of signature is to be used when signing challenges. + * The token supply of a specific token, set on issuance */ -export function sign_challenge(private_key: Uint8Array, message: Uint8Array): Uint8Array; +export enum TotalSupply { + /** + * Can be issued with no limit, but then can be locked to have a fixed supply. + */ + Lockable = 0, + /** + * Unlimited supply, no limits except for numeric limits due to u128 + */ + Unlimited = 1, + /** + * On issuance, the total number of coins is fixed + */ + Fixed = 2, +} + /** - * Given a signed challenge, an address and a message, verify that - * the signature is produced by signing the message with the private key - * that derived the given public key. - * This function is used for verifying messages-related challenges. - * - * Note: for signatures that were created by `sign_challenge`, the provided address must be - * a 'pubkeyhash' address. - * - * Note: currently this function never returns `false` - it either returns `true` or fails with an error. + * Returns the fee that needs to be paid by a transaction for issuing a data deposit */ -export function verify_challenge(address: string, network: Network, signed_challenge: Uint8Array, message: Uint8Array): boolean; +export function data_deposit_fee(current_block_height: bigint, network: Network): Amount; + /** - * Return the message that has to be signed to produce a signed transaction intent. + * Decodes a partially signed transaction from its binary encoding into a JavaScript object. */ -export function make_transaction_intent_message_to_sign(intent: string, transaction_id: string): Uint8Array; +export function decode_partially_signed_transaction_to_js(transaction: Uint8Array, network: Network): any; + /** - * Return a `SignedTransactionIntent` object as bytes given the message and encoded signatures. + * Decodes a signed transaction from its binary encoding into a JavaScript object. + */ +export function decode_signed_transaction_to_js(transaction: Uint8Array, network: Network): any; + +/** + * Decodes an unsigned transaction from its binary encoding into a JavaScript object. + */ +export function decode_transaction_to_js(transaction: Uint8Array, network: Network): any; + +/** + * Calculate the "effective balance" of a pool, given the total pool balance and pledge by the pool owner/staker. + * The effective balance is how the influence of a pool is calculated due to its balance. + */ +export function effective_pool_balance(network: Network, pledge_amount: Amount, pool_balance: Amount): Amount; + +/** + * Given ask and give amounts and a conclude key create output that creates an order. * - * Note: to produce a valid signed intent one is expected to sign the corresponding message by private keys - * corresponding to each input of the transaction. + * 'ask_token_id': the parameter represents a Token if it's Some and coins otherwise. + * 'give_token_id': the parameter represents a Token if it's Some and coins otherwise. + */ +export function encode_create_order_output(ask_amount: Amount, ask_token_id: string | null | undefined, give_amount: Amount, give_token_id: string | null | undefined, conclude_address: string, network: Network): Uint8Array; + +/** + * Convert the specified string address into a Destination object, encoded as bytes. + */ +export function encode_destination(address: string, network: Network): Uint8Array; + +/** + * Given a token_id, new authority destination and nonce return an encoded change token authority input + */ +export function encode_input_for_change_token_authority(token_id: string, new_authority: string, nonce: bigint, network: Network): Uint8Array; + +/** + * Given a token_id, new metadata uri and nonce return an encoded change token metadata uri input + */ +export function encode_input_for_change_token_metadata_uri(token_id: string, new_metadata_uri: string, nonce: bigint, network: Network): Uint8Array; + +/** + * Given an order id create an input that concludes the order. * - * Parameters: - * `signed_message` - this must have been produced by `make_transaction_intent_message_to_sign`. - * `signatures` - this should be an array of arrays of bytes, each of them representing an individual signature - * of `signed_message` produced by `sign_challenge` using the private key for the corresponding input destination - * of the transaction. The number of signatures must be equal to the number of inputs in the transaction. + * Note: the nonce is only needed before the orders V1 fork activation. After the fork the nonce is + * ignored and any value can be passed for the parameter. */ -export function encode_signed_transaction_intent(signed_message: Uint8Array, signatures: any): Uint8Array; +export function encode_input_for_conclude_order(order_id: string, nonce: bigint, current_block_height: bigint, network: Network): Uint8Array; + /** - * Verify a signed transaction intent. + * Given an order id and an amount in the order's ask currency, create an input that fills the order. * - * Parameters: - * `expected_signed_message` - the message that is supposed to be signed; this must have been - * produced by `make_transaction_intent_message_to_sign`. - * `encoded_signed_intent` - the signed transaction intent produced by `encode_signed_transaction_intent`. - * `input_destinations` - an array of addresses (strings), corresponding to the transaction's input destinations - * (note that this function treats "pub key" and "pub key hash" addresses interchangeably, so it's ok to pass - * one instead of the other). - * `network` - the network being used (needed to decode the addresses). + * Note: + * 1) The nonce is only needed before the orders V1 fork activation. After the fork the nonce is + * ignored and any value can be passed for the parameter. + * 2) FillOrder inputs should not be signed, i.e. use `encode_witness_no_signature` for the inputs + * instead of `encode_witness`). + * Note that in orders v0 FillOrder inputs can technically have a signature, it's just not checked. + * But in orders V1 we actually require that those inputs don't have signatures. + * Also, in orders V1 the provided destination is always ignored. + */ +export function encode_input_for_fill_order(order_id: string, fill_amount: Amount, destination: string, nonce: bigint, current_block_height: bigint, network: Network): Uint8Array; + +/** + * Given an order id create an input that freezes the order. + * + * Note: order freezing is available only after the orders V1 fork activation. */ -export function verify_transaction_intent(expected_signed_message: Uint8Array, encoded_signed_intent: Uint8Array, input_destinations: string[], network: Network): void; +export function encode_input_for_freeze_order(order_id: string, current_block_height: bigint, network: Network): Uint8Array; + /** - * Given a destination address, an amount and a network type (mainnet, testnet, etc), this function - * creates an output of type Transfer, and returns it as bytes. + * Given a token_id, is token unfreezable and nonce return an encoded freeze token input */ -export function encode_output_transfer(amount: Amount, address: string, network: Network): Uint8Array; +export function encode_input_for_freeze_token(token_id: string, is_token_unfreezable: TokenUnfreezable, nonce: bigint, network: Network): Uint8Array; + /** - * Given a destination address, an amount, token ID (in address form) and a network type (mainnet, testnet, etc), this function - * creates an output of type Transfer for tokens, and returns it as bytes. + * Given a token_id and nonce return an encoded lock_token_supply input */ -export function encode_output_token_transfer(amount: Amount, address: string, token_id: string, network: Network): Uint8Array; +export function encode_input_for_lock_token_supply(token_id: string, nonce: bigint, network: Network): Uint8Array; + /** - * Given the current block height and a network type (mainnet, testnet, etc), - * this function returns the number of blocks, after which a pool that decommissioned, - * will have its funds unlocked and available for spending. - * The current block height information is used in case a network upgrade changed the value. + * Given a token_id, an amount of tokens to mint and nonce return an encoded mint tokens input */ -export function staking_pool_spend_maturity_block_count(current_block_height: bigint, network: Network): bigint; +export function encode_input_for_mint_tokens(token_id: string, amount: Amount, nonce: bigint, network: Network): Uint8Array; + +/** + * Given a token_id and nonce return an encoded unfreeze token input + */ +export function encode_input_for_unfreeze_token(token_id: string, nonce: bigint, network: Network): Uint8Array; + +/** + * Given a token_id and nonce return an encoded unmint tokens input + */ +export function encode_input_for_unmint_tokens(token_id: string, nonce: bigint, network: Network): Uint8Array; + +/** + * Given an output source id as bytes, and an output index, together representing a utxo, + * this function returns the input that puts them together, as bytes. + */ +export function encode_input_for_utxo(outpoint_source_id: Uint8Array, output_index: number): Uint8Array; + +/** + * Given a delegation id, an amount and a network type (mainnet, testnet, etc), this function + * creates an input that withdraws from a delegation. + * A nonce is needed because this spends from an account. The nonce must be in sequence for everything in that account. + */ +export function encode_input_for_withdraw_from_delegation(delegation_id: string, amount: Amount, nonce: bigint, network: Network): Uint8Array; + /** * Given a number of blocks, this function returns the output timelock * which is used in locked outputs to lock an output for a given number of blocks * since that output's transaction is included the blockchain */ export function encode_lock_for_block_count(block_count: bigint): Uint8Array; + /** * Given a number of clock seconds, this function returns the output timelock * which is used in locked outputs to lock an output for a given number of seconds * since that output's transaction is included in the blockchain */ export function encode_lock_for_seconds(total_seconds: bigint): Uint8Array; + +/** + * Given a block height, this function returns the output timelock which is used in + * locked outputs to lock an output until that block height is reached. + */ +export function encode_lock_until_height(block_height: bigint): Uint8Array; + /** * Given a timestamp represented by as unix timestamp, i.e., number of seconds since unix epoch, * this function returns the output timelock which is used in locked outputs to lock an output * until the given timestamp */ export function encode_lock_until_time(timestamp_since_epoch_in_seconds: bigint): Uint8Array; + /** - * Given a block height, this function returns the output timelock which is used in - * locked outputs to lock an output until that block height is reached. - */ -export function encode_lock_until_height(block_height: bigint): Uint8Array; -/** - * Given a valid receiving address, and a locking rule as bytes (available in this file), - * and a network type (mainnet, testnet, etc), this function creates an output of type - * LockThenTransfer with the parameters provided. + * Given an arbitrary number of public keys as bytes, number of minimum required signatures, and a network type, this function returns + * the multisig challenge, as bytes. */ -export function encode_output_lock_then_transfer(amount: Amount, address: string, lock: Uint8Array, network: Network): Uint8Array; +export function encode_multisig_challenge(public_keys: Uint8Array, min_required_signatures: number, network: Network): Uint8Array; + /** - * Given a valid receiving address, token ID (in address form), a locking rule as bytes (available in this file), - * and a network type (mainnet, testnet, etc), this function creates an output of type - * LockThenTransfer with the parameters provided. + * A utxo can either come from a transaction or a block reward. + * Given a source id, whether from a block reward or transaction, this function + * takes a generic id with it, and returns serialized binary data of the id + * with the given source id. */ -export function encode_output_token_lock_then_transfer(amount: Amount, address: string, token_id: string, lock: Uint8Array, network: Network): Uint8Array; +export function encode_outpoint_source_id(id: Uint8Array, source: SourceId): Uint8Array; + /** * Given an amount, this function creates an output (as bytes) to burn a given amount of coins */ export function encode_output_coin_burn(amount: Amount): Uint8Array; -/** - * Given an amount, token ID (in address form) and network type (mainnet, testnet, etc), - * this function creates an output (as bytes) to burn a given amount of tokens - */ -export function encode_output_token_burn(amount: Amount, token_id: string, network: Network): Uint8Array; + /** * Given a pool id as string, an owner address and a network type (mainnet, testnet, etc), * this function returns an output (as bytes) to create a delegation to the given pool. * The owner address is the address that is authorized to withdraw from that delegation. */ export function encode_output_create_delegation(pool_id: string, owner_address: string, network: Network): Uint8Array; + +/** + * Given a pool id, staking data as bytes and the network type (mainnet, testnet, etc), + * this function returns an output that creates that staking pool. + * Note that the pool id is mandated to be taken from the hash of the first input. + * It is not arbitrary. + * + * Note: a UTXO of this kind is consumed when decommissioning a pool (provided that the pool + * never staked). + */ +export function encode_output_create_stake_pool(pool_id: string, pool_data: Uint8Array, network: Network): Uint8Array; + +/** + * Given data to be deposited in the blockchain, this function provides the output that deposits this data + */ +export function encode_output_data_deposit(data: Uint8Array): Uint8Array; + /** * Given a delegation id (as string, in address form), an amount and a network type (mainnet, testnet, etc), * this function returns an output (as bytes) that would delegate coins to be staked in the specified delegation id. */ export function encode_output_delegate_staking(amount: Amount, delegation_id: string, network: Network): Uint8Array; + /** - * This function returns the staking pool data needed to create a staking pool in an output as bytes, - * given its parameters and the network type (testnet, mainnet, etc). + * Given the parameters needed to create hash timelock contract, and a network type (mainnet, testnet, etc), + * this function creates an output. */ -export function encode_stake_pool_data(value: Amount, staker: string, vrf_public_key: string, decommission_key: string, margin_ratio_per_thousand: number, cost_per_block: Amount, network: Network): Uint8Array; +export function encode_output_htlc(amount: Amount, token_id: string | null | undefined, secret_hash: string, spend_address: string, refund_address: string, refund_timelock: Uint8Array, network: Network): Uint8Array; + /** - * Given a pool id, staking data as bytes and the network type (mainnet, testnet, etc), - * this function returns an output that creates that staking pool. - * Note that the pool id is mandated to be taken from the hash of the first input. - * It is not arbitrary. + * Given the parameters needed to issue a fungible token, and a network type (mainnet, testnet, etc), + * this function creates an output that issues that token. */ -export function encode_output_create_stake_pool(pool_id: string, pool_data: Uint8Array, network: Network): Uint8Array; +export function encode_output_issue_fungible_token(authority: string, token_ticker: string, metadata_uri: string, number_of_decimals: number, total_supply: TotalSupply, supply_amount: Amount | null | undefined, is_token_freezable: FreezableToken, _current_block_height: bigint, network: Network): Uint8Array; + /** - * Returns the fee that needs to be paid by a transaction for issuing a new fungible token + * Given the parameters needed to issue an NFT, and a network type (mainnet, testnet, etc), + * this function creates an output that issues that NFT. */ -export function fungible_token_issuance_fee(_current_block_height: bigint, network: Network): Amount; +export function encode_output_issue_nft(token_id: string, authority: string, name: string, ticker: string, description: string, media_hash: Uint8Array, creator: Uint8Array | null | undefined, media_uri: string | null | undefined, icon_uri: string | null | undefined, additional_metadata_uri: string | null | undefined, _current_block_height: bigint, network: Network): Uint8Array; + /** - * Given the current block height and a network type (mainnet, testnet, etc), - * this will return the fee that needs to be paid by a transaction for issuing a new NFT - * The current block height information is used in case a network upgrade changed the value. + * Given a valid receiving address, and a locking rule as bytes (available in this file), + * and a network type (mainnet, testnet, etc), this function creates an output of type + * LockThenTransfer with the parameters provided. */ -export function nft_issuance_fee(current_block_height: bigint, network: Network): Amount; +export function encode_output_lock_then_transfer(amount: Amount, address: string, lock: Uint8Array, network: Network): Uint8Array; + /** - * Given the current block height and a network type (mainnet, testnet, etc), - * this will return the fee that needs to be paid by a transaction for changing the total supply of a token - * by either minting or unminting tokens - * The current block height information is used in case a network upgrade changed the value. + * Given a pool id and a staker address, this function returns an output that is emitted + * when producing a block via that pool. + * + * Note: a UTXO of this kind is consumed when decommissioning a pool (provided that the pool + * has staked at least once). */ -export function token_supply_change_fee(current_block_height: bigint, network: Network): Amount; +export function encode_output_produce_block_from_stake(pool_id: string, staker: string, network: Network): Uint8Array; + /** - * Given the current block height and a network type (mainnet, testnet, etc), - * this will return the fee that needs to be paid by a transaction for freezing/unfreezing a token - * The current block height information is used in case a network upgrade changed the value. + * Given an amount, token ID (in address form) and network type (mainnet, testnet, etc), + * this function creates an output (as bytes) to burn a given amount of tokens */ -export function token_freeze_fee(current_block_height: bigint, network: Network): Amount; +export function encode_output_token_burn(amount: Amount, token_id: string, network: Network): Uint8Array; + /** - * Given the current block height and a network type (mainnet, testnet, etc), - * this will return the fee that needs to be paid by a transaction for changing the authority of a token - * The current block height information is used in case a network upgrade changed the value. + * Given a valid receiving address, token ID (in address form), a locking rule as bytes (available in this file), + * and a network type (mainnet, testnet, etc), this function creates an output of type + * LockThenTransfer with the parameters provided. */ -export function token_change_authority_fee(current_block_height: bigint, network: Network): Amount; +export function encode_output_token_lock_then_transfer(amount: Amount, address: string, token_id: string, lock: Uint8Array, network: Network): Uint8Array; + /** - * Given the parameters needed to issue a fungible token, and a network type (mainnet, testnet, etc), - * this function creates an output that issues that token. + * Given a destination address, an amount, token ID (in address form) and a network type (mainnet, testnet, etc), this function + * creates an output of type Transfer for tokens, and returns it as bytes. */ -export function encode_output_issue_fungible_token(authority: string, token_ticker: string, metadata_uri: string, number_of_decimals: number, total_supply: TotalSupply, supply_amount: Amount | null | undefined, is_token_freezable: FreezableToken, _current_block_height: bigint, network: Network): Uint8Array; +export function encode_output_token_transfer(amount: Amount, address: string, token_id: string, network: Network): Uint8Array; + /** - * Returns the Fungible/NFT Token ID for the given inputs of a transaction + * Given a destination address, an amount and a network type (mainnet, testnet, etc), this function + * creates an output of type Transfer, and returns it as bytes. */ -export function get_token_id(inputs: Uint8Array, network: Network): string; +export function encode_output_transfer(amount: Amount, address: string, network: Network): Uint8Array; + /** - * Given the parameters needed to issue an NFT, and a network type (mainnet, testnet, etc), - * this function creates an output that issues that NFT. + * Return a PartiallySignedTransaction object as bytes. + * + * `transaction` is an encoded `Transaction` (which can be produced via `encode_transaction`). + * + * `signatures`, `input_utxos`, `input_destinations` and `htlc_secrets` are encoded lists of + * optional objects of the corresponding type. To produce such a list, iterate over your + * original list of optional objects and then: + * 1) emit byte 0 if the current object is null; + * 2) otherwise emit byte 1 followed by the object in its encoded form. + * + * Each individual object in each of the lists corresponds to the transaction input with the same + * index and its meaning is as follows: + * 1) `signatures` - the signature for the input; + * 2) `input_utxos`- the utxo for the input (if it's utxo-based); + * 3) `input_destinations` - the destination (address) corresponding to the input; this determines + * the key(s) with which the input has to be signed. Note that for utxo-based inputs the + * corresponding destination can usually be extracted from the utxo itself (the exception + * being the `ProduceBlockFromStake` utxo, which doesn't contain the pool's decommission key). + * However, PartiallySignedTransaction requires that *all* input destinations are provided + * explicitly anyway. + * 4) `htlc_secrets` - if the input is an HTLC one and if the transaction is spending the HTLC, + * this should be the HTLC secret. Otherwise it should be null. + * + * The number of items in each list must be equal to the number of transaction inputs. + * + * `additional_info` has the same meaning as in `encode_witness`. */ -export function encode_output_issue_nft(token_id: string, authority: string, name: string, ticker: string, description: string, media_hash: Uint8Array, creator: Uint8Array | null | undefined, media_uri: string | null | undefined, icon_uri: string | null | undefined, additional_metadata_uri: string | null | undefined, _current_block_height: bigint, network: Network): Uint8Array; +export function encode_partially_signed_transaction(transaction: Uint8Array, signatures: Uint8Array, input_utxos: Uint8Array, input_destinations: Uint8Array, htlc_secrets: Uint8Array, additional_info: TxAdditionalInfo, network: Network): Uint8Array; + /** - * Given data to be deposited in the blockchain, this function provides the output that deposits this data + * Given an unsigned transaction and signatures, this function returns a SignedTransaction object as bytes. */ -export function encode_output_data_deposit(data: Uint8Array): Uint8Array; +export function encode_signed_transaction(transaction: Uint8Array, signatures: Uint8Array): Uint8Array; + /** - * Returns the fee that needs to be paid by a transaction for issuing a data deposit + * Return a `SignedTransactionIntent` object as bytes given the message and encoded signatures. + * + * Note: to produce a valid signed intent one is expected to sign the corresponding message by private keys + * corresponding to each input of the transaction. + * + * Parameters: + * `signed_message` - this must have been produced by `make_transaction_intent_message_to_sign`. + * `signatures` - this should be an array of Uint8Array, each of them representing an individual signature + * of `signed_message` produced by `sign_challenge` using the private key for the corresponding input destination + * of the transaction. The number of signatures must be equal to the number of inputs in the transaction. */ -export function data_deposit_fee(current_block_height: bigint, network: Network): Amount; +export function encode_signed_transaction_intent(signed_message: Uint8Array, signatures: Uint8Array[]): Uint8Array; + /** - * Given the parameters needed to create hash timelock contract, and a network type (mainnet, testnet, etc), - * this function creates an output. + * This function returns the staking pool data needed to create a staking pool in an output as bytes, + * given its parameters and the network type (testnet, mainnet, etc). */ -export function encode_output_htlc(amount: Amount, token_id: string | null | undefined, secret_hash: string, spend_address: string, refund_address: string, refund_timelock: Uint8Array, network: Network): Uint8Array; +export function encode_stake_pool_data(value: Amount, staker: string, vrf_public_key: string, decommission_key: string, margin_ratio_per_thousand: number, cost_per_block: Amount, network: Network): Uint8Array; + /** - * Given a signed transaction and input outpoint that spends an htlc utxo, extract a secret that is - * encoded in the corresponding input signature + * Given inputs as bytes, outputs as bytes, and flags settings, this function returns + * the transaction that contains them all, as bytes. */ -export function extract_htlc_secret(signed_tx_bytes: Uint8Array, strict_byte_size: boolean, htlc_outpoint_source_id: Uint8Array, htlc_output_index: number): Uint8Array; +export function encode_transaction(inputs: Uint8Array, outputs: Uint8Array, flags: bigint): Uint8Array; + /** - * Given an output source id as bytes, and an output index, together representing a utxo, - * this function returns the input that puts them together, as bytes. + * Sign the specified input of the transaction and encode the signature as InputWitness. + * + * `input_utxos` must be formed as follows: for each transaction input, emit byte 0 if it's a non-UTXO input, + * otherwise emit 1 followed by the corresponding transaction output encoded via the appropriate "encode_output_" + * function. + * + * `additional_info` must contain the following: + * 1) for each `ProduceBlockFromStake` input of the transaction, the pool info for the pool referenced by that input; + * 2) for each `FillOrder` and `ConcludeOrder` input of the transaction, the order info for the order referenced by + * that input. + * Note: + * - It doesn't matter which input witness is currently being encoded. E.g. even if you are encoding a witness + * for some UTXO-based input but another input of the same transaction is `FillOrder`, you have to include the order + * info when encoding the witness for the UTXO-based input too. + * - After a certain hard fork, the produced signature will "commit" to the provided additional info, i.e. the info + * will become a part of what is being signed. So, passing invalid additional info will result in an invalid signature + * (with one small caveat: for `FillOrder` we only commit to order's initial balances and not the current ones; + * so if you only have `FillOrder` inputs, you can technically pass bogus values for the current balances and + * the resulting signature will still be valid; though it's better to avoid doing this). + */ +export function encode_witness(sighashtype: SignatureHashType, private_key: Uint8Array, input_owner_destination: string, transaction: Uint8Array, input_utxos: Uint8Array, input_index: number, additional_info: TxAdditionalInfo, current_block_height: bigint, network: Network): Uint8Array; + +/** + * Sign the specified HTLC input of the transaction and encode the signature as InputWitness. + * + * This function must be used for HTLC refunding when the refund address is a multisig one. + * + * `key_index` parameter is an index of the public key in the multisig challenge corresponding to + * the specified private key. + * `input_witness` parameter can be either empty or a result of previous calls to this function. + * + * `input_utxos` and `additional_info` have the same format and requirements as in `encode_witness`. */ -export function encode_input_for_utxo(outpoint_source_id: Uint8Array, output_index: number): Uint8Array; +export function encode_witness_htlc_refund_multisig(sighashtype: SignatureHashType, private_key: Uint8Array, key_index: number, input_witness: Uint8Array, multisig_challenge: Uint8Array, transaction: Uint8Array, input_utxos: Uint8Array, input_index: number, additional_info: TxAdditionalInfo, current_block_height: bigint, network: Network): Uint8Array; + /** - * Given a delegation id, an amount and a network type (mainnet, testnet, etc), this function - * creates an input that withdraws from a delegation. - * A nonce is needed because this spends from an account. The nonce must be in sequence for everything in that account. + * Sign the specified HTLC input of the transaction and encode the signature as InputWitness. + * + * This function must be used for HTLC refunding when the refund address is a single-sig one. + * + * `input_utxos` and `additional_info` have the same format and requirements as in `encode_witness`. */ -export function encode_input_for_withdraw_from_delegation(delegation_id: string, amount: Amount, nonce: bigint, network: Network): Uint8Array; +export function encode_witness_htlc_refund_single_sig(sighashtype: SignatureHashType, private_key: Uint8Array, input_owner_destination: string, transaction: Uint8Array, input_utxos: Uint8Array, input_index: number, additional_info: TxAdditionalInfo, current_block_height: bigint, network: Network): Uint8Array; + +/** + * Sign the specified HTLC input of the transaction and encode the signature as InputWitness. + * + * This function must be used for HTLC spending. + * + * `input_utxos` and `additional_info` have the same format and requirements as in `encode_witness`. + */ +export function encode_witness_htlc_spend(sighashtype: SignatureHashType, private_key: Uint8Array, input_owner_destination: string, transaction: Uint8Array, input_utxos: Uint8Array, input_index: number, secret: Uint8Array, additional_info: TxAdditionalInfo, current_block_height: bigint, network: Network): Uint8Array; + +/** + * Encode an input witness of the variant that contains no signature. + */ +export function encode_witness_no_signature(): Uint8Array; + /** * Given the inputs, along each input's destination that can spend that input * (e.g. If we are spending a UTXO in input number 1 and it is owned by address mtc1xxxx, then it is mtc1xxxx in element number 2 in the vector/list. @@ -273,44 +499,46 @@ export function encode_input_for_withdraw_from_delegation(delegation_id: string, * and in the case of AccountCommand inputs which change a token it is the token's authority destination) * and the outputs, estimate the transaction size. * ScriptHash and ClassicMultisig destinations are not supported. + * Also, the function assumes that the input UTXOs are not HTLC. */ export function estimate_transaction_size(inputs: Uint8Array, input_utxos_destinations: string[], outputs: Uint8Array, network: Network): number; + /** - * Given inputs as bytes, outputs as bytes, and flags settings, this function returns - * the transaction that contains them all, as bytes. + * Return the extended public key from an extended private key */ -export function encode_transaction(inputs: Uint8Array, outputs: Uint8Array, flags: bigint): Uint8Array; +export function extended_public_key_from_extended_private_key(private_key: Uint8Array): Uint8Array; + /** - * Encode an input witness of the variant that contains no signature. + * Given a signed transaction and input outpoint that spends an htlc utxo, extract a secret that is + * encoded in the corresponding input signature */ -export function encode_witness_no_signature(): Uint8Array; +export function extract_htlc_secret(signed_tx: Uint8Array, strict_byte_size: boolean, htlc_outpoint_source_id: Uint8Array, htlc_output_index: number): Uint8Array; + /** - * Given a private key, inputs and an input number to sign, and the destination that owns that output (through the utxo), - * and a network type (mainnet, testnet, etc), this function returns a witness to be used in a signed transaction, as bytes. + * Returns the fee that needs to be paid by a transaction for issuing a new fungible token */ -export function encode_witness(sighashtype: SignatureHashType, private_key_bytes: Uint8Array, input_owner_destination: string, transaction_bytes: Uint8Array, inputs: Uint8Array, input_num: number, network: Network): Uint8Array; +export function fungible_token_issuance_fee(_current_block_height: bigint, network: Network): Amount; + /** - * Given a private key, inputs and an input number to sign, and the destination that owns that output (through the utxo), - * and a network type (mainnet, testnet, etc), and an htlc secret this function returns a witness to be used in a signed transaction, as bytes. + * Returns the Delegation ID for the given inputs of a transaction */ -export function encode_witness_htlc_secret(sighashtype: SignatureHashType, private_key_bytes: Uint8Array, input_owner_destination: string, transaction_bytes: Uint8Array, inputs: Uint8Array, input_num: number, secret: Uint8Array, network: Network): Uint8Array; +export function get_delegation_id(inputs: Uint8Array, network: Network): string; + /** - * Given an arbitrary number of public keys as bytes, number of minimum required signatures, and a network type, this function returns - * the multisig challenge, as bytes. + * Returns the Order ID for the given inputs of a transaction */ -export function encode_multisig_challenge(public_keys_bytes: Uint8Array, min_required_signatures: number, network: Network): Uint8Array; +export function get_order_id(inputs: Uint8Array, network: Network): string; + /** - * Given a private key, inputs and an input number to sign, and multisig challenge, - * and a network type (mainnet, testnet, etc), this function returns a witness to be used in a signed transaction, as bytes. - * - * `key_index` parameter is an index of a public key in the challenge, against which is the signature produces from private key is to be verified. - * `input_witness` parameter can be either empty or a result of previous calls to this function. + * Returns the Pool ID for the given inputs of a transaction */ -export function encode_witness_htlc_multisig(sighashtype: SignatureHashType, private_key_bytes: Uint8Array, key_index: number, input_witness: Uint8Array, multisig_challenge: Uint8Array, transaction_bytes: Uint8Array, utxos: Uint8Array, input_num: number, network: Network): Uint8Array; +export function get_pool_id(inputs: Uint8Array, network: Network): string; + /** - * Given an unsigned transaction and signatures, this function returns a SignedTransaction object as bytes. + * Returns the Fungible/NFT Token ID for the given inputs of a transaction */ -export function encode_signed_transaction(transaction_bytes: Uint8Array, signatures: Uint8Array): Uint8Array; +export function get_token_id(inputs: Uint8Array, current_block_height: bigint, network: Network): string; + /** * Given a `Transaction` encoded in bytes (not a signed transaction, but a signed transaction is tolerated by ignoring the extra bytes, by choice) * this function will return the transaction id. @@ -322,121 +550,155 @@ export function encode_signed_transaction(transaction_bytes: Uint8Array, signatu * since the signatures are appended at the end of the `Transaction` object as a vector to create a `SignedTransaction`. * It is recommended to use a strict `Transaction` size and set the second parameter to `true`. */ -export function get_transaction_id(transaction_bytes: Uint8Array, strict_byte_size: boolean): string; +export function get_transaction_id(transaction: Uint8Array, strict_byte_size: boolean): string; + /** - * Calculate the "effective balance" of a pool, given the total pool balance and pledge by the pool owner/staker. - * The effective balance is how the influence of a pool is calculated due to its balance. + * Verify a witness produced by one of the `encode_witness` functions. + * + * `input_owner_destination` must be specified if `witness` actually contains a signature + * (i.e. it's not InputWitness::NoSignature) and the input is not an HTLC one. Otherwise it must + * be null. */ -export function effective_pool_balance(network: Network, pledge_amount: Amount, pool_balance: Amount): Amount; +export function internal_verify_witness(sighashtype: SignatureHashType, input_owner_destination: string | null | undefined, witness: Uint8Array, transaction: Uint8Array, input_utxos: Uint8Array, input_index: number, additional_info: TxAdditionalInfo, current_block_height: bigint, network: Network): void; + /** - * Given a token_id, an amount of tokens to mint and nonce return an encoded mint tokens input + * From an extended private key create a change private key for a given key index + * derivation path: current_derivation_path/1/key_index */ -export function encode_input_for_mint_tokens(token_id: string, amount: Amount, nonce: bigint, network: Network): Uint8Array; +export function make_change_address(private_key: Uint8Array, key_index: number): Uint8Array; + /** - * Given a token_id and nonce return an encoded unmint tokens input + * From an extended public key create a change public key for a given key index + * derivation path: current_derivation_path/1/key_index */ -export function encode_input_for_unmint_tokens(token_id: string, nonce: bigint, network: Network): Uint8Array; +export function make_change_address_public_key(extended_public_key: Uint8Array, key_index: number): Uint8Array; + /** - * Given a token_id and nonce return an encoded lock_token_supply input + * Create the default account's extended private key for a given mnemonic + * derivation path: 44'/mintlayer_coin_type'/0' */ -export function encode_input_for_lock_token_supply(token_id: string, nonce: bigint, network: Network): Uint8Array; +export function make_default_account_privkey(mnemonic: string, network: Network): Uint8Array; + /** - * Given a token_id, is token unfreezable and nonce return an encoded freeze token input + * Generates a new, random private key from entropy */ -export function encode_input_for_freeze_token(token_id: string, is_token_unfreezable: TokenUnfreezable, nonce: bigint, network: Network): Uint8Array; +export function make_private_key(): Uint8Array; + /** - * Given a token_id and nonce return an encoded unfreeze token input + * From an extended private key create a receiving private key for a given key index + * derivation path: current_derivation_path/0/key_index */ -export function encode_input_for_unfreeze_token(token_id: string, nonce: bigint, network: Network): Uint8Array; +export function make_receiving_address(private_key: Uint8Array, key_index: number): Uint8Array; + /** - * Given a token_id, new authority destination and nonce return an encoded change token authority input + * From an extended public key create a receiving public key for a given key index + * derivation path: current_derivation_path/0/key_index */ -export function encode_input_for_change_token_authority(token_id: string, new_authority: string, nonce: bigint, network: Network): Uint8Array; +export function make_receiving_address_public_key(extended_public_key: Uint8Array, key_index: number): Uint8Array; + /** - * Given a token_id, new metadata uri and nonce return an encoded change token metadata uri input + * Return the message that has to be signed to produce a signed transaction intent. */ -export function encode_input_for_change_token_metadata_uri(token_id: string, new_metadata_uri: string, nonce: bigint, network: Network): Uint8Array; +export function make_transaction_intent_message_to_sign(intent: string, transaction_id: string): Uint8Array; + /** - * Given ask and give amounts and a conclude key create output that creates an order. - * - * 'ask_token_id': the parameter represents a Token if it's Some and coins otherwise. - * 'give_token_id': the parameter represents a Token if it's Some and coins otherwise. + * Produce a multisig address given a multisig challenge. */ -export function encode_create_order_output(ask_amount: Amount, ask_token_id: string | null | undefined, give_amount: Amount, give_token_id: string | null | undefined, conclude_address: string, network: Network): Uint8Array; +export function multisig_challenge_to_address(multisig_challenge: Uint8Array, network: Network): string; + /** - * Given an amount to fill an order (which is described in terms of ask currency) and a destination - * for result outputs create an input that fills the order. + * Given the current block height and a network type (mainnet, testnet, etc), + * this will return the fee that needs to be paid by a transaction for issuing a new NFT + * The current block height information is used in case a network upgrade changed the value. */ -export function encode_input_for_fill_order(order_id: string, fill_amount: Amount, destination: string, nonce: bigint, network: Network): Uint8Array; +export function nft_issuance_fee(current_block_height: bigint, network: Network): Amount; + /** - * Given an order id create an input that concludes the order. + * Given a public key (as bytes) and a network type (mainnet, testnet, etc), + * return the address public key hash from that public key as an address */ -export function encode_input_for_conclude_order(order_id: string, nonce: bigint, network: Network): Uint8Array; +export function pubkey_to_pubkeyhash_address(public_key: Uint8Array, network: Network): string; + /** - * Indicates whether a token can be frozen + * Given a private key, as bytes, return the bytes of the corresponding public key */ -export enum FreezableToken { - No = 0, - Yes = 1, -} +export function public_key_from_private_key(private_key: Uint8Array): Uint8Array; + /** - * The network, for which an operation to be done. Mainnet, testnet, etc. + * Given a message and a private key, create and sign a challenge with the given private key. + * This kind of signature is to be used when signing challenges. */ -export enum Network { - Mainnet = 0, - Testnet = 1, - Regtest = 2, - Signet = 3, -} +export function sign_challenge(private_key: Uint8Array, message: Uint8Array): Uint8Array; + /** - * The part of the transaction that will be committed in the signature. Similar to bitcoin's sighash. + * Given a message and a private key, sign the message with the given private key + * This kind of signature is to be used when signing spend requests, such as transaction + * input witness. */ -export enum SignatureHashType { - ALL = 0, - NONE = 1, - SINGLE = 2, - ANYONECANPAY = 3, -} +export function sign_message_for_spending(private_key: Uint8Array, message: Uint8Array): Uint8Array; + /** - * A utxo can either come from a transaction or a block reward. This enum signifies that. + * Given the current block height and a network type (mainnet, testnet, etc), + * this function returns the number of blocks, after which a pool that decommissioned, + * will have its funds unlocked and available for spending. + * The current block height information is used in case a network upgrade changed the value. */ -export enum SourceId { - Transaction = 0, - BlockReward = 1, -} +export function staking_pool_spend_maturity_block_count(current_block_height: bigint, network: Network): bigint; + /** - * Indicates whether a token can be unfrozen once frozen + * Given the current block height and a network type (mainnet, testnet, etc), + * this will return the fee that needs to be paid by a transaction for changing the authority of a token + * The current block height information is used in case a network upgrade changed the value. */ -export enum TokenUnfreezable { - No = 0, - Yes = 1, -} +export function token_change_authority_fee(current_block_height: bigint, network: Network): Amount; + /** - * The token supply of a specific token, set on issuance + * Given the current block height and a network type (mainnet, testnet, etc), + * this will return the fee that needs to be paid by a transaction for freezing/unfreezing a token + * The current block height information is used in case a network upgrade changed the value. */ -export enum TotalSupply { - /** - * Can be issued with no limit, but then can be locked to have a fixed supply. - */ - Lockable = 0, - /** - * Unlimited supply, no limits except for numeric limits due to u128 - */ - Unlimited = 1, - /** - * On issuance, the total number of coins is fixed - */ - Fixed = 2, -} +export function token_freeze_fee(current_block_height: bigint, network: Network): Amount; + /** - * Amount type abstraction. The amount type is stored in a string - * since JavaScript number type cannot fit 128-bit integers. - * The amount is given as an integer in units of "atoms". - * Atoms are the smallest, indivisible amount of a coin or token. + * Given the current block height and a network type (mainnet, testnet, etc), + * this will return the fee that needs to be paid by a transaction for changing the total supply of a token + * by either minting or unminting tokens + * The current block height information is used in case a network upgrade changed the value. */ -export class Amount { - private constructor(); - free(): void; - static from_atoms(atoms: string): Amount; - atoms(): string; -} +export function token_supply_change_fee(current_block_height: bigint, network: Network): Amount; + +/** + * Given a signed challenge, an address and a message, verify that + * the signature is produced by signing the message with the private key + * that derived the given public key. + * This function is used for verifying messages-related challenges. + * + * Note: for signatures that were created by `sign_challenge`, the provided address must be + * a 'pubkeyhash' address. + * + * Note: currently this function never returns `false` - it either returns `true` or fails with an error. + */ +export function verify_challenge(address: string, network: Network, signed_challenge: Uint8Array, message: Uint8Array): boolean; + +/** + * Given a digital signature, a public key and a message. Verify that + * the signature is produced by signing the message with the private key + * that derived the given public key. + * Note that this function is used for verifying messages related to spending, + * such as transaction input witness. + */ +export function verify_signature_for_spending(public_key: Uint8Array, signature: Uint8Array, message: Uint8Array): boolean; + +/** + * Verify a signed transaction intent. + * + * Parameters: + * `expected_signed_message` - the message that is supposed to be signed; this must have been + * produced by `make_transaction_intent_message_to_sign`. + * `encoded_signed_intent` - the signed transaction intent produced by `encode_signed_transaction_intent`. + * `input_destinations` - an array of addresses (strings), corresponding to the transaction's input destinations + * (note that this function treats "pub key" and "pub key hash" addresses interchangeably, so it's ok to pass + * one instead of the other). + * `network` - the network being used (needed to decode the addresses). + */ +export function verify_transaction_intent(expected_signed_message: Uint8Array, encoded_signed_intent: Uint8Array, input_destinations: string[], network: Network): void; diff --git a/packages/sdk/tests/__mocks__/pkg-node/wasm_wrappers.js b/packages/sdk/tests/__mocks__/pkg-node/wasm_wrappers.js index d7eb5de..7cc4626 100644 --- a/packages/sdk/tests/__mocks__/pkg-node/wasm_wrappers.js +++ b/packages/sdk/tests/__mocks__/pkg-node/wasm_wrappers.js @@ -1,561 +1,546 @@ +/* @ts-self-types="./wasm_wrappers.d.ts" */ -let imports = {}; -imports['__wbindgen_placeholder__'] = module.exports; -let wasm; -const { TextDecoder, TextEncoder } = require(`util`); - -function addToExternrefTable0(obj) { - const idx = wasm.__externref_table_alloc(); - wasm.__wbindgen_export_2.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); - } -} - -let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); - -cachedTextDecoder.decode(); - -let cachedUint8ArrayMemory0 = null; - -function getUint8ArrayMemory0() { - if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) { - cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer); +/** + * Amount type abstraction. The amount type is stored in a string + * since JavaScript number type cannot fit 128-bit integers. + * The amount is given as an integer in units of "atoms". + * Atoms are the smallest, indivisible amount of a coin or token. + */ +class Amount { + static __wrap(ptr) { + ptr = ptr >>> 0; + const obj = Object.create(Amount.prototype); + obj.__wbg_ptr = ptr; + AmountFinalization.register(obj, obj.__wbg_ptr, obj); + return obj; } - return cachedUint8ArrayMemory0; -} - -function getStringFromWasm0(ptr, len) { - ptr = ptr >>> 0; - return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len)); -} - -function isLikeNone(x) { - return x === undefined || x === null; -} - -let WASM_VECTOR_LEN = 0; - -let cachedTextEncoder = new TextEncoder('utf-8'); - -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; + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + AmountFinalization.unregister(this); 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; + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_amount_free(ptr, 0); } - - if (offset !== len) { - if (offset !== 0) { - arg = arg.slice(offset); + /** + * @returns {string} + */ + atoms() { + let deferred1_0; + let deferred1_1; + try { + const ptr = this.__destroy_into_raw(); + const ret = wasm.amount_atoms(ptr); + deferred1_0 = ret[0]; + deferred1_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); } - 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; + /** + * @param {string} atoms + * @returns {Amount} + */ + static from_atoms(atoms) { + const ptr0 = passStringToWasm0(atoms, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.amount_from_atoms(ptr0, len0); + return Amount.__wrap(ret); + } } +if (Symbol.dispose) Amount.prototype[Symbol.dispose] = Amount.prototype.free; +exports.Amount = Amount; -let cachedDataViewMemory0 = null; +/** + * Indicates whether a token can be frozen + * @enum {0 | 1} + */ +const FreezableToken = Object.freeze({ + No: 0, "0": "No", + Yes: 1, "1": "Yes", +}); +exports.FreezableToken = FreezableToken; -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; -} +/** + * The network, for which an operation to be done. Mainnet, testnet, etc. + * @enum {0 | 1 | 2 | 3} + */ +const Network = Object.freeze({ + Mainnet: 0, "0": "Mainnet", + Testnet: 1, "1": "Testnet", + Regtest: 2, "2": "Regtest", + Signet: 3, "3": "Signet", +}); +exports.Network = Network; -function passArray8ToWasm0(arg, malloc) { - const ptr = malloc(arg.length * 1, 1) >>> 0; - getUint8ArrayMemory0().set(arg, ptr / 1); - WASM_VECTOR_LEN = arg.length; - return ptr; -} +/** + * The part of the transaction that will be committed in the signature. Similar to bitcoin's sighash. + * @enum {0 | 1 | 2 | 3} + */ +const SignatureHashType = Object.freeze({ + ALL: 0, "0": "ALL", + NONE: 1, "1": "NONE", + SINGLE: 2, "2": "SINGLE", + ANYONECANPAY: 3, "3": "ANYONECANPAY", +}); +exports.SignatureHashType = SignatureHashType; -function getArrayU8FromWasm0(ptr, len) { - ptr = ptr >>> 0; - return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len); -} /** - * A utxo can either come from a transaction or a block reward. - * Given a source id, whether from a block reward or transaction, this function - * takes a generic id with it, and returns serialized binary data of the id - * with the given source id. - * @param {Uint8Array} id - * @param {SourceId} source - * @returns {Uint8Array} + * A utxo can either come from a transaction or a block reward. This enum signifies that. + * @enum {0 | 1} */ -module.exports.encode_outpoint_source_id = function(id, source) { - const ptr0 = passArray8ToWasm0(id, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.encode_outpoint_source_id(ptr0, len0, source); - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; -}; +const SourceId = Object.freeze({ + Transaction: 0, "0": "Transaction", + BlockReward: 1, "1": "BlockReward", +}); +exports.SourceId = SourceId; /** - * Generates a new, random private key from entropy - * @returns {Uint8Array} + * Indicates whether a token can be unfrozen once frozen + * @enum {0 | 1} */ -module.exports.make_private_key = function() { - const ret = wasm.make_private_key(); - var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v1; -}; +const TokenUnfreezable = Object.freeze({ + No: 0, "0": "No", + Yes: 1, "1": "Yes", +}); +exports.TokenUnfreezable = TokenUnfreezable; -function takeFromExternrefTable0(idx) { - const value = wasm.__wbindgen_export_2.get(idx); - wasm.__externref_table_dealloc(idx); - return value; +/** + * The token supply of a specific token, set on issuance + * @enum {0 | 1 | 2} + */ +const TotalSupply = Object.freeze({ + /** + * Can be issued with no limit, but then can be locked to have a fixed supply. + */ + Lockable: 0, "0": "Lockable", + /** + * Unlimited supply, no limits except for numeric limits due to u128 + */ + Unlimited: 1, "1": "Unlimited", + /** + * On issuance, the total number of coins is fixed + */ + Fixed: 2, "2": "Fixed", +}); +exports.TotalSupply = TotalSupply; + +/** + * Returns the fee that needs to be paid by a transaction for issuing a data deposit + * @param {bigint} current_block_height + * @param {Network} network + * @returns {Amount} + */ +function data_deposit_fee(current_block_height, network) { + const ret = wasm.data_deposit_fee(current_block_height, network); + return Amount.__wrap(ret); } +exports.data_deposit_fee = data_deposit_fee; + /** - * Create the default account's extended private key for a given mnemonic - * derivation path: 44'/mintlayer_coin_type'/0' - * @param {string} mnemonic + * Decodes a partially signed transaction from its binary encoding into a JavaScript object. + * @param {Uint8Array} transaction * @param {Network} network - * @returns {Uint8Array} + * @returns {any} */ -module.exports.make_default_account_privkey = function(mnemonic, network) { - const ptr0 = passStringToWasm0(mnemonic, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +function decode_partially_signed_transaction_to_js(transaction, network) { + const ptr0 = passArray8ToWasm0(transaction, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ret = wasm.make_default_account_privkey(ptr0, len0, network); - if (ret[3]) { - throw takeFromExternrefTable0(ret[2]); + const ret = wasm.decode_partially_signed_transaction_to_js(ptr0, len0, network); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); } - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; -}; + return takeFromExternrefTable0(ret[0]); +} +exports.decode_partially_signed_transaction_to_js = decode_partially_signed_transaction_to_js; /** - * From an extended private key create a receiving private key for a given key index - * derivation path: current_derivation_path/0/key_index - * @param {Uint8Array} private_key_bytes - * @param {number} key_index - * @returns {Uint8Array} + * Decodes a signed transaction from its binary encoding into a JavaScript object. + * @param {Uint8Array} transaction + * @param {Network} network + * @returns {any} */ -module.exports.make_receiving_address = function(private_key_bytes, key_index) { - const ptr0 = passArray8ToWasm0(private_key_bytes, wasm.__wbindgen_malloc); +function decode_signed_transaction_to_js(transaction, network) { + const ptr0 = passArray8ToWasm0(transaction, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ret = wasm.make_receiving_address(ptr0, len0, key_index); - if (ret[3]) { - throw takeFromExternrefTable0(ret[2]); + const ret = wasm.decode_signed_transaction_to_js(ptr0, len0, network); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); } - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; -}; + return takeFromExternrefTable0(ret[0]); +} +exports.decode_signed_transaction_to_js = decode_signed_transaction_to_js; /** - * From an extended private key create a change private key for a given key index - * derivation path: current_derivation_path/1/key_index - * @param {Uint8Array} private_key_bytes - * @param {number} key_index - * @returns {Uint8Array} + * Decodes an unsigned transaction from its binary encoding into a JavaScript object. + * @param {Uint8Array} transaction + * @param {Network} network + * @returns {any} */ -module.exports.make_change_address = function(private_key_bytes, key_index) { - const ptr0 = passArray8ToWasm0(private_key_bytes, wasm.__wbindgen_malloc); +function decode_transaction_to_js(transaction, network) { + const ptr0 = passArray8ToWasm0(transaction, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ret = wasm.make_change_address(ptr0, len0, key_index); - if (ret[3]) { - throw takeFromExternrefTable0(ret[2]); + const ret = wasm.decode_transaction_to_js(ptr0, len0, network); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); } - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; -}; + return takeFromExternrefTable0(ret[0]); +} +exports.decode_transaction_to_js = decode_transaction_to_js; /** - * Given a public key (as bytes) and a network type (mainnet, testnet, etc), - * return the address public key hash from that public key as an address - * @param {Uint8Array} public_key_bytes + * Calculate the "effective balance" of a pool, given the total pool balance and pledge by the pool owner/staker. + * The effective balance is how the influence of a pool is calculated due to its balance. * @param {Network} network - * @returns {string} + * @param {Amount} pledge_amount + * @param {Amount} pool_balance + * @returns {Amount} */ -module.exports.pubkey_to_pubkeyhash_address = function(public_key_bytes, network) { - let deferred3_0; - let deferred3_1; - try { - const ptr0 = passArray8ToWasm0(public_key_bytes, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.pubkey_to_pubkeyhash_address(ptr0, len0, network); - var ptr2 = ret[0]; - var len2 = ret[1]; - if (ret[3]) { - ptr2 = 0; len2 = 0; - throw takeFromExternrefTable0(ret[2]); - } - deferred3_0 = ptr2; - deferred3_1 = len2; - return getStringFromWasm0(ptr2, len2); - } finally { - wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); +function effective_pool_balance(network, pledge_amount, pool_balance) { + _assertClass(pledge_amount, Amount); + var ptr0 = pledge_amount.__destroy_into_raw(); + _assertClass(pool_balance, Amount); + var ptr1 = pool_balance.__destroy_into_raw(); + const ret = wasm.effective_pool_balance(network, ptr0, ptr1); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); } -}; + return Amount.__wrap(ret[0]); +} +exports.effective_pool_balance = effective_pool_balance; /** - * Given a private key, as bytes, return the bytes of the corresponding public key - * @param {Uint8Array} private_key + * Given ask and give amounts and a conclude key create output that creates an order. + * + * 'ask_token_id': the parameter represents a Token if it's Some and coins otherwise. + * 'give_token_id': the parameter represents a Token if it's Some and coins otherwise. + * @param {Amount} ask_amount + * @param {string | null | undefined} ask_token_id + * @param {Amount} give_amount + * @param {string | null | undefined} give_token_id + * @param {string} conclude_address + * @param {Network} network * @returns {Uint8Array} */ -module.exports.public_key_from_private_key = function(private_key) { - const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.public_key_from_private_key(ptr0, len0); +function encode_create_order_output(ask_amount, ask_token_id, give_amount, give_token_id, conclude_address, network) { + _assertClass(ask_amount, Amount); + var ptr0 = ask_amount.__destroy_into_raw(); + var ptr1 = isLikeNone(ask_token_id) ? 0 : passStringToWasm0(ask_token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + var len1 = WASM_VECTOR_LEN; + _assertClass(give_amount, Amount); + var ptr2 = give_amount.__destroy_into_raw(); + var ptr3 = isLikeNone(give_token_id) ? 0 : passStringToWasm0(give_token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + var len3 = WASM_VECTOR_LEN; + const ptr4 = passStringToWasm0(conclude_address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len4 = WASM_VECTOR_LEN; + const ret = wasm.encode_create_order_output(ptr0, ptr1, len1, ptr2, ptr3, len3, ptr4, len4, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v6 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; -}; + return v6; +} +exports.encode_create_order_output = encode_create_order_output; /** - * Return the extended public key from an extended private key - * @param {Uint8Array} private_key_bytes + * Convert the specified string address into a Destination object, encoded as bytes. + * @param {string} address + * @param {Network} network * @returns {Uint8Array} */ -module.exports.extended_public_key_from_extended_private_key = function(private_key_bytes) { - const ptr0 = passArray8ToWasm0(private_key_bytes, wasm.__wbindgen_malloc); +function encode_destination(address, network) { + const ptr0 = passStringToWasm0(address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; - const ret = wasm.extended_public_key_from_extended_private_key(ptr0, len0); + const ret = wasm.encode_destination(ptr0, len0, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v2; -}; +} +exports.encode_destination = encode_destination; /** - * From an extended public key create a receiving public key for a given key index - * derivation path: current_derivation_path/0/key_index - * @param {Uint8Array} extended_public_key_bytes - * @param {number} key_index + * Given a token_id, new authority destination and nonce return an encoded change token authority input + * @param {string} token_id + * @param {string} new_authority + * @param {bigint} nonce + * @param {Network} network * @returns {Uint8Array} */ -module.exports.make_receiving_address_public_key = function(extended_public_key_bytes, key_index) { - const ptr0 = passArray8ToWasm0(extended_public_key_bytes, wasm.__wbindgen_malloc); +function encode_input_for_change_token_authority(token_id, new_authority, nonce, network) { + const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; - const ret = wasm.make_receiving_address_public_key(ptr0, len0, key_index); + const ptr1 = passStringToWasm0(new_authority, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.encode_input_for_change_token_authority(ptr0, len0, ptr1, len1, nonce, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; -}; + return v3; +} +exports.encode_input_for_change_token_authority = encode_input_for_change_token_authority; /** - * From an extended public key create a change public key for a given key index - * derivation path: current_derivation_path/1/key_index - * @param {Uint8Array} extended_public_key_bytes - * @param {number} key_index + * Given a token_id, new metadata uri and nonce return an encoded change token metadata uri input + * @param {string} token_id + * @param {string} new_metadata_uri + * @param {bigint} nonce + * @param {Network} network * @returns {Uint8Array} */ -module.exports.make_change_address_public_key = function(extended_public_key_bytes, key_index) { - const ptr0 = passArray8ToWasm0(extended_public_key_bytes, wasm.__wbindgen_malloc); +function encode_input_for_change_token_metadata_uri(token_id, new_metadata_uri, nonce, network) { + const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; - const ret = wasm.make_change_address_public_key(ptr0, len0, key_index); + const ptr1 = passStringToWasm0(new_metadata_uri, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.encode_input_for_change_token_metadata_uri(ptr0, len0, ptr1, len1, nonce, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; -}; + return v3; +} +exports.encode_input_for_change_token_metadata_uri = encode_input_for_change_token_metadata_uri; /** - * Given a message and a private key, sign the message with the given private key - * This kind of signature is to be used when signing spend requests, such as transaction - * input witness. - * @param {Uint8Array} private_key - * @param {Uint8Array} message + * Given an order id create an input that concludes the order. + * + * Note: the nonce is only needed before the orders V1 fork activation. After the fork the nonce is + * ignored and any value can be passed for the parameter. + * @param {string} order_id + * @param {bigint} nonce + * @param {bigint} current_block_height + * @param {Network} network * @returns {Uint8Array} */ -module.exports.sign_message_for_spending = function(private_key, message) { - const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); +function encode_input_for_conclude_order(order_id, nonce, current_block_height, network) { + const ptr0 = passStringToWasm0(order_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; - const ptr1 = passArray8ToWasm0(message, wasm.__wbindgen_malloc); - const len1 = WASM_VECTOR_LEN; - const ret = wasm.sign_message_for_spending(ptr0, len0, ptr1, len1); + const ret = wasm.encode_input_for_conclude_order(ptr0, len0, nonce, current_block_height, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v3; -}; - -/** - * Given a digital signature, a public key and a message. Verify that - * the signature is produced by signing the message with the private key - * that derived the given public key. - * Note that this function is used for verifying messages related to spending, - * such as transaction input witness. - * @param {Uint8Array} public_key - * @param {Uint8Array} signature - * @param {Uint8Array} message - * @returns {boolean} - */ -module.exports.verify_signature_for_spending = function(public_key, signature, message) { - const ptr0 = passArray8ToWasm0(public_key, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ptr1 = passArray8ToWasm0(signature, wasm.__wbindgen_malloc); - const len1 = WASM_VECTOR_LEN; - const ptr2 = passArray8ToWasm0(message, wasm.__wbindgen_malloc); - const len2 = WASM_VECTOR_LEN; - const ret = wasm.verify_signature_for_spending(ptr0, len0, ptr1, len1, ptr2, len2); - if (ret[2]) { - throw takeFromExternrefTable0(ret[1]); - } - return ret[0] !== 0; -}; + return v2; +} +exports.encode_input_for_conclude_order = encode_input_for_conclude_order; /** - * Given a message and a private key, create and sign a challenge with the given private key. - * This kind of signature is to be used when signing challenges. - * @param {Uint8Array} private_key - * @param {Uint8Array} message + * Given an order id and an amount in the order's ask currency, create an input that fills the order. + * + * Note: + * 1) The nonce is only needed before the orders V1 fork activation. After the fork the nonce is + * ignored and any value can be passed for the parameter. + * 2) FillOrder inputs should not be signed, i.e. use `encode_witness_no_signature` for the inputs + * instead of `encode_witness`). + * Note that in orders v0 FillOrder inputs can technically have a signature, it's just not checked. + * But in orders V1 we actually require that those inputs don't have signatures. + * Also, in orders V1 the provided destination is always ignored. + * @param {string} order_id + * @param {Amount} fill_amount + * @param {string} destination + * @param {bigint} nonce + * @param {bigint} current_block_height + * @param {Network} network * @returns {Uint8Array} */ -module.exports.sign_challenge = function(private_key, message) { - const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); +function encode_input_for_fill_order(order_id, fill_amount, destination, nonce, current_block_height, network) { + const ptr0 = passStringToWasm0(order_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; - const ptr1 = passArray8ToWasm0(message, wasm.__wbindgen_malloc); - const len1 = WASM_VECTOR_LEN; - const ret = wasm.sign_challenge(ptr0, len0, ptr1, len1); + _assertClass(fill_amount, Amount); + var ptr1 = fill_amount.__destroy_into_raw(); + const ptr2 = passStringToWasm0(destination, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len2 = WASM_VECTOR_LEN; + const ret = wasm.encode_input_for_fill_order(ptr0, len0, ptr1, ptr2, len2, nonce, current_block_height, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v4 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v3; -}; + return v4; +} +exports.encode_input_for_fill_order = encode_input_for_fill_order; /** - * Given a signed challenge, an address and a message, verify that - * the signature is produced by signing the message with the private key - * that derived the given public key. - * This function is used for verifying messages-related challenges. - * - * Note: for signatures that were created by `sign_challenge`, the provided address must be - * a 'pubkeyhash' address. + * Given an order id create an input that freezes the order. * - * Note: currently this function never returns `false` - it either returns `true` or fails with an error. - * @param {string} address + * Note: order freezing is available only after the orders V1 fork activation. + * @param {string} order_id + * @param {bigint} current_block_height * @param {Network} network - * @param {Uint8Array} signed_challenge - * @param {Uint8Array} message - * @returns {boolean} + * @returns {Uint8Array} */ -module.exports.verify_challenge = function(address, network, signed_challenge, message) { - const ptr0 = passStringToWasm0(address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +function encode_input_for_freeze_order(order_id, current_block_height, network) { + const ptr0 = passStringToWasm0(order_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; - const ptr1 = passArray8ToWasm0(signed_challenge, wasm.__wbindgen_malloc); - const len1 = WASM_VECTOR_LEN; - const ptr2 = passArray8ToWasm0(message, wasm.__wbindgen_malloc); - const len2 = WASM_VECTOR_LEN; - const ret = wasm.verify_challenge(ptr0, len0, network, ptr1, len1, ptr2, len2); - if (ret[2]) { - throw takeFromExternrefTable0(ret[1]); + const ret = wasm.encode_input_for_freeze_order(ptr0, len0, current_block_height, network); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); } - return ret[0] !== 0; -}; + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v2; +} +exports.encode_input_for_freeze_order = encode_input_for_freeze_order; /** - * Return the message that has to be signed to produce a signed transaction intent. - * @param {string} intent - * @param {string} transaction_id + * Given a token_id, is token unfreezable and nonce return an encoded freeze token input + * @param {string} token_id + * @param {TokenUnfreezable} is_token_unfreezable + * @param {bigint} nonce + * @param {Network} network * @returns {Uint8Array} */ -module.exports.make_transaction_intent_message_to_sign = function(intent, transaction_id) { - const ptr0 = passStringToWasm0(intent, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +function encode_input_for_freeze_token(token_id, is_token_unfreezable, nonce, network) { + const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; - const ptr1 = passStringToWasm0(transaction_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len1 = WASM_VECTOR_LEN; - const ret = wasm.make_transaction_intent_message_to_sign(ptr0, len0, ptr1, len1); + const ret = wasm.encode_input_for_freeze_token(ptr0, len0, is_token_unfreezable, nonce, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v3; -}; + return v2; +} +exports.encode_input_for_freeze_token = encode_input_for_freeze_token; /** - * Return a `SignedTransactionIntent` object as bytes given the message and encoded signatures. - * - * Note: to produce a valid signed intent one is expected to sign the corresponding message by private keys - * corresponding to each input of the transaction. - * - * Parameters: - * `signed_message` - this must have been produced by `make_transaction_intent_message_to_sign`. - * `signatures` - this should be an array of arrays of bytes, each of them representing an individual signature - * of `signed_message` produced by `sign_challenge` using the private key for the corresponding input destination - * of the transaction. The number of signatures must be equal to the number of inputs in the transaction. - * @param {Uint8Array} signed_message - * @param {any} signatures + * Given a token_id and nonce return an encoded lock_token_supply input + * @param {string} token_id + * @param {bigint} nonce + * @param {Network} network * @returns {Uint8Array} */ -module.exports.encode_signed_transaction_intent = function(signed_message, signatures) { - const ptr0 = passArray8ToWasm0(signed_message, wasm.__wbindgen_malloc); +function encode_input_for_lock_token_supply(token_id, nonce, network) { + const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; - const ret = wasm.encode_signed_transaction_intent(ptr0, len0, signatures); + const ret = wasm.encode_input_for_lock_token_supply(ptr0, len0, nonce, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v2; -}; +} +exports.encode_input_for_lock_token_supply = encode_input_for_lock_token_supply; -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; -} /** - * Verify a signed transaction intent. - * - * Parameters: - * `expected_signed_message` - the message that is supposed to be signed; this must have been - * produced by `make_transaction_intent_message_to_sign`. - * `encoded_signed_intent` - the signed transaction intent produced by `encode_signed_transaction_intent`. - * `input_destinations` - an array of addresses (strings), corresponding to the transaction's input destinations - * (note that this function treats "pub key" and "pub key hash" addresses interchangeably, so it's ok to pass - * one instead of the other). - * `network` - the network being used (needed to decode the addresses). - * @param {Uint8Array} expected_signed_message - * @param {Uint8Array} encoded_signed_intent - * @param {string[]} input_destinations + * Given a token_id, an amount of tokens to mint and nonce return an encoded mint tokens input + * @param {string} token_id + * @param {Amount} amount + * @param {bigint} nonce * @param {Network} network + * @returns {Uint8Array} */ -module.exports.verify_transaction_intent = function(expected_signed_message, encoded_signed_intent, input_destinations, network) { - const ptr0 = passArray8ToWasm0(expected_signed_message, wasm.__wbindgen_malloc); +function encode_input_for_mint_tokens(token_id, amount, nonce, network) { + const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; - const ptr1 = passArray8ToWasm0(encoded_signed_intent, wasm.__wbindgen_malloc); - const len1 = WASM_VECTOR_LEN; - const ptr2 = passArrayJsValueToWasm0(input_destinations, wasm.__wbindgen_malloc); - const len2 = WASM_VECTOR_LEN; - const ret = wasm.verify_transaction_intent(ptr0, len0, ptr1, len1, ptr2, len2, network); - if (ret[1]) { - throw takeFromExternrefTable0(ret[0]); - } -}; - -function _assertClass(instance, klass) { - if (!(instance instanceof klass)) { - throw new Error(`expected instance of ${klass.name}`); + _assertClass(amount, Amount); + var ptr1 = amount.__destroy_into_raw(); + const ret = wasm.encode_input_for_mint_tokens(ptr0, len0, ptr1, nonce, network); + 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; } +exports.encode_input_for_mint_tokens = encode_input_for_mint_tokens; + /** - * Given a destination address, an amount and a network type (mainnet, testnet, etc), this function - * creates an output of type Transfer, and returns it as bytes. - * @param {Amount} amount - * @param {string} address + * Given a token_id and nonce return an encoded unfreeze token input + * @param {string} token_id + * @param {bigint} nonce * @param {Network} network * @returns {Uint8Array} */ -module.exports.encode_output_transfer = function(amount, address, network) { - _assertClass(amount, Amount); - var ptr0 = amount.__destroy_into_raw(); - const ptr1 = passStringToWasm0(address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len1 = WASM_VECTOR_LEN; - const ret = wasm.encode_output_transfer(ptr0, ptr1, len1, network); +function encode_input_for_unfreeze_token(token_id, nonce, network) { + const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.encode_input_for_unfreeze_token(ptr0, len0, nonce, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v3; -}; + return v2; +} +exports.encode_input_for_unfreeze_token = encode_input_for_unfreeze_token; /** - * Given a destination address, an amount, token ID (in address form) and a network type (mainnet, testnet, etc), this function - * creates an output of type Transfer for tokens, and returns it as bytes. - * @param {Amount} amount - * @param {string} address + * Given a token_id and nonce return an encoded unmint tokens input * @param {string} token_id + * @param {bigint} nonce * @param {Network} network * @returns {Uint8Array} */ -module.exports.encode_output_token_transfer = function(amount, address, token_id, network) { - _assertClass(amount, Amount); - var ptr0 = amount.__destroy_into_raw(); - const ptr1 = passStringToWasm0(address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len1 = WASM_VECTOR_LEN; - const ptr2 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len2 = WASM_VECTOR_LEN; - const ret = wasm.encode_output_token_transfer(ptr0, ptr1, len1, ptr2, len2, network); +function encode_input_for_unmint_tokens(token_id, nonce, network) { + const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.encode_input_for_unmint_tokens(ptr0, len0, nonce, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v4 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v4; -}; + return v2; +} +exports.encode_input_for_unmint_tokens = encode_input_for_unmint_tokens; /** - * Given the current block height and a network type (mainnet, testnet, etc), - * this function returns the number of blocks, after which a pool that decommissioned, - * will have its funds unlocked and available for spending. - * The current block height information is used in case a network upgrade changed the value. - * @param {bigint} current_block_height + * Given an output source id as bytes, and an output index, together representing a utxo, + * this function returns the input that puts them together, as bytes. + * @param {Uint8Array} outpoint_source_id + * @param {number} output_index + * @returns {Uint8Array} + */ +function encode_input_for_utxo(outpoint_source_id, output_index) { + const ptr0 = passArray8ToWasm0(outpoint_source_id, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.encode_input_for_utxo(ptr0, len0, output_index); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v2; +} +exports.encode_input_for_utxo = encode_input_for_utxo; + +/** + * Given a delegation id, an amount and a network type (mainnet, testnet, etc), this function + * creates an input that withdraws from a delegation. + * A nonce is needed because this spends from an account. The nonce must be in sequence for everything in that account. + * @param {string} delegation_id + * @param {Amount} amount + * @param {bigint} nonce * @param {Network} network - * @returns {bigint} + * @returns {Uint8Array} */ -module.exports.staking_pool_spend_maturity_block_count = function(current_block_height, network) { - const ret = wasm.staking_pool_spend_maturity_block_count(current_block_height, network); - return BigInt.asUintN(64, ret); -}; +function encode_input_for_withdraw_from_delegation(delegation_id, amount, nonce, network) { + const ptr0 = passStringToWasm0(delegation_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + _assertClass(amount, Amount); + var ptr1 = amount.__destroy_into_raw(); + const ret = wasm.encode_input_for_withdraw_from_delegation(ptr0, len0, ptr1, nonce, network); + 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; +} +exports.encode_input_for_withdraw_from_delegation = encode_input_for_withdraw_from_delegation; /** * Given a number of blocks, this function returns the output timelock @@ -564,12 +549,13 @@ module.exports.staking_pool_spend_maturity_block_count = function(current_block_ * @param {bigint} block_count * @returns {Uint8Array} */ -module.exports.encode_lock_for_block_count = function(block_count) { +function encode_lock_for_block_count(block_count) { const ret = wasm.encode_lock_for_block_count(block_count); var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v1; -}; +} +exports.encode_lock_for_block_count = encode_lock_for_block_count; /** * Given a number of clock seconds, this function returns the output timelock @@ -578,26 +564,13 @@ module.exports.encode_lock_for_block_count = function(block_count) { * @param {bigint} total_seconds * @returns {Uint8Array} */ -module.exports.encode_lock_for_seconds = function(total_seconds) { +function encode_lock_for_seconds(total_seconds) { const ret = wasm.encode_lock_for_seconds(total_seconds); var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v1; -}; - -/** - * Given a timestamp represented by as unix timestamp, i.e., number of seconds since unix epoch, - * this function returns the output timelock which is used in locked outputs to lock an output - * until the given timestamp - * @param {bigint} timestamp_since_epoch_in_seconds - * @returns {Uint8Array} - */ -module.exports.encode_lock_until_time = function(timestamp_since_epoch_in_seconds) { - const ret = wasm.encode_lock_until_time(timestamp_since_epoch_in_seconds); - var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v1; -}; +} +exports.encode_lock_for_seconds = encode_lock_for_seconds; /** * Given a block height, this function returns the output timelock which is used in @@ -605,106 +578,86 @@ module.exports.encode_lock_until_time = function(timestamp_since_epoch_in_second * @param {bigint} block_height * @returns {Uint8Array} */ -module.exports.encode_lock_until_height = function(block_height) { +function encode_lock_until_height(block_height) { const ret = wasm.encode_lock_until_height(block_height); var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v1; -}; +} +exports.encode_lock_until_height = encode_lock_until_height; /** - * Given a valid receiving address, and a locking rule as bytes (available in this file), - * and a network type (mainnet, testnet, etc), this function creates an output of type - * LockThenTransfer with the parameters provided. - * @param {Amount} amount - * @param {string} address - * @param {Uint8Array} lock - * @param {Network} network + * Given a timestamp represented by as unix timestamp, i.e., number of seconds since unix epoch, + * this function returns the output timelock which is used in locked outputs to lock an output + * until the given timestamp + * @param {bigint} timestamp_since_epoch_in_seconds * @returns {Uint8Array} */ -module.exports.encode_output_lock_then_transfer = function(amount, address, lock, network) { - _assertClass(amount, Amount); - var ptr0 = amount.__destroy_into_raw(); - const ptr1 = passStringToWasm0(address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len1 = WASM_VECTOR_LEN; - const ptr2 = passArray8ToWasm0(lock, wasm.__wbindgen_malloc); - const len2 = WASM_VECTOR_LEN; - const ret = wasm.encode_output_lock_then_transfer(ptr0, ptr1, len1, ptr2, len2, network); - if (ret[3]) { - throw takeFromExternrefTable0(ret[2]); - } - var v4 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); +function encode_lock_until_time(timestamp_since_epoch_in_seconds) { + const ret = wasm.encode_lock_until_time(timestamp_since_epoch_in_seconds); + var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v4; -}; + return v1; +} +exports.encode_lock_until_time = encode_lock_until_time; /** - * Given a valid receiving address, token ID (in address form), a locking rule as bytes (available in this file), - * and a network type (mainnet, testnet, etc), this function creates an output of type - * LockThenTransfer with the parameters provided. - * @param {Amount} amount - * @param {string} address - * @param {string} token_id - * @param {Uint8Array} lock + * Given an arbitrary number of public keys as bytes, number of minimum required signatures, and a network type, this function returns + * the multisig challenge, as bytes. + * @param {Uint8Array} public_keys + * @param {number} min_required_signatures * @param {Network} network * @returns {Uint8Array} */ -module.exports.encode_output_token_lock_then_transfer = function(amount, address, token_id, lock, network) { - _assertClass(amount, Amount); - var ptr0 = amount.__destroy_into_raw(); - const ptr1 = passStringToWasm0(address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len1 = WASM_VECTOR_LEN; - const ptr2 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len2 = WASM_VECTOR_LEN; - const ptr3 = passArray8ToWasm0(lock, wasm.__wbindgen_malloc); - const len3 = WASM_VECTOR_LEN; - const ret = wasm.encode_output_token_lock_then_transfer(ptr0, ptr1, len1, ptr2, len2, ptr3, len3, network); +function encode_multisig_challenge(public_keys, min_required_signatures, network) { + const ptr0 = passArray8ToWasm0(public_keys, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.encode_multisig_challenge(ptr0, len0, min_required_signatures, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v5 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v5; -}; + return v2; +} +exports.encode_multisig_challenge = encode_multisig_challenge; /** - * Given an amount, this function creates an output (as bytes) to burn a given amount of coins - * @param {Amount} amount + * A utxo can either come from a transaction or a block reward. + * Given a source id, whether from a block reward or transaction, this function + * takes a generic id with it, and returns serialized binary data of the id + * with the given source id. + * @param {Uint8Array} id + * @param {SourceId} source * @returns {Uint8Array} */ -module.exports.encode_output_coin_burn = function(amount) { - _assertClass(amount, Amount); - var ptr0 = amount.__destroy_into_raw(); - const ret = wasm.encode_output_coin_burn(ptr0); - if (ret[3]) { - throw takeFromExternrefTable0(ret[2]); - } +function encode_outpoint_source_id(id, source) { + const ptr0 = passArray8ToWasm0(id, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.encode_outpoint_source_id(ptr0, len0, source); var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v2; -}; +} +exports.encode_outpoint_source_id = encode_outpoint_source_id; /** - * Given an amount, token ID (in address form) and network type (mainnet, testnet, etc), - * this function creates an output (as bytes) to burn a given amount of tokens + * Given an amount, this function creates an output (as bytes) to burn a given amount of coins * @param {Amount} amount - * @param {string} token_id - * @param {Network} network * @returns {Uint8Array} */ -module.exports.encode_output_token_burn = function(amount, token_id, network) { +function encode_output_coin_burn(amount) { _assertClass(amount, Amount); var ptr0 = amount.__destroy_into_raw(); - const ptr1 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len1 = WASM_VECTOR_LEN; - const ret = wasm.encode_output_token_burn(ptr0, ptr1, len1, network); + const ret = wasm.encode_output_coin_burn(ptr0); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v3; -}; + return v2; +} +exports.encode_output_coin_burn = encode_output_coin_burn; /** * Given a pool id as string, an owner address and a network type (mainnet, testnet, etc), @@ -715,7 +668,7 @@ module.exports.encode_output_token_burn = function(amount, token_id, network) { * @param {Network} network * @returns {Uint8Array} */ -module.exports.encode_output_create_delegation = function(pool_id, owner_address, network) { +function encode_output_create_delegation(pool_id, owner_address, network) { const ptr0 = passStringToWasm0(pool_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; const ptr1 = passStringToWasm0(owner_address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); @@ -727,149 +680,112 @@ module.exports.encode_output_create_delegation = function(pool_id, owner_address var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v3; -}; +} +exports.encode_output_create_delegation = encode_output_create_delegation; /** - * Given a delegation id (as string, in address form), an amount and a network type (mainnet, testnet, etc), - * this function returns an output (as bytes) that would delegate coins to be staked in the specified delegation id. - * @param {Amount} amount - * @param {string} delegation_id + * Given a pool id, staking data as bytes and the network type (mainnet, testnet, etc), + * this function returns an output that creates that staking pool. + * Note that the pool id is mandated to be taken from the hash of the first input. + * It is not arbitrary. + * + * Note: a UTXO of this kind is consumed when decommissioning a pool (provided that the pool + * never staked). + * @param {string} pool_id + * @param {Uint8Array} pool_data * @param {Network} network * @returns {Uint8Array} */ -module.exports.encode_output_delegate_staking = function(amount, delegation_id, network) { - _assertClass(amount, Amount); - var ptr0 = amount.__destroy_into_raw(); - const ptr1 = passStringToWasm0(delegation_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +function encode_output_create_stake_pool(pool_id, pool_data, network) { + const ptr0 = passStringToWasm0(pool_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(pool_data, wasm.__wbindgen_malloc); const len1 = WASM_VECTOR_LEN; - const ret = wasm.encode_output_delegate_staking(ptr0, ptr1, len1, network); + const ret = wasm.encode_output_create_stake_pool(ptr0, len0, ptr1, len1, network); 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; -}; +} +exports.encode_output_create_stake_pool = encode_output_create_stake_pool; /** - * This function returns the staking pool data needed to create a staking pool in an output as bytes, - * given its parameters and the network type (testnet, mainnet, etc). - * @param {Amount} value - * @param {string} staker - * @param {string} vrf_public_key - * @param {string} decommission_key - * @param {number} margin_ratio_per_thousand - * @param {Amount} cost_per_block - * @param {Network} network + * Given data to be deposited in the blockchain, this function provides the output that deposits this data + * @param {Uint8Array} data * @returns {Uint8Array} */ -module.exports.encode_stake_pool_data = function(value, staker, vrf_public_key, decommission_key, margin_ratio_per_thousand, cost_per_block, network) { - _assertClass(value, Amount); - var ptr0 = value.__destroy_into_raw(); - const ptr1 = passStringToWasm0(staker, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len1 = WASM_VECTOR_LEN; - const ptr2 = passStringToWasm0(vrf_public_key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len2 = WASM_VECTOR_LEN; - const ptr3 = passStringToWasm0(decommission_key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len3 = WASM_VECTOR_LEN; - _assertClass(cost_per_block, Amount); - var ptr4 = cost_per_block.__destroy_into_raw(); - const ret = wasm.encode_stake_pool_data(ptr0, ptr1, len1, ptr2, len2, ptr3, len3, margin_ratio_per_thousand, ptr4, network); +function encode_output_data_deposit(data) { + const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.encode_output_data_deposit(ptr0, len0); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v6 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v6; -}; + return v2; +} +exports.encode_output_data_deposit = encode_output_data_deposit; /** - * Given a pool id, staking data as bytes and the network type (mainnet, testnet, etc), - * this function returns an output that creates that staking pool. - * Note that the pool id is mandated to be taken from the hash of the first input. - * It is not arbitrary. - * @param {string} pool_id - * @param {Uint8Array} pool_data + * Given a delegation id (as string, in address form), an amount and a network type (mainnet, testnet, etc), + * this function returns an output (as bytes) that would delegate coins to be staked in the specified delegation id. + * @param {Amount} amount + * @param {string} delegation_id * @param {Network} network * @returns {Uint8Array} */ -module.exports.encode_output_create_stake_pool = function(pool_id, pool_data, network) { - const ptr0 = passStringToWasm0(pool_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - const ptr1 = passArray8ToWasm0(pool_data, wasm.__wbindgen_malloc); +function encode_output_delegate_staking(amount, delegation_id, network) { + _assertClass(amount, Amount); + var ptr0 = amount.__destroy_into_raw(); + const ptr1 = passStringToWasm0(delegation_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len1 = WASM_VECTOR_LEN; - const ret = wasm.encode_output_create_stake_pool(ptr0, len0, ptr1, len1, network); + const ret = wasm.encode_output_delegate_staking(ptr0, ptr1, len1, network); 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; -}; - -/** - * Returns the fee that needs to be paid by a transaction for issuing a new fungible token - * @param {bigint} _current_block_height - * @param {Network} network - * @returns {Amount} - */ -module.exports.fungible_token_issuance_fee = function(_current_block_height, network) { - const ret = wasm.fungible_token_issuance_fee(_current_block_height, network); - return Amount.__wrap(ret); -}; - -/** - * Given the current block height and a network type (mainnet, testnet, etc), - * this will return the fee that needs to be paid by a transaction for issuing a new NFT - * The current block height information is used in case a network upgrade changed the value. - * @param {bigint} current_block_height - * @param {Network} network - * @returns {Amount} - */ -module.exports.nft_issuance_fee = function(current_block_height, network) { - const ret = wasm.nft_issuance_fee(current_block_height, network); - return Amount.__wrap(ret); -}; - -/** - * Given the current block height and a network type (mainnet, testnet, etc), - * this will return the fee that needs to be paid by a transaction for changing the total supply of a token - * by either minting or unminting tokens - * The current block height information is used in case a network upgrade changed the value. - * @param {bigint} current_block_height - * @param {Network} network - * @returns {Amount} - */ -module.exports.token_supply_change_fee = function(current_block_height, network) { - const ret = wasm.token_supply_change_fee(current_block_height, network); - return Amount.__wrap(ret); -}; - -/** - * Given the current block height and a network type (mainnet, testnet, etc), - * this will return the fee that needs to be paid by a transaction for freezing/unfreezing a token - * The current block height information is used in case a network upgrade changed the value. - * @param {bigint} current_block_height - * @param {Network} network - * @returns {Amount} - */ -module.exports.token_freeze_fee = function(current_block_height, network) { - const ret = wasm.token_freeze_fee(current_block_height, network); - return Amount.__wrap(ret); -}; +} +exports.encode_output_delegate_staking = encode_output_delegate_staking; /** - * Given the current block height and a network type (mainnet, testnet, etc), - * this will return the fee that needs to be paid by a transaction for changing the authority of a token - * The current block height information is used in case a network upgrade changed the value. - * @param {bigint} current_block_height + * Given the parameters needed to create hash timelock contract, and a network type (mainnet, testnet, etc), + * this function creates an output. + * @param {Amount} amount + * @param {string | null | undefined} token_id + * @param {string} secret_hash + * @param {string} spend_address + * @param {string} refund_address + * @param {Uint8Array} refund_timelock * @param {Network} network - * @returns {Amount} + * @returns {Uint8Array} */ -module.exports.token_change_authority_fee = function(current_block_height, network) { - const ret = wasm.token_change_authority_fee(current_block_height, network); - return Amount.__wrap(ret); -}; +function encode_output_htlc(amount, token_id, secret_hash, spend_address, refund_address, refund_timelock, network) { + _assertClass(amount, Amount); + var ptr0 = amount.__destroy_into_raw(); + var ptr1 = isLikeNone(token_id) ? 0 : passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + var len1 = WASM_VECTOR_LEN; + const ptr2 = passStringToWasm0(secret_hash, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len2 = WASM_VECTOR_LEN; + const ptr3 = passStringToWasm0(spend_address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len3 = WASM_VECTOR_LEN; + const ptr4 = passStringToWasm0(refund_address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len4 = WASM_VECTOR_LEN; + const ptr5 = passArray8ToWasm0(refund_timelock, wasm.__wbindgen_malloc); + const len5 = WASM_VECTOR_LEN; + const ret = wasm.encode_output_htlc(ptr0, ptr1, len1, ptr2, len2, ptr3, len3, ptr4, len4, ptr5, len5, network); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v7 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v7; +} +exports.encode_output_htlc = encode_output_htlc; /** * Given the parameters needed to issue a fungible token, and a network type (mainnet, testnet, etc), @@ -885,7 +801,7 @@ module.exports.token_change_authority_fee = function(current_block_height, netwo * @param {Network} network * @returns {Uint8Array} */ -module.exports.encode_output_issue_fungible_token = function(authority, token_ticker, metadata_uri, number_of_decimals, total_supply, supply_amount, is_token_freezable, _current_block_height, network) { +function encode_output_issue_fungible_token(authority, token_ticker, metadata_uri, number_of_decimals, total_supply, supply_amount, is_token_freezable, _current_block_height, network) { const ptr0 = passStringToWasm0(authority, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; const ptr1 = passStringToWasm0(token_ticker, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); @@ -904,34 +820,8 @@ module.exports.encode_output_issue_fungible_token = function(authority, token_ti var v5 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v5; -}; - -/** - * Returns the Fungible/NFT Token ID for the given inputs of a transaction - * @param {Uint8Array} inputs - * @param {Network} network - * @returns {string} - */ -module.exports.get_token_id = function(inputs, network) { - let deferred3_0; - let deferred3_1; - try { - const ptr0 = passArray8ToWasm0(inputs, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.get_token_id(ptr0, len0, network); - var ptr2 = ret[0]; - var len2 = ret[1]; - if (ret[3]) { - ptr2 = 0; len2 = 0; - throw takeFromExternrefTable0(ret[2]); - } - deferred3_0 = ptr2; - deferred3_1 = len2; - return getStringFromWasm0(ptr2, len2); - } finally { - wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); - } -}; +} +exports.encode_output_issue_fungible_token = encode_output_issue_fungible_token; /** * Given the parameters needed to issue an NFT, and a network type (mainnet, testnet, etc), @@ -950,7 +840,7 @@ module.exports.get_token_id = function(inputs, network) { * @param {Network} network * @returns {Uint8Array} */ -module.exports.encode_output_issue_nft = function(token_id, authority, name, ticker, description, media_hash, creator, media_uri, icon_uri, additional_metadata_uri, _current_block_height, network) { +function encode_output_issue_nft(token_id, authority, name, ticker, description, media_hash, creator, media_uri, icon_uri, additional_metadata_uri, _current_block_height, network) { const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; const ptr1 = passStringToWasm0(authority, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); @@ -978,324 +868,229 @@ module.exports.encode_output_issue_nft = function(token_id, authority, name, tic var v11 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v11; -}; - -/** - * Given data to be deposited in the blockchain, this function provides the output that deposits this data - * @param {Uint8Array} data - * @returns {Uint8Array} - */ -module.exports.encode_output_data_deposit = function(data) { - const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.encode_output_data_deposit(ptr0, len0); - if (ret[3]) { - throw takeFromExternrefTable0(ret[2]); - } - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; -}; - -/** - * Returns the fee that needs to be paid by a transaction for issuing a data deposit - * @param {bigint} current_block_height - * @param {Network} network - * @returns {Amount} - */ -module.exports.data_deposit_fee = function(current_block_height, network) { - const ret = wasm.data_deposit_fee(current_block_height, network); - return Amount.__wrap(ret); -}; +} +exports.encode_output_issue_nft = encode_output_issue_nft; /** - * Given the parameters needed to create hash timelock contract, and a network type (mainnet, testnet, etc), - * this function creates an output. + * Given a valid receiving address, and a locking rule as bytes (available in this file), + * and a network type (mainnet, testnet, etc), this function creates an output of type + * LockThenTransfer with the parameters provided. * @param {Amount} amount - * @param {string | null | undefined} token_id - * @param {string} secret_hash - * @param {string} spend_address - * @param {string} refund_address - * @param {Uint8Array} refund_timelock + * @param {string} address + * @param {Uint8Array} lock * @param {Network} network * @returns {Uint8Array} */ -module.exports.encode_output_htlc = function(amount, token_id, secret_hash, spend_address, refund_address, refund_timelock, network) { +function encode_output_lock_then_transfer(amount, address, lock, network) { _assertClass(amount, Amount); var ptr0 = amount.__destroy_into_raw(); - var ptr1 = isLikeNone(token_id) ? 0 : passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - var len1 = WASM_VECTOR_LEN; - const ptr2 = passStringToWasm0(secret_hash, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const ptr1 = passStringToWasm0(address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passArray8ToWasm0(lock, wasm.__wbindgen_malloc); const len2 = WASM_VECTOR_LEN; - const ptr3 = passStringToWasm0(spend_address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len3 = WASM_VECTOR_LEN; - const ptr4 = passStringToWasm0(refund_address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len4 = WASM_VECTOR_LEN; - const ptr5 = passArray8ToWasm0(refund_timelock, wasm.__wbindgen_malloc); - const len5 = WASM_VECTOR_LEN; - const ret = wasm.encode_output_htlc(ptr0, ptr1, len1, ptr2, len2, ptr3, len3, ptr4, len4, ptr5, len5, network); + const ret = wasm.encode_output_lock_then_transfer(ptr0, ptr1, len1, ptr2, len2, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v7 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v4 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v7; -}; + return v4; +} +exports.encode_output_lock_then_transfer = encode_output_lock_then_transfer; /** - * Given a signed transaction and input outpoint that spends an htlc utxo, extract a secret that is - * encoded in the corresponding input signature - * @param {Uint8Array} signed_tx_bytes - * @param {boolean} strict_byte_size - * @param {Uint8Array} htlc_outpoint_source_id - * @param {number} htlc_output_index + * Given a pool id and a staker address, this function returns an output that is emitted + * when producing a block via that pool. + * + * Note: a UTXO of this kind is consumed when decommissioning a pool (provided that the pool + * has staked at least once). + * @param {string} pool_id + * @param {string} staker + * @param {Network} network * @returns {Uint8Array} */ -module.exports.extract_htlc_secret = function(signed_tx_bytes, strict_byte_size, htlc_outpoint_source_id, htlc_output_index) { - const ptr0 = passArray8ToWasm0(signed_tx_bytes, wasm.__wbindgen_malloc); +function encode_output_produce_block_from_stake(pool_id, staker, network) { + const ptr0 = passStringToWasm0(pool_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; - const ptr1 = passArray8ToWasm0(htlc_outpoint_source_id, wasm.__wbindgen_malloc); + const ptr1 = passStringToWasm0(staker, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len1 = WASM_VECTOR_LEN; - const ret = wasm.extract_htlc_secret(ptr0, len0, strict_byte_size, ptr1, len1, htlc_output_index); + const ret = wasm.encode_output_produce_block_from_stake(ptr0, len0, ptr1, len1, network); 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; -}; - -/** - * Given an output source id as bytes, and an output index, together representing a utxo, - * this function returns the input that puts them together, as bytes. - * @param {Uint8Array} outpoint_source_id - * @param {number} output_index - * @returns {Uint8Array} - */ -module.exports.encode_input_for_utxo = function(outpoint_source_id, output_index) { - const ptr0 = passArray8ToWasm0(outpoint_source_id, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.encode_input_for_utxo(ptr0, len0, output_index); - if (ret[3]) { - throw takeFromExternrefTable0(ret[2]); - } - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; -}; +} +exports.encode_output_produce_block_from_stake = encode_output_produce_block_from_stake; /** - * Given a delegation id, an amount and a network type (mainnet, testnet, etc), this function - * creates an input that withdraws from a delegation. - * A nonce is needed because this spends from an account. The nonce must be in sequence for everything in that account. - * @param {string} delegation_id + * Given an amount, token ID (in address form) and network type (mainnet, testnet, etc), + * this function creates an output (as bytes) to burn a given amount of tokens * @param {Amount} amount - * @param {bigint} nonce + * @param {string} token_id * @param {Network} network * @returns {Uint8Array} */ -module.exports.encode_input_for_withdraw_from_delegation = function(delegation_id, amount, nonce, network) { - const ptr0 = passStringToWasm0(delegation_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; +function encode_output_token_burn(amount, token_id, network) { _assertClass(amount, Amount); - var ptr1 = amount.__destroy_into_raw(); - const ret = wasm.encode_input_for_withdraw_from_delegation(ptr0, len0, ptr1, nonce, network); + var ptr0 = amount.__destroy_into_raw(); + const ptr1 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.encode_output_token_burn(ptr0, ptr1, len1, network); 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; -}; +} +exports.encode_output_token_burn = encode_output_token_burn; /** - * Given the inputs, along each input's destination that can spend that input - * (e.g. If we are spending a UTXO in input number 1 and it is owned by address mtc1xxxx, then it is mtc1xxxx in element number 2 in the vector/list. - * for Account inputs that spend from a delegation it is the owning address of that delegation, - * and in the case of AccountCommand inputs which change a token it is the token's authority destination) - * and the outputs, estimate the transaction size. - * ScriptHash and ClassicMultisig destinations are not supported. - * @param {Uint8Array} inputs - * @param {string[]} input_utxos_destinations - * @param {Uint8Array} outputs - * @param {Network} network - * @returns {number} - */ -module.exports.estimate_transaction_size = function(inputs, input_utxos_destinations, outputs, network) { - const ptr0 = passArray8ToWasm0(inputs, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ptr1 = passArrayJsValueToWasm0(input_utxos_destinations, wasm.__wbindgen_malloc); - const len1 = WASM_VECTOR_LEN; - const ptr2 = passArray8ToWasm0(outputs, wasm.__wbindgen_malloc); - const len2 = WASM_VECTOR_LEN; - const ret = wasm.estimate_transaction_size(ptr0, len0, ptr1, len1, ptr2, len2, network); - if (ret[2]) { - throw takeFromExternrefTable0(ret[1]); - } - return ret[0] >>> 0; -}; - -/** - * Given inputs as bytes, outputs as bytes, and flags settings, this function returns - * the transaction that contains them all, as bytes. - * @param {Uint8Array} inputs - * @param {Uint8Array} outputs - * @param {bigint} flags - * @returns {Uint8Array} - */ -module.exports.encode_transaction = function(inputs, outputs, flags) { - const ptr0 = passArray8ToWasm0(inputs, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ptr1 = passArray8ToWasm0(outputs, wasm.__wbindgen_malloc); - const len1 = WASM_VECTOR_LEN; - const ret = wasm.encode_transaction(ptr0, len0, ptr1, len1, flags); - 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; -}; - -/** - * Encode an input witness of the variant that contains no signature. - * @returns {Uint8Array} - */ -module.exports.encode_witness_no_signature = function() { - const ret = wasm.encode_witness_no_signature(); - var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v1; -}; - -/** - * Given a private key, inputs and an input number to sign, and the destination that owns that output (through the utxo), - * and a network type (mainnet, testnet, etc), this function returns a witness to be used in a signed transaction, as bytes. - * @param {SignatureHashType} sighashtype - * @param {Uint8Array} private_key_bytes - * @param {string} input_owner_destination - * @param {Uint8Array} transaction_bytes - * @param {Uint8Array} inputs - * @param {number} input_num + * Given a valid receiving address, token ID (in address form), a locking rule as bytes (available in this file), + * and a network type (mainnet, testnet, etc), this function creates an output of type + * LockThenTransfer with the parameters provided. + * @param {Amount} amount + * @param {string} address + * @param {string} token_id + * @param {Uint8Array} lock * @param {Network} network * @returns {Uint8Array} */ -module.exports.encode_witness = function(sighashtype, private_key_bytes, input_owner_destination, transaction_bytes, inputs, input_num, network) { - const ptr0 = passArray8ToWasm0(private_key_bytes, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ptr1 = passStringToWasm0(input_owner_destination, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +function encode_output_token_lock_then_transfer(amount, address, token_id, lock, network) { + _assertClass(amount, Amount); + var ptr0 = amount.__destroy_into_raw(); + const ptr1 = passStringToWasm0(address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len1 = WASM_VECTOR_LEN; - const ptr2 = passArray8ToWasm0(transaction_bytes, wasm.__wbindgen_malloc); + const ptr2 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len2 = WASM_VECTOR_LEN; - const ptr3 = passArray8ToWasm0(inputs, wasm.__wbindgen_malloc); + const ptr3 = passArray8ToWasm0(lock, wasm.__wbindgen_malloc); const len3 = WASM_VECTOR_LEN; - const ret = wasm.encode_witness(sighashtype, ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3, input_num, network); + const ret = wasm.encode_output_token_lock_then_transfer(ptr0, ptr1, len1, ptr2, len2, ptr3, len3, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } var v5 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v5; -}; +} +exports.encode_output_token_lock_then_transfer = encode_output_token_lock_then_transfer; /** - * Given a private key, inputs and an input number to sign, and the destination that owns that output (through the utxo), - * and a network type (mainnet, testnet, etc), and an htlc secret this function returns a witness to be used in a signed transaction, as bytes. - * @param {SignatureHashType} sighashtype - * @param {Uint8Array} private_key_bytes - * @param {string} input_owner_destination - * @param {Uint8Array} transaction_bytes - * @param {Uint8Array} inputs - * @param {number} input_num - * @param {Uint8Array} secret + * Given a destination address, an amount, token ID (in address form) and a network type (mainnet, testnet, etc), this function + * creates an output of type Transfer for tokens, and returns it as bytes. + * @param {Amount} amount + * @param {string} address + * @param {string} token_id * @param {Network} network * @returns {Uint8Array} */ -module.exports.encode_witness_htlc_secret = function(sighashtype, private_key_bytes, input_owner_destination, transaction_bytes, inputs, input_num, secret, network) { - const ptr0 = passArray8ToWasm0(private_key_bytes, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ptr1 = passStringToWasm0(input_owner_destination, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +function encode_output_token_transfer(amount, address, token_id, network) { + _assertClass(amount, Amount); + var ptr0 = amount.__destroy_into_raw(); + const ptr1 = passStringToWasm0(address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len1 = WASM_VECTOR_LEN; - const ptr2 = passArray8ToWasm0(transaction_bytes, wasm.__wbindgen_malloc); + const ptr2 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len2 = WASM_VECTOR_LEN; - const ptr3 = passArray8ToWasm0(inputs, wasm.__wbindgen_malloc); - const len3 = WASM_VECTOR_LEN; - const ptr4 = passArray8ToWasm0(secret, wasm.__wbindgen_malloc); - const len4 = WASM_VECTOR_LEN; - const ret = wasm.encode_witness_htlc_secret(sighashtype, ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3, input_num, ptr4, len4, network); + const ret = wasm.encode_output_token_transfer(ptr0, ptr1, len1, ptr2, len2, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v6 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v4 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v6; -}; + return v4; +} +exports.encode_output_token_transfer = encode_output_token_transfer; /** - * Given an arbitrary number of public keys as bytes, number of minimum required signatures, and a network type, this function returns - * the multisig challenge, as bytes. - * @param {Uint8Array} public_keys_bytes - * @param {number} min_required_signatures + * Given a destination address, an amount and a network type (mainnet, testnet, etc), this function + * creates an output of type Transfer, and returns it as bytes. + * @param {Amount} amount + * @param {string} address * @param {Network} network * @returns {Uint8Array} */ -module.exports.encode_multisig_challenge = function(public_keys_bytes, min_required_signatures, network) { - const ptr0 = passArray8ToWasm0(public_keys_bytes, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.encode_multisig_challenge(ptr0, len0, min_required_signatures, network); +function encode_output_transfer(amount, address, network) { + _assertClass(amount, Amount); + var ptr0 = amount.__destroy_into_raw(); + const ptr1 = passStringToWasm0(address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.encode_output_transfer(ptr0, ptr1, len1, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; -}; + return v3; +} +exports.encode_output_transfer = encode_output_transfer; /** - * Given a private key, inputs and an input number to sign, and multisig challenge, - * and a network type (mainnet, testnet, etc), this function returns a witness to be used in a signed transaction, as bytes. + * Return a PartiallySignedTransaction object as bytes. * - * `key_index` parameter is an index of a public key in the challenge, against which is the signature produces from private key is to be verified. - * `input_witness` parameter can be either empty or a result of previous calls to this function. - * @param {SignatureHashType} sighashtype - * @param {Uint8Array} private_key_bytes - * @param {number} key_index - * @param {Uint8Array} input_witness - * @param {Uint8Array} multisig_challenge - * @param {Uint8Array} transaction_bytes - * @param {Uint8Array} utxos - * @param {number} input_num + * `transaction` is an encoded `Transaction` (which can be produced via `encode_transaction`). + * + * `signatures`, `input_utxos`, `input_destinations` and `htlc_secrets` are encoded lists of + * optional objects of the corresponding type. To produce such a list, iterate over your + * original list of optional objects and then: + * 1) emit byte 0 if the current object is null; + * 2) otherwise emit byte 1 followed by the object in its encoded form. + * + * Each individual object in each of the lists corresponds to the transaction input with the same + * index and its meaning is as follows: + * 1) `signatures` - the signature for the input; + * 2) `input_utxos`- the utxo for the input (if it's utxo-based); + * 3) `input_destinations` - the destination (address) corresponding to the input; this determines + * the key(s) with which the input has to be signed. Note that for utxo-based inputs the + * corresponding destination can usually be extracted from the utxo itself (the exception + * being the `ProduceBlockFromStake` utxo, which doesn't contain the pool's decommission key). + * However, PartiallySignedTransaction requires that *all* input destinations are provided + * explicitly anyway. + * 4) `htlc_secrets` - if the input is an HTLC one and if the transaction is spending the HTLC, + * this should be the HTLC secret. Otherwise it should be null. + * + * The number of items in each list must be equal to the number of transaction inputs. + * + * `additional_info` has the same meaning as in `encode_witness`. + * @param {Uint8Array} transaction + * @param {Uint8Array} signatures + * @param {Uint8Array} input_utxos + * @param {Uint8Array} input_destinations + * @param {Uint8Array} htlc_secrets + * @param {TxAdditionalInfo} additional_info * @param {Network} network * @returns {Uint8Array} */ -module.exports.encode_witness_htlc_multisig = function(sighashtype, private_key_bytes, key_index, input_witness, multisig_challenge, transaction_bytes, utxos, input_num, network) { - const ptr0 = passArray8ToWasm0(private_key_bytes, wasm.__wbindgen_malloc); +function encode_partially_signed_transaction(transaction, signatures, input_utxos, input_destinations, htlc_secrets, additional_info, network) { + const ptr0 = passArray8ToWasm0(transaction, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ptr1 = passArray8ToWasm0(input_witness, wasm.__wbindgen_malloc); + const ptr1 = passArray8ToWasm0(signatures, wasm.__wbindgen_malloc); const len1 = WASM_VECTOR_LEN; - const ptr2 = passArray8ToWasm0(multisig_challenge, wasm.__wbindgen_malloc); + const ptr2 = passArray8ToWasm0(input_utxos, wasm.__wbindgen_malloc); const len2 = WASM_VECTOR_LEN; - const ptr3 = passArray8ToWasm0(transaction_bytes, wasm.__wbindgen_malloc); + const ptr3 = passArray8ToWasm0(input_destinations, wasm.__wbindgen_malloc); const len3 = WASM_VECTOR_LEN; - const ptr4 = passArray8ToWasm0(utxos, wasm.__wbindgen_malloc); + const ptr4 = passArray8ToWasm0(htlc_secrets, wasm.__wbindgen_malloc); const len4 = WASM_VECTOR_LEN; - const ret = wasm.encode_witness_htlc_multisig(sighashtype, ptr0, len0, key_index, ptr1, len1, ptr2, len2, ptr3, len3, ptr4, len4, input_num, network); + const ret = wasm.encode_partially_signed_transaction(ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3, ptr4, len4, additional_info, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } var v6 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v6; -}; +} +exports.encode_partially_signed_transaction = encode_partially_signed_transaction; /** * Given an unsigned transaction and signatures, this function returns a SignedTransaction object as bytes. - * @param {Uint8Array} transaction_bytes + * @param {Uint8Array} transaction * @param {Uint8Array} signatures * @returns {Uint8Array} */ -module.exports.encode_signed_transaction = function(transaction_bytes, signatures) { - const ptr0 = passArray8ToWasm0(transaction_bytes, wasm.__wbindgen_malloc); +function encode_signed_transaction(transaction, signatures) { + const ptr0 = passArray8ToWasm0(transaction, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ptr1 = passArray8ToWasm0(signatures, wasm.__wbindgen_malloc); const len1 = WASM_VECTOR_LEN; @@ -1306,575 +1101,1219 @@ module.exports.encode_signed_transaction = function(transaction_bytes, signature var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v3; -}; +} +exports.encode_signed_transaction = encode_signed_transaction; /** - * Given a `Transaction` encoded in bytes (not a signed transaction, but a signed transaction is tolerated by ignoring the extra bytes, by choice) - * this function will return the transaction id. + * Return a `SignedTransactionIntent` object as bytes given the message and encoded signatures. * - * The second parameter, the boolean, is provided as means of asserting that the given bytes exactly match a `Transaction` object. - * When set to `true`, the bytes provided must exactly match a single `Transaction` object. - * When set to `false`, extra bytes can exist, but will be ignored. - * This is useful when the provided bytes are of a `SignedTransaction` instead of a `Transaction`, - * since the signatures are appended at the end of the `Transaction` object as a vector to create a `SignedTransaction`. - * It is recommended to use a strict `Transaction` size and set the second parameter to `true`. - * @param {Uint8Array} transaction_bytes - * @param {boolean} strict_byte_size - * @returns {string} - */ -module.exports.get_transaction_id = function(transaction_bytes, strict_byte_size) { - let deferred3_0; - let deferred3_1; - try { - const ptr0 = passArray8ToWasm0(transaction_bytes, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.get_transaction_id(ptr0, len0, strict_byte_size); - var ptr2 = ret[0]; - var len2 = ret[1]; - if (ret[3]) { - ptr2 = 0; len2 = 0; - throw takeFromExternrefTable0(ret[2]); - } - deferred3_0 = ptr2; - deferred3_1 = len2; - return getStringFromWasm0(ptr2, len2); - } finally { - wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); - } -}; - -/** - * Calculate the "effective balance" of a pool, given the total pool balance and pledge by the pool owner/staker. - * The effective balance is how the influence of a pool is calculated due to its balance. - * @param {Network} network - * @param {Amount} pledge_amount - * @param {Amount} pool_balance - * @returns {Amount} - */ -module.exports.effective_pool_balance = function(network, pledge_amount, pool_balance) { - _assertClass(pledge_amount, Amount); - var ptr0 = pledge_amount.__destroy_into_raw(); - _assertClass(pool_balance, Amount); - var ptr1 = pool_balance.__destroy_into_raw(); - const ret = wasm.effective_pool_balance(network, ptr0, ptr1); - if (ret[2]) { - throw takeFromExternrefTable0(ret[1]); - } - return Amount.__wrap(ret[0]); -}; - -/** - * Given a token_id, an amount of tokens to mint and nonce return an encoded mint tokens input - * @param {string} token_id - * @param {Amount} amount - * @param {bigint} nonce - * @param {Network} network + * Note: to produce a valid signed intent one is expected to sign the corresponding message by private keys + * corresponding to each input of the transaction. + * + * Parameters: + * `signed_message` - this must have been produced by `make_transaction_intent_message_to_sign`. + * `signatures` - this should be an array of Uint8Array, each of them representing an individual signature + * of `signed_message` produced by `sign_challenge` using the private key for the corresponding input destination + * of the transaction. The number of signatures must be equal to the number of inputs in the transaction. + * @param {Uint8Array} signed_message + * @param {Uint8Array[]} signatures * @returns {Uint8Array} */ -module.exports.encode_input_for_mint_tokens = function(token_id, amount, nonce, network) { - const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +function encode_signed_transaction_intent(signed_message, signatures) { + const ptr0 = passArray8ToWasm0(signed_message, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - _assertClass(amount, Amount); - var ptr1 = amount.__destroy_into_raw(); - const ret = wasm.encode_input_for_mint_tokens(ptr0, len0, ptr1, nonce, network); + const ptr1 = passArrayJsValueToWasm0(signatures, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.encode_signed_transaction_intent(ptr0, len0, ptr1, len1); 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; -}; +} +exports.encode_signed_transaction_intent = encode_signed_transaction_intent; /** - * Given a token_id and nonce return an encoded unmint tokens input - * @param {string} token_id - * @param {bigint} nonce + * This function returns the staking pool data needed to create a staking pool in an output as bytes, + * given its parameters and the network type (testnet, mainnet, etc). + * @param {Amount} value + * @param {string} staker + * @param {string} vrf_public_key + * @param {string} decommission_key + * @param {number} margin_ratio_per_thousand + * @param {Amount} cost_per_block * @param {Network} network * @returns {Uint8Array} */ -module.exports.encode_input_for_unmint_tokens = function(token_id, nonce, network) { - const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.encode_input_for_unmint_tokens(ptr0, len0, nonce, network); +function encode_stake_pool_data(value, staker, vrf_public_key, decommission_key, margin_ratio_per_thousand, cost_per_block, network) { + _assertClass(value, Amount); + var ptr0 = value.__destroy_into_raw(); + const ptr1 = passStringToWasm0(staker, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passStringToWasm0(vrf_public_key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len2 = WASM_VECTOR_LEN; + const ptr3 = passStringToWasm0(decommission_key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len3 = WASM_VECTOR_LEN; + _assertClass(cost_per_block, Amount); + var ptr4 = cost_per_block.__destroy_into_raw(); + const ret = wasm.encode_stake_pool_data(ptr0, ptr1, len1, ptr2, len2, ptr3, len3, margin_ratio_per_thousand, ptr4, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v6 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; -}; + return v6; +} +exports.encode_stake_pool_data = encode_stake_pool_data; /** - * Given a token_id and nonce return an encoded lock_token_supply input - * @param {string} token_id - * @param {bigint} nonce - * @param {Network} network + * Given inputs as bytes, outputs as bytes, and flags settings, this function returns + * the transaction that contains them all, as bytes. + * @param {Uint8Array} inputs + * @param {Uint8Array} outputs + * @param {bigint} flags * @returns {Uint8Array} */ -module.exports.encode_input_for_lock_token_supply = function(token_id, nonce, network) { - const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +function encode_transaction(inputs, outputs, flags) { + const ptr0 = passArray8ToWasm0(inputs, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ret = wasm.encode_input_for_lock_token_supply(ptr0, len0, nonce, network); + const ptr1 = passArray8ToWasm0(outputs, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.encode_transaction(ptr0, len0, ptr1, len1, flags); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; -}; + return v3; +} +exports.encode_transaction = encode_transaction; /** - * Given a token_id, is token unfreezable and nonce return an encoded freeze token input - * @param {string} token_id - * @param {TokenUnfreezable} is_token_unfreezable - * @param {bigint} nonce + * Sign the specified input of the transaction and encode the signature as InputWitness. + * + * `input_utxos` must be formed as follows: for each transaction input, emit byte 0 if it's a non-UTXO input, + * otherwise emit 1 followed by the corresponding transaction output encoded via the appropriate "encode_output_" + * function. + * + * `additional_info` must contain the following: + * 1) for each `ProduceBlockFromStake` input of the transaction, the pool info for the pool referenced by that input; + * 2) for each `FillOrder` and `ConcludeOrder` input of the transaction, the order info for the order referenced by + * that input. + * Note: + * - It doesn't matter which input witness is currently being encoded. E.g. even if you are encoding a witness + * for some UTXO-based input but another input of the same transaction is `FillOrder`, you have to include the order + * info when encoding the witness for the UTXO-based input too. + * - After a certain hard fork, the produced signature will "commit" to the provided additional info, i.e. the info + * will become a part of what is being signed. So, passing invalid additional info will result in an invalid signature + * (with one small caveat: for `FillOrder` we only commit to order's initial balances and not the current ones; + * so if you only have `FillOrder` inputs, you can technically pass bogus values for the current balances and + * the resulting signature will still be valid; though it's better to avoid doing this). + * @param {SignatureHashType} sighashtype + * @param {Uint8Array} private_key + * @param {string} input_owner_destination + * @param {Uint8Array} transaction + * @param {Uint8Array} input_utxos + * @param {number} input_index + * @param {TxAdditionalInfo} additional_info + * @param {bigint} current_block_height * @param {Network} network * @returns {Uint8Array} */ -module.exports.encode_input_for_freeze_token = function(token_id, is_token_unfreezable, nonce, network) { - const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +function encode_witness(sighashtype, private_key, input_owner_destination, transaction, input_utxos, input_index, additional_info, current_block_height, network) { + const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ret = wasm.encode_input_for_freeze_token(ptr0, len0, is_token_unfreezable, nonce, network); + const ptr1 = passStringToWasm0(input_owner_destination, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passArray8ToWasm0(transaction, wasm.__wbindgen_malloc); + const len2 = WASM_VECTOR_LEN; + const ptr3 = passArray8ToWasm0(input_utxos, wasm.__wbindgen_malloc); + const len3 = WASM_VECTOR_LEN; + const ret = wasm.encode_witness(sighashtype, ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3, input_index, additional_info, current_block_height, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v5 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; -}; + return v5; +} +exports.encode_witness = encode_witness; /** - * Given a token_id and nonce return an encoded unfreeze token input - * @param {string} token_id - * @param {bigint} nonce + * Sign the specified HTLC input of the transaction and encode the signature as InputWitness. + * + * This function must be used for HTLC refunding when the refund address is a multisig one. + * + * `key_index` parameter is an index of the public key in the multisig challenge corresponding to + * the specified private key. + * `input_witness` parameter can be either empty or a result of previous calls to this function. + * + * `input_utxos` and `additional_info` have the same format and requirements as in `encode_witness`. + * @param {SignatureHashType} sighashtype + * @param {Uint8Array} private_key + * @param {number} key_index + * @param {Uint8Array} input_witness + * @param {Uint8Array} multisig_challenge + * @param {Uint8Array} transaction + * @param {Uint8Array} input_utxos + * @param {number} input_index + * @param {TxAdditionalInfo} additional_info + * @param {bigint} current_block_height * @param {Network} network * @returns {Uint8Array} */ -module.exports.encode_input_for_unfreeze_token = function(token_id, nonce, network) { - const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +function encode_witness_htlc_refund_multisig(sighashtype, private_key, key_index, input_witness, multisig_challenge, transaction, input_utxos, input_index, additional_info, current_block_height, network) { + const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ret = wasm.encode_input_for_unfreeze_token(ptr0, len0, nonce, network); + const ptr1 = passArray8ToWasm0(input_witness, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passArray8ToWasm0(multisig_challenge, wasm.__wbindgen_malloc); + const len2 = WASM_VECTOR_LEN; + const ptr3 = passArray8ToWasm0(transaction, wasm.__wbindgen_malloc); + const len3 = WASM_VECTOR_LEN; + const ptr4 = passArray8ToWasm0(input_utxos, wasm.__wbindgen_malloc); + const len4 = WASM_VECTOR_LEN; + const ret = wasm.encode_witness_htlc_refund_multisig(sighashtype, ptr0, len0, key_index, ptr1, len1, ptr2, len2, ptr3, len3, ptr4, len4, input_index, additional_info, current_block_height, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v6 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; -}; + return v6; +} +exports.encode_witness_htlc_refund_multisig = encode_witness_htlc_refund_multisig; /** - * Given a token_id, new authority destination and nonce return an encoded change token authority input - * @param {string} token_id - * @param {string} new_authority - * @param {bigint} nonce + * Sign the specified HTLC input of the transaction and encode the signature as InputWitness. + * + * This function must be used for HTLC refunding when the refund address is a single-sig one. + * + * `input_utxos` and `additional_info` have the same format and requirements as in `encode_witness`. + * @param {SignatureHashType} sighashtype + * @param {Uint8Array} private_key + * @param {string} input_owner_destination + * @param {Uint8Array} transaction + * @param {Uint8Array} input_utxos + * @param {number} input_index + * @param {TxAdditionalInfo} additional_info + * @param {bigint} current_block_height * @param {Network} network * @returns {Uint8Array} */ -module.exports.encode_input_for_change_token_authority = function(token_id, new_authority, nonce, network) { - const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +function encode_witness_htlc_refund_single_sig(sighashtype, private_key, input_owner_destination, transaction, input_utxos, input_index, additional_info, current_block_height, network) { + const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ptr1 = passStringToWasm0(new_authority, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const ptr1 = passStringToWasm0(input_owner_destination, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len1 = WASM_VECTOR_LEN; - const ret = wasm.encode_input_for_change_token_authority(ptr0, len0, ptr1, len1, nonce, network); + const ptr2 = passArray8ToWasm0(transaction, wasm.__wbindgen_malloc); + const len2 = WASM_VECTOR_LEN; + const ptr3 = passArray8ToWasm0(input_utxos, wasm.__wbindgen_malloc); + const len3 = WASM_VECTOR_LEN; + const ret = wasm.encode_witness_htlc_refund_single_sig(sighashtype, ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3, input_index, additional_info, current_block_height, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v5 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v3; -}; + return v5; +} +exports.encode_witness_htlc_refund_single_sig = encode_witness_htlc_refund_single_sig; /** - * Given a token_id, new metadata uri and nonce return an encoded change token metadata uri input - * @param {string} token_id - * @param {string} new_metadata_uri - * @param {bigint} nonce + * Sign the specified HTLC input of the transaction and encode the signature as InputWitness. + * + * This function must be used for HTLC spending. + * + * `input_utxos` and `additional_info` have the same format and requirements as in `encode_witness`. + * @param {SignatureHashType} sighashtype + * @param {Uint8Array} private_key + * @param {string} input_owner_destination + * @param {Uint8Array} transaction + * @param {Uint8Array} input_utxos + * @param {number} input_index + * @param {Uint8Array} secret + * @param {TxAdditionalInfo} additional_info + * @param {bigint} current_block_height * @param {Network} network * @returns {Uint8Array} */ -module.exports.encode_input_for_change_token_metadata_uri = function(token_id, new_metadata_uri, nonce, network) { - const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +function encode_witness_htlc_spend(sighashtype, private_key, input_owner_destination, transaction, input_utxos, input_index, secret, additional_info, current_block_height, network) { + const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ptr1 = passStringToWasm0(new_metadata_uri, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const ptr1 = passStringToWasm0(input_owner_destination, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len1 = WASM_VECTOR_LEN; - const ret = wasm.encode_input_for_change_token_metadata_uri(ptr0, len0, ptr1, len1, nonce, network); + const ptr2 = passArray8ToWasm0(transaction, wasm.__wbindgen_malloc); + const len2 = WASM_VECTOR_LEN; + const ptr3 = passArray8ToWasm0(input_utxos, wasm.__wbindgen_malloc); + const len3 = WASM_VECTOR_LEN; + const ptr4 = passArray8ToWasm0(secret, wasm.__wbindgen_malloc); + const len4 = WASM_VECTOR_LEN; + const ret = wasm.encode_witness_htlc_spend(sighashtype, ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3, input_index, ptr4, len4, additional_info, current_block_height, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v6 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v3; -}; + return v6; +} +exports.encode_witness_htlc_spend = encode_witness_htlc_spend; /** - * Given ask and give amounts and a conclude key create output that creates an order. - * - * 'ask_token_id': the parameter represents a Token if it's Some and coins otherwise. - * 'give_token_id': the parameter represents a Token if it's Some and coins otherwise. - * @param {Amount} ask_amount - * @param {string | null | undefined} ask_token_id - * @param {Amount} give_amount - * @param {string | null | undefined} give_token_id - * @param {string} conclude_address - * @param {Network} network + * Encode an input witness of the variant that contains no signature. * @returns {Uint8Array} */ -module.exports.encode_create_order_output = function(ask_amount, ask_token_id, give_amount, give_token_id, conclude_address, network) { - _assertClass(ask_amount, Amount); - var ptr0 = ask_amount.__destroy_into_raw(); - var ptr1 = isLikeNone(ask_token_id) ? 0 : passStringToWasm0(ask_token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - var len1 = WASM_VECTOR_LEN; - _assertClass(give_amount, Amount); - var ptr2 = give_amount.__destroy_into_raw(); - var ptr3 = isLikeNone(give_token_id) ? 0 : passStringToWasm0(give_token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - var len3 = WASM_VECTOR_LEN; - const ptr4 = passStringToWasm0(conclude_address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len4 = WASM_VECTOR_LEN; - const ret = wasm.encode_create_order_output(ptr0, ptr1, len1, ptr2, ptr3, len3, ptr4, len4, network); - if (ret[3]) { - throw takeFromExternrefTable0(ret[2]); - } - var v6 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); +function encode_witness_no_signature() { + const ret = wasm.encode_witness_no_signature(); + var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v6; -}; + return v1; +} +exports.encode_witness_no_signature = encode_witness_no_signature; /** - * Given an amount to fill an order (which is described in terms of ask currency) and a destination - * for result outputs create an input that fills the order. - * @param {string} order_id - * @param {Amount} fill_amount - * @param {string} destination - * @param {bigint} nonce + * Given the inputs, along each input's destination that can spend that input + * (e.g. If we are spending a UTXO in input number 1 and it is owned by address mtc1xxxx, then it is mtc1xxxx in element number 2 in the vector/list. + * for Account inputs that spend from a delegation it is the owning address of that delegation, + * and in the case of AccountCommand inputs which change a token it is the token's authority destination) + * and the outputs, estimate the transaction size. + * ScriptHash and ClassicMultisig destinations are not supported. + * Also, the function assumes that the input UTXOs are not HTLC. + * @param {Uint8Array} inputs + * @param {string[]} input_utxos_destinations + * @param {Uint8Array} outputs * @param {Network} network - * @returns {Uint8Array} + * @returns {number} */ -module.exports.encode_input_for_fill_order = function(order_id, fill_amount, destination, nonce, network) { - const ptr0 = passStringToWasm0(order_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +function estimate_transaction_size(inputs, input_utxos_destinations, outputs, network) { + const ptr0 = passArray8ToWasm0(inputs, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - _assertClass(fill_amount, Amount); - var ptr1 = fill_amount.__destroy_into_raw(); - const ptr2 = passStringToWasm0(destination, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const ptr1 = passArrayJsValueToWasm0(input_utxos_destinations, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passArray8ToWasm0(outputs, wasm.__wbindgen_malloc); const len2 = WASM_VECTOR_LEN; - const ret = wasm.encode_input_for_fill_order(ptr0, len0, ptr1, ptr2, len2, nonce, network); - if (ret[3]) { - throw takeFromExternrefTable0(ret[2]); + const ret = wasm.estimate_transaction_size(ptr0, len0, ptr1, len1, ptr2, len2, network); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); } - var v4 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v4; -}; + return ret[0] >>> 0; +} +exports.estimate_transaction_size = estimate_transaction_size; /** - * Given an order id create an input that concludes the order. - * @param {string} order_id - * @param {bigint} nonce - * @param {Network} network + * Return the extended public key from an extended private key + * @param {Uint8Array} private_key * @returns {Uint8Array} */ -module.exports.encode_input_for_conclude_order = function(order_id, nonce, network) { - const ptr0 = passStringToWasm0(order_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +function extended_public_key_from_extended_private_key(private_key) { + const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ret = wasm.encode_input_for_conclude_order(ptr0, len0, nonce, network); + const ret = wasm.extended_public_key_from_extended_private_key(ptr0, len0); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v2; -}; +} +exports.extended_public_key_from_extended_private_key = extended_public_key_from_extended_private_key; /** - * Indicates whether a token can be frozen - * @enum {0 | 1} + * Given a signed transaction and input outpoint that spends an htlc utxo, extract a secret that is + * encoded in the corresponding input signature + * @param {Uint8Array} signed_tx + * @param {boolean} strict_byte_size + * @param {Uint8Array} htlc_outpoint_source_id + * @param {number} htlc_output_index + * @returns {Uint8Array} */ -module.exports.FreezableToken = Object.freeze({ - No: 0, "0": "No", - Yes: 1, "1": "Yes", -}); +function extract_htlc_secret(signed_tx, strict_byte_size, htlc_outpoint_source_id, htlc_output_index) { + const ptr0 = passArray8ToWasm0(signed_tx, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(htlc_outpoint_source_id, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.extract_htlc_secret(ptr0, len0, strict_byte_size, ptr1, len1, htlc_output_index); + 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; +} +exports.extract_htlc_secret = extract_htlc_secret; + /** - * The network, for which an operation to be done. Mainnet, testnet, etc. - * @enum {0 | 1 | 2 | 3} + * Returns the fee that needs to be paid by a transaction for issuing a new fungible token + * @param {bigint} _current_block_height + * @param {Network} network + * @returns {Amount} */ -module.exports.Network = Object.freeze({ - Mainnet: 0, "0": "Mainnet", - Testnet: 1, "1": "Testnet", - Regtest: 2, "2": "Regtest", - Signet: 3, "3": "Signet", -}); +function fungible_token_issuance_fee(_current_block_height, network) { + const ret = wasm.fungible_token_issuance_fee(_current_block_height, network); + return Amount.__wrap(ret); +} +exports.fungible_token_issuance_fee = fungible_token_issuance_fee; + /** - * The part of the transaction that will be committed in the signature. Similar to bitcoin's sighash. - * @enum {0 | 1 | 2 | 3} + * Returns the Delegation ID for the given inputs of a transaction + * @param {Uint8Array} inputs + * @param {Network} network + * @returns {string} */ -module.exports.SignatureHashType = Object.freeze({ - ALL: 0, "0": "ALL", - NONE: 1, "1": "NONE", - SINGLE: 2, "2": "SINGLE", - ANYONECANPAY: 3, "3": "ANYONECANPAY", -}); +function get_delegation_id(inputs, network) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passArray8ToWasm0(inputs, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.get_delegation_id(ptr0, len0, network); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } +} +exports.get_delegation_id = get_delegation_id; + /** - * A utxo can either come from a transaction or a block reward. This enum signifies that. - * @enum {0 | 1} + * Returns the Order ID for the given inputs of a transaction + * @param {Uint8Array} inputs + * @param {Network} network + * @returns {string} */ -module.exports.SourceId = Object.freeze({ - Transaction: 0, "0": "Transaction", - BlockReward: 1, "1": "BlockReward", -}); +function get_order_id(inputs, network) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passArray8ToWasm0(inputs, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.get_order_id(ptr0, len0, network); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } +} +exports.get_order_id = get_order_id; + /** - * Indicates whether a token can be unfrozen once frozen - * @enum {0 | 1} - */ -module.exports.TokenUnfreezable = Object.freeze({ - No: 0, "0": "No", - Yes: 1, "1": "Yes", -}); + * Returns the Pool ID for the given inputs of a transaction + * @param {Uint8Array} inputs + * @param {Network} network + * @returns {string} + */ +function get_pool_id(inputs, network) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passArray8ToWasm0(inputs, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.get_pool_id(ptr0, len0, network); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } +} +exports.get_pool_id = get_pool_id; + +/** + * Returns the Fungible/NFT Token ID for the given inputs of a transaction + * @param {Uint8Array} inputs + * @param {bigint} current_block_height + * @param {Network} network + * @returns {string} + */ +function get_token_id(inputs, current_block_height, network) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passArray8ToWasm0(inputs, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.get_token_id(ptr0, len0, current_block_height, network); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } +} +exports.get_token_id = get_token_id; + +/** + * Given a `Transaction` encoded in bytes (not a signed transaction, but a signed transaction is tolerated by ignoring the extra bytes, by choice) + * this function will return the transaction id. + * + * The second parameter, the boolean, is provided as means of asserting that the given bytes exactly match a `Transaction` object. + * When set to `true`, the bytes provided must exactly match a single `Transaction` object. + * When set to `false`, extra bytes can exist, but will be ignored. + * This is useful when the provided bytes are of a `SignedTransaction` instead of a `Transaction`, + * since the signatures are appended at the end of the `Transaction` object as a vector to create a `SignedTransaction`. + * It is recommended to use a strict `Transaction` size and set the second parameter to `true`. + * @param {Uint8Array} transaction + * @param {boolean} strict_byte_size + * @returns {string} + */ +function get_transaction_id(transaction, strict_byte_size) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passArray8ToWasm0(transaction, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.get_transaction_id(ptr0, len0, strict_byte_size); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } +} +exports.get_transaction_id = get_transaction_id; + +/** + * Verify a witness produced by one of the `encode_witness` functions. + * + * `input_owner_destination` must be specified if `witness` actually contains a signature + * (i.e. it's not InputWitness::NoSignature) and the input is not an HTLC one. Otherwise it must + * be null. + * @param {SignatureHashType} sighashtype + * @param {string | null | undefined} input_owner_destination + * @param {Uint8Array} witness + * @param {Uint8Array} transaction + * @param {Uint8Array} input_utxos + * @param {number} input_index + * @param {TxAdditionalInfo} additional_info + * @param {bigint} current_block_height + * @param {Network} network + */ +function internal_verify_witness(sighashtype, input_owner_destination, witness, transaction, input_utxos, input_index, additional_info, current_block_height, network) { + var ptr0 = isLikeNone(input_owner_destination) ? 0 : passStringToWasm0(input_owner_destination, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + var len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(witness, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passArray8ToWasm0(transaction, wasm.__wbindgen_malloc); + const len2 = WASM_VECTOR_LEN; + const ptr3 = passArray8ToWasm0(input_utxos, wasm.__wbindgen_malloc); + const len3 = WASM_VECTOR_LEN; + const ret = wasm.internal_verify_witness(sighashtype, ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3, input_index, additional_info, current_block_height, network); + if (ret[1]) { + throw takeFromExternrefTable0(ret[0]); + } +} +exports.internal_verify_witness = internal_verify_witness; + +/** + * From an extended private key create a change private key for a given key index + * derivation path: current_derivation_path/1/key_index + * @param {Uint8Array} private_key + * @param {number} key_index + * @returns {Uint8Array} + */ +function make_change_address(private_key, key_index) { + const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.make_change_address(ptr0, len0, key_index); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v2; +} +exports.make_change_address = make_change_address; + +/** + * From an extended public key create a change public key for a given key index + * derivation path: current_derivation_path/1/key_index + * @param {Uint8Array} extended_public_key + * @param {number} key_index + * @returns {Uint8Array} + */ +function make_change_address_public_key(extended_public_key, key_index) { + const ptr0 = passArray8ToWasm0(extended_public_key, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.make_change_address_public_key(ptr0, len0, key_index); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v2; +} +exports.make_change_address_public_key = make_change_address_public_key; + +/** + * Create the default account's extended private key for a given mnemonic + * derivation path: 44'/mintlayer_coin_type'/0' + * @param {string} mnemonic + * @param {Network} network + * @returns {Uint8Array} + */ +function make_default_account_privkey(mnemonic, network) { + const ptr0 = passStringToWasm0(mnemonic, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.make_default_account_privkey(ptr0, len0, network); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v2; +} +exports.make_default_account_privkey = make_default_account_privkey; + +/** + * Generates a new, random private key from entropy + * @returns {Uint8Array} + */ +function make_private_key() { + const ret = wasm.make_private_key(); + var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v1; +} +exports.make_private_key = make_private_key; + +/** + * From an extended private key create a receiving private key for a given key index + * derivation path: current_derivation_path/0/key_index + * @param {Uint8Array} private_key + * @param {number} key_index + * @returns {Uint8Array} + */ +function make_receiving_address(private_key, key_index) { + const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.make_receiving_address(ptr0, len0, key_index); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v2; +} +exports.make_receiving_address = make_receiving_address; + +/** + * From an extended public key create a receiving public key for a given key index + * derivation path: current_derivation_path/0/key_index + * @param {Uint8Array} extended_public_key + * @param {number} key_index + * @returns {Uint8Array} + */ +function make_receiving_address_public_key(extended_public_key, key_index) { + const ptr0 = passArray8ToWasm0(extended_public_key, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.make_receiving_address_public_key(ptr0, len0, key_index); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v2; +} +exports.make_receiving_address_public_key = make_receiving_address_public_key; + +/** + * Return the message that has to be signed to produce a signed transaction intent. + * @param {string} intent + * @param {string} transaction_id + * @returns {Uint8Array} + */ +function make_transaction_intent_message_to_sign(intent, transaction_id) { + const ptr0 = passStringToWasm0(intent, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(transaction_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.make_transaction_intent_message_to_sign(ptr0, len0, ptr1, len1); + 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; +} +exports.make_transaction_intent_message_to_sign = make_transaction_intent_message_to_sign; + +/** + * Produce a multisig address given a multisig challenge. + * @param {Uint8Array} multisig_challenge + * @param {Network} network + * @returns {string} + */ +function multisig_challenge_to_address(multisig_challenge, network) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passArray8ToWasm0(multisig_challenge, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.multisig_challenge_to_address(ptr0, len0, network); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } +} +exports.multisig_challenge_to_address = multisig_challenge_to_address; + +/** + * Given the current block height and a network type (mainnet, testnet, etc), + * this will return the fee that needs to be paid by a transaction for issuing a new NFT + * The current block height information is used in case a network upgrade changed the value. + * @param {bigint} current_block_height + * @param {Network} network + * @returns {Amount} + */ +function nft_issuance_fee(current_block_height, network) { + const ret = wasm.nft_issuance_fee(current_block_height, network); + return Amount.__wrap(ret); +} +exports.nft_issuance_fee = nft_issuance_fee; + +/** + * Given a public key (as bytes) and a network type (mainnet, testnet, etc), + * return the address public key hash from that public key as an address + * @param {Uint8Array} public_key + * @param {Network} network + * @returns {string} + */ +function pubkey_to_pubkeyhash_address(public_key, network) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passArray8ToWasm0(public_key, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.pubkey_to_pubkeyhash_address(ptr0, len0, network); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } +} +exports.pubkey_to_pubkeyhash_address = pubkey_to_pubkeyhash_address; + +/** + * Given a private key, as bytes, return the bytes of the corresponding public key + * @param {Uint8Array} private_key + * @returns {Uint8Array} + */ +function public_key_from_private_key(private_key) { + const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.public_key_from_private_key(ptr0, len0); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v2; +} +exports.public_key_from_private_key = public_key_from_private_key; + +/** + * Given a message and a private key, create and sign a challenge with the given private key. + * This kind of signature is to be used when signing challenges. + * @param {Uint8Array} private_key + * @param {Uint8Array} message + * @returns {Uint8Array} + */ +function sign_challenge(private_key, message) { + const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(message, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.sign_challenge(ptr0, len0, ptr1, len1); + 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; +} +exports.sign_challenge = sign_challenge; + +/** + * Given a message and a private key, sign the message with the given private key + * This kind of signature is to be used when signing spend requests, such as transaction + * input witness. + * @param {Uint8Array} private_key + * @param {Uint8Array} message + * @returns {Uint8Array} + */ +function sign_message_for_spending(private_key, message) { + const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(message, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.sign_message_for_spending(ptr0, len0, ptr1, len1); + 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; +} +exports.sign_message_for_spending = sign_message_for_spending; + +/** + * Given the current block height and a network type (mainnet, testnet, etc), + * this function returns the number of blocks, after which a pool that decommissioned, + * will have its funds unlocked and available for spending. + * The current block height information is used in case a network upgrade changed the value. + * @param {bigint} current_block_height + * @param {Network} network + * @returns {bigint} + */ +function staking_pool_spend_maturity_block_count(current_block_height, network) { + const ret = wasm.staking_pool_spend_maturity_block_count(current_block_height, network); + return BigInt.asUintN(64, ret); +} +exports.staking_pool_spend_maturity_block_count = staking_pool_spend_maturity_block_count; + +/** + * Given the current block height and a network type (mainnet, testnet, etc), + * this will return the fee that needs to be paid by a transaction for changing the authority of a token + * The current block height information is used in case a network upgrade changed the value. + * @param {bigint} current_block_height + * @param {Network} network + * @returns {Amount} + */ +function token_change_authority_fee(current_block_height, network) { + const ret = wasm.token_change_authority_fee(current_block_height, network); + return Amount.__wrap(ret); +} +exports.token_change_authority_fee = token_change_authority_fee; + +/** + * Given the current block height and a network type (mainnet, testnet, etc), + * this will return the fee that needs to be paid by a transaction for freezing/unfreezing a token + * The current block height information is used in case a network upgrade changed the value. + * @param {bigint} current_block_height + * @param {Network} network + * @returns {Amount} + */ +function token_freeze_fee(current_block_height, network) { + const ret = wasm.token_freeze_fee(current_block_height, network); + return Amount.__wrap(ret); +} +exports.token_freeze_fee = token_freeze_fee; + +/** + * Given the current block height and a network type (mainnet, testnet, etc), + * this will return the fee that needs to be paid by a transaction for changing the total supply of a token + * by either minting or unminting tokens + * The current block height information is used in case a network upgrade changed the value. + * @param {bigint} current_block_height + * @param {Network} network + * @returns {Amount} + */ +function token_supply_change_fee(current_block_height, network) { + const ret = wasm.token_supply_change_fee(current_block_height, network); + return Amount.__wrap(ret); +} +exports.token_supply_change_fee = token_supply_change_fee; + +/** + * Given a signed challenge, an address and a message, verify that + * the signature is produced by signing the message with the private key + * that derived the given public key. + * This function is used for verifying messages-related challenges. + * + * Note: for signatures that were created by `sign_challenge`, the provided address must be + * a 'pubkeyhash' address. + * + * Note: currently this function never returns `false` - it either returns `true` or fails with an error. + * @param {string} address + * @param {Network} network + * @param {Uint8Array} signed_challenge + * @param {Uint8Array} message + * @returns {boolean} + */ +function verify_challenge(address, network, signed_challenge, message) { + const ptr0 = passStringToWasm0(address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(signed_challenge, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passArray8ToWasm0(message, wasm.__wbindgen_malloc); + const len2 = WASM_VECTOR_LEN; + const ret = wasm.verify_challenge(ptr0, len0, network, ptr1, len1, ptr2, len2); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return ret[0] !== 0; +} +exports.verify_challenge = verify_challenge; + /** - * The token supply of a specific token, set on issuance - * @enum {0 | 1 | 2} + * Given a digital signature, a public key and a message. Verify that + * the signature is produced by signing the message with the private key + * that derived the given public key. + * Note that this function is used for verifying messages related to spending, + * such as transaction input witness. + * @param {Uint8Array} public_key + * @param {Uint8Array} signature + * @param {Uint8Array} message + * @returns {boolean} */ -module.exports.TotalSupply = Object.freeze({ - /** - * Can be issued with no limit, but then can be locked to have a fixed supply. - */ - Lockable: 0, "0": "Lockable", - /** - * Unlimited supply, no limits except for numeric limits due to u128 - */ - Unlimited: 1, "1": "Unlimited", - /** - * On issuance, the total number of coins is fixed - */ - Fixed: 2, "2": "Fixed", -}); +function verify_signature_for_spending(public_key, signature, message) { + const ptr0 = passArray8ToWasm0(public_key, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(signature, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passArray8ToWasm0(message, wasm.__wbindgen_malloc); + const len2 = WASM_VECTOR_LEN; + const ret = wasm.verify_signature_for_spending(ptr0, len0, ptr1, len1, ptr2, len2); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return ret[0] !== 0; +} +exports.verify_signature_for_spending = verify_signature_for_spending; + +/** + * Verify a signed transaction intent. + * + * Parameters: + * `expected_signed_message` - the message that is supposed to be signed; this must have been + * produced by `make_transaction_intent_message_to_sign`. + * `encoded_signed_intent` - the signed transaction intent produced by `encode_signed_transaction_intent`. + * `input_destinations` - an array of addresses (strings), corresponding to the transaction's input destinations + * (note that this function treats "pub key" and "pub key hash" addresses interchangeably, so it's ok to pass + * one instead of the other). + * `network` - the network being used (needed to decode the addresses). + * @param {Uint8Array} expected_signed_message + * @param {Uint8Array} encoded_signed_intent + * @param {string[]} input_destinations + * @param {Network} network + */ +function verify_transaction_intent(expected_signed_message, encoded_signed_intent, input_destinations, network) { + const ptr0 = passArray8ToWasm0(expected_signed_message, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(encoded_signed_intent, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passArrayJsValueToWasm0(input_destinations, wasm.__wbindgen_malloc); + const len2 = WASM_VECTOR_LEN; + const ret = wasm.verify_transaction_intent(ptr0, len0, ptr1, len1, ptr2, len2, network); + if (ret[1]) { + throw takeFromExternrefTable0(ret[0]); + } +} +exports.verify_transaction_intent = verify_transaction_intent; + +function __wbg_get_imports() { + const import0 = { + __proto__: null, + __wbg___wbindgen_debug_string_0bc8482c6e3508ae: function(arg0, arg1) { + const ret = debugString(arg1); + const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true); + getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true); + }, + __wbg___wbindgen_is_undefined_9e4d92534c42d778: function(arg0) { + const ret = arg0 === undefined; + return ret; + }, + __wbg___wbindgen_string_get_72fb696202c56729: function(arg0, arg1) { + const obj = arg1; + const ret = typeof(obj) === 'string' ? obj : undefined; + var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + var len1 = WASM_VECTOR_LEN; + getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true); + getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true); + }, + __wbg___wbindgen_throw_be289d5034ed271b: function(arg0, arg1) { + throw new Error(getStringFromWasm0(arg0, arg1)); + }, + __wbg_getRandomValues_2a91986308c74a93: function() { return handleError(function (arg0, arg1) { + globalThis.crypto.getRandomValues(getArrayU8FromWasm0(arg0, arg1)); + }, arguments); }, + __wbg_length_32ed9a279acd054c: function(arg0) { + const ret = arg0.length; + return ret; + }, + __wbg_parse_708461a1feddfb38: function() { return handleError(function (arg0, arg1) { + const ret = JSON.parse(getStringFromWasm0(arg0, arg1)); + return ret; + }, arguments); }, + __wbg_prototypesetcall_bdcdcc5842e4d77d: function(arg0, arg1, arg2) { + Uint8Array.prototype.set.call(getArrayU8FromWasm0(arg0, arg1), arg2); + }, + __wbg_stringify_8d1cc6ff383e8bae: function() { return handleError(function (arg0) { + const ret = JSON.stringify(arg0); + return ret; + }, arguments); }, + __wbindgen_cast_0000000000000001: function(arg0, arg1) { + // Cast intrinsic for `Ref(String) -> Externref`. + const ret = getStringFromWasm0(arg0, arg1); + return ret; + }, + __wbindgen_init_externref_table: function() { + const table = wasm.__wbindgen_externrefs; + const offset = table.grow(4); + table.set(0, undefined); + table.set(offset + 0, undefined); + table.set(offset + 1, null); + table.set(offset + 2, true); + table.set(offset + 3, false); + }, + }; + return { + __proto__: null, + "./wasm_wrappers_bg.js": import0, + }; +} const AmountFinalization = (typeof FinalizationRegistry === 'undefined') ? { register: () => {}, unregister: () => {} } : new FinalizationRegistry(ptr => wasm.__wbg_amount_free(ptr >>> 0, 1)); -/** - * Amount type abstraction. The amount type is stored in a string - * since JavaScript number type cannot fit 128-bit integers. - * The amount is given as an integer in units of "atoms". - * Atoms are the smallest, indivisible amount of a coin or token. - */ -class Amount { - static __wrap(ptr) { - ptr = ptr >>> 0; - const obj = Object.create(Amount.prototype); - obj.__wbg_ptr = ptr; - AmountFinalization.register(obj, obj.__wbg_ptr, obj); - return obj; - } +function addToExternrefTable0(obj) { + const idx = wasm.__externref_table_alloc(); + wasm.__wbindgen_externrefs.set(idx, obj); + return idx; +} - __destroy_into_raw() { - const ptr = this.__wbg_ptr; - this.__wbg_ptr = 0; - AmountFinalization.unregister(this); - return ptr; +function _assertClass(instance, klass) { + if (!(instance instanceof klass)) { + throw new Error(`expected instance of ${klass.name}`); } +} - free() { - const ptr = this.__destroy_into_raw(); - wasm.__wbg_amount_free(ptr, 0); +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})`; + } } - /** - * @param {string} atoms - * @returns {Amount} - */ - static from_atoms(atoms) { - const ptr0 = passStringToWasm0(atoms, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.amount_from_atoms(ptr0, len0); - return Amount.__wrap(ret); + if (type == 'function') { + const name = val.name; + if (typeof name == 'string' && name.length > 0) { + return `Function(${name})`; + } else { + return 'Function'; + } } - /** - * @returns {string} - */ - atoms() { - let deferred1_0; - let deferred1_1; + // 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 { - const ptr = this.__destroy_into_raw(); - const ret = wasm.amount_atoms(ptr); - deferred1_0 = ret[0]; - deferred1_1 = ret[1]; - return getStringFromWasm0(ret[0], ret[1]); - } finally { - wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); + 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; +} + +function getArrayU8FromWasm0(ptr, len) { + ptr = ptr >>> 0; + return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len); +} + +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; } -module.exports.Amount = Amount; - -module.exports.__wbg_buffer_609cc3eee51ed158 = function(arg0) { - const ret = arg0.buffer; - return ret; -}; - -module.exports.__wbg_call_672a4d21634d4a24 = function() { return handleError(function (arg0, arg1) { - const ret = arg0.call(arg1); - return ret; -}, arguments) }; - -module.exports.__wbg_call_7cccdd69e0791ae2 = function() { return handleError(function (arg0, arg1, arg2) { - const ret = arg0.call(arg1, arg2); - return ret; -}, arguments) }; - -module.exports.__wbg_crypto_ed58b8e10a292839 = function(arg0) { - const ret = arg0.crypto; - return ret; -}; - -module.exports.__wbg_getRandomValues_bcb4912f16000dc4 = function() { return handleError(function (arg0, arg1) { - arg0.getRandomValues(arg1); -}, arguments) }; - -module.exports.__wbg_msCrypto_0a36e2ec3a343d26 = function(arg0) { - const ret = arg0.msCrypto; - return ret; -}; - -module.exports.__wbg_new_a12002a7f91c75be = function(arg0) { - const ret = new Uint8Array(arg0); - return ret; -}; - -module.exports.__wbg_newnoargs_105ed471475aaf50 = function(arg0, arg1) { - const ret = new Function(getStringFromWasm0(arg0, arg1)); - return ret; -}; - -module.exports.__wbg_newwithbyteoffsetandlength_d97e637ebe145a9a = function(arg0, arg1, arg2) { - const ret = new Uint8Array(arg0, arg1 >>> 0, arg2 >>> 0); - return ret; -}; - -module.exports.__wbg_newwithlength_a381634e90c276d4 = function(arg0) { - const ret = new Uint8Array(arg0 >>> 0); - return ret; -}; - -module.exports.__wbg_node_02999533c4ea02e3 = function(arg0) { - const ret = arg0.node; - return ret; -}; - -module.exports.__wbg_process_5c1d670bc53614b8 = function(arg0) { - const ret = arg0.process; - return ret; -}; - -module.exports.__wbg_randomFillSync_ab2cfe79ebbf2740 = function() { return handleError(function (arg0, arg1) { - arg0.randomFillSync(arg1); -}, arguments) }; - -module.exports.__wbg_require_79b1e9274cde3c87 = function() { return handleError(function () { - const ret = module.require; - return ret; -}, arguments) }; - -module.exports.__wbg_set_65595bdd868b3009 = function(arg0, arg1, arg2) { - arg0.set(arg1, arg2 >>> 0); -}; - -module.exports.__wbg_static_accessor_GLOBAL_88a902d13a557d07 = function() { - const ret = typeof global === 'undefined' ? null : global; - return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); -}; - -module.exports.__wbg_static_accessor_GLOBAL_THIS_56578be7e9f832b0 = function() { - const ret = typeof globalThis === 'undefined' ? null : globalThis; - return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); -}; - -module.exports.__wbg_static_accessor_SELF_37c5d418e4bf5819 = function() { - const ret = typeof self === 'undefined' ? null : self; - return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); -}; - -module.exports.__wbg_static_accessor_WINDOW_5de37043a91a9c40 = function() { - const ret = typeof window === 'undefined' ? null : window; - return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); -}; - -module.exports.__wbg_stringify_f7ed6987935b4a24 = function() { return handleError(function (arg0) { - const ret = JSON.stringify(arg0); - return ret; -}, arguments) }; - -module.exports.__wbg_subarray_aa9065fa9dc5df96 = function(arg0, arg1, arg2) { - const ret = arg0.subarray(arg1 >>> 0, arg2 >>> 0); - return ret; -}; - -module.exports.__wbg_versions_c71aa1626a93e0a1 = function(arg0) { - const ret = arg0.versions; - return ret; -}; - -module.exports.__wbindgen_init_externref_table = function() { - const table = wasm.__wbindgen_export_2; - const offset = table.grow(4); - table.set(0, undefined); - table.set(offset + 0, undefined); - table.set(offset + 1, null); - table.set(offset + 2, true); - table.set(offset + 3, false); - ; -}; - -module.exports.__wbindgen_is_function = function(arg0) { - const ret = typeof(arg0) === 'function'; - return ret; -}; - -module.exports.__wbindgen_is_object = function(arg0) { - const val = arg0; - const ret = typeof(val) === 'object' && val !== null; - return ret; -}; - -module.exports.__wbindgen_is_string = function(arg0) { - const ret = typeof(arg0) === 'string'; - return ret; -}; - -module.exports.__wbindgen_is_undefined = function(arg0) { - const ret = arg0 === undefined; - return ret; -}; - -module.exports.__wbindgen_memory = function() { - const ret = wasm.memory; - return ret; -}; - -module.exports.__wbindgen_string_get = function(arg0, arg1) { - const obj = arg1; - const ret = typeof(obj) === 'string' ? obj : undefined; - var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - var len1 = WASM_VECTOR_LEN; - getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true); - getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true); -}; -module.exports.__wbindgen_string_new = function(arg0, arg1) { - const ret = getStringFromWasm0(arg0, arg1); - return ret; -}; +function getStringFromWasm0(ptr, len) { + ptr = ptr >>> 0; + return decodeText(ptr, len); +} -module.exports.__wbindgen_throw = function(arg0, arg1) { - throw new Error(getStringFromWasm0(arg0, arg1)); -}; +let cachedUint8ArrayMemory0 = null; +function getUint8ArrayMemory0() { + if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) { + cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer); + } + return cachedUint8ArrayMemory0; +} -const path = require('path').join(__dirname, 'wasm_wrappers_bg.wasm'); -const bytes = require('fs').readFileSync(path); +function handleError(f, args) { + try { + return f.apply(this, args); + } catch (e) { + const idx = addToExternrefTable0(e); + wasm.__wbindgen_exn_store(idx); + } +} -const wasmModule = new WebAssembly.Module(bytes); -const wasmInstance = new WebAssembly.Instance(wasmModule, imports); -wasm = wasmInstance.exports; -module.exports.__wasm = wasm; +function isLikeNone(x) { + return x === undefined || x === null; +} -wasm.__wbindgen_start(); +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 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; +} + +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 = cachedTextEncoder.encodeInto(arg, view); + + offset += ret.written; + ptr = realloc(ptr, len, offset, 1) >>> 0; + } + + WASM_VECTOR_LEN = offset; + return ptr; +} + +function takeFromExternrefTable0(idx) { + const value = wasm.__wbindgen_externrefs.get(idx); + wasm.__externref_table_dealloc(idx); + return value; +} + +let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); +cachedTextDecoder.decode(); +function decodeText(ptr, len) { + return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len)); +} + +const cachedTextEncoder = new TextEncoder(); + +if (!('encodeInto' in cachedTextEncoder)) { + cachedTextEncoder.encodeInto = function (arg, view) { + const buf = cachedTextEncoder.encode(arg); + view.set(buf); + return { + read: arg.length, + written: buf.length + }; + }; +} + +let WASM_VECTOR_LEN = 0; + +const wasmPath = `${__dirname}/wasm_wrappers_bg.wasm`; +const wasmBytes = require('fs').readFileSync(wasmPath); +const wasmModule = new WebAssembly.Module(wasmBytes); +const wasm = new WebAssembly.Instance(wasmModule, __wbg_get_imports()).exports; +wasm.__wbindgen_start(); diff --git a/packages/sdk/tests/__mocks__/pkg-node/wasm_wrappers_bg.wasm b/packages/sdk/tests/__mocks__/pkg-node/wasm_wrappers_bg.wasm index 343bfab..d8c6052 100644 Binary files a/packages/sdk/tests/__mocks__/pkg-node/wasm_wrappers_bg.wasm and b/packages/sdk/tests/__mocks__/pkg-node/wasm_wrappers_bg.wasm differ diff --git a/packages/sdk/tests/__mocks__/pkg-node/wasm_wrappers_bg.wasm.d.ts b/packages/sdk/tests/__mocks__/pkg-node/wasm_wrappers_bg.wasm.d.ts index eab52a2..9363a87 100644 --- a/packages/sdk/tests/__mocks__/pkg-node/wasm_wrappers_bg.wasm.d.ts +++ b/packages/sdk/tests/__mocks__/pkg-node/wasm_wrappers_bg.wasm.d.ts @@ -1,84 +1,97 @@ /* tslint:disable */ /* eslint-disable */ export const memory: WebAssembly.Memory; -export const __wbg_amount_free: (a: number, b: number) => void; -export const amount_from_atoms: (a: number, b: number) => number; -export const amount_atoms: (a: number) => [number, number]; +export const data_deposit_fee: (a: bigint, b: number) => number; +export const decode_partially_signed_transaction_to_js: (a: number, b: number, c: number) => [number, number, number]; +export const decode_signed_transaction_to_js: (a: number, b: number, c: number) => [number, number, number]; +export const decode_transaction_to_js: (a: number, b: number, c: number) => [number, number, number]; +export const effective_pool_balance: (a: number, b: number, c: number) => [number, number, number]; +export const encode_destination: (a: number, b: number, c: number) => [number, number, number, number]; +export const encode_lock_for_block_count: (a: bigint) => [number, number]; +export const encode_lock_for_seconds: (a: bigint) => [number, number]; +export const encode_lock_until_height: (a: bigint) => [number, number]; +export const encode_lock_until_time: (a: bigint) => [number, number]; +export const encode_multisig_challenge: (a: number, b: number, c: number, d: number) => [number, number, number, number]; export const encode_outpoint_source_id: (a: number, b: number, c: number) => [number, number]; -export const make_private_key: () => [number, number]; +export const encode_partially_signed_transaction: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: any, l: number) => [number, number, number, number]; +export const encode_signed_transaction: (a: number, b: number, c: number, d: number) => [number, number, number, number]; +export const encode_signed_transaction_intent: (a: number, b: number, c: number, d: number) => [number, number, number, number]; +export const encode_stake_pool_data: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number) => [number, number, number, number]; +export const encode_transaction: (a: number, b: number, c: number, d: number, e: bigint) => [number, number, number, number]; +export const encode_witness: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: any, l: bigint, m: number) => [number, number, number, number]; +export const encode_witness_htlc_refund_multisig: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number, n: any, o: bigint, p: number) => [number, number, number, number]; +export const encode_witness_htlc_refund_single_sig: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: any, l: bigint, m: number) => [number, number, number, number]; +export const encode_witness_htlc_spend: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: any, n: bigint, o: number) => [number, number, number, number]; +export const encode_witness_no_signature: () => [number, number]; +export const estimate_transaction_size: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number]; +export const extended_public_key_from_extended_private_key: (a: number, b: number) => [number, number, number, number]; +export const extract_htlc_secret: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number]; +export const fungible_token_issuance_fee: (a: bigint, b: number) => number; +export const get_delegation_id: (a: number, b: number, c: number) => [number, number, number, number]; +export const get_order_id: (a: number, b: number, c: number) => [number, number, number, number]; +export const get_pool_id: (a: number, b: number, c: number) => [number, number, number, number]; +export const get_token_id: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; +export const get_transaction_id: (a: number, b: number, c: number) => [number, number, number, number]; +export const make_change_address: (a: number, b: number, c: number) => [number, number, number, number]; +export const make_change_address_public_key: (a: number, b: number, c: number) => [number, number, number, number]; export const make_default_account_privkey: (a: number, b: number, c: number) => [number, number, number, number]; +export const make_private_key: () => [number, number]; export const make_receiving_address: (a: number, b: number, c: number) => [number, number, number, number]; -export const make_change_address: (a: number, b: number, c: number) => [number, number, number, number]; +export const make_receiving_address_public_key: (a: number, b: number, c: number) => [number, number, number, number]; +export const make_transaction_intent_message_to_sign: (a: number, b: number, c: number, d: number) => [number, number, number, number]; +export const multisig_challenge_to_address: (a: number, b: number, c: number) => [number, number, number, number]; +export const nft_issuance_fee: (a: bigint, b: number) => number; export const pubkey_to_pubkeyhash_address: (a: number, b: number, c: number) => [number, number, number, number]; export const public_key_from_private_key: (a: number, b: number) => [number, number, number, number]; -export const extended_public_key_from_extended_private_key: (a: number, b: number) => [number, number, number, number]; -export const make_receiving_address_public_key: (a: number, b: number, c: number) => [number, number, number, number]; -export const make_change_address_public_key: (a: number, b: number, c: number) => [number, number, number, number]; -export const sign_message_for_spending: (a: number, b: number, c: number, d: number) => [number, number, number, number]; -export const verify_signature_for_spending: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number]; export const sign_challenge: (a: number, b: number, c: number, d: number) => [number, number, number, number]; +export const sign_message_for_spending: (a: number, b: number, c: number, d: number) => [number, number, number, number]; +export const staking_pool_spend_maturity_block_count: (a: bigint, b: number) => bigint; +export const token_change_authority_fee: (a: bigint, b: number) => number; +export const token_freeze_fee: (a: bigint, b: number) => number; +export const token_supply_change_fee: (a: bigint, b: number) => number; export const verify_challenge: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number]; -export const make_transaction_intent_message_to_sign: (a: number, b: number, c: number, d: number) => [number, number, number, number]; -export const encode_signed_transaction_intent: (a: number, b: number, c: any) => [number, number, number, number]; +export const verify_signature_for_spending: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number]; export const verify_transaction_intent: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number]; -export const encode_output_transfer: (a: number, b: number, c: number, d: number) => [number, number, number, number]; -export const encode_output_token_transfer: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number]; -export const staking_pool_spend_maturity_block_count: (a: bigint, b: number) => bigint; -export const encode_lock_for_block_count: (a: bigint) => [number, number]; -export const encode_lock_for_seconds: (a: bigint) => [number, number]; -export const encode_lock_until_time: (a: bigint) => [number, number]; -export const encode_lock_until_height: (a: bigint) => [number, number]; -export const encode_output_lock_then_transfer: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number]; -export const encode_output_token_lock_then_transfer: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => [number, number, number, number]; +export const encode_create_order_output: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => [number, number, number, number]; export const encode_output_coin_burn: (a: number) => [number, number, number, number]; -export const encode_output_token_burn: (a: number, b: number, c: number, d: number) => [number, number, number, number]; export const encode_output_create_delegation: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number]; -export const encode_output_delegate_staking: (a: number, b: number, c: number, d: number) => [number, number, number, number]; -export const encode_stake_pool_data: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number) => [number, number, number, number]; export const encode_output_create_stake_pool: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number]; -export const fungible_token_issuance_fee: (a: bigint, b: number) => number; -export const nft_issuance_fee: (a: bigint, b: number) => number; -export const token_supply_change_fee: (a: bigint, b: number) => number; -export const token_freeze_fee: (a: bigint, b: number) => number; -export const token_change_authority_fee: (a: bigint, b: number) => number; -export const encode_output_issue_fungible_token: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: bigint, l: number) => [number, number, number, number]; -export const get_token_id: (a: number, b: number, c: number) => [number, number, number, number]; -export const encode_output_issue_nft: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number, n: number, o: number, p: number, q: number, r: number, s: number, t: number, u: bigint, v: number) => [number, number, number, number]; export const encode_output_data_deposit: (a: number, b: number) => [number, number, number, number]; -export const data_deposit_fee: (a: bigint, b: number) => number; +export const encode_output_delegate_staking: (a: number, b: number, c: number, d: number) => [number, number, number, number]; export const encode_output_htlc: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number) => [number, number, number, number]; -export const extract_htlc_secret: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number]; -export const encode_input_for_utxo: (a: number, b: number, c: number) => [number, number, number, number]; -export const encode_input_for_withdraw_from_delegation: (a: number, b: number, c: number, d: bigint, e: number) => [number, number, number, number]; -export const estimate_transaction_size: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number]; -export const encode_transaction: (a: number, b: number, c: number, d: number, e: bigint) => [number, number, number, number]; -export const encode_witness_no_signature: () => [number, number]; -export const encode_witness: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number) => [number, number, number, number]; -export const encode_witness_htlc_secret: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number) => [number, number, number, number]; -export const encode_multisig_challenge: (a: number, b: number, c: number, d: number) => [number, number, number, number]; -export const encode_witness_htlc_multisig: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number, n: number) => [number, number, number, number]; -export const encode_signed_transaction: (a: number, b: number, c: number, d: number) => [number, number, number, number]; -export const get_transaction_id: (a: number, b: number, c: number) => [number, number, number, number]; -export const effective_pool_balance: (a: number, b: number, c: number) => [number, number, number]; -export const encode_input_for_mint_tokens: (a: number, b: number, c: number, d: bigint, e: number) => [number, number, number, number]; -export const encode_input_for_unmint_tokens: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; -export const encode_input_for_lock_token_supply: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; -export const encode_input_for_freeze_token: (a: number, b: number, c: number, d: bigint, e: number) => [number, number, number, number]; -export const encode_input_for_unfreeze_token: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; +export const encode_output_issue_fungible_token: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: bigint, l: number) => [number, number, number, number]; +export const encode_output_issue_nft: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number, n: number, o: number, p: number, q: number, r: number, s: number, t: number, u: bigint, v: number) => [number, number, number, number]; +export const encode_output_lock_then_transfer: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number]; +export const encode_output_produce_block_from_stake: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number]; +export const encode_output_token_burn: (a: number, b: number, c: number, d: number) => [number, number, number, number]; +export const encode_output_token_lock_then_transfer: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => [number, number, number, number]; +export const encode_output_token_transfer: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number]; +export const encode_output_transfer: (a: number, b: number, c: number, d: number) => [number, number, number, number]; +export const __wbg_amount_free: (a: number, b: number) => void; +export const amount_atoms: (a: number) => [number, number]; +export const amount_from_atoms: (a: number, b: number) => number; export const encode_input_for_change_token_authority: (a: number, b: number, c: number, d: number, e: bigint, f: number) => [number, number, number, number]; export const encode_input_for_change_token_metadata_uri: (a: number, b: number, c: number, d: number, e: bigint, f: number) => [number, number, number, number]; -export const encode_create_order_output: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => [number, number, number, number]; -export const encode_input_for_fill_order: (a: number, b: number, c: number, d: number, e: number, f: bigint, g: number) => [number, number, number, number]; -export const encode_input_for_conclude_order: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; +export const encode_input_for_conclude_order: (a: number, b: number, c: bigint, d: bigint, e: number) => [number, number, number, number]; +export const encode_input_for_fill_order: (a: number, b: number, c: number, d: number, e: number, f: bigint, g: bigint, h: number) => [number, number, number, number]; +export const encode_input_for_freeze_order: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; +export const encode_input_for_freeze_token: (a: number, b: number, c: number, d: bigint, e: number) => [number, number, number, number]; +export const encode_input_for_lock_token_supply: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; +export const encode_input_for_mint_tokens: (a: number, b: number, c: number, d: bigint, e: number) => [number, number, number, number]; +export const encode_input_for_unfreeze_token: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; +export const encode_input_for_unmint_tokens: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; +export const encode_input_for_utxo: (a: number, b: number, c: number) => [number, number, number, number]; +export const encode_input_for_withdraw_from_delegation: (a: number, b: number, c: number, d: bigint, e: number) => [number, number, number, number]; +export const internal_verify_witness: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: any, l: bigint, m: number) => [number, number]; export const rustsecp256k1_v0_10_0_context_create: (a: number) => number; export const rustsecp256k1_v0_10_0_context_destroy: (a: number) => void; -export const rustsecp256k1_v0_10_0_default_illegal_callback_fn: (a: number, b: number) => void; export const rustsecp256k1_v0_10_0_default_error_callback_fn: (a: number, b: number) => void; -export const __wbindgen_exn_store: (a: number) => void; -export const __externref_table_alloc: () => number; -export const __wbindgen_export_2: WebAssembly.Table; +export const rustsecp256k1_v0_10_0_default_illegal_callback_fn: (a: number, b: number) => void; export const __wbindgen_malloc: (a: number, b: number) => number; export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number; +export const __wbindgen_exn_store: (a: number) => void; +export const __externref_table_alloc: () => number; +export const __wbindgen_externrefs: WebAssembly.Table; export const __wbindgen_free: (a: number, b: number, c: number) => void; export const __externref_table_dealloc: (a: number) => void; export const __wbindgen_start: () => void; diff --git a/packages/sdk/tests/transaction-builder.test.ts b/packages/sdk/tests/transaction-builder.test.ts index 3ebe3c9..67d3a71 100644 --- a/packages/sdk/tests/transaction-builder.test.ts +++ b/packages/sdk/tests/transaction-builder.test.ts @@ -2,6 +2,9 @@ import { Transaction, } from '../src/transaction'; +import * as wasmLib from '@mintlayer/wasm-lib'; + + const CHANGE_ADDR = 'tmt1qycauu4rc92v80vpjrtkqjv2utr7jl5ygve28sdt'; const RECIPIENT = 'tmt1q9mfg7d6ul2nt5yhmm7l7r6wwyqkd822rymr83uc'; const COIN_UTXO = { @@ -220,3 +223,44 @@ test('nft transfer produces no token change and a fee', () => { expect(nftOut).toBeDefined(); expect(nftOut.value.amount.atoms).toBe('1'); }); + +test('fromHex restores a coin transfer transaction from predefined string', () => { + const t_hex = + '0100040000cfbf6a782377d1936ae404d49d4862267c72e365c65668cb07d5c78f1e1f9b7b010000000800000b0038d360ba010137d69c6f94355b7b37f4c3f899e0dd2f2765fb3b00000f4007ee39c08b0301d26a1ca86df0108c4fde32ec77ddf5eaedac5906'; + + const restored = Transaction.fromHEX(t_hex); + + expect(restored.transaction_id).toBe('7b0272c04824ce1bba10495e8c1de135761687b82cf860d2baf06516a05ff776'); +}); + +test('fromHex restores a coin transfer transaction', () => { + const transaction = new Transaction() + .setNetwork('testnet') + .setChangeAddress(CHANGE_ADDR) + .withUTXO(COIN_UTXO) + .addOutput(new Transaction().transfer(RECIPIENT, '10')) + .build(); + + const hex = transaction.hex(); + + const restored = Transaction.fromHEX(hex).enrichUtxo(COIN_UTXO); + + expect(restored).toBeDefined(); + expect(restored.hex()).toBe(hex); + + const originalJson = transaction.json(); + const restoredJson = restored.json(); + + expect(restoredJson.id).toBe(originalJson.id); + expect(restoredJson.inputs).toEqual(originalJson.inputs); + expect(restoredJson.outputs).toEqual(originalJson.outputs); + expect(restored.getFee()).toEqual(originalJson.fee); +}); + +test('fromHex rejects invalid hex', () => { + expect(() => Transaction.fromHEX('deadbeef')).toThrow(); +}); + +test('fromHex rejects malformed hex', () => { + expect(() => Transaction.fromHEX('not-a-hex')).toThrow(); +}); diff --git a/packages/wasm-lib/package.json b/packages/wasm-lib/package.json index 5b7417f..d27f552 100644 --- a/packages/wasm-lib/package.json +++ b/packages/wasm-lib/package.json @@ -1,6 +1,6 @@ { "name": "@mintlayer/wasm-lib", - "version": "0.1.0", + "version": "1.4.0", "description": "A WebAssembly library compiled from Rust for browser and Node.js", "license": "MIT", "main": "wasm_wrappers.js", diff --git a/packages/wasm-lib/wasm_wrappers.d.ts b/packages/wasm-lib/wasm_wrappers.d.ts index e385f48..1557162 100644 --- a/packages/wasm-lib/wasm_wrappers.d.ts +++ b/packages/wasm-lib/wasm_wrappers.d.ts @@ -1,271 +1,497 @@ /* tslint:disable */ /* eslint-disable */ /** - * A utxo can either come from a transaction or a block reward. - * Given a source id, whether from a block reward or transaction, this function - * takes a generic id with it, and returns serialized binary data of the id - * with the given source id. + * Additional information for a pool. */ -export function encode_outpoint_source_id(id: Uint8Array, source: SourceId): Uint8Array; +export interface PoolAdditionalInfo { + staker_balance: SimpleAmount; +} + /** - * Generates a new, random private key from entropy + * Additional information for a transaction. */ -export function make_private_key(): Uint8Array; +export interface TxAdditionalInfo { + pool_info: Record; + order_info: Record; +} + /** - * Create the default account's extended private key for a given mnemonic - * derivation path: 44'/mintlayer_coin_type'/0' + * Additional information for an order. */ -export function make_default_account_privkey(mnemonic: string, network: Network): Uint8Array; +export interface OrderAdditionalInfo { + initially_asked: SimpleCurrencyAmount; + initially_given: SimpleCurrencyAmount; + ask_balance: SimpleAmount; + give_balance: SimpleAmount; +} + /** - * From an extended private key create a receiving private key for a given key index - * derivation path: current_derivation_path/0/key_index + * An alternative representation of `Amount`. */ -export function make_receiving_address(private_key_bytes: Uint8Array, key_index: number): Uint8Array; +export interface SimpleAmount { + atoms: string; +} + /** - * From an extended private key create a change private key for a given key index - * derivation path: current_derivation_path/1/key_index + * An amount of coins or some token, */ -export function make_change_address(private_key_bytes: Uint8Array, key_index: number): Uint8Array; +export type SimpleCurrencyAmount = { coins: SimpleAmount } | { tokens: SimpleTokenAmount }; + /** - * Given a public key (as bytes) and a network type (mainnet, testnet, etc), - * return the address public key hash from that public key as an address + * An amount of some token. */ -export function pubkey_to_pubkeyhash_address(public_key_bytes: Uint8Array, network: Network): string; +export interface SimpleTokenAmount { + token_id: string; + amount: SimpleAmount; +} + + /** - * Given a private key, as bytes, return the bytes of the corresponding public key + * Amount type abstraction. The amount type is stored in a string + * since JavaScript number type cannot fit 128-bit integers. + * The amount is given as an integer in units of "atoms". + * Atoms are the smallest, indivisible amount of a coin or token. */ -export function public_key_from_private_key(private_key: Uint8Array): Uint8Array; +export class Amount { + private constructor(); + free(): void; + [Symbol.dispose](): void; + atoms(): string; + static from_atoms(atoms: string): Amount; +} + /** - * Return the extended public key from an extended private key + * Indicates whether a token can be frozen */ -export function extended_public_key_from_extended_private_key(private_key_bytes: Uint8Array): Uint8Array; +export enum FreezableToken { + No = 0, + Yes = 1, +} + /** - * From an extended public key create a receiving public key for a given key index - * derivation path: current_derivation_path/0/key_index + * The network, for which an operation to be done. Mainnet, testnet, etc. */ -export function make_receiving_address_public_key(extended_public_key_bytes: Uint8Array, key_index: number): Uint8Array; +export enum Network { + Mainnet = 0, + Testnet = 1, + Regtest = 2, + Signet = 3, +} + /** - * From an extended public key create a change public key for a given key index - * derivation path: current_derivation_path/1/key_index + * The part of the transaction that will be committed in the signature. Similar to bitcoin's sighash. */ -export function make_change_address_public_key(extended_public_key_bytes: Uint8Array, key_index: number): Uint8Array; +export enum SignatureHashType { + ALL = 0, + NONE = 1, + SINGLE = 2, + ANYONECANPAY = 3, +} + /** - * Given a message and a private key, sign the message with the given private key - * This kind of signature is to be used when signing spend requests, such as transaction - * input witness. + * A utxo can either come from a transaction or a block reward. This enum signifies that. */ -export function sign_message_for_spending(private_key: Uint8Array, message: Uint8Array): Uint8Array; +export enum SourceId { + Transaction = 0, + BlockReward = 1, +} + /** - * Given a digital signature, a public key and a message. Verify that - * the signature is produced by signing the message with the private key - * that derived the given public key. - * Note that this function is used for verifying messages related to spending, - * such as transaction input witness. + * Indicates whether a token can be unfrozen once frozen */ -export function verify_signature_for_spending(public_key: Uint8Array, signature: Uint8Array, message: Uint8Array): boolean; +export enum TokenUnfreezable { + No = 0, + Yes = 1, +} + /** - * Given a message and a private key, create and sign a challenge with the given private key. - * This kind of signature is to be used when signing challenges. + * The token supply of a specific token, set on issuance */ -export function sign_challenge(private_key: Uint8Array, message: Uint8Array): Uint8Array; +export enum TotalSupply { + /** + * Can be issued with no limit, but then can be locked to have a fixed supply. + */ + Lockable = 0, + /** + * Unlimited supply, no limits except for numeric limits due to u128 + */ + Unlimited = 1, + /** + * On issuance, the total number of coins is fixed + */ + Fixed = 2, +} + /** - * Given a signed challenge, an address and a message, verify that - * the signature is produced by signing the message with the private key - * that derived the given public key. - * This function is used for verifying messages-related challenges. - * - * Note: for signatures that were created by `sign_challenge`, the provided address must be - * a 'pubkeyhash' address. - * - * Note: currently this function never returns `false` - it either returns `true` or fails with an error. + * Returns the fee that needs to be paid by a transaction for issuing a data deposit */ -export function verify_challenge(address: string, network: Network, signed_challenge: Uint8Array, message: Uint8Array): boolean; +export function data_deposit_fee(current_block_height: bigint, network: Network): Amount; + /** - * Return the message that has to be signed to produce a signed transaction intent. + * Decodes a partially signed transaction from its binary encoding into a JavaScript object. */ -export function make_transaction_intent_message_to_sign(intent: string, transaction_id: string): Uint8Array; +export function decode_partially_signed_transaction_to_js(transaction: Uint8Array, network: Network): any; + /** - * Return a `SignedTransactionIntent` object as bytes given the message and encoded signatures. + * Decodes a signed transaction from its binary encoding into a JavaScript object. + */ +export function decode_signed_transaction_to_js(transaction: Uint8Array, network: Network): any; + +/** + * Decodes an unsigned transaction from its binary encoding into a JavaScript object. + */ +export function decode_transaction_to_js(transaction: Uint8Array, network: Network): any; + +/** + * Calculate the "effective balance" of a pool, given the total pool balance and pledge by the pool owner/staker. + * The effective balance is how the influence of a pool is calculated due to its balance. + */ +export function effective_pool_balance(network: Network, pledge_amount: Amount, pool_balance: Amount): Amount; + +/** + * Given ask and give amounts and a conclude key create output that creates an order. * - * Note: to produce a valid signed intent one is expected to sign the corresponding message by private keys - * corresponding to each input of the transaction. + * 'ask_token_id': the parameter represents a Token if it's Some and coins otherwise. + * 'give_token_id': the parameter represents a Token if it's Some and coins otherwise. + */ +export function encode_create_order_output(ask_amount: Amount, ask_token_id: string | null | undefined, give_amount: Amount, give_token_id: string | null | undefined, conclude_address: string, network: Network): Uint8Array; + +/** + * Convert the specified string address into a Destination object, encoded as bytes. + */ +export function encode_destination(address: string, network: Network): Uint8Array; + +/** + * Given a token_id, new authority destination and nonce return an encoded change token authority input + */ +export function encode_input_for_change_token_authority(token_id: string, new_authority: string, nonce: bigint, network: Network): Uint8Array; + +/** + * Given a token_id, new metadata uri and nonce return an encoded change token metadata uri input + */ +export function encode_input_for_change_token_metadata_uri(token_id: string, new_metadata_uri: string, nonce: bigint, network: Network): Uint8Array; + +/** + * Given an order id create an input that concludes the order. * - * Parameters: - * `signed_message` - this must have been produced by `make_transaction_intent_message_to_sign`. - * `signatures` - this should be an array of arrays of bytes, each of them representing an individual signature - * of `signed_message` produced by `sign_challenge` using the private key for the corresponding input destination - * of the transaction. The number of signatures must be equal to the number of inputs in the transaction. + * Note: the nonce is only needed before the orders V1 fork activation. After the fork the nonce is + * ignored and any value can be passed for the parameter. */ -export function encode_signed_transaction_intent(signed_message: Uint8Array, signatures: any): Uint8Array; +export function encode_input_for_conclude_order(order_id: string, nonce: bigint, current_block_height: bigint, network: Network): Uint8Array; + /** - * Verify a signed transaction intent. + * Given an order id and an amount in the order's ask currency, create an input that fills the order. * - * Parameters: - * `expected_signed_message` - the message that is supposed to be signed; this must have been - * produced by `make_transaction_intent_message_to_sign`. - * `encoded_signed_intent` - the signed transaction intent produced by `encode_signed_transaction_intent`. - * `input_destinations` - an array of addresses (strings), corresponding to the transaction's input destinations - * (note that this function treats "pub key" and "pub key hash" addresses interchangeably, so it's ok to pass - * one instead of the other). - * `network` - the network being used (needed to decode the addresses). + * Note: + * 1) The nonce is only needed before the orders V1 fork activation. After the fork the nonce is + * ignored and any value can be passed for the parameter. + * 2) FillOrder inputs should not be signed, i.e. use `encode_witness_no_signature` for the inputs + * instead of `encode_witness`). + * Note that in orders v0 FillOrder inputs can technically have a signature, it's just not checked. + * But in orders V1 we actually require that those inputs don't have signatures. + * Also, in orders V1 the provided destination is always ignored. + */ +export function encode_input_for_fill_order(order_id: string, fill_amount: Amount, destination: string, nonce: bigint, current_block_height: bigint, network: Network): Uint8Array; + +/** + * Given an order id create an input that freezes the order. + * + * Note: order freezing is available only after the orders V1 fork activation. */ -export function verify_transaction_intent(expected_signed_message: Uint8Array, encoded_signed_intent: Uint8Array, input_destinations: string[], network: Network): void; +export function encode_input_for_freeze_order(order_id: string, current_block_height: bigint, network: Network): Uint8Array; + /** - * Given a destination address, an amount and a network type (mainnet, testnet, etc), this function - * creates an output of type Transfer, and returns it as bytes. + * Given a token_id, is token unfreezable and nonce return an encoded freeze token input */ -export function encode_output_transfer(amount: Amount, address: string, network: Network): Uint8Array; +export function encode_input_for_freeze_token(token_id: string, is_token_unfreezable: TokenUnfreezable, nonce: bigint, network: Network): Uint8Array; + /** - * Given a destination address, an amount, token ID (in address form) and a network type (mainnet, testnet, etc), this function - * creates an output of type Transfer for tokens, and returns it as bytes. + * Given a token_id and nonce return an encoded lock_token_supply input */ -export function encode_output_token_transfer(amount: Amount, address: string, token_id: string, network: Network): Uint8Array; +export function encode_input_for_lock_token_supply(token_id: string, nonce: bigint, network: Network): Uint8Array; + /** - * Given the current block height and a network type (mainnet, testnet, etc), - * this function returns the number of blocks, after which a pool that decommissioned, - * will have its funds unlocked and available for spending. - * The current block height information is used in case a network upgrade changed the value. + * Given a token_id, an amount of tokens to mint and nonce return an encoded mint tokens input */ -export function staking_pool_spend_maturity_block_count(current_block_height: bigint, network: Network): bigint; +export function encode_input_for_mint_tokens(token_id: string, amount: Amount, nonce: bigint, network: Network): Uint8Array; + +/** + * Given a token_id and nonce return an encoded unfreeze token input + */ +export function encode_input_for_unfreeze_token(token_id: string, nonce: bigint, network: Network): Uint8Array; + +/** + * Given a token_id and nonce return an encoded unmint tokens input + */ +export function encode_input_for_unmint_tokens(token_id: string, nonce: bigint, network: Network): Uint8Array; + +/** + * Given an output source id as bytes, and an output index, together representing a utxo, + * this function returns the input that puts them together, as bytes. + */ +export function encode_input_for_utxo(outpoint_source_id: Uint8Array, output_index: number): Uint8Array; + +/** + * Given a delegation id, an amount and a network type (mainnet, testnet, etc), this function + * creates an input that withdraws from a delegation. + * A nonce is needed because this spends from an account. The nonce must be in sequence for everything in that account. + */ +export function encode_input_for_withdraw_from_delegation(delegation_id: string, amount: Amount, nonce: bigint, network: Network): Uint8Array; + /** * Given a number of blocks, this function returns the output timelock * which is used in locked outputs to lock an output for a given number of blocks * since that output's transaction is included the blockchain */ export function encode_lock_for_block_count(block_count: bigint): Uint8Array; + /** * Given a number of clock seconds, this function returns the output timelock * which is used in locked outputs to lock an output for a given number of seconds * since that output's transaction is included in the blockchain */ export function encode_lock_for_seconds(total_seconds: bigint): Uint8Array; + +/** + * Given a block height, this function returns the output timelock which is used in + * locked outputs to lock an output until that block height is reached. + */ +export function encode_lock_until_height(block_height: bigint): Uint8Array; + /** * Given a timestamp represented by as unix timestamp, i.e., number of seconds since unix epoch, * this function returns the output timelock which is used in locked outputs to lock an output * until the given timestamp */ export function encode_lock_until_time(timestamp_since_epoch_in_seconds: bigint): Uint8Array; + /** - * Given a block height, this function returns the output timelock which is used in - * locked outputs to lock an output until that block height is reached. - */ -export function encode_lock_until_height(block_height: bigint): Uint8Array; -/** - * Given a valid receiving address, and a locking rule as bytes (available in this file), - * and a network type (mainnet, testnet, etc), this function creates an output of type - * LockThenTransfer with the parameters provided. + * Given an arbitrary number of public keys as bytes, number of minimum required signatures, and a network type, this function returns + * the multisig challenge, as bytes. */ -export function encode_output_lock_then_transfer(amount: Amount, address: string, lock: Uint8Array, network: Network): Uint8Array; +export function encode_multisig_challenge(public_keys: Uint8Array, min_required_signatures: number, network: Network): Uint8Array; + /** - * Given a valid receiving address, token ID (in address form), a locking rule as bytes (available in this file), - * and a network type (mainnet, testnet, etc), this function creates an output of type - * LockThenTransfer with the parameters provided. + * A utxo can either come from a transaction or a block reward. + * Given a source id, whether from a block reward or transaction, this function + * takes a generic id with it, and returns serialized binary data of the id + * with the given source id. */ -export function encode_output_token_lock_then_transfer(amount: Amount, address: string, token_id: string, lock: Uint8Array, network: Network): Uint8Array; +export function encode_outpoint_source_id(id: Uint8Array, source: SourceId): Uint8Array; + /** * Given an amount, this function creates an output (as bytes) to burn a given amount of coins */ export function encode_output_coin_burn(amount: Amount): Uint8Array; -/** - * Given an amount, token ID (in address form) and network type (mainnet, testnet, etc), - * this function creates an output (as bytes) to burn a given amount of tokens - */ -export function encode_output_token_burn(amount: Amount, token_id: string, network: Network): Uint8Array; + /** * Given a pool id as string, an owner address and a network type (mainnet, testnet, etc), * this function returns an output (as bytes) to create a delegation to the given pool. * The owner address is the address that is authorized to withdraw from that delegation. */ export function encode_output_create_delegation(pool_id: string, owner_address: string, network: Network): Uint8Array; + +/** + * Given a pool id, staking data as bytes and the network type (mainnet, testnet, etc), + * this function returns an output that creates that staking pool. + * Note that the pool id is mandated to be taken from the hash of the first input. + * It is not arbitrary. + * + * Note: a UTXO of this kind is consumed when decommissioning a pool (provided that the pool + * never staked). + */ +export function encode_output_create_stake_pool(pool_id: string, pool_data: Uint8Array, network: Network): Uint8Array; + +/** + * Given data to be deposited in the blockchain, this function provides the output that deposits this data + */ +export function encode_output_data_deposit(data: Uint8Array): Uint8Array; + /** * Given a delegation id (as string, in address form), an amount and a network type (mainnet, testnet, etc), * this function returns an output (as bytes) that would delegate coins to be staked in the specified delegation id. */ export function encode_output_delegate_staking(amount: Amount, delegation_id: string, network: Network): Uint8Array; + /** - * This function returns the staking pool data needed to create a staking pool in an output as bytes, - * given its parameters and the network type (testnet, mainnet, etc). + * Given the parameters needed to create hash timelock contract, and a network type (mainnet, testnet, etc), + * this function creates an output. */ -export function encode_stake_pool_data(value: Amount, staker: string, vrf_public_key: string, decommission_key: string, margin_ratio_per_thousand: number, cost_per_block: Amount, network: Network): Uint8Array; +export function encode_output_htlc(amount: Amount, token_id: string | null | undefined, secret_hash: string, spend_address: string, refund_address: string, refund_timelock: Uint8Array, network: Network): Uint8Array; + /** - * Given a pool id, staking data as bytes and the network type (mainnet, testnet, etc), - * this function returns an output that creates that staking pool. - * Note that the pool id is mandated to be taken from the hash of the first input. - * It is not arbitrary. + * Given the parameters needed to issue a fungible token, and a network type (mainnet, testnet, etc), + * this function creates an output that issues that token. */ -export function encode_output_create_stake_pool(pool_id: string, pool_data: Uint8Array, network: Network): Uint8Array; +export function encode_output_issue_fungible_token(authority: string, token_ticker: string, metadata_uri: string, number_of_decimals: number, total_supply: TotalSupply, supply_amount: Amount | null | undefined, is_token_freezable: FreezableToken, _current_block_height: bigint, network: Network): Uint8Array; + /** - * Returns the fee that needs to be paid by a transaction for issuing a new fungible token + * Given the parameters needed to issue an NFT, and a network type (mainnet, testnet, etc), + * this function creates an output that issues that NFT. */ -export function fungible_token_issuance_fee(_current_block_height: bigint, network: Network): Amount; +export function encode_output_issue_nft(token_id: string, authority: string, name: string, ticker: string, description: string, media_hash: Uint8Array, creator: Uint8Array | null | undefined, media_uri: string | null | undefined, icon_uri: string | null | undefined, additional_metadata_uri: string | null | undefined, _current_block_height: bigint, network: Network): Uint8Array; + /** - * Given the current block height and a network type (mainnet, testnet, etc), - * this will return the fee that needs to be paid by a transaction for issuing a new NFT - * The current block height information is used in case a network upgrade changed the value. + * Given a valid receiving address, and a locking rule as bytes (available in this file), + * and a network type (mainnet, testnet, etc), this function creates an output of type + * LockThenTransfer with the parameters provided. */ -export function nft_issuance_fee(current_block_height: bigint, network: Network): Amount; +export function encode_output_lock_then_transfer(amount: Amount, address: string, lock: Uint8Array, network: Network): Uint8Array; + /** - * Given the current block height and a network type (mainnet, testnet, etc), - * this will return the fee that needs to be paid by a transaction for changing the total supply of a token - * by either minting or unminting tokens - * The current block height information is used in case a network upgrade changed the value. + * Given a pool id and a staker address, this function returns an output that is emitted + * when producing a block via that pool. + * + * Note: a UTXO of this kind is consumed when decommissioning a pool (provided that the pool + * has staked at least once). */ -export function token_supply_change_fee(current_block_height: bigint, network: Network): Amount; +export function encode_output_produce_block_from_stake(pool_id: string, staker: string, network: Network): Uint8Array; + /** - * Given the current block height and a network type (mainnet, testnet, etc), - * this will return the fee that needs to be paid by a transaction for freezing/unfreezing a token - * The current block height information is used in case a network upgrade changed the value. + * Given an amount, token ID (in address form) and network type (mainnet, testnet, etc), + * this function creates an output (as bytes) to burn a given amount of tokens */ -export function token_freeze_fee(current_block_height: bigint, network: Network): Amount; +export function encode_output_token_burn(amount: Amount, token_id: string, network: Network): Uint8Array; + +/** + * Given a valid receiving address, token ID (in address form), a locking rule as bytes (available in this file), + * and a network type (mainnet, testnet, etc), this function creates an output of type + * LockThenTransfer with the parameters provided. + */ +export function encode_output_token_lock_then_transfer(amount: Amount, address: string, token_id: string, lock: Uint8Array, network: Network): Uint8Array; + +/** + * Given a destination address, an amount, token ID (in address form) and a network type (mainnet, testnet, etc), this function + * creates an output of type Transfer for tokens, and returns it as bytes. + */ +export function encode_output_token_transfer(amount: Amount, address: string, token_id: string, network: Network): Uint8Array; + /** - * Given the current block height and a network type (mainnet, testnet, etc), - * this will return the fee that needs to be paid by a transaction for changing the authority of a token - * The current block height information is used in case a network upgrade changed the value. + * Given a destination address, an amount and a network type (mainnet, testnet, etc), this function + * creates an output of type Transfer, and returns it as bytes. */ -export function token_change_authority_fee(current_block_height: bigint, network: Network): Amount; +export function encode_output_transfer(amount: Amount, address: string, network: Network): Uint8Array; + /** - * Given the parameters needed to issue a fungible token, and a network type (mainnet, testnet, etc), - * this function creates an output that issues that token. + * Return a PartiallySignedTransaction object as bytes. + * + * `transaction` is an encoded `Transaction` (which can be produced via `encode_transaction`). + * + * `signatures`, `input_utxos`, `input_destinations` and `htlc_secrets` are encoded lists of + * optional objects of the corresponding type. To produce such a list, iterate over your + * original list of optional objects and then: + * 1) emit byte 0 if the current object is null; + * 2) otherwise emit byte 1 followed by the object in its encoded form. + * + * Each individual object in each of the lists corresponds to the transaction input with the same + * index and its meaning is as follows: + * 1) `signatures` - the signature for the input; + * 2) `input_utxos`- the utxo for the input (if it's utxo-based); + * 3) `input_destinations` - the destination (address) corresponding to the input; this determines + * the key(s) with which the input has to be signed. Note that for utxo-based inputs the + * corresponding destination can usually be extracted from the utxo itself (the exception + * being the `ProduceBlockFromStake` utxo, which doesn't contain the pool's decommission key). + * However, PartiallySignedTransaction requires that *all* input destinations are provided + * explicitly anyway. + * 4) `htlc_secrets` - if the input is an HTLC one and if the transaction is spending the HTLC, + * this should be the HTLC secret. Otherwise it should be null. + * + * The number of items in each list must be equal to the number of transaction inputs. + * + * `additional_info` has the same meaning as in `encode_witness`. */ -export function encode_output_issue_fungible_token(authority: string, token_ticker: string, metadata_uri: string, number_of_decimals: number, total_supply: TotalSupply, supply_amount: Amount | null | undefined, is_token_freezable: FreezableToken, _current_block_height: bigint, network: Network): Uint8Array; +export function encode_partially_signed_transaction(transaction: Uint8Array, signatures: Uint8Array, input_utxos: Uint8Array, input_destinations: Uint8Array, htlc_secrets: Uint8Array, additional_info: TxAdditionalInfo, network: Network): Uint8Array; + /** - * Returns the Fungible/NFT Token ID for the given inputs of a transaction + * Given an unsigned transaction and signatures, this function returns a SignedTransaction object as bytes. */ -export function get_token_id(inputs: Uint8Array, network: Network): string; +export function encode_signed_transaction(transaction: Uint8Array, signatures: Uint8Array): Uint8Array; + /** - * Given the parameters needed to issue an NFT, and a network type (mainnet, testnet, etc), - * this function creates an output that issues that NFT. + * Return a `SignedTransactionIntent` object as bytes given the message and encoded signatures. + * + * Note: to produce a valid signed intent one is expected to sign the corresponding message by private keys + * corresponding to each input of the transaction. + * + * Parameters: + * `signed_message` - this must have been produced by `make_transaction_intent_message_to_sign`. + * `signatures` - this should be an array of Uint8Array, each of them representing an individual signature + * of `signed_message` produced by `sign_challenge` using the private key for the corresponding input destination + * of the transaction. The number of signatures must be equal to the number of inputs in the transaction. */ -export function encode_output_issue_nft(token_id: string, authority: string, name: string, ticker: string, description: string, media_hash: Uint8Array, creator: Uint8Array | null | undefined, media_uri: string | null | undefined, icon_uri: string | null | undefined, additional_metadata_uri: string | null | undefined, _current_block_height: bigint, network: Network): Uint8Array; +export function encode_signed_transaction_intent(signed_message: Uint8Array, signatures: Uint8Array[]): Uint8Array; + /** - * Given data to be deposited in the blockchain, this function provides the output that deposits this data + * This function returns the staking pool data needed to create a staking pool in an output as bytes, + * given its parameters and the network type (testnet, mainnet, etc). */ -export function encode_output_data_deposit(data: Uint8Array): Uint8Array; +export function encode_stake_pool_data(value: Amount, staker: string, vrf_public_key: string, decommission_key: string, margin_ratio_per_thousand: number, cost_per_block: Amount, network: Network): Uint8Array; + /** - * Returns the fee that needs to be paid by a transaction for issuing a data deposit + * Given inputs as bytes, outputs as bytes, and flags settings, this function returns + * the transaction that contains them all, as bytes. */ -export function data_deposit_fee(current_block_height: bigint, network: Network): Amount; +export function encode_transaction(inputs: Uint8Array, outputs: Uint8Array, flags: bigint): Uint8Array; + /** - * Given the parameters needed to create hash timelock contract, and a network type (mainnet, testnet, etc), - * this function creates an output. + * Sign the specified input of the transaction and encode the signature as InputWitness. + * + * `input_utxos` must be formed as follows: for each transaction input, emit byte 0 if it's a non-UTXO input, + * otherwise emit 1 followed by the corresponding transaction output encoded via the appropriate "encode_output_" + * function. + * + * `additional_info` must contain the following: + * 1) for each `ProduceBlockFromStake` input of the transaction, the pool info for the pool referenced by that input; + * 2) for each `FillOrder` and `ConcludeOrder` input of the transaction, the order info for the order referenced by + * that input. + * Note: + * - It doesn't matter which input witness is currently being encoded. E.g. even if you are encoding a witness + * for some UTXO-based input but another input of the same transaction is `FillOrder`, you have to include the order + * info when encoding the witness for the UTXO-based input too. + * - After a certain hard fork, the produced signature will "commit" to the provided additional info, i.e. the info + * will become a part of what is being signed. So, passing invalid additional info will result in an invalid signature + * (with one small caveat: for `FillOrder` we only commit to order's initial balances and not the current ones; + * so if you only have `FillOrder` inputs, you can technically pass bogus values for the current balances and + * the resulting signature will still be valid; though it's better to avoid doing this). + */ +export function encode_witness(sighashtype: SignatureHashType, private_key: Uint8Array, input_owner_destination: string, transaction: Uint8Array, input_utxos: Uint8Array, input_index: number, additional_info: TxAdditionalInfo, current_block_height: bigint, network: Network): Uint8Array; + +/** + * Sign the specified HTLC input of the transaction and encode the signature as InputWitness. + * + * This function must be used for HTLC refunding when the refund address is a multisig one. + * + * `key_index` parameter is an index of the public key in the multisig challenge corresponding to + * the specified private key. + * `input_witness` parameter can be either empty or a result of previous calls to this function. + * + * `input_utxos` and `additional_info` have the same format and requirements as in `encode_witness`. */ -export function encode_output_htlc(amount: Amount, token_id: string | null | undefined, secret_hash: string, spend_address: string, refund_address: string, refund_timelock: Uint8Array, network: Network): Uint8Array; +export function encode_witness_htlc_refund_multisig(sighashtype: SignatureHashType, private_key: Uint8Array, key_index: number, input_witness: Uint8Array, multisig_challenge: Uint8Array, transaction: Uint8Array, input_utxos: Uint8Array, input_index: number, additional_info: TxAdditionalInfo, current_block_height: bigint, network: Network): Uint8Array; + /** - * Given a signed transaction and input outpoint that spends an htlc utxo, extract a secret that is - * encoded in the corresponding input signature + * Sign the specified HTLC input of the transaction and encode the signature as InputWitness. + * + * This function must be used for HTLC refunding when the refund address is a single-sig one. + * + * `input_utxos` and `additional_info` have the same format and requirements as in `encode_witness`. */ -export function extract_htlc_secret(signed_tx_bytes: Uint8Array, strict_byte_size: boolean, htlc_outpoint_source_id: Uint8Array, htlc_output_index: number): Uint8Array; +export function encode_witness_htlc_refund_single_sig(sighashtype: SignatureHashType, private_key: Uint8Array, input_owner_destination: string, transaction: Uint8Array, input_utxos: Uint8Array, input_index: number, additional_info: TxAdditionalInfo, current_block_height: bigint, network: Network): Uint8Array; + /** - * Given an output source id as bytes, and an output index, together representing a utxo, - * this function returns the input that puts them together, as bytes. + * Sign the specified HTLC input of the transaction and encode the signature as InputWitness. + * + * This function must be used for HTLC spending. + * + * `input_utxos` and `additional_info` have the same format and requirements as in `encode_witness`. */ -export function encode_input_for_utxo(outpoint_source_id: Uint8Array, output_index: number): Uint8Array; +export function encode_witness_htlc_spend(sighashtype: SignatureHashType, private_key: Uint8Array, input_owner_destination: string, transaction: Uint8Array, input_utxos: Uint8Array, input_index: number, secret: Uint8Array, additional_info: TxAdditionalInfo, current_block_height: bigint, network: Network): Uint8Array; + /** - * Given a delegation id, an amount and a network type (mainnet, testnet, etc), this function - * creates an input that withdraws from a delegation. - * A nonce is needed because this spends from an account. The nonce must be in sequence for everything in that account. + * Encode an input witness of the variant that contains no signature. */ -export function encode_input_for_withdraw_from_delegation(delegation_id: string, amount: Amount, nonce: bigint, network: Network): Uint8Array; +export function encode_witness_no_signature(): Uint8Array; + /** * Given the inputs, along each input's destination that can spend that input * (e.g. If we are spending a UTXO in input number 1 and it is owned by address mtc1xxxx, then it is mtc1xxxx in element number 2 in the vector/list. @@ -273,44 +499,46 @@ export function encode_input_for_withdraw_from_delegation(delegation_id: string, * and in the case of AccountCommand inputs which change a token it is the token's authority destination) * and the outputs, estimate the transaction size. * ScriptHash and ClassicMultisig destinations are not supported. + * Also, the function assumes that the input UTXOs are not HTLC. */ export function estimate_transaction_size(inputs: Uint8Array, input_utxos_destinations: string[], outputs: Uint8Array, network: Network): number; + /** - * Given inputs as bytes, outputs as bytes, and flags settings, this function returns - * the transaction that contains them all, as bytes. + * Return the extended public key from an extended private key */ -export function encode_transaction(inputs: Uint8Array, outputs: Uint8Array, flags: bigint): Uint8Array; +export function extended_public_key_from_extended_private_key(private_key: Uint8Array): Uint8Array; + /** - * Encode an input witness of the variant that contains no signature. + * Given a signed transaction and input outpoint that spends an htlc utxo, extract a secret that is + * encoded in the corresponding input signature */ -export function encode_witness_no_signature(): Uint8Array; +export function extract_htlc_secret(signed_tx: Uint8Array, strict_byte_size: boolean, htlc_outpoint_source_id: Uint8Array, htlc_output_index: number): Uint8Array; + /** - * Given a private key, inputs and an input number to sign, and the destination that owns that output (through the utxo), - * and a network type (mainnet, testnet, etc), this function returns a witness to be used in a signed transaction, as bytes. + * Returns the fee that needs to be paid by a transaction for issuing a new fungible token */ -export function encode_witness(sighashtype: SignatureHashType, private_key_bytes: Uint8Array, input_owner_destination: string, transaction_bytes: Uint8Array, inputs: Uint8Array, input_num: number, network: Network): Uint8Array; +export function fungible_token_issuance_fee(_current_block_height: bigint, network: Network): Amount; + /** - * Given a private key, inputs and an input number to sign, and the destination that owns that output (through the utxo), - * and a network type (mainnet, testnet, etc), and an htlc secret this function returns a witness to be used in a signed transaction, as bytes. + * Returns the Delegation ID for the given inputs of a transaction */ -export function encode_witness_htlc_secret(sighashtype: SignatureHashType, private_key_bytes: Uint8Array, input_owner_destination: string, transaction_bytes: Uint8Array, inputs: Uint8Array, input_num: number, secret: Uint8Array, network: Network): Uint8Array; +export function get_delegation_id(inputs: Uint8Array, network: Network): string; + /** - * Given an arbitrary number of public keys as bytes, number of minimum required signatures, and a network type, this function returns - * the multisig challenge, as bytes. + * Returns the Order ID for the given inputs of a transaction */ -export function encode_multisig_challenge(public_keys_bytes: Uint8Array, min_required_signatures: number, network: Network): Uint8Array; +export function get_order_id(inputs: Uint8Array, network: Network): string; + /** - * Given a private key, inputs and an input number to sign, and multisig challenge, - * and a network type (mainnet, testnet, etc), this function returns a witness to be used in a signed transaction, as bytes. - * - * `key_index` parameter is an index of a public key in the challenge, against which is the signature produces from private key is to be verified. - * `input_witness` parameter can be either empty or a result of previous calls to this function. + * Returns the Pool ID for the given inputs of a transaction */ -export function encode_witness_htlc_multisig(sighashtype: SignatureHashType, private_key_bytes: Uint8Array, key_index: number, input_witness: Uint8Array, multisig_challenge: Uint8Array, transaction_bytes: Uint8Array, utxos: Uint8Array, input_num: number, network: Network): Uint8Array; +export function get_pool_id(inputs: Uint8Array, network: Network): string; + /** - * Given an unsigned transaction and signatures, this function returns a SignedTransaction object as bytes. + * Returns the Fungible/NFT Token ID for the given inputs of a transaction */ -export function encode_signed_transaction(transaction_bytes: Uint8Array, signatures: Uint8Array): Uint8Array; +export function get_token_id(inputs: Uint8Array, current_block_height: bigint, network: Network): string; + /** * Given a `Transaction` encoded in bytes (not a signed transaction, but a signed transaction is tolerated by ignoring the extra bytes, by choice) * this function will return the transaction id. @@ -322,229 +550,277 @@ export function encode_signed_transaction(transaction_bytes: Uint8Array, signatu * since the signatures are appended at the end of the `Transaction` object as a vector to create a `SignedTransaction`. * It is recommended to use a strict `Transaction` size and set the second parameter to `true`. */ -export function get_transaction_id(transaction_bytes: Uint8Array, strict_byte_size: boolean): string; +export function get_transaction_id(transaction: Uint8Array, strict_byte_size: boolean): string; + /** - * Calculate the "effective balance" of a pool, given the total pool balance and pledge by the pool owner/staker. - * The effective balance is how the influence of a pool is calculated due to its balance. + * Verify a witness produced by one of the `encode_witness` functions. + * + * `input_owner_destination` must be specified if `witness` actually contains a signature + * (i.e. it's not InputWitness::NoSignature) and the input is not an HTLC one. Otherwise it must + * be null. */ -export function effective_pool_balance(network: Network, pledge_amount: Amount, pool_balance: Amount): Amount; +export function internal_verify_witness(sighashtype: SignatureHashType, input_owner_destination: string | null | undefined, witness: Uint8Array, transaction: Uint8Array, input_utxos: Uint8Array, input_index: number, additional_info: TxAdditionalInfo, current_block_height: bigint, network: Network): void; + /** - * Given a token_id, an amount of tokens to mint and nonce return an encoded mint tokens input + * From an extended private key create a change private key for a given key index + * derivation path: current_derivation_path/1/key_index */ -export function encode_input_for_mint_tokens(token_id: string, amount: Amount, nonce: bigint, network: Network): Uint8Array; +export function make_change_address(private_key: Uint8Array, key_index: number): Uint8Array; + /** - * Given a token_id and nonce return an encoded unmint tokens input + * From an extended public key create a change public key for a given key index + * derivation path: current_derivation_path/1/key_index */ -export function encode_input_for_unmint_tokens(token_id: string, nonce: bigint, network: Network): Uint8Array; +export function make_change_address_public_key(extended_public_key: Uint8Array, key_index: number): Uint8Array; + /** - * Given a token_id and nonce return an encoded lock_token_supply input + * Create the default account's extended private key for a given mnemonic + * derivation path: 44'/mintlayer_coin_type'/0' */ -export function encode_input_for_lock_token_supply(token_id: string, nonce: bigint, network: Network): Uint8Array; +export function make_default_account_privkey(mnemonic: string, network: Network): Uint8Array; + /** - * Given a token_id, is token unfreezable and nonce return an encoded freeze token input + * Generates a new, random private key from entropy */ -export function encode_input_for_freeze_token(token_id: string, is_token_unfreezable: TokenUnfreezable, nonce: bigint, network: Network): Uint8Array; +export function make_private_key(): Uint8Array; + /** - * Given a token_id and nonce return an encoded unfreeze token input + * From an extended private key create a receiving private key for a given key index + * derivation path: current_derivation_path/0/key_index */ -export function encode_input_for_unfreeze_token(token_id: string, nonce: bigint, network: Network): Uint8Array; +export function make_receiving_address(private_key: Uint8Array, key_index: number): Uint8Array; + /** - * Given a token_id, new authority destination and nonce return an encoded change token authority input + * From an extended public key create a receiving public key for a given key index + * derivation path: current_derivation_path/0/key_index */ -export function encode_input_for_change_token_authority(token_id: string, new_authority: string, nonce: bigint, network: Network): Uint8Array; +export function make_receiving_address_public_key(extended_public_key: Uint8Array, key_index: number): Uint8Array; + /** - * Given a token_id, new metadata uri and nonce return an encoded change token metadata uri input + * Return the message that has to be signed to produce a signed transaction intent. */ -export function encode_input_for_change_token_metadata_uri(token_id: string, new_metadata_uri: string, nonce: bigint, network: Network): Uint8Array; +export function make_transaction_intent_message_to_sign(intent: string, transaction_id: string): Uint8Array; + /** - * Given ask and give amounts and a conclude key create output that creates an order. - * - * 'ask_token_id': the parameter represents a Token if it's Some and coins otherwise. - * 'give_token_id': the parameter represents a Token if it's Some and coins otherwise. + * Produce a multisig address given a multisig challenge. */ -export function encode_create_order_output(ask_amount: Amount, ask_token_id: string | null | undefined, give_amount: Amount, give_token_id: string | null | undefined, conclude_address: string, network: Network): Uint8Array; +export function multisig_challenge_to_address(multisig_challenge: Uint8Array, network: Network): string; + /** - * Given an amount to fill an order (which is described in terms of ask currency) and a destination - * for result outputs create an input that fills the order. + * Given the current block height and a network type (mainnet, testnet, etc), + * this will return the fee that needs to be paid by a transaction for issuing a new NFT + * The current block height information is used in case a network upgrade changed the value. */ -export function encode_input_for_fill_order(order_id: string, fill_amount: Amount, destination: string, nonce: bigint, network: Network): Uint8Array; +export function nft_issuance_fee(current_block_height: bigint, network: Network): Amount; + /** - * Given an order id create an input that concludes the order. + * Given a public key (as bytes) and a network type (mainnet, testnet, etc), + * return the address public key hash from that public key as an address */ -export function encode_input_for_conclude_order(order_id: string, nonce: bigint, network: Network): Uint8Array; +export function pubkey_to_pubkeyhash_address(public_key: Uint8Array, network: Network): string; + /** - * Indicates whether a token can be frozen + * Given a private key, as bytes, return the bytes of the corresponding public key */ -export enum FreezableToken { - No = 0, - Yes = 1, -} +export function public_key_from_private_key(private_key: Uint8Array): Uint8Array; + /** - * The network, for which an operation to be done. Mainnet, testnet, etc. + * Given a message and a private key, create and sign a challenge with the given private key. + * This kind of signature is to be used when signing challenges. */ -export enum Network { - Mainnet = 0, - Testnet = 1, - Regtest = 2, - Signet = 3, -} +export function sign_challenge(private_key: Uint8Array, message: Uint8Array): Uint8Array; + /** - * The part of the transaction that will be committed in the signature. Similar to bitcoin's sighash. + * Given a message and a private key, sign the message with the given private key + * This kind of signature is to be used when signing spend requests, such as transaction + * input witness. */ -export enum SignatureHashType { - ALL = 0, - NONE = 1, - SINGLE = 2, - ANYONECANPAY = 3, -} +export function sign_message_for_spending(private_key: Uint8Array, message: Uint8Array): Uint8Array; + /** - * A utxo can either come from a transaction or a block reward. This enum signifies that. + * Given the current block height and a network type (mainnet, testnet, etc), + * this function returns the number of blocks, after which a pool that decommissioned, + * will have its funds unlocked and available for spending. + * The current block height information is used in case a network upgrade changed the value. */ -export enum SourceId { - Transaction = 0, - BlockReward = 1, -} +export function staking_pool_spend_maturity_block_count(current_block_height: bigint, network: Network): bigint; + /** - * Indicates whether a token can be unfrozen once frozen + * Given the current block height and a network type (mainnet, testnet, etc), + * this will return the fee that needs to be paid by a transaction for changing the authority of a token + * The current block height information is used in case a network upgrade changed the value. */ -export enum TokenUnfreezable { - No = 0, - Yes = 1, -} +export function token_change_authority_fee(current_block_height: bigint, network: Network): Amount; + /** - * The token supply of a specific token, set on issuance + * Given the current block height and a network type (mainnet, testnet, etc), + * this will return the fee that needs to be paid by a transaction for freezing/unfreezing a token + * The current block height information is used in case a network upgrade changed the value. */ -export enum TotalSupply { - /** - * Can be issued with no limit, but then can be locked to have a fixed supply. - */ - Lockable = 0, - /** - * Unlimited supply, no limits except for numeric limits due to u128 - */ - Unlimited = 1, - /** - * On issuance, the total number of coins is fixed - */ - Fixed = 2, -} +export function token_freeze_fee(current_block_height: bigint, network: Network): Amount; + /** - * Amount type abstraction. The amount type is stored in a string - * since JavaScript number type cannot fit 128-bit integers. - * The amount is given as an integer in units of "atoms". - * Atoms are the smallest, indivisible amount of a coin or token. + * Given the current block height and a network type (mainnet, testnet, etc), + * this will return the fee that needs to be paid by a transaction for changing the total supply of a token + * by either minting or unminting tokens + * The current block height information is used in case a network upgrade changed the value. */ -export class Amount { - private constructor(); - free(): void; - static from_atoms(atoms: string): Amount; - atoms(): string; -} +export function token_supply_change_fee(current_block_height: bigint, network: Network): Amount; + +/** + * Given a signed challenge, an address and a message, verify that + * the signature is produced by signing the message with the private key + * that derived the given public key. + * This function is used for verifying messages-related challenges. + * + * Note: for signatures that were created by `sign_challenge`, the provided address must be + * a 'pubkeyhash' address. + * + * Note: currently this function never returns `false` - it either returns `true` or fails with an error. + */ +export function verify_challenge(address: string, network: Network, signed_challenge: Uint8Array, message: Uint8Array): boolean; + +/** + * Given a digital signature, a public key and a message. Verify that + * the signature is produced by signing the message with the private key + * that derived the given public key. + * Note that this function is used for verifying messages related to spending, + * such as transaction input witness. + */ +export function verify_signature_for_spending(public_key: Uint8Array, signature: Uint8Array, message: Uint8Array): boolean; + +/** + * Verify a signed transaction intent. + * + * Parameters: + * `expected_signed_message` - the message that is supposed to be signed; this must have been + * produced by `make_transaction_intent_message_to_sign`. + * `encoded_signed_intent` - the signed transaction intent produced by `encode_signed_transaction_intent`. + * `input_destinations` - an array of addresses (strings), corresponding to the transaction's input destinations + * (note that this function treats "pub key" and "pub key hash" addresses interchangeably, so it's ok to pass + * one instead of the other). + * `network` - the network being used (needed to decode the addresses). + */ +export function verify_transaction_intent(expected_signed_message: Uint8Array, encoded_signed_intent: Uint8Array, input_destinations: string[], network: Network): void; export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module; export interface InitOutput { - readonly memory: WebAssembly.Memory; - readonly __wbg_amount_free: (a: number, b: number) => void; - readonly amount_from_atoms: (a: number, b: number) => number; - readonly amount_atoms: (a: number) => [number, number]; - readonly encode_outpoint_source_id: (a: number, b: number, c: number) => [number, number]; - readonly make_private_key: () => [number, number]; - readonly make_default_account_privkey: (a: number, b: number, c: number) => [number, number, number, number]; - readonly make_receiving_address: (a: number, b: number, c: number) => [number, number, number, number]; - readonly make_change_address: (a: number, b: number, c: number) => [number, number, number, number]; - readonly pubkey_to_pubkeyhash_address: (a: number, b: number, c: number) => [number, number, number, number]; - readonly public_key_from_private_key: (a: number, b: number) => [number, number, number, number]; - readonly extended_public_key_from_extended_private_key: (a: number, b: number) => [number, number, number, number]; - readonly make_receiving_address_public_key: (a: number, b: number, c: number) => [number, number, number, number]; - readonly make_change_address_public_key: (a: number, b: number, c: number) => [number, number, number, number]; - readonly sign_message_for_spending: (a: number, b: number, c: number, d: number) => [number, number, number, number]; - readonly verify_signature_for_spending: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number]; - readonly sign_challenge: (a: number, b: number, c: number, d: number) => [number, number, number, number]; - readonly verify_challenge: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number]; - readonly make_transaction_intent_message_to_sign: (a: number, b: number, c: number, d: number) => [number, number, number, number]; - readonly encode_signed_transaction_intent: (a: number, b: number, c: any) => [number, number, number, number]; - readonly verify_transaction_intent: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number]; - readonly encode_output_transfer: (a: number, b: number, c: number, d: number) => [number, number, number, number]; - readonly encode_output_token_transfer: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number]; - readonly staking_pool_spend_maturity_block_count: (a: bigint, b: number) => bigint; - readonly encode_lock_for_block_count: (a: bigint) => [number, number]; - readonly encode_lock_for_seconds: (a: bigint) => [number, number]; - readonly encode_lock_until_time: (a: bigint) => [number, number]; - readonly encode_lock_until_height: (a: bigint) => [number, number]; - readonly encode_output_lock_then_transfer: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number]; - readonly encode_output_token_lock_then_transfer: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => [number, number, number, number]; - readonly encode_output_coin_burn: (a: number) => [number, number, number, number]; - readonly encode_output_token_burn: (a: number, b: number, c: number, d: number) => [number, number, number, number]; - readonly encode_output_create_delegation: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number]; - readonly encode_output_delegate_staking: (a: number, b: number, c: number, d: number) => [number, number, number, number]; - readonly encode_stake_pool_data: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number) => [number, number, number, number]; - readonly encode_output_create_stake_pool: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number]; - readonly fungible_token_issuance_fee: (a: bigint, b: number) => number; - readonly nft_issuance_fee: (a: bigint, b: number) => number; - readonly token_supply_change_fee: (a: bigint, b: number) => number; - readonly token_freeze_fee: (a: bigint, b: number) => number; - readonly token_change_authority_fee: (a: bigint, b: number) => number; - readonly encode_output_issue_fungible_token: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: bigint, l: number) => [number, number, number, number]; - readonly get_token_id: (a: number, b: number, c: number) => [number, number, number, number]; - readonly encode_output_issue_nft: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number, n: number, o: number, p: number, q: number, r: number, s: number, t: number, u: bigint, v: number) => [number, number, number, number]; - readonly encode_output_data_deposit: (a: number, b: number) => [number, number, number, number]; - readonly data_deposit_fee: (a: bigint, b: number) => number; - readonly encode_output_htlc: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number) => [number, number, number, number]; - readonly extract_htlc_secret: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number]; - readonly encode_input_for_utxo: (a: number, b: number, c: number) => [number, number, number, number]; - readonly encode_input_for_withdraw_from_delegation: (a: number, b: number, c: number, d: bigint, e: number) => [number, number, number, number]; - readonly estimate_transaction_size: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number]; - readonly encode_transaction: (a: number, b: number, c: number, d: number, e: bigint) => [number, number, number, number]; - readonly encode_witness_no_signature: () => [number, number]; - readonly encode_witness: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number) => [number, number, number, number]; - readonly encode_witness_htlc_secret: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number) => [number, number, number, number]; - readonly encode_multisig_challenge: (a: number, b: number, c: number, d: number) => [number, number, number, number]; - readonly encode_witness_htlc_multisig: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number, n: number) => [number, number, number, number]; - readonly encode_signed_transaction: (a: number, b: number, c: number, d: number) => [number, number, number, number]; - readonly get_transaction_id: (a: number, b: number, c: number) => [number, number, number, number]; - readonly effective_pool_balance: (a: number, b: number, c: number) => [number, number, number]; - readonly encode_input_for_mint_tokens: (a: number, b: number, c: number, d: bigint, e: number) => [number, number, number, number]; - readonly encode_input_for_unmint_tokens: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; - readonly encode_input_for_lock_token_supply: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; - readonly encode_input_for_freeze_token: (a: number, b: number, c: number, d: bigint, e: number) => [number, number, number, number]; - readonly encode_input_for_unfreeze_token: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; - readonly encode_input_for_change_token_authority: (a: number, b: number, c: number, d: number, e: bigint, f: number) => [number, number, number, number]; - readonly encode_input_for_change_token_metadata_uri: (a: number, b: number, c: number, d: number, e: bigint, f: number) => [number, number, number, number]; - readonly encode_create_order_output: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => [number, number, number, number]; - readonly encode_input_for_fill_order: (a: number, b: number, c: number, d: number, e: number, f: bigint, g: number) => [number, number, number, number]; - readonly encode_input_for_conclude_order: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; - readonly rustsecp256k1_v0_10_0_context_create: (a: number) => number; - readonly rustsecp256k1_v0_10_0_context_destroy: (a: number) => void; - readonly rustsecp256k1_v0_10_0_default_illegal_callback_fn: (a: number, b: number) => void; - readonly rustsecp256k1_v0_10_0_default_error_callback_fn: (a: number, b: number) => void; - readonly __wbindgen_exn_store: (a: number) => void; - readonly __externref_table_alloc: () => number; - readonly __wbindgen_export_2: WebAssembly.Table; - readonly __wbindgen_malloc: (a: number, b: number) => number; - readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number; - readonly __wbindgen_free: (a: number, b: number, c: number) => void; - readonly __externref_table_dealloc: (a: number) => void; - readonly __wbindgen_start: () => void; + readonly memory: WebAssembly.Memory; + readonly data_deposit_fee: (a: bigint, b: number) => number; + readonly decode_partially_signed_transaction_to_js: (a: number, b: number, c: number) => [number, number, number]; + readonly decode_signed_transaction_to_js: (a: number, b: number, c: number) => [number, number, number]; + readonly decode_transaction_to_js: (a: number, b: number, c: number) => [number, number, number]; + readonly effective_pool_balance: (a: number, b: number, c: number) => [number, number, number]; + readonly encode_destination: (a: number, b: number, c: number) => [number, number, number, number]; + readonly encode_lock_for_block_count: (a: bigint) => [number, number]; + readonly encode_lock_for_seconds: (a: bigint) => [number, number]; + readonly encode_lock_until_height: (a: bigint) => [number, number]; + readonly encode_lock_until_time: (a: bigint) => [number, number]; + readonly encode_multisig_challenge: (a: number, b: number, c: number, d: number) => [number, number, number, number]; + readonly encode_outpoint_source_id: (a: number, b: number, c: number) => [number, number]; + readonly encode_partially_signed_transaction: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: any, l: number) => [number, number, number, number]; + readonly encode_signed_transaction: (a: number, b: number, c: number, d: number) => [number, number, number, number]; + readonly encode_signed_transaction_intent: (a: number, b: number, c: number, d: number) => [number, number, number, number]; + readonly encode_stake_pool_data: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number) => [number, number, number, number]; + readonly encode_transaction: (a: number, b: number, c: number, d: number, e: bigint) => [number, number, number, number]; + readonly encode_witness: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: any, l: bigint, m: number) => [number, number, number, number]; + readonly encode_witness_htlc_refund_multisig: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number, n: any, o: bigint, p: number) => [number, number, number, number]; + readonly encode_witness_htlc_refund_single_sig: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: any, l: bigint, m: number) => [number, number, number, number]; + readonly encode_witness_htlc_spend: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: any, n: bigint, o: number) => [number, number, number, number]; + readonly encode_witness_no_signature: () => [number, number]; + readonly estimate_transaction_size: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number]; + readonly extended_public_key_from_extended_private_key: (a: number, b: number) => [number, number, number, number]; + readonly extract_htlc_secret: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number]; + readonly fungible_token_issuance_fee: (a: bigint, b: number) => number; + readonly get_delegation_id: (a: number, b: number, c: number) => [number, number, number, number]; + readonly get_order_id: (a: number, b: number, c: number) => [number, number, number, number]; + readonly get_pool_id: (a: number, b: number, c: number) => [number, number, number, number]; + readonly get_token_id: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; + readonly get_transaction_id: (a: number, b: number, c: number) => [number, number, number, number]; + readonly make_change_address: (a: number, b: number, c: number) => [number, number, number, number]; + readonly make_change_address_public_key: (a: number, b: number, c: number) => [number, number, number, number]; + readonly make_default_account_privkey: (a: number, b: number, c: number) => [number, number, number, number]; + readonly make_private_key: () => [number, number]; + readonly make_receiving_address: (a: number, b: number, c: number) => [number, number, number, number]; + readonly make_receiving_address_public_key: (a: number, b: number, c: number) => [number, number, number, number]; + readonly make_transaction_intent_message_to_sign: (a: number, b: number, c: number, d: number) => [number, number, number, number]; + readonly multisig_challenge_to_address: (a: number, b: number, c: number) => [number, number, number, number]; + readonly nft_issuance_fee: (a: bigint, b: number) => number; + readonly pubkey_to_pubkeyhash_address: (a: number, b: number, c: number) => [number, number, number, number]; + readonly public_key_from_private_key: (a: number, b: number) => [number, number, number, number]; + readonly sign_challenge: (a: number, b: number, c: number, d: number) => [number, number, number, number]; + readonly sign_message_for_spending: (a: number, b: number, c: number, d: number) => [number, number, number, number]; + readonly staking_pool_spend_maturity_block_count: (a: bigint, b: number) => bigint; + readonly token_change_authority_fee: (a: bigint, b: number) => number; + readonly token_freeze_fee: (a: bigint, b: number) => number; + readonly token_supply_change_fee: (a: bigint, b: number) => number; + readonly verify_challenge: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number]; + readonly verify_signature_for_spending: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number]; + readonly verify_transaction_intent: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number]; + readonly encode_create_order_output: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => [number, number, number, number]; + readonly encode_output_coin_burn: (a: number) => [number, number, number, number]; + readonly encode_output_create_delegation: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number]; + readonly encode_output_create_stake_pool: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number]; + readonly encode_output_data_deposit: (a: number, b: number) => [number, number, number, number]; + readonly encode_output_delegate_staking: (a: number, b: number, c: number, d: number) => [number, number, number, number]; + readonly encode_output_htlc: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number) => [number, number, number, number]; + readonly encode_output_issue_fungible_token: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: bigint, l: number) => [number, number, number, number]; + readonly encode_output_issue_nft: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number, n: number, o: number, p: number, q: number, r: number, s: number, t: number, u: bigint, v: number) => [number, number, number, number]; + readonly encode_output_lock_then_transfer: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number]; + readonly encode_output_produce_block_from_stake: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number]; + readonly encode_output_token_burn: (a: number, b: number, c: number, d: number) => [number, number, number, number]; + readonly encode_output_token_lock_then_transfer: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => [number, number, number, number]; + readonly encode_output_token_transfer: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number]; + readonly encode_output_transfer: (a: number, b: number, c: number, d: number) => [number, number, number, number]; + readonly __wbg_amount_free: (a: number, b: number) => void; + readonly amount_atoms: (a: number) => [number, number]; + readonly amount_from_atoms: (a: number, b: number) => number; + readonly encode_input_for_change_token_authority: (a: number, b: number, c: number, d: number, e: bigint, f: number) => [number, number, number, number]; + readonly encode_input_for_change_token_metadata_uri: (a: number, b: number, c: number, d: number, e: bigint, f: number) => [number, number, number, number]; + readonly encode_input_for_conclude_order: (a: number, b: number, c: bigint, d: bigint, e: number) => [number, number, number, number]; + readonly encode_input_for_fill_order: (a: number, b: number, c: number, d: number, e: number, f: bigint, g: bigint, h: number) => [number, number, number, number]; + readonly encode_input_for_freeze_order: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; + readonly encode_input_for_freeze_token: (a: number, b: number, c: number, d: bigint, e: number) => [number, number, number, number]; + readonly encode_input_for_lock_token_supply: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; + readonly encode_input_for_mint_tokens: (a: number, b: number, c: number, d: bigint, e: number) => [number, number, number, number]; + readonly encode_input_for_unfreeze_token: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; + readonly encode_input_for_unmint_tokens: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; + readonly encode_input_for_utxo: (a: number, b: number, c: number) => [number, number, number, number]; + readonly encode_input_for_withdraw_from_delegation: (a: number, b: number, c: number, d: bigint, e: number) => [number, number, number, number]; + readonly internal_verify_witness: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: any, l: bigint, m: number) => [number, number]; + readonly rustsecp256k1_v0_10_0_context_create: (a: number) => number; + readonly rustsecp256k1_v0_10_0_context_destroy: (a: number) => void; + readonly rustsecp256k1_v0_10_0_default_error_callback_fn: (a: number, b: number) => void; + readonly rustsecp256k1_v0_10_0_default_illegal_callback_fn: (a: number, b: number) => void; + readonly __wbindgen_malloc: (a: number, b: number) => number; + readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number; + readonly __wbindgen_exn_store: (a: number) => void; + readonly __externref_table_alloc: () => number; + readonly __wbindgen_externrefs: WebAssembly.Table; + readonly __wbindgen_free: (a: number, b: number, c: number) => void; + readonly __externref_table_dealloc: (a: number) => void; + readonly __wbindgen_start: () => void; } export type SyncInitInput = BufferSource | WebAssembly.Module; + /** -* Instantiates the given `module`, which can either be bytes or -* a precompiled `WebAssembly.Module`. -* -* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated. -* -* @returns {InitOutput} -*/ + * Instantiates the given `module`, which can either be bytes or + * a precompiled `WebAssembly.Module`. + * + * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated. + * + * @returns {InitOutput} + */ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput; /** -* If `module_or_path` is {RequestInfo} or {URL}, makes a request and -* for everything else, calls `WebAssembly.instantiate` directly. -* -* @param {{ module_or_path: InitInput | Promise }} module_or_path - Passing `InitInput` directly is deprecated. -* -* @returns {Promise} -*/ + * If `module_or_path` is {RequestInfo} or {URL}, makes a request and + * for everything else, calls `WebAssembly.instantiate` directly. + * + * @param {{ module_or_path: InitInput | Promise }} module_or_path - Passing `InitInput` directly is deprecated. + * + * @returns {Promise} + */ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise } | InitInput | Promise): Promise; diff --git a/packages/wasm-lib/wasm_wrappers.js b/packages/wasm-lib/wasm_wrappers.js index e294bf6..80fc618 100644 --- a/packages/wasm-lib/wasm_wrappers.js +++ b/packages/wasm-lib/wasm_wrappers.js @@ -1,163 +1,451 @@ -let wasm; +/* @ts-self-types="./wasm_wrappers.d.ts" */ -function addToExternrefTable0(obj) { - const idx = wasm.__externref_table_alloc(); - wasm.__wbindgen_export_2.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); +/** + * Amount type abstraction. The amount type is stored in a string + * since JavaScript number type cannot fit 128-bit integers. + * The amount is given as an integer in units of "atoms". + * Atoms are the smallest, indivisible amount of a coin or token. + */ +export class Amount { + static __wrap(ptr) { + ptr = ptr >>> 0; + const obj = Object.create(Amount.prototype); + obj.__wbg_ptr = ptr; + AmountFinalization.register(obj, obj.__wbg_ptr, obj); + return obj; + } + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + AmountFinalization.unregister(this); + return ptr; + } + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_amount_free(ptr, 0); + } + /** + * @returns {string} + */ + atoms() { + let deferred1_0; + let deferred1_1; + try { + const ptr = this.__destroy_into_raw(); + const ret = wasm.amount_atoms(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 {string} atoms + * @returns {Amount} + */ + static from_atoms(atoms) { + const ptr0 = passStringToWasm0(atoms, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.amount_from_atoms(ptr0, len0); + return Amount.__wrap(ret); } } +if (Symbol.dispose) Amount.prototype[Symbol.dispose] = Amount.prototype.free; -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(); }; +/** + * Indicates whether a token can be frozen + * @enum {0 | 1} + */ +export const FreezableToken = Object.freeze({ + No: 0, "0": "No", + Yes: 1, "1": "Yes", +}); -let cachedUint8ArrayMemory0 = null; +/** + * The network, for which an operation to be done. Mainnet, testnet, etc. + * @enum {0 | 1 | 2 | 3} + */ +export const Network = Object.freeze({ + Mainnet: 0, "0": "Mainnet", + Testnet: 1, "1": "Testnet", + Regtest: 2, "2": "Regtest", + Signet: 3, "3": "Signet", +}); -function getUint8ArrayMemory0() { - if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) { - cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer); - } - return cachedUint8ArrayMemory0; -} +/** + * The part of the transaction that will be committed in the signature. Similar to bitcoin's sighash. + * @enum {0 | 1 | 2 | 3} + */ +export const SignatureHashType = Object.freeze({ + ALL: 0, "0": "ALL", + NONE: 1, "1": "NONE", + SINGLE: 2, "2": "SINGLE", + ANYONECANPAY: 3, "3": "ANYONECANPAY", +}); -function getStringFromWasm0(ptr, len) { - ptr = ptr >>> 0; - return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len)); -} +/** + * A utxo can either come from a transaction or a block reward. This enum signifies that. + * @enum {0 | 1} + */ +export const SourceId = Object.freeze({ + Transaction: 0, "0": "Transaction", + BlockReward: 1, "1": "BlockReward", +}); -function isLikeNone(x) { - return x === undefined || x === null; -} +/** + * Indicates whether a token can be unfrozen once frozen + * @enum {0 | 1} + */ +export const TokenUnfreezable = Object.freeze({ + No: 0, "0": "No", + Yes: 1, "1": "Yes", +}); -let WASM_VECTOR_LEN = 0; +/** + * The token supply of a specific token, set on issuance + * @enum {0 | 1 | 2} + */ +export const TotalSupply = Object.freeze({ + /** + * Can be issued with no limit, but then can be locked to have a fixed supply. + */ + Lockable: 0, "0": "Lockable", + /** + * Unlimited supply, no limits except for numeric limits due to u128 + */ + Unlimited: 1, "1": "Unlimited", + /** + * On issuance, the total number of coins is fixed + */ + Fixed: 2, "2": "Fixed", +}); -const cachedTextEncoder = (typeof TextEncoder !== 'undefined' ? new TextEncoder('utf-8') : { encode: () => { throw Error('TextEncoder not available') } } ); +/** + * Returns the fee that needs to be paid by a transaction for issuing a data deposit + * @param {bigint} current_block_height + * @param {Network} network + * @returns {Amount} + */ +export function data_deposit_fee(current_block_height, network) { + const ret = wasm.data_deposit_fee(current_block_height, network); + return Amount.__wrap(ret); +} -const encodeString = (typeof cachedTextEncoder.encodeInto === 'function' - ? function (arg, view) { - return cachedTextEncoder.encodeInto(arg, view); +/** + * Decodes a partially signed transaction from its binary encoding into a JavaScript object. + * @param {Uint8Array} transaction + * @param {Network} network + * @returns {any} + */ +export function decode_partially_signed_transaction_to_js(transaction, network) { + const ptr0 = passArray8ToWasm0(transaction, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.decode_partially_signed_transaction_to_js(ptr0, len0, network); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return takeFromExternrefTable0(ret[0]); } - : function (arg, view) { - const buf = cachedTextEncoder.encode(arg); - view.set(buf); - return { - read: arg.length, - written: buf.length - }; -}); -function passStringToWasm0(arg, malloc, realloc) { +/** + * Decodes a signed transaction from its binary encoding into a JavaScript object. + * @param {Uint8Array} transaction + * @param {Network} network + * @returns {any} + */ +export function decode_signed_transaction_to_js(transaction, network) { + const ptr0 = passArray8ToWasm0(transaction, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.decode_signed_transaction_to_js(ptr0, len0, network); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return takeFromExternrefTable0(ret[0]); +} - 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; +/** + * Decodes an unsigned transaction from its binary encoding into a JavaScript object. + * @param {Uint8Array} transaction + * @param {Network} network + * @returns {any} + */ +export function decode_transaction_to_js(transaction, network) { + const ptr0 = passArray8ToWasm0(transaction, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.decode_transaction_to_js(ptr0, len0, network); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); } + return takeFromExternrefTable0(ret[0]); +} - let len = arg.length; - let ptr = malloc(len, 1) >>> 0; +/** + * Calculate the "effective balance" of a pool, given the total pool balance and pledge by the pool owner/staker. + * The effective balance is how the influence of a pool is calculated due to its balance. + * @param {Network} network + * @param {Amount} pledge_amount + * @param {Amount} pool_balance + * @returns {Amount} + */ +export function effective_pool_balance(network, pledge_amount, pool_balance) { + _assertClass(pledge_amount, Amount); + var ptr0 = pledge_amount.__destroy_into_raw(); + _assertClass(pool_balance, Amount); + var ptr1 = pool_balance.__destroy_into_raw(); + const ret = wasm.effective_pool_balance(network, ptr0, ptr1); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return Amount.__wrap(ret[0]); +} - const mem = getUint8ArrayMemory0(); +/** + * Given ask and give amounts and a conclude key create output that creates an order. + * + * 'ask_token_id': the parameter represents a Token if it's Some and coins otherwise. + * 'give_token_id': the parameter represents a Token if it's Some and coins otherwise. + * @param {Amount} ask_amount + * @param {string | null | undefined} ask_token_id + * @param {Amount} give_amount + * @param {string | null | undefined} give_token_id + * @param {string} conclude_address + * @param {Network} network + * @returns {Uint8Array} + */ +export function encode_create_order_output(ask_amount, ask_token_id, give_amount, give_token_id, conclude_address, network) { + _assertClass(ask_amount, Amount); + var ptr0 = ask_amount.__destroy_into_raw(); + var ptr1 = isLikeNone(ask_token_id) ? 0 : passStringToWasm0(ask_token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + var len1 = WASM_VECTOR_LEN; + _assertClass(give_amount, Amount); + var ptr2 = give_amount.__destroy_into_raw(); + var ptr3 = isLikeNone(give_token_id) ? 0 : passStringToWasm0(give_token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + var len3 = WASM_VECTOR_LEN; + const ptr4 = passStringToWasm0(conclude_address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len4 = WASM_VECTOR_LEN; + const ret = wasm.encode_create_order_output(ptr0, ptr1, len1, ptr2, ptr3, len3, ptr4, len4, network); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v6 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v6; +} - let offset = 0; +/** + * Convert the specified string address into a Destination object, encoded as bytes. + * @param {string} address + * @param {Network} network + * @returns {Uint8Array} + */ +export function encode_destination(address, network) { + const ptr0 = passStringToWasm0(address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.encode_destination(ptr0, len0, network); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v2; +} - for (; offset < len; offset++) { - const code = arg.charCodeAt(offset); - if (code > 0x7F) break; - mem[ptr + offset] = code; +/** + * Given a token_id, new authority destination and nonce return an encoded change token authority input + * @param {string} token_id + * @param {string} new_authority + * @param {bigint} nonce + * @param {Network} network + * @returns {Uint8Array} + */ +export function encode_input_for_change_token_authority(token_id, new_authority, nonce, network) { + const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(new_authority, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.encode_input_for_change_token_authority(ptr0, len0, ptr1, len1, nonce, network); + 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; +} - 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; +/** + * Given a token_id, new metadata uri and nonce return an encoded change token metadata uri input + * @param {string} token_id + * @param {string} new_metadata_uri + * @param {bigint} nonce + * @param {Network} network + * @returns {Uint8Array} + */ +export function encode_input_for_change_token_metadata_uri(token_id, new_metadata_uri, nonce, network) { + const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(new_metadata_uri, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.encode_input_for_change_token_metadata_uri(ptr0, len0, ptr1, len1, nonce, network); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); } - - WASM_VECTOR_LEN = offset; - return ptr; + var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v3; } -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); +/** + * Given an order id create an input that concludes the order. + * + * Note: the nonce is only needed before the orders V1 fork activation. After the fork the nonce is + * ignored and any value can be passed for the parameter. + * @param {string} order_id + * @param {bigint} nonce + * @param {bigint} current_block_height + * @param {Network} network + * @returns {Uint8Array} + */ +export function encode_input_for_conclude_order(order_id, nonce, current_block_height, network) { + const ptr0 = passStringToWasm0(order_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.encode_input_for_conclude_order(ptr0, len0, nonce, current_block_height, network); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); } - return cachedDataViewMemory0; + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v2; } -function passArray8ToWasm0(arg, malloc) { - const ptr = malloc(arg.length * 1, 1) >>> 0; - getUint8ArrayMemory0().set(arg, ptr / 1); - WASM_VECTOR_LEN = arg.length; - return ptr; +/** + * Given an order id and an amount in the order's ask currency, create an input that fills the order. + * + * Note: + * 1) The nonce is only needed before the orders V1 fork activation. After the fork the nonce is + * ignored and any value can be passed for the parameter. + * 2) FillOrder inputs should not be signed, i.e. use `encode_witness_no_signature` for the inputs + * instead of `encode_witness`). + * Note that in orders v0 FillOrder inputs can technically have a signature, it's just not checked. + * But in orders V1 we actually require that those inputs don't have signatures. + * Also, in orders V1 the provided destination is always ignored. + * @param {string} order_id + * @param {Amount} fill_amount + * @param {string} destination + * @param {bigint} nonce + * @param {bigint} current_block_height + * @param {Network} network + * @returns {Uint8Array} + */ +export function encode_input_for_fill_order(order_id, fill_amount, destination, nonce, current_block_height, network) { + const ptr0 = passStringToWasm0(order_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + _assertClass(fill_amount, Amount); + var ptr1 = fill_amount.__destroy_into_raw(); + const ptr2 = passStringToWasm0(destination, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len2 = WASM_VECTOR_LEN; + const ret = wasm.encode_input_for_fill_order(ptr0, len0, ptr1, ptr2, len2, nonce, current_block_height, network); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v4 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v4; } -function getArrayU8FromWasm0(ptr, len) { - ptr = ptr >>> 0; - return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len); +/** + * Given an order id create an input that freezes the order. + * + * Note: order freezing is available only after the orders V1 fork activation. + * @param {string} order_id + * @param {bigint} current_block_height + * @param {Network} network + * @returns {Uint8Array} + */ +export function encode_input_for_freeze_order(order_id, current_block_height, network) { + const ptr0 = passStringToWasm0(order_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.encode_input_for_freeze_order(ptr0, len0, current_block_height, network); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v2; } + /** - * A utxo can either come from a transaction or a block reward. - * Given a source id, whether from a block reward or transaction, this function - * takes a generic id with it, and returns serialized binary data of the id - * with the given source id. - * @param {Uint8Array} id - * @param {SourceId} source + * Given a token_id, is token unfreezable and nonce return an encoded freeze token input + * @param {string} token_id + * @param {TokenUnfreezable} is_token_unfreezable + * @param {bigint} nonce + * @param {Network} network * @returns {Uint8Array} */ -export function encode_outpoint_source_id(id, source) { - const ptr0 = passArray8ToWasm0(id, wasm.__wbindgen_malloc); +export function encode_input_for_freeze_token(token_id, is_token_unfreezable, nonce, network) { + const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; - const ret = wasm.encode_outpoint_source_id(ptr0, len0, source); + const ret = wasm.encode_input_for_freeze_token(ptr0, len0, is_token_unfreezable, nonce, network); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v2; } /** - * Generates a new, random private key from entropy + * Given a token_id and nonce return an encoded lock_token_supply input + * @param {string} token_id + * @param {bigint} nonce + * @param {Network} network * @returns {Uint8Array} */ -export function make_private_key() { - const ret = wasm.make_private_key(); - var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); +export function encode_input_for_lock_token_supply(token_id, nonce, network) { + const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.encode_input_for_lock_token_supply(ptr0, len0, nonce, network); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v1; + return v2; } -function takeFromExternrefTable0(idx) { - const value = wasm.__wbindgen_export_2.get(idx); - wasm.__externref_table_dealloc(idx); - return value; +/** + * Given a token_id, an amount of tokens to mint and nonce return an encoded mint tokens input + * @param {string} token_id + * @param {Amount} amount + * @param {bigint} nonce + * @param {Network} network + * @returns {Uint8Array} + */ +export function encode_input_for_mint_tokens(token_id, amount, nonce, network) { + const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + _assertClass(amount, Amount); + var ptr1 = amount.__destroy_into_raw(); + const ret = wasm.encode_input_for_mint_tokens(ptr0, len0, ptr1, nonce, network); + 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; } + /** - * Create the default account's extended private key for a given mnemonic - * derivation path: 44'/mintlayer_coin_type'/0' - * @param {string} mnemonic + * Given a token_id and nonce return an encoded unfreeze token input + * @param {string} token_id + * @param {bigint} nonce * @param {Network} network * @returns {Uint8Array} */ -export function make_default_account_privkey(mnemonic, network) { - const ptr0 = passStringToWasm0(mnemonic, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +export function encode_input_for_unfreeze_token(token_id, nonce, network) { + const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; - const ret = wasm.make_default_account_privkey(ptr0, len0, network); + const ret = wasm.encode_input_for_unfreeze_token(ptr0, len0, nonce, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } @@ -167,16 +455,16 @@ export function make_default_account_privkey(mnemonic, network) { } /** - * From an extended private key create a receiving private key for a given key index - * derivation path: current_derivation_path/0/key_index - * @param {Uint8Array} private_key_bytes - * @param {number} key_index + * Given a token_id and nonce return an encoded unmint tokens input + * @param {string} token_id + * @param {bigint} nonce + * @param {Network} network * @returns {Uint8Array} */ -export function make_receiving_address(private_key_bytes, key_index) { - const ptr0 = passArray8ToWasm0(private_key_bytes, wasm.__wbindgen_malloc); +export function encode_input_for_unmint_tokens(token_id, nonce, network) { + const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; - const ret = wasm.make_receiving_address(ptr0, len0, key_index); + const ret = wasm.encode_input_for_unmint_tokens(ptr0, len0, nonce, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } @@ -186,16 +474,16 @@ export function make_receiving_address(private_key_bytes, key_index) { } /** - * From an extended private key create a change private key for a given key index - * derivation path: current_derivation_path/1/key_index - * @param {Uint8Array} private_key_bytes - * @param {number} key_index + * Given an output source id as bytes, and an output index, together representing a utxo, + * this function returns the input that puts them together, as bytes. + * @param {Uint8Array} outpoint_source_id + * @param {number} output_index * @returns {Uint8Array} */ -export function make_change_address(private_key_bytes, key_index) { - const ptr0 = passArray8ToWasm0(private_key_bytes, wasm.__wbindgen_malloc); +export function encode_input_for_utxo(outpoint_source_id, output_index) { + const ptr0 = passArray8ToWasm0(outpoint_source_id, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ret = wasm.make_change_address(ptr0, len0, key_index); + const ret = wasm.encode_input_for_utxo(ptr0, len0, output_index); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } @@ -205,59 +493,96 @@ export function make_change_address(private_key_bytes, key_index) { } /** - * Given a public key (as bytes) and a network type (mainnet, testnet, etc), - * return the address public key hash from that public key as an address - * @param {Uint8Array} public_key_bytes + * Given a delegation id, an amount and a network type (mainnet, testnet, etc), this function + * creates an input that withdraws from a delegation. + * A nonce is needed because this spends from an account. The nonce must be in sequence for everything in that account. + * @param {string} delegation_id + * @param {Amount} amount + * @param {bigint} nonce * @param {Network} network - * @returns {string} + * @returns {Uint8Array} */ -export function pubkey_to_pubkeyhash_address(public_key_bytes, network) { - let deferred3_0; - let deferred3_1; - try { - const ptr0 = passArray8ToWasm0(public_key_bytes, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.pubkey_to_pubkeyhash_address(ptr0, len0, network); - var ptr2 = ret[0]; - var len2 = ret[1]; - if (ret[3]) { - ptr2 = 0; len2 = 0; - throw takeFromExternrefTable0(ret[2]); - } - deferred3_0 = ptr2; - deferred3_1 = len2; - return getStringFromWasm0(ptr2, len2); - } finally { - wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); +export function encode_input_for_withdraw_from_delegation(delegation_id, amount, nonce, network) { + const ptr0 = passStringToWasm0(delegation_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + _assertClass(amount, Amount); + var ptr1 = amount.__destroy_into_raw(); + const ret = wasm.encode_input_for_withdraw_from_delegation(ptr0, len0, ptr1, nonce, network); + 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; +} + +/** + * Given a number of blocks, this function returns the output timelock + * which is used in locked outputs to lock an output for a given number of blocks + * since that output's transaction is included the blockchain + * @param {bigint} block_count + * @returns {Uint8Array} + */ +export function encode_lock_for_block_count(block_count) { + const ret = wasm.encode_lock_for_block_count(block_count); + var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v1; +} + +/** + * Given a number of clock seconds, this function returns the output timelock + * which is used in locked outputs to lock an output for a given number of seconds + * since that output's transaction is included in the blockchain + * @param {bigint} total_seconds + * @returns {Uint8Array} + */ +export function encode_lock_for_seconds(total_seconds) { + const ret = wasm.encode_lock_for_seconds(total_seconds); + var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v1; } /** - * Given a private key, as bytes, return the bytes of the corresponding public key - * @param {Uint8Array} private_key + * Given a block height, this function returns the output timelock which is used in + * locked outputs to lock an output until that block height is reached. + * @param {bigint} block_height * @returns {Uint8Array} */ -export function public_key_from_private_key(private_key) { - const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.public_key_from_private_key(ptr0, len0); - if (ret[3]) { - throw takeFromExternrefTable0(ret[2]); - } - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); +export function encode_lock_until_height(block_height) { + const ret = wasm.encode_lock_until_height(block_height); + var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; + return v1; } /** - * Return the extended public key from an extended private key - * @param {Uint8Array} private_key_bytes + * Given a timestamp represented by as unix timestamp, i.e., number of seconds since unix epoch, + * this function returns the output timelock which is used in locked outputs to lock an output + * until the given timestamp + * @param {bigint} timestamp_since_epoch_in_seconds + * @returns {Uint8Array} + */ +export function encode_lock_until_time(timestamp_since_epoch_in_seconds) { + const ret = wasm.encode_lock_until_time(timestamp_since_epoch_in_seconds); + var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v1; +} + +/** + * Given an arbitrary number of public keys as bytes, number of minimum required signatures, and a network type, this function returns + * the multisig challenge, as bytes. + * @param {Uint8Array} public_keys + * @param {number} min_required_signatures + * @param {Network} network * @returns {Uint8Array} */ -export function extended_public_key_from_extended_private_key(private_key_bytes) { - const ptr0 = passArray8ToWasm0(private_key_bytes, wasm.__wbindgen_malloc); +export function encode_multisig_challenge(public_keys, min_required_signatures, network) { + const ptr0 = passArray8ToWasm0(public_keys, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ret = wasm.extended_public_key_from_extended_private_key(ptr0, len0); + const ret = wasm.encode_multisig_challenge(ptr0, len0, min_required_signatures, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } @@ -267,35 +592,32 @@ export function extended_public_key_from_extended_private_key(private_key_bytes) } /** - * From an extended public key create a receiving public key for a given key index - * derivation path: current_derivation_path/0/key_index - * @param {Uint8Array} extended_public_key_bytes - * @param {number} key_index + * A utxo can either come from a transaction or a block reward. + * Given a source id, whether from a block reward or transaction, this function + * takes a generic id with it, and returns serialized binary data of the id + * with the given source id. + * @param {Uint8Array} id + * @param {SourceId} source * @returns {Uint8Array} */ -export function make_receiving_address_public_key(extended_public_key_bytes, key_index) { - const ptr0 = passArray8ToWasm0(extended_public_key_bytes, wasm.__wbindgen_malloc); +export function encode_outpoint_source_id(id, source) { + const ptr0 = passArray8ToWasm0(id, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ret = wasm.make_receiving_address_public_key(ptr0, len0, key_index); - if (ret[3]) { - throw takeFromExternrefTable0(ret[2]); - } + const ret = wasm.encode_outpoint_source_id(ptr0, len0, source); var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); return v2; } /** - * From an extended public key create a change public key for a given key index - * derivation path: current_derivation_path/1/key_index - * @param {Uint8Array} extended_public_key_bytes - * @param {number} key_index + * Given an amount, this function creates an output (as bytes) to burn a given amount of coins + * @param {Amount} amount * @returns {Uint8Array} */ -export function make_change_address_public_key(extended_public_key_bytes, key_index) { - const ptr0 = passArray8ToWasm0(extended_public_key_bytes, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.make_change_address_public_key(ptr0, len0, key_index); +export function encode_output_coin_burn(amount) { + _assertClass(amount, Amount); + var ptr0 = amount.__destroy_into_raw(); + const ret = wasm.encode_output_coin_burn(ptr0); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } @@ -305,19 +627,20 @@ export function make_change_address_public_key(extended_public_key_bytes, key_in } /** - * Given a message and a private key, sign the message with the given private key - * This kind of signature is to be used when signing spend requests, such as transaction - * input witness. - * @param {Uint8Array} private_key - * @param {Uint8Array} message + * Given a pool id as string, an owner address and a network type (mainnet, testnet, etc), + * this function returns an output (as bytes) to create a delegation to the given pool. + * The owner address is the address that is authorized to withdraw from that delegation. + * @param {string} pool_id + * @param {string} owner_address + * @param {Network} network * @returns {Uint8Array} */ -export function sign_message_for_spending(private_key, message) { - const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); +export function encode_output_create_delegation(pool_id, owner_address, network) { + const ptr0 = passStringToWasm0(pool_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; - const ptr1 = passArray8ToWasm0(message, wasm.__wbindgen_malloc); + const ptr1 = passStringToWasm0(owner_address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len1 = WASM_VECTOR_LEN; - const ret = wasm.sign_message_for_spending(ptr0, len0, ptr1, len1); + const ret = wasm.encode_output_create_delegation(ptr0, len0, ptr1, len1, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } @@ -327,43 +650,24 @@ export function sign_message_for_spending(private_key, message) { } /** - * Given a digital signature, a public key and a message. Verify that - * the signature is produced by signing the message with the private key - * that derived the given public key. - * Note that this function is used for verifying messages related to spending, - * such as transaction input witness. - * @param {Uint8Array} public_key - * @param {Uint8Array} signature - * @param {Uint8Array} message - * @returns {boolean} - */ -export function verify_signature_for_spending(public_key, signature, message) { - const ptr0 = passArray8ToWasm0(public_key, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ptr1 = passArray8ToWasm0(signature, wasm.__wbindgen_malloc); - const len1 = WASM_VECTOR_LEN; - const ptr2 = passArray8ToWasm0(message, wasm.__wbindgen_malloc); - const len2 = WASM_VECTOR_LEN; - const ret = wasm.verify_signature_for_spending(ptr0, len0, ptr1, len1, ptr2, len2); - if (ret[2]) { - throw takeFromExternrefTable0(ret[1]); - } - return ret[0] !== 0; -} - -/** - * Given a message and a private key, create and sign a challenge with the given private key. - * This kind of signature is to be used when signing challenges. - * @param {Uint8Array} private_key - * @param {Uint8Array} message + * Given a pool id, staking data as bytes and the network type (mainnet, testnet, etc), + * this function returns an output that creates that staking pool. + * Note that the pool id is mandated to be taken from the hash of the first input. + * It is not arbitrary. + * + * Note: a UTXO of this kind is consumed when decommissioning a pool (provided that the pool + * never staked). + * @param {string} pool_id + * @param {Uint8Array} pool_data + * @param {Network} network * @returns {Uint8Array} */ -export function sign_challenge(private_key, message) { - const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); +export function encode_output_create_stake_pool(pool_id, pool_data, network) { + const ptr0 = passStringToWasm0(pool_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; - const ptr1 = passArray8ToWasm0(message, wasm.__wbindgen_malloc); + const ptr1 = passArray8ToWasm0(pool_data, wasm.__wbindgen_malloc); const len1 = WASM_VECTOR_LEN; - const ret = wasm.sign_challenge(ptr0, len0, ptr1, len1); + const ret = wasm.encode_output_create_stake_pool(ptr0, len0, ptr1, len1, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } @@ -373,47 +677,36 @@ export function sign_challenge(private_key, message) { } /** - * Given a signed challenge, an address and a message, verify that - * the signature is produced by signing the message with the private key - * that derived the given public key. - * This function is used for verifying messages-related challenges. - * - * Note: for signatures that were created by `sign_challenge`, the provided address must be - * a 'pubkeyhash' address. - * - * Note: currently this function never returns `false` - it either returns `true` or fails with an error. - * @param {string} address - * @param {Network} network - * @param {Uint8Array} signed_challenge - * @param {Uint8Array} message - * @returns {boolean} + * Given data to be deposited in the blockchain, this function provides the output that deposits this data + * @param {Uint8Array} data + * @returns {Uint8Array} */ -export function verify_challenge(address, network, signed_challenge, message) { - const ptr0 = passStringToWasm0(address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +export function encode_output_data_deposit(data) { + const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ptr1 = passArray8ToWasm0(signed_challenge, wasm.__wbindgen_malloc); - const len1 = WASM_VECTOR_LEN; - const ptr2 = passArray8ToWasm0(message, wasm.__wbindgen_malloc); - const len2 = WASM_VECTOR_LEN; - const ret = wasm.verify_challenge(ptr0, len0, network, ptr1, len1, ptr2, len2); - if (ret[2]) { - throw takeFromExternrefTable0(ret[1]); + const ret = wasm.encode_output_data_deposit(ptr0, len0); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); } - return ret[0] !== 0; + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v2; } /** - * Return the message that has to be signed to produce a signed transaction intent. - * @param {string} intent - * @param {string} transaction_id + * Given a delegation id (as string, in address form), an amount and a network type (mainnet, testnet, etc), + * this function returns an output (as bytes) that would delegate coins to be staked in the specified delegation id. + * @param {Amount} amount + * @param {string} delegation_id + * @param {Network} network * @returns {Uint8Array} */ -export function make_transaction_intent_message_to_sign(intent, transaction_id) { - const ptr0 = passStringToWasm0(intent, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - const ptr1 = passStringToWasm0(transaction_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +export function encode_output_delegate_staking(amount, delegation_id, network) { + _assertClass(amount, Amount); + var ptr0 = amount.__destroy_into_raw(); + const ptr1 = passStringToWasm0(delegation_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len1 = WASM_VECTOR_LEN; - const ret = wasm.make_transaction_intent_message_to_sign(ptr0, len0, ptr1, len1); + const ret = wasm.encode_output_delegate_staking(ptr0, ptr1, len1, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } @@ -423,114 +716,139 @@ export function make_transaction_intent_message_to_sign(intent, transaction_id) } /** - * Return a `SignedTransactionIntent` object as bytes given the message and encoded signatures. - * - * Note: to produce a valid signed intent one is expected to sign the corresponding message by private keys - * corresponding to each input of the transaction. - * - * Parameters: - * `signed_message` - this must have been produced by `make_transaction_intent_message_to_sign`. - * `signatures` - this should be an array of arrays of bytes, each of them representing an individual signature - * of `signed_message` produced by `sign_challenge` using the private key for the corresponding input destination - * of the transaction. The number of signatures must be equal to the number of inputs in the transaction. - * @param {Uint8Array} signed_message - * @param {any} signatures + * Given the parameters needed to create hash timelock contract, and a network type (mainnet, testnet, etc), + * this function creates an output. + * @param {Amount} amount + * @param {string | null | undefined} token_id + * @param {string} secret_hash + * @param {string} spend_address + * @param {string} refund_address + * @param {Uint8Array} refund_timelock + * @param {Network} network * @returns {Uint8Array} */ -export function encode_signed_transaction_intent(signed_message, signatures) { - const ptr0 = passArray8ToWasm0(signed_message, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.encode_signed_transaction_intent(ptr0, len0, signatures); +export function encode_output_htlc(amount, token_id, secret_hash, spend_address, refund_address, refund_timelock, network) { + _assertClass(amount, Amount); + var ptr0 = amount.__destroy_into_raw(); + var ptr1 = isLikeNone(token_id) ? 0 : passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + var len1 = WASM_VECTOR_LEN; + const ptr2 = passStringToWasm0(secret_hash, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len2 = WASM_VECTOR_LEN; + const ptr3 = passStringToWasm0(spend_address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len3 = WASM_VECTOR_LEN; + const ptr4 = passStringToWasm0(refund_address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len4 = WASM_VECTOR_LEN; + const ptr5 = passArray8ToWasm0(refund_timelock, wasm.__wbindgen_malloc); + const len5 = WASM_VECTOR_LEN; + const ret = wasm.encode_output_htlc(ptr0, ptr1, len1, ptr2, len2, ptr3, len3, ptr4, len4, ptr5, len5, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v7 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; -} - -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; -} -/** - * Verify a signed transaction intent. - * - * Parameters: - * `expected_signed_message` - the message that is supposed to be signed; this must have been - * produced by `make_transaction_intent_message_to_sign`. - * `encoded_signed_intent` - the signed transaction intent produced by `encode_signed_transaction_intent`. - * `input_destinations` - an array of addresses (strings), corresponding to the transaction's input destinations - * (note that this function treats "pub key" and "pub key hash" addresses interchangeably, so it's ok to pass - * one instead of the other). - * `network` - the network being used (needed to decode the addresses). - * @param {Uint8Array} expected_signed_message - * @param {Uint8Array} encoded_signed_intent - * @param {string[]} input_destinations + return v7; +} + +/** + * Given the parameters needed to issue a fungible token, and a network type (mainnet, testnet, etc), + * this function creates an output that issues that token. + * @param {string} authority + * @param {string} token_ticker + * @param {string} metadata_uri + * @param {number} number_of_decimals + * @param {TotalSupply} total_supply + * @param {Amount | null | undefined} supply_amount + * @param {FreezableToken} is_token_freezable + * @param {bigint} _current_block_height * @param {Network} network + * @returns {Uint8Array} */ -export function verify_transaction_intent(expected_signed_message, encoded_signed_intent, input_destinations, network) { - const ptr0 = passArray8ToWasm0(expected_signed_message, wasm.__wbindgen_malloc); +export function encode_output_issue_fungible_token(authority, token_ticker, metadata_uri, number_of_decimals, total_supply, supply_amount, is_token_freezable, _current_block_height, network) { + const ptr0 = passStringToWasm0(authority, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; - const ptr1 = passArray8ToWasm0(encoded_signed_intent, wasm.__wbindgen_malloc); + const ptr1 = passStringToWasm0(token_ticker, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len1 = WASM_VECTOR_LEN; - const ptr2 = passArrayJsValueToWasm0(input_destinations, wasm.__wbindgen_malloc); + const ptr2 = passStringToWasm0(metadata_uri, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len2 = WASM_VECTOR_LEN; - const ret = wasm.verify_transaction_intent(ptr0, len0, ptr1, len1, ptr2, len2, network); - if (ret[1]) { - throw takeFromExternrefTable0(ret[0]); + let ptr3 = 0; + if (!isLikeNone(supply_amount)) { + _assertClass(supply_amount, Amount); + ptr3 = supply_amount.__destroy_into_raw(); } -} - -function _assertClass(instance, klass) { - if (!(instance instanceof klass)) { - throw new Error(`expected instance of ${klass.name}`); + const ret = wasm.encode_output_issue_fungible_token(ptr0, len0, ptr1, len1, ptr2, len2, number_of_decimals, total_supply, ptr3, is_token_freezable, _current_block_height, network); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); } + var v5 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v5; } + /** - * Given a destination address, an amount and a network type (mainnet, testnet, etc), this function - * creates an output of type Transfer, and returns it as bytes. - * @param {Amount} amount - * @param {string} address + * Given the parameters needed to issue an NFT, and a network type (mainnet, testnet, etc), + * this function creates an output that issues that NFT. + * @param {string} token_id + * @param {string} authority + * @param {string} name + * @param {string} ticker + * @param {string} description + * @param {Uint8Array} media_hash + * @param {Uint8Array | null | undefined} creator + * @param {string | null | undefined} media_uri + * @param {string | null | undefined} icon_uri + * @param {string | null | undefined} additional_metadata_uri + * @param {bigint} _current_block_height * @param {Network} network * @returns {Uint8Array} */ -export function encode_output_transfer(amount, address, network) { - _assertClass(amount, Amount); - var ptr0 = amount.__destroy_into_raw(); - const ptr1 = passStringToWasm0(address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +export function encode_output_issue_nft(token_id, authority, name, ticker, description, media_hash, creator, media_uri, icon_uri, additional_metadata_uri, _current_block_height, network) { + const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(authority, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len1 = WASM_VECTOR_LEN; - const ret = wasm.encode_output_transfer(ptr0, ptr1, len1, network); + const ptr2 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len2 = WASM_VECTOR_LEN; + const ptr3 = passStringToWasm0(ticker, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len3 = WASM_VECTOR_LEN; + const ptr4 = passStringToWasm0(description, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len4 = WASM_VECTOR_LEN; + const ptr5 = passArray8ToWasm0(media_hash, wasm.__wbindgen_malloc); + const len5 = WASM_VECTOR_LEN; + var ptr6 = isLikeNone(creator) ? 0 : passArray8ToWasm0(creator, wasm.__wbindgen_malloc); + var len6 = WASM_VECTOR_LEN; + var ptr7 = isLikeNone(media_uri) ? 0 : passStringToWasm0(media_uri, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + var len7 = WASM_VECTOR_LEN; + var ptr8 = isLikeNone(icon_uri) ? 0 : passStringToWasm0(icon_uri, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + var len8 = WASM_VECTOR_LEN; + var ptr9 = isLikeNone(additional_metadata_uri) ? 0 : passStringToWasm0(additional_metadata_uri, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + var len9 = WASM_VECTOR_LEN; + const ret = wasm.encode_output_issue_nft(ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3, ptr4, len4, ptr5, len5, ptr6, len6, ptr7, len7, ptr8, len8, ptr9, len9, _current_block_height, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v11 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v3; + return v11; } /** - * Given a destination address, an amount, token ID (in address form) and a network type (mainnet, testnet, etc), this function - * creates an output of type Transfer for tokens, and returns it as bytes. + * Given a valid receiving address, and a locking rule as bytes (available in this file), + * and a network type (mainnet, testnet, etc), this function creates an output of type + * LockThenTransfer with the parameters provided. * @param {Amount} amount * @param {string} address - * @param {string} token_id + * @param {Uint8Array} lock * @param {Network} network * @returns {Uint8Array} */ -export function encode_output_token_transfer(amount, address, token_id, network) { +export function encode_output_lock_then_transfer(amount, address, lock, network) { _assertClass(amount, Amount); var ptr0 = amount.__destroy_into_raw(); const ptr1 = passStringToWasm0(address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len1 = WASM_VECTOR_LEN; - const ptr2 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const ptr2 = passArray8ToWasm0(lock, wasm.__wbindgen_malloc); const len2 = WASM_VECTOR_LEN; - const ret = wasm.encode_output_token_transfer(ptr0, ptr1, len1, ptr2, len2, network); + const ret = wasm.encode_output_lock_then_transfer(ptr0, ptr1, len1, ptr2, len2, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } @@ -540,98 +858,50 @@ export function encode_output_token_transfer(amount, address, token_id, network) } /** - * Given the current block height and a network type (mainnet, testnet, etc), - * this function returns the number of blocks, after which a pool that decommissioned, - * will have its funds unlocked and available for spending. - * The current block height information is used in case a network upgrade changed the value. - * @param {bigint} current_block_height + * Given a pool id and a staker address, this function returns an output that is emitted + * when producing a block via that pool. + * + * Note: a UTXO of this kind is consumed when decommissioning a pool (provided that the pool + * has staked at least once). + * @param {string} pool_id + * @param {string} staker * @param {Network} network - * @returns {bigint} - */ -export function staking_pool_spend_maturity_block_count(current_block_height, network) { - const ret = wasm.staking_pool_spend_maturity_block_count(current_block_height, network); - return BigInt.asUintN(64, ret); -} - -/** - * Given a number of blocks, this function returns the output timelock - * which is used in locked outputs to lock an output for a given number of blocks - * since that output's transaction is included the blockchain - * @param {bigint} block_count - * @returns {Uint8Array} - */ -export function encode_lock_for_block_count(block_count) { - const ret = wasm.encode_lock_for_block_count(block_count); - var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v1; -} - -/** - * Given a number of clock seconds, this function returns the output timelock - * which is used in locked outputs to lock an output for a given number of seconds - * since that output's transaction is included in the blockchain - * @param {bigint} total_seconds - * @returns {Uint8Array} - */ -export function encode_lock_for_seconds(total_seconds) { - const ret = wasm.encode_lock_for_seconds(total_seconds); - var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v1; -} - -/** - * Given a timestamp represented by as unix timestamp, i.e., number of seconds since unix epoch, - * this function returns the output timelock which is used in locked outputs to lock an output - * until the given timestamp - * @param {bigint} timestamp_since_epoch_in_seconds - * @returns {Uint8Array} - */ -export function encode_lock_until_time(timestamp_since_epoch_in_seconds) { - const ret = wasm.encode_lock_until_time(timestamp_since_epoch_in_seconds); - var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v1; -} - -/** - * Given a block height, this function returns the output timelock which is used in - * locked outputs to lock an output until that block height is reached. - * @param {bigint} block_height * @returns {Uint8Array} */ -export function encode_lock_until_height(block_height) { - const ret = wasm.encode_lock_until_height(block_height); - var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); +export function encode_output_produce_block_from_stake(pool_id, staker, network) { + const ptr0 = passStringToWasm0(pool_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(staker, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.encode_output_produce_block_from_stake(ptr0, len0, ptr1, len1, network); + 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 v1; + return v3; } /** - * Given a valid receiving address, and a locking rule as bytes (available in this file), - * and a network type (mainnet, testnet, etc), this function creates an output of type - * LockThenTransfer with the parameters provided. + * Given an amount, token ID (in address form) and network type (mainnet, testnet, etc), + * this function creates an output (as bytes) to burn a given amount of tokens * @param {Amount} amount - * @param {string} address - * @param {Uint8Array} lock + * @param {string} token_id * @param {Network} network * @returns {Uint8Array} */ -export function encode_output_lock_then_transfer(amount, address, lock, network) { +export function encode_output_token_burn(amount, token_id, network) { _assertClass(amount, Amount); var ptr0 = amount.__destroy_into_raw(); - const ptr1 = passStringToWasm0(address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const ptr1 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len1 = WASM_VECTOR_LEN; - const ptr2 = passArray8ToWasm0(lock, wasm.__wbindgen_malloc); - const len2 = WASM_VECTOR_LEN; - const ret = wasm.encode_output_lock_then_transfer(ptr0, ptr1, len1, ptr2, len2, network); + const ret = wasm.encode_output_token_burn(ptr0, ptr1, len1, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v4 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v4; + return v3; } /** @@ -664,36 +934,44 @@ export function encode_output_token_lock_then_transfer(amount, address, token_id } /** - * Given an amount, this function creates an output (as bytes) to burn a given amount of coins + * Given a destination address, an amount, token ID (in address form) and a network type (mainnet, testnet, etc), this function + * creates an output of type Transfer for tokens, and returns it as bytes. * @param {Amount} amount + * @param {string} address + * @param {string} token_id + * @param {Network} network * @returns {Uint8Array} */ -export function encode_output_coin_burn(amount) { +export function encode_output_token_transfer(amount, address, token_id, network) { _assertClass(amount, Amount); var ptr0 = amount.__destroy_into_raw(); - const ret = wasm.encode_output_coin_burn(ptr0); + const ptr1 = passStringToWasm0(address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len2 = WASM_VECTOR_LEN; + const ret = wasm.encode_output_token_transfer(ptr0, ptr1, len1, ptr2, len2, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v4 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; + return v4; } /** - * Given an amount, token ID (in address form) and network type (mainnet, testnet, etc), - * this function creates an output (as bytes) to burn a given amount of tokens + * Given a destination address, an amount and a network type (mainnet, testnet, etc), this function + * creates an output of type Transfer, and returns it as bytes. * @param {Amount} amount - * @param {string} token_id + * @param {string} address * @param {Network} network * @returns {Uint8Array} */ -export function encode_output_token_burn(amount, token_id, network) { +export function encode_output_transfer(amount, address, network) { _assertClass(amount, Amount); var ptr0 = amount.__destroy_into_raw(); - const ptr1 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const ptr1 = passStringToWasm0(address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len1 = WASM_VECTOR_LEN; - const ret = wasm.encode_output_token_burn(ptr0, ptr1, len1, network); + const ret = wasm.encode_output_transfer(ptr0, ptr1, len1, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } @@ -703,20 +981,73 @@ export function encode_output_token_burn(amount, token_id, network) { } /** - * Given a pool id as string, an owner address and a network type (mainnet, testnet, etc), - * this function returns an output (as bytes) to create a delegation to the given pool. - * The owner address is the address that is authorized to withdraw from that delegation. - * @param {string} pool_id - * @param {string} owner_address + * Return a PartiallySignedTransaction object as bytes. + * + * `transaction` is an encoded `Transaction` (which can be produced via `encode_transaction`). + * + * `signatures`, `input_utxos`, `input_destinations` and `htlc_secrets` are encoded lists of + * optional objects of the corresponding type. To produce such a list, iterate over your + * original list of optional objects and then: + * 1) emit byte 0 if the current object is null; + * 2) otherwise emit byte 1 followed by the object in its encoded form. + * + * Each individual object in each of the lists corresponds to the transaction input with the same + * index and its meaning is as follows: + * 1) `signatures` - the signature for the input; + * 2) `input_utxos`- the utxo for the input (if it's utxo-based); + * 3) `input_destinations` - the destination (address) corresponding to the input; this determines + * the key(s) with which the input has to be signed. Note that for utxo-based inputs the + * corresponding destination can usually be extracted from the utxo itself (the exception + * being the `ProduceBlockFromStake` utxo, which doesn't contain the pool's decommission key). + * However, PartiallySignedTransaction requires that *all* input destinations are provided + * explicitly anyway. + * 4) `htlc_secrets` - if the input is an HTLC one and if the transaction is spending the HTLC, + * this should be the HTLC secret. Otherwise it should be null. + * + * The number of items in each list must be equal to the number of transaction inputs. + * + * `additional_info` has the same meaning as in `encode_witness`. + * @param {Uint8Array} transaction + * @param {Uint8Array} signatures + * @param {Uint8Array} input_utxos + * @param {Uint8Array} input_destinations + * @param {Uint8Array} htlc_secrets + * @param {TxAdditionalInfo} additional_info * @param {Network} network * @returns {Uint8Array} */ -export function encode_output_create_delegation(pool_id, owner_address, network) { - const ptr0 = passStringToWasm0(pool_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +export function encode_partially_signed_transaction(transaction, signatures, input_utxos, input_destinations, htlc_secrets, additional_info, network) { + const ptr0 = passArray8ToWasm0(transaction, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(signatures, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passArray8ToWasm0(input_utxos, wasm.__wbindgen_malloc); + const len2 = WASM_VECTOR_LEN; + const ptr3 = passArray8ToWasm0(input_destinations, wasm.__wbindgen_malloc); + const len3 = WASM_VECTOR_LEN; + const ptr4 = passArray8ToWasm0(htlc_secrets, wasm.__wbindgen_malloc); + const len4 = WASM_VECTOR_LEN; + const ret = wasm.encode_partially_signed_transaction(ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3, ptr4, len4, additional_info, network); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v6 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v6; +} + +/** + * Given an unsigned transaction and signatures, this function returns a SignedTransaction object as bytes. + * @param {Uint8Array} transaction + * @param {Uint8Array} signatures + * @returns {Uint8Array} + */ +export function encode_signed_transaction(transaction, signatures) { + const ptr0 = passArray8ToWasm0(transaction, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ptr1 = passStringToWasm0(owner_address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const ptr1 = passArray8ToWasm0(signatures, wasm.__wbindgen_malloc); const len1 = WASM_VECTOR_LEN; - const ret = wasm.encode_output_create_delegation(ptr0, len0, ptr1, len1, network); + const ret = wasm.encode_signed_transaction(ptr0, len0, ptr1, len1); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } @@ -726,19 +1057,26 @@ export function encode_output_create_delegation(pool_id, owner_address, network) } /** - * Given a delegation id (as string, in address form), an amount and a network type (mainnet, testnet, etc), - * this function returns an output (as bytes) that would delegate coins to be staked in the specified delegation id. - * @param {Amount} amount - * @param {string} delegation_id - * @param {Network} network + * Return a `SignedTransactionIntent` object as bytes given the message and encoded signatures. + * + * Note: to produce a valid signed intent one is expected to sign the corresponding message by private keys + * corresponding to each input of the transaction. + * + * Parameters: + * `signed_message` - this must have been produced by `make_transaction_intent_message_to_sign`. + * `signatures` - this should be an array of Uint8Array, each of them representing an individual signature + * of `signed_message` produced by `sign_challenge` using the private key for the corresponding input destination + * of the transaction. The number of signatures must be equal to the number of inputs in the transaction. + * @param {Uint8Array} signed_message + * @param {Uint8Array[]} signatures * @returns {Uint8Array} */ -export function encode_output_delegate_staking(amount, delegation_id, network) { - _assertClass(amount, Amount); - var ptr0 = amount.__destroy_into_raw(); - const ptr1 = passStringToWasm0(delegation_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +export function encode_signed_transaction_intent(signed_message, signatures) { + const ptr0 = passArray8ToWasm0(signed_message, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArrayJsValueToWasm0(signatures, wasm.__wbindgen_malloc); const len1 = WASM_VECTOR_LEN; - const ret = wasm.encode_output_delegate_staking(ptr0, ptr1, len1, network); + const ret = wasm.encode_signed_transaction_intent(ptr0, len0, ptr1, len1); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } @@ -780,21 +1118,19 @@ export function encode_stake_pool_data(value, staker, vrf_public_key, decommissi } /** - * Given a pool id, staking data as bytes and the network type (mainnet, testnet, etc), - * this function returns an output that creates that staking pool. - * Note that the pool id is mandated to be taken from the hash of the first input. - * It is not arbitrary. - * @param {string} pool_id - * @param {Uint8Array} pool_data - * @param {Network} network + * Given inputs as bytes, outputs as bytes, and flags settings, this function returns + * the transaction that contains them all, as bytes. + * @param {Uint8Array} inputs + * @param {Uint8Array} outputs + * @param {bigint} flags * @returns {Uint8Array} */ -export function encode_output_create_stake_pool(pool_id, pool_data, network) { - const ptr0 = passStringToWasm0(pool_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +export function encode_transaction(inputs, outputs, flags) { + const ptr0 = passArray8ToWasm0(inputs, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ptr1 = passArray8ToWasm0(pool_data, wasm.__wbindgen_malloc); + const ptr1 = passArray8ToWasm0(outputs, wasm.__wbindgen_malloc); const len1 = WASM_VECTOR_LEN; - const ret = wasm.encode_output_create_stake_pool(ptr0, len0, ptr1, len1, network); + const ret = wasm.encode_transaction(ptr0, len0, ptr1, len1, flags); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } @@ -804,117 +1140,273 @@ export function encode_output_create_stake_pool(pool_id, pool_data, network) { } /** - * Returns the fee that needs to be paid by a transaction for issuing a new fungible token - * @param {bigint} _current_block_height + * Sign the specified input of the transaction and encode the signature as InputWitness. + * + * `input_utxos` must be formed as follows: for each transaction input, emit byte 0 if it's a non-UTXO input, + * otherwise emit 1 followed by the corresponding transaction output encoded via the appropriate "encode_output_" + * function. + * + * `additional_info` must contain the following: + * 1) for each `ProduceBlockFromStake` input of the transaction, the pool info for the pool referenced by that input; + * 2) for each `FillOrder` and `ConcludeOrder` input of the transaction, the order info for the order referenced by + * that input. + * Note: + * - It doesn't matter which input witness is currently being encoded. E.g. even if you are encoding a witness + * for some UTXO-based input but another input of the same transaction is `FillOrder`, you have to include the order + * info when encoding the witness for the UTXO-based input too. + * - After a certain hard fork, the produced signature will "commit" to the provided additional info, i.e. the info + * will become a part of what is being signed. So, passing invalid additional info will result in an invalid signature + * (with one small caveat: for `FillOrder` we only commit to order's initial balances and not the current ones; + * so if you only have `FillOrder` inputs, you can technically pass bogus values for the current balances and + * the resulting signature will still be valid; though it's better to avoid doing this). + * @param {SignatureHashType} sighashtype + * @param {Uint8Array} private_key + * @param {string} input_owner_destination + * @param {Uint8Array} transaction + * @param {Uint8Array} input_utxos + * @param {number} input_index + * @param {TxAdditionalInfo} additional_info + * @param {bigint} current_block_height * @param {Network} network - * @returns {Amount} + * @returns {Uint8Array} */ -export function fungible_token_issuance_fee(_current_block_height, network) { - const ret = wasm.fungible_token_issuance_fee(_current_block_height, network); - return Amount.__wrap(ret); +export function encode_witness(sighashtype, private_key, input_owner_destination, transaction, input_utxos, input_index, additional_info, current_block_height, network) { + const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(input_owner_destination, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passArray8ToWasm0(transaction, wasm.__wbindgen_malloc); + const len2 = WASM_VECTOR_LEN; + const ptr3 = passArray8ToWasm0(input_utxos, wasm.__wbindgen_malloc); + const len3 = WASM_VECTOR_LEN; + const ret = wasm.encode_witness(sighashtype, ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3, input_index, additional_info, current_block_height, network); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v5 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v5; } /** - * Given the current block height and a network type (mainnet, testnet, etc), - * this will return the fee that needs to be paid by a transaction for issuing a new NFT - * The current block height information is used in case a network upgrade changed the value. + * Sign the specified HTLC input of the transaction and encode the signature as InputWitness. + * + * This function must be used for HTLC refunding when the refund address is a multisig one. + * + * `key_index` parameter is an index of the public key in the multisig challenge corresponding to + * the specified private key. + * `input_witness` parameter can be either empty or a result of previous calls to this function. + * + * `input_utxos` and `additional_info` have the same format and requirements as in `encode_witness`. + * @param {SignatureHashType} sighashtype + * @param {Uint8Array} private_key + * @param {number} key_index + * @param {Uint8Array} input_witness + * @param {Uint8Array} multisig_challenge + * @param {Uint8Array} transaction + * @param {Uint8Array} input_utxos + * @param {number} input_index + * @param {TxAdditionalInfo} additional_info * @param {bigint} current_block_height * @param {Network} network - * @returns {Amount} + * @returns {Uint8Array} */ -export function nft_issuance_fee(current_block_height, network) { - const ret = wasm.nft_issuance_fee(current_block_height, network); - return Amount.__wrap(ret); +export function encode_witness_htlc_refund_multisig(sighashtype, private_key, key_index, input_witness, multisig_challenge, transaction, input_utxos, input_index, additional_info, current_block_height, network) { + const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(input_witness, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passArray8ToWasm0(multisig_challenge, wasm.__wbindgen_malloc); + const len2 = WASM_VECTOR_LEN; + const ptr3 = passArray8ToWasm0(transaction, wasm.__wbindgen_malloc); + const len3 = WASM_VECTOR_LEN; + const ptr4 = passArray8ToWasm0(input_utxos, wasm.__wbindgen_malloc); + const len4 = WASM_VECTOR_LEN; + const ret = wasm.encode_witness_htlc_refund_multisig(sighashtype, ptr0, len0, key_index, ptr1, len1, ptr2, len2, ptr3, len3, ptr4, len4, input_index, additional_info, current_block_height, network); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v6 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v6; } /** - * Given the current block height and a network type (mainnet, testnet, etc), - * this will return the fee that needs to be paid by a transaction for changing the total supply of a token - * by either minting or unminting tokens - * The current block height information is used in case a network upgrade changed the value. + * Sign the specified HTLC input of the transaction and encode the signature as InputWitness. + * + * This function must be used for HTLC refunding when the refund address is a single-sig one. + * + * `input_utxos` and `additional_info` have the same format and requirements as in `encode_witness`. + * @param {SignatureHashType} sighashtype + * @param {Uint8Array} private_key + * @param {string} input_owner_destination + * @param {Uint8Array} transaction + * @param {Uint8Array} input_utxos + * @param {number} input_index + * @param {TxAdditionalInfo} additional_info * @param {bigint} current_block_height * @param {Network} network - * @returns {Amount} + * @returns {Uint8Array} */ -export function token_supply_change_fee(current_block_height, network) { - const ret = wasm.token_supply_change_fee(current_block_height, network); - return Amount.__wrap(ret); +export function encode_witness_htlc_refund_single_sig(sighashtype, private_key, input_owner_destination, transaction, input_utxos, input_index, additional_info, current_block_height, network) { + const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(input_owner_destination, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passArray8ToWasm0(transaction, wasm.__wbindgen_malloc); + const len2 = WASM_VECTOR_LEN; + const ptr3 = passArray8ToWasm0(input_utxos, wasm.__wbindgen_malloc); + const len3 = WASM_VECTOR_LEN; + const ret = wasm.encode_witness_htlc_refund_single_sig(sighashtype, ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3, input_index, additional_info, current_block_height, network); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v5 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v5; } /** - * Given the current block height and a network type (mainnet, testnet, etc), - * this will return the fee that needs to be paid by a transaction for freezing/unfreezing a token - * The current block height information is used in case a network upgrade changed the value. + * Sign the specified HTLC input of the transaction and encode the signature as InputWitness. + * + * This function must be used for HTLC spending. + * + * `input_utxos` and `additional_info` have the same format and requirements as in `encode_witness`. + * @param {SignatureHashType} sighashtype + * @param {Uint8Array} private_key + * @param {string} input_owner_destination + * @param {Uint8Array} transaction + * @param {Uint8Array} input_utxos + * @param {number} input_index + * @param {Uint8Array} secret + * @param {TxAdditionalInfo} additional_info * @param {bigint} current_block_height * @param {Network} network - * @returns {Amount} + * @returns {Uint8Array} */ -export function token_freeze_fee(current_block_height, network) { - const ret = wasm.token_freeze_fee(current_block_height, network); - return Amount.__wrap(ret); +export function encode_witness_htlc_spend(sighashtype, private_key, input_owner_destination, transaction, input_utxos, input_index, secret, additional_info, current_block_height, network) { + const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(input_owner_destination, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passArray8ToWasm0(transaction, wasm.__wbindgen_malloc); + const len2 = WASM_VECTOR_LEN; + const ptr3 = passArray8ToWasm0(input_utxos, wasm.__wbindgen_malloc); + const len3 = WASM_VECTOR_LEN; + const ptr4 = passArray8ToWasm0(secret, wasm.__wbindgen_malloc); + const len4 = WASM_VECTOR_LEN; + const ret = wasm.encode_witness_htlc_spend(sighashtype, ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3, input_index, ptr4, len4, additional_info, current_block_height, network); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v6 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v6; } /** - * Given the current block height and a network type (mainnet, testnet, etc), - * this will return the fee that needs to be paid by a transaction for changing the authority of a token - * The current block height information is used in case a network upgrade changed the value. - * @param {bigint} current_block_height - * @param {Network} network - * @returns {Amount} + * Encode an input witness of the variant that contains no signature. + * @returns {Uint8Array} */ -export function token_change_authority_fee(current_block_height, network) { - const ret = wasm.token_change_authority_fee(current_block_height, network); - return Amount.__wrap(ret); +export function encode_witness_no_signature() { + const ret = wasm.encode_witness_no_signature(); + var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v1; } /** - * Given the parameters needed to issue a fungible token, and a network type (mainnet, testnet, etc), - * this function creates an output that issues that token. - * @param {string} authority - * @param {string} token_ticker - * @param {string} metadata_uri - * @param {number} number_of_decimals - * @param {TotalSupply} total_supply - * @param {Amount | null | undefined} supply_amount - * @param {FreezableToken} is_token_freezable - * @param {bigint} _current_block_height + * Given the inputs, along each input's destination that can spend that input + * (e.g. If we are spending a UTXO in input number 1 and it is owned by address mtc1xxxx, then it is mtc1xxxx in element number 2 in the vector/list. + * for Account inputs that spend from a delegation it is the owning address of that delegation, + * and in the case of AccountCommand inputs which change a token it is the token's authority destination) + * and the outputs, estimate the transaction size. + * ScriptHash and ClassicMultisig destinations are not supported. + * Also, the function assumes that the input UTXOs are not HTLC. + * @param {Uint8Array} inputs + * @param {string[]} input_utxos_destinations + * @param {Uint8Array} outputs * @param {Network} network - * @returns {Uint8Array} + * @returns {number} */ -export function encode_output_issue_fungible_token(authority, token_ticker, metadata_uri, number_of_decimals, total_supply, supply_amount, is_token_freezable, _current_block_height, network) { - const ptr0 = passStringToWasm0(authority, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +export function estimate_transaction_size(inputs, input_utxos_destinations, outputs, network) { + const ptr0 = passArray8ToWasm0(inputs, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ptr1 = passStringToWasm0(token_ticker, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const ptr1 = passArrayJsValueToWasm0(input_utxos_destinations, wasm.__wbindgen_malloc); const len1 = WASM_VECTOR_LEN; - const ptr2 = passStringToWasm0(metadata_uri, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const ptr2 = passArray8ToWasm0(outputs, wasm.__wbindgen_malloc); const len2 = WASM_VECTOR_LEN; - let ptr3 = 0; - if (!isLikeNone(supply_amount)) { - _assertClass(supply_amount, Amount); - ptr3 = supply_amount.__destroy_into_raw(); + const ret = wasm.estimate_transaction_size(ptr0, len0, ptr1, len1, ptr2, len2, network); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); } - const ret = wasm.encode_output_issue_fungible_token(ptr0, len0, ptr1, len1, ptr2, len2, number_of_decimals, total_supply, ptr3, is_token_freezable, _current_block_height, network); + return ret[0] >>> 0; +} + +/** + * Return the extended public key from an extended private key + * @param {Uint8Array} private_key + * @returns {Uint8Array} + */ +export function extended_public_key_from_extended_private_key(private_key) { + const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.extended_public_key_from_extended_private_key(ptr0, len0); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v5 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v5; + return v2; +} + +/** + * Given a signed transaction and input outpoint that spends an htlc utxo, extract a secret that is + * encoded in the corresponding input signature + * @param {Uint8Array} signed_tx + * @param {boolean} strict_byte_size + * @param {Uint8Array} htlc_outpoint_source_id + * @param {number} htlc_output_index + * @returns {Uint8Array} + */ +export function extract_htlc_secret(signed_tx, strict_byte_size, htlc_outpoint_source_id, htlc_output_index) { + const ptr0 = passArray8ToWasm0(signed_tx, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(htlc_outpoint_source_id, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.extract_htlc_secret(ptr0, len0, strict_byte_size, ptr1, len1, htlc_output_index); + 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; +} + +/** + * Returns the fee that needs to be paid by a transaction for issuing a new fungible token + * @param {bigint} _current_block_height + * @param {Network} network + * @returns {Amount} + */ +export function fungible_token_issuance_fee(_current_block_height, network) { + const ret = wasm.fungible_token_issuance_fee(_current_block_height, network); + return Amount.__wrap(ret); } /** - * Returns the Fungible/NFT Token ID for the given inputs of a transaction + * Returns the Delegation ID for the given inputs of a transaction * @param {Uint8Array} inputs * @param {Network} network * @returns {string} */ -export function get_token_id(inputs, network) { +export function get_delegation_id(inputs, network) { let deferred3_0; let deferred3_1; try { const ptr0 = passArray8ToWasm0(inputs, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ret = wasm.get_token_id(ptr0, len0, network); + const ret = wasm.get_delegation_id(ptr0, len0, network); var ptr2 = ret[0]; var len2 = ret[1]; if (ret[3]) { @@ -930,315 +1422,232 @@ export function get_token_id(inputs, network) { } /** - * Given the parameters needed to issue an NFT, and a network type (mainnet, testnet, etc), - * this function creates an output that issues that NFT. - * @param {string} token_id - * @param {string} authority - * @param {string} name - * @param {string} ticker - * @param {string} description - * @param {Uint8Array} media_hash - * @param {Uint8Array | null | undefined} creator - * @param {string | null | undefined} media_uri - * @param {string | null | undefined} icon_uri - * @param {string | null | undefined} additional_metadata_uri - * @param {bigint} _current_block_height + * Returns the Order ID for the given inputs of a transaction + * @param {Uint8Array} inputs * @param {Network} network - * @returns {Uint8Array} + * @returns {string} */ -export function encode_output_issue_nft(token_id, authority, name, ticker, description, media_hash, creator, media_uri, icon_uri, additional_metadata_uri, _current_block_height, network) { - const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - const ptr1 = passStringToWasm0(authority, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len1 = WASM_VECTOR_LEN; - const ptr2 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len2 = WASM_VECTOR_LEN; - const ptr3 = passStringToWasm0(ticker, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len3 = WASM_VECTOR_LEN; - const ptr4 = passStringToWasm0(description, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len4 = WASM_VECTOR_LEN; - const ptr5 = passArray8ToWasm0(media_hash, wasm.__wbindgen_malloc); - const len5 = WASM_VECTOR_LEN; - var ptr6 = isLikeNone(creator) ? 0 : passArray8ToWasm0(creator, wasm.__wbindgen_malloc); - var len6 = WASM_VECTOR_LEN; - var ptr7 = isLikeNone(media_uri) ? 0 : passStringToWasm0(media_uri, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - var len7 = WASM_VECTOR_LEN; - var ptr8 = isLikeNone(icon_uri) ? 0 : passStringToWasm0(icon_uri, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - var len8 = WASM_VECTOR_LEN; - var ptr9 = isLikeNone(additional_metadata_uri) ? 0 : passStringToWasm0(additional_metadata_uri, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - var len9 = WASM_VECTOR_LEN; - const ret = wasm.encode_output_issue_nft(ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3, ptr4, len4, ptr5, len5, ptr6, len6, ptr7, len7, ptr8, len8, ptr9, len9, _current_block_height, network); - if (ret[3]) { - throw takeFromExternrefTable0(ret[2]); +export function get_order_id(inputs, network) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passArray8ToWasm0(inputs, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.get_order_id(ptr0, len0, network); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); } - var v11 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v11; } /** - * Given data to be deposited in the blockchain, this function provides the output that deposits this data - * @param {Uint8Array} data - * @returns {Uint8Array} + * Returns the Pool ID for the given inputs of a transaction + * @param {Uint8Array} inputs + * @param {Network} network + * @returns {string} */ -export function encode_output_data_deposit(data) { - const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.encode_output_data_deposit(ptr0, len0); - if (ret[3]) { - throw takeFromExternrefTable0(ret[2]); +export function get_pool_id(inputs, network) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passArray8ToWasm0(inputs, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.get_pool_id(ptr0, len0, network); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); } - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; } /** - * Returns the fee that needs to be paid by a transaction for issuing a data deposit + * Returns the Fungible/NFT Token ID for the given inputs of a transaction + * @param {Uint8Array} inputs * @param {bigint} current_block_height * @param {Network} network - * @returns {Amount} - */ -export function data_deposit_fee(current_block_height, network) { - const ret = wasm.data_deposit_fee(current_block_height, network); - return Amount.__wrap(ret); -} - -/** - * Given the parameters needed to create hash timelock contract, and a network type (mainnet, testnet, etc), - * this function creates an output. - * @param {Amount} amount - * @param {string | null | undefined} token_id - * @param {string} secret_hash - * @param {string} spend_address - * @param {string} refund_address - * @param {Uint8Array} refund_timelock - * @param {Network} network - * @returns {Uint8Array} + * @returns {string} */ -export function encode_output_htlc(amount, token_id, secret_hash, spend_address, refund_address, refund_timelock, network) { - _assertClass(amount, Amount); - var ptr0 = amount.__destroy_into_raw(); - var ptr1 = isLikeNone(token_id) ? 0 : passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - var len1 = WASM_VECTOR_LEN; - const ptr2 = passStringToWasm0(secret_hash, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len2 = WASM_VECTOR_LEN; - const ptr3 = passStringToWasm0(spend_address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len3 = WASM_VECTOR_LEN; - const ptr4 = passStringToWasm0(refund_address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len4 = WASM_VECTOR_LEN; - const ptr5 = passArray8ToWasm0(refund_timelock, wasm.__wbindgen_malloc); - const len5 = WASM_VECTOR_LEN; - const ret = wasm.encode_output_htlc(ptr0, ptr1, len1, ptr2, len2, ptr3, len3, ptr4, len4, ptr5, len5, network); - if (ret[3]) { - throw takeFromExternrefTable0(ret[2]); +export function get_token_id(inputs, current_block_height, network) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passArray8ToWasm0(inputs, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.get_token_id(ptr0, len0, current_block_height, network); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); } - var v7 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v7; } /** - * Given a signed transaction and input outpoint that spends an htlc utxo, extract a secret that is - * encoded in the corresponding input signature - * @param {Uint8Array} signed_tx_bytes + * Given a `Transaction` encoded in bytes (not a signed transaction, but a signed transaction is tolerated by ignoring the extra bytes, by choice) + * this function will return the transaction id. + * + * The second parameter, the boolean, is provided as means of asserting that the given bytes exactly match a `Transaction` object. + * When set to `true`, the bytes provided must exactly match a single `Transaction` object. + * When set to `false`, extra bytes can exist, but will be ignored. + * This is useful when the provided bytes are of a `SignedTransaction` instead of a `Transaction`, + * since the signatures are appended at the end of the `Transaction` object as a vector to create a `SignedTransaction`. + * It is recommended to use a strict `Transaction` size and set the second parameter to `true`. + * @param {Uint8Array} transaction * @param {boolean} strict_byte_size - * @param {Uint8Array} htlc_outpoint_source_id - * @param {number} htlc_output_index - * @returns {Uint8Array} - */ -export function extract_htlc_secret(signed_tx_bytes, strict_byte_size, htlc_outpoint_source_id, htlc_output_index) { - const ptr0 = passArray8ToWasm0(signed_tx_bytes, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ptr1 = passArray8ToWasm0(htlc_outpoint_source_id, wasm.__wbindgen_malloc); - const len1 = WASM_VECTOR_LEN; - const ret = wasm.extract_htlc_secret(ptr0, len0, strict_byte_size, ptr1, len1, htlc_output_index); - 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; -} - -/** - * Given an output source id as bytes, and an output index, together representing a utxo, - * this function returns the input that puts them together, as bytes. - * @param {Uint8Array} outpoint_source_id - * @param {number} output_index - * @returns {Uint8Array} - */ -export function encode_input_for_utxo(outpoint_source_id, output_index) { - const ptr0 = passArray8ToWasm0(outpoint_source_id, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.encode_input_for_utxo(ptr0, len0, output_index); - if (ret[3]) { - throw takeFromExternrefTable0(ret[2]); - } - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; -} - -/** - * Given a delegation id, an amount and a network type (mainnet, testnet, etc), this function - * creates an input that withdraws from a delegation. - * A nonce is needed because this spends from an account. The nonce must be in sequence for everything in that account. - * @param {string} delegation_id - * @param {Amount} amount - * @param {bigint} nonce - * @param {Network} network - * @returns {Uint8Array} + * @returns {string} */ -export function encode_input_for_withdraw_from_delegation(delegation_id, amount, nonce, network) { - const ptr0 = passStringToWasm0(delegation_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - _assertClass(amount, Amount); - var ptr1 = amount.__destroy_into_raw(); - const ret = wasm.encode_input_for_withdraw_from_delegation(ptr0, len0, ptr1, nonce, network); - if (ret[3]) { - throw takeFromExternrefTable0(ret[2]); +export function get_transaction_id(transaction, strict_byte_size) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passArray8ToWasm0(transaction, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.get_transaction_id(ptr0, len0, strict_byte_size); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); } - var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v3; } /** - * Given the inputs, along each input's destination that can spend that input - * (e.g. If we are spending a UTXO in input number 1 and it is owned by address mtc1xxxx, then it is mtc1xxxx in element number 2 in the vector/list. - * for Account inputs that spend from a delegation it is the owning address of that delegation, - * and in the case of AccountCommand inputs which change a token it is the token's authority destination) - * and the outputs, estimate the transaction size. - * ScriptHash and ClassicMultisig destinations are not supported. - * @param {Uint8Array} inputs - * @param {string[]} input_utxos_destinations - * @param {Uint8Array} outputs + * Verify a witness produced by one of the `encode_witness` functions. + * + * `input_owner_destination` must be specified if `witness` actually contains a signature + * (i.e. it's not InputWitness::NoSignature) and the input is not an HTLC one. Otherwise it must + * be null. + * @param {SignatureHashType} sighashtype + * @param {string | null | undefined} input_owner_destination + * @param {Uint8Array} witness + * @param {Uint8Array} transaction + * @param {Uint8Array} input_utxos + * @param {number} input_index + * @param {TxAdditionalInfo} additional_info + * @param {bigint} current_block_height * @param {Network} network - * @returns {number} */ -export function estimate_transaction_size(inputs, input_utxos_destinations, outputs, network) { - const ptr0 = passArray8ToWasm0(inputs, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ptr1 = passArrayJsValueToWasm0(input_utxos_destinations, wasm.__wbindgen_malloc); +export function internal_verify_witness(sighashtype, input_owner_destination, witness, transaction, input_utxos, input_index, additional_info, current_block_height, network) { + var ptr0 = isLikeNone(input_owner_destination) ? 0 : passStringToWasm0(input_owner_destination, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + var len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(witness, wasm.__wbindgen_malloc); const len1 = WASM_VECTOR_LEN; - const ptr2 = passArray8ToWasm0(outputs, wasm.__wbindgen_malloc); + const ptr2 = passArray8ToWasm0(transaction, wasm.__wbindgen_malloc); const len2 = WASM_VECTOR_LEN; - const ret = wasm.estimate_transaction_size(ptr0, len0, ptr1, len1, ptr2, len2, network); - if (ret[2]) { - throw takeFromExternrefTable0(ret[1]); + const ptr3 = passArray8ToWasm0(input_utxos, wasm.__wbindgen_malloc); + const len3 = WASM_VECTOR_LEN; + const ret = wasm.internal_verify_witness(sighashtype, ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3, input_index, additional_info, current_block_height, network); + if (ret[1]) { + throw takeFromExternrefTable0(ret[0]); } - return ret[0] >>> 0; } /** - * Given inputs as bytes, outputs as bytes, and flags settings, this function returns - * the transaction that contains them all, as bytes. - * @param {Uint8Array} inputs - * @param {Uint8Array} outputs - * @param {bigint} flags + * From an extended private key create a change private key for a given key index + * derivation path: current_derivation_path/1/key_index + * @param {Uint8Array} private_key + * @param {number} key_index * @returns {Uint8Array} */ -export function encode_transaction(inputs, outputs, flags) { - const ptr0 = passArray8ToWasm0(inputs, wasm.__wbindgen_malloc); +export function make_change_address(private_key, key_index) { + const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ptr1 = passArray8ToWasm0(outputs, wasm.__wbindgen_malloc); - const len1 = WASM_VECTOR_LEN; - const ret = wasm.encode_transaction(ptr0, len0, ptr1, len1, flags); + const ret = wasm.make_change_address(ptr0, len0, key_index); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v3; + return v2; } /** - * Encode an input witness of the variant that contains no signature. + * From an extended public key create a change public key for a given key index + * derivation path: current_derivation_path/1/key_index + * @param {Uint8Array} extended_public_key + * @param {number} key_index * @returns {Uint8Array} */ -export function encode_witness_no_signature() { - const ret = wasm.encode_witness_no_signature(); - var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); +export function make_change_address_public_key(extended_public_key, key_index) { + const ptr0 = passArray8ToWasm0(extended_public_key, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.make_change_address_public_key(ptr0, len0, key_index); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v1; + return v2; } /** - * Given a private key, inputs and an input number to sign, and the destination that owns that output (through the utxo), - * and a network type (mainnet, testnet, etc), this function returns a witness to be used in a signed transaction, as bytes. - * @param {SignatureHashType} sighashtype - * @param {Uint8Array} private_key_bytes - * @param {string} input_owner_destination - * @param {Uint8Array} transaction_bytes - * @param {Uint8Array} inputs - * @param {number} input_num + * Create the default account's extended private key for a given mnemonic + * derivation path: 44'/mintlayer_coin_type'/0' + * @param {string} mnemonic * @param {Network} network * @returns {Uint8Array} */ -export function encode_witness(sighashtype, private_key_bytes, input_owner_destination, transaction_bytes, inputs, input_num, network) { - const ptr0 = passArray8ToWasm0(private_key_bytes, wasm.__wbindgen_malloc); +export function make_default_account_privkey(mnemonic, network) { + const ptr0 = passStringToWasm0(mnemonic, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; - const ptr1 = passStringToWasm0(input_owner_destination, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len1 = WASM_VECTOR_LEN; - const ptr2 = passArray8ToWasm0(transaction_bytes, wasm.__wbindgen_malloc); - const len2 = WASM_VECTOR_LEN; - const ptr3 = passArray8ToWasm0(inputs, wasm.__wbindgen_malloc); - const len3 = WASM_VECTOR_LEN; - const ret = wasm.encode_witness(sighashtype, ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3, input_num, network); + const ret = wasm.make_default_account_privkey(ptr0, len0, network); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v5 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v5; + return v2; } /** - * Given a private key, inputs and an input number to sign, and the destination that owns that output (through the utxo), - * and a network type (mainnet, testnet, etc), and an htlc secret this function returns a witness to be used in a signed transaction, as bytes. - * @param {SignatureHashType} sighashtype - * @param {Uint8Array} private_key_bytes - * @param {string} input_owner_destination - * @param {Uint8Array} transaction_bytes - * @param {Uint8Array} inputs - * @param {number} input_num - * @param {Uint8Array} secret - * @param {Network} network + * Generates a new, random private key from entropy * @returns {Uint8Array} */ -export function encode_witness_htlc_secret(sighashtype, private_key_bytes, input_owner_destination, transaction_bytes, inputs, input_num, secret, network) { - const ptr0 = passArray8ToWasm0(private_key_bytes, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ptr1 = passStringToWasm0(input_owner_destination, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len1 = WASM_VECTOR_LEN; - const ptr2 = passArray8ToWasm0(transaction_bytes, wasm.__wbindgen_malloc); - const len2 = WASM_VECTOR_LEN; - const ptr3 = passArray8ToWasm0(inputs, wasm.__wbindgen_malloc); - const len3 = WASM_VECTOR_LEN; - const ptr4 = passArray8ToWasm0(secret, wasm.__wbindgen_malloc); - const len4 = WASM_VECTOR_LEN; - const ret = wasm.encode_witness_htlc_secret(sighashtype, ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3, input_num, ptr4, len4, network); - if (ret[3]) { - throw takeFromExternrefTable0(ret[2]); - } - var v6 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); +export function make_private_key() { + const ret = wasm.make_private_key(); + var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v6; + return v1; } /** - * Given an arbitrary number of public keys as bytes, number of minimum required signatures, and a network type, this function returns - * the multisig challenge, as bytes. - * @param {Uint8Array} public_keys_bytes - * @param {number} min_required_signatures - * @param {Network} network + * From an extended private key create a receiving private key for a given key index + * derivation path: current_derivation_path/0/key_index + * @param {Uint8Array} private_key + * @param {number} key_index * @returns {Uint8Array} */ -export function encode_multisig_challenge(public_keys_bytes, min_required_signatures, network) { - const ptr0 = passArray8ToWasm0(public_keys_bytes, wasm.__wbindgen_malloc); +export function make_receiving_address(private_key, key_index) { + const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ret = wasm.encode_multisig_challenge(ptr0, len0, min_required_signatures, network); + const ret = wasm.make_receiving_address(ptr0, len0, key_index); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } @@ -1248,54 +1657,36 @@ export function encode_multisig_challenge(public_keys_bytes, min_required_signat } /** - * Given a private key, inputs and an input number to sign, and multisig challenge, - * and a network type (mainnet, testnet, etc), this function returns a witness to be used in a signed transaction, as bytes. - * - * `key_index` parameter is an index of a public key in the challenge, against which is the signature produces from private key is to be verified. - * `input_witness` parameter can be either empty or a result of previous calls to this function. - * @param {SignatureHashType} sighashtype - * @param {Uint8Array} private_key_bytes + * From an extended public key create a receiving public key for a given key index + * derivation path: current_derivation_path/0/key_index + * @param {Uint8Array} extended_public_key * @param {number} key_index - * @param {Uint8Array} input_witness - * @param {Uint8Array} multisig_challenge - * @param {Uint8Array} transaction_bytes - * @param {Uint8Array} utxos - * @param {number} input_num - * @param {Network} network * @returns {Uint8Array} */ -export function encode_witness_htlc_multisig(sighashtype, private_key_bytes, key_index, input_witness, multisig_challenge, transaction_bytes, utxos, input_num, network) { - const ptr0 = passArray8ToWasm0(private_key_bytes, wasm.__wbindgen_malloc); +export function make_receiving_address_public_key(extended_public_key, key_index) { + const ptr0 = passArray8ToWasm0(extended_public_key, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ptr1 = passArray8ToWasm0(input_witness, wasm.__wbindgen_malloc); - const len1 = WASM_VECTOR_LEN; - const ptr2 = passArray8ToWasm0(multisig_challenge, wasm.__wbindgen_malloc); - const len2 = WASM_VECTOR_LEN; - const ptr3 = passArray8ToWasm0(transaction_bytes, wasm.__wbindgen_malloc); - const len3 = WASM_VECTOR_LEN; - const ptr4 = passArray8ToWasm0(utxos, wasm.__wbindgen_malloc); - const len4 = WASM_VECTOR_LEN; - const ret = wasm.encode_witness_htlc_multisig(sighashtype, ptr0, len0, key_index, ptr1, len1, ptr2, len2, ptr3, len3, ptr4, len4, input_num, network); + const ret = wasm.make_receiving_address_public_key(ptr0, len0, key_index); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } - var v6 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v6; + return v2; } /** - * Given an unsigned transaction and signatures, this function returns a SignedTransaction object as bytes. - * @param {Uint8Array} transaction_bytes - * @param {Uint8Array} signatures + * Return the message that has to be signed to produce a signed transaction intent. + * @param {string} intent + * @param {string} transaction_id * @returns {Uint8Array} */ -export function encode_signed_transaction(transaction_bytes, signatures) { - const ptr0 = passArray8ToWasm0(transaction_bytes, wasm.__wbindgen_malloc); +export function make_transaction_intent_message_to_sign(intent, transaction_id) { + const ptr0 = passStringToWasm0(intent, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; - const ptr1 = passArray8ToWasm0(signatures, wasm.__wbindgen_malloc); + const ptr1 = passStringToWasm0(transaction_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len1 = WASM_VECTOR_LEN; - const ret = wasm.encode_signed_transaction(ptr0, len0, ptr1, len1); + const ret = wasm.make_transaction_intent_message_to_sign(ptr0, len0, ptr1, len1); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } @@ -1305,26 +1696,18 @@ export function encode_signed_transaction(transaction_bytes, signatures) { } /** - * Given a `Transaction` encoded in bytes (not a signed transaction, but a signed transaction is tolerated by ignoring the extra bytes, by choice) - * this function will return the transaction id. - * - * The second parameter, the boolean, is provided as means of asserting that the given bytes exactly match a `Transaction` object. - * When set to `true`, the bytes provided must exactly match a single `Transaction` object. - * When set to `false`, extra bytes can exist, but will be ignored. - * This is useful when the provided bytes are of a `SignedTransaction` instead of a `Transaction`, - * since the signatures are appended at the end of the `Transaction` object as a vector to create a `SignedTransaction`. - * It is recommended to use a strict `Transaction` size and set the second parameter to `true`. - * @param {Uint8Array} transaction_bytes - * @param {boolean} strict_byte_size + * Produce a multisig address given a multisig challenge. + * @param {Uint8Array} multisig_challenge + * @param {Network} network * @returns {string} */ -export function get_transaction_id(transaction_bytes, strict_byte_size) { +export function multisig_challenge_to_address(multisig_challenge, network) { let deferred3_0; let deferred3_1; try { - const ptr0 = passArray8ToWasm0(transaction_bytes, wasm.__wbindgen_malloc); + const ptr0 = passArray8ToWasm0(multisig_challenge, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ret = wasm.get_transaction_id(ptr0, len0, strict_byte_size); + const ret = wasm.multisig_challenge_to_address(ptr0, len0, network); var ptr2 = ret[0]; var len2 = ret[1]; if (ret[3]) { @@ -1340,116 +1723,55 @@ export function get_transaction_id(transaction_bytes, strict_byte_size) { } /** - * Calculate the "effective balance" of a pool, given the total pool balance and pledge by the pool owner/staker. - * The effective balance is how the influence of a pool is calculated due to its balance. + * Given the current block height and a network type (mainnet, testnet, etc), + * this will return the fee that needs to be paid by a transaction for issuing a new NFT + * The current block height information is used in case a network upgrade changed the value. + * @param {bigint} current_block_height * @param {Network} network - * @param {Amount} pledge_amount - * @param {Amount} pool_balance * @returns {Amount} */ -export function effective_pool_balance(network, pledge_amount, pool_balance) { - _assertClass(pledge_amount, Amount); - var ptr0 = pledge_amount.__destroy_into_raw(); - _assertClass(pool_balance, Amount); - var ptr1 = pool_balance.__destroy_into_raw(); - const ret = wasm.effective_pool_balance(network, ptr0, ptr1); - if (ret[2]) { - throw takeFromExternrefTable0(ret[1]); - } - return Amount.__wrap(ret[0]); -} - -/** - * Given a token_id, an amount of tokens to mint and nonce return an encoded mint tokens input - * @param {string} token_id - * @param {Amount} amount - * @param {bigint} nonce - * @param {Network} network - * @returns {Uint8Array} - */ -export function encode_input_for_mint_tokens(token_id, amount, nonce, network) { - const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - _assertClass(amount, Amount); - var ptr1 = amount.__destroy_into_raw(); - const ret = wasm.encode_input_for_mint_tokens(ptr0, len0, ptr1, nonce, network); - 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; -} - -/** - * Given a token_id and nonce return an encoded unmint tokens input - * @param {string} token_id - * @param {bigint} nonce - * @param {Network} network - * @returns {Uint8Array} - */ -export function encode_input_for_unmint_tokens(token_id, nonce, network) { - const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.encode_input_for_unmint_tokens(ptr0, len0, nonce, network); - if (ret[3]) { - throw takeFromExternrefTable0(ret[2]); - } - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; -} - -/** - * Given a token_id and nonce return an encoded lock_token_supply input - * @param {string} token_id - * @param {bigint} nonce - * @param {Network} network - * @returns {Uint8Array} - */ -export function encode_input_for_lock_token_supply(token_id, nonce, network) { - const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.encode_input_for_lock_token_supply(ptr0, len0, nonce, network); - if (ret[3]) { - throw takeFromExternrefTable0(ret[2]); - } - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; +export function nft_issuance_fee(current_block_height, network) { + const ret = wasm.nft_issuance_fee(current_block_height, network); + return Amount.__wrap(ret); } /** - * Given a token_id, is token unfreezable and nonce return an encoded freeze token input - * @param {string} token_id - * @param {TokenUnfreezable} is_token_unfreezable - * @param {bigint} nonce + * Given a public key (as bytes) and a network type (mainnet, testnet, etc), + * return the address public key hash from that public key as an address + * @param {Uint8Array} public_key * @param {Network} network - * @returns {Uint8Array} + * @returns {string} */ -export function encode_input_for_freeze_token(token_id, is_token_unfreezable, nonce, network) { - const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.encode_input_for_freeze_token(ptr0, len0, is_token_unfreezable, nonce, network); - if (ret[3]) { - throw takeFromExternrefTable0(ret[2]); +export function pubkey_to_pubkeyhash_address(public_key, network) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passArray8ToWasm0(public_key, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.pubkey_to_pubkeyhash_address(ptr0, len0, network); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); } - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; } /** - * Given a token_id and nonce return an encoded unfreeze token input - * @param {string} token_id - * @param {bigint} nonce - * @param {Network} network + * Given a private key, as bytes, return the bytes of the corresponding public key + * @param {Uint8Array} private_key * @returns {Uint8Array} */ -export function encode_input_for_unfreeze_token(token_id, nonce, network) { - const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +export function public_key_from_private_key(private_key) { + const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ret = wasm.encode_input_for_unfreeze_token(ptr0, len0, nonce, network); + const ret = wasm.public_key_from_private_key(ptr0, len0); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } @@ -1459,19 +1781,18 @@ export function encode_input_for_unfreeze_token(token_id, nonce, network) { } /** - * Given a token_id, new authority destination and nonce return an encoded change token authority input - * @param {string} token_id - * @param {string} new_authority - * @param {bigint} nonce - * @param {Network} network + * Given a message and a private key, create and sign a challenge with the given private key. + * This kind of signature is to be used when signing challenges. + * @param {Uint8Array} private_key + * @param {Uint8Array} message * @returns {Uint8Array} */ -export function encode_input_for_change_token_authority(token_id, new_authority, nonce, network) { - const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +export function sign_challenge(private_key, message) { + const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ptr1 = passStringToWasm0(new_authority, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const ptr1 = passArray8ToWasm0(message, wasm.__wbindgen_malloc); const len1 = WASM_VECTOR_LEN; - const ret = wasm.encode_input_for_change_token_authority(ptr0, len0, ptr1, len1, nonce, network); + const ret = wasm.sign_challenge(ptr0, len0, ptr1, len1); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } @@ -1481,19 +1802,19 @@ export function encode_input_for_change_token_authority(token_id, new_authority, } /** - * Given a token_id, new metadata uri and nonce return an encoded change token metadata uri input - * @param {string} token_id - * @param {string} new_metadata_uri - * @param {bigint} nonce - * @param {Network} network + * Given a message and a private key, sign the message with the given private key + * This kind of signature is to be used when signing spend requests, such as transaction + * input witness. + * @param {Uint8Array} private_key + * @param {Uint8Array} message * @returns {Uint8Array} */ -export function encode_input_for_change_token_metadata_uri(token_id, new_metadata_uri, nonce, network) { - const ptr0 = passStringToWasm0(token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +export function sign_message_for_spending(private_key, message) { + const ptr0 = passArray8ToWasm0(private_key, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - const ptr1 = passStringToWasm0(new_metadata_uri, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const ptr1 = passArray8ToWasm0(message, wasm.__wbindgen_malloc); const len1 = WASM_VECTOR_LEN; - const ret = wasm.encode_input_for_change_token_metadata_uri(ptr0, len0, ptr1, len1, nonce, network); + const ret = wasm.sign_message_for_spending(ptr0, len0, ptr1, len1); if (ret[3]) { throw takeFromExternrefTable0(ret[2]); } @@ -1503,202 +1824,424 @@ export function encode_input_for_change_token_metadata_uri(token_id, new_metadat } /** - * Given ask and give amounts and a conclude key create output that creates an order. + * Given the current block height and a network type (mainnet, testnet, etc), + * this function returns the number of blocks, after which a pool that decommissioned, + * will have its funds unlocked and available for spending. + * The current block height information is used in case a network upgrade changed the value. + * @param {bigint} current_block_height + * @param {Network} network + * @returns {bigint} + */ +export function staking_pool_spend_maturity_block_count(current_block_height, network) { + const ret = wasm.staking_pool_spend_maturity_block_count(current_block_height, network); + return BigInt.asUintN(64, ret); +} + +/** + * Given the current block height and a network type (mainnet, testnet, etc), + * this will return the fee that needs to be paid by a transaction for changing the authority of a token + * The current block height information is used in case a network upgrade changed the value. + * @param {bigint} current_block_height + * @param {Network} network + * @returns {Amount} + */ +export function token_change_authority_fee(current_block_height, network) { + const ret = wasm.token_change_authority_fee(current_block_height, network); + return Amount.__wrap(ret); +} + +/** + * Given the current block height and a network type (mainnet, testnet, etc), + * this will return the fee that needs to be paid by a transaction for freezing/unfreezing a token + * The current block height information is used in case a network upgrade changed the value. + * @param {bigint} current_block_height + * @param {Network} network + * @returns {Amount} + */ +export function token_freeze_fee(current_block_height, network) { + const ret = wasm.token_freeze_fee(current_block_height, network); + return Amount.__wrap(ret); +} + +/** + * Given the current block height and a network type (mainnet, testnet, etc), + * this will return the fee that needs to be paid by a transaction for changing the total supply of a token + * by either minting or unminting tokens + * The current block height information is used in case a network upgrade changed the value. + * @param {bigint} current_block_height + * @param {Network} network + * @returns {Amount} + */ +export function token_supply_change_fee(current_block_height, network) { + const ret = wasm.token_supply_change_fee(current_block_height, network); + return Amount.__wrap(ret); +} + +/** + * Given a signed challenge, an address and a message, verify that + * the signature is produced by signing the message with the private key + * that derived the given public key. + * This function is used for verifying messages-related challenges. * - * 'ask_token_id': the parameter represents a Token if it's Some and coins otherwise. - * 'give_token_id': the parameter represents a Token if it's Some and coins otherwise. - * @param {Amount} ask_amount - * @param {string | null | undefined} ask_token_id - * @param {Amount} give_amount - * @param {string | null | undefined} give_token_id - * @param {string} conclude_address + * Note: for signatures that were created by `sign_challenge`, the provided address must be + * a 'pubkeyhash' address. + * + * Note: currently this function never returns `false` - it either returns `true` or fails with an error. + * @param {string} address * @param {Network} network - * @returns {Uint8Array} + * @param {Uint8Array} signed_challenge + * @param {Uint8Array} message + * @returns {boolean} */ -export function encode_create_order_output(ask_amount, ask_token_id, give_amount, give_token_id, conclude_address, network) { - _assertClass(ask_amount, Amount); - var ptr0 = ask_amount.__destroy_into_raw(); - var ptr1 = isLikeNone(ask_token_id) ? 0 : passStringToWasm0(ask_token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - var len1 = WASM_VECTOR_LEN; - _assertClass(give_amount, Amount); - var ptr2 = give_amount.__destroy_into_raw(); - var ptr3 = isLikeNone(give_token_id) ? 0 : passStringToWasm0(give_token_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - var len3 = WASM_VECTOR_LEN; - const ptr4 = passStringToWasm0(conclude_address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len4 = WASM_VECTOR_LEN; - const ret = wasm.encode_create_order_output(ptr0, ptr1, len1, ptr2, ptr3, len3, ptr4, len4, network); - if (ret[3]) { - throw takeFromExternrefTable0(ret[2]); +export function verify_challenge(address, network, signed_challenge, message) { + const ptr0 = passStringToWasm0(address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(signed_challenge, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passArray8ToWasm0(message, wasm.__wbindgen_malloc); + const len2 = WASM_VECTOR_LEN; + const ret = wasm.verify_challenge(ptr0, len0, network, ptr1, len1, ptr2, len2); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); } - var v6 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v6; + return ret[0] !== 0; } /** - * Given an amount to fill an order (which is described in terms of ask currency) and a destination - * for result outputs create an input that fills the order. - * @param {string} order_id - * @param {Amount} fill_amount - * @param {string} destination - * @param {bigint} nonce + * Given a digital signature, a public key and a message. Verify that + * the signature is produced by signing the message with the private key + * that derived the given public key. + * Note that this function is used for verifying messages related to spending, + * such as transaction input witness. + * @param {Uint8Array} public_key + * @param {Uint8Array} signature + * @param {Uint8Array} message + * @returns {boolean} + */ +export function verify_signature_for_spending(public_key, signature, message) { + const ptr0 = passArray8ToWasm0(public_key, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(signature, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passArray8ToWasm0(message, wasm.__wbindgen_malloc); + const len2 = WASM_VECTOR_LEN; + const ret = wasm.verify_signature_for_spending(ptr0, len0, ptr1, len1, ptr2, len2); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return ret[0] !== 0; +} + +/** + * Verify a signed transaction intent. + * + * Parameters: + * `expected_signed_message` - the message that is supposed to be signed; this must have been + * produced by `make_transaction_intent_message_to_sign`. + * `encoded_signed_intent` - the signed transaction intent produced by `encode_signed_transaction_intent`. + * `input_destinations` - an array of addresses (strings), corresponding to the transaction's input destinations + * (note that this function treats "pub key" and "pub key hash" addresses interchangeably, so it's ok to pass + * one instead of the other). + * `network` - the network being used (needed to decode the addresses). + * @param {Uint8Array} expected_signed_message + * @param {Uint8Array} encoded_signed_intent + * @param {string[]} input_destinations * @param {Network} network - * @returns {Uint8Array} */ -export function encode_input_for_fill_order(order_id, fill_amount, destination, nonce, network) { - const ptr0 = passStringToWasm0(order_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); +export function verify_transaction_intent(expected_signed_message, encoded_signed_intent, input_destinations, network) { + const ptr0 = passArray8ToWasm0(expected_signed_message, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; - _assertClass(fill_amount, Amount); - var ptr1 = fill_amount.__destroy_into_raw(); - const ptr2 = passStringToWasm0(destination, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const ptr1 = passArray8ToWasm0(encoded_signed_intent, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passArrayJsValueToWasm0(input_destinations, wasm.__wbindgen_malloc); const len2 = WASM_VECTOR_LEN; - const ret = wasm.encode_input_for_fill_order(ptr0, len0, ptr1, ptr2, len2, nonce, network); - if (ret[3]) { - throw takeFromExternrefTable0(ret[2]); + const ret = wasm.verify_transaction_intent(ptr0, len0, ptr1, len1, ptr2, len2, network); + if (ret[1]) { + throw takeFromExternrefTable0(ret[0]); + } +} + +function __wbg_get_imports() { + const import0 = { + __proto__: null, + __wbg___wbindgen_debug_string_0bc8482c6e3508ae: function(arg0, arg1) { + const ret = debugString(arg1); + const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true); + getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true); + }, + __wbg___wbindgen_is_undefined_9e4d92534c42d778: function(arg0) { + const ret = arg0 === undefined; + return ret; + }, + __wbg___wbindgen_string_get_72fb696202c56729: function(arg0, arg1) { + const obj = arg1; + const ret = typeof(obj) === 'string' ? obj : undefined; + var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + var len1 = WASM_VECTOR_LEN; + getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true); + getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true); + }, + __wbg___wbindgen_throw_be289d5034ed271b: function(arg0, arg1) { + throw new Error(getStringFromWasm0(arg0, arg1)); + }, + __wbg_getRandomValues_2a91986308c74a93: function() { return handleError(function (arg0, arg1) { + globalThis.crypto.getRandomValues(getArrayU8FromWasm0(arg0, arg1)); + }, arguments); }, + __wbg_length_32ed9a279acd054c: function(arg0) { + const ret = arg0.length; + return ret; + }, + __wbg_parse_708461a1feddfb38: function() { return handleError(function (arg0, arg1) { + const ret = JSON.parse(getStringFromWasm0(arg0, arg1)); + return ret; + }, arguments); }, + __wbg_prototypesetcall_bdcdcc5842e4d77d: function(arg0, arg1, arg2) { + Uint8Array.prototype.set.call(getArrayU8FromWasm0(arg0, arg1), arg2); + }, + __wbg_stringify_8d1cc6ff383e8bae: function() { return handleError(function (arg0) { + const ret = JSON.stringify(arg0); + return ret; + }, arguments); }, + __wbindgen_cast_0000000000000001: function(arg0, arg1) { + // Cast intrinsic for `Ref(String) -> Externref`. + const ret = getStringFromWasm0(arg0, arg1); + return ret; + }, + __wbindgen_init_externref_table: function() { + const table = wasm.__wbindgen_externrefs; + const offset = table.grow(4); + table.set(0, undefined); + table.set(offset + 0, undefined); + table.set(offset + 1, null); + table.set(offset + 2, true); + table.set(offset + 3, false); + }, + }; + return { + __proto__: null, + "./wasm_wrappers_bg.js": import0, + }; +} + +const AmountFinalization = (typeof FinalizationRegistry === 'undefined') + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry(ptr => wasm.__wbg_amount_free(ptr >>> 0, 1)); + +function addToExternrefTable0(obj) { + const idx = wasm.__externref_table_alloc(); + wasm.__wbindgen_externrefs.set(idx, obj); + return idx; +} + +function _assertClass(instance, klass) { + if (!(instance instanceof klass)) { + throw new Error(`expected instance of ${klass.name}`); + } +} + +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; +} + +function getArrayU8FromWasm0(ptr, len) { + ptr = ptr >>> 0; + return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len); +} + +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 getStringFromWasm0(ptr, len) { + ptr = ptr >>> 0; + return decodeText(ptr, len); +} + +let cachedUint8ArrayMemory0 = null; +function getUint8ArrayMemory0() { + if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) { + cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer); } - var v4 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v4; + return cachedUint8ArrayMemory0; } -/** - * Given an order id create an input that concludes the order. - * @param {string} order_id - * @param {bigint} nonce - * @param {Network} network - * @returns {Uint8Array} - */ -export function encode_input_for_conclude_order(order_id, nonce, network) { - const ptr0 = passStringToWasm0(order_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.encode_input_for_conclude_order(ptr0, len0, nonce, network); - if (ret[3]) { - throw takeFromExternrefTable0(ret[2]); +function handleError(f, args) { + try { + return f.apply(this, args); + } catch (e) { + const idx = addToExternrefTable0(e); + wasm.__wbindgen_exn_store(idx); } - var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); - wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); - return v2; } -/** - * Indicates whether a token can be frozen - * @enum {0 | 1} - */ -export const FreezableToken = Object.freeze({ - No: 0, "0": "No", - Yes: 1, "1": "Yes", -}); -/** - * The network, for which an operation to be done. Mainnet, testnet, etc. - * @enum {0 | 1 | 2 | 3} - */ -export const Network = Object.freeze({ - Mainnet: 0, "0": "Mainnet", - Testnet: 1, "1": "Testnet", - Regtest: 2, "2": "Regtest", - Signet: 3, "3": "Signet", -}); -/** - * The part of the transaction that will be committed in the signature. Similar to bitcoin's sighash. - * @enum {0 | 1 | 2 | 3} - */ -export const SignatureHashType = Object.freeze({ - ALL: 0, "0": "ALL", - NONE: 1, "1": "NONE", - SINGLE: 2, "2": "SINGLE", - ANYONECANPAY: 3, "3": "ANYONECANPAY", -}); -/** - * A utxo can either come from a transaction or a block reward. This enum signifies that. - * @enum {0 | 1} - */ -export const SourceId = Object.freeze({ - Transaction: 0, "0": "Transaction", - BlockReward: 1, "1": "BlockReward", -}); -/** - * Indicates whether a token can be unfrozen once frozen - * @enum {0 | 1} - */ -export const TokenUnfreezable = Object.freeze({ - No: 0, "0": "No", - Yes: 1, "1": "Yes", -}); -/** - * The token supply of a specific token, set on issuance - * @enum {0 | 1 | 2} - */ -export const TotalSupply = Object.freeze({ - /** - * Can be issued with no limit, but then can be locked to have a fixed supply. - */ - Lockable: 0, "0": "Lockable", - /** - * Unlimited supply, no limits except for numeric limits due to u128 - */ - Unlimited: 1, "1": "Unlimited", - /** - * On issuance, the total number of coins is fixed - */ - Fixed: 2, "2": "Fixed", -}); +function isLikeNone(x) { + return x === undefined || x === null; +} -const AmountFinalization = (typeof FinalizationRegistry === 'undefined') - ? { register: () => {}, unregister: () => {} } - : new FinalizationRegistry(ptr => wasm.__wbg_amount_free(ptr >>> 0, 1)); -/** - * Amount type abstraction. The amount type is stored in a string - * since JavaScript number type cannot fit 128-bit integers. - * The amount is given as an integer in units of "atoms". - * Atoms are the smallest, indivisible amount of a coin or token. - */ -export class Amount { +function passArray8ToWasm0(arg, malloc) { + const ptr = malloc(arg.length * 1, 1) >>> 0; + getUint8ArrayMemory0().set(arg, ptr / 1); + WASM_VECTOR_LEN = arg.length; + return ptr; +} - static __wrap(ptr) { - ptr = ptr >>> 0; - const obj = Object.create(Amount.prototype); - obj.__wbg_ptr = ptr; - AmountFinalization.register(obj, obj.__wbg_ptr, obj); - return obj; +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; +} - __destroy_into_raw() { - const ptr = this.__wbg_ptr; - this.__wbg_ptr = 0; - AmountFinalization.unregister(this); +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; } - free() { - const ptr = this.__destroy_into_raw(); - wasm.__wbg_amount_free(ptr, 0); - } - /** - * @param {string} atoms - * @returns {Amount} - */ - static from_atoms(atoms) { - const ptr0 = passStringToWasm0(atoms, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.amount_from_atoms(ptr0, len0); - return Amount.__wrap(ret); + 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; } - /** - * @returns {string} - */ - atoms() { - let deferred1_0; - let deferred1_1; - try { - const ptr = this.__destroy_into_raw(); - const ret = wasm.amount_atoms(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 (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 = cachedTextEncoder.encodeInto(arg, view); + + offset += ret.written; + ptr = realloc(ptr, len, offset, 1) >>> 0; + } + + WASM_VECTOR_LEN = offset; + return ptr; +} + +function takeFromExternrefTable0(idx) { + const value = wasm.__wbindgen_externrefs.get(idx); + wasm.__externref_table_dealloc(idx); + return value; +} + +let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); +cachedTextDecoder.decode(); +const MAX_SAFARI_DECODE_BYTES = 2146435072; +let numBytesDecoded = 0; +function decodeText(ptr, len) { + numBytesDecoded += len; + if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) { + cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); + cachedTextDecoder.decode(); + numBytesDecoded = len; } + return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len)); +} + +const cachedTextEncoder = new TextEncoder(); + +if (!('encodeInto' in cachedTextEncoder)) { + cachedTextEncoder.encodeInto = function (arg, view) { + const buf = cachedTextEncoder.encode(arg); + view.set(buf); + return { + read: arg.length, + written: buf.length + }; + }; +} + +let WASM_VECTOR_LEN = 0; + +let wasmModule, wasm; +function __wbg_finalize_init(instance, module) { + wasm = instance.exports; + wasmModule = module; + cachedDataViewMemory0 = null; + cachedUint8ArrayMemory0 = null; + wasm.__wbindgen_start(); + return wasm; } async function __wbg_load(module, imports) { @@ -1706,190 +2249,41 @@ async function __wbg_load(module, imports) { if (typeof WebAssembly.instantiateStreaming === 'function') { try { return await WebAssembly.instantiateStreaming(module, imports); - } catch (e) { - if (module.headers.get('Content-Type') != 'application/wasm') { + const validResponse = module.ok && expectedResponseType(module.type); + + if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') { console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e); - } else { - throw e; - } + } else { throw e; } } } const bytes = await module.arrayBuffer(); return await WebAssembly.instantiate(bytes, imports); - } else { const instance = await WebAssembly.instantiate(module, imports); if (instance instanceof WebAssembly.Instance) { return { instance, module }; - } else { return instance; } } -} - -function __wbg_get_imports() { - const imports = {}; - imports.wbg = {}; - imports.wbg.__wbg_buffer_609cc3eee51ed158 = function(arg0) { - const ret = arg0.buffer; - return ret; - }; - imports.wbg.__wbg_call_672a4d21634d4a24 = function() { return handleError(function (arg0, arg1) { - const ret = arg0.call(arg1); - return ret; - }, arguments) }; - imports.wbg.__wbg_call_7cccdd69e0791ae2 = function() { return handleError(function (arg0, arg1, arg2) { - const ret = arg0.call(arg1, arg2); - return ret; - }, arguments) }; - imports.wbg.__wbg_crypto_ed58b8e10a292839 = function(arg0) { - const ret = arg0.crypto; - return ret; - }; - imports.wbg.__wbg_getRandomValues_bcb4912f16000dc4 = function() { return handleError(function (arg0, arg1) { - arg0.getRandomValues(arg1); - }, arguments) }; - imports.wbg.__wbg_msCrypto_0a36e2ec3a343d26 = function(arg0) { - const ret = arg0.msCrypto; - return ret; - }; - imports.wbg.__wbg_new_a12002a7f91c75be = function(arg0) { - const ret = new Uint8Array(arg0); - return ret; - }; - imports.wbg.__wbg_newnoargs_105ed471475aaf50 = function(arg0, arg1) { - const ret = new Function(getStringFromWasm0(arg0, arg1)); - return ret; - }; - imports.wbg.__wbg_newwithbyteoffsetandlength_d97e637ebe145a9a = function(arg0, arg1, arg2) { - const ret = new Uint8Array(arg0, arg1 >>> 0, arg2 >>> 0); - return ret; - }; - imports.wbg.__wbg_newwithlength_a381634e90c276d4 = function(arg0) { - const ret = new Uint8Array(arg0 >>> 0); - return ret; - }; - imports.wbg.__wbg_node_02999533c4ea02e3 = function(arg0) { - const ret = arg0.node; - return ret; - }; - imports.wbg.__wbg_process_5c1d670bc53614b8 = function(arg0) { - const ret = arg0.process; - return ret; - }; - imports.wbg.__wbg_randomFillSync_ab2cfe79ebbf2740 = function() { return handleError(function (arg0, arg1) { - arg0.randomFillSync(arg1); - }, arguments) }; - imports.wbg.__wbg_require_79b1e9274cde3c87 = function() { return handleError(function () { - const ret = module.require; - return ret; - }, arguments) }; - imports.wbg.__wbg_set_65595bdd868b3009 = function(arg0, arg1, arg2) { - arg0.set(arg1, arg2 >>> 0); - }; - imports.wbg.__wbg_static_accessor_GLOBAL_88a902d13a557d07 = function() { - const ret = typeof global === 'undefined' ? null : global; - return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); - }; - imports.wbg.__wbg_static_accessor_GLOBAL_THIS_56578be7e9f832b0 = function() { - const ret = typeof globalThis === 'undefined' ? null : globalThis; - return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); - }; - imports.wbg.__wbg_static_accessor_SELF_37c5d418e4bf5819 = function() { - const ret = typeof self === 'undefined' ? null : self; - return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); - }; - imports.wbg.__wbg_static_accessor_WINDOW_5de37043a91a9c40 = function() { - const ret = typeof window === 'undefined' ? null : window; - return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); - }; - imports.wbg.__wbg_stringify_f7ed6987935b4a24 = function() { return handleError(function (arg0) { - const ret = JSON.stringify(arg0); - return ret; - }, arguments) }; - imports.wbg.__wbg_subarray_aa9065fa9dc5df96 = function(arg0, arg1, arg2) { - const ret = arg0.subarray(arg1 >>> 0, arg2 >>> 0); - return ret; - }; - imports.wbg.__wbg_versions_c71aa1626a93e0a1 = function(arg0) { - const ret = arg0.versions; - return ret; - }; - imports.wbg.__wbindgen_init_externref_table = function() { - const table = wasm.__wbindgen_export_2; - const offset = table.grow(4); - table.set(0, undefined); - table.set(offset + 0, undefined); - table.set(offset + 1, null); - table.set(offset + 2, true); - table.set(offset + 3, false); - ; - }; - imports.wbg.__wbindgen_is_function = function(arg0) { - const ret = typeof(arg0) === 'function'; - return ret; - }; - imports.wbg.__wbindgen_is_object = function(arg0) { - const val = arg0; - const ret = typeof(val) === 'object' && val !== null; - return ret; - }; - imports.wbg.__wbindgen_is_string = function(arg0) { - const ret = typeof(arg0) === 'string'; - return ret; - }; - imports.wbg.__wbindgen_is_undefined = function(arg0) { - const ret = arg0 === undefined; - return ret; - }; - imports.wbg.__wbindgen_memory = function() { - const ret = wasm.memory; - return ret; - }; - imports.wbg.__wbindgen_string_get = function(arg0, arg1) { - const obj = arg1; - const ret = typeof(obj) === 'string' ? obj : undefined; - var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - var len1 = WASM_VECTOR_LEN; - getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true); - getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true); - }; - imports.wbg.__wbindgen_string_new = function(arg0, arg1) { - const ret = getStringFromWasm0(arg0, arg1); - return ret; - }; - imports.wbg.__wbindgen_throw = function(arg0, arg1) { - throw new Error(getStringFromWasm0(arg0, arg1)); - }; - - return imports; -} - -function __wbg_init_memory(imports, memory) { - -} - -function __wbg_finalize_init(instance, module) { - wasm = instance.exports; - __wbg_init.__wbindgen_wasm_module = module; - cachedDataViewMemory0 = null; - cachedUint8ArrayMemory0 = null; - - wasm.__wbindgen_start(); - return wasm; + function expectedResponseType(type) { + switch (type) { + case 'basic': case 'cors': case 'default': return true; + } + return false; + } } function initSync(module) { if (wasm !== undefined) return wasm; - if (typeof module !== 'undefined') { + if (module !== undefined) { if (Object.getPrototypeOf(module) === Object.prototype) { ({module} = module) } else { @@ -1898,15 +2292,10 @@ function initSync(module) { } const imports = __wbg_get_imports(); - - __wbg_init_memory(imports); - if (!(module instanceof WebAssembly.Module)) { module = new WebAssembly.Module(module); } - const instance = new WebAssembly.Instance(module, imports); - return __wbg_finalize_init(instance, module); } @@ -1914,7 +2303,7 @@ async function __wbg_init(module_or_path) { if (wasm !== undefined) return wasm; - if (typeof module_or_path !== 'undefined') { + if (module_or_path !== undefined) { if (Object.getPrototypeOf(module_or_path) === Object.prototype) { ({module_or_path} = module_or_path) } else { @@ -1922,7 +2311,7 @@ async function __wbg_init(module_or_path) { } } - if (typeof module_or_path === 'undefined') { + if (module_or_path === undefined) { module_or_path = new URL('wasm_wrappers_bg.wasm', import.meta.url); } const imports = __wbg_get_imports(); @@ -1931,12 +2320,9 @@ async function __wbg_init(module_or_path) { module_or_path = fetch(module_or_path); } - __wbg_init_memory(imports); - const { instance, module } = await __wbg_load(await module_or_path, imports); return __wbg_finalize_init(instance, module); } -export { initSync }; -export default __wbg_init; +export { initSync, __wbg_init as default }; diff --git a/packages/wasm-lib/wasm_wrappers_bg.wasm b/packages/wasm-lib/wasm_wrappers_bg.wasm index 6c5c613..d8c6052 100644 Binary files a/packages/wasm-lib/wasm_wrappers_bg.wasm and b/packages/wasm-lib/wasm_wrappers_bg.wasm differ diff --git a/packages/wasm-lib/wasm_wrappers_bg.wasm.d.ts b/packages/wasm-lib/wasm_wrappers_bg.wasm.d.ts index eab52a2..9363a87 100644 --- a/packages/wasm-lib/wasm_wrappers_bg.wasm.d.ts +++ b/packages/wasm-lib/wasm_wrappers_bg.wasm.d.ts @@ -1,84 +1,97 @@ /* tslint:disable */ /* eslint-disable */ export const memory: WebAssembly.Memory; -export const __wbg_amount_free: (a: number, b: number) => void; -export const amount_from_atoms: (a: number, b: number) => number; -export const amount_atoms: (a: number) => [number, number]; +export const data_deposit_fee: (a: bigint, b: number) => number; +export const decode_partially_signed_transaction_to_js: (a: number, b: number, c: number) => [number, number, number]; +export const decode_signed_transaction_to_js: (a: number, b: number, c: number) => [number, number, number]; +export const decode_transaction_to_js: (a: number, b: number, c: number) => [number, number, number]; +export const effective_pool_balance: (a: number, b: number, c: number) => [number, number, number]; +export const encode_destination: (a: number, b: number, c: number) => [number, number, number, number]; +export const encode_lock_for_block_count: (a: bigint) => [number, number]; +export const encode_lock_for_seconds: (a: bigint) => [number, number]; +export const encode_lock_until_height: (a: bigint) => [number, number]; +export const encode_lock_until_time: (a: bigint) => [number, number]; +export const encode_multisig_challenge: (a: number, b: number, c: number, d: number) => [number, number, number, number]; export const encode_outpoint_source_id: (a: number, b: number, c: number) => [number, number]; -export const make_private_key: () => [number, number]; +export const encode_partially_signed_transaction: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: any, l: number) => [number, number, number, number]; +export const encode_signed_transaction: (a: number, b: number, c: number, d: number) => [number, number, number, number]; +export const encode_signed_transaction_intent: (a: number, b: number, c: number, d: number) => [number, number, number, number]; +export const encode_stake_pool_data: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number) => [number, number, number, number]; +export const encode_transaction: (a: number, b: number, c: number, d: number, e: bigint) => [number, number, number, number]; +export const encode_witness: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: any, l: bigint, m: number) => [number, number, number, number]; +export const encode_witness_htlc_refund_multisig: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number, n: any, o: bigint, p: number) => [number, number, number, number]; +export const encode_witness_htlc_refund_single_sig: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: any, l: bigint, m: number) => [number, number, number, number]; +export const encode_witness_htlc_spend: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: any, n: bigint, o: number) => [number, number, number, number]; +export const encode_witness_no_signature: () => [number, number]; +export const estimate_transaction_size: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number]; +export const extended_public_key_from_extended_private_key: (a: number, b: number) => [number, number, number, number]; +export const extract_htlc_secret: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number]; +export const fungible_token_issuance_fee: (a: bigint, b: number) => number; +export const get_delegation_id: (a: number, b: number, c: number) => [number, number, number, number]; +export const get_order_id: (a: number, b: number, c: number) => [number, number, number, number]; +export const get_pool_id: (a: number, b: number, c: number) => [number, number, number, number]; +export const get_token_id: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; +export const get_transaction_id: (a: number, b: number, c: number) => [number, number, number, number]; +export const make_change_address: (a: number, b: number, c: number) => [number, number, number, number]; +export const make_change_address_public_key: (a: number, b: number, c: number) => [number, number, number, number]; export const make_default_account_privkey: (a: number, b: number, c: number) => [number, number, number, number]; +export const make_private_key: () => [number, number]; export const make_receiving_address: (a: number, b: number, c: number) => [number, number, number, number]; -export const make_change_address: (a: number, b: number, c: number) => [number, number, number, number]; +export const make_receiving_address_public_key: (a: number, b: number, c: number) => [number, number, number, number]; +export const make_transaction_intent_message_to_sign: (a: number, b: number, c: number, d: number) => [number, number, number, number]; +export const multisig_challenge_to_address: (a: number, b: number, c: number) => [number, number, number, number]; +export const nft_issuance_fee: (a: bigint, b: number) => number; export const pubkey_to_pubkeyhash_address: (a: number, b: number, c: number) => [number, number, number, number]; export const public_key_from_private_key: (a: number, b: number) => [number, number, number, number]; -export const extended_public_key_from_extended_private_key: (a: number, b: number) => [number, number, number, number]; -export const make_receiving_address_public_key: (a: number, b: number, c: number) => [number, number, number, number]; -export const make_change_address_public_key: (a: number, b: number, c: number) => [number, number, number, number]; -export const sign_message_for_spending: (a: number, b: number, c: number, d: number) => [number, number, number, number]; -export const verify_signature_for_spending: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number]; export const sign_challenge: (a: number, b: number, c: number, d: number) => [number, number, number, number]; +export const sign_message_for_spending: (a: number, b: number, c: number, d: number) => [number, number, number, number]; +export const staking_pool_spend_maturity_block_count: (a: bigint, b: number) => bigint; +export const token_change_authority_fee: (a: bigint, b: number) => number; +export const token_freeze_fee: (a: bigint, b: number) => number; +export const token_supply_change_fee: (a: bigint, b: number) => number; export const verify_challenge: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number]; -export const make_transaction_intent_message_to_sign: (a: number, b: number, c: number, d: number) => [number, number, number, number]; -export const encode_signed_transaction_intent: (a: number, b: number, c: any) => [number, number, number, number]; +export const verify_signature_for_spending: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number]; export const verify_transaction_intent: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number]; -export const encode_output_transfer: (a: number, b: number, c: number, d: number) => [number, number, number, number]; -export const encode_output_token_transfer: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number]; -export const staking_pool_spend_maturity_block_count: (a: bigint, b: number) => bigint; -export const encode_lock_for_block_count: (a: bigint) => [number, number]; -export const encode_lock_for_seconds: (a: bigint) => [number, number]; -export const encode_lock_until_time: (a: bigint) => [number, number]; -export const encode_lock_until_height: (a: bigint) => [number, number]; -export const encode_output_lock_then_transfer: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number]; -export const encode_output_token_lock_then_transfer: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => [number, number, number, number]; +export const encode_create_order_output: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => [number, number, number, number]; export const encode_output_coin_burn: (a: number) => [number, number, number, number]; -export const encode_output_token_burn: (a: number, b: number, c: number, d: number) => [number, number, number, number]; export const encode_output_create_delegation: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number]; -export const encode_output_delegate_staking: (a: number, b: number, c: number, d: number) => [number, number, number, number]; -export const encode_stake_pool_data: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number) => [number, number, number, number]; export const encode_output_create_stake_pool: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number]; -export const fungible_token_issuance_fee: (a: bigint, b: number) => number; -export const nft_issuance_fee: (a: bigint, b: number) => number; -export const token_supply_change_fee: (a: bigint, b: number) => number; -export const token_freeze_fee: (a: bigint, b: number) => number; -export const token_change_authority_fee: (a: bigint, b: number) => number; -export const encode_output_issue_fungible_token: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: bigint, l: number) => [number, number, number, number]; -export const get_token_id: (a: number, b: number, c: number) => [number, number, number, number]; -export const encode_output_issue_nft: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number, n: number, o: number, p: number, q: number, r: number, s: number, t: number, u: bigint, v: number) => [number, number, number, number]; export const encode_output_data_deposit: (a: number, b: number) => [number, number, number, number]; -export const data_deposit_fee: (a: bigint, b: number) => number; +export const encode_output_delegate_staking: (a: number, b: number, c: number, d: number) => [number, number, number, number]; export const encode_output_htlc: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number) => [number, number, number, number]; -export const extract_htlc_secret: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number]; -export const encode_input_for_utxo: (a: number, b: number, c: number) => [number, number, number, number]; -export const encode_input_for_withdraw_from_delegation: (a: number, b: number, c: number, d: bigint, e: number) => [number, number, number, number]; -export const estimate_transaction_size: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => [number, number, number]; -export const encode_transaction: (a: number, b: number, c: number, d: number, e: bigint) => [number, number, number, number]; -export const encode_witness_no_signature: () => [number, number]; -export const encode_witness: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number) => [number, number, number, number]; -export const encode_witness_htlc_secret: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number) => [number, number, number, number]; -export const encode_multisig_challenge: (a: number, b: number, c: number, d: number) => [number, number, number, number]; -export const encode_witness_htlc_multisig: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number, n: number) => [number, number, number, number]; -export const encode_signed_transaction: (a: number, b: number, c: number, d: number) => [number, number, number, number]; -export const get_transaction_id: (a: number, b: number, c: number) => [number, number, number, number]; -export const effective_pool_balance: (a: number, b: number, c: number) => [number, number, number]; -export const encode_input_for_mint_tokens: (a: number, b: number, c: number, d: bigint, e: number) => [number, number, number, number]; -export const encode_input_for_unmint_tokens: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; -export const encode_input_for_lock_token_supply: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; -export const encode_input_for_freeze_token: (a: number, b: number, c: number, d: bigint, e: number) => [number, number, number, number]; -export const encode_input_for_unfreeze_token: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; +export const encode_output_issue_fungible_token: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: bigint, l: number) => [number, number, number, number]; +export const encode_output_issue_nft: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number, n: number, o: number, p: number, q: number, r: number, s: number, t: number, u: bigint, v: number) => [number, number, number, number]; +export const encode_output_lock_then_transfer: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number]; +export const encode_output_produce_block_from_stake: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number]; +export const encode_output_token_burn: (a: number, b: number, c: number, d: number) => [number, number, number, number]; +export const encode_output_token_lock_then_transfer: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => [number, number, number, number]; +export const encode_output_token_transfer: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number]; +export const encode_output_transfer: (a: number, b: number, c: number, d: number) => [number, number, number, number]; +export const __wbg_amount_free: (a: number, b: number) => void; +export const amount_atoms: (a: number) => [number, number]; +export const amount_from_atoms: (a: number, b: number) => number; export const encode_input_for_change_token_authority: (a: number, b: number, c: number, d: number, e: bigint, f: number) => [number, number, number, number]; export const encode_input_for_change_token_metadata_uri: (a: number, b: number, c: number, d: number, e: bigint, f: number) => [number, number, number, number]; -export const encode_create_order_output: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => [number, number, number, number]; -export const encode_input_for_fill_order: (a: number, b: number, c: number, d: number, e: number, f: bigint, g: number) => [number, number, number, number]; -export const encode_input_for_conclude_order: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; +export const encode_input_for_conclude_order: (a: number, b: number, c: bigint, d: bigint, e: number) => [number, number, number, number]; +export const encode_input_for_fill_order: (a: number, b: number, c: number, d: number, e: number, f: bigint, g: bigint, h: number) => [number, number, number, number]; +export const encode_input_for_freeze_order: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; +export const encode_input_for_freeze_token: (a: number, b: number, c: number, d: bigint, e: number) => [number, number, number, number]; +export const encode_input_for_lock_token_supply: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; +export const encode_input_for_mint_tokens: (a: number, b: number, c: number, d: bigint, e: number) => [number, number, number, number]; +export const encode_input_for_unfreeze_token: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; +export const encode_input_for_unmint_tokens: (a: number, b: number, c: bigint, d: number) => [number, number, number, number]; +export const encode_input_for_utxo: (a: number, b: number, c: number) => [number, number, number, number]; +export const encode_input_for_withdraw_from_delegation: (a: number, b: number, c: number, d: bigint, e: number) => [number, number, number, number]; +export const internal_verify_witness: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: any, l: bigint, m: number) => [number, number]; export const rustsecp256k1_v0_10_0_context_create: (a: number) => number; export const rustsecp256k1_v0_10_0_context_destroy: (a: number) => void; -export const rustsecp256k1_v0_10_0_default_illegal_callback_fn: (a: number, b: number) => void; export const rustsecp256k1_v0_10_0_default_error_callback_fn: (a: number, b: number) => void; -export const __wbindgen_exn_store: (a: number) => void; -export const __externref_table_alloc: () => number; -export const __wbindgen_export_2: WebAssembly.Table; +export const rustsecp256k1_v0_10_0_default_illegal_callback_fn: (a: number, b: number) => void; export const __wbindgen_malloc: (a: number, b: number) => number; export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number; +export const __wbindgen_exn_store: (a: number) => void; +export const __externref_table_alloc: () => number; +export const __wbindgen_externrefs: WebAssembly.Table; export const __wbindgen_free: (a: number, b: number, c: number) => void; export const __externref_table_dealloc: (a: number) => void; export const __wbindgen_start: () => void; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b52e48a..b04fe59 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -172,12 +172,15 @@ importers: packages/sdk: dependencies: '@mintlayer/wasm-lib': - specifier: ^0.1.0 - version: 0.1.0 + specifier: workspace:* + version: link:../wasm-lib devDependencies: '@types/jest': specifier: ^29.5.14 version: 29.5.14 + '@types/node': + specifier: ^20.4.2 + version: 20.19.43 jest: specifier: ^29.7.0 version: 29.7.0(@types/node@20.19.43) @@ -522,9 +525,6 @@ packages: '@leichtgewicht/ip-codec@2.0.5': resolution: {integrity: sha512-Vo+PSpZG2/fmgmiNzYK9qWRh8h/CHrwD0mo1h1DzL4yzHNSfWYujGTYsWGreD000gcgmZ7K4Ys6Tx9TxtsKdDw==} - '@mintlayer/wasm-lib@0.1.0': - resolution: {integrity: sha512-XDcOHtTl6BZi3kmUCTvclIkLEujWuvck2wDd9ixifFHHYicAZFKzUcuKLUyo6Rah7+TZ1/iwMFEfY9GrvLN8+Q==} - '@napi-rs/wasm-runtime@1.2.3': resolution: {integrity: sha512-UMduMbqO5s5zF2NkNacMT/yK5Y5QiKvWr2+50bzIIxFDwVJ2h49b+oyjaCGPhJxd2/gC2x39EHv/gHVuu36x2Q==} engines: {node: ^20.19.0 || ^22.13.0 || >=23.5.0} @@ -4480,8 +4480,6 @@ snapshots: '@leichtgewicht/ip-codec@2.0.5': {} - '@mintlayer/wasm-lib@0.1.0': {} - '@napi-rs/wasm-runtime@1.2.3(@emnapi/core@1.10.0)(@emnapi/runtime@1.10.0)': dependencies: '@emnapi/core': 1.10.0