Skip to content

Commit ddcaa05

Browse files
committed
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.
1 parent ade8ac3 commit ddcaa05

File tree

1 file changed

+8
-26
lines changed

1 file changed

+8
-26
lines changed

payjoin/src/send/mod.rs

+8-26
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use bitcoin::psbt::Psbt;
3131
use bitcoin::secp256k1::rand;
3232
#[cfg(feature = "v2")]
3333
use bitcoin::secp256k1::PublicKey;
34-
use bitcoin::{AddressType, Amount, FeeRate, Script, ScriptBuf, Sequence, TxOut, Weight};
34+
use bitcoin::{Amount, FeeRate, Script, ScriptBuf, TxOut, Weight};
3535
pub use error::{CreateRequestError, ResponseError, ValidationError};
3636
pub(crate) use error::{InternalCreateRequestError, InternalValidationError};
3737
#[cfg(feature = "v2")]
@@ -225,22 +225,12 @@ impl<'a> RequestBuilder<'a> {
225225
)?;
226226
clear_unneeded_fields(&mut psbt);
227227

228-
let zeroth_input = psbt.input_pairs().next().ok_or(InternalCreateRequestError::NoInputs)?;
229-
230-
let sequence = zeroth_input.txin.sequence;
231-
let input_type = zeroth_input
232-
.address_type()
233-
.map_err(InternalCreateRequestError::AddressType)?
234-
.to_string();
235-
236228
Ok(RequestContext {
237229
psbt,
238230
endpoint,
239231
disable_output_substitution,
240232
fee_contribution,
241233
payee,
242-
input_type,
243-
sequence,
244234
min_fee_rate: self.min_fee_rate,
245235
#[cfg(feature = "v2")]
246236
e: crate::v2::HpkeKeyPair::gen_keypair().secret_key().clone(),
@@ -256,8 +246,6 @@ pub struct RequestContext {
256246
disable_output_substitution: bool,
257247
fee_contribution: Option<(bitcoin::Amount, usize)>,
258248
min_fee_rate: FeeRate,
259-
input_type: String,
260-
sequence: Sequence,
261249
payee: ScriptBuf,
262250
#[cfg(feature = "v2")]
263251
e: crate::v2::HpkeSecretKey,
@@ -282,8 +270,6 @@ impl RequestContext {
282270
disable_output_substitution: self.disable_output_substitution,
283271
fee_contribution: self.fee_contribution,
284272
payee: self.payee.clone(),
285-
input_type: AddressType::from_str(&self.input_type).expect("Unknown address type"),
286-
sequence: self.sequence,
287273
min_fee_rate: self.min_fee_rate,
288274
},
289275
))
@@ -352,9 +338,6 @@ impl RequestContext {
352338
disable_output_substitution: self.disable_output_substitution,
353339
fee_contribution: self.fee_contribution,
354340
payee: self.payee.clone(),
355-
input_type: AddressType::from_str(&self.input_type)
356-
.expect("Unknown address type"),
357-
sequence: self.sequence,
358341
min_fee_rate: self.min_fee_rate,
359342
},
360343
rs: Some(self.extract_rs_pubkey()?),
@@ -397,8 +380,6 @@ pub struct ContextV1 {
397380
disable_output_substitution: bool,
398381
fee_contribution: Option<(bitcoin::Amount, usize)>,
399382
min_fee_rate: FeeRate,
400-
input_type: AddressType,
401-
sequence: Sequence,
402383
payee: ScriptBuf,
403384
}
404385

@@ -565,6 +546,11 @@ impl ContextV1 {
565546
}
566547
// theirs (receiver)
567548
None | Some(_) => {
549+
let original = self
550+
.original_psbt
551+
.input_pairs()
552+
.next()
553+
.expect("original PSBT should have an input");
568554
// Verify the PSBT input is finalized
569555
ensure!(
570556
proposed.psbtin.final_script_sig.is_some()
@@ -577,8 +563,8 @@ impl ContextV1 {
577563
|| proposed.psbtin.non_witness_utxo.is_some(),
578564
ReceiverTxinMissingUtxoInfo
579565
);
580-
ensure!(proposed.txin.sequence == self.sequence, MixedSequence);
581-
check_eq!(proposed.address_type()?, self.input_type, MixedInputTypes);
566+
ensure!(proposed.txin.sequence == original.txin.sequence, MixedSequence);
567+
check_eq!(proposed.address_type()?, original.address_type()?, MixedInputTypes);
582568
}
583569
}
584570
}
@@ -855,8 +841,6 @@ mod test {
855841
fee_contribution: Some((bitcoin::Amount::from_sat(182), 0)),
856842
min_fee_rate: FeeRate::ZERO,
857843
payee,
858-
input_type: bitcoin::AddressType::P2sh,
859-
sequence,
860844
};
861845
ctx
862846
}
@@ -911,8 +895,6 @@ mod test {
911895
disable_output_substitution: false,
912896
fee_contribution: None,
913897
min_fee_rate: FeeRate::ZERO,
914-
input_type: bitcoin::AddressType::P2sh.to_string(),
915-
sequence: Sequence::MAX,
916898
payee: ScriptBuf::from(vec![0x00]),
917899
e: HpkeSecretKey(
918900
<hpke::kem::SecpK256HkdfSha256 as hpke::Kem>::PrivateKey::from_bytes(&[0x01; 32])

0 commit comments

Comments
 (0)