Skip to content

Commit f6bd1eb

Browse files
BOLT 12 invoice: extract helper for invoice signing pubkey checks
Will be useful for static invoices.
1 parent b073711 commit f6bd1eb

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

lightning/src/offers/invoice.rs

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,37 +1337,18 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
13371337
features, signing_pubkey,
13381338
};
13391339

1340-
match (offer_tlv_stream.node_id, &offer_tlv_stream.paths) {
1341-
(Some(expected_signing_pubkey), _) => {
1342-
if fields.signing_pubkey != expected_signing_pubkey {
1343-
return Err(Bolt12SemanticError::InvalidSigningPubkey);
1344-
}
1345-
1346-
let invoice_request = InvoiceRequestContents::try_from(
1347-
(payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream)
1348-
)?;
1349-
Ok(InvoiceContents::ForOffer { invoice_request, fields })
1350-
},
1351-
(None, Some(paths)) => {
1352-
if !paths
1353-
.iter()
1354-
.filter_map(|path| path.blinded_hops.last())
1355-
.any(|last_hop| fields.signing_pubkey == last_hop.blinded_node_id)
1356-
{
1357-
return Err(Bolt12SemanticError::InvalidSigningPubkey);
1358-
}
1359-
1360-
let invoice_request = InvoiceRequestContents::try_from(
1361-
(payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream)
1362-
)?;
1363-
Ok(InvoiceContents::ForOffer { invoice_request, fields })
1364-
},
1365-
(None, None) => {
1366-
let refund = RefundContents::try_from(
1367-
(payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream)
1368-
)?;
1369-
Ok(InvoiceContents::ForRefund { refund, fields })
1370-
},
1340+
check_invoice_signing_pubkey(&fields.signing_pubkey, &offer_tlv_stream)?;
1341+
1342+
if offer_tlv_stream.node_id.is_none() && offer_tlv_stream.paths.is_none() {
1343+
let refund = RefundContents::try_from(
1344+
(payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream)
1345+
)?;
1346+
Ok(InvoiceContents::ForRefund { refund, fields })
1347+
} else {
1348+
let invoice_request = InvoiceRequestContents::try_from(
1349+
(payer_tlv_stream, offer_tlv_stream, invoice_request_tlv_stream)
1350+
)?;
1351+
Ok(InvoiceContents::ForOffer { invoice_request, fields })
13711352
}
13721353
}
13731354
}
@@ -1388,6 +1369,29 @@ pub(super) fn construct_payment_paths(
13881369
}
13891370
}
13901371

1372+
pub(super) fn check_invoice_signing_pubkey(
1373+
invoice_signing_pubkey: &PublicKey, offer_tlv_stream: &OfferTlvStream
1374+
) -> Result<(), Bolt12SemanticError> {
1375+
match (&offer_tlv_stream.node_id, &offer_tlv_stream.paths) {
1376+
(Some(expected_signing_pubkey), _) => {
1377+
if invoice_signing_pubkey != expected_signing_pubkey {
1378+
return Err(Bolt12SemanticError::InvalidSigningPubkey);
1379+
}
1380+
},
1381+
(None, Some(paths)) => {
1382+
if !paths
1383+
.iter()
1384+
.filter_map(|path| path.blinded_hops.last())
1385+
.any(|last_hop| invoice_signing_pubkey == &last_hop.blinded_node_id)
1386+
{
1387+
return Err(Bolt12SemanticError::InvalidSigningPubkey);
1388+
}
1389+
},
1390+
_ => {},
1391+
}
1392+
Ok(())
1393+
}
1394+
13911395
#[cfg(test)]
13921396
mod tests {
13931397
use super::{Bolt12Invoice, DEFAULT_RELATIVE_EXPIRY, FallbackAddress, FullInvoiceTlvStreamRef, InvoiceTlvStreamRef, SIGNATURE_TAG, UnsignedBolt12Invoice};

0 commit comments

Comments
 (0)