Bitcoin Transaction Creation Functions

selectPaymentUtxos(utxos, amount, vinsLength, voutsLength, feeRate):

Purpose:

Selects a set of UTXOs (Unspent Transaction Outputs) for a payment transaction, ensuring that the total value of selected UTXOs covers the payment amount and transaction fees.


Parameters:

utxos: Array of UTXOs available for selection.

amount: Payment amount in Satoshis.

vinsLength: Number of inputs in the transaction.

voutsLength: Number of outputs in the transaction.

feeRate: Fee rate for the transaction.

Returns:

Array of selected UTXOs.

Details:

Sorts available UTXOs by value in descending order.

teratively selects UTXOs until the total value covers the payment amount and transaction fees.

Returns an empty array if the selected UTXOs' total value is insufficient.


createPaymentPsbt(recipientAddress, amount, feeRate):

Purpose:

Creates a PSBT for a payment transaction to a specified recipient address.

Parameters:

recipientAddress: Bitcoin address of the payment recipient.

amount: Payment amount in Satoshis.

feeRate: Fee rate for the transaction.

Details:

Initializes a new PSBT object.

Retrieves UTXOs associated with the recipient address.

Calls selectPaymentUtxos to select UTXOs for the payment.

Adds selected UTXOs as inputs to the PSBT.


createInscriptionPsbt(recipientAddress, inscriptionId, feeRate):

Purpose:

Creates a PSBT for an inscription transaction to a specified recipient address.

Parameters:

recipientAddress: Bitcoin address of the payment recipient.

inscriptionId: ID associated with the inscription (not used in the function).

feeRate: Fee rate for the transaction.

Details:

Initializes a new PSBT object.

Retrieves UTXOs associated with the recipient address.

Calls selectPaymentUtxos to select UTXOs for the inscription.

Adds selected UTXOs as inputs to the PSBT.


getRelayFee():

Purpose:

Retrieves the relay fee from the Bitcoin network.

Returns:

The relay fee (Satoshi per byte).

Details:

Calls bitcoin-cli getnetworkinfo to fetch network information.

Parses the output to obtain the relay fee.

Returns the relay fee or 0 if an error occurs.


createRawTransaction(inputs, outputs):

Purpose:

Creates a raw Bitcoin transaction.

Parameters:

inputs: Array of transaction inputs.

outputs: Array of transaction outputs.

Returns:

The raw transaction string.

Details:

Calls bitcoin-cli createrawtransaction with the specified inputs and outputs.

Returns the raw transaction string or throws an error if the command fails.


signRawTransactionWithWallet(wallet, rawTransaction):

Purpose:

Signs a raw Bitcoin transaction using a specified wallet.

Parameters:

wallet: Wallet to use for signing.

rawTransaction: The raw transaction string to sign.

Returns:

The signed raw transaction details (parsed JSON).

Details:

Calls bitcoin-cli -rpcwallet to sign the raw transaction with the specified wallet.

Returns the signed raw transaction details as parsed JSON or throws an error if the command fails.


sendRawTransaction(rawTransaction):

Purpose:

Sends a raw Bitcoin transaction to the network.

Parameters:

rawTransaction: The raw transaction string to send.

Returns:

The transaction ID (txid) if successful.

Details:

Calls bitcoin-cli sendrawtransaction with the specified raw transaction.

Returns the transaction ID or throws an error if the command fails.


Last updated