|
11 | 11 |
|
12 | 12 | use std::str::FromStr; |
13 | 13 |
|
14 | | -use bitcoin::{psbt, AddressType, Psbt, TxIn, TxOut}; |
| 14 | +use bitcoin::{psbt, AddressType, OutPoint, Psbt, Sequence, TxIn, TxOut}; |
15 | 15 | pub(crate) use error::InternalPayloadError; |
16 | 16 | pub use error::{ |
17 | 17 | Error, InputContributionError, JsonReply, OutputSubstitutionError, PayloadError, |
@@ -58,6 +58,69 @@ impl InputPair { |
58 | 58 | Ok(input_pair) |
59 | 59 | } |
60 | 60 |
|
| 61 | + /// Helper function for creating SegWit input pairs |
| 62 | + fn new_segwit_input_pair( |
| 63 | + txout: TxOut, |
| 64 | + outpoint: OutPoint, |
| 65 | + sequence: Option<Sequence>, |
| 66 | + ) -> Result<Self, PsbtInputError> { |
| 67 | + let txin = TxIn { |
| 68 | + previous_output: OutPoint { txid: outpoint.txid, vout: outpoint.vout }, |
| 69 | + script_sig: Default::default(), |
| 70 | + sequence: sequence.unwrap_or_default(), |
| 71 | + witness: Default::default(), |
| 72 | + }; |
| 73 | + |
| 74 | + let psbtin = psbt::Input { |
| 75 | + witness_utxo: Some(TxOut { value: txout.value, script_pubkey: txout.script_pubkey }), |
| 76 | + ..psbt::Input::default() |
| 77 | + }; |
| 78 | + let input_pair = Self { txin, psbtin }; |
| 79 | + let raw = InternalInputPair::from(&input_pair); |
| 80 | + raw.validate_utxo()?; |
| 81 | + |
| 82 | + Ok(input_pair) |
| 83 | + } |
| 84 | + |
| 85 | + /// Constructs a new ['InputPair'] for spending a native SegWit P2WPKH output |
| 86 | + pub fn new_p2wpkh( |
| 87 | + txout: TxOut, |
| 88 | + outpoint: OutPoint, |
| 89 | + sequence: Option<Sequence>, |
| 90 | + ) -> Result<Self, PsbtInputError> { |
| 91 | + if !txout.script_pubkey.is_p2wpkh() { |
| 92 | + return Err(InternalPsbtInputError::InvalidP2wpkhScriptPubkey.into()); |
| 93 | + } |
| 94 | + |
| 95 | + Self::new_segwit_input_pair(txout, outpoint, sequence) |
| 96 | + } |
| 97 | + |
| 98 | + /// Constructs a new ['InputPair'] for spending a native SegWit P2WSH output |
| 99 | + pub fn new_p2wsh( |
| 100 | + txout: TxOut, |
| 101 | + outpoint: OutPoint, |
| 102 | + sequence: Option<Sequence>, |
| 103 | + ) -> Result<Self, PsbtInputError> { |
| 104 | + if !txout.script_pubkey.is_p2wsh() { |
| 105 | + return Err(InternalPsbtInputError::InvalidP2wshScriptPubkey.into()); |
| 106 | + } |
| 107 | + |
| 108 | + Self::new_segwit_input_pair(txout, outpoint, sequence) |
| 109 | + } |
| 110 | + |
| 111 | + /// Constructs a new ['InputPair'] for spending a native SegWit P2TR output |
| 112 | + pub fn new_p2tr( |
| 113 | + txout: TxOut, |
| 114 | + outpoint: OutPoint, |
| 115 | + sequence: Option<Sequence>, |
| 116 | + ) -> Result<Self, PsbtInputError> { |
| 117 | + if !txout.script_pubkey.is_p2tr() { |
| 118 | + return Err(InternalPsbtInputError::InvalidP2trScriptPubkey.into()); |
| 119 | + } |
| 120 | + |
| 121 | + Self::new_segwit_input_pair(txout, outpoint, sequence) |
| 122 | + } |
| 123 | + |
61 | 124 | pub(crate) fn previous_txout(&self) -> TxOut { |
62 | 125 | InternalInputPair::from(self) |
63 | 126 | .previous_txout() |
@@ -88,3 +151,91 @@ pub(crate) fn parse_payload( |
88 | 151 |
|
89 | 152 | Ok((psbt, params)) |
90 | 153 | } |
| 154 | + |
| 155 | +#[cfg(test)] |
| 156 | +mod tests { |
| 157 | + use bitcoin::hashes::Hash; |
| 158 | + use bitcoin::key::{PublicKey, WPubkeyHash}; |
| 159 | + use bitcoin::secp256k1::Secp256k1; |
| 160 | + use bitcoin::{Amount, ScriptBuf, ScriptHash, Txid, WScriptHash, XOnlyPublicKey}; |
| 161 | + use payjoin_test_utils::{DUMMY20, DUMMY32}; |
| 162 | + |
| 163 | + use super::*; |
| 164 | + use crate::psbt::InternalPsbtInputError::{ |
| 165 | + InvalidP2trScriptPubkey, InvalidP2wpkhScriptPubkey, InvalidP2wshScriptPubkey, |
| 166 | + }; |
| 167 | + |
| 168 | + #[test] |
| 169 | + fn create_p2wpkh_input_pair() { |
| 170 | + let outpoint = OutPoint { txid: Txid::from_byte_array(DUMMY32), vout: 31 }; |
| 171 | + let sequence = Sequence::from_512_second_intervals(123); |
| 172 | + let p2wpkh_txout = TxOut { |
| 173 | + value: Amount::from_sat(12345), |
| 174 | + script_pubkey: ScriptBuf::new_p2wpkh(&WPubkeyHash::from_byte_array(DUMMY20)), |
| 175 | + }; |
| 176 | + let p2wpkh_pair = |
| 177 | + InputPair::new_p2wpkh(p2wpkh_txout.clone(), outpoint, Some(sequence)).unwrap(); |
| 178 | + assert_eq!(p2wpkh_pair.txin.previous_output, outpoint); |
| 179 | + assert_eq!(p2wpkh_pair.txin.sequence, sequence); |
| 180 | + assert_eq!(p2wpkh_pair.psbtin.witness_utxo.unwrap(), p2wpkh_txout); |
| 181 | + |
| 182 | + let p2sh_txout = TxOut { |
| 183 | + value: Default::default(), |
| 184 | + script_pubkey: ScriptBuf::new_p2sh(&ScriptHash::all_zeros()), |
| 185 | + }; |
| 186 | + assert_eq!( |
| 187 | + InputPair::new_p2wpkh(p2sh_txout, outpoint, Some(sequence)).err().unwrap(), |
| 188 | + PsbtInputError::from(InvalidP2wpkhScriptPubkey) |
| 189 | + ) |
| 190 | + } |
| 191 | + |
| 192 | + #[test] |
| 193 | + fn create_p2wsh_input_pair() { |
| 194 | + let outpoint = OutPoint { txid: Txid::from_byte_array(DUMMY32), vout: 31 }; |
| 195 | + let sequence = Sequence::from_512_second_intervals(123); |
| 196 | + let p2wsh_txout = TxOut { |
| 197 | + value: Amount::from_sat(12345), |
| 198 | + script_pubkey: ScriptBuf::new_p2wsh(&WScriptHash::from_byte_array(DUMMY32)), |
| 199 | + }; |
| 200 | + let p2wsh_pair = |
| 201 | + InputPair::new_p2wsh(p2wsh_txout.clone(), outpoint, Some(sequence)).unwrap(); |
| 202 | + assert_eq!(p2wsh_pair.txin.previous_output, outpoint); |
| 203 | + assert_eq!(p2wsh_pair.txin.sequence, sequence); |
| 204 | + assert_eq!(p2wsh_pair.psbtin.witness_utxo.unwrap(), p2wsh_txout); |
| 205 | + |
| 206 | + let p2sh_txout = TxOut { |
| 207 | + value: Default::default(), |
| 208 | + script_pubkey: ScriptBuf::new_p2sh(&ScriptHash::all_zeros()), |
| 209 | + }; |
| 210 | + assert_eq!( |
| 211 | + InputPair::new_p2wsh(p2sh_txout, outpoint, Some(sequence)).err().unwrap(), |
| 212 | + PsbtInputError::from(InvalidP2wshScriptPubkey) |
| 213 | + ) |
| 214 | + } |
| 215 | + |
| 216 | + #[test] |
| 217 | + fn create_p2tr_input_pair() { |
| 218 | + let outpoint = OutPoint { txid: Txid::from_byte_array(DUMMY32), vout: 31 }; |
| 219 | + let sequence = Sequence::from_512_second_intervals(123); |
| 220 | + let pubkey_string = "0347ff3dacd07a1f43805ec6808e801505a6e18245178609972a68afbc2777ff2b"; |
| 221 | + let pubkey = pubkey_string.parse::<PublicKey>().expect("valid pubkey"); |
| 222 | + let xonly_pubkey = XOnlyPublicKey::from(pubkey.inner); |
| 223 | + let p2tr_txout = TxOut { |
| 224 | + value: Amount::from_sat(12345), |
| 225 | + script_pubkey: ScriptBuf::new_p2tr(&Secp256k1::new(), xonly_pubkey, None), |
| 226 | + }; |
| 227 | + let p2tr_pair = InputPair::new_p2tr(p2tr_txout.clone(), outpoint, Some(sequence)).unwrap(); |
| 228 | + assert_eq!(p2tr_pair.txin.previous_output, outpoint); |
| 229 | + assert_eq!(p2tr_pair.txin.sequence, sequence); |
| 230 | + assert_eq!(p2tr_pair.psbtin.witness_utxo.unwrap(), p2tr_txout); |
| 231 | + |
| 232 | + let p2sh_txout = TxOut { |
| 233 | + value: Default::default(), |
| 234 | + script_pubkey: ScriptBuf::new_p2sh(&ScriptHash::all_zeros()), |
| 235 | + }; |
| 236 | + assert_eq!( |
| 237 | + InputPair::new_p2tr(p2sh_txout, outpoint, Some(sequence)).err().unwrap(), |
| 238 | + PsbtInputError::from(InvalidP2trScriptPubkey) |
| 239 | + ) |
| 240 | + } |
| 241 | +} |
0 commit comments