Skip to content

Commit f45a840

Browse files
authored
Merge pull request #3132 from jkczyz/2024-06-bolt12-unannounced
Blinded paths with unannounced introduction nodes
2 parents 87fc324 + c1eda4b commit f45a840

File tree

8 files changed

+222
-41
lines changed

8 files changed

+222
-41
lines changed

lightning/src/blinded_path/message.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use core::mem;
3030
use core::ops::Deref;
3131

3232
/// An intermediate node, and possibly a short channel id leading to the next node.
33-
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
33+
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
3434
pub struct ForwardNode {
3535
/// This node's pubkey.
3636
pub node_id: PublicKey,
@@ -106,6 +106,8 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
106106

107107
// Advance the blinded onion message path by one hop, so make the second hop into the new
108108
// introduction node.
109+
//
110+
// Will only modify `path` when returning `Ok`.
109111
pub(crate) fn advance_path_by_one<NS: Deref, NL: Deref, T>(
110112
path: &mut BlindedPath, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1<T>
111113
) -> Result<(), ()>
@@ -116,8 +118,8 @@ where
116118
{
117119
let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &path.blinding_point, None)?;
118120
let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
119-
let encrypted_control_tlvs = path.blinded_hops.remove(0).encrypted_payload;
120-
let mut s = Cursor::new(&encrypted_control_tlvs);
121+
let encrypted_control_tlvs = &path.blinded_hops.get(0).ok_or(())?.encrypted_payload;
122+
let mut s = Cursor::new(encrypted_control_tlvs);
121123
let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
122124
match ChaChaPolyReadAdapter::read(&mut reader, rho) {
123125
Ok(ChaChaPolyReadAdapter {
@@ -139,6 +141,7 @@ where
139141
};
140142
mem::swap(&mut path.blinding_point, &mut new_blinding_point);
141143
path.introduction_node = IntroductionNode::NodeId(next_node_id);
144+
path.blinded_hops.remove(0);
142145
Ok(())
143146
},
144147
_ => Err(())

lightning/src/blinded_path/payment.rs

+46-2
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,24 @@
1313
1414
use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
1515

16-
use crate::blinded_path::BlindedHop;
16+
use crate::blinded_path::{BlindedHop, BlindedPath, IntroductionNode, NodeIdLookUp};
1717
use crate::blinded_path::utils;
18+
use crate::crypto::streams::ChaChaPolyReadAdapter;
1819
use crate::io;
20+
use crate::io::Cursor;
1921
use crate::ln::types::PaymentSecret;
2022
use crate::ln::channel_state::CounterpartyForwardingInfo;
2123
use crate::ln::features::BlindedHopFeatures;
2224
use crate::ln::msgs::DecodeError;
25+
use crate::ln::onion_utils;
2326
use crate::offers::invoice::BlindedPayInfo;
2427
use crate::offers::invoice_request::InvoiceRequestFields;
2528
use crate::offers::offer::OfferId;
26-
use crate::util::ser::{HighZeroBytesDroppedBigSize, Readable, Writeable, Writer};
29+
use crate::sign::{NodeSigner, Recipient};
30+
use crate::util::ser::{FixedLengthReader, LengthReadableArgs, HighZeroBytesDroppedBigSize, Readable, Writeable, Writer};
31+
32+
use core::mem;
33+
use core::ops::Deref;
2734

2835
#[allow(unused_imports)]
2936
use crate::prelude::*;
@@ -274,6 +281,43 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
274281
utils::construct_blinded_hops(secp_ctx, pks, tlvs, session_priv)
275282
}
276283

284+
// Advance the blinded onion payment path by one hop, so make the second hop into the new
285+
// introduction node.
286+
//
287+
// Will only modify `path` when returning `Ok`.
288+
pub(crate) fn advance_path_by_one<NS: Deref, NL: Deref, T>(
289+
path: &mut BlindedPath, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1<T>
290+
) -> Result<(), ()>
291+
where
292+
NS::Target: NodeSigner,
293+
NL::Target: NodeIdLookUp,
294+
T: secp256k1::Signing + secp256k1::Verification,
295+
{
296+
let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &path.blinding_point, None)?;
297+
let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
298+
let encrypted_control_tlvs = &path.blinded_hops.get(0).ok_or(())?.encrypted_payload;
299+
let mut s = Cursor::new(encrypted_control_tlvs);
300+
let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
301+
match ChaChaPolyReadAdapter::read(&mut reader, rho) {
302+
Ok(ChaChaPolyReadAdapter {
303+
readable: BlindedPaymentTlvs::Forward(ForwardTlvs { short_channel_id, .. })
304+
}) => {
305+
let next_node_id = match node_id_lookup.next_node_id(short_channel_id) {
306+
Some(node_id) => node_id,
307+
None => return Err(()),
308+
};
309+
let mut new_blinding_point = onion_utils::next_hop_pubkey(
310+
secp_ctx, path.blinding_point, control_tlvs_ss.as_ref()
311+
).map_err(|_| ())?;
312+
mem::swap(&mut path.blinding_point, &mut new_blinding_point);
313+
path.introduction_node = IntroductionNode::NodeId(next_node_id);
314+
path.blinded_hops.remove(0);
315+
Ok(())
316+
},
317+
_ => Err(())
318+
}
319+
}
320+
277321
/// `None` if underflow occurs.
278322
pub(crate) fn amt_to_forward_msat(inbound_amt_msat: u64, payment_relay: &PaymentRelay) -> Option<u64> {
279323
let inbound_amt = inbound_amt_msat as u128;

lightning/src/ln/channelmanager.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4031,8 +4031,8 @@ where
40314031
self.pending_outbound_payments
40324032
.send_payment_for_bolt12_invoice(
40334033
invoice, payment_id, &self.router, self.list_usable_channels(),
4034-
|| self.compute_inflight_htlcs(), &self.entropy_source, &self.node_signer,
4035-
best_block_height, &self.logger, &self.pending_events,
4034+
|| self.compute_inflight_htlcs(), &self.entropy_source, &self.node_signer, &self,
4035+
&self.secp_ctx, best_block_height, &self.logger, &self.pending_events,
40364036
|args| self.send_payment_along_path(args)
40374037
)
40384038
}

lightning/src/ln/offers_tests.rs

+85-17
Original file line numberDiff line numberDiff line change
@@ -950,43 +950,112 @@ fn pays_bolt12_invoice_asynchronously() {
950950
);
951951
}
952952

953-
/// Fails creating an offer when a blinded path cannot be created without exposing the node's id.
953+
/// Checks that an offer can be created using an unannounced node as a blinded path's introduction
954+
/// node. This is only preferred if there are no other options which may indicated either the offer
955+
/// is intended for the unannounced node or that the node is actually announced (e.g., an LSP) but
956+
/// the recipient doesn't have a network graph.
954957
#[test]
955-
fn fails_creating_offer_without_blinded_paths() {
958+
fn creates_offer_with_blinded_path_using_unannounced_introduction_node() {
956959
let chanmon_cfgs = create_chanmon_cfgs(2);
957960
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
958961
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
959962
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
960963

961964
create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
962965

963-
match nodes[0].node.create_offer_builder(None) {
964-
Ok(_) => panic!("Expected error"),
965-
Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
966+
let alice = &nodes[0];
967+
let alice_id = alice.node.get_our_node_id();
968+
let bob = &nodes[1];
969+
let bob_id = bob.node.get_our_node_id();
970+
971+
let offer = alice.node
972+
.create_offer_builder(None).unwrap()
973+
.amount_msats(10_000_000)
974+
.build().unwrap();
975+
assert_ne!(offer.signing_pubkey(), Some(alice_id));
976+
assert!(!offer.paths().is_empty());
977+
for path in offer.paths() {
978+
assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
966979
}
980+
981+
let payment_id = PaymentId([1; 32]);
982+
bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
983+
expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
984+
985+
let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
986+
alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
987+
988+
let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
989+
let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
990+
offer_id: offer.id(),
991+
invoice_request: InvoiceRequestFields {
992+
payer_id: invoice_request.payer_id(),
993+
quantity: None,
994+
payer_note_truncated: None,
995+
},
996+
});
997+
assert_ne!(invoice_request.payer_id(), bob_id);
998+
assert_eq!(reply_path.introduction_node, IntroductionNode::NodeId(alice_id));
999+
1000+
let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
1001+
bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1002+
1003+
let invoice = extract_invoice(bob, &onion_message);
1004+
assert_ne!(invoice.signing_pubkey(), alice_id);
1005+
assert!(!invoice.payment_paths().is_empty());
1006+
for (_, path) in invoice.payment_paths() {
1007+
assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
1008+
}
1009+
1010+
route_bolt12_payment(bob, &[alice], &invoice);
1011+
expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
1012+
1013+
claim_bolt12_payment(bob, &[alice], payment_context);
1014+
expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
9671015
}
9681016

969-
/// Fails creating a refund when a blinded path cannot be created without exposing the node's id.
1017+
/// Checks that a refund can be created using an unannounced node as a blinded path's introduction
1018+
/// node. This is only preferred if there are no other options which may indicated either the refund
1019+
/// is intended for the unannounced node or that the node is actually announced (e.g., an LSP) but
1020+
/// the sender doesn't have a network graph.
9701021
#[test]
971-
fn fails_creating_refund_without_blinded_paths() {
1022+
fn creates_refund_with_blinded_path_using_unannounced_introduction_node() {
9721023
let chanmon_cfgs = create_chanmon_cfgs(2);
9731024
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9741025
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9751026
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9761027

9771028
create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
9781029

1030+
let alice = &nodes[0];
1031+
let alice_id = alice.node.get_our_node_id();
1032+
let bob = &nodes[1];
1033+
let bob_id = bob.node.get_our_node_id();
1034+
9791035
let absolute_expiry = Duration::from_secs(u64::MAX);
9801036
let payment_id = PaymentId([1; 32]);
981-
982-
match nodes[0].node.create_refund_builder(
983-
10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
984-
) {
985-
Ok(_) => panic!("Expected error"),
986-
Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1037+
let refund = bob.node
1038+
.create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1039+
.unwrap()
1040+
.build().unwrap();
1041+
assert_ne!(refund.payer_id(), bob_id);
1042+
assert!(!refund.paths().is_empty());
1043+
for path in refund.paths() {
1044+
assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
9871045
}
1046+
expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
9881047

989-
assert!(nodes[0].node.list_recent_payments().is_empty());
1048+
let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
1049+
1050+
let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
1051+
1052+
let invoice = extract_invoice(bob, &onion_message);
1053+
assert_eq!(invoice, expected_invoice);
1054+
assert_ne!(invoice.signing_pubkey(), alice_id);
1055+
assert!(!invoice.payment_paths().is_empty());
1056+
for (_, path) in invoice.payment_paths() {
1057+
assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
1058+
}
9901059
}
9911060

9921061
/// Fails creating or paying an offer when a blinded path cannot be created because no peers are
@@ -1165,8 +1234,7 @@ fn fails_sending_invoice_with_unsupported_chain_for_refund() {
11651234
}
11661235
}
11671236

1168-
/// Fails creating an invoice request when a blinded reply path cannot be created without exposing
1169-
/// the node's id.
1237+
/// Fails creating an invoice request when a blinded reply path cannot be created.
11701238
#[test]
11711239
fn fails_creating_invoice_request_without_blinded_reply_path() {
11721240
let chanmon_cfgs = create_chanmon_cfgs(6);
@@ -1183,7 +1251,7 @@ fn fails_creating_invoice_request_without_blinded_reply_path() {
11831251
let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
11841252

11851253
disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1186-
disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1254+
disconnect_peers(david, &[bob, charlie, &nodes[4], &nodes[5]]);
11871255

11881256
let offer = alice.node
11891257
.create_offer_builder(None).unwrap()

0 commit comments

Comments
 (0)