Skip to content

Commit 4ac1504

Browse files
Reduce visibility of BlindedMessagePath inner path.
Works towards making the inner BlindedPath struct pub(super).
1 parent d00942b commit 4ac1504

File tree

8 files changed

+161
-158
lines changed

8 files changed

+161
-158
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use core::ops::Deref;
3535
/// A [`BlindedPath`] to be used for sending or receiving a message, hiding the identity of the
3636
/// recipient.
3737
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
38-
pub struct BlindedMessagePath(pub BlindedPath);
38+
pub struct BlindedMessagePath(pub(super) BlindedPath);
3939

4040
impl Writeable for BlindedMessagePath {
4141
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
@@ -135,6 +135,26 @@ impl BlindedMessagePath {
135135
pub fn blinded_hops(&self) -> &Vec<BlindedHop> {
136136
&self.0.blinded_hops
137137
}
138+
139+
pub(crate) fn set_introduction_node_id(&mut self, node_id: PublicKey) {
140+
self.0.introduction_node = IntroductionNode::NodeId(node_id);
141+
}
142+
143+
#[cfg(test)]
144+
pub fn from_raw(
145+
introduction_node_id: PublicKey, blinding_point: PublicKey, blinded_hops: Vec<BlindedHop>
146+
) -> Self {
147+
Self(BlindedPath {
148+
introduction_node: IntroductionNode::NodeId(introduction_node_id),
149+
blinding_point,
150+
blinded_hops,
151+
})
152+
}
153+
154+
#[cfg(test)]
155+
pub fn clear_blinded_hops(&mut self) {
156+
self.0.blinded_hops.clear()
157+
}
138158
}
139159

140160
/// An intermediate node, and possibly a short channel id leading to the next node.

lightning/src/ln/offers_tests.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ fn creates_short_lived_offer() {
389389
for path in offer.paths() {
390390
let introduction_node_id = resolve_introduction_node(bob, &path);
391391
assert_eq!(introduction_node_id, alice_id);
392-
assert!(matches!(path.0.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
392+
assert!(matches!(path.introduction_node(), &IntroductionNode::DirectedShortChannelId(..)));
393393
}
394394
}
395395

@@ -415,7 +415,7 @@ fn creates_long_lived_offer() {
415415
assert_eq!(offer.absolute_expiry(), Some(absolute_expiry));
416416
assert!(!offer.paths().is_empty());
417417
for path in offer.paths() {
418-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(alice_id));
418+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(alice_id));
419419
}
420420

421421
let offer = alice.node
@@ -424,7 +424,7 @@ fn creates_long_lived_offer() {
424424
assert_eq!(offer.absolute_expiry(), None);
425425
assert!(!offer.paths().is_empty());
426426
for path in offer.paths() {
427-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(alice_id));
427+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(alice_id));
428428
}
429429
}
430430

@@ -453,7 +453,7 @@ fn creates_short_lived_refund() {
453453
for path in refund.paths() {
454454
let introduction_node_id = resolve_introduction_node(alice, &path);
455455
assert_eq!(introduction_node_id, bob_id);
456-
assert!(matches!(path.0.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
456+
assert!(matches!(path.introduction_node(), &IntroductionNode::DirectedShortChannelId(..)));
457457
}
458458
}
459459

@@ -480,7 +480,7 @@ fn creates_long_lived_refund() {
480480
assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
481481
assert!(!refund.paths().is_empty());
482482
for path in refund.paths() {
483-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(bob_id));
483+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(bob_id));
484484
}
485485
}
486486

@@ -530,7 +530,7 @@ fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
530530
assert_ne!(offer.signing_pubkey(), Some(alice_id));
531531
assert!(!offer.paths().is_empty());
532532
for path in offer.paths() {
533-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(bob_id));
533+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(bob_id));
534534
}
535535

536536
let payment_id = PaymentId([1; 32]);
@@ -559,7 +559,7 @@ fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
559559
});
560560
assert_eq!(invoice_request.amount_msats(), None);
561561
assert_ne!(invoice_request.payer_id(), david_id);
562-
assert_eq!(reply_path.0.introduction_node, IntroductionNode::NodeId(charlie_id));
562+
assert_eq!(reply_path.introduction_node(), &IntroductionNode::NodeId(charlie_id));
563563

564564
let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
565565
charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
@@ -631,7 +631,7 @@ fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
631631
assert_ne!(refund.payer_id(), david_id);
632632
assert!(!refund.paths().is_empty());
633633
for path in refund.paths() {
634-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(charlie_id));
634+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(charlie_id));
635635
}
636636
expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
637637

@@ -687,7 +687,7 @@ fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
687687
assert_ne!(offer.signing_pubkey(), Some(alice_id));
688688
assert!(!offer.paths().is_empty());
689689
for path in offer.paths() {
690-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(alice_id));
690+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(alice_id));
691691
}
692692

693693
let payment_id = PaymentId([1; 32]);
@@ -708,7 +708,7 @@ fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
708708
});
709709
assert_eq!(invoice_request.amount_msats(), None);
710710
assert_ne!(invoice_request.payer_id(), bob_id);
711-
assert_eq!(reply_path.0.introduction_node, IntroductionNode::NodeId(bob_id));
711+
assert_eq!(reply_path.introduction_node(), &IntroductionNode::NodeId(bob_id));
712712

713713
let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
714714
bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
@@ -756,7 +756,7 @@ fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
756756
assert_ne!(refund.payer_id(), bob_id);
757757
assert!(!refund.paths().is_empty());
758758
for path in refund.paths() {
759-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(bob_id));
759+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(bob_id));
760760
}
761761
expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
762762

@@ -931,7 +931,7 @@ fn send_invoice_requests_with_distinct_reply_path() {
931931
assert_ne!(offer.signing_pubkey(), Some(alice_id));
932932
assert!(!offer.paths().is_empty());
933933
for path in offer.paths() {
934-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(bob_id));
934+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(bob_id));
935935
}
936936

937937
let payment_id = PaymentId([1; 32]);
@@ -950,7 +950,7 @@ fn send_invoice_requests_with_distinct_reply_path() {
950950
alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
951951

952952
let (_, reply_path) = extract_invoice_request(alice, &onion_message);
953-
assert_eq!(reply_path.0.introduction_node, IntroductionNode::NodeId(charlie_id));
953+
assert_eq!(reply_path.introduction_node(), &IntroductionNode::NodeId(charlie_id));
954954

955955
// Send, extract and verify the second Invoice Request message
956956
let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
@@ -960,7 +960,7 @@ fn send_invoice_requests_with_distinct_reply_path() {
960960
alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
961961

962962
let (_, reply_path) = extract_invoice_request(alice, &onion_message);
963-
assert_eq!(reply_path.0.introduction_node, IntroductionNode::NodeId(nodes[6].node.get_our_node_id()));
963+
assert_eq!(reply_path.introduction_node(), &IntroductionNode::NodeId(nodes[6].node.get_our_node_id()));
964964
}
965965

966966
/// This test checks that when multiple potential introduction nodes are available for the payee,
@@ -1015,7 +1015,7 @@ fn send_invoice_for_refund_with_distinct_reply_path() {
10151015
.build().unwrap();
10161016
assert_ne!(refund.payer_id(), alice_id);
10171017
for path in refund.paths() {
1018-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(bob_id));
1018+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(bob_id));
10191019
}
10201020
expect_recent_payment!(alice, RecentPaymentDetails::AwaitingInvoice, payment_id);
10211021

@@ -1031,7 +1031,7 @@ fn send_invoice_for_refund_with_distinct_reply_path() {
10311031
let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
10321032

10331033
let (_, reply_path) = extract_invoice(alice, &onion_message);
1034-
assert_eq!(reply_path.unwrap().0.introduction_node, IntroductionNode::NodeId(charlie_id));
1034+
assert_eq!(reply_path.unwrap().introduction_node(), &IntroductionNode::NodeId(charlie_id));
10351035

10361036
// Send, extract and verify the second Invoice Request message
10371037
let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
@@ -1040,7 +1040,7 @@ fn send_invoice_for_refund_with_distinct_reply_path() {
10401040
let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
10411041

10421042
let (_, reply_path) = extract_invoice(alice, &onion_message);
1043-
assert_eq!(reply_path.unwrap().0.introduction_node, IntroductionNode::NodeId(nodes[6].node.get_our_node_id()));
1043+
assert_eq!(reply_path.unwrap().introduction_node(), &IntroductionNode::NodeId(nodes[6].node.get_our_node_id()));
10441044
}
10451045

10461046
/// Checks that a deferred invoice can be paid asynchronously from an Event::InvoiceReceived.
@@ -1152,7 +1152,7 @@ fn creates_offer_with_blinded_path_using_unannounced_introduction_node() {
11521152
assert_ne!(offer.signing_pubkey(), Some(alice_id));
11531153
assert!(!offer.paths().is_empty());
11541154
for path in offer.paths() {
1155-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(bob_id));
1155+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(bob_id));
11561156
}
11571157

11581158
let payment_id = PaymentId([1; 32]);
@@ -1172,7 +1172,7 @@ fn creates_offer_with_blinded_path_using_unannounced_introduction_node() {
11721172
},
11731173
});
11741174
assert_ne!(invoice_request.payer_id(), bob_id);
1175-
assert_eq!(reply_path.0.introduction_node, IntroductionNode::NodeId(alice_id));
1175+
assert_eq!(reply_path.introduction_node(), &IntroductionNode::NodeId(alice_id));
11761176

11771177
let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
11781178
bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
@@ -1218,7 +1218,7 @@ fn creates_refund_with_blinded_path_using_unannounced_introduction_node() {
12181218
assert_ne!(refund.payer_id(), bob_id);
12191219
assert!(!refund.paths().is_empty());
12201220
for path in refund.paths() {
1221-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(alice_id));
1221+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(alice_id));
12221222
}
12231223
expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
12241224

@@ -1282,7 +1282,7 @@ fn fails_authentication_when_handling_invoice_request() {
12821282
assert_ne!(offer.signing_pubkey(), Some(alice_id));
12831283
assert!(!offer.paths().is_empty());
12841284
for path in offer.paths() {
1285-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(bob_id));
1285+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(bob_id));
12861286
}
12871287

12881288
let invalid_path = alice.node
@@ -1291,7 +1291,7 @@ fn fails_authentication_when_handling_invoice_request() {
12911291
.build().unwrap()
12921292
.paths().first().unwrap()
12931293
.clone();
1294-
assert_eq!(invalid_path.0.introduction_node, IntroductionNode::NodeId(bob_id));
1294+
assert_eq!(invalid_path.introduction_node(), &IntroductionNode::NodeId(bob_id));
12951295

12961296
// Send the invoice request directly to Alice instead of using a blinded path.
12971297
let payment_id = PaymentId([1; 32]);
@@ -1315,7 +1315,7 @@ fn fails_authentication_when_handling_invoice_request() {
13151315
let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
13161316
assert_eq!(invoice_request.amount_msats(), None);
13171317
assert_ne!(invoice_request.payer_id(), david_id);
1318-
assert_eq!(reply_path.0.introduction_node, IntroductionNode::NodeId(charlie_id));
1318+
assert_eq!(reply_path.introduction_node(), &IntroductionNode::NodeId(charlie_id));
13191319

13201320
assert_eq!(alice.onion_messenger.next_onion_message_for_peer(charlie_id), None);
13211321

@@ -1348,7 +1348,7 @@ fn fails_authentication_when_handling_invoice_request() {
13481348
let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
13491349
assert_eq!(invoice_request.amount_msats(), None);
13501350
assert_ne!(invoice_request.payer_id(), david_id);
1351-
assert_eq!(reply_path.0.introduction_node, IntroductionNode::NodeId(charlie_id));
1351+
assert_eq!(reply_path.introduction_node(), &IntroductionNode::NodeId(charlie_id));
13521352

13531353
assert_eq!(alice.onion_messenger.next_onion_message_for_peer(charlie_id), None);
13541354
}
@@ -1399,7 +1399,7 @@ fn fails_authentication_when_handling_invoice_for_offer() {
13991399
assert_ne!(offer.signing_pubkey(), Some(alice_id));
14001400
assert!(!offer.paths().is_empty());
14011401
for path in offer.paths() {
1402-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(bob_id));
1402+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(bob_id));
14031403
}
14041404

14051405
// Initiate an invoice request, but abandon tracking it.
@@ -1453,7 +1453,7 @@ fn fails_authentication_when_handling_invoice_for_offer() {
14531453
let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
14541454
assert_eq!(invoice_request.amount_msats(), None);
14551455
assert_ne!(invoice_request.payer_id(), david_id);
1456-
assert_eq!(reply_path.0.introduction_node, IntroductionNode::NodeId(charlie_id));
1456+
assert_eq!(reply_path.introduction_node(), &IntroductionNode::NodeId(charlie_id));
14571457

14581458
let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
14591459
charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
@@ -1510,7 +1510,7 @@ fn fails_authentication_when_handling_invoice_for_refund() {
15101510
assert_ne!(refund.payer_id(), david_id);
15111511
assert!(!refund.paths().is_empty());
15121512
for path in refund.paths() {
1513-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(charlie_id));
1513+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(charlie_id));
15141514
}
15151515
expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
15161516

@@ -1547,7 +1547,7 @@ fn fails_authentication_when_handling_invoice_for_refund() {
15471547
assert_ne!(refund.payer_id(), david_id);
15481548
assert!(!refund.paths().is_empty());
15491549
for path in refund.paths() {
1550-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(charlie_id));
1550+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(charlie_id));
15511551
}
15521552

15531553
let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();

lightning/src/offers/invoice.rs

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,7 @@ pub(super) fn check_invoice_signing_pubkey(
14081408
(None, Some(paths)) => {
14091409
if !paths
14101410
.iter()
1411-
.filter_map(|path| path.0.blinded_hops.last())
1411+
.filter_map(|path| path.blinded_hops().last())
14121412
.any(|last_hop| invoice_signing_pubkey == &last_hop.blinded_node_id)
14131413
{
14141414
return Err(Bolt12SemanticError::InvalidSigningPubkey);
@@ -1434,7 +1434,7 @@ mod tests {
14341434

14351435
use core::time::Duration;
14361436

1437-
use crate::blinded_path::{BlindedHop, BlindedPath, IntroductionNode};
1437+
use crate::blinded_path::BlindedHop;
14381438
use crate::blinded_path::message::BlindedMessagePath;
14391439
use crate::sign::KeyMaterial;
14401440
use crate::ln::features::{Bolt12InvoiceFeatures, InvoiceRequestFeatures, OfferFeatures};
@@ -1784,14 +1784,13 @@ mod tests {
17841784
let nonce = Nonce::from_entropy_source(&entropy);
17851785
let secp_ctx = Secp256k1::new();
17861786

1787-
let blinded_path = BlindedMessagePath(BlindedPath {
1788-
introduction_node: IntroductionNode::NodeId(pubkey(40)),
1789-
blinding_point: pubkey(41),
1790-
blinded_hops: vec![
1787+
let blinded_path = BlindedMessagePath::from_raw(
1788+
pubkey(40), pubkey(41),
1789+
vec![
17911790
BlindedHop { blinded_node_id: pubkey(42), encrypted_payload: vec![0; 43] },
17921791
BlindedHop { blinded_node_id: node_id, encrypted_payload: vec![0; 44] },
1793-
],
1794-
});
1792+
]
1793+
);
17951794

17961795
#[cfg(c_bindings)]
17971796
use crate::offers::offer::OfferWithDerivedMetadataBuilder as OfferBuilder;
@@ -1860,14 +1859,13 @@ mod tests {
18601859
let entropy = FixedEntropy {};
18611860
let secp_ctx = Secp256k1::new();
18621861

1863-
let blinded_path = BlindedMessagePath(BlindedPath {
1864-
introduction_node: IntroductionNode::NodeId(pubkey(40)),
1865-
blinding_point: pubkey(41),
1866-
blinded_hops: vec![
1862+
let blinded_path = BlindedMessagePath::from_raw(
1863+
pubkey(40), pubkey(41),
1864+
vec![
18671865
BlindedHop { blinded_node_id: pubkey(42), encrypted_payload: vec![0; 43] },
18681866
BlindedHop { blinded_node_id: node_id, encrypted_payload: vec![0; 44] },
1869-
],
1870-
});
1867+
]
1868+
);
18711869

18721870
let refund = RefundBuilder::new(vec![1; 32], payer_pubkey(), 1000).unwrap()
18731871
.path(blinded_path)
@@ -2364,22 +2362,20 @@ mod tests {
23642362
#[test]
23652363
fn parses_invoice_with_node_id_from_blinded_path() {
23662364
let paths = vec![
2367-
BlindedMessagePath(BlindedPath {
2368-
introduction_node: IntroductionNode::NodeId(pubkey(40)),
2369-
blinding_point: pubkey(41),
2370-
blinded_hops: vec![
2365+
BlindedMessagePath::from_raw(
2366+
pubkey(40), pubkey(41),
2367+
vec![
23712368
BlindedHop { blinded_node_id: pubkey(43), encrypted_payload: vec![0; 43] },
23722369
BlindedHop { blinded_node_id: pubkey(44), encrypted_payload: vec![0; 44] },
2373-
],
2374-
}),
2375-
BlindedMessagePath(BlindedPath {
2376-
introduction_node: IntroductionNode::NodeId(pubkey(40)),
2377-
blinding_point: pubkey(41),
2378-
blinded_hops: vec![
2370+
]
2371+
),
2372+
BlindedMessagePath::from_raw(
2373+
pubkey(40), pubkey(41),
2374+
vec![
23792375
BlindedHop { blinded_node_id: pubkey(45), encrypted_payload: vec![0; 45] },
23802376
BlindedHop { blinded_node_id: pubkey(46), encrypted_payload: vec![0; 46] },
2381-
],
2382-
}),
2377+
]
2378+
),
23832379
];
23842380

23852381
let blinded_node_id_sign = |message: &UnsignedBolt12Invoice| {
@@ -2517,14 +2513,13 @@ mod tests {
25172513
.build().unwrap()
25182514
.sign(recipient_sign).unwrap();
25192515

2520-
let blinded_path = BlindedMessagePath(BlindedPath {
2521-
introduction_node: IntroductionNode::NodeId(pubkey(40)),
2522-
blinding_point: pubkey(41),
2523-
blinded_hops: vec![
2516+
let blinded_path = BlindedMessagePath::from_raw(
2517+
pubkey(40), pubkey(41),
2518+
vec![
25242519
BlindedHop { blinded_node_id: pubkey(42), encrypted_payload: vec![0; 43] },
25252520
BlindedHop { blinded_node_id: pubkey(43), encrypted_payload: vec![0; 44] },
2526-
],
2527-
});
2521+
]
2522+
);
25282523

25292524
let mut tlv_stream = invoice.as_tlv_stream();
25302525
let message_paths = vec![blinded_path];

0 commit comments

Comments
 (0)