Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions lightning/src/ln/channelmanager.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -8765,14 +8765,7 @@ where
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;

let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
if offer.paths().is_empty() {
let message = new_pending_onion_message(
OffersMessage::InvoiceRequest(invoice_request),
Destination::Node(offer.signing_pubkey()),
Some(reply_path),
);
pending_offers_messages.push(message);
} else {
if !offer.paths().is_empty() {
// Send as many invoice requests as there are paths in the offer (with an upper bound).
// Using only one path could result in a failure if the path no longer exists. But only
// one invoice for a given payment id will be paid, even if more than one is received.
Expand All@@ -8785,6 +8778,16 @@ where
);
pending_offers_messages.push(message);
}
} else if let Some(signing_pubkey) = offer.signing_pubkey() {
let message = new_pending_onion_message(
OffersMessage::InvoiceRequest(invoice_request),
Destination::Node(signing_pubkey),
Some(reply_path),
);
pending_offers_messages.push(message);
} else {
debug_assert!(false);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its usually worth having a comment on these kinds of assertions as to why we think its not reachable.

return Err(Bolt12SemanticError::MissingSigningPubkey);
}

Ok(())
Expand Down
12 changes: 6 additions & 6 deletions lightning/src/ln/offers_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -271,7 +271,7 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_ne!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand All@@ -286,7 +286,7 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand DownExpand Up@@ -336,7 +336,7 @@ fn prefers_more_connected_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(nodes[4].node.get_our_node_id()));
Expand DownExpand Up@@ -386,7 +386,7 @@ fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
.unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), alice_id);
assert_ne!(offer.signing_pubkey(), Some(alice_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand DownExpand Up@@ -547,7 +547,7 @@ fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), alice_id);
assert_ne!(offer.signing_pubkey(), Some(alice_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
Expand DownExpand Up@@ -672,7 +672,7 @@ fn pays_for_offer_without_blinded_paths() {
.clear_paths()
.amount_msats(10_000_000)
.build().unwrap();
assert_eq!(offer.signing_pubkey(), alice_id);
assert_eq!(offer.signing_pubkey(), Some(alice_id));
assert!(offer.paths().is_empty());

let payment_id = PaymentId([1; 32]);
Expand Down
104 changes: 97 additions & 7 deletions lightning/src/offers/invoice.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -210,10 +210,9 @@ macro_rules! invoice_explicit_signing_pubkey_builder_methods { ($self: ident, $s
#[cfg_attr(c_bindings, allow(dead_code))]
pub(super) fn for_offer(
invoice_request: &'a InvoiceRequest, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>,
created_at: Duration, payment_hash: PaymentHash
created_at: Duration, payment_hash: PaymentHash, signing_pubkey: PublicKey
) -> Result<Self, Bolt12SemanticError> {
let amount_msats = Self::amount_msats(invoice_request)?;
let signing_pubkey = invoice_request.contents.inner.offer.signing_pubkey();
let contents = InvoiceContents::ForOffer {
invoice_request: invoice_request.contents.clone(),
fields: Self::fields(
Expand DownExpand Up@@ -272,7 +271,7 @@ macro_rules! invoice_derived_signing_pubkey_builder_methods { ($self: ident, $se
created_at: Duration, payment_hash: PaymentHash, keys: KeyPair
) -> Result<Self, Bolt12SemanticError> {
let amount_msats = Self::amount_msats(invoice_request)?;
let signing_pubkey = invoice_request.contents.inner.offer.signing_pubkey();
let signing_pubkey = keys.public_key();
let contents = InvoiceContents::ForOffer {
invoice_request: invoice_request.contents.clone(),
fields: Self::fields(
Expand DownExpand Up@@ -1435,8 +1434,8 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
features, signing_pubkey,
};

match offer_tlv_stream.node_id {
Some(expected_signing_pubkey) => {
match (offer_tlv_stream.node_id, &offer_tlv_stream.paths) {
(Some(expected_signing_pubkey), _) => {
if fields.signing_pubkey != expected_signing_pubkey {
return Err(Bolt12SemanticError::InvalidSigningPubkey);
}
Expand All@@ -1446,7 +1445,21 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
)?;
Ok(InvoiceContents::ForOffer { invoice_request, fields })
},
None => {
(None, Some(paths)) => {
if !paths
.iter()
.filter_map(|path| path.blinded_hops.last())
.any(|last_hop| fields.signing_pubkey == last_hop.blinded_node_id)
Comment on lines +1450 to +1452

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the spec, it looks like we MUST verify that the signing pubkey matches a path we sent an invoice request to. I'm not certain it matters or if the spec should change but it looks like we don't specifically check that at the moment.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is a slight deviation as we don't have the blinded node id for the path that the invoice was sent over when parsing. We could do some sort of check at the handler level, but even that would require piping that data through.

{
return Err(Bolt12SemanticError::InvalidSigningPubkey);
}

let invoice_request = InvoiceRequestContents::try_from(
(payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream)
)?;
Ok(InvoiceContents::ForOffer { invoice_request, fields })
},
(None, None) => {
let refund = RefundContents::try_from(
(payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream)
)?;
Expand All@@ -1464,7 +1477,7 @@ mod tests {
use bitcoin::blockdata::script::ScriptBuf;
use bitcoin::hashes::Hash;
use bitcoin::network::constants::Network;
use bitcoin::secp256k1::{Message, Secp256k1, XOnlyPublicKey, self};
use bitcoin::secp256k1::{KeyPair, Message, Secp256k1, SecretKey, XOnlyPublicKey, self};
use bitcoin::address::{Address, Payload, WitnessProgram, WitnessVersion};
use bitcoin::key::TweakedPublicKey;

Expand DownExpand Up@@ -1633,6 +1646,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
InvoiceTlvStreamRef {
paths: Some(Iterable(payment_paths.iter().map(|(_, path)| path))),
Expand DownExpand Up@@ -1725,6 +1739,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
InvoiceTlvStreamRef {
paths: Some(Iterable(payment_paths.iter().map(|(_, path)| path))),
Expand DownExpand Up@@ -2365,6 +2380,81 @@ mod tests {
}
}

#[test]
fn parses_invoice_with_node_id_from_blinded_path() {
let paths = vec![
BlindedPath {
introduction_node: IntroductionNode::NodeId(pubkey(40)),
blinding_point: pubkey(41),
blinded_hops: vec![
BlindedHop { blinded_node_id: pubkey(43), encrypted_payload: vec![0; 43] },
BlindedHop { blinded_node_id: pubkey(44), encrypted_payload: vec![0; 44] },
],
},
BlindedPath {
introduction_node: IntroductionNode::NodeId(pubkey(40)),
blinding_point: pubkey(41),
blinded_hops: vec![
BlindedHop { blinded_node_id: pubkey(45), encrypted_payload: vec![0; 45] },
BlindedHop { blinded_node_id: pubkey(46), encrypted_payload: vec![0; 46] },
],
},
];

let blinded_node_id_sign = |message: &UnsignedBolt12Invoice| {
let secp_ctx = Secp256k1::new();
let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[46; 32]).unwrap());
Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
};

let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
.clear_signing_pubkey()
.amount_msats(1000)
.path(paths[0].clone())
.path(paths[1].clone())
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.build().unwrap()
.sign(payer_sign).unwrap()
.respond_with_no_std_using_signing_pubkey(
payment_paths(), payment_hash(), now(), pubkey(46)
).unwrap()
.build().unwrap()
.sign(blinded_node_id_sign).unwrap();

let mut buffer = Vec::new();
invoice.write(&mut buffer).unwrap();

if let Err(e) = Bolt12Invoice::try_from(buffer) {
panic!("error parsing invoice: {:?}", e);
}

let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
.clear_signing_pubkey()
.amount_msats(1000)
.path(paths[0].clone())
.path(paths[1].clone())
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.build().unwrap()
.sign(payer_sign).unwrap()
.respond_with_no_std_using_signing_pubkey(
payment_paths(), payment_hash(), now(), recipient_pubkey()
).unwrap()
.build().unwrap()
.sign(recipient_sign).unwrap();

let mut buffer = Vec::new();
invoice.write(&mut buffer).unwrap();

match Bolt12Invoice::try_from(buffer) {
Ok(_) => panic!("expected error"),
Err(e) => {
assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::InvalidSigningPubkey));
},
}
}

#[test]
fn fails_parsing_invoice_without_signature() {
let mut buffer = Vec::new();
Expand Down
47 changes: 42 additions & 5 deletions lightning/src/offers/invoice_request.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -747,7 +747,27 @@ macro_rules! invoice_request_respond_with_explicit_signing_pubkey_methods { (
return Err(Bolt12SemanticError::UnknownRequiredFeatures);
}

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash)
let signing_pubkey = match $contents.contents.inner.offer.signing_pubkey() {
Some(signing_pubkey) => signing_pubkey,
None => return Err(Bolt12SemanticError::MissingSigningPubkey),
};

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash, signing_pubkey)
}

#[cfg(test)]
#[allow(dead_code)]
pub(super) fn respond_with_no_std_using_signing_pubkey(
&$self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
created_at: core::time::Duration, signing_pubkey: PublicKey
) -> Result<$builder, Bolt12SemanticError> {
debug_assert!($contents.contents.inner.offer.signing_pubkey().is_none());

if $contents.invoice_request_features().requires_unknown_bits() {
return Err(Bolt12SemanticError::UnknownRequiredFeatures);
}

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash, signing_pubkey)
}
} }

Expand DownExpand Up@@ -855,6 +875,11 @@ macro_rules! invoice_request_respond_with_derived_signing_pubkey_methods { (
Some(keys) => keys,
};

match $contents.contents.inner.offer.signing_pubkey() {
Some(signing_pubkey) => debug_assert_eq!(signing_pubkey, keys.public_key()),
None => return Err(Bolt12SemanticError::MissingSigningPubkey),
}

<$builder>::for_offer_using_keys(
&$self.inner, payment_paths, created_at, payment_hash, keys
)
Expand DownExpand Up@@ -961,6 +986,7 @@ impl InvoiceRequestContentsWithoutPayerId {
quantity: self.quantity,
payer_id: None,
payer_note: self.payer_note.as_ref(),
paths: None,
};

(payer, offer, invoice_request)
Expand DownExpand Up@@ -993,13 +1019,17 @@ pub(super) const INVOICE_REQUEST_TYPES: core::ops::Range<u64> = 80..160;
/// [`Refund::payer_id`]: crate::offers::refund::Refund::payer_id
pub(super) const INVOICE_REQUEST_PAYER_ID_TYPE: u64 = 88;

// This TLV stream is used for both InvoiceRequest and Refund, but not all TLV records are valid for
// InvoiceRequest as noted below.
tlv_stream!(InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef, INVOICE_REQUEST_TYPES, {
(80, chain: ChainHash),
(82, amount: (u64, HighZeroBytesDroppedBigSize)),
(84, features: (InvoiceRequestFeatures, WithoutLength)),
(86, quantity: (u64, HighZeroBytesDroppedBigSize)),
(INVOICE_REQUEST_PAYER_ID_TYPE, payer_id: PublicKey),
(89, payer_note: (String, WithoutLength)),
// Only used for Refund since the onion message of an InvoiceRequest has a reply path.
(90, paths: (Vec<BlindedPath>, WithoutLength)),
Comment thread
jkczyz marked this conversation as resolved.
});

type FullInvoiceRequestTlvStream =
Expand DownExpand Up@@ -1082,7 +1112,9 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
let (
PayerTlvStream { metadata },
offer_tlv_stream,
InvoiceRequestTlvStream { chain, amount, features, quantity, payer_id, payer_note },
InvoiceRequestTlvStream {
chain, amount, features, quantity, payer_id, payer_note, paths,
},
) = tlv_stream;

let payer = match metadata {
Expand All@@ -1109,6 +1141,10 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
Some(payer_id) => payer_id,
};

if paths.is_some() {
return Err(Bolt12SemanticError::UnexpectedPaths);
}

Ok(InvoiceRequestContents {
inner: InvoiceRequestContentsWithoutPayerId {
payer, offer, chain, amount_msats: amount, features, quantity, payer_note,
Expand DownExpand Up@@ -1233,7 +1269,7 @@ mod tests {
assert_eq!(unsigned_invoice_request.paths(), &[]);
assert_eq!(unsigned_invoice_request.issuer(), None);
assert_eq!(unsigned_invoice_request.supported_quantity(), Quantity::One);
assert_eq!(unsigned_invoice_request.signing_pubkey(), recipient_pubkey());
assert_eq!(unsigned_invoice_request.signing_pubkey(), Some(recipient_pubkey()));
assert_eq!(unsigned_invoice_request.chain(), ChainHash::using_genesis_block(Network::Bitcoin));
assert_eq!(unsigned_invoice_request.amount_msats(), None);
assert_eq!(unsigned_invoice_request.invoice_request_features(), &InvoiceRequestFeatures::empty());
Expand DownExpand Up@@ -1265,7 +1301,7 @@ mod tests {
assert_eq!(invoice_request.paths(), &[]);
assert_eq!(invoice_request.issuer(), None);
assert_eq!(invoice_request.supported_quantity(), Quantity::One);
assert_eq!(invoice_request.signing_pubkey(), recipient_pubkey());
assert_eq!(invoice_request.signing_pubkey(), Some(recipient_pubkey()));
assert_eq!(invoice_request.chain(), ChainHash::using_genesis_block(Network::Bitcoin));
assert_eq!(invoice_request.amount_msats(), None);
assert_eq!(invoice_request.invoice_request_features(), &InvoiceRequestFeatures::empty());
Expand DownExpand Up@@ -1300,6 +1336,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
SignatureTlvStreamRef { signature: Some(&invoice_request.signature()) },
),
Expand DownExpand Up@@ -2260,7 +2297,7 @@ mod tests {
.amount_msats(1000)
.supported_quantity(Quantity::Unbounded)
.build().unwrap();
assert_eq!(offer.signing_pubkey(), node_id);
assert_eq!(offer.signing_pubkey(), Some(node_id));

let invoice_request = offer.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.chain(Network::Testnet).unwrap()
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions lightning/src/ln/channelmanager.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -8765,14 +8765,7 @@ where
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;

let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
if offer.paths().is_empty() {
let message = new_pending_onion_message(
OffersMessage::InvoiceRequest(invoice_request),
Destination::Node(offer.signing_pubkey()),
Some(reply_path),
);
pending_offers_messages.push(message);
} else {
if !offer.paths().is_empty() {
// Send as many invoice requests as there are paths in the offer (with an upper bound).
// Using only one path could result in a failure if the path no longer exists. But only
// one invoice for a given payment id will be paid, even if more than one is received.
Expand All@@ -8785,6 +8778,16 @@ where
);
pending_offers_messages.push(message);
}
} else if let Some(signing_pubkey) = offer.signing_pubkey() {
let message = new_pending_onion_message(
OffersMessage::InvoiceRequest(invoice_request),
Destination::Node(signing_pubkey),
Some(reply_path),
);
pending_offers_messages.push(message);
} else {
debug_assert!(false);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its usually worth having a comment on these kinds of assertions as to why we think its not reachable.

return Err(Bolt12SemanticError::MissingSigningPubkey);
}

Ok(())
Expand Down
12 changes: 6 additions & 6 deletions lightning/src/ln/offers_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -271,7 +271,7 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_ne!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand All@@ -286,7 +286,7 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand DownExpand Up@@ -336,7 +336,7 @@ fn prefers_more_connected_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(nodes[4].node.get_our_node_id()));
Expand DownExpand Up@@ -386,7 +386,7 @@ fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
.unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), alice_id);
assert_ne!(offer.signing_pubkey(), Some(alice_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand DownExpand Up@@ -547,7 +547,7 @@ fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), alice_id);
assert_ne!(offer.signing_pubkey(), Some(alice_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
Expand DownExpand Up@@ -672,7 +672,7 @@ fn pays_for_offer_without_blinded_paths() {
.clear_paths()
.amount_msats(10_000_000)
.build().unwrap();
assert_eq!(offer.signing_pubkey(), alice_id);
assert_eq!(offer.signing_pubkey(), Some(alice_id));
assert!(offer.paths().is_empty());

let payment_id = PaymentId([1; 32]);
Expand Down
104 changes: 97 additions & 7 deletions lightning/src/offers/invoice.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -210,10 +210,9 @@ macro_rules! invoice_explicit_signing_pubkey_builder_methods { ($self: ident, $s
#[cfg_attr(c_bindings, allow(dead_code))]
pub(super) fn for_offer(
invoice_request: &'a InvoiceRequest, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>,
created_at: Duration, payment_hash: PaymentHash
created_at: Duration, payment_hash: PaymentHash, signing_pubkey: PublicKey
) -> Result<Self, Bolt12SemanticError> {
let amount_msats = Self::amount_msats(invoice_request)?;
let signing_pubkey = invoice_request.contents.inner.offer.signing_pubkey();
let contents = InvoiceContents::ForOffer {
invoice_request: invoice_request.contents.clone(),
fields: Self::fields(
Expand DownExpand Up@@ -272,7 +271,7 @@ macro_rules! invoice_derived_signing_pubkey_builder_methods { ($self: ident, $se
created_at: Duration, payment_hash: PaymentHash, keys: KeyPair
) -> Result<Self, Bolt12SemanticError> {
let amount_msats = Self::amount_msats(invoice_request)?;
let signing_pubkey = invoice_request.contents.inner.offer.signing_pubkey();
let signing_pubkey = keys.public_key();
let contents = InvoiceContents::ForOffer {
invoice_request: invoice_request.contents.clone(),
fields: Self::fields(
Expand DownExpand Up@@ -1435,8 +1434,8 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
features, signing_pubkey,
};

match offer_tlv_stream.node_id {
Some(expected_signing_pubkey) => {
match (offer_tlv_stream.node_id, &offer_tlv_stream.paths) {
(Some(expected_signing_pubkey), _) => {
if fields.signing_pubkey != expected_signing_pubkey {
return Err(Bolt12SemanticError::InvalidSigningPubkey);
}
Expand All@@ -1446,7 +1445,21 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
)?;
Ok(InvoiceContents::ForOffer { invoice_request, fields })
},
None => {
(None, Some(paths)) => {
if !paths
.iter()
.filter_map(|path| path.blinded_hops.last())
.any(|last_hop| fields.signing_pubkey == last_hop.blinded_node_id)
Comment on lines +1450 to +1452

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the spec, it looks like we MUST verify that the signing pubkey matches a path we sent an invoice request to. I'm not certain it matters or if the spec should change but it looks like we don't specifically check that at the moment.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is a slight deviation as we don't have the blinded node id for the path that the invoice was sent over when parsing. We could do some sort of check at the handler level, but even that would require piping that data through.

{
return Err(Bolt12SemanticError::InvalidSigningPubkey);
}

let invoice_request = InvoiceRequestContents::try_from(
(payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream)
)?;
Ok(InvoiceContents::ForOffer { invoice_request, fields })
},
(None, None) => {
let refund = RefundContents::try_from(
(payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream)
)?;
Expand All@@ -1464,7 +1477,7 @@ mod tests {
use bitcoin::blockdata::script::ScriptBuf;
use bitcoin::hashes::Hash;
use bitcoin::network::constants::Network;
use bitcoin::secp256k1::{Message, Secp256k1, XOnlyPublicKey, self};
use bitcoin::secp256k1::{KeyPair, Message, Secp256k1, SecretKey, XOnlyPublicKey, self};
use bitcoin::address::{Address, Payload, WitnessProgram, WitnessVersion};
use bitcoin::key::TweakedPublicKey;

Expand DownExpand Up@@ -1633,6 +1646,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
InvoiceTlvStreamRef {
paths: Some(Iterable(payment_paths.iter().map(|(_, path)| path))),
Expand DownExpand Up@@ -1725,6 +1739,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
InvoiceTlvStreamRef {
paths: Some(Iterable(payment_paths.iter().map(|(_, path)| path))),
Expand DownExpand Up@@ -2365,6 +2380,81 @@ mod tests {
}
}

#[test]
fn parses_invoice_with_node_id_from_blinded_path() {
let paths = vec![
BlindedPath {
introduction_node: IntroductionNode::NodeId(pubkey(40)),
blinding_point: pubkey(41),
blinded_hops: vec![
BlindedHop { blinded_node_id: pubkey(43), encrypted_payload: vec![0; 43] },
BlindedHop { blinded_node_id: pubkey(44), encrypted_payload: vec![0; 44] },
],
},
BlindedPath {
introduction_node: IntroductionNode::NodeId(pubkey(40)),
blinding_point: pubkey(41),
blinded_hops: vec![
BlindedHop { blinded_node_id: pubkey(45), encrypted_payload: vec![0; 45] },
BlindedHop { blinded_node_id: pubkey(46), encrypted_payload: vec![0; 46] },
],
},
];

let blinded_node_id_sign = |message: &UnsignedBolt12Invoice| {
let secp_ctx = Secp256k1::new();
let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[46; 32]).unwrap());
Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
};

let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
.clear_signing_pubkey()
.amount_msats(1000)
.path(paths[0].clone())
.path(paths[1].clone())
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.build().unwrap()
.sign(payer_sign).unwrap()
.respond_with_no_std_using_signing_pubkey(
payment_paths(), payment_hash(), now(), pubkey(46)
).unwrap()
.build().unwrap()
.sign(blinded_node_id_sign).unwrap();

let mut buffer = Vec::new();
invoice.write(&mut buffer).unwrap();

if let Err(e) = Bolt12Invoice::try_from(buffer) {
panic!("error parsing invoice: {:?}", e);
}

let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
.clear_signing_pubkey()
.amount_msats(1000)
.path(paths[0].clone())
.path(paths[1].clone())
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.build().unwrap()
.sign(payer_sign).unwrap()
.respond_with_no_std_using_signing_pubkey(
payment_paths(), payment_hash(), now(), recipient_pubkey()
).unwrap()
.build().unwrap()
.sign(recipient_sign).unwrap();

let mut buffer = Vec::new();
invoice.write(&mut buffer).unwrap();

match Bolt12Invoice::try_from(buffer) {
Ok(_) => panic!("expected error"),
Err(e) => {
assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::InvalidSigningPubkey));
},
}
}

#[test]
fn fails_parsing_invoice_without_signature() {
let mut buffer = Vec::new();
Expand Down
47 changes: 42 additions & 5 deletions lightning/src/offers/invoice_request.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -747,7 +747,27 @@ macro_rules! invoice_request_respond_with_explicit_signing_pubkey_methods { (
return Err(Bolt12SemanticError::UnknownRequiredFeatures);
}

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash)
let signing_pubkey = match $contents.contents.inner.offer.signing_pubkey() {
Some(signing_pubkey) => signing_pubkey,
None => return Err(Bolt12SemanticError::MissingSigningPubkey),
};

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash, signing_pubkey)
}

#[cfg(test)]
#[allow(dead_code)]
pub(super) fn respond_with_no_std_using_signing_pubkey(
&$self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
created_at: core::time::Duration, signing_pubkey: PublicKey
) -> Result<$builder, Bolt12SemanticError> {
debug_assert!($contents.contents.inner.offer.signing_pubkey().is_none());

if $contents.invoice_request_features().requires_unknown_bits() {
return Err(Bolt12SemanticError::UnknownRequiredFeatures);
}

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash, signing_pubkey)
}
} }

Expand DownExpand Up@@ -855,6 +875,11 @@ macro_rules! invoice_request_respond_with_derived_signing_pubkey_methods { (
Some(keys) => keys,
};

match $contents.contents.inner.offer.signing_pubkey() {
Some(signing_pubkey) => debug_assert_eq!(signing_pubkey, keys.public_key()),
None => return Err(Bolt12SemanticError::MissingSigningPubkey),
}

<$builder>::for_offer_using_keys(
&$self.inner, payment_paths, created_at, payment_hash, keys
)
Expand DownExpand Up@@ -961,6 +986,7 @@ impl InvoiceRequestContentsWithoutPayerId {
quantity: self.quantity,
payer_id: None,
payer_note: self.payer_note.as_ref(),
paths: None,
};

(payer, offer, invoice_request)
Expand DownExpand Up@@ -993,13 +1019,17 @@ pub(super) const INVOICE_REQUEST_TYPES: core::ops::Range<u64> = 80..160;
/// [`Refund::payer_id`]: crate::offers::refund::Refund::payer_id
pub(super) const INVOICE_REQUEST_PAYER_ID_TYPE: u64 = 88;

// This TLV stream is used for both InvoiceRequest and Refund, but not all TLV records are valid for
// InvoiceRequest as noted below.
tlv_stream!(InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef, INVOICE_REQUEST_TYPES, {
(80, chain: ChainHash),
(82, amount: (u64, HighZeroBytesDroppedBigSize)),
(84, features: (InvoiceRequestFeatures, WithoutLength)),
(86, quantity: (u64, HighZeroBytesDroppedBigSize)),
(INVOICE_REQUEST_PAYER_ID_TYPE, payer_id: PublicKey),
(89, payer_note: (String, WithoutLength)),
// Only used for Refund since the onion message of an InvoiceRequest has a reply path.
(90, paths: (Vec<BlindedPath>, WithoutLength)),
Comment thread
jkczyz marked this conversation as resolved.
});

type FullInvoiceRequestTlvStream =
Expand DownExpand Up@@ -1082,7 +1112,9 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
let (
PayerTlvStream { metadata },
offer_tlv_stream,
InvoiceRequestTlvStream { chain, amount, features, quantity, payer_id, payer_note },
InvoiceRequestTlvStream {
chain, amount, features, quantity, payer_id, payer_note, paths,
},
) = tlv_stream;

let payer = match metadata {
Expand All@@ -1109,6 +1141,10 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
Some(payer_id) => payer_id,
};

if paths.is_some() {
return Err(Bolt12SemanticError::UnexpectedPaths);
}

Ok(InvoiceRequestContents {
inner: InvoiceRequestContentsWithoutPayerId {
payer, offer, chain, amount_msats: amount, features, quantity, payer_note,
Expand DownExpand Up@@ -1233,7 +1269,7 @@ mod tests {
assert_eq!(unsigned_invoice_request.paths(), &[]);
assert_eq!(unsigned_invoice_request.issuer(), None);
assert_eq!(unsigned_invoice_request.supported_quantity(), Quantity::One);
assert_eq!(unsigned_invoice_request.signing_pubkey(), recipient_pubkey());
assert_eq!(unsigned_invoice_request.signing_pubkey(), Some(recipient_pubkey()));
assert_eq!(unsigned_invoice_request.chain(), ChainHash::using_genesis_block(Network::Bitcoin));
assert_eq!(unsigned_invoice_request.amount_msats(), None);
assert_eq!(unsigned_invoice_request.invoice_request_features(), &InvoiceRequestFeatures::empty());
Expand DownExpand Up@@ -1265,7 +1301,7 @@ mod tests {
assert_eq!(invoice_request.paths(), &[]);
assert_eq!(invoice_request.issuer(), None);
assert_eq!(invoice_request.supported_quantity(), Quantity::One);
assert_eq!(invoice_request.signing_pubkey(), recipient_pubkey());
assert_eq!(invoice_request.signing_pubkey(), Some(recipient_pubkey()));
assert_eq!(invoice_request.chain(), ChainHash::using_genesis_block(Network::Bitcoin));
assert_eq!(invoice_request.amount_msats(), None);
assert_eq!(invoice_request.invoice_request_features(), &InvoiceRequestFeatures::empty());
Expand DownExpand Up@@ -1300,6 +1336,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
SignatureTlvStreamRef { signature: Some(&invoice_request.signature()) },
),
Expand DownExpand Up@@ -2260,7 +2297,7 @@ mod tests {
.amount_msats(1000)
.supported_quantity(Quantity::Unbounded)
.build().unwrap();
assert_eq!(offer.signing_pubkey(), node_id);
assert_eq!(offer.signing_pubkey(), Some(node_id));

let invoice_request = offer.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.chain(Network::Testnet).unwrap()
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions lightning/src/ln/channelmanager.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -8765,14 +8765,7 @@ where
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;

let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
if offer.paths().is_empty() {
let message = new_pending_onion_message(
OffersMessage::InvoiceRequest(invoice_request),
Destination::Node(offer.signing_pubkey()),
Some(reply_path),
);
pending_offers_messages.push(message);
} else {
if !offer.paths().is_empty() {
// Send as many invoice requests as there are paths in the offer (with an upper bound).
// Using only one path could result in a failure if the path no longer exists. But only
// one invoice for a given payment id will be paid, even if more than one is received.
Expand All@@ -8785,6 +8778,16 @@ where
);
pending_offers_messages.push(message);
}
} else if let Some(signing_pubkey) = offer.signing_pubkey() {
let message = new_pending_onion_message(
OffersMessage::InvoiceRequest(invoice_request),
Destination::Node(signing_pubkey),
Some(reply_path),
);
pending_offers_messages.push(message);
} else {
debug_assert!(false);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its usually worth having a comment on these kinds of assertions as to why we think its not reachable.

return Err(Bolt12SemanticError::MissingSigningPubkey);
}

Ok(())
Expand Down
12 changes: 6 additions & 6 deletions lightning/src/ln/offers_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -271,7 +271,7 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_ne!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand All@@ -286,7 +286,7 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand DownExpand Up@@ -336,7 +336,7 @@ fn prefers_more_connected_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(nodes[4].node.get_our_node_id()));
Expand DownExpand Up@@ -386,7 +386,7 @@ fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
.unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), alice_id);
assert_ne!(offer.signing_pubkey(), Some(alice_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand DownExpand Up@@ -547,7 +547,7 @@ fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), alice_id);
assert_ne!(offer.signing_pubkey(), Some(alice_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
Expand DownExpand Up@@ -672,7 +672,7 @@ fn pays_for_offer_without_blinded_paths() {
.clear_paths()
.amount_msats(10_000_000)
.build().unwrap();
assert_eq!(offer.signing_pubkey(), alice_id);
assert_eq!(offer.signing_pubkey(), Some(alice_id));
assert!(offer.paths().is_empty());

let payment_id = PaymentId([1; 32]);
Expand Down
104 changes: 97 additions & 7 deletions lightning/src/offers/invoice.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -210,10 +210,9 @@ macro_rules! invoice_explicit_signing_pubkey_builder_methods { ($self: ident, $s
#[cfg_attr(c_bindings, allow(dead_code))]
pub(super) fn for_offer(
invoice_request: &'a InvoiceRequest, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>,
created_at: Duration, payment_hash: PaymentHash
created_at: Duration, payment_hash: PaymentHash, signing_pubkey: PublicKey
) -> Result<Self, Bolt12SemanticError> {
let amount_msats = Self::amount_msats(invoice_request)?;
let signing_pubkey = invoice_request.contents.inner.offer.signing_pubkey();
let contents = InvoiceContents::ForOffer {
invoice_request: invoice_request.contents.clone(),
fields: Self::fields(
Expand DownExpand Up@@ -272,7 +271,7 @@ macro_rules! invoice_derived_signing_pubkey_builder_methods { ($self: ident, $se
created_at: Duration, payment_hash: PaymentHash, keys: KeyPair
) -> Result<Self, Bolt12SemanticError> {
let amount_msats = Self::amount_msats(invoice_request)?;
let signing_pubkey = invoice_request.contents.inner.offer.signing_pubkey();
let signing_pubkey = keys.public_key();
let contents = InvoiceContents::ForOffer {
invoice_request: invoice_request.contents.clone(),
fields: Self::fields(
Expand DownExpand Up@@ -1435,8 +1434,8 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
features, signing_pubkey,
};

match offer_tlv_stream.node_id {
Some(expected_signing_pubkey) => {
match (offer_tlv_stream.node_id, &offer_tlv_stream.paths) {
(Some(expected_signing_pubkey), _) => {
if fields.signing_pubkey != expected_signing_pubkey {
return Err(Bolt12SemanticError::InvalidSigningPubkey);
}
Expand All@@ -1446,7 +1445,21 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
)?;
Ok(InvoiceContents::ForOffer { invoice_request, fields })
},
None => {
(None, Some(paths)) => {
if !paths
.iter()
.filter_map(|path| path.blinded_hops.last())
.any(|last_hop| fields.signing_pubkey == last_hop.blinded_node_id)
Comment on lines +1450 to +1452

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the spec, it looks like we MUST verify that the signing pubkey matches a path we sent an invoice request to. I'm not certain it matters or if the spec should change but it looks like we don't specifically check that at the moment.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is a slight deviation as we don't have the blinded node id for the path that the invoice was sent over when parsing. We could do some sort of check at the handler level, but even that would require piping that data through.

{
return Err(Bolt12SemanticError::InvalidSigningPubkey);
}

let invoice_request = InvoiceRequestContents::try_from(
(payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream)
)?;
Ok(InvoiceContents::ForOffer { invoice_request, fields })
},
(None, None) => {
let refund = RefundContents::try_from(
(payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream)
)?;
Expand All@@ -1464,7 +1477,7 @@ mod tests {
use bitcoin::blockdata::script::ScriptBuf;
use bitcoin::hashes::Hash;
use bitcoin::network::constants::Network;
use bitcoin::secp256k1::{Message, Secp256k1, XOnlyPublicKey, self};
use bitcoin::secp256k1::{KeyPair, Message, Secp256k1, SecretKey, XOnlyPublicKey, self};
use bitcoin::address::{Address, Payload, WitnessProgram, WitnessVersion};
use bitcoin::key::TweakedPublicKey;

Expand DownExpand Up@@ -1633,6 +1646,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
InvoiceTlvStreamRef {
paths: Some(Iterable(payment_paths.iter().map(|(_, path)| path))),
Expand DownExpand Up@@ -1725,6 +1739,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
InvoiceTlvStreamRef {
paths: Some(Iterable(payment_paths.iter().map(|(_, path)| path))),
Expand DownExpand Up@@ -2365,6 +2380,81 @@ mod tests {
}
}

#[test]
fn parses_invoice_with_node_id_from_blinded_path() {
let paths = vec![
BlindedPath {
introduction_node: IntroductionNode::NodeId(pubkey(40)),
blinding_point: pubkey(41),
blinded_hops: vec![
BlindedHop { blinded_node_id: pubkey(43), encrypted_payload: vec![0; 43] },
BlindedHop { blinded_node_id: pubkey(44), encrypted_payload: vec![0; 44] },
],
},
BlindedPath {
introduction_node: IntroductionNode::NodeId(pubkey(40)),
blinding_point: pubkey(41),
blinded_hops: vec![
BlindedHop { blinded_node_id: pubkey(45), encrypted_payload: vec![0; 45] },
BlindedHop { blinded_node_id: pubkey(46), encrypted_payload: vec![0; 46] },
],
},
];

let blinded_node_id_sign = |message: &UnsignedBolt12Invoice| {
let secp_ctx = Secp256k1::new();
let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[46; 32]).unwrap());
Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
};

let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
.clear_signing_pubkey()
.amount_msats(1000)
.path(paths[0].clone())
.path(paths[1].clone())
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.build().unwrap()
.sign(payer_sign).unwrap()
.respond_with_no_std_using_signing_pubkey(
payment_paths(), payment_hash(), now(), pubkey(46)
).unwrap()
.build().unwrap()
.sign(blinded_node_id_sign).unwrap();

let mut buffer = Vec::new();
invoice.write(&mut buffer).unwrap();

if let Err(e) = Bolt12Invoice::try_from(buffer) {
panic!("error parsing invoice: {:?}", e);
}

let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
.clear_signing_pubkey()
.amount_msats(1000)
.path(paths[0].clone())
.path(paths[1].clone())
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.build().unwrap()
.sign(payer_sign).unwrap()
.respond_with_no_std_using_signing_pubkey(
payment_paths(), payment_hash(), now(), recipient_pubkey()
).unwrap()
.build().unwrap()
.sign(recipient_sign).unwrap();

let mut buffer = Vec::new();
invoice.write(&mut buffer).unwrap();

match Bolt12Invoice::try_from(buffer) {
Ok(_) => panic!("expected error"),
Err(e) => {
assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::InvalidSigningPubkey));
},
}
}

#[test]
fn fails_parsing_invoice_without_signature() {
let mut buffer = Vec::new();
Expand Down
47 changes: 42 additions & 5 deletions lightning/src/offers/invoice_request.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -747,7 +747,27 @@ macro_rules! invoice_request_respond_with_explicit_signing_pubkey_methods { (
return Err(Bolt12SemanticError::UnknownRequiredFeatures);
}

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash)
let signing_pubkey = match $contents.contents.inner.offer.signing_pubkey() {
Some(signing_pubkey) => signing_pubkey,
None => return Err(Bolt12SemanticError::MissingSigningPubkey),
};

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash, signing_pubkey)
}

#[cfg(test)]
#[allow(dead_code)]
pub(super) fn respond_with_no_std_using_signing_pubkey(
&$self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
created_at: core::time::Duration, signing_pubkey: PublicKey
) -> Result<$builder, Bolt12SemanticError> {
debug_assert!($contents.contents.inner.offer.signing_pubkey().is_none());

if $contents.invoice_request_features().requires_unknown_bits() {
return Err(Bolt12SemanticError::UnknownRequiredFeatures);
}

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash, signing_pubkey)
}
} }

Expand DownExpand Up@@ -855,6 +875,11 @@ macro_rules! invoice_request_respond_with_derived_signing_pubkey_methods { (
Some(keys) => keys,
};

match $contents.contents.inner.offer.signing_pubkey() {
Some(signing_pubkey) => debug_assert_eq!(signing_pubkey, keys.public_key()),
None => return Err(Bolt12SemanticError::MissingSigningPubkey),
}

<$builder>::for_offer_using_keys(
&$self.inner, payment_paths, created_at, payment_hash, keys
)
Expand DownExpand Up@@ -961,6 +986,7 @@ impl InvoiceRequestContentsWithoutPayerId {
quantity: self.quantity,
payer_id: None,
payer_note: self.payer_note.as_ref(),
paths: None,
};

(payer, offer, invoice_request)
Expand DownExpand Up@@ -993,13 +1019,17 @@ pub(super) const INVOICE_REQUEST_TYPES: core::ops::Range<u64> = 80..160;
/// [`Refund::payer_id`]: crate::offers::refund::Refund::payer_id
pub(super) const INVOICE_REQUEST_PAYER_ID_TYPE: u64 = 88;

// This TLV stream is used for both InvoiceRequest and Refund, but not all TLV records are valid for
// InvoiceRequest as noted below.
tlv_stream!(InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef, INVOICE_REQUEST_TYPES, {
(80, chain: ChainHash),
(82, amount: (u64, HighZeroBytesDroppedBigSize)),
(84, features: (InvoiceRequestFeatures, WithoutLength)),
(86, quantity: (u64, HighZeroBytesDroppedBigSize)),
(INVOICE_REQUEST_PAYER_ID_TYPE, payer_id: PublicKey),
(89, payer_note: (String, WithoutLength)),
// Only used for Refund since the onion message of an InvoiceRequest has a reply path.
(90, paths: (Vec<BlindedPath>, WithoutLength)),
Comment thread
jkczyz marked this conversation as resolved.
});

type FullInvoiceRequestTlvStream =
Expand DownExpand Up@@ -1082,7 +1112,9 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
let (
PayerTlvStream { metadata },
offer_tlv_stream,
InvoiceRequestTlvStream { chain, amount, features, quantity, payer_id, payer_note },
InvoiceRequestTlvStream {
chain, amount, features, quantity, payer_id, payer_note, paths,
},
) = tlv_stream;

let payer = match metadata {
Expand All@@ -1109,6 +1141,10 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
Some(payer_id) => payer_id,
};

if paths.is_some() {
return Err(Bolt12SemanticError::UnexpectedPaths);
}

Ok(InvoiceRequestContents {
inner: InvoiceRequestContentsWithoutPayerId {
payer, offer, chain, amount_msats: amount, features, quantity, payer_note,
Expand DownExpand Up@@ -1233,7 +1269,7 @@ mod tests {
assert_eq!(unsigned_invoice_request.paths(), &[]);
assert_eq!(unsigned_invoice_request.issuer(), None);
assert_eq!(unsigned_invoice_request.supported_quantity(), Quantity::One);
assert_eq!(unsigned_invoice_request.signing_pubkey(), recipient_pubkey());
assert_eq!(unsigned_invoice_request.signing_pubkey(), Some(recipient_pubkey()));
assert_eq!(unsigned_invoice_request.chain(), ChainHash::using_genesis_block(Network::Bitcoin));
assert_eq!(unsigned_invoice_request.amount_msats(), None);
assert_eq!(unsigned_invoice_request.invoice_request_features(), &InvoiceRequestFeatures::empty());
Expand DownExpand Up@@ -1265,7 +1301,7 @@ mod tests {
assert_eq!(invoice_request.paths(), &[]);
assert_eq!(invoice_request.issuer(), None);
assert_eq!(invoice_request.supported_quantity(), Quantity::One);
assert_eq!(invoice_request.signing_pubkey(), recipient_pubkey());
assert_eq!(invoice_request.signing_pubkey(), Some(recipient_pubkey()));
assert_eq!(invoice_request.chain(), ChainHash::using_genesis_block(Network::Bitcoin));
assert_eq!(invoice_request.amount_msats(), None);
assert_eq!(invoice_request.invoice_request_features(), &InvoiceRequestFeatures::empty());
Expand DownExpand Up@@ -1300,6 +1336,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
SignatureTlvStreamRef { signature: Some(&invoice_request.signature()) },
),
Expand DownExpand Up@@ -2260,7 +2297,7 @@ mod tests {
.amount_msats(1000)
.supported_quantity(Quantity::Unbounded)
.build().unwrap();
assert_eq!(offer.signing_pubkey(), node_id);
assert_eq!(offer.signing_pubkey(), Some(node_id));

let invoice_request = offer.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.chain(Network::Testnet).unwrap()
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions lightning/src/ln/channelmanager.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -8765,14 +8765,7 @@ where
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;

let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
if offer.paths().is_empty() {
let message = new_pending_onion_message(
OffersMessage::InvoiceRequest(invoice_request),
Destination::Node(offer.signing_pubkey()),
Some(reply_path),
);
pending_offers_messages.push(message);
} else {
if !offer.paths().is_empty() {
// Send as many invoice requests as there are paths in the offer (with an upper bound).
// Using only one path could result in a failure if the path no longer exists. But only
// one invoice for a given payment id will be paid, even if more than one is received.
Expand All@@ -8785,6 +8778,16 @@ where
);
pending_offers_messages.push(message);
}
} else if let Some(signing_pubkey) = offer.signing_pubkey() {
let message = new_pending_onion_message(
OffersMessage::InvoiceRequest(invoice_request),
Destination::Node(signing_pubkey),
Some(reply_path),
);
pending_offers_messages.push(message);
} else {
debug_assert!(false);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its usually worth having a comment on these kinds of assertions as to why we think its not reachable.

return Err(Bolt12SemanticError::MissingSigningPubkey);
}

Ok(())
Expand Down
12 changes: 6 additions & 6 deletions lightning/src/ln/offers_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -271,7 +271,7 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_ne!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand All@@ -286,7 +286,7 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand DownExpand Up@@ -336,7 +336,7 @@ fn prefers_more_connected_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(nodes[4].node.get_our_node_id()));
Expand DownExpand Up@@ -386,7 +386,7 @@ fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
.unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), alice_id);
assert_ne!(offer.signing_pubkey(), Some(alice_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand DownExpand Up@@ -547,7 +547,7 @@ fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), alice_id);
assert_ne!(offer.signing_pubkey(), Some(alice_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
Expand DownExpand Up@@ -672,7 +672,7 @@ fn pays_for_offer_without_blinded_paths() {
.clear_paths()
.amount_msats(10_000_000)
.build().unwrap();
assert_eq!(offer.signing_pubkey(), alice_id);
assert_eq!(offer.signing_pubkey(), Some(alice_id));
assert!(offer.paths().is_empty());

let payment_id = PaymentId([1; 32]);
Expand Down
104 changes: 97 additions & 7 deletions lightning/src/offers/invoice.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -210,10 +210,9 @@ macro_rules! invoice_explicit_signing_pubkey_builder_methods { ($self: ident, $s
#[cfg_attr(c_bindings, allow(dead_code))]
pub(super) fn for_offer(
invoice_request: &'a InvoiceRequest, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>,
created_at: Duration, payment_hash: PaymentHash
created_at: Duration, payment_hash: PaymentHash, signing_pubkey: PublicKey
) -> Result<Self, Bolt12SemanticError> {
let amount_msats = Self::amount_msats(invoice_request)?;
let signing_pubkey = invoice_request.contents.inner.offer.signing_pubkey();
let contents = InvoiceContents::ForOffer {
invoice_request: invoice_request.contents.clone(),
fields: Self::fields(
Expand DownExpand Up@@ -272,7 +271,7 @@ macro_rules! invoice_derived_signing_pubkey_builder_methods { ($self: ident, $se
created_at: Duration, payment_hash: PaymentHash, keys: KeyPair
) -> Result<Self, Bolt12SemanticError> {
let amount_msats = Self::amount_msats(invoice_request)?;
let signing_pubkey = invoice_request.contents.inner.offer.signing_pubkey();
let signing_pubkey = keys.public_key();
let contents = InvoiceContents::ForOffer {
invoice_request: invoice_request.contents.clone(),
fields: Self::fields(
Expand DownExpand Up@@ -1435,8 +1434,8 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
features, signing_pubkey,
};

match offer_tlv_stream.node_id {
Some(expected_signing_pubkey) => {
match (offer_tlv_stream.node_id, &offer_tlv_stream.paths) {
(Some(expected_signing_pubkey), _) => {
if fields.signing_pubkey != expected_signing_pubkey {
return Err(Bolt12SemanticError::InvalidSigningPubkey);
}
Expand All@@ -1446,7 +1445,21 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
)?;
Ok(InvoiceContents::ForOffer { invoice_request, fields })
},
None => {
(None, Some(paths)) => {
if !paths
.iter()
.filter_map(|path| path.blinded_hops.last())
.any(|last_hop| fields.signing_pubkey == last_hop.blinded_node_id)
Comment on lines +1450 to +1452

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the spec, it looks like we MUST verify that the signing pubkey matches a path we sent an invoice request to. I'm not certain it matters or if the spec should change but it looks like we don't specifically check that at the moment.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is a slight deviation as we don't have the blinded node id for the path that the invoice was sent over when parsing. We could do some sort of check at the handler level, but even that would require piping that data through.

{
return Err(Bolt12SemanticError::InvalidSigningPubkey);
}

let invoice_request = InvoiceRequestContents::try_from(
(payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream)
)?;
Ok(InvoiceContents::ForOffer { invoice_request, fields })
},
(None, None) => {
let refund = RefundContents::try_from(
(payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream)
)?;
Expand All@@ -1464,7 +1477,7 @@ mod tests {
use bitcoin::blockdata::script::ScriptBuf;
use bitcoin::hashes::Hash;
use bitcoin::network::constants::Network;
use bitcoin::secp256k1::{Message, Secp256k1, XOnlyPublicKey, self};
use bitcoin::secp256k1::{KeyPair, Message, Secp256k1, SecretKey, XOnlyPublicKey, self};
use bitcoin::address::{Address, Payload, WitnessProgram, WitnessVersion};
use bitcoin::key::TweakedPublicKey;

Expand DownExpand Up@@ -1633,6 +1646,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
InvoiceTlvStreamRef {
paths: Some(Iterable(payment_paths.iter().map(|(_, path)| path))),
Expand DownExpand Up@@ -1725,6 +1739,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
InvoiceTlvStreamRef {
paths: Some(Iterable(payment_paths.iter().map(|(_, path)| path))),
Expand DownExpand Up@@ -2365,6 +2380,81 @@ mod tests {
}
}

#[test]
fn parses_invoice_with_node_id_from_blinded_path() {
let paths = vec![
BlindedPath {
introduction_node: IntroductionNode::NodeId(pubkey(40)),
blinding_point: pubkey(41),
blinded_hops: vec![
BlindedHop { blinded_node_id: pubkey(43), encrypted_payload: vec![0; 43] },
BlindedHop { blinded_node_id: pubkey(44), encrypted_payload: vec![0; 44] },
],
},
BlindedPath {
introduction_node: IntroductionNode::NodeId(pubkey(40)),
blinding_point: pubkey(41),
blinded_hops: vec![
BlindedHop { blinded_node_id: pubkey(45), encrypted_payload: vec![0; 45] },
BlindedHop { blinded_node_id: pubkey(46), encrypted_payload: vec![0; 46] },
],
},
];

let blinded_node_id_sign = |message: &UnsignedBolt12Invoice| {
let secp_ctx = Secp256k1::new();
let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[46; 32]).unwrap());
Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
};

let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
.clear_signing_pubkey()
.amount_msats(1000)
.path(paths[0].clone())
.path(paths[1].clone())
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.build().unwrap()
.sign(payer_sign).unwrap()
.respond_with_no_std_using_signing_pubkey(
payment_paths(), payment_hash(), now(), pubkey(46)
).unwrap()
.build().unwrap()
.sign(blinded_node_id_sign).unwrap();

let mut buffer = Vec::new();
invoice.write(&mut buffer).unwrap();

if let Err(e) = Bolt12Invoice::try_from(buffer) {
panic!("error parsing invoice: {:?}", e);
}

let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
.clear_signing_pubkey()
.amount_msats(1000)
.path(paths[0].clone())
.path(paths[1].clone())
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.build().unwrap()
.sign(payer_sign).unwrap()
.respond_with_no_std_using_signing_pubkey(
payment_paths(), payment_hash(), now(), recipient_pubkey()
).unwrap()
.build().unwrap()
.sign(recipient_sign).unwrap();

let mut buffer = Vec::new();
invoice.write(&mut buffer).unwrap();

match Bolt12Invoice::try_from(buffer) {
Ok(_) => panic!("expected error"),
Err(e) => {
assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::InvalidSigningPubkey));
},
}
}

#[test]
fn fails_parsing_invoice_without_signature() {
let mut buffer = Vec::new();
Expand Down
47 changes: 42 additions & 5 deletions lightning/src/offers/invoice_request.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -747,7 +747,27 @@ macro_rules! invoice_request_respond_with_explicit_signing_pubkey_methods { (
return Err(Bolt12SemanticError::UnknownRequiredFeatures);
}

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash)
let signing_pubkey = match $contents.contents.inner.offer.signing_pubkey() {
Some(signing_pubkey) => signing_pubkey,
None => return Err(Bolt12SemanticError::MissingSigningPubkey),
};

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash, signing_pubkey)
}

#[cfg(test)]
#[allow(dead_code)]
pub(super) fn respond_with_no_std_using_signing_pubkey(
&$self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
created_at: core::time::Duration, signing_pubkey: PublicKey
) -> Result<$builder, Bolt12SemanticError> {
debug_assert!($contents.contents.inner.offer.signing_pubkey().is_none());

if $contents.invoice_request_features().requires_unknown_bits() {
return Err(Bolt12SemanticError::UnknownRequiredFeatures);
}

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash, signing_pubkey)
}
} }

Expand DownExpand Up@@ -855,6 +875,11 @@ macro_rules! invoice_request_respond_with_derived_signing_pubkey_methods { (
Some(keys) => keys,
};

match $contents.contents.inner.offer.signing_pubkey() {
Some(signing_pubkey) => debug_assert_eq!(signing_pubkey, keys.public_key()),
None => return Err(Bolt12SemanticError::MissingSigningPubkey),
}

<$builder>::for_offer_using_keys(
&$self.inner, payment_paths, created_at, payment_hash, keys
)
Expand DownExpand Up@@ -961,6 +986,7 @@ impl InvoiceRequestContentsWithoutPayerId {
quantity: self.quantity,
payer_id: None,
payer_note: self.payer_note.as_ref(),
paths: None,
};

(payer, offer, invoice_request)
Expand DownExpand Up@@ -993,13 +1019,17 @@ pub(super) const INVOICE_REQUEST_TYPES: core::ops::Range<u64> = 80..160;
/// [`Refund::payer_id`]: crate::offers::refund::Refund::payer_id
pub(super) const INVOICE_REQUEST_PAYER_ID_TYPE: u64 = 88;

// This TLV stream is used for both InvoiceRequest and Refund, but not all TLV records are valid for
// InvoiceRequest as noted below.
tlv_stream!(InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef, INVOICE_REQUEST_TYPES, {
(80, chain: ChainHash),
(82, amount: (u64, HighZeroBytesDroppedBigSize)),
(84, features: (InvoiceRequestFeatures, WithoutLength)),
(86, quantity: (u64, HighZeroBytesDroppedBigSize)),
(INVOICE_REQUEST_PAYER_ID_TYPE, payer_id: PublicKey),
(89, payer_note: (String, WithoutLength)),
// Only used for Refund since the onion message of an InvoiceRequest has a reply path.
(90, paths: (Vec<BlindedPath>, WithoutLength)),
Comment thread
jkczyz marked this conversation as resolved.
});

type FullInvoiceRequestTlvStream =
Expand DownExpand Up@@ -1082,7 +1112,9 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
let (
PayerTlvStream { metadata },
offer_tlv_stream,
InvoiceRequestTlvStream { chain, amount, features, quantity, payer_id, payer_note },
InvoiceRequestTlvStream {
chain, amount, features, quantity, payer_id, payer_note, paths,
},
) = tlv_stream;

let payer = match metadata {
Expand All@@ -1109,6 +1141,10 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
Some(payer_id) => payer_id,
};

if paths.is_some() {
return Err(Bolt12SemanticError::UnexpectedPaths);
}

Ok(InvoiceRequestContents {
inner: InvoiceRequestContentsWithoutPayerId {
payer, offer, chain, amount_msats: amount, features, quantity, payer_note,
Expand DownExpand Up@@ -1233,7 +1269,7 @@ mod tests {
assert_eq!(unsigned_invoice_request.paths(), &[]);
assert_eq!(unsigned_invoice_request.issuer(), None);
assert_eq!(unsigned_invoice_request.supported_quantity(), Quantity::One);
assert_eq!(unsigned_invoice_request.signing_pubkey(), recipient_pubkey());
assert_eq!(unsigned_invoice_request.signing_pubkey(), Some(recipient_pubkey()));
assert_eq!(unsigned_invoice_request.chain(), ChainHash::using_genesis_block(Network::Bitcoin));
assert_eq!(unsigned_invoice_request.amount_msats(), None);
assert_eq!(unsigned_invoice_request.invoice_request_features(), &InvoiceRequestFeatures::empty());
Expand DownExpand Up@@ -1265,7 +1301,7 @@ mod tests {
assert_eq!(invoice_request.paths(), &[]);
assert_eq!(invoice_request.issuer(), None);
assert_eq!(invoice_request.supported_quantity(), Quantity::One);
assert_eq!(invoice_request.signing_pubkey(), recipient_pubkey());
assert_eq!(invoice_request.signing_pubkey(), Some(recipient_pubkey()));
assert_eq!(invoice_request.chain(), ChainHash::using_genesis_block(Network::Bitcoin));
assert_eq!(invoice_request.amount_msats(), None);
assert_eq!(invoice_request.invoice_request_features(), &InvoiceRequestFeatures::empty());
Expand DownExpand Up@@ -1300,6 +1336,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
SignatureTlvStreamRef { signature: Some(&invoice_request.signature()) },
),
Expand DownExpand Up@@ -2260,7 +2297,7 @@ mod tests {
.amount_msats(1000)
.supported_quantity(Quantity::Unbounded)
.build().unwrap();
assert_eq!(offer.signing_pubkey(), node_id);
assert_eq!(offer.signing_pubkey(), Some(node_id));

let invoice_request = offer.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.chain(Network::Testnet).unwrap()
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions lightning/src/ln/channelmanager.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -8765,14 +8765,7 @@ where
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;

let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
if offer.paths().is_empty() {
let message = new_pending_onion_message(
OffersMessage::InvoiceRequest(invoice_request),
Destination::Node(offer.signing_pubkey()),
Some(reply_path),
);
pending_offers_messages.push(message);
} else {
if !offer.paths().is_empty() {
// Send as many invoice requests as there are paths in the offer (with an upper bound).
// Using only one path could result in a failure if the path no longer exists. But only
// one invoice for a given payment id will be paid, even if more than one is received.
Expand All@@ -8785,6 +8778,16 @@ where
);
pending_offers_messages.push(message);
}
} else if let Some(signing_pubkey) = offer.signing_pubkey() {
let message = new_pending_onion_message(
OffersMessage::InvoiceRequest(invoice_request),
Destination::Node(signing_pubkey),
Some(reply_path),
);
pending_offers_messages.push(message);
} else {
debug_assert!(false);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its usually worth having a comment on these kinds of assertions as to why we think its not reachable.

return Err(Bolt12SemanticError::MissingSigningPubkey);
}

Ok(())
Expand Down
12 changes: 6 additions & 6 deletions lightning/src/ln/offers_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -271,7 +271,7 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_ne!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand All@@ -286,7 +286,7 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand DownExpand Up@@ -336,7 +336,7 @@ fn prefers_more_connected_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(nodes[4].node.get_our_node_id()));
Expand DownExpand Up@@ -386,7 +386,7 @@ fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
.unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), alice_id);
assert_ne!(offer.signing_pubkey(), Some(alice_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand DownExpand Up@@ -547,7 +547,7 @@ fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), alice_id);
assert_ne!(offer.signing_pubkey(), Some(alice_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
Expand DownExpand Up@@ -672,7 +672,7 @@ fn pays_for_offer_without_blinded_paths() {
.clear_paths()
.amount_msats(10_000_000)
.build().unwrap();
assert_eq!(offer.signing_pubkey(), alice_id);
assert_eq!(offer.signing_pubkey(), Some(alice_id));
assert!(offer.paths().is_empty());

let payment_id = PaymentId([1; 32]);
Expand Down
104 changes: 97 additions & 7 deletions lightning/src/offers/invoice.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -210,10 +210,9 @@ macro_rules! invoice_explicit_signing_pubkey_builder_methods { ($self: ident, $s
#[cfg_attr(c_bindings, allow(dead_code))]
pub(super) fn for_offer(
invoice_request: &'a InvoiceRequest, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>,
created_at: Duration, payment_hash: PaymentHash
created_at: Duration, payment_hash: PaymentHash, signing_pubkey: PublicKey
) -> Result<Self, Bolt12SemanticError> {
let amount_msats = Self::amount_msats(invoice_request)?;
let signing_pubkey = invoice_request.contents.inner.offer.signing_pubkey();
let contents = InvoiceContents::ForOffer {
invoice_request: invoice_request.contents.clone(),
fields: Self::fields(
Expand DownExpand Up@@ -272,7 +271,7 @@ macro_rules! invoice_derived_signing_pubkey_builder_methods { ($self: ident, $se
created_at: Duration, payment_hash: PaymentHash, keys: KeyPair
) -> Result<Self, Bolt12SemanticError> {
let amount_msats = Self::amount_msats(invoice_request)?;
let signing_pubkey = invoice_request.contents.inner.offer.signing_pubkey();
let signing_pubkey = keys.public_key();
let contents = InvoiceContents::ForOffer {
invoice_request: invoice_request.contents.clone(),
fields: Self::fields(
Expand DownExpand Up@@ -1435,8 +1434,8 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
features, signing_pubkey,
};

match offer_tlv_stream.node_id {
Some(expected_signing_pubkey) => {
match (offer_tlv_stream.node_id, &offer_tlv_stream.paths) {
(Some(expected_signing_pubkey), _) => {
if fields.signing_pubkey != expected_signing_pubkey {
return Err(Bolt12SemanticError::InvalidSigningPubkey);
}
Expand All@@ -1446,7 +1445,21 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
)?;
Ok(InvoiceContents::ForOffer { invoice_request, fields })
},
None => {
(None, Some(paths)) => {
if !paths
.iter()
.filter_map(|path| path.blinded_hops.last())
.any(|last_hop| fields.signing_pubkey == last_hop.blinded_node_id)
Comment on lines +1450 to +1452

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the spec, it looks like we MUST verify that the signing pubkey matches a path we sent an invoice request to. I'm not certain it matters or if the spec should change but it looks like we don't specifically check that at the moment.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is a slight deviation as we don't have the blinded node id for the path that the invoice was sent over when parsing. We could do some sort of check at the handler level, but even that would require piping that data through.

{
return Err(Bolt12SemanticError::InvalidSigningPubkey);
}

let invoice_request = InvoiceRequestContents::try_from(
(payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream)
)?;
Ok(InvoiceContents::ForOffer { invoice_request, fields })
},
(None, None) => {
let refund = RefundContents::try_from(
(payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream)
)?;
Expand All@@ -1464,7 +1477,7 @@ mod tests {
use bitcoin::blockdata::script::ScriptBuf;
use bitcoin::hashes::Hash;
use bitcoin::network::constants::Network;
use bitcoin::secp256k1::{Message, Secp256k1, XOnlyPublicKey, self};
use bitcoin::secp256k1::{KeyPair, Message, Secp256k1, SecretKey, XOnlyPublicKey, self};
use bitcoin::address::{Address, Payload, WitnessProgram, WitnessVersion};
use bitcoin::key::TweakedPublicKey;

Expand DownExpand Up@@ -1633,6 +1646,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
InvoiceTlvStreamRef {
paths: Some(Iterable(payment_paths.iter().map(|(_, path)| path))),
Expand DownExpand Up@@ -1725,6 +1739,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
InvoiceTlvStreamRef {
paths: Some(Iterable(payment_paths.iter().map(|(_, path)| path))),
Expand DownExpand Up@@ -2365,6 +2380,81 @@ mod tests {
}
}

#[test]
fn parses_invoice_with_node_id_from_blinded_path() {
let paths = vec![
BlindedPath {
introduction_node: IntroductionNode::NodeId(pubkey(40)),
blinding_point: pubkey(41),
blinded_hops: vec![
BlindedHop { blinded_node_id: pubkey(43), encrypted_payload: vec![0; 43] },
BlindedHop { blinded_node_id: pubkey(44), encrypted_payload: vec![0; 44] },
],
},
BlindedPath {
introduction_node: IntroductionNode::NodeId(pubkey(40)),
blinding_point: pubkey(41),
blinded_hops: vec![
BlindedHop { blinded_node_id: pubkey(45), encrypted_payload: vec![0; 45] },
BlindedHop { blinded_node_id: pubkey(46), encrypted_payload: vec![0; 46] },
],
},
];

let blinded_node_id_sign = |message: &UnsignedBolt12Invoice| {
let secp_ctx = Secp256k1::new();
let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[46; 32]).unwrap());
Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
};

let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
.clear_signing_pubkey()
.amount_msats(1000)
.path(paths[0].clone())
.path(paths[1].clone())
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.build().unwrap()
.sign(payer_sign).unwrap()
.respond_with_no_std_using_signing_pubkey(
payment_paths(), payment_hash(), now(), pubkey(46)
).unwrap()
.build().unwrap()
.sign(blinded_node_id_sign).unwrap();

let mut buffer = Vec::new();
invoice.write(&mut buffer).unwrap();

if let Err(e) = Bolt12Invoice::try_from(buffer) {
panic!("error parsing invoice: {:?}", e);
}

let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
.clear_signing_pubkey()
.amount_msats(1000)
.path(paths[0].clone())
.path(paths[1].clone())
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.build().unwrap()
.sign(payer_sign).unwrap()
.respond_with_no_std_using_signing_pubkey(
payment_paths(), payment_hash(), now(), recipient_pubkey()
).unwrap()
.build().unwrap()
.sign(recipient_sign).unwrap();

let mut buffer = Vec::new();
invoice.write(&mut buffer).unwrap();

match Bolt12Invoice::try_from(buffer) {
Ok(_) => panic!("expected error"),
Err(e) => {
assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::InvalidSigningPubkey));
},
}
}

#[test]
fn fails_parsing_invoice_without_signature() {
let mut buffer = Vec::new();
Expand Down
47 changes: 42 additions & 5 deletions lightning/src/offers/invoice_request.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -747,7 +747,27 @@ macro_rules! invoice_request_respond_with_explicit_signing_pubkey_methods { (
return Err(Bolt12SemanticError::UnknownRequiredFeatures);
}

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash)
let signing_pubkey = match $contents.contents.inner.offer.signing_pubkey() {
Some(signing_pubkey) => signing_pubkey,
None => return Err(Bolt12SemanticError::MissingSigningPubkey),
};

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash, signing_pubkey)
}

#[cfg(test)]
#[allow(dead_code)]
pub(super) fn respond_with_no_std_using_signing_pubkey(
&$self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
created_at: core::time::Duration, signing_pubkey: PublicKey
) -> Result<$builder, Bolt12SemanticError> {
debug_assert!($contents.contents.inner.offer.signing_pubkey().is_none());

if $contents.invoice_request_features().requires_unknown_bits() {
return Err(Bolt12SemanticError::UnknownRequiredFeatures);
}

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash, signing_pubkey)
}
} }

Expand DownExpand Up@@ -855,6 +875,11 @@ macro_rules! invoice_request_respond_with_derived_signing_pubkey_methods { (
Some(keys) => keys,
};

match $contents.contents.inner.offer.signing_pubkey() {
Some(signing_pubkey) => debug_assert_eq!(signing_pubkey, keys.public_key()),
None => return Err(Bolt12SemanticError::MissingSigningPubkey),
}

<$builder>::for_offer_using_keys(
&$self.inner, payment_paths, created_at, payment_hash, keys
)
Expand DownExpand Up@@ -961,6 +986,7 @@ impl InvoiceRequestContentsWithoutPayerId {
quantity: self.quantity,
payer_id: None,
payer_note: self.payer_note.as_ref(),
paths: None,
};

(payer, offer, invoice_request)
Expand DownExpand Up@@ -993,13 +1019,17 @@ pub(super) const INVOICE_REQUEST_TYPES: core::ops::Range<u64> = 80..160;
/// [`Refund::payer_id`]: crate::offers::refund::Refund::payer_id
pub(super) const INVOICE_REQUEST_PAYER_ID_TYPE: u64 = 88;

// This TLV stream is used for both InvoiceRequest and Refund, but not all TLV records are valid for
// InvoiceRequest as noted below.
tlv_stream!(InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef, INVOICE_REQUEST_TYPES, {
(80, chain: ChainHash),
(82, amount: (u64, HighZeroBytesDroppedBigSize)),
(84, features: (InvoiceRequestFeatures, WithoutLength)),
(86, quantity: (u64, HighZeroBytesDroppedBigSize)),
(INVOICE_REQUEST_PAYER_ID_TYPE, payer_id: PublicKey),
(89, payer_note: (String, WithoutLength)),
// Only used for Refund since the onion message of an InvoiceRequest has a reply path.
(90, paths: (Vec<BlindedPath>, WithoutLength)),
Comment thread
jkczyz marked this conversation as resolved.
});

type FullInvoiceRequestTlvStream =
Expand DownExpand Up@@ -1082,7 +1112,9 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
let (
PayerTlvStream { metadata },
offer_tlv_stream,
InvoiceRequestTlvStream { chain, amount, features, quantity, payer_id, payer_note },
InvoiceRequestTlvStream {
chain, amount, features, quantity, payer_id, payer_note, paths,
},
) = tlv_stream;

let payer = match metadata {
Expand All@@ -1109,6 +1141,10 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
Some(payer_id) => payer_id,
};

if paths.is_some() {
return Err(Bolt12SemanticError::UnexpectedPaths);
}

Ok(InvoiceRequestContents {
inner: InvoiceRequestContentsWithoutPayerId {
payer, offer, chain, amount_msats: amount, features, quantity, payer_note,
Expand DownExpand Up@@ -1233,7 +1269,7 @@ mod tests {
assert_eq!(unsigned_invoice_request.paths(), &[]);
assert_eq!(unsigned_invoice_request.issuer(), None);
assert_eq!(unsigned_invoice_request.supported_quantity(), Quantity::One);
assert_eq!(unsigned_invoice_request.signing_pubkey(), recipient_pubkey());
assert_eq!(unsigned_invoice_request.signing_pubkey(), Some(recipient_pubkey()));
assert_eq!(unsigned_invoice_request.chain(), ChainHash::using_genesis_block(Network::Bitcoin));
assert_eq!(unsigned_invoice_request.amount_msats(), None);
assert_eq!(unsigned_invoice_request.invoice_request_features(), &InvoiceRequestFeatures::empty());
Expand DownExpand Up@@ -1265,7 +1301,7 @@ mod tests {
assert_eq!(invoice_request.paths(), &[]);
assert_eq!(invoice_request.issuer(), None);
assert_eq!(invoice_request.supported_quantity(), Quantity::One);
assert_eq!(invoice_request.signing_pubkey(), recipient_pubkey());
assert_eq!(invoice_request.signing_pubkey(), Some(recipient_pubkey()));
assert_eq!(invoice_request.chain(), ChainHash::using_genesis_block(Network::Bitcoin));
assert_eq!(invoice_request.amount_msats(), None);
assert_eq!(invoice_request.invoice_request_features(), &InvoiceRequestFeatures::empty());
Expand DownExpand Up@@ -1300,6 +1336,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
SignatureTlvStreamRef { signature: Some(&invoice_request.signature()) },
),
Expand DownExpand Up@@ -2260,7 +2297,7 @@ mod tests {
.amount_msats(1000)
.supported_quantity(Quantity::Unbounded)
.build().unwrap();
assert_eq!(offer.signing_pubkey(), node_id);
assert_eq!(offer.signing_pubkey(), Some(node_id));

let invoice_request = offer.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.chain(Network::Testnet).unwrap()
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions lightning/src/ln/channelmanager.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -8765,14 +8765,7 @@ where
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;

let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
if offer.paths().is_empty() {
let message = new_pending_onion_message(
OffersMessage::InvoiceRequest(invoice_request),
Destination::Node(offer.signing_pubkey()),
Some(reply_path),
);
pending_offers_messages.push(message);
} else {
if !offer.paths().is_empty() {
// Send as many invoice requests as there are paths in the offer (with an upper bound).
// Using only one path could result in a failure if the path no longer exists. But only
// one invoice for a given payment id will be paid, even if more than one is received.
Expand All@@ -8785,6 +8778,16 @@ where
);
pending_offers_messages.push(message);
}
} else if let Some(signing_pubkey) = offer.signing_pubkey() {
let message = new_pending_onion_message(
OffersMessage::InvoiceRequest(invoice_request),
Destination::Node(signing_pubkey),
Some(reply_path),
);
pending_offers_messages.push(message);
} else {
debug_assert!(false);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its usually worth having a comment on these kinds of assertions as to why we think its not reachable.

return Err(Bolt12SemanticError::MissingSigningPubkey);
}

Ok(())
Expand Down
12 changes: 6 additions & 6 deletions lightning/src/ln/offers_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -271,7 +271,7 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_ne!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand All@@ -286,7 +286,7 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand DownExpand Up@@ -336,7 +336,7 @@ fn prefers_more_connected_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(nodes[4].node.get_our_node_id()));
Expand DownExpand Up@@ -386,7 +386,7 @@ fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
.unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), alice_id);
assert_ne!(offer.signing_pubkey(), Some(alice_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand DownExpand Up@@ -547,7 +547,7 @@ fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), alice_id);
assert_ne!(offer.signing_pubkey(), Some(alice_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
Expand DownExpand Up@@ -672,7 +672,7 @@ fn pays_for_offer_without_blinded_paths() {
.clear_paths()
.amount_msats(10_000_000)
.build().unwrap();
assert_eq!(offer.signing_pubkey(), alice_id);
assert_eq!(offer.signing_pubkey(), Some(alice_id));
assert!(offer.paths().is_empty());

let payment_id = PaymentId([1; 32]);
Expand Down
104 changes: 97 additions & 7 deletions lightning/src/offers/invoice.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -210,10 +210,9 @@ macro_rules! invoice_explicit_signing_pubkey_builder_methods { ($self: ident, $s
#[cfg_attr(c_bindings, allow(dead_code))]
pub(super) fn for_offer(
invoice_request: &'a InvoiceRequest, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>,
created_at: Duration, payment_hash: PaymentHash
created_at: Duration, payment_hash: PaymentHash, signing_pubkey: PublicKey
) -> Result<Self, Bolt12SemanticError> {
let amount_msats = Self::amount_msats(invoice_request)?;
let signing_pubkey = invoice_request.contents.inner.offer.signing_pubkey();
let contents = InvoiceContents::ForOffer {
invoice_request: invoice_request.contents.clone(),
fields: Self::fields(
Expand DownExpand Up@@ -272,7 +271,7 @@ macro_rules! invoice_derived_signing_pubkey_builder_methods { ($self: ident, $se
created_at: Duration, payment_hash: PaymentHash, keys: KeyPair
) -> Result<Self, Bolt12SemanticError> {
let amount_msats = Self::amount_msats(invoice_request)?;
let signing_pubkey = invoice_request.contents.inner.offer.signing_pubkey();
let signing_pubkey = keys.public_key();
let contents = InvoiceContents::ForOffer {
invoice_request: invoice_request.contents.clone(),
fields: Self::fields(
Expand DownExpand Up@@ -1435,8 +1434,8 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
features, signing_pubkey,
};

match offer_tlv_stream.node_id {
Some(expected_signing_pubkey) => {
match (offer_tlv_stream.node_id, &offer_tlv_stream.paths) {
(Some(expected_signing_pubkey), _) => {
if fields.signing_pubkey != expected_signing_pubkey {
return Err(Bolt12SemanticError::InvalidSigningPubkey);
}
Expand All@@ -1446,7 +1445,21 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
)?;
Ok(InvoiceContents::ForOffer { invoice_request, fields })
},
None => {
(None, Some(paths)) => {
if !paths
.iter()
.filter_map(|path| path.blinded_hops.last())
.any(|last_hop| fields.signing_pubkey == last_hop.blinded_node_id)
Comment on lines +1450 to +1452

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the spec, it looks like we MUST verify that the signing pubkey matches a path we sent an invoice request to. I'm not certain it matters or if the spec should change but it looks like we don't specifically check that at the moment.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is a slight deviation as we don't have the blinded node id for the path that the invoice was sent over when parsing. We could do some sort of check at the handler level, but even that would require piping that data through.

{
return Err(Bolt12SemanticError::InvalidSigningPubkey);
}

let invoice_request = InvoiceRequestContents::try_from(
(payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream)
)?;
Ok(InvoiceContents::ForOffer { invoice_request, fields })
},
(None, None) => {
let refund = RefundContents::try_from(
(payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream)
)?;
Expand All@@ -1464,7 +1477,7 @@ mod tests {
use bitcoin::blockdata::script::ScriptBuf;
use bitcoin::hashes::Hash;
use bitcoin::network::constants::Network;
use bitcoin::secp256k1::{Message, Secp256k1, XOnlyPublicKey, self};
use bitcoin::secp256k1::{KeyPair, Message, Secp256k1, SecretKey, XOnlyPublicKey, self};
use bitcoin::address::{Address, Payload, WitnessProgram, WitnessVersion};
use bitcoin::key::TweakedPublicKey;

Expand DownExpand Up@@ -1633,6 +1646,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
InvoiceTlvStreamRef {
paths: Some(Iterable(payment_paths.iter().map(|(_, path)| path))),
Expand DownExpand Up@@ -1725,6 +1739,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
InvoiceTlvStreamRef {
paths: Some(Iterable(payment_paths.iter().map(|(_, path)| path))),
Expand DownExpand Up@@ -2365,6 +2380,81 @@ mod tests {
}
}

#[test]
fn parses_invoice_with_node_id_from_blinded_path() {
let paths = vec![
BlindedPath {
introduction_node: IntroductionNode::NodeId(pubkey(40)),
blinding_point: pubkey(41),
blinded_hops: vec![
BlindedHop { blinded_node_id: pubkey(43), encrypted_payload: vec![0; 43] },
BlindedHop { blinded_node_id: pubkey(44), encrypted_payload: vec![0; 44] },
],
},
BlindedPath {
introduction_node: IntroductionNode::NodeId(pubkey(40)),
blinding_point: pubkey(41),
blinded_hops: vec![
BlindedHop { blinded_node_id: pubkey(45), encrypted_payload: vec![0; 45] },
BlindedHop { blinded_node_id: pubkey(46), encrypted_payload: vec![0; 46] },
],
},
];

let blinded_node_id_sign = |message: &UnsignedBolt12Invoice| {
let secp_ctx = Secp256k1::new();
let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[46; 32]).unwrap());
Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
};

let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
.clear_signing_pubkey()
.amount_msats(1000)
.path(paths[0].clone())
.path(paths[1].clone())
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.build().unwrap()
.sign(payer_sign).unwrap()
.respond_with_no_std_using_signing_pubkey(
payment_paths(), payment_hash(), now(), pubkey(46)
).unwrap()
.build().unwrap()
.sign(blinded_node_id_sign).unwrap();

let mut buffer = Vec::new();
invoice.write(&mut buffer).unwrap();

if let Err(e) = Bolt12Invoice::try_from(buffer) {
panic!("error parsing invoice: {:?}", e);
}

let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
.clear_signing_pubkey()
.amount_msats(1000)
.path(paths[0].clone())
.path(paths[1].clone())
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.build().unwrap()
.sign(payer_sign).unwrap()
.respond_with_no_std_using_signing_pubkey(
payment_paths(), payment_hash(), now(), recipient_pubkey()
).unwrap()
.build().unwrap()
.sign(recipient_sign).unwrap();

let mut buffer = Vec::new();
invoice.write(&mut buffer).unwrap();

match Bolt12Invoice::try_from(buffer) {
Ok(_) => panic!("expected error"),
Err(e) => {
assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::InvalidSigningPubkey));
},
}
}

#[test]
fn fails_parsing_invoice_without_signature() {
let mut buffer = Vec::new();
Expand Down
47 changes: 42 additions & 5 deletions lightning/src/offers/invoice_request.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -747,7 +747,27 @@ macro_rules! invoice_request_respond_with_explicit_signing_pubkey_methods { (
return Err(Bolt12SemanticError::UnknownRequiredFeatures);
}

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash)
let signing_pubkey = match $contents.contents.inner.offer.signing_pubkey() {
Some(signing_pubkey) => signing_pubkey,
None => return Err(Bolt12SemanticError::MissingSigningPubkey),
};

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash, signing_pubkey)
}

#[cfg(test)]
#[allow(dead_code)]
pub(super) fn respond_with_no_std_using_signing_pubkey(
&$self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
created_at: core::time::Duration, signing_pubkey: PublicKey
) -> Result<$builder, Bolt12SemanticError> {
debug_assert!($contents.contents.inner.offer.signing_pubkey().is_none());

if $contents.invoice_request_features().requires_unknown_bits() {
return Err(Bolt12SemanticError::UnknownRequiredFeatures);
}

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash, signing_pubkey)
}
} }

Expand DownExpand Up@@ -855,6 +875,11 @@ macro_rules! invoice_request_respond_with_derived_signing_pubkey_methods { (
Some(keys) => keys,
};

match $contents.contents.inner.offer.signing_pubkey() {
Some(signing_pubkey) => debug_assert_eq!(signing_pubkey, keys.public_key()),
None => return Err(Bolt12SemanticError::MissingSigningPubkey),
}

<$builder>::for_offer_using_keys(
&$self.inner, payment_paths, created_at, payment_hash, keys
)
Expand DownExpand Up@@ -961,6 +986,7 @@ impl InvoiceRequestContentsWithoutPayerId {
quantity: self.quantity,
payer_id: None,
payer_note: self.payer_note.as_ref(),
paths: None,
};

(payer, offer, invoice_request)
Expand DownExpand Up@@ -993,13 +1019,17 @@ pub(super) const INVOICE_REQUEST_TYPES: core::ops::Range<u64> = 80..160;
/// [`Refund::payer_id`]: crate::offers::refund::Refund::payer_id
pub(super) const INVOICE_REQUEST_PAYER_ID_TYPE: u64 = 88;

// This TLV stream is used for both InvoiceRequest and Refund, but not all TLV records are valid for
// InvoiceRequest as noted below.
tlv_stream!(InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef, INVOICE_REQUEST_TYPES, {
(80, chain: ChainHash),
(82, amount: (u64, HighZeroBytesDroppedBigSize)),
(84, features: (InvoiceRequestFeatures, WithoutLength)),
(86, quantity: (u64, HighZeroBytesDroppedBigSize)),
(INVOICE_REQUEST_PAYER_ID_TYPE, payer_id: PublicKey),
(89, payer_note: (String, WithoutLength)),
// Only used for Refund since the onion message of an InvoiceRequest has a reply path.
(90, paths: (Vec<BlindedPath>, WithoutLength)),
Comment thread
jkczyz marked this conversation as resolved.
});

type FullInvoiceRequestTlvStream =
Expand DownExpand Up@@ -1082,7 +1112,9 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
let (
PayerTlvStream { metadata },
offer_tlv_stream,
InvoiceRequestTlvStream { chain, amount, features, quantity, payer_id, payer_note },
InvoiceRequestTlvStream {
chain, amount, features, quantity, payer_id, payer_note, paths,
},
) = tlv_stream;

let payer = match metadata {
Expand All@@ -1109,6 +1141,10 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
Some(payer_id) => payer_id,
};

if paths.is_some() {
return Err(Bolt12SemanticError::UnexpectedPaths);
}

Ok(InvoiceRequestContents {
inner: InvoiceRequestContentsWithoutPayerId {
payer, offer, chain, amount_msats: amount, features, quantity, payer_note,
Expand DownExpand Up@@ -1233,7 +1269,7 @@ mod tests {
assert_eq!(unsigned_invoice_request.paths(), &[]);
assert_eq!(unsigned_invoice_request.issuer(), None);
assert_eq!(unsigned_invoice_request.supported_quantity(), Quantity::One);
assert_eq!(unsigned_invoice_request.signing_pubkey(), recipient_pubkey());
assert_eq!(unsigned_invoice_request.signing_pubkey(), Some(recipient_pubkey()));
assert_eq!(unsigned_invoice_request.chain(), ChainHash::using_genesis_block(Network::Bitcoin));
assert_eq!(unsigned_invoice_request.amount_msats(), None);
assert_eq!(unsigned_invoice_request.invoice_request_features(), &InvoiceRequestFeatures::empty());
Expand DownExpand Up@@ -1265,7 +1301,7 @@ mod tests {
assert_eq!(invoice_request.paths(), &[]);
assert_eq!(invoice_request.issuer(), None);
assert_eq!(invoice_request.supported_quantity(), Quantity::One);
assert_eq!(invoice_request.signing_pubkey(), recipient_pubkey());
assert_eq!(invoice_request.signing_pubkey(), Some(recipient_pubkey()));
assert_eq!(invoice_request.chain(), ChainHash::using_genesis_block(Network::Bitcoin));
assert_eq!(invoice_request.amount_msats(), None);
assert_eq!(invoice_request.invoice_request_features(), &InvoiceRequestFeatures::empty());
Expand DownExpand Up@@ -1300,6 +1336,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
SignatureTlvStreamRef { signature: Some(&invoice_request.signature()) },
),
Expand DownExpand Up@@ -2260,7 +2297,7 @@ mod tests {
.amount_msats(1000)
.supported_quantity(Quantity::Unbounded)
.build().unwrap();
assert_eq!(offer.signing_pubkey(), node_id);
assert_eq!(offer.signing_pubkey(), Some(node_id));

let invoice_request = offer.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.chain(Network::Testnet).unwrap()
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions lightning/src/ln/channelmanager.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -8765,14 +8765,7 @@ where
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;

let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
if offer.paths().is_empty() {
let message = new_pending_onion_message(
OffersMessage::InvoiceRequest(invoice_request),
Destination::Node(offer.signing_pubkey()),
Some(reply_path),
);
pending_offers_messages.push(message);
} else {
if !offer.paths().is_empty() {
// Send as many invoice requests as there are paths in the offer (with an upper bound).
// Using only one path could result in a failure if the path no longer exists. But only
// one invoice for a given payment id will be paid, even if more than one is received.
Expand All@@ -8785,6 +8778,16 @@ where
);
pending_offers_messages.push(message);
}
} else if let Some(signing_pubkey) = offer.signing_pubkey() {
let message = new_pending_onion_message(
OffersMessage::InvoiceRequest(invoice_request),
Destination::Node(signing_pubkey),
Some(reply_path),
);
pending_offers_messages.push(message);
} else {
debug_assert!(false);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its usually worth having a comment on these kinds of assertions as to why we think its not reachable.

return Err(Bolt12SemanticError::MissingSigningPubkey);
}

Ok(())
Expand Down
12 changes: 6 additions & 6 deletions lightning/src/ln/offers_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -271,7 +271,7 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_ne!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand All@@ -286,7 +286,7 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand DownExpand Up@@ -336,7 +336,7 @@ fn prefers_more_connected_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(nodes[4].node.get_our_node_id()));
Expand DownExpand Up@@ -386,7 +386,7 @@ fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
.unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), alice_id);
assert_ne!(offer.signing_pubkey(), Some(alice_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand DownExpand Up@@ -547,7 +547,7 @@ fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), alice_id);
assert_ne!(offer.signing_pubkey(), Some(alice_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
Expand DownExpand Up@@ -672,7 +672,7 @@ fn pays_for_offer_without_blinded_paths() {
.clear_paths()
.amount_msats(10_000_000)
.build().unwrap();
assert_eq!(offer.signing_pubkey(), alice_id);
assert_eq!(offer.signing_pubkey(), Some(alice_id));
assert!(offer.paths().is_empty());

let payment_id = PaymentId([1; 32]);
Expand Down
104 changes: 97 additions & 7 deletions lightning/src/offers/invoice.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -210,10 +210,9 @@ macro_rules! invoice_explicit_signing_pubkey_builder_methods { ($self: ident, $s
#[cfg_attr(c_bindings, allow(dead_code))]
pub(super) fn for_offer(
invoice_request: &'a InvoiceRequest, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>,
created_at: Duration, payment_hash: PaymentHash
created_at: Duration, payment_hash: PaymentHash, signing_pubkey: PublicKey
) -> Result<Self, Bolt12SemanticError> {
let amount_msats = Self::amount_msats(invoice_request)?;
let signing_pubkey = invoice_request.contents.inner.offer.signing_pubkey();
let contents = InvoiceContents::ForOffer {
invoice_request: invoice_request.contents.clone(),
fields: Self::fields(
Expand DownExpand Up@@ -272,7 +271,7 @@ macro_rules! invoice_derived_signing_pubkey_builder_methods { ($self: ident, $se
created_at: Duration, payment_hash: PaymentHash, keys: KeyPair
) -> Result<Self, Bolt12SemanticError> {
let amount_msats = Self::amount_msats(invoice_request)?;
let signing_pubkey = invoice_request.contents.inner.offer.signing_pubkey();
let signing_pubkey = keys.public_key();
let contents = InvoiceContents::ForOffer {
invoice_request: invoice_request.contents.clone(),
fields: Self::fields(
Expand DownExpand Up@@ -1435,8 +1434,8 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
features, signing_pubkey,
};

match offer_tlv_stream.node_id {
Some(expected_signing_pubkey) => {
match (offer_tlv_stream.node_id, &offer_tlv_stream.paths) {
(Some(expected_signing_pubkey), _) => {
if fields.signing_pubkey != expected_signing_pubkey {
return Err(Bolt12SemanticError::InvalidSigningPubkey);
}
Expand All@@ -1446,7 +1445,21 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
)?;
Ok(InvoiceContents::ForOffer { invoice_request, fields })
},
None => {
(None, Some(paths)) => {
if !paths
.iter()
.filter_map(|path| path.blinded_hops.last())
.any(|last_hop| fields.signing_pubkey == last_hop.blinded_node_id)
Comment on lines +1450 to +1452

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the spec, it looks like we MUST verify that the signing pubkey matches a path we sent an invoice request to. I'm not certain it matters or if the spec should change but it looks like we don't specifically check that at the moment.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is a slight deviation as we don't have the blinded node id for the path that the invoice was sent over when parsing. We could do some sort of check at the handler level, but even that would require piping that data through.

{
return Err(Bolt12SemanticError::InvalidSigningPubkey);
}

let invoice_request = InvoiceRequestContents::try_from(
(payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream)
)?;
Ok(InvoiceContents::ForOffer { invoice_request, fields })
},
(None, None) => {
let refund = RefundContents::try_from(
(payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream)
)?;
Expand All@@ -1464,7 +1477,7 @@ mod tests {
use bitcoin::blockdata::script::ScriptBuf;
use bitcoin::hashes::Hash;
use bitcoin::network::constants::Network;
use bitcoin::secp256k1::{Message, Secp256k1, XOnlyPublicKey, self};
use bitcoin::secp256k1::{KeyPair, Message, Secp256k1, SecretKey, XOnlyPublicKey, self};
use bitcoin::address::{Address, Payload, WitnessProgram, WitnessVersion};
use bitcoin::key::TweakedPublicKey;

Expand DownExpand Up@@ -1633,6 +1646,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
InvoiceTlvStreamRef {
paths: Some(Iterable(payment_paths.iter().map(|(_, path)| path))),
Expand DownExpand Up@@ -1725,6 +1739,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
InvoiceTlvStreamRef {
paths: Some(Iterable(payment_paths.iter().map(|(_, path)| path))),
Expand DownExpand Up@@ -2365,6 +2380,81 @@ mod tests {
}
}

#[test]
fn parses_invoice_with_node_id_from_blinded_path() {
let paths = vec![
BlindedPath {
introduction_node: IntroductionNode::NodeId(pubkey(40)),
blinding_point: pubkey(41),
blinded_hops: vec![
BlindedHop { blinded_node_id: pubkey(43), encrypted_payload: vec![0; 43] },
BlindedHop { blinded_node_id: pubkey(44), encrypted_payload: vec![0; 44] },
],
},
BlindedPath {
introduction_node: IntroductionNode::NodeId(pubkey(40)),
blinding_point: pubkey(41),
blinded_hops: vec![
BlindedHop { blinded_node_id: pubkey(45), encrypted_payload: vec![0; 45] },
BlindedHop { blinded_node_id: pubkey(46), encrypted_payload: vec![0; 46] },
],
},
];

let blinded_node_id_sign = |message: &UnsignedBolt12Invoice| {
let secp_ctx = Secp256k1::new();
let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[46; 32]).unwrap());
Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
};

let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
.clear_signing_pubkey()
.amount_msats(1000)
.path(paths[0].clone())
.path(paths[1].clone())
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.build().unwrap()
.sign(payer_sign).unwrap()
.respond_with_no_std_using_signing_pubkey(
payment_paths(), payment_hash(), now(), pubkey(46)
).unwrap()
.build().unwrap()
.sign(blinded_node_id_sign).unwrap();

let mut buffer = Vec::new();
invoice.write(&mut buffer).unwrap();

if let Err(e) = Bolt12Invoice::try_from(buffer) {
panic!("error parsing invoice: {:?}", e);
}

let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
.clear_signing_pubkey()
.amount_msats(1000)
.path(paths[0].clone())
.path(paths[1].clone())
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.build().unwrap()
.sign(payer_sign).unwrap()
.respond_with_no_std_using_signing_pubkey(
payment_paths(), payment_hash(), now(), recipient_pubkey()
).unwrap()
.build().unwrap()
.sign(recipient_sign).unwrap();

let mut buffer = Vec::new();
invoice.write(&mut buffer).unwrap();

match Bolt12Invoice::try_from(buffer) {
Ok(_) => panic!("expected error"),
Err(e) => {
assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::InvalidSigningPubkey));
},
}
}

#[test]
fn fails_parsing_invoice_without_signature() {
let mut buffer = Vec::new();
Expand Down
47 changes: 42 additions & 5 deletions lightning/src/offers/invoice_request.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -747,7 +747,27 @@ macro_rules! invoice_request_respond_with_explicit_signing_pubkey_methods { (
return Err(Bolt12SemanticError::UnknownRequiredFeatures);
}

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash)
let signing_pubkey = match $contents.contents.inner.offer.signing_pubkey() {
Some(signing_pubkey) => signing_pubkey,
None => return Err(Bolt12SemanticError::MissingSigningPubkey),
};

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash, signing_pubkey)
}

#[cfg(test)]
#[allow(dead_code)]
pub(super) fn respond_with_no_std_using_signing_pubkey(
&$self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
created_at: core::time::Duration, signing_pubkey: PublicKey
) -> Result<$builder, Bolt12SemanticError> {
debug_assert!($contents.contents.inner.offer.signing_pubkey().is_none());

if $contents.invoice_request_features().requires_unknown_bits() {
return Err(Bolt12SemanticError::UnknownRequiredFeatures);
}

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash, signing_pubkey)
}
} }

Expand DownExpand Up@@ -855,6 +875,11 @@ macro_rules! invoice_request_respond_with_derived_signing_pubkey_methods { (
Some(keys) => keys,
};

match $contents.contents.inner.offer.signing_pubkey() {
Some(signing_pubkey) => debug_assert_eq!(signing_pubkey, keys.public_key()),
None => return Err(Bolt12SemanticError::MissingSigningPubkey),
}

<$builder>::for_offer_using_keys(
&$self.inner, payment_paths, created_at, payment_hash, keys
)
Expand DownExpand Up@@ -961,6 +986,7 @@ impl InvoiceRequestContentsWithoutPayerId {
quantity: self.quantity,
payer_id: None,
payer_note: self.payer_note.as_ref(),
paths: None,
};

(payer, offer, invoice_request)
Expand DownExpand Up@@ -993,13 +1019,17 @@ pub(super) const INVOICE_REQUEST_TYPES: core::ops::Range<u64> = 80..160;
/// [`Refund::payer_id`]: crate::offers::refund::Refund::payer_id
pub(super) const INVOICE_REQUEST_PAYER_ID_TYPE: u64 = 88;

// This TLV stream is used for both InvoiceRequest and Refund, but not all TLV records are valid for
// InvoiceRequest as noted below.
tlv_stream!(InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef, INVOICE_REQUEST_TYPES, {
(80, chain: ChainHash),
(82, amount: (u64, HighZeroBytesDroppedBigSize)),
(84, features: (InvoiceRequestFeatures, WithoutLength)),
(86, quantity: (u64, HighZeroBytesDroppedBigSize)),
(INVOICE_REQUEST_PAYER_ID_TYPE, payer_id: PublicKey),
(89, payer_note: (String, WithoutLength)),
// Only used for Refund since the onion message of an InvoiceRequest has a reply path.
(90, paths: (Vec<BlindedPath>, WithoutLength)),
Comment thread
jkczyz marked this conversation as resolved.
});

type FullInvoiceRequestTlvStream =
Expand DownExpand Up@@ -1082,7 +1112,9 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
let (
PayerTlvStream { metadata },
offer_tlv_stream,
InvoiceRequestTlvStream { chain, amount, features, quantity, payer_id, payer_note },
InvoiceRequestTlvStream {
chain, amount, features, quantity, payer_id, payer_note, paths,
},
) = tlv_stream;

let payer = match metadata {
Expand All@@ -1109,6 +1141,10 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
Some(payer_id) => payer_id,
};

if paths.is_some() {
return Err(Bolt12SemanticError::UnexpectedPaths);
}

Ok(InvoiceRequestContents {
inner: InvoiceRequestContentsWithoutPayerId {
payer, offer, chain, amount_msats: amount, features, quantity, payer_note,
Expand DownExpand Up@@ -1233,7 +1269,7 @@ mod tests {
assert_eq!(unsigned_invoice_request.paths(), &[]);
assert_eq!(unsigned_invoice_request.issuer(), None);
assert_eq!(unsigned_invoice_request.supported_quantity(), Quantity::One);
assert_eq!(unsigned_invoice_request.signing_pubkey(), recipient_pubkey());
assert_eq!(unsigned_invoice_request.signing_pubkey(), Some(recipient_pubkey()));
assert_eq!(unsigned_invoice_request.chain(), ChainHash::using_genesis_block(Network::Bitcoin));
assert_eq!(unsigned_invoice_request.amount_msats(), None);
assert_eq!(unsigned_invoice_request.invoice_request_features(), &InvoiceRequestFeatures::empty());
Expand DownExpand Up@@ -1265,7 +1301,7 @@ mod tests {
assert_eq!(invoice_request.paths(), &[]);
assert_eq!(invoice_request.issuer(), None);
assert_eq!(invoice_request.supported_quantity(), Quantity::One);
assert_eq!(invoice_request.signing_pubkey(), recipient_pubkey());
assert_eq!(invoice_request.signing_pubkey(), Some(recipient_pubkey()));
assert_eq!(invoice_request.chain(), ChainHash::using_genesis_block(Network::Bitcoin));
assert_eq!(invoice_request.amount_msats(), None);
assert_eq!(invoice_request.invoice_request_features(), &InvoiceRequestFeatures::empty());
Expand DownExpand Up@@ -1300,6 +1336,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
SignatureTlvStreamRef { signature: Some(&invoice_request.signature()) },
),
Expand DownExpand Up@@ -2260,7 +2297,7 @@ mod tests {
.amount_msats(1000)
.supported_quantity(Quantity::Unbounded)
.build().unwrap();
assert_eq!(offer.signing_pubkey(), node_id);
assert_eq!(offer.signing_pubkey(), Some(node_id));

let invoice_request = offer.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.chain(Network::Testnet).unwrap()
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions lightning/src/ln/channelmanager.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -8765,14 +8765,7 @@ where
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;

let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
if offer.paths().is_empty() {
let message = new_pending_onion_message(
OffersMessage::InvoiceRequest(invoice_request),
Destination::Node(offer.signing_pubkey()),
Some(reply_path),
);
pending_offers_messages.push(message);
} else {
if !offer.paths().is_empty() {
// Send as many invoice requests as there are paths in the offer (with an upper bound).
// Using only one path could result in a failure if the path no longer exists. But only
// one invoice for a given payment id will be paid, even if more than one is received.
Expand All@@ -8785,6 +8778,16 @@ where
);
pending_offers_messages.push(message);
}
} else if let Some(signing_pubkey) = offer.signing_pubkey() {
let message = new_pending_onion_message(
OffersMessage::InvoiceRequest(invoice_request),
Destination::Node(signing_pubkey),
Some(reply_path),
);
pending_offers_messages.push(message);
} else {
debug_assert!(false);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its usually worth having a comment on these kinds of assertions as to why we think its not reachable.

return Err(Bolt12SemanticError::MissingSigningPubkey);
}

Ok(())
Expand Down
12 changes: 6 additions & 6 deletions lightning/src/ln/offers_tests.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -271,7 +271,7 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_ne!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand All@@ -286,7 +286,7 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand DownExpand Up@@ -336,7 +336,7 @@ fn prefers_more_connected_nodes_in_blinded_paths() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert_ne!(offer.signing_pubkey(), Some(bob_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(nodes[4].node.get_our_node_id()));
Expand DownExpand Up@@ -386,7 +386,7 @@ fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
.unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), alice_id);
assert_ne!(offer.signing_pubkey(), Some(alice_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
Expand DownExpand Up@@ -547,7 +547,7 @@ fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), alice_id);
assert_ne!(offer.signing_pubkey(), Some(alice_id));
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
Expand DownExpand Up@@ -672,7 +672,7 @@ fn pays_for_offer_without_blinded_paths() {
.clear_paths()
.amount_msats(10_000_000)
.build().unwrap();
assert_eq!(offer.signing_pubkey(), alice_id);
assert_eq!(offer.signing_pubkey(), Some(alice_id));
assert!(offer.paths().is_empty());

let payment_id = PaymentId([1; 32]);
Expand Down
104 changes: 97 additions & 7 deletions lightning/src/offers/invoice.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -210,10 +210,9 @@ macro_rules! invoice_explicit_signing_pubkey_builder_methods { ($self: ident, $s
#[cfg_attr(c_bindings, allow(dead_code))]
pub(super) fn for_offer(
invoice_request: &'a InvoiceRequest, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>,
created_at: Duration, payment_hash: PaymentHash
created_at: Duration, payment_hash: PaymentHash, signing_pubkey: PublicKey
) -> Result<Self, Bolt12SemanticError> {
let amount_msats = Self::amount_msats(invoice_request)?;
let signing_pubkey = invoice_request.contents.inner.offer.signing_pubkey();
let contents = InvoiceContents::ForOffer {
invoice_request: invoice_request.contents.clone(),
fields: Self::fields(
Expand DownExpand Up@@ -272,7 +271,7 @@ macro_rules! invoice_derived_signing_pubkey_builder_methods { ($self: ident, $se
created_at: Duration, payment_hash: PaymentHash, keys: KeyPair
) -> Result<Self, Bolt12SemanticError> {
let amount_msats = Self::amount_msats(invoice_request)?;
let signing_pubkey = invoice_request.contents.inner.offer.signing_pubkey();
let signing_pubkey = keys.public_key();
let contents = InvoiceContents::ForOffer {
invoice_request: invoice_request.contents.clone(),
fields: Self::fields(
Expand DownExpand Up@@ -1435,8 +1434,8 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
features, signing_pubkey,
};

match offer_tlv_stream.node_id {
Some(expected_signing_pubkey) => {
match (offer_tlv_stream.node_id, &offer_tlv_stream.paths) {
(Some(expected_signing_pubkey), _) => {
if fields.signing_pubkey != expected_signing_pubkey {
return Err(Bolt12SemanticError::InvalidSigningPubkey);
}
Expand All@@ -1446,7 +1445,21 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
)?;
Ok(InvoiceContents::ForOffer { invoice_request, fields })
},
None => {
(None, Some(paths)) => {
if !paths
.iter()
.filter_map(|path| path.blinded_hops.last())
.any(|last_hop| fields.signing_pubkey == last_hop.blinded_node_id)
Comment on lines +1450 to +1452

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the spec, it looks like we MUST verify that the signing pubkey matches a path we sent an invoice request to. I'm not certain it matters or if the spec should change but it looks like we don't specifically check that at the moment.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is a slight deviation as we don't have the blinded node id for the path that the invoice was sent over when parsing. We could do some sort of check at the handler level, but even that would require piping that data through.

{
return Err(Bolt12SemanticError::InvalidSigningPubkey);
}

let invoice_request = InvoiceRequestContents::try_from(
(payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream)
)?;
Ok(InvoiceContents::ForOffer { invoice_request, fields })
},
(None, None) => {
let refund = RefundContents::try_from(
(payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream)
)?;
Expand All@@ -1464,7 +1477,7 @@ mod tests {
use bitcoin::blockdata::script::ScriptBuf;
use bitcoin::hashes::Hash;
use bitcoin::network::constants::Network;
use bitcoin::secp256k1::{Message, Secp256k1, XOnlyPublicKey, self};
use bitcoin::secp256k1::{KeyPair, Message, Secp256k1, SecretKey, XOnlyPublicKey, self};
use bitcoin::address::{Address, Payload, WitnessProgram, WitnessVersion};
use bitcoin::key::TweakedPublicKey;

Expand DownExpand Up@@ -1633,6 +1646,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
InvoiceTlvStreamRef {
paths: Some(Iterable(payment_paths.iter().map(|(_, path)| path))),
Expand DownExpand Up@@ -1725,6 +1739,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
InvoiceTlvStreamRef {
paths: Some(Iterable(payment_paths.iter().map(|(_, path)| path))),
Expand DownExpand Up@@ -2365,6 +2380,81 @@ mod tests {
}
}

#[test]
fn parses_invoice_with_node_id_from_blinded_path() {
let paths = vec![
BlindedPath {
introduction_node: IntroductionNode::NodeId(pubkey(40)),
blinding_point: pubkey(41),
blinded_hops: vec![
BlindedHop { blinded_node_id: pubkey(43), encrypted_payload: vec![0; 43] },
BlindedHop { blinded_node_id: pubkey(44), encrypted_payload: vec![0; 44] },
],
},
BlindedPath {
introduction_node: IntroductionNode::NodeId(pubkey(40)),
blinding_point: pubkey(41),
blinded_hops: vec![
BlindedHop { blinded_node_id: pubkey(45), encrypted_payload: vec![0; 45] },
BlindedHop { blinded_node_id: pubkey(46), encrypted_payload: vec![0; 46] },
],
},
];

let blinded_node_id_sign = |message: &UnsignedBolt12Invoice| {
let secp_ctx = Secp256k1::new();
let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[46; 32]).unwrap());
Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
};

let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
.clear_signing_pubkey()
.amount_msats(1000)
.path(paths[0].clone())
.path(paths[1].clone())
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.build().unwrap()
.sign(payer_sign).unwrap()
.respond_with_no_std_using_signing_pubkey(
payment_paths(), payment_hash(), now(), pubkey(46)
).unwrap()
.build().unwrap()
.sign(blinded_node_id_sign).unwrap();

let mut buffer = Vec::new();
invoice.write(&mut buffer).unwrap();

if let Err(e) = Bolt12Invoice::try_from(buffer) {
panic!("error parsing invoice: {:?}", e);
}

let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
.clear_signing_pubkey()
.amount_msats(1000)
.path(paths[0].clone())
.path(paths[1].clone())
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.build().unwrap()
.sign(payer_sign).unwrap()
.respond_with_no_std_using_signing_pubkey(
payment_paths(), payment_hash(), now(), recipient_pubkey()
).unwrap()
.build().unwrap()
.sign(recipient_sign).unwrap();

let mut buffer = Vec::new();
invoice.write(&mut buffer).unwrap();

match Bolt12Invoice::try_from(buffer) {
Ok(_) => panic!("expected error"),
Err(e) => {
assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::InvalidSigningPubkey));
},
}
}

#[test]
fn fails_parsing_invoice_without_signature() {
let mut buffer = Vec::new();
Expand Down
47 changes: 42 additions & 5 deletions lightning/src/offers/invoice_request.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -747,7 +747,27 @@ macro_rules! invoice_request_respond_with_explicit_signing_pubkey_methods { (
return Err(Bolt12SemanticError::UnknownRequiredFeatures);
}

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash)
let signing_pubkey = match $contents.contents.inner.offer.signing_pubkey() {
Some(signing_pubkey) => signing_pubkey,
None => return Err(Bolt12SemanticError::MissingSigningPubkey),
};

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash, signing_pubkey)
}

#[cfg(test)]
#[allow(dead_code)]
pub(super) fn respond_with_no_std_using_signing_pubkey(
&$self, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, payment_hash: PaymentHash,
created_at: core::time::Duration, signing_pubkey: PublicKey
) -> Result<$builder, Bolt12SemanticError> {
debug_assert!($contents.contents.inner.offer.signing_pubkey().is_none());

if $contents.invoice_request_features().requires_unknown_bits() {
return Err(Bolt12SemanticError::UnknownRequiredFeatures);
}

<$builder>::for_offer(&$contents, payment_paths, created_at, payment_hash, signing_pubkey)
}
} }

Expand DownExpand Up@@ -855,6 +875,11 @@ macro_rules! invoice_request_respond_with_derived_signing_pubkey_methods { (
Some(keys) => keys,
};

match $contents.contents.inner.offer.signing_pubkey() {
Some(signing_pubkey) => debug_assert_eq!(signing_pubkey, keys.public_key()),
None => return Err(Bolt12SemanticError::MissingSigningPubkey),
}

<$builder>::for_offer_using_keys(
&$self.inner, payment_paths, created_at, payment_hash, keys
)
Expand DownExpand Up@@ -961,6 +986,7 @@ impl InvoiceRequestContentsWithoutPayerId {
quantity: self.quantity,
payer_id: None,
payer_note: self.payer_note.as_ref(),
paths: None,
};

(payer, offer, invoice_request)
Expand DownExpand Up@@ -993,13 +1019,17 @@ pub(super) const INVOICE_REQUEST_TYPES: core::ops::Range<u64> = 80..160;
/// [`Refund::payer_id`]: crate::offers::refund::Refund::payer_id
pub(super) const INVOICE_REQUEST_PAYER_ID_TYPE: u64 = 88;

// This TLV stream is used for both InvoiceRequest and Refund, but not all TLV records are valid for
// InvoiceRequest as noted below.
tlv_stream!(InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef, INVOICE_REQUEST_TYPES, {
(80, chain: ChainHash),
(82, amount: (u64, HighZeroBytesDroppedBigSize)),
(84, features: (InvoiceRequestFeatures, WithoutLength)),
(86, quantity: (u64, HighZeroBytesDroppedBigSize)),
(INVOICE_REQUEST_PAYER_ID_TYPE, payer_id: PublicKey),
(89, payer_note: (String, WithoutLength)),
// Only used for Refund since the onion message of an InvoiceRequest has a reply path.
(90, paths: (Vec<BlindedPath>, WithoutLength)),
Comment thread
jkczyz marked this conversation as resolved.
});

type FullInvoiceRequestTlvStream =
Expand DownExpand Up@@ -1082,7 +1112,9 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
let (
PayerTlvStream { metadata },
offer_tlv_stream,
InvoiceRequestTlvStream { chain, amount, features, quantity, payer_id, payer_note },
InvoiceRequestTlvStream {
chain, amount, features, quantity, payer_id, payer_note, paths,
},
) = tlv_stream;

let payer = match metadata {
Expand All@@ -1109,6 +1141,10 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
Some(payer_id) => payer_id,
};

if paths.is_some() {
return Err(Bolt12SemanticError::UnexpectedPaths);
}

Ok(InvoiceRequestContents {
inner: InvoiceRequestContentsWithoutPayerId {
payer, offer, chain, amount_msats: amount, features, quantity, payer_note,
Expand DownExpand Up@@ -1233,7 +1269,7 @@ mod tests {
assert_eq!(unsigned_invoice_request.paths(), &[]);
assert_eq!(unsigned_invoice_request.issuer(), None);
assert_eq!(unsigned_invoice_request.supported_quantity(), Quantity::One);
assert_eq!(unsigned_invoice_request.signing_pubkey(), recipient_pubkey());
assert_eq!(unsigned_invoice_request.signing_pubkey(), Some(recipient_pubkey()));
assert_eq!(unsigned_invoice_request.chain(), ChainHash::using_genesis_block(Network::Bitcoin));
assert_eq!(unsigned_invoice_request.amount_msats(), None);
assert_eq!(unsigned_invoice_request.invoice_request_features(), &InvoiceRequestFeatures::empty());
Expand DownExpand Up@@ -1265,7 +1301,7 @@ mod tests {
assert_eq!(invoice_request.paths(), &[]);
assert_eq!(invoice_request.issuer(), None);
assert_eq!(invoice_request.supported_quantity(), Quantity::One);
assert_eq!(invoice_request.signing_pubkey(), recipient_pubkey());
assert_eq!(invoice_request.signing_pubkey(), Some(recipient_pubkey()));
assert_eq!(invoice_request.chain(), ChainHash::using_genesis_block(Network::Bitcoin));
assert_eq!(invoice_request.amount_msats(), None);
assert_eq!(invoice_request.invoice_request_features(), &InvoiceRequestFeatures::empty());
Expand DownExpand Up@@ -1300,6 +1336,7 @@ mod tests {
quantity: None,
payer_id: Some(&payer_pubkey()),
payer_note: None,
paths: None,
},
SignatureTlvStreamRef { signature: Some(&invoice_request.signature()) },
),
Expand DownExpand Up@@ -2260,7 +2297,7 @@ mod tests {
.amount_msats(1000)
.supported_quantity(Quantity::Unbounded)
.build().unwrap();
assert_eq!(offer.signing_pubkey(), node_id);
assert_eq!(offer.signing_pubkey(), Some(node_id));

let invoice_request = offer.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.chain(Network::Testnet).unwrap()
Expand Down
Loading