Skip to content

Commit 06a7bb0

Browse files
Reduce visibility of BlindedMessagePath inner path.
Works towards making the inner BlindedPath struct private to the module.
1 parent 6f0190d commit 06a7bb0

File tree

8 files changed

+161
-157
lines changed

8 files changed

+161
-157
lines changed

lightning/src/blinded_path/message.rs

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

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

142162
/// 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
@@ -402,7 +402,7 @@ fn creates_short_lived_offer() {
402402
for path in offer.paths() {
403403
let introduction_node_id = resolve_introduction_node(bob, &path);
404404
assert_eq!(introduction_node_id, alice_id);
405-
assert!(matches!(path.0.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
405+
assert!(matches!(path.introduction_node(), &IntroductionNode::DirectedShortChannelId(..)));
406406
}
407407
}
408408

@@ -428,7 +428,7 @@ fn creates_long_lived_offer() {
428428
assert_eq!(offer.absolute_expiry(), Some(absolute_expiry));
429429
assert!(!offer.paths().is_empty());
430430
for path in offer.paths() {
431-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(alice_id));
431+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(alice_id));
432432
}
433433

434434
let offer = alice.node
@@ -437,7 +437,7 @@ fn creates_long_lived_offer() {
437437
assert_eq!(offer.absolute_expiry(), None);
438438
assert!(!offer.paths().is_empty());
439439
for path in offer.paths() {
440-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(alice_id));
440+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(alice_id));
441441
}
442442
}
443443

@@ -466,7 +466,7 @@ fn creates_short_lived_refund() {
466466
for path in refund.paths() {
467467
let introduction_node_id = resolve_introduction_node(alice, &path);
468468
assert_eq!(introduction_node_id, bob_id);
469-
assert!(matches!(path.0.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
469+
assert!(matches!(path.introduction_node(), &IntroductionNode::DirectedShortChannelId(..)));
470470
}
471471
}
472472

@@ -493,7 +493,7 @@ fn creates_long_lived_refund() {
493493
assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
494494
assert!(!refund.paths().is_empty());
495495
for path in refund.paths() {
496-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(bob_id));
496+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(bob_id));
497497
}
498498
}
499499

@@ -543,7 +543,7 @@ fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
543543
assert_ne!(offer.signing_pubkey(), Some(alice_id));
544544
assert!(!offer.paths().is_empty());
545545
for path in offer.paths() {
546-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(bob_id));
546+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(bob_id));
547547
}
548548

549549
let payment_id = PaymentId([1; 32]);
@@ -572,7 +572,7 @@ fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
572572
});
573573
assert_eq!(invoice_request.amount_msats(), None);
574574
assert_ne!(invoice_request.payer_id(), david_id);
575-
assert_eq!(reply_path.0.introduction_node, IntroductionNode::NodeId(charlie_id));
575+
assert_eq!(reply_path.introduction_node(), &IntroductionNode::NodeId(charlie_id));
576576

577577
let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
578578
charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
@@ -644,7 +644,7 @@ fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
644644
assert_ne!(refund.payer_id(), david_id);
645645
assert!(!refund.paths().is_empty());
646646
for path in refund.paths() {
647-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(charlie_id));
647+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(charlie_id));
648648
}
649649
expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
650650

@@ -700,7 +700,7 @@ fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
700700
assert_ne!(offer.signing_pubkey(), Some(alice_id));
701701
assert!(!offer.paths().is_empty());
702702
for path in offer.paths() {
703-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(alice_id));
703+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(alice_id));
704704
}
705705

706706
let payment_id = PaymentId([1; 32]);
@@ -721,7 +721,7 @@ fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
721721
});
722722
assert_eq!(invoice_request.amount_msats(), None);
723723
assert_ne!(invoice_request.payer_id(), bob_id);
724-
assert_eq!(reply_path.0.introduction_node, IntroductionNode::NodeId(bob_id));
724+
assert_eq!(reply_path.introduction_node(), &IntroductionNode::NodeId(bob_id));
725725

726726
let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
727727
bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
@@ -769,7 +769,7 @@ fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
769769
assert_ne!(refund.payer_id(), bob_id);
770770
assert!(!refund.paths().is_empty());
771771
for path in refund.paths() {
772-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(bob_id));
772+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(bob_id));
773773
}
774774
expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
775775

@@ -944,7 +944,7 @@ fn send_invoice_requests_with_distinct_reply_path() {
944944
assert_ne!(offer.signing_pubkey(), Some(alice_id));
945945
assert!(!offer.paths().is_empty());
946946
for path in offer.paths() {
947-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(bob_id));
947+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(bob_id));
948948
}
949949

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

965965
let (_, reply_path) = extract_invoice_request(alice, &onion_message);
966-
assert_eq!(reply_path.0.introduction_node, IntroductionNode::NodeId(charlie_id));
966+
assert_eq!(reply_path.introduction_node(), &IntroductionNode::NodeId(charlie_id));
967967

968968
// Send, extract and verify the second Invoice Request message
969969
let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
@@ -973,7 +973,7 @@ fn send_invoice_requests_with_distinct_reply_path() {
973973
alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
974974

975975
let (_, reply_path) = extract_invoice_request(alice, &onion_message);
976-
assert_eq!(reply_path.0.introduction_node, IntroductionNode::NodeId(nodes[6].node.get_our_node_id()));
976+
assert_eq!(reply_path.introduction_node(), &IntroductionNode::NodeId(nodes[6].node.get_our_node_id()));
977977
}
978978

979979
/// This test checks that when multiple potential introduction nodes are available for the payee,
@@ -1028,7 +1028,7 @@ fn send_invoice_for_refund_with_distinct_reply_path() {
10281028
.build().unwrap();
10291029
assert_ne!(refund.payer_id(), alice_id);
10301030
for path in refund.paths() {
1031-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(bob_id));
1031+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(bob_id));
10321032
}
10331033
expect_recent_payment!(alice, RecentPaymentDetails::AwaitingInvoice, payment_id);
10341034

@@ -1044,7 +1044,7 @@ fn send_invoice_for_refund_with_distinct_reply_path() {
10441044
let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
10451045

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

10491049
// Send, extract and verify the second Invoice Request message
10501050
let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
@@ -1053,7 +1053,7 @@ fn send_invoice_for_refund_with_distinct_reply_path() {
10531053
let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
10541054

10551055
let (_, reply_path) = extract_invoice(alice, &onion_message);
1056-
assert_eq!(reply_path.unwrap().0.introduction_node, IntroductionNode::NodeId(nodes[6].node.get_our_node_id()));
1056+
assert_eq!(reply_path.unwrap().introduction_node(), &IntroductionNode::NodeId(nodes[6].node.get_our_node_id()));
10571057
}
10581058

10591059
/// Checks that a deferred invoice can be paid asynchronously from an Event::InvoiceReceived.
@@ -1165,7 +1165,7 @@ fn creates_offer_with_blinded_path_using_unannounced_introduction_node() {
11651165
assert_ne!(offer.signing_pubkey(), Some(alice_id));
11661166
assert!(!offer.paths().is_empty());
11671167
for path in offer.paths() {
1168-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(bob_id));
1168+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(bob_id));
11691169
}
11701170

11711171
let payment_id = PaymentId([1; 32]);
@@ -1185,7 +1185,7 @@ fn creates_offer_with_blinded_path_using_unannounced_introduction_node() {
11851185
},
11861186
});
11871187
assert_ne!(invoice_request.payer_id(), bob_id);
1188-
assert_eq!(reply_path.0.introduction_node, IntroductionNode::NodeId(alice_id));
1188+
assert_eq!(reply_path.introduction_node(), &IntroductionNode::NodeId(alice_id));
11891189

11901190
let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
11911191
bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
@@ -1231,7 +1231,7 @@ fn creates_refund_with_blinded_path_using_unannounced_introduction_node() {
12311231
assert_ne!(refund.payer_id(), bob_id);
12321232
assert!(!refund.paths().is_empty());
12331233
for path in refund.paths() {
1234-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(alice_id));
1234+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(alice_id));
12351235
}
12361236
expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
12371237

@@ -1295,7 +1295,7 @@ fn fails_authentication_when_handling_invoice_request() {
12951295
assert_ne!(offer.signing_pubkey(), Some(alice_id));
12961296
assert!(!offer.paths().is_empty());
12971297
for path in offer.paths() {
1298-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(bob_id));
1298+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(bob_id));
12991299
}
13001300

13011301
let invalid_path = alice.node
@@ -1304,7 +1304,7 @@ fn fails_authentication_when_handling_invoice_request() {
13041304
.build().unwrap()
13051305
.paths().first().unwrap()
13061306
.clone();
1307-
assert_eq!(invalid_path.0.introduction_node, IntroductionNode::NodeId(bob_id));
1307+
assert_eq!(invalid_path.introduction_node(), &IntroductionNode::NodeId(bob_id));
13081308

13091309
// Send the invoice request directly to Alice instead of using a blinded path.
13101310
let payment_id = PaymentId([1; 32]);
@@ -1328,7 +1328,7 @@ fn fails_authentication_when_handling_invoice_request() {
13281328
let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
13291329
assert_eq!(invoice_request.amount_msats(), None);
13301330
assert_ne!(invoice_request.payer_id(), david_id);
1331-
assert_eq!(reply_path.0.introduction_node, IntroductionNode::NodeId(charlie_id));
1331+
assert_eq!(reply_path.introduction_node(), &IntroductionNode::NodeId(charlie_id));
13321332

13331333
assert_eq!(alice.onion_messenger.next_onion_message_for_peer(charlie_id), None);
13341334

@@ -1361,7 +1361,7 @@ fn fails_authentication_when_handling_invoice_request() {
13611361
let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
13621362
assert_eq!(invoice_request.amount_msats(), None);
13631363
assert_ne!(invoice_request.payer_id(), david_id);
1364-
assert_eq!(reply_path.0.introduction_node, IntroductionNode::NodeId(charlie_id));
1364+
assert_eq!(reply_path.introduction_node(), &IntroductionNode::NodeId(charlie_id));
13651365

13661366
assert_eq!(alice.onion_messenger.next_onion_message_for_peer(charlie_id), None);
13671367
}
@@ -1412,7 +1412,7 @@ fn fails_authentication_when_handling_invoice_for_offer() {
14121412
assert_ne!(offer.signing_pubkey(), Some(alice_id));
14131413
assert!(!offer.paths().is_empty());
14141414
for path in offer.paths() {
1415-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(bob_id));
1415+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(bob_id));
14161416
}
14171417

14181418
// Initiate an invoice request, but abandon tracking it.
@@ -1466,7 +1466,7 @@ fn fails_authentication_when_handling_invoice_for_offer() {
14661466
let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
14671467
assert_eq!(invoice_request.amount_msats(), None);
14681468
assert_ne!(invoice_request.payer_id(), david_id);
1469-
assert_eq!(reply_path.0.introduction_node, IntroductionNode::NodeId(charlie_id));
1469+
assert_eq!(reply_path.introduction_node(), &IntroductionNode::NodeId(charlie_id));
14701470

14711471
let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
14721472
charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
@@ -1523,7 +1523,7 @@ fn fails_authentication_when_handling_invoice_for_refund() {
15231523
assert_ne!(refund.payer_id(), david_id);
15241524
assert!(!refund.paths().is_empty());
15251525
for path in refund.paths() {
1526-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(charlie_id));
1526+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(charlie_id));
15271527
}
15281528
expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
15291529

@@ -1560,7 +1560,7 @@ fn fails_authentication_when_handling_invoice_for_refund() {
15601560
assert_ne!(refund.payer_id(), david_id);
15611561
assert!(!refund.paths().is_empty());
15621562
for path in refund.paths() {
1563-
assert_eq!(path.0.introduction_node, IntroductionNode::NodeId(charlie_id));
1563+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(charlie_id));
15641564
}
15651565

15661566
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
@@ -1415,7 +1415,7 @@ pub(super) fn check_invoice_signing_pubkey(
14151415
(None, Some(paths)) => {
14161416
if !paths
14171417
.iter()
1418-
.filter_map(|path| path.0.blinded_hops.last())
1418+
.filter_map(|path| path.blinded_hops().last())
14191419
.any(|last_hop| invoice_signing_pubkey == &last_hop.blinded_node_id)
14201420
{
14211421
return Err(Bolt12SemanticError::InvalidSigningPubkey);
@@ -1441,7 +1441,7 @@ mod tests {
14411441

14421442
use core::time::Duration;
14431443

1444-
use crate::blinded_path::{BlindedHop, BlindedPath, IntroductionNode};
1444+
use crate::blinded_path::BlindedHop;
14451445
use crate::blinded_path::message::BlindedMessagePath;
14461446
use crate::sign::KeyMaterial;
14471447
use crate::ln::features::{Bolt12InvoiceFeatures, InvoiceRequestFeatures, OfferFeatures};
@@ -1791,14 +1791,13 @@ mod tests {
17911791
let nonce = Nonce::from_entropy_source(&entropy);
17921792
let secp_ctx = Secp256k1::new();
17931793

1794-
let blinded_path = BlindedMessagePath(BlindedPath {
1795-
introduction_node: IntroductionNode::NodeId(pubkey(40)),
1796-
blinding_point: pubkey(41),
1797-
blinded_hops: vec![
1794+
let blinded_path = BlindedMessagePath::from_raw(
1795+
pubkey(40), pubkey(41),
1796+
vec![
17981797
BlindedHop { blinded_node_id: pubkey(42), encrypted_payload: vec![0; 43] },
17991798
BlindedHop { blinded_node_id: node_id, encrypted_payload: vec![0; 44] },
1800-
],
1801-
});
1799+
]
1800+
);
18021801

18031802
#[cfg(c_bindings)]
18041803
use crate::offers::offer::OfferWithDerivedMetadataBuilder as OfferBuilder;
@@ -1867,14 +1866,13 @@ mod tests {
18671866
let entropy = FixedEntropy {};
18681867
let secp_ctx = Secp256k1::new();
18691868

1870-
let blinded_path = BlindedMessagePath(BlindedPath {
1871-
introduction_node: IntroductionNode::NodeId(pubkey(40)),
1872-
blinding_point: pubkey(41),
1873-
blinded_hops: vec![
1869+
let blinded_path = BlindedMessagePath::from_raw(
1870+
pubkey(40), pubkey(41),
1871+
vec![
18741872
BlindedHop { blinded_node_id: pubkey(42), encrypted_payload: vec![0; 43] },
18751873
BlindedHop { blinded_node_id: node_id, encrypted_payload: vec![0; 44] },
1876-
],
1877-
});
1874+
]
1875+
);
18781876

18791877
let refund = RefundBuilder::new(vec![1; 32], payer_pubkey(), 1000).unwrap()
18801878
.path(blinded_path)
@@ -2371,22 +2369,20 @@ mod tests {
23712369
#[test]
23722370
fn parses_invoice_with_node_id_from_blinded_path() {
23732371
let paths = vec![
2374-
BlindedMessagePath(BlindedPath {
2375-
introduction_node: IntroductionNode::NodeId(pubkey(40)),
2376-
blinding_point: pubkey(41),
2377-
blinded_hops: vec![
2372+
BlindedMessagePath::from_raw(
2373+
pubkey(40), pubkey(41),
2374+
vec![
23782375
BlindedHop { blinded_node_id: pubkey(43), encrypted_payload: vec![0; 43] },
23792376
BlindedHop { blinded_node_id: pubkey(44), encrypted_payload: vec![0; 44] },
2380-
],
2381-
}),
2382-
BlindedMessagePath(BlindedPath {
2383-
introduction_node: IntroductionNode::NodeId(pubkey(40)),
2384-
blinding_point: pubkey(41),
2385-
blinded_hops: vec![
2377+
]
2378+
),
2379+
BlindedMessagePath::from_raw(
2380+
pubkey(40), pubkey(41),
2381+
vec![
23862382
BlindedHop { blinded_node_id: pubkey(45), encrypted_payload: vec![0; 45] },
23872383
BlindedHop { blinded_node_id: pubkey(46), encrypted_payload: vec![0; 46] },
2388-
],
2389-
}),
2384+
]
2385+
),
23902386
];
23912387

23922388
let blinded_node_id_sign = |message: &UnsignedBolt12Invoice| {
@@ -2524,14 +2520,13 @@ mod tests {
25242520
.build().unwrap()
25252521
.sign(recipient_sign).unwrap();
25262522

2527-
let blinded_path = BlindedMessagePath(BlindedPath {
2528-
introduction_node: IntroductionNode::NodeId(pubkey(40)),
2529-
blinding_point: pubkey(41),
2530-
blinded_hops: vec![
2523+
let blinded_path = BlindedMessagePath::from_raw(
2524+
pubkey(40), pubkey(41),
2525+
vec![
25312526
BlindedHop { blinded_node_id: pubkey(42), encrypted_payload: vec![0; 43] },
25322527
BlindedHop { blinded_node_id: pubkey(43), encrypted_payload: vec![0; 44] },
2533-
],
2534-
});
2528+
]
2529+
);
25352530

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

0 commit comments

Comments
 (0)