Skip to content

Commit

Permalink
Remove input_type and sequence from Context
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
spacebear21 committed Oct 7, 2024
1 parent ade8ac3 commit ddcaa05
Showing 1 changed file with 8 additions and 26 deletions.
34 changes: 8 additions & 26 deletions payjoin/src/send/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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(),
Expand All @@ -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,
Expand All @@ -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,
},
))
Expand Down Expand Up @@ -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()?),
Expand Down Expand Up @@ -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,
}

Expand Down Expand Up @@ -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()
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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(
<hpke::kem::SecpK256HkdfSha256 as hpke::Kem>::PrivateKey::from_bytes(&[0x01; 32])
Expand Down

0 comments on commit ddcaa05

Please sign in to comment.