Skip to content

Commit ba342de

Browse files
authored
Merge pull request #2120 from valentinewallace/2023-03-blinded-pathfinding
2 parents c5214c2 + 6c3ca55 commit ba342de

File tree

7 files changed

+595
-136
lines changed

7 files changed

+595
-136
lines changed

fuzz/src/invoice_request_deser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ fn build_response<'a, T: secp256k1::Signing + secp256k1::Verification>(
9797
},
9898
];
9999

100-
let payment_paths = paths.into_iter().zip(payinfo.into_iter()).collect();
100+
let payment_paths = payinfo.into_iter().zip(paths.into_iter()).collect();
101101
let payment_hash = PaymentHash([42; 32]);
102102
invoice_request.respond_with(payment_paths, payment_hash)?.build()
103103
}

fuzz/src/refund_deser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ fn build_response<'a, T: secp256k1::Signing + secp256k1::Verification>(
8686
},
8787
];
8888

89-
let payment_paths = paths.into_iter().zip(payinfo.into_iter()).collect();
89+
let payment_paths = payinfo.into_iter().zip(paths.into_iter()).collect();
9090
let payment_hash = PaymentHash([42; 32]);
9191
refund.respond_with(payment_paths, payment_hash, signing_pubkey)?.build()
9292
}

lightning/src/offers/invoice.rs

+27-27
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
//! # use lightning::offers::invoice::BlindedPayInfo;
3232
//! # use lightning::blinded_path::BlindedPath;
3333
//! #
34-
//! # fn create_payment_paths() -> Vec<(BlindedPath, BlindedPayInfo)> { unimplemented!() }
34+
//! # fn create_payment_paths() -> Vec<(BlindedPayInfo, BlindedPath)> { unimplemented!() }
3535
//! # fn create_payment_hash() -> PaymentHash { unimplemented!() }
3636
//! #
3737
//! # fn parse_invoice_request(bytes: Vec<u8>) -> Result<(), lightning::offers::parse::ParseError> {
@@ -166,7 +166,7 @@ impl SigningPubkeyStrategy for DerivedSigningPubkey {}
166166

167167
impl<'a> InvoiceBuilder<'a, ExplicitSigningPubkey> {
168168
pub(super) fn for_offer(
169-
invoice_request: &'a InvoiceRequest, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>,
169+
invoice_request: &'a InvoiceRequest, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>,
170170
created_at: Duration, payment_hash: PaymentHash
171171
) -> Result<Self, SemanticError> {
172172
let amount_msats = Self::check_amount_msats(invoice_request)?;
@@ -182,7 +182,7 @@ impl<'a> InvoiceBuilder<'a, ExplicitSigningPubkey> {
182182
}
183183

184184
pub(super) fn for_refund(
185-
refund: &'a Refund, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, created_at: Duration,
185+
refund: &'a Refund, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, created_at: Duration,
186186
payment_hash: PaymentHash, signing_pubkey: PublicKey
187187
) -> Result<Self, SemanticError> {
188188
let amount_msats = refund.amount_msats();
@@ -199,7 +199,7 @@ impl<'a> InvoiceBuilder<'a, ExplicitSigningPubkey> {
199199

200200
impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> {
201201
pub(super) fn for_offer_using_keys(
202-
invoice_request: &'a InvoiceRequest, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>,
202+
invoice_request: &'a InvoiceRequest, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>,
203203
created_at: Duration, payment_hash: PaymentHash, keys: KeyPair
204204
) -> Result<Self, SemanticError> {
205205
let amount_msats = Self::check_amount_msats(invoice_request)?;
@@ -215,7 +215,7 @@ impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> {
215215
}
216216

217217
pub(super) fn for_refund_using_keys(
218-
refund: &'a Refund, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, created_at: Duration,
218+
refund: &'a Refund, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, created_at: Duration,
219219
payment_hash: PaymentHash, keys: KeyPair,
220220
) -> Result<Self, SemanticError> {
221221
let amount_msats = refund.amount_msats();
@@ -247,7 +247,7 @@ impl<'a, S: SigningPubkeyStrategy> InvoiceBuilder<'a, S> {
247247
}
248248

249249
fn fields(
250-
payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, created_at: Duration,
250+
payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, created_at: Duration,
251251
payment_hash: PaymentHash, amount_msats: u64, signing_pubkey: PublicKey
252252
) -> InvoiceFields {
253253
InvoiceFields {
@@ -454,7 +454,7 @@ enum InvoiceContents {
454454
/// Invoice-specific fields for an `invoice` message.
455455
#[derive(Clone, Debug, PartialEq)]
456456
struct InvoiceFields {
457-
payment_paths: Vec<(BlindedPath, BlindedPayInfo)>,
457+
payment_paths: Vec<(BlindedPayInfo, BlindedPath)>,
458458
created_at: Duration,
459459
relative_expiry: Option<Duration>,
460460
payment_hash: PaymentHash,
@@ -476,7 +476,7 @@ impl Invoice {
476476
///
477477
/// Blinded paths provide recipient privacy by obfuscating its node id. Note, however, that this
478478
/// privacy is lost if a public node id is used for [`Invoice::signing_pubkey`].
479-
pub fn payment_paths(&self) -> &[(BlindedPath, BlindedPayInfo)] {
479+
pub fn payment_paths(&self) -> &[(BlindedPayInfo, BlindedPath)] {
480480
&self.contents.fields().payment_paths[..]
481481
}
482482

@@ -703,8 +703,8 @@ impl InvoiceFields {
703703
};
704704

705705
InvoiceTlvStreamRef {
706-
paths: Some(Iterable(self.payment_paths.iter().map(|(path, _)| path))),
707-
blindedpay: Some(Iterable(self.payment_paths.iter().map(|(_, payinfo)| payinfo))),
706+
paths: Some(Iterable(self.payment_paths.iter().map(|(_, path)| path))),
707+
blindedpay: Some(Iterable(self.payment_paths.iter().map(|(payinfo, _)| payinfo))),
708708
created_at: Some(self.created_at.as_secs()),
709709
relative_expiry: self.relative_expiry.map(|duration| duration.as_secs() as u32),
710710
payment_hash: Some(&self.payment_hash),
@@ -750,13 +750,13 @@ tlv_stream!(InvoiceTlvStream, InvoiceTlvStreamRef, 160..240, {
750750
});
751751

752752
type BlindedPathIter<'a> = core::iter::Map<
753-
core::slice::Iter<'a, (BlindedPath, BlindedPayInfo)>,
754-
for<'r> fn(&'r (BlindedPath, BlindedPayInfo)) -> &'r BlindedPath,
753+
core::slice::Iter<'a, (BlindedPayInfo, BlindedPath)>,
754+
for<'r> fn(&'r (BlindedPayInfo, BlindedPath)) -> &'r BlindedPath,
755755
>;
756756

757757
type BlindedPayInfoIter<'a> = core::iter::Map<
758-
core::slice::Iter<'a, (BlindedPath, BlindedPayInfo)>,
759-
for<'r> fn(&'r (BlindedPath, BlindedPayInfo)) -> &'r BlindedPayInfo,
758+
core::slice::Iter<'a, (BlindedPayInfo, BlindedPath)>,
759+
for<'r> fn(&'r (BlindedPayInfo, BlindedPath)) -> &'r BlindedPayInfo,
760760
>;
761761

762762
/// Information needed to route a payment across a [`BlindedPath`].
@@ -878,15 +878,15 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
878878
},
879879
) = tlv_stream;
880880

881-
let payment_paths = match (paths, blindedpay) {
882-
(None, _) => return Err(SemanticError::MissingPaths),
883-
(_, None) => return Err(SemanticError::InvalidPayInfo),
884-
(Some(paths), _) if paths.is_empty() => return Err(SemanticError::MissingPaths),
885-
(Some(paths), Some(blindedpay)) if paths.len() != blindedpay.len() => {
881+
let payment_paths = match (blindedpay, paths) {
882+
(_, None) => return Err(SemanticError::MissingPaths),
883+
(None, _) => return Err(SemanticError::InvalidPayInfo),
884+
(_, Some(paths)) if paths.is_empty() => return Err(SemanticError::MissingPaths),
885+
(Some(blindedpay), Some(paths)) if paths.len() != blindedpay.len() => {
886886
return Err(SemanticError::InvalidPayInfo);
887887
},
888-
(Some(paths), Some(blindedpay)) => {
889-
paths.into_iter().zip(blindedpay.into_iter()).collect::<Vec<_>>()
888+
(Some(blindedpay), Some(paths)) => {
889+
blindedpay.into_iter().zip(paths.into_iter()).collect::<Vec<_>>()
890890
},
891891
};
892892

@@ -1052,8 +1052,8 @@ mod tests {
10521052
payer_note: None,
10531053
},
10541054
InvoiceTlvStreamRef {
1055-
paths: Some(Iterable(payment_paths.iter().map(|(path, _)| path))),
1056-
blindedpay: Some(Iterable(payment_paths.iter().map(|(_, payinfo)| payinfo))),
1055+
paths: Some(Iterable(payment_paths.iter().map(|(_, path)| path))),
1056+
blindedpay: Some(Iterable(payment_paths.iter().map(|(payinfo, _)| payinfo))),
10571057
created_at: Some(now.as_secs()),
10581058
relative_expiry: None,
10591059
payment_hash: Some(&payment_hash),
@@ -1130,8 +1130,8 @@ mod tests {
11301130
payer_note: None,
11311131
},
11321132
InvoiceTlvStreamRef {
1133-
paths: Some(Iterable(payment_paths.iter().map(|(path, _)| path))),
1134-
blindedpay: Some(Iterable(payment_paths.iter().map(|(_, payinfo)| payinfo))),
1133+
paths: Some(Iterable(payment_paths.iter().map(|(_, path)| path))),
1134+
blindedpay: Some(Iterable(payment_paths.iter().map(|(payinfo, _)| payinfo))),
11351135
created_at: Some(now.as_secs()),
11361136
relative_expiry: None,
11371137
payment_hash: Some(&payment_hash),
@@ -1516,7 +1516,7 @@ mod tests {
15161516

15171517
let empty_payment_paths = vec![];
15181518
let mut tlv_stream = invoice.as_tlv_stream();
1519-
tlv_stream.3.paths = Some(Iterable(empty_payment_paths.iter().map(|(path, _)| path)));
1519+
tlv_stream.3.paths = Some(Iterable(empty_payment_paths.iter().map(|(_, path)| path)));
15201520

15211521
match Invoice::try_from(tlv_stream.to_bytes()) {
15221522
Ok(_) => panic!("expected error"),
@@ -1526,7 +1526,7 @@ mod tests {
15261526
let mut payment_paths = payment_paths();
15271527
payment_paths.pop();
15281528
let mut tlv_stream = invoice.as_tlv_stream();
1529-
tlv_stream.3.blindedpay = Some(Iterable(payment_paths.iter().map(|(_, payinfo)| payinfo)));
1529+
tlv_stream.3.blindedpay = Some(Iterable(payment_paths.iter().map(|(payinfo, _)| payinfo)));
15301530

15311531
match Invoice::try_from(tlv_stream.to_bytes()) {
15321532
Ok(_) => panic!("expected error"),

lightning/src/offers/invoice_request.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ impl InvoiceRequest {
480480
/// [`Duration`]: core::time::Duration
481481
#[cfg(feature = "std")]
482482
pub fn respond_with(
483-
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash
483+
&self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash
484484
) -> Result<InvoiceBuilder<ExplicitSigningPubkey>, SemanticError> {
485485
let created_at = std::time::SystemTime::now()
486486
.duration_since(std::time::SystemTime::UNIX_EPOCH)
@@ -509,7 +509,7 @@ impl InvoiceRequest {
509509
///
510510
/// [`Invoice::created_at`]: crate::offers::invoice::Invoice::created_at
511511
pub fn respond_with_no_std(
512-
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
512+
&self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
513513
created_at: core::time::Duration
514514
) -> Result<InvoiceBuilder<ExplicitSigningPubkey>, SemanticError> {
515515
if self.features().requires_unknown_bits() {
@@ -530,7 +530,7 @@ impl InvoiceRequest {
530530
/// [`Invoice`]: crate::offers::invoice::Invoice
531531
#[cfg(feature = "std")]
532532
pub fn verify_and_respond_using_derived_keys<T: secp256k1::Signing>(
533-
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
533+
&self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
534534
expanded_key: &ExpandedKey, secp_ctx: &Secp256k1<T>
535535
) -> Result<InvoiceBuilder<DerivedSigningPubkey>, SemanticError> {
536536
let created_at = std::time::SystemTime::now()
@@ -552,7 +552,7 @@ impl InvoiceRequest {
552552
///
553553
/// [`Invoice`]: crate::offers::invoice::Invoice
554554
pub fn verify_and_respond_using_derived_keys_no_std<T: secp256k1::Signing>(
555-
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
555+
&self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
556556
created_at: core::time::Duration, expanded_key: &ExpandedKey, secp_ctx: &Secp256k1<T>
557557
) -> Result<InvoiceBuilder<DerivedSigningPubkey>, SemanticError> {
558558
if self.features().requires_unknown_bits() {

lightning/src/offers/refund.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ impl Refund {
394394
/// [`Duration`]: core::time::Duration
395395
#[cfg(feature = "std")]
396396
pub fn respond_with(
397-
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
397+
&self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
398398
signing_pubkey: PublicKey,
399399
) -> Result<InvoiceBuilder<ExplicitSigningPubkey>, SemanticError> {
400400
let created_at = std::time::SystemTime::now()
@@ -427,7 +427,7 @@ impl Refund {
427427
///
428428
/// [`Invoice::created_at`]: crate::offers::invoice::Invoice::created_at
429429
pub fn respond_with_no_std(
430-
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
430+
&self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
431431
signing_pubkey: PublicKey, created_at: Duration
432432
) -> Result<InvoiceBuilder<ExplicitSigningPubkey>, SemanticError> {
433433
if self.features().requires_unknown_bits() {
@@ -447,7 +447,7 @@ impl Refund {
447447
/// [`Invoice`]: crate::offers::invoice::Invoice
448448
#[cfg(feature = "std")]
449449
pub fn respond_using_derived_keys<ES: Deref>(
450-
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
450+
&self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
451451
expanded_key: &ExpandedKey, entropy_source: ES
452452
) -> Result<InvoiceBuilder<DerivedSigningPubkey>, SemanticError>
453453
where
@@ -471,7 +471,7 @@ impl Refund {
471471
///
472472
/// [`Invoice`]: crate::offers::invoice::Invoice
473473
pub fn respond_using_derived_keys_no_std<ES: Deref>(
474-
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
474+
&self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
475475
created_at: core::time::Duration, expanded_key: &ExpandedKey, entropy_source: ES
476476
) -> Result<InvoiceBuilder<DerivedSigningPubkey>, SemanticError>
477477
where

lightning/src/offers/test_utils.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub(super) fn privkey(byte: u8) -> SecretKey {
5858
SecretKey::from_slice(&[byte; 32]).unwrap()
5959
}
6060

61-
pub(super) fn payment_paths() -> Vec<(BlindedPath, BlindedPayInfo)> {
61+
pub(super) fn payment_paths() -> Vec<(BlindedPayInfo, BlindedPath)> {
6262
let paths = vec![
6363
BlindedPath {
6464
introduction_node_id: pubkey(40),
@@ -97,7 +97,7 @@ pub(super) fn payment_paths() -> Vec<(BlindedPath, BlindedPayInfo)> {
9797
},
9898
];
9999

100-
paths.into_iter().zip(payinfo.into_iter()).collect()
100+
payinfo.into_iter().zip(paths.into_iter()).collect()
101101
}
102102

103103
pub(super) fn payment_hash() -> PaymentHash {

0 commit comments

Comments
 (0)