Skip to content

Commit fd596c3

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 bdf3330 commit fd596c3

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
@@ -8976,8 +8976,9 @@ where
89768976
let entropy = &*self.entropy_source;
89778977
let secp_ctx = &self.secp_ctx;
89788978

8979+
let nonce = Nonce::from_entropy_source(entropy);
89798980
let builder: InvoiceRequestBuilder<DerivedPayerId, secp256k1::All> = offer
8980-
.request_invoice_deriving_payer_id(expanded_key, entropy, secp_ctx, payment_id)?
8981+
.request_invoice_deriving_payer_id(expanded_key, nonce, secp_ctx, payment_id)?
89818982
.into();
89828983
let builder = builder.chain_hash(self.chain_hash)?;
89838984

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);
@@ -1403,14 +1399,15 @@ mod tests {
14031399
let payer_id = payer_pubkey();
14041400
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
14051401
let entropy = FixedEntropy {};
1402+
let nonce = Nonce::from_entropy_source(&entropy);
14061403
let secp_ctx = Secp256k1::new();
14071404
let payment_id = PaymentId([1; 32]);
14081405

14091406
let offer = OfferBuilder::new(recipient_pubkey())
14101407
.amount_msats(1000)
14111408
.build().unwrap();
14121409
let invoice_request = offer
1413-
.request_invoice_deriving_metadata(payer_id, &expanded_key, &entropy, payment_id)
1410+
.request_invoice_deriving_metadata(payer_id, &expanded_key, nonce, payment_id)
14141411
.unwrap()
14151412
.build().unwrap()
14161413
.sign(payer_sign).unwrap();
@@ -1476,14 +1473,15 @@ mod tests {
14761473
fn builds_invoice_request_with_derived_payer_id() {
14771474
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
14781475
let entropy = FixedEntropy {};
1476+
let nonce = Nonce::from_entropy_source(&entropy);
14791477
let secp_ctx = Secp256k1::new();
14801478
let payment_id = PaymentId([1; 32]);
14811479

14821480
let offer = OfferBuilder::new(recipient_pubkey())
14831481
.amount_msats(1000)
14841482
.build().unwrap();
14851483
let invoice_request = offer
1486-
.request_invoice_deriving_payer_id(&expanded_key, &entropy, &secp_ctx, payment_id)
1484+
.request_invoice_deriving_payer_id(&expanded_key, nonce, &secp_ctx, payment_id)
14871485
.unwrap()
14881486
.build_and_sign()
14891487
.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;
@@ -699,25 +697,22 @@ macro_rules! request_invoice_derived_payer_id { ($self: ident, $builder: ty) =>
699697
/// [`Bolt12Invoice::verify`]: crate::offers::invoice::Bolt12Invoice::verify
700698
/// [`ExpandedKey`]: crate::ln::inbound_payment::ExpandedKey
701699
pub fn request_invoice_deriving_payer_id<
702-
'a, 'b, ES: Deref,
700+
'a, 'b,
703701
#[cfg(not(c_bindings))]
704702
T: secp256k1::Signing
705703
>(
706-
&'a $self, expanded_key: &ExpandedKey, entropy_source: ES,
704+
&'a $self, expanded_key: &ExpandedKey, nonce: Nonce,
707705
#[cfg(not(c_bindings))]
708706
secp_ctx: &'b Secp256k1<T>,
709707
#[cfg(c_bindings)]
710708
secp_ctx: &'b Secp256k1<secp256k1::All>,
711709
payment_id: PaymentId
712-
) -> Result<$builder, Bolt12SemanticError>
713-
where
714-
ES::Target: EntropySource,
715-
{
710+
) -> Result<$builder, Bolt12SemanticError> {
716711
if $self.offer_features().requires_unknown_bits() {
717712
return Err(Bolt12SemanticError::UnknownRequiredFeatures);
718713
}
719714

720-
Ok(<$builder>::deriving_payer_id($self, expanded_key, entropy_source, secp_ctx, payment_id))
715+
Ok(<$builder>::deriving_payer_id($self, expanded_key, nonce, secp_ctx, payment_id))
721716
}
722717
} }
723718

@@ -728,18 +723,15 @@ macro_rules! request_invoice_explicit_payer_id { ($self: ident, $builder: ty) =>
728723
/// Useful for recurring payments using the same `payer_id` with different invoices.
729724
///
730725
/// [`InvoiceRequest::payer_id`]: crate::offers::invoice_request::InvoiceRequest::payer_id
731-
pub fn request_invoice_deriving_metadata<ES: Deref>(
732-
&$self, payer_id: PublicKey, expanded_key: &ExpandedKey, entropy_source: ES,
726+
pub fn request_invoice_deriving_metadata(
727+
&$self, payer_id: PublicKey, expanded_key: &ExpandedKey, nonce: Nonce,
733728
payment_id: PaymentId
734-
) -> Result<$builder, Bolt12SemanticError>
735-
where
736-
ES::Target: EntropySource,
737-
{
729+
) -> Result<$builder, Bolt12SemanticError> {
738730
if $self.offer_features().requires_unknown_bits() {
739731
return Err(Bolt12SemanticError::UnknownRequiredFeatures);
740732
}
741733

742-
Ok(<$builder>::deriving_metadata($self, payer_id, expanded_key, entropy_source, payment_id))
734+
Ok(<$builder>::deriving_metadata($self, payer_id, expanded_key, nonce, payment_id))
743735
}
744736

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

0 commit comments

Comments
 (0)