Skip to content

Commit cd9a904

Browse files
committed
WIP: Elide metadata from Offer with derived keys
1 parent c17d677 commit cd9a904

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

lightning/src/offers/invoice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1776,7 +1776,7 @@ mod tests {
17761776
.sign(payer_sign).unwrap();
17771777

17781778
if let Err(e) = invoice_request.clone()
1779-
.verify(&expanded_key, &secp_ctx).unwrap()
1779+
.verify_using_nonce(nonce, &expanded_key, &secp_ctx).unwrap()
17801780
.respond_using_derived_keys_no_std(payment_paths(), payment_hash(), now()).unwrap()
17811781
.build_and_sign(&secp_ctx)
17821782
{

lightning/src/offers/offer.rs

+9-15
Original file line numberDiff line numberDiff line change
@@ -399,13 +399,13 @@ macro_rules! offer_builder_methods { (
399399
}
400400

401401
let (derived_metadata, keys) = metadata.derive_from(tlv_stream, $self.secp_ctx);
402-
metadata = derived_metadata;
403-
if let Some(keys) = keys {
404-
$self.offer.signing_pubkey = Some(keys.public_key());
402+
match keys {
403+
Some(keys) => $self.offer.signing_pubkey = Some(keys.public_key()),
404+
None => $self.offer.metadata = Some(derived_metadata),
405405
}
406+
} else {
407+
$self.offer.metadata = Some(metadata);
406408
}
407-
408-
$self.offer.metadata = Some(metadata);
409409
}
410410

411411
let mut bytes = Vec::new();
@@ -666,9 +666,9 @@ impl Offer {
666666
}
667667

668668
pub(super) fn verify<T: secp256k1::Signing>(
669-
&self, key: &ExpandedKey, secp_ctx: &Secp256k1<T>
669+
&self, nonce: Nonce, key: &ExpandedKey, secp_ctx: &Secp256k1<T>
670670
) -> Result<(OfferId, Option<Keypair>), ()> {
671-
self.contents.verify(&self.bytes, key, secp_ctx)
671+
self.contents.verify_using_nonce(&self.bytes, nonce, key, secp_ctx)
672672
}
673673
}
674674

@@ -1295,6 +1295,7 @@ mod tests {
12951295
let offer = OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, nonce, &secp_ctx)
12961296
.amount_msats(1000)
12971297
.build().unwrap();
1298+
assert!(offer.metadata().is_some());
12981299
assert_eq!(offer.signing_pubkey(), Some(node_id));
12991300

13001301
let invoice_request = offer.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
@@ -1361,16 +1362,9 @@ mod tests {
13611362
.amount_msats(1000)
13621363
.path(blinded_path)
13631364
.build().unwrap();
1365+
assert!(offer.metadata().is_none());
13641366
assert_ne!(offer.signing_pubkey(), Some(node_id));
13651367

1366-
let invoice_request = offer.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1367-
.build().unwrap()
1368-
.sign(payer_sign).unwrap();
1369-
match invoice_request.verify(&expanded_key, &secp_ctx) {
1370-
Ok(invoice_request) => assert_eq!(invoice_request.offer_id, offer.id()),
1371-
Err(_) => panic!("unexpected error"),
1372-
}
1373-
13741368
let invoice_request = offer.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
13751369
.build().unwrap()
13761370
.sign(payer_sign).unwrap();

lightning/src/offers/static_invoice.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::offers::invoice_macros::{invoice_accessors_common, invoice_builder_me
2222
use crate::offers::merkle::{
2323
self, SignError, SignFn, SignatureTlvStream, SignatureTlvStreamRef, TaggedHash,
2424
};
25+
use crate::offers::nonce::Nonce;
2526
use crate::offers::offer::{
2627
Amount, Offer, OfferContents, OfferTlvStream, OfferTlvStreamRef, Quantity,
2728
};
@@ -99,7 +100,7 @@ impl<'a> StaticInvoiceBuilder<'a> {
99100
pub fn for_offer_using_derived_keys<T: secp256k1::Signing>(
100101
offer: &'a Offer, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>,
101102
message_paths: Vec<BlindedPath>, created_at: Duration, expanded_key: &ExpandedKey,
102-
secp_ctx: &Secp256k1<T>,
103+
nonce: Nonce, secp_ctx: &Secp256k1<T>,
103104
) -> Result<Self, Bolt12SemanticError> {
104105
if offer.chains().len() > 1 {
105106
return Err(Bolt12SemanticError::UnexpectedChain);
@@ -113,7 +114,7 @@ impl<'a> StaticInvoiceBuilder<'a> {
113114
offer.signing_pubkey().ok_or(Bolt12SemanticError::MissingSigningPubkey)?;
114115

115116
let keys = offer
116-
.verify(&expanded_key, &secp_ctx)
117+
.verify(nonce, &expanded_key, &secp_ctx)
117118
.map_err(|()| Bolt12SemanticError::InvalidMetadata)?
118119
.1
119120
.ok_or(Bolt12SemanticError::MissingSigningPubkey)?;
@@ -625,6 +626,7 @@ mod tests {
625626
vec![blinded_path()],
626627
now,
627628
&expanded_key,
629+
nonce,
628630
&secp_ctx,
629631
)
630632
.unwrap()
@@ -664,6 +666,7 @@ mod tests {
664666
vec![blinded_path()],
665667
now,
666668
&expanded_key,
669+
nonce,
667670
&secp_ctx,
668671
)
669672
.unwrap()
@@ -674,7 +677,7 @@ mod tests {
674677
invoice.write(&mut buffer).unwrap();
675678

676679
assert_eq!(invoice.bytes, buffer.as_slice());
677-
assert!(invoice.metadata().is_some());
680+
assert_eq!(invoice.metadata(), None);
678681
assert_eq!(invoice.amount(), None);
679682
assert_eq!(invoice.description(), None);
680683
assert_eq!(invoice.offer_features(), &OfferFeatures::empty());
@@ -700,13 +703,12 @@ mod tests {
700703
);
701704

702705
let paths = vec![blinded_path()];
703-
let metadata = vec![42; 16];
704706
assert_eq!(
705707
invoice.as_tlv_stream(),
706708
(
707709
OfferTlvStreamRef {
708710
chains: None,
709-
metadata: Some(&metadata),
711+
metadata: None,
710712
currency: None,
711713
amount: None,
712714
description: None,
@@ -764,6 +766,7 @@ mod tests {
764766
vec![blinded_path()],
765767
now,
766768
&expanded_key,
769+
nonce,
767770
&secp_ctx,
768771
)
769772
.unwrap()
@@ -784,6 +787,7 @@ mod tests {
784787
vec![blinded_path()],
785788
now,
786789
&expanded_key,
790+
nonce,
787791
&secp_ctx,
788792
)
789793
.unwrap()
@@ -817,6 +821,7 @@ mod tests {
817821
vec![blinded_path()],
818822
now,
819823
&expanded_key,
824+
nonce,
820825
&secp_ctx,
821826
) {
822827
assert_eq!(e, Bolt12SemanticError::MissingPaths);
@@ -831,6 +836,7 @@ mod tests {
831836
Vec::new(),
832837
now,
833838
&expanded_key,
839+
nonce,
834840
&secp_ctx,
835841
) {
836842
assert_eq!(e, Bolt12SemanticError::MissingPaths);
@@ -851,6 +857,7 @@ mod tests {
851857
vec![blinded_path()],
852858
now,
853859
&expanded_key,
860+
nonce,
854861
&secp_ctx,
855862
) {
856863
assert_eq!(e, Bolt12SemanticError::MissingPaths);
@@ -888,6 +895,7 @@ mod tests {
888895
vec![blinded_path()],
889896
now,
890897
&expanded_key,
898+
nonce,
891899
&secp_ctx,
892900
) {
893901
assert_eq!(e, Bolt12SemanticError::MissingSigningPubkey);
@@ -908,6 +916,7 @@ mod tests {
908916
vec![blinded_path()],
909917
now,
910918
&expanded_key,
919+
nonce,
911920
&secp_ctx,
912921
) {
913922
assert_eq!(e, Bolt12SemanticError::InvalidMetadata);
@@ -939,6 +948,7 @@ mod tests {
939948
vec![blinded_path()],
940949
now,
941950
&expanded_key,
951+
nonce,
942952
&secp_ctx,
943953
) {
944954
assert_eq!(e, Bolt12SemanticError::UnexpectedChain);
@@ -969,6 +979,7 @@ mod tests {
969979
vec![blinded_path()],
970980
now,
971981
&expanded_key,
982+
nonce,
972983
&secp_ctx,
973984
)
974985
.unwrap()
@@ -1009,6 +1020,7 @@ mod tests {
10091020
vec![blinded_path()],
10101021
now,
10111022
&expanded_key,
1023+
nonce,
10121024
&secp_ctx,
10131025
)
10141026
.unwrap()

0 commit comments

Comments
 (0)