|
| 1 | +import { ccc } from "@ckb-ccc/ccc"; |
| 2 | +import { bitcoin, signer } from "@ckb-ccc/playground"; |
| 3 | + |
| 4 | +// Supported wallets: Unisat, JoyID, Xverse |
| 5 | +// Check if the current signer is also a Bitcoin signer |
| 6 | +if (!(signer instanceof ccc.SignerBtc)) { |
| 7 | + throw new Error("Signer is not a Bitcoin signer"); |
| 8 | +} |
| 9 | + |
| 10 | +// Only support testnet for safety |
| 11 | +if (signer.client.addressPrefix !== "ckt") { |
| 12 | + throw new Error("Only supported on testnet"); |
| 13 | +} |
| 14 | + |
| 15 | +// Xverse has deprecated Testnet3 support, so we default to Signet. Make sure to switch to Signet in Xverse's network settings. |
| 16 | +const isXverse = signer instanceof ccc.Xverse.Signer; |
| 17 | +const btcTestnetName = isXverse ? "signet" : "testnet"; |
| 18 | + |
| 19 | +const btcAddress = await signer.getBtcAccount(); |
| 20 | +// Fetch UTXOs from mempool.space API |
| 21 | +const utxos = (await fetch( |
| 22 | + `https://mempool.space/${btcTestnetName}/api/address/${btcAddress}/utxo`, |
| 23 | +).then((res) => { |
| 24 | + if (!res.ok) { |
| 25 | + throw new Error(`Failed to fetch UTXOs: ${res.status} ${res.statusText}`); |
| 26 | + } |
| 27 | + return res.json(); |
| 28 | +})) as { value: number; txid: string; vout: number }[]; |
| 29 | + |
| 30 | +const DUST_LIMIT = 546; |
| 31 | +const FEE_SATS = 200; |
| 32 | + |
| 33 | +// Select a UTXO above the 546 sat dust threshold |
| 34 | +const selectedUtxo = utxos.find((utxo) => utxo.value > DUST_LIMIT + FEE_SATS); |
| 35 | +if (!selectedUtxo) { |
| 36 | + throw new Error("No UTXO available"); |
| 37 | +} |
| 38 | + |
| 39 | +// Fetch the full transaction to get the scriptpubkey |
| 40 | +const btcTx = (await fetch( |
| 41 | + `https://mempool.space/${btcTestnetName}/api/tx/${selectedUtxo.txid}`, |
| 42 | +).then((res) => { |
| 43 | + if (!res.ok) { |
| 44 | + throw new Error( |
| 45 | + `Failed to fetch transaction: ${res.status} ${res.statusText}`, |
| 46 | + ); |
| 47 | + } |
| 48 | + return res.json(); |
| 49 | +})) as { |
| 50 | + vout: { |
| 51 | + value: number; |
| 52 | + scriptpubkey: string; |
| 53 | + scriptpubkey_type: string; |
| 54 | + }[]; |
| 55 | +}; |
| 56 | +const vout = btcTx.vout[selectedUtxo.vout]; |
| 57 | + |
| 58 | +if (!vout || !vout.scriptpubkey) { |
| 59 | + throw new Error("Invalid vout data"); |
| 60 | +} |
| 61 | + |
| 62 | +// Build PSBT with the selected UTXO as input |
| 63 | +const psbt = new bitcoin.Psbt({ |
| 64 | + network: isXverse ? bitcoin.networks.testnet : bitcoin.networks.testnet, |
| 65 | +}); |
| 66 | +const input: { |
| 67 | + hash: string; |
| 68 | + index: number; |
| 69 | + witnessUtxo: { |
| 70 | + script: Uint8Array; |
| 71 | + value: bigint; |
| 72 | + }; |
| 73 | + tapInternalKey?: Uint8Array; |
| 74 | +} = { |
| 75 | + hash: selectedUtxo.txid, |
| 76 | + index: selectedUtxo.vout, |
| 77 | + witnessUtxo: { |
| 78 | + script: ccc.bytesFrom(vout.scriptpubkey), |
| 79 | + value: BigInt(vout.value), |
| 80 | + }, |
| 81 | +}; |
| 82 | + |
| 83 | +// Handle Taproot (P2TR) specific input fields |
| 84 | +if ( |
| 85 | + vout.scriptpubkey_type === "v1_p2tr" || |
| 86 | + vout.scriptpubkey_type === "witness_v1_taproot" |
| 87 | +) { |
| 88 | + input.tapInternalKey = ccc.bytesFrom(await signer.getBtcPublicKey()).slice(1); |
| 89 | +} |
| 90 | + |
| 91 | +psbt.addInput(input); |
| 92 | + |
| 93 | +// Add a single output back to the same address minus a hardcoded 200 sat fee |
| 94 | +psbt.addOutput({ |
| 95 | + address: btcAddress, |
| 96 | + value: BigInt(vout.value) - BigInt(FEE_SATS), |
| 97 | +}); |
| 98 | + |
| 99 | +// Sign and broadcast the transaction |
| 100 | +const txId = await signer.signAndBroadcastPsbt(psbt.toHex()); |
| 101 | +console.log( |
| 102 | + `View transaction: https://mempool.space/${btcTestnetName}/tx/${txId.slice(2)}`, |
| 103 | +); |
0 commit comments