1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| const [txHex, setTxHex] = useState(''); const createTransaction = async () => { const network = bitcoin.networks.testnet; const senderAddress = address; const recipientAddress = 'tb1qp4gpusm5vq7ysa38wa92a6n8ps89hp7ts2907m'; const amountToSend = 0.0001 * 1e8; const utxos = await getUtxos();
if (utxos.length === 0) { throw new Error('No UTXOs available for the address.'); } const utxo = utxos[0];
if (!/^[0-9a-fA-F]{64}$/.test(utxo.txid)) { throw new TypeError('Invalid txid'); } const psbt = new bitcoin.Psbt({ network }); try { psbt.addInput({ hash: utxo.txid, index: utxo.vout, witnessUtxo: { script: bitcoin.address.toOutputScript(senderAddress, network), value: utxo.value, }, }); } catch (error) { throw error; } try { psbt.addOutput({ address: recipientAddress, value: amountToSend, }); } catch (error) { throw error; }
const changeAddress = senderAddress; const fee = 0.00001 * 1e8; const changeValue = utxo.value - amountToSend - fee;
if (changeValue > 0) { try { psbt.addOutput({ address: changeAddress, value: changeValue, }); } catch (error) { throw error; } } const psbtHex = psbt.toHex(); setTxHex(psbtHex) };
|