From ddcaa05efefe3743c10dddbb6c3f9b00f7d0a17d Mon Sep 17 00:00:00 2001 From: spacebear Date: Mon, 7 Oct 2024 15:17:54 -0400 Subject: [PATCH] Remove input_type and sequence from Context Input type and sequence can be computed at little cost from the original PSBT, which is already included in RequestContext/ContextV1. Remove those fields from those structs and compute them when needed. --- payjoin/src/send/mod.rs | 34 ++++++++-------------------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/payjoin/src/send/mod.rs b/payjoin/src/send/mod.rs index ec5475d5..27a49889 100644 --- a/payjoin/src/send/mod.rs +++ b/payjoin/src/send/mod.rs @@ -31,7 +31,7 @@ use bitcoin::psbt::Psbt; use bitcoin::secp256k1::rand; #[cfg(feature = "v2")] use bitcoin::secp256k1::PublicKey; -use bitcoin::{AddressType, Amount, FeeRate, Script, ScriptBuf, Sequence, TxOut, Weight}; +use bitcoin::{Amount, FeeRate, Script, ScriptBuf, TxOut, Weight}; pub use error::{CreateRequestError, ResponseError, ValidationError}; pub(crate) use error::{InternalCreateRequestError, InternalValidationError}; #[cfg(feature = "v2")] @@ -225,22 +225,12 @@ impl<'a> RequestBuilder<'a> { )?; clear_unneeded_fields(&mut psbt); - let zeroth_input = psbt.input_pairs().next().ok_or(InternalCreateRequestError::NoInputs)?; - - let sequence = zeroth_input.txin.sequence; - let input_type = zeroth_input - .address_type() - .map_err(InternalCreateRequestError::AddressType)? - .to_string(); - Ok(RequestContext { psbt, endpoint, disable_output_substitution, fee_contribution, payee, - input_type, - sequence, min_fee_rate: self.min_fee_rate, #[cfg(feature = "v2")] e: crate::v2::HpkeKeyPair::gen_keypair().secret_key().clone(), @@ -256,8 +246,6 @@ pub struct RequestContext { disable_output_substitution: bool, fee_contribution: Option<(bitcoin::Amount, usize)>, min_fee_rate: FeeRate, - input_type: String, - sequence: Sequence, payee: ScriptBuf, #[cfg(feature = "v2")] e: crate::v2::HpkeSecretKey, @@ -282,8 +270,6 @@ impl RequestContext { disable_output_substitution: self.disable_output_substitution, fee_contribution: self.fee_contribution, payee: self.payee.clone(), - input_type: AddressType::from_str(&self.input_type).expect("Unknown address type"), - sequence: self.sequence, min_fee_rate: self.min_fee_rate, }, )) @@ -352,9 +338,6 @@ impl RequestContext { disable_output_substitution: self.disable_output_substitution, fee_contribution: self.fee_contribution, payee: self.payee.clone(), - input_type: AddressType::from_str(&self.input_type) - .expect("Unknown address type"), - sequence: self.sequence, min_fee_rate: self.min_fee_rate, }, rs: Some(self.extract_rs_pubkey()?), @@ -397,8 +380,6 @@ pub struct ContextV1 { disable_output_substitution: bool, fee_contribution: Option<(bitcoin::Amount, usize)>, min_fee_rate: FeeRate, - input_type: AddressType, - sequence: Sequence, payee: ScriptBuf, } @@ -565,6 +546,11 @@ impl ContextV1 { } // theirs (receiver) None | Some(_) => { + let original = self + .original_psbt + .input_pairs() + .next() + .expect("original PSBT should have an input"); // Verify the PSBT input is finalized ensure!( proposed.psbtin.final_script_sig.is_some() @@ -577,8 +563,8 @@ impl ContextV1 { || proposed.psbtin.non_witness_utxo.is_some(), ReceiverTxinMissingUtxoInfo ); - ensure!(proposed.txin.sequence == self.sequence, MixedSequence); - check_eq!(proposed.address_type()?, self.input_type, MixedInputTypes); + ensure!(proposed.txin.sequence == original.txin.sequence, MixedSequence); + check_eq!(proposed.address_type()?, original.address_type()?, MixedInputTypes); } } } @@ -855,8 +841,6 @@ mod test { fee_contribution: Some((bitcoin::Amount::from_sat(182), 0)), min_fee_rate: FeeRate::ZERO, payee, - input_type: bitcoin::AddressType::P2sh, - sequence, }; ctx } @@ -911,8 +895,6 @@ mod test { disable_output_substitution: false, fee_contribution: None, min_fee_rate: FeeRate::ZERO, - input_type: bitcoin::AddressType::P2sh.to_string(), - sequence: Sequence::MAX, payee: ScriptBuf::from(vec![0x00]), e: HpkeSecretKey( ::PrivateKey::from_bytes(&[0x01; 32])