Skip to content

Commit 099c7d5

Browse files
committed
Pass Nonce directly to OfferBuilder
When using OfferBuilder::deriving_signing_pubkey, the nonce generated needs to be the same one included in any OfferBuilder::paths. This is because the nonce is used along with the offer TLVs to derive a signing pubkey and will soon be elided from the metadata entirely.
1 parent a9a0227 commit 099c7d5

File tree

5 files changed

+48
-42
lines changed

5 files changed

+48
-42
lines changed

lightning/src/ln/channelmanager.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ use crate::ln::wire::Encode;
6363
use crate::offers::invoice::{BlindedPayInfo, Bolt12Invoice, DEFAULT_RELATIVE_EXPIRY, DerivedSigningPubkey, ExplicitSigningPubkey, InvoiceBuilder, UnsignedBolt12Invoice};
6464
use crate::offers::invoice_error::InvoiceError;
6565
use crate::offers::invoice_request::{DerivedPayerId, InvoiceRequestBuilder};
66+
use crate::offers::nonce::Nonce;
6667
use crate::offers::offer::{Offer, OfferBuilder};
6768
use crate::offers::parse::Bolt12SemanticError;
6869
use crate::offers::refund::{Refund, RefundBuilder};
@@ -8322,11 +8323,10 @@ macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {
83228323
let entropy = &*$self.entropy_source;
83238324
let secp_ctx = &$self.secp_ctx;
83248325

8326+
let nonce = Nonce::from_entropy_source(entropy);
83258327
let path = $self.create_blinded_path_using_absolute_expiry(absolute_expiry)
83268328
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
8327-
let builder = OfferBuilder::deriving_signing_pubkey(
8328-
node_id, expanded_key, entropy, secp_ctx
8329-
)
8329+
let builder = OfferBuilder::deriving_signing_pubkey(node_id, expanded_key, nonce, secp_ctx)
83308330
.chain_hash($self.chain_hash)
83318331
.path(path);
83328332

lightning/src/offers/invoice.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,7 @@ mod tests {
14161416
use crate::ln::msgs::DecodeError;
14171417
use crate::offers::invoice_request::InvoiceRequestTlvStreamRef;
14181418
use crate::offers::merkle::{SignError, SignatureTlvStreamRef, TaggedHash, self};
1419+
use crate::offers::nonce::Nonce;
14191420
use crate::offers::offer::{Amount, OfferTlvStreamRef, Quantity};
14201421
use crate::prelude::*;
14211422
#[cfg(not(c_bindings))]
@@ -1752,6 +1753,7 @@ mod tests {
17521753
let node_id = recipient_pubkey();
17531754
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
17541755
let entropy = FixedEntropy {};
1756+
let nonce = Nonce::from_entropy_source(&entropy);
17551757
let secp_ctx = Secp256k1::new();
17561758

17571759
let blinded_path = BlindedPath {
@@ -1765,8 +1767,7 @@ mod tests {
17651767

17661768
#[cfg(c_bindings)]
17671769
use crate::offers::offer::OfferWithDerivedMetadataBuilder as OfferBuilder;
1768-
let offer = OfferBuilder
1769-
::deriving_signing_pubkey(node_id, &expanded_key, &entropy, &secp_ctx)
1770+
let offer = OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, nonce, &secp_ctx)
17701771
.amount_msats(1000)
17711772
.path(blinded_path)
17721773
.build().unwrap();
@@ -1785,8 +1786,7 @@ mod tests {
17851786
let expanded_key = ExpandedKey::new(&KeyMaterial([41; 32]));
17861787
assert!(invoice_request.verify(&expanded_key, &secp_ctx).is_err());
17871788

1788-
let offer = OfferBuilder
1789-
::deriving_signing_pubkey(node_id, &expanded_key, &entropy, &secp_ctx)
1789+
let offer = OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, nonce, &secp_ctx)
17901790
.amount_msats(1000)
17911791
// Omit the path so that node_id is used for the signing pubkey instead of deriving
17921792
.build().unwrap();

lightning/src/offers/invoice_request.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,7 @@ mod tests {
12171217
use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
12181218
use crate::offers::invoice::{Bolt12Invoice, SIGNATURE_TAG as INVOICE_SIGNATURE_TAG};
12191219
use crate::offers::merkle::{SignError, SignatureTlvStreamRef, TaggedHash, self};
1220+
use crate::offers::nonce::Nonce;
12201221
use crate::offers::offer::{Amount, OfferTlvStreamRef, Quantity};
12211222
#[cfg(not(c_bindings))]
12221223
use {
@@ -2274,12 +2275,12 @@ mod tests {
22742275
let node_id = recipient_pubkey();
22752276
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
22762277
let entropy = FixedEntropy {};
2278+
let nonce = Nonce::from_entropy_source(&entropy);
22772279
let secp_ctx = Secp256k1::new();
22782280

22792281
#[cfg(c_bindings)]
22802282
use crate::offers::offer::OfferWithDerivedMetadataBuilder as OfferBuilder;
2281-
let offer = OfferBuilder
2282-
::deriving_signing_pubkey(node_id, &expanded_key, &entropy, &secp_ctx)
2283+
let offer = OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, nonce, &secp_ctx)
22832284
.chain(Network::Testnet)
22842285
.amount_msats(1000)
22852286
.supported_quantity(Quantity::Unbounded)

lightning/src/offers/offer.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,10 @@ macro_rules! offer_derived_metadata_builder_methods { ($secp_context: ty) => {
253253
///
254254
/// [`InvoiceRequest::verify`]: crate::offers::invoice_request::InvoiceRequest::verify
255255
/// [`ExpandedKey`]: crate::ln::inbound_payment::ExpandedKey
256-
pub fn deriving_signing_pubkey<ES: Deref>(
257-
node_id: PublicKey, expanded_key: &ExpandedKey, entropy_source: ES,
256+
pub fn deriving_signing_pubkey(
257+
node_id: PublicKey, expanded_key: &ExpandedKey, nonce: Nonce,
258258
secp_ctx: &'a Secp256k1<$secp_context>
259-
) -> Self where ES::Target: EntropySource {
260-
let nonce = Nonce::from_entropy_source(entropy_source);
259+
) -> Self {
261260
let derivation_material = MetadataMaterial::new(nonce, expanded_key, IV_BYTES, None);
262261
let metadata = Metadata::DerivedSigningPubkey(derivation_material);
263262
Self {
@@ -1163,6 +1162,7 @@ mod tests {
11631162
use crate::ln::features::OfferFeatures;
11641163
use crate::ln::inbound_payment::ExpandedKey;
11651164
use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
1165+
use crate::offers::nonce::Nonce;
11661166
use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError};
11671167
use crate::offers::test_utils::*;
11681168
use crate::util::ser::{BigSize, Writeable};
@@ -1277,12 +1277,12 @@ mod tests {
12771277
let node_id = recipient_pubkey();
12781278
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
12791279
let entropy = FixedEntropy {};
1280+
let nonce = Nonce::from_entropy_source(&entropy);
12801281
let secp_ctx = Secp256k1::new();
12811282

12821283
#[cfg(c_bindings)]
12831284
use super::OfferWithDerivedMetadataBuilder as OfferBuilder;
1284-
let offer = OfferBuilder
1285-
::deriving_signing_pubkey(node_id, &expanded_key, &entropy, &secp_ctx)
1285+
let offer = OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, nonce, &secp_ctx)
12861286
.amount_msats(1000)
12871287
.build().unwrap();
12881288
assert_eq!(offer.signing_pubkey(), Some(node_id));
@@ -1328,6 +1328,7 @@ mod tests {
13281328
let node_id = recipient_pubkey();
13291329
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
13301330
let entropy = FixedEntropy {};
1331+
let nonce = Nonce::from_entropy_source(&entropy);
13311332
let secp_ctx = Secp256k1::new();
13321333

13331334
let blinded_path = BlindedPath {
@@ -1341,8 +1342,7 @@ mod tests {
13411342

13421343
#[cfg(c_bindings)]
13431344
use super::OfferWithDerivedMetadataBuilder as OfferBuilder;
1344-
let offer = OfferBuilder
1345-
::deriving_signing_pubkey(node_id, &expanded_key, &entropy, &secp_ctx)
1345+
let offer = OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, nonce, &secp_ctx)
13461346
.amount_msats(1000)
13471347
.path(blinded_path)
13481348
.build().unwrap();

lightning/src/offers/static_invoice.rs

+30-25
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@ mod tests {
567567
use crate::offers::invoice::InvoiceTlvStreamRef;
568568
use crate::offers::merkle;
569569
use crate::offers::merkle::{SignatureTlvStreamRef, TaggedHash};
570+
use crate::offers::nonce::Nonce;
570571
use crate::offers::offer::{Offer, OfferBuilder, OfferTlvStreamRef, Quantity};
571572
use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError};
572573
use crate::offers::static_invoice::{
@@ -610,13 +611,13 @@ mod tests {
610611
let now = now();
611612
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
612613
let entropy = FixedEntropy {};
614+
let nonce = Nonce::from_entropy_source(&entropy);
613615
let secp_ctx = Secp256k1::new();
614616

615-
let offer =
616-
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, &entropy, &secp_ctx)
617-
.path(blinded_path())
618-
.build()
619-
.unwrap();
617+
let offer = OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, nonce, &secp_ctx)
618+
.path(blinded_path())
619+
.build()
620+
.unwrap();
620621

621622
StaticInvoiceBuilder::for_offer_using_derived_keys(
622623
&offer,
@@ -649,13 +650,13 @@ mod tests {
649650
let now = now();
650651
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
651652
let entropy = FixedEntropy {};
653+
let nonce = Nonce::from_entropy_source(&entropy);
652654
let secp_ctx = Secp256k1::new();
653655

654-
let offer =
655-
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, &entropy, &secp_ctx)
656-
.path(blinded_path())
657-
.build()
658-
.unwrap();
656+
let offer = OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, nonce, &secp_ctx)
657+
.path(blinded_path())
658+
.build()
659+
.unwrap();
659660

660661
let invoice = StaticInvoiceBuilder::for_offer_using_derived_keys(
661662
&offer,
@@ -744,13 +745,14 @@ mod tests {
744745
let now = now();
745746
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
746747
let entropy = FixedEntropy {};
748+
let nonce = Nonce::from_entropy_source(&entropy);
747749
let secp_ctx = Secp256k1::new();
748750

749751
let future_expiry = Duration::from_secs(u64::max_value());
750752
let past_expiry = Duration::from_secs(0);
751753

752754
let valid_offer =
753-
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, &entropy, &secp_ctx)
755+
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, nonce, &secp_ctx)
754756
.path(blinded_path())
755757
.absolute_expiry(future_expiry)
756758
.build()
@@ -771,7 +773,7 @@ mod tests {
771773
assert_eq!(invoice.absolute_expiry(), Some(future_expiry));
772774

773775
let expired_offer =
774-
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, &entropy, &secp_ctx)
776+
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, nonce, &secp_ctx)
775777
.path(blinded_path())
776778
.absolute_expiry(past_expiry)
777779
.build()
@@ -799,10 +801,11 @@ mod tests {
799801
let now = now();
800802
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
801803
let entropy = FixedEntropy {};
804+
let nonce = Nonce::from_entropy_source(&entropy);
802805
let secp_ctx = Secp256k1::new();
803806

804807
let valid_offer =
805-
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, &entropy, &secp_ctx)
808+
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, nonce, &secp_ctx)
806809
.path(blinded_path())
807810
.build()
808811
.unwrap();
@@ -862,10 +865,11 @@ mod tests {
862865
let now = now();
863866
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
864867
let entropy = FixedEntropy {};
868+
let nonce = Nonce::from_entropy_source(&entropy);
865869
let secp_ctx = Secp256k1::new();
866870

867871
let valid_offer =
868-
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, &entropy, &secp_ctx)
872+
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, nonce, &secp_ctx)
869873
.path(blinded_path())
870874
.build()
871875
.unwrap();
@@ -918,10 +922,11 @@ mod tests {
918922
let now = now();
919923
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
920924
let entropy = FixedEntropy {};
925+
let nonce = Nonce::from_entropy_source(&entropy);
921926
let secp_ctx = Secp256k1::new();
922927

923928
let offer_with_extra_chain =
924-
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, &entropy, &secp_ctx)
929+
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, nonce, &secp_ctx)
925930
.path(blinded_path())
926931
.chain(Network::Bitcoin)
927932
.chain(Network::Testnet)
@@ -949,13 +954,13 @@ mod tests {
949954
let now = now();
950955
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
951956
let entropy = FixedEntropy {};
957+
let nonce = Nonce::from_entropy_source(&entropy);
952958
let secp_ctx = Secp256k1::new();
953959

954-
let offer =
955-
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, &entropy, &secp_ctx)
956-
.path(blinded_path())
957-
.build()
958-
.unwrap();
960+
let offer = OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, nonce, &secp_ctx)
961+
.path(blinded_path())
962+
.build()
963+
.unwrap();
959964

960965
const TEST_RELATIVE_EXPIRY: u32 = 3600;
961966
let invoice = StaticInvoiceBuilder::for_offer_using_derived_keys(
@@ -990,13 +995,13 @@ mod tests {
990995
let now = now();
991996
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
992997
let entropy = FixedEntropy {};
998+
let nonce = Nonce::from_entropy_source(&entropy);
993999
let secp_ctx = Secp256k1::new();
9941000

995-
let offer =
996-
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, &entropy, &secp_ctx)
997-
.path(blinded_path())
998-
.build()
999-
.unwrap();
1001+
let offer = OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, nonce, &secp_ctx)
1002+
.path(blinded_path())
1003+
.build()
1004+
.unwrap();
10001005

10011006
let invoice = StaticInvoiceBuilder::for_offer_using_derived_keys(
10021007
&offer,

0 commit comments

Comments
 (0)