Skip to content

Commit 2f852a6

Browse files
committed
Pass Nonce directly to InvoiceRequestBuilder
When using InvoiceRequestBuilder::deriving_payer_id, the nonce generated needs to be the same one included in any reply path. This is because the nonce is used along with the invoice request TLVs to derive a payer id. While this data is also included in the payer_metadata, including it in the blinded path would allow reducing the amount of data needed there to just enough to provide entropy (i.e., 16 bytes). This is more important for Refund because it can be transmitted via a QR code. But using the same payer_metadata structure for both InvoiceRequest and Refund would be beneficial to avoid more code.
1 parent 39e9bdf commit 2f852a6

File tree

3 files changed

+20
-29
lines changed

3 files changed

+20
-29
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8803,8 +8803,9 @@ where
88038803
let entropy = &*self.entropy_source;
88048804
let secp_ctx = &self.secp_ctx;
88058805

8806+
let nonce = Nonce::from_entropy_source(entropy);
88068807
let builder: InvoiceRequestBuilder<DerivedPayerId, secp256k1::All> = offer
8807-
.request_invoice_deriving_payer_id(expanded_key, entropy, secp_ctx, payment_id)?
8808+
.request_invoice_deriving_payer_id(expanded_key, nonce, secp_ctx, payment_id)?
88088809
.into();
88098810
let builder = builder.chain_hash(self.chain_hash)?;
88108811

lightning/src/offers/invoice_request.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ use bitcoin::blockdata::constants::ChainHash;
6161
use bitcoin::network::Network;
6262
use bitcoin::secp256k1::{Keypair, PublicKey, Secp256k1, self};
6363
use bitcoin::secp256k1::schnorr::Signature;
64-
use core::ops::Deref;
65-
use crate::sign::EntropySource;
6664
use crate::io;
6765
use crate::blinded_path::BlindedPath;
6866
use crate::ln::types::PaymentHash;
@@ -171,11 +169,10 @@ macro_rules! invoice_request_explicit_payer_id_builder_methods { ($self: ident,
171169
}
172170

173171
#[cfg_attr(c_bindings, allow(dead_code))]
174-
pub(super) fn deriving_metadata<ES: Deref>(
175-
offer: &'a Offer, payer_id: PublicKey, expanded_key: &ExpandedKey, entropy_source: ES,
172+
pub(super) fn deriving_metadata(
173+
offer: &'a Offer, payer_id: PublicKey, expanded_key: &ExpandedKey, nonce: Nonce,
176174
payment_id: PaymentId,
177-
) -> Self where ES::Target: EntropySource {
178-
let nonce = Nonce::from_entropy_source(entropy_source);
175+
) -> Self {
179176
let payment_id = Some(payment_id);
180177
let derivation_material = MetadataMaterial::new(nonce, expanded_key, IV_BYTES, payment_id);
181178
let metadata = Metadata::Derived(derivation_material);
@@ -201,11 +198,10 @@ macro_rules! invoice_request_derived_payer_id_builder_methods { (
201198
$self: ident, $self_type: ty, $secp_context: ty
202199
) => {
203200
#[cfg_attr(c_bindings, allow(dead_code))]
204-
pub(super) fn deriving_payer_id<ES: Deref>(
205-
offer: &'a Offer, expanded_key: &ExpandedKey, entropy_source: ES,
201+
pub(super) fn deriving_payer_id(
202+
offer: &'a Offer, expanded_key: &ExpandedKey, nonce: Nonce,
206203
secp_ctx: &'b Secp256k1<$secp_context>, payment_id: PaymentId
207-
) -> Self where ES::Target: EntropySource {
208-
let nonce = Nonce::from_entropy_source(entropy_source);
204+
) -> Self {
209205
let payment_id = Some(payment_id);
210206
let derivation_material = MetadataMaterial::new(nonce, expanded_key, IV_BYTES, payment_id);
211207
let metadata = Metadata::DerivedSigningPubkey(derivation_material);
@@ -1399,14 +1395,15 @@ mod tests {
13991395
let payer_id = payer_pubkey();
14001396
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
14011397
let entropy = FixedEntropy {};
1398+
let nonce = Nonce::from_entropy_source(&entropy);
14021399
let secp_ctx = Secp256k1::new();
14031400
let payment_id = PaymentId([1; 32]);
14041401

14051402
let offer = OfferBuilder::new(recipient_pubkey())
14061403
.amount_msats(1000)
14071404
.build().unwrap();
14081405
let invoice_request = offer
1409-
.request_invoice_deriving_metadata(payer_id, &expanded_key, &entropy, payment_id)
1406+
.request_invoice_deriving_metadata(payer_id, &expanded_key, nonce, payment_id)
14101407
.unwrap()
14111408
.build().unwrap()
14121409
.sign(payer_sign).unwrap();
@@ -1472,14 +1469,15 @@ mod tests {
14721469
fn builds_invoice_request_with_derived_payer_id() {
14731470
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
14741471
let entropy = FixedEntropy {};
1472+
let nonce = Nonce::from_entropy_source(&entropy);
14751473
let secp_ctx = Secp256k1::new();
14761474
let payment_id = PaymentId([1; 32]);
14771475

14781476
let offer = OfferBuilder::new(recipient_pubkey())
14791477
.amount_msats(1000)
14801478
.build().unwrap();
14811479
let invoice_request = offer
1482-
.request_invoice_deriving_payer_id(&expanded_key, &entropy, &secp_ctx, payment_id)
1480+
.request_invoice_deriving_payer_id(&expanded_key, nonce, &secp_ctx, payment_id)
14831481
.unwrap()
14841482
.build_and_sign()
14851483
.unwrap();

lightning/src/offers/offer.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,8 @@ use bitcoin::network::Network;
8282
use bitcoin::secp256k1::{Keypair, PublicKey, Secp256k1, self};
8383
use core::hash::{Hash, Hasher};
8484
use core::num::NonZeroU64;
85-
use core::ops::Deref;
8685
use core::str::FromStr;
8786
use core::time::Duration;
88-
use crate::sign::EntropySource;
8987
use crate::io;
9088
use crate::blinded_path::BlindedPath;
9189
use crate::ln::channelmanager::PaymentId;
@@ -690,25 +688,22 @@ macro_rules! request_invoice_derived_payer_id { ($self: ident, $builder: ty) =>
690688
/// [`Bolt12Invoice::verify`]: crate::offers::invoice::Bolt12Invoice::verify
691689
/// [`ExpandedKey`]: crate::ln::inbound_payment::ExpandedKey
692690
pub fn request_invoice_deriving_payer_id<
693-
'a, 'b, ES: Deref,
691+
'a, 'b,
694692
#[cfg(not(c_bindings))]
695693
T: secp256k1::Signing
696694
>(
697-
&'a $self, expanded_key: &ExpandedKey, entropy_source: ES,
695+
&'a $self, expanded_key: &ExpandedKey, nonce: Nonce,
698696
#[cfg(not(c_bindings))]
699697
secp_ctx: &'b Secp256k1<T>,
700698
#[cfg(c_bindings)]
701699
secp_ctx: &'b Secp256k1<secp256k1::All>,
702700
payment_id: PaymentId
703-
) -> Result<$builder, Bolt12SemanticError>
704-
where
705-
ES::Target: EntropySource,
706-
{
701+
) -> Result<$builder, Bolt12SemanticError> {
707702
if $self.offer_features().requires_unknown_bits() {
708703
return Err(Bolt12SemanticError::UnknownRequiredFeatures);
709704
}
710705

711-
Ok(<$builder>::deriving_payer_id($self, expanded_key, entropy_source, secp_ctx, payment_id))
706+
Ok(<$builder>::deriving_payer_id($self, expanded_key, nonce, secp_ctx, payment_id))
712707
}
713708
} }
714709

@@ -719,18 +714,15 @@ macro_rules! request_invoice_explicit_payer_id { ($self: ident, $builder: ty) =>
719714
/// Useful for recurring payments using the same `payer_id` with different invoices.
720715
///
721716
/// [`InvoiceRequest::payer_id`]: crate::offers::invoice_request::InvoiceRequest::payer_id
722-
pub fn request_invoice_deriving_metadata<ES: Deref>(
723-
&$self, payer_id: PublicKey, expanded_key: &ExpandedKey, entropy_source: ES,
717+
pub fn request_invoice_deriving_metadata(
718+
&$self, payer_id: PublicKey, expanded_key: &ExpandedKey, nonce: Nonce,
724719
payment_id: PaymentId
725-
) -> Result<$builder, Bolt12SemanticError>
726-
where
727-
ES::Target: EntropySource,
728-
{
720+
) -> Result<$builder, Bolt12SemanticError> {
729721
if $self.offer_features().requires_unknown_bits() {
730722
return Err(Bolt12SemanticError::UnknownRequiredFeatures);
731723
}
732724

733-
Ok(<$builder>::deriving_metadata($self, payer_id, expanded_key, entropy_source, payment_id))
725+
Ok(<$builder>::deriving_metadata($self, payer_id, expanded_key, nonce, payment_id))
734726
}
735727

736728
/// Creates an [`InvoiceRequestBuilder`] for the offer with the given `metadata` and `payer_id`,

0 commit comments

Comments
 (0)