-
Notifications
You must be signed in to change notification settings - Fork 34
feat(btc): add PSBT signing and broadcasting support #346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
e9a3a6e
19490f4
b4d9e5e
efcdcd8
7b16831
8f4091f
f3e3c7f
6c31f8c
fa639c3
bc82b34
a47d1bf
2dad12b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export * from "./psbt.js"; | ||
| export * from "./signerBtc.js"; | ||
| export * from "./signerBtcPublicKeyReadonly.js"; | ||
| export * from "./verify.js"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { HexLike, hexFrom } from "../../hex/index.js"; | ||
|
|
||
| /** | ||
| * Options for signing a PSBT (Partially Signed Bitcoin Transaction) | ||
| */ | ||
| export type SignPsbtOptionsLike = { | ||
| /** | ||
| * Whether to finalize the PSBT after signing. | ||
| * Default is true. | ||
| */ | ||
| autoFinalized?: boolean; | ||
| /** | ||
| * Array of inputs to sign | ||
| */ | ||
| inputsToSign?: InputToSignLike[]; | ||
| }; | ||
|
|
||
| export class SignPsbtOptions { | ||
| constructor( | ||
| public autoFinalized: boolean, | ||
| public inputsToSign: InputToSign[], | ||
| ) {} | ||
|
|
||
| static from(options?: SignPsbtOptionsLike): SignPsbtOptions { | ||
| return new SignPsbtOptions( | ||
| options?.autoFinalized ?? true, | ||
| options?.inputsToSign?.map((i) => InputToSign.from(i)) ?? [], | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Specification for an input to sign in a PSBT. | ||
| * Must specify at least one of: address or pubkey. | ||
| */ | ||
| export type InputToSignLike = { | ||
| /** | ||
| * Which input to sign (index in the PSBT inputs array) | ||
| */ | ||
| index: number; | ||
| /** | ||
| * (Optional) Sighash types to use for signing. | ||
| */ | ||
| sighashTypes?: number[]; | ||
| /** | ||
| * (Optional) When signing and unlocking Taproot addresses, the tweakSigner is used by default | ||
| * for signature generation. Setting this to true allows for signing with the original private key. | ||
| * Default value is false. | ||
| */ | ||
| disableTweakSigner?: boolean; | ||
| } & ( | ||
| | { | ||
| /** | ||
| * The address whose corresponding private key to use for signing. | ||
| */ | ||
| address: string; | ||
| /** | ||
| * The public key whose corresponding private key to use for signing. | ||
| */ | ||
| publicKey?: HexLike; | ||
| } | ||
| | { | ||
| /** | ||
| * The address whose corresponding private key to use for signing. | ||
| */ | ||
| address?: string; | ||
| /** | ||
| * The public key whose corresponding private key to use for signing. | ||
| */ | ||
| publicKey: HexLike; | ||
| } | ||
| ); | ||
|
|
||
| export class InputToSign { | ||
| constructor( | ||
| public index: number, | ||
| public sighashTypes?: number[], | ||
| public disableTweakSigner?: boolean, | ||
| public address?: string, | ||
| public publicKey?: string, | ||
| ) {} | ||
|
|
||
| static from(input: InputToSignLike): InputToSign { | ||
| return new InputToSign( | ||
| input.index, | ||
| input.sighashTypes, | ||
| input.disableTweakSigner, | ||
| input.address, | ||
| input.publicKey ? hexFrom(input.publicKey).slice(2) : undefined, | ||
| ); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| import { ccc } from "@ckb-ccc/ccc"; | ||
| import * as bitcoinLib from "bitcoinjs-lib"; | ||
|
|
||
| export function render(tx: ccc.Transaction): Promise<void>; | ||
| export const signer: ccc.Signer; | ||
| export const client: ccc.Client; | ||
| export const bitcoin: typeof bitcoinLib; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { ccc } from "@ckb-ccc/ccc"; | ||
| import { bitcoin, signer } from "@ckb-ccc/playground"; | ||
|
|
||
| // Supported wallets: Unisat, JoyID, Xverse | ||
| // Check if the current signer is also a Bitcoin signer | ||
| if (!(signer instanceof ccc.SignerBtc)) { | ||
| throw new Error("Signer is not a Bitcoin signer"); | ||
| } | ||
|
|
||
| // Only support testnet for safety | ||
| if (signer.client.addressPrefix !== "ckt") { | ||
| throw new Error("Only supported on testnet"); | ||
| } | ||
|
|
||
| // Xverse has deprecated Testnet3 support, so we default to Signet. Make sure to switch to Signet in Xverse's network settings. | ||
| const isXverse = signer instanceof ccc.Xverse.Signer; | ||
| const btcTestnetName = isXverse ? "signet" : "testnet"; | ||
|
|
||
| const btcAddress = await signer.getBtcAccount(); | ||
| // Fetch UTXOs from mempool.space API | ||
| const utxos = (await fetch( | ||
| `https://mempool.space/${btcTestnetName}/api/address/${btcAddress}/utxo`, | ||
| ).then((res) => { | ||
| if (!res.ok) { | ||
| throw new Error(`Failed to fetch UTXOs: ${res.status} ${res.statusText}`); | ||
| } | ||
| return res.json(); | ||
| })) as { value: number; txid: string; vout: number }[]; | ||
|
|
||
| const DUST_LIMIT = 546; | ||
| const FEE_SATS = 200; | ||
|
|
||
| // Select a UTXO above the 546 sat dust threshold | ||
| const selectedUtxo = utxos.find((utxo) => utxo.value > DUST_LIMIT + FEE_SATS); | ||
| if (!selectedUtxo) { | ||
| throw new Error("No UTXO available"); | ||
| } | ||
|
|
||
| // Fetch the full transaction to get the scriptpubkey | ||
| const btcTx = (await fetch( | ||
| `https://mempool.space/${btcTestnetName}/api/tx/${selectedUtxo.txid}`, | ||
| ).then((res) => { | ||
| if (!res.ok) { | ||
| throw new Error( | ||
| `Failed to fetch transaction: ${res.status} ${res.statusText}`, | ||
| ); | ||
| } | ||
| return res.json(); | ||
| })) as { | ||
| vout: { | ||
| value: number; | ||
| scriptpubkey: string; | ||
| scriptpubkey_type: string; | ||
| }[]; | ||
| }; | ||
| const vout = btcTx.vout[selectedUtxo.vout]; | ||
|
|
||
| if (!vout || !vout.scriptpubkey) { | ||
| throw new Error("Invalid vout data"); | ||
| } | ||
|
|
||
| // Build PSBT with the selected UTXO as input | ||
| const psbt = new bitcoin.Psbt({ | ||
| network: isXverse ? bitcoin.networks.testnet : bitcoin.networks.testnet, | ||
| }); | ||
| const input: { | ||
| hash: string; | ||
| index: number; | ||
| witnessUtxo: { | ||
| script: Uint8Array; | ||
| value: bigint; | ||
| }; | ||
| tapInternalKey?: Uint8Array; | ||
| } = { | ||
| hash: selectedUtxo.txid, | ||
| index: selectedUtxo.vout, | ||
| witnessUtxo: { | ||
| script: ccc.bytesFrom(vout.scriptpubkey), | ||
| value: BigInt(vout.value), | ||
| }, | ||
| }; | ||
|
|
||
| // Handle Taproot (P2TR) specific input fields | ||
| if ( | ||
| vout.scriptpubkey_type === "v1_p2tr" || | ||
| vout.scriptpubkey_type === "witness_v1_taproot" | ||
| ) { | ||
| const publicKey = await signer.getBtcPublicKey(); | ||
| input.tapInternalKey = ccc.bytesFrom(ccc.hexFrom(publicKey)).slice(1); | ||
| } | ||
|
|
||
| psbt.addInput(input); | ||
|
|
||
| // Add a single output back to the same address minus a hardcoded 200 sat fee | ||
| psbt.addOutput({ | ||
| address: btcAddress, | ||
| value: BigInt(vout.value) - BigInt(FEE_SATS), | ||
| }); | ||
|
|
||
| // Sign and broadcast the transaction | ||
| const txId = await signer.signAndBroadcastPsbt(psbt.toHex()); | ||
| console.log( | ||
| `View transaction: https://mempool.space/${btcTestnetName}/tx/${txId.slice(2)}`, | ||
| ); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -198,4 +198,80 @@ export class BitcoinSigner extends ccc.SignerBtc { | |||||
| ); | ||||||
| return signature; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Signs a PSBT using JoyID wallet. | ||||||
| * | ||||||
| * @param psbtHex - The hex string of PSBT to sign. | ||||||
| * @returns A promise that resolves to the signed PSBT as a Hex string. | ||||||
| */ | ||||||
| async signPsbt( | ||||||
| psbtHex: ccc.HexLike, | ||||||
| options?: ccc.SignPsbtOptionsLike, | ||||||
| ): Promise<ccc.Hex> { | ||||||
| const { address } = await this.assertConnection(); | ||||||
| const formattedOptions = ccc.SignPsbtOptions.from(options); | ||||||
|
|
||||||
| const config = this.getConfig(); | ||||||
| const { tx: signedPsbtHex } = await createPopup( | ||||||
| buildJoyIDURL( | ||||||
| { | ||||||
| ...config, | ||||||
| tx: ccc.hexFrom(psbtHex).slice(2), | ||||||
| options: formattedOptions, | ||||||
| signerAddress: address, | ||||||
| autoFinalized: formattedOptions.autoFinalized, | ||||||
| }, | ||||||
| "popup", | ||||||
| "/sign-psbt", | ||||||
| ), | ||||||
| { ...config, type: DappRequestType.SignPsbt }, | ||||||
| ); | ||||||
|
|
||||||
| return ccc.hexFrom(signedPsbtHex); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Broadcasts a PSBT to the Bitcoin network. | ||||||
| * | ||||||
| * @remarks | ||||||
| * JoyID does not support broadcasting a signed PSBT directly. | ||||||
| * It only supports "Sign and Broadcast" as a single atomic operation via `signAndBroadcastPsbt`. | ||||||
| */ | ||||||
| async broadcastPsbt( | ||||||
| _psbtHex: ccc.HexLike, | ||||||
| _options?: ccc.SignPsbtOptionsLike, | ||||||
| ): Promise<ccc.Hex> { | ||||||
| throw new Error( | ||||||
| "JoyID does not support broadcasting signed PSBTs directly. Use signAndBroadcastPsbt instead.", | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| async signAndBroadcastPsbt( | ||||||
| psbtHex: ccc.HexLike, | ||||||
| options?: ccc.SignPsbtOptionsLike, | ||||||
| ): Promise<ccc.Hex> { | ||||||
| const { address } = await this.assertConnection(); | ||||||
| const formattedOptions = ccc.SignPsbtOptions.from(options); | ||||||
|
|
||||||
| const config = this.getConfig(); | ||||||
| // ccc.hexFrom adds 0x prefix, but BTC expects non-0x | ||||||
| const { tx: txid } = await createPopup( | ||||||
| buildJoyIDURL( | ||||||
| { | ||||||
| ...config, | ||||||
| tx: ccc.hexFrom(psbtHex).slice(2), | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The expression
Suggested change
|
||||||
| options: formattedOptions, | ||||||
| signerAddress: address, | ||||||
| autoFinalized: true, // sendPsbt always finalizes | ||||||
| isSend: true, | ||||||
| }, | ||||||
| "popup", | ||||||
| "/sign-psbt", | ||||||
| ), | ||||||
| { ...config, type: DappRequestType.SignPsbt }, // Use SignPsbt type for both operations | ||||||
| ); | ||||||
|
|
||||||
| return ccc.hexFrom(txid); | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The expression
ccc.hexFrom(psbtHex).slice(2)is used to get a non-0x-prefixed hex string. A more direct and readable approach is to useccc.bytesTo(ccc.bytesFrom(psbtHex), "hex"). This avoids the intermediate step of potentially adding and then immediately removing the "0x" prefix.