Skip to content

Commit 40f9e86

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 c25effb commit 40f9e86

File tree

5 files changed

+48
-42
lines changed

5 files changed

+48
-42
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 3 additions & 3 deletions
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};
@@ -8375,11 +8376,10 @@ macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {
83758376
let entropy = &*$self.entropy_source;
83768377
let secp_ctx = &$self.secp_ctx;
83778378

8379+
let nonce = Nonce::from_entropy_source(entropy);
83788380
let path = $self.create_blinded_path_using_absolute_expiry(absolute_expiry)
83798381
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
8380-
let builder = OfferBuilder::deriving_signing_pubkey(
8381-
node_id, expanded_key, entropy, secp_ctx
8382-
)
8382+
let builder = OfferBuilder::deriving_signing_pubkey(node_id, expanded_key, nonce, secp_ctx)
83838383
.chain_hash($self.chain_hash)
83848384
.path(path);
83858385

lightning/src/offers/invoice.rs

Lines changed: 4 additions & 4 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 8 additions & 8 deletions
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 {
@@ -1164,6 +1163,7 @@ mod tests {
11641163
use crate::ln::features::OfferFeatures;
11651164
use crate::ln::inbound_payment::ExpandedKey;
11661165
use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
1166+
use crate::offers::nonce::Nonce;
11671167
use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError};
11681168
use crate::offers::test_utils::*;
11691169
use crate::util::ser::{BigSize, Writeable};
@@ -1278,12 +1278,12 @@ mod tests {
12781278
let node_id = recipient_pubkey();
12791279
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
12801280
let entropy = FixedEntropy {};
1281+
let nonce = Nonce::from_entropy_source(&entropy);
12811282
let secp_ctx = Secp256k1::new();
12821283

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

13341335
let blinded_path = BlindedPath {
@@ -1342,8 +1343,7 @@ mod tests {
13421343

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

lightning/src/offers/static_invoice.rs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ mod tests {
565565
use crate::offers::invoice::InvoiceTlvStreamRef;
566566
use crate::offers::merkle;
567567
use crate::offers::merkle::{SignatureTlvStreamRef, TaggedHash};
568+
use crate::offers::nonce::Nonce;
568569
use crate::offers::offer::{Offer, OfferBuilder, OfferTlvStreamRef, Quantity};
569570
use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError};
570571
use crate::offers::static_invoice::{
@@ -608,13 +609,13 @@ mod tests {
608609
let now = now();
609610
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
610611
let entropy = FixedEntropy {};
612+
let nonce = Nonce::from_entropy_source(&entropy);
611613
let secp_ctx = Secp256k1::new();
612614

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

619620
StaticInvoiceBuilder::for_offer_using_derived_keys(
620621
&offer,
@@ -647,13 +648,13 @@ mod tests {
647648
let now = now();
648649
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
649650
let entropy = FixedEntropy {};
651+
let nonce = Nonce::from_entropy_source(&entropy);
650652
let secp_ctx = Secp256k1::new();
651653

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

658659
let invoice = StaticInvoiceBuilder::for_offer_using_derived_keys(
659660
&offer,
@@ -742,13 +743,14 @@ mod tests {
742743
let now = now();
743744
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
744745
let entropy = FixedEntropy {};
746+
let nonce = Nonce::from_entropy_source(&entropy);
745747
let secp_ctx = Secp256k1::new();
746748

747749
let future_expiry = Duration::from_secs(u64::max_value());
748750
let past_expiry = Duration::from_secs(0);
749751

750752
let valid_offer =
751-
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, &entropy, &secp_ctx)
753+
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, nonce, &secp_ctx)
752754
.path(blinded_path())
753755
.absolute_expiry(future_expiry)
754756
.build()
@@ -769,7 +771,7 @@ mod tests {
769771
assert_eq!(invoice.absolute_expiry(), Some(future_expiry));
770772

771773
let expired_offer =
772-
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, &entropy, &secp_ctx)
774+
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, nonce, &secp_ctx)
773775
.path(blinded_path())
774776
.absolute_expiry(past_expiry)
775777
.build()
@@ -797,10 +799,11 @@ mod tests {
797799
let now = now();
798800
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
799801
let entropy = FixedEntropy {};
802+
let nonce = Nonce::from_entropy_source(&entropy);
800803
let secp_ctx = Secp256k1::new();
801804

802805
let valid_offer =
803-
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, &entropy, &secp_ctx)
806+
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, nonce, &secp_ctx)
804807
.path(blinded_path())
805808
.build()
806809
.unwrap();
@@ -860,10 +863,11 @@ mod tests {
860863
let now = now();
861864
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
862865
let entropy = FixedEntropy {};
866+
let nonce = Nonce::from_entropy_source(&entropy);
863867
let secp_ctx = Secp256k1::new();
864868

865869
let valid_offer =
866-
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, &entropy, &secp_ctx)
870+
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, nonce, &secp_ctx)
867871
.path(blinded_path())
868872
.build()
869873
.unwrap();
@@ -916,10 +920,11 @@ mod tests {
916920
let now = now();
917921
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
918922
let entropy = FixedEntropy {};
923+
let nonce = Nonce::from_entropy_source(&entropy);
919924
let secp_ctx = Secp256k1::new();
920925

921926
let offer_with_extra_chain =
922-
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, &entropy, &secp_ctx)
927+
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, nonce, &secp_ctx)
923928
.path(blinded_path())
924929
.chain(Network::Bitcoin)
925930
.chain(Network::Testnet)
@@ -947,13 +952,13 @@ mod tests {
947952
let now = now();
948953
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
949954
let entropy = FixedEntropy {};
955+
let nonce = Nonce::from_entropy_source(&entropy);
950956
let secp_ctx = Secp256k1::new();
951957

952-
let offer =
953-
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, &entropy, &secp_ctx)
954-
.path(blinded_path())
955-
.build()
956-
.unwrap();
958+
let offer = OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, nonce, &secp_ctx)
959+
.path(blinded_path())
960+
.build()
961+
.unwrap();
957962

958963
const TEST_RELATIVE_EXPIRY: u32 = 3600;
959964
let invoice = StaticInvoiceBuilder::for_offer_using_derived_keys(
@@ -988,13 +993,13 @@ mod tests {
988993
let now = now();
989994
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
990995
let entropy = FixedEntropy {};
996+
let nonce = Nonce::from_entropy_source(&entropy);
991997
let secp_ctx = Secp256k1::new();
992998

993-
let offer =
994-
OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, &entropy, &secp_ctx)
995-
.path(blinded_path())
996-
.build()
997-
.unwrap();
999+
let offer = OfferBuilder::deriving_signing_pubkey(node_id, &expanded_key, nonce, &secp_ctx)
1000+
.path(blinded_path())
1001+
.build()
1002+
.unwrap();
9981003

9991004
let invoice = StaticInvoiceBuilder::for_offer_using_derived_keys(
10001005
&offer,

0 commit comments

Comments
 (0)