Signing a Transaction
async function signWalletTransaction() {
const utxos = await fetchUTXO(nativeSegwitAddress);
if (!utxos) {
alert('No UTXOs found! Please deposit some funds to your wallet.');
return;
}
const selectedUTXO = await selectUTXO(10000, utxos);
const scriptPubKey = await fetchScriptPubKey(selectedUTXO.txid, selectedUTXO.vout);
const psbt = await createPSBT({
utxo: selectedUTXO,
recipientAddress: RECIPIENT_ADDRESS,
changeAddress: nativeSegwitAddress,
amountToSend: 1000,
scriptPubKey,
});
try {
await signTransaction({
payload: {
network: {
type: BitcoinNetworkType.Mainnet,
},
psbtBase64: psbt,
broadcast: true,
message: "tip the author! Don't worry this will not be broadcasted.",
inputsToSign: [
{
address: nativeSegwitAddress!,
signingIndexes: [0],
},
],
},
onFinish: (response) => {
const psbResponse = Psbt.fromBase64(response.psbtBase64);
psbResponse.finalizeAllInputs();
const signedTx = psbResponse.extractTransaction();
const txHex = signedTx.toHex();
},
onCancel: () => {
alert('Request canceled');
},
});
} catch (err) {
console.error(err);
}
}Understanding UTXOs:
Script
Creating PSBTs
Last updated