Skip to content

Commit 896e9d0

Browse files
Use crate::Version instead of string
The Version type exists for this less-prone-to-error purpose.
1 parent c2aaf86 commit 896e9d0

4 files changed

Lines changed: 17 additions & 23 deletions

File tree

payjoin/src/core/send/mod.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use url::Url;
2626

2727
use crate::output_substitution::OutputSubstitution;
2828
use crate::psbt::PsbtExt;
29+
use crate::Version;
2930

3031
// See usize casts
3132
#[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
@@ -457,10 +458,10 @@ fn serialize_url(
457458
output_substitution: OutputSubstitution,
458459
fee_contribution: Option<AdditionalFeeContribution>,
459460
min_fee_rate: FeeRate,
460-
version: &str,
461+
version: Version,
461462
) -> Url {
462463
let mut url = endpoint;
463-
url.query_pairs_mut().append_pair("v", version);
464+
url.query_pairs_mut().append_pair("v", &version.to_string());
464465
if output_substitution == OutputSubstitution::Disabled {
465466
url.query_pairs_mut().append_pair("disableoutputsubstitution", "true");
466467
}
@@ -485,7 +486,6 @@ mod test {
485486
use bitcoin::ecdsa::Signature;
486487
use bitcoin::hex::FromHex;
487488
use bitcoin::secp256k1::{Message, PublicKey, Secp256k1, SecretKey};
488-
use bitcoin::transaction::Version;
489489
use bitcoin::{
490490
Amount, FeeRate, OutPoint, Script, ScriptBuf, Sequence, Witness, XOnlyPublicKey,
491491
};
@@ -495,9 +495,7 @@ mod test {
495495
};
496496
use url::Url;
497497

498-
use super::{
499-
check_single_payee, clear_unneeded_fields, determine_fee_contribution, serialize_url,
500-
};
498+
use super::*;
501499
use crate::output_substitution::OutputSubstitution;
502500
use crate::psbt::PsbtExt;
503501
use crate::send::{AdditionalFeeContribution, InternalBuildSenderError, InternalProposalError};
@@ -799,7 +797,7 @@ mod test {
799797
OutputSubstitution::Disabled,
800798
None,
801799
FeeRate::ZERO,
802-
"2",
800+
Version::Two,
803801
);
804802
assert_eq!(url, Url::parse("http://localhost?v=2&disableoutputsubstitution=true")?);
805803

@@ -808,7 +806,7 @@ mod test {
808806
OutputSubstitution::Enabled,
809807
None,
810808
FeeRate::ZERO,
811-
"2",
809+
Version::Two,
812810
);
813811
assert_eq!(url, Url::parse("http://localhost?v=2")?);
814812
Ok(())
@@ -821,7 +819,7 @@ mod test {
821819
OutputSubstitution::Enabled,
822820
None,
823821
FeeRate::from_sat_per_vb(10).expect("Could not parse feerate"),
824-
"2",
822+
Version::Two,
825823
);
826824
assert_eq!(url, Url::parse("http://localhost?v=2&minfeerate=10")?);
827825
Ok(())
@@ -834,7 +832,7 @@ mod test {
834832
OutputSubstitution::Enabled,
835833
Some(AdditionalFeeContribution { max_amount: Amount::from_sat(1000), vout: 0 }),
836834
FeeRate::ZERO,
837-
"2",
835+
Version::Two,
838836
);
839837
assert_eq!(
840838
url,
@@ -856,7 +854,7 @@ mod test {
856854
let mut proposal: bitcoin::Psbt = PARSED_PAYJOIN_PROPOSAL.clone();
857855

858856
let original_version = ctx.original_psbt.unsigned_tx.version;
859-
let proposed_version = Version::non_standard(88);
857+
let proposed_version = bitcoin::transaction::Version::non_standard(88);
860858
proposal.unsigned_tx.version = proposed_version;
861859

862860
assert!(matches!(

payjoin/src/core/send/multiparty/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::output_substitution::OutputSubstitution;
1313
use crate::persist::NoopSessionPersister;
1414
use crate::send::v2::V2PostContext;
1515
use crate::uri::UrlExt;
16-
use crate::{ImplementationError, IntoUrl, PjUri, Request};
16+
use crate::{ImplementationError, IntoUrl, PjUri, Request, Version};
1717

1818
mod error;
1919

@@ -105,7 +105,7 @@ fn serialize_v2_body(
105105
output_substitution,
106106
fee_contribution,
107107
min_fee_rate,
108-
"2",
108+
Version::Two,
109109
);
110110
append_optimisitic_merge_query_param(&mut url);
111111
let base64 = psbt.to_string();
@@ -231,6 +231,7 @@ mod test {
231231
use payjoin_test_utils::BoxError;
232232
use url::Url;
233233

234+
use super::*;
234235
use crate::output_substitution::OutputSubstitution;
235236
use crate::send::multiparty::append_optimisitic_merge_query_param;
236237
use crate::send::serialize_url;
@@ -242,7 +243,7 @@ mod test {
242243
OutputSubstitution::Enabled,
243244
None,
244245
FeeRate::ZERO,
245-
"2",
246+
Version::Two,
246247
);
247248
append_optimisitic_merge_query_param(&mut url);
248249
assert_eq!(url, Url::parse("http://localhost?v=2&optimisticmerge=true")?);
@@ -252,7 +253,7 @@ mod test {
252253
OutputSubstitution::Enabled,
253254
None,
254255
FeeRate::ZERO,
255-
"2",
256+
Version::Two,
256257
);
257258
assert_eq!(url, Url::parse("http://localhost?v=2")?);
258259

payjoin/src/core/send/v1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ impl Sender {
236236
self.psbt_ctx.output_substitution,
237237
self.psbt_ctx.fee_contribution,
238238
self.psbt_ctx.min_fee_rate,
239-
"1", // payjoin version
239+
Version::One,
240240
);
241241
let body = self.psbt_ctx.original_psbt.to_string().as_bytes().to_vec();
242242
(

payjoin/src/core/send/v2/mod.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -353,13 +353,8 @@ pub(crate) fn serialize_v2_body(
353353
// Grug say localhost base be discarded anyway. no big brain needed.
354354
let base_url = Url::parse("http://localhost").expect("invalid URL");
355355

356-
let placeholder_url = serialize_url(
357-
base_url,
358-
output_substitution,
359-
fee_contribution,
360-
min_fee_rate,
361-
"2", // payjoin version
362-
);
356+
let placeholder_url =
357+
serialize_url(base_url, output_substitution, fee_contribution, min_fee_rate, Version::Two);
363358
let query_params = placeholder_url.query().unwrap_or_default();
364359
let base64 = psbt.to_string();
365360
Ok(format!("{base64}\n{query_params}").into_bytes())

0 commit comments

Comments
 (0)