diff --git a/payjoin-cli/CHANGELOG.md b/payjoin-cli/CHANGELOG.md index 394878ecb..be97ca13f 100644 --- a/payjoin-cli/CHANGELOG.md +++ b/payjoin-cli/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +- 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 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..e74f1dfce 100644 --- a/payjoin-cli/src/app/wallet.rs +++ b/payjoin-cli/src/app/wallet.rs @@ -197,7 +197,21 @@ 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()) + // 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() + .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 @@ -215,21 +229,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") }