Transactions
Performing a Transaction
The following code snippet shows the following steps:
- Create a Partially Signed Bitcoin Transaction (PSBT)
- Prepare the PSBT by fetching UTXOs to cover any transaction fees and add necessary outputs
- Sign the PSBT
- Relay the PSBT to the network
import { JsonRpcDatasource, PSBTBuilder } from "@ordzaar/ordit-sdk";
import { signPsbt } from "@ordzaar/ordit-sdk/unisat";
async function performTransaction() {
// 1. Create psbt
const psbtBuilder = new PSBTBuilder({
address: "YOUR_ADDRESS",
publicKey: "YOUR_PUBLIC_KEY",
outputs: [
{
address: "YOUR_OUTPUT_ADDRESS",
value: 600, // amount in satoshis
},
],
feeRate: 1,
network: "testnet",
});
// 2. Prepare psbt for sending by fetching UTXOs to cover any transaction fees and outputs
await psbtBuilder.prepare();
// 3. Sign psbt (using browser wallet API)
const signedPsbt = await signPsbt(psbtBuilder.toPSBT());
// 4. Relay psbt to the network
const datasource = new JsonRpcDatasource({ network });
const txId = await datasource.relay({ hex: signedPsbt.hex });
console.log("Transaction Id: ", txId);
}
performTransaction();
API Reference
PSBTBuilder
constructor
constructor(options)
This class creates a PSBTBuilder object and encapsulates a Psbt
.
Parameters:
options
:object
(required)publicKey
:string
- Public keyfeeRate
:number
- Fee rate in sats per byteaddress
:string
- Address of sender of transactionoutputs
:Output[]
- Recipients of transactionnetwork
:'mainnet' | 'testnet' | 'signet' | 'regtest'
(optional) (defaults tomainnet
)
Returns: PSBTBuilder
prepare
prepare()
Prepares PSBT to be set to a network, calculating and validating both inputs and outputs.
Returns: Promise<void>
setRBF
setRBF(value)
Enables or disables the Replace-by-fee (RBF) feature (see BIP-125). This option is enabled by default.
warning
If RBF is disabled, it will not be possible to increase the transaction fee and low-fee transactions may remain unconfirmed or fail.
Parameters:
value
:boolean