From 03f1ddbc7d9ead58d92fad7ffc2b8f58b742dd53 Mon Sep 17 00:00:00 2001 From: Benalleng Date: Mon, 12 Oct 2026 13:32:19 -0400 Subject: [PATCH 1/2] Handle p2tr key-spend weights This handles a bug that prevented taproot coins from being properly handled by the payjoin-cli. Namely this prevents the panic when `list_unspent` reaches a taprrot coin. Taproot script-path spends remain unsupported. --- payjoin-cli/CHANGELOG.md | 3 +++ payjoin-cli/src/app/wallet.rs | 36 ++++++++++++++++++++++------------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/payjoin-cli/CHANGELOG.md b/payjoin-cli/CHANGELOG.md index 394878ecb..d9820255d 100644 --- a/payjoin-cli/CHANGELOG.md +++ b/payjoin-cli/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +- Fix panic when spending taproot UTXOs. They are now contributed as key-path + spends with an assumed default input weight. + ## 1.0.0 The first stable payjoin-cli release, tracking payjoin 1.2.0. The CLI adopts diff --git a/payjoin-cli/src/app/wallet.rs b/payjoin-cli/src/app/wallet.rs index 8532c8535..a58f9c1a5 100644 --- a/payjoin-cli/src/app/wallet.rs +++ b/payjoin-cli/src/app/wallet.rs @@ -197,7 +197,12 @@ impl BitcoindWallet { .block_on(async { self.rpc.list_unspent(None, None, None, None, None).await }) }) .context("Failed to list unspent")?; - Ok(unspent.0.into_iter().map(input_pair_from_corepc).collect()) + unspent + .0 + .into_iter() + .map(input_pair_from_corepc) + .collect::, _>>() + .context("Failed to create input pair from unspent output") } /// Check if wallet has any spendable UTXOs @@ -215,21 +220,26 @@ impl BitcoindWallet { } } -pub fn input_pair_from_corepc(utxo: ListUnspentItem) -> InputPair { +pub fn input_pair_from_corepc(utxo: ListUnspentItem) -> Result { + let txout = TxOut { + value: Amount::from_btc(utxo.amount.to_btc()).expect("Valid amount"), + script_pubkey: utxo.script_pubkey, + }; + let outpoint = OutPoint { txid: utxo.txid, vout: utxo.vout }; + + if txout.script_pubkey.is_p2tr() { + // bitcoin core wallets spend the taproot key path by default + return InputPair::new_p2tr_keyspend(txout, outpoint) + .context("Failed to create taproot key-spend input pair"); + } + let psbtin = Input { // NOTE: non_witness_utxo is not necessary because bitcoin-cli always supplies // witness_utxo, even for non-witness inputs - witness_utxo: Some(TxOut { - value: Amount::from_btc(utxo.amount.to_btc()).expect("Valid amount"), - script_pubkey: utxo.script_pubkey, - }), - redeem_script: None, - witness_script: None, // Not available in this version - ..Default::default() - }; - let txin = TxIn { - previous_output: OutPoint { txid: utxo.txid, vout: utxo.vout }, + witness_utxo: Some(txout), + redeem_script: utxo.redeem_script, ..Default::default() }; - InputPair::new(txin, psbtin, None).expect("Input pair should be valid") + let txin = TxIn { previous_output: outpoint, ..Default::default() }; + InputPair::new(txin, psbtin, None).context("Failed to create input pair") } From 3989c828b4394f94ddbb3fe28d3017e8e86d4b28 Mon Sep 17 00:00:00 2001 From: Benalleng Date: Mon, 12 Oct 2026 16:52:23 -0400 Subject: [PATCH 2/2] Filter invalid coins from payjoin-cli when validating the input weights Currently P2WSH and P2SH or some custom script is not able to be properly measured by `InputPair` and as a result `list_unspent` will fail blocking the whole wallet instead of the unsupported coins. By filtering those coins out of the available list we can still optimistically proceed and avoid any unsupported coins moving on to ones that we can use for a payjoin. --- payjoin-cli/CHANGELOG.md | 1 + payjoin-cli/src/app/wallet.rs | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/payjoin-cli/CHANGELOG.md b/payjoin-cli/CHANGELOG.md index d9820255d..be97ca13f 100644 --- a/payjoin-cli/CHANGELOG.md +++ b/payjoin-cli/CHANGELOG.md @@ -4,6 +4,7 @@ - Fix panic when spending taproot UTXOs. They are now contributed as key-path spends with an assumed default input weight. +- Any unsupported coin is simply skipped instead of crashing the cli. ## 1.0.0 diff --git a/payjoin-cli/src/app/wallet.rs b/payjoin-cli/src/app/wallet.rs index a58f9c1a5..e74f1dfce 100644 --- a/payjoin-cli/src/app/wallet.rs +++ b/payjoin-cli/src/app/wallet.rs @@ -197,12 +197,21 @@ impl BitcoindWallet { .block_on(async { self.rpc.list_unspent(None, None, None, None, None).await }) }) .context("Failed to list unspent")?; - unspent + // Skip UTXOs whose input weight cannot be predicted before signing + // (e.g. P2WSH, legacy P2SH, bare scripts) rather than failing the + // whole candidate list. + let candidate_inputs = unspent .0 .into_iter() - .map(input_pair_from_corepc) - .collect::, _>>() - .context("Failed to create input pair from unspent output") + .filter_map(|utxo| match input_pair_from_corepc(utxo) { + Ok(pair) => Some(pair), + Err(e) => { + tracing::warn!("Skipping UTXO unusable for payjoin: {e:#}"); + None + } + }) + .collect(); + Ok(candidate_inputs) } /// Check if wallet has any spendable UTXOs