Skip to content

Commit c4a87a7

Browse files
committed
Stateless offer and refund builder utilities
Add utility functions to ChannelManager for creating OfferBuilder and RefundBuilder such that derived keys are used for the signing pubkey and payer id, respectively. This allows for stateless verification of any InvoiceRequest and Invoice messages. Later, blinded paths can be included in the returned builders.
1 parent 43a97d6 commit c4a87a7

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ use crate::ln::outbound_payment;
5454
use crate::ln::outbound_payment::{OutboundPayments, PaymentAttempts, PendingOutboundPayment};
5555
use crate::ln::wire::Encode;
5656
use crate::chain::keysinterface::{EntropySource, KeysManager, NodeSigner, Recipient, SignerProvider, ChannelSigner, WriteableEcdsaChannelSigner};
57+
use crate::offers::offer::OfferBuilder;
58+
use crate::offers::parse::SemanticError;
59+
use crate::offers::refund::RefundBuilder;
5760
use crate::util::config::{UserConfig, ChannelConfig};
5861
use crate::util::events::{Event, EventHandler, EventsProvider, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination};
5962
use crate::util::events;
@@ -5427,6 +5430,38 @@ where
54275430
Ok(payment_secret)
54285431
}
54295432

5433+
/// Creates an [`OfferBuilder`] such that the [`Offer`] it builds is recognized by the
5434+
/// [`OnionMessenger`] when handling [`InvoiceRequest`] messages for the offer.
5435+
///
5436+
/// [`Offer`]: crate::offers::offer::Offer
5437+
/// [`OnionMessenger`]: crate::onion_message::OnionMessenger
5438+
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
5439+
pub fn create_offer_builder(&self, description: String) -> OfferBuilder {
5440+
let node_id = self.get_our_node_id();
5441+
let expanded_key = &self.inbound_payment_key;
5442+
let nonce = inbound_payment::Nonce::from_entropy_source(&*self.entropy_source);
5443+
5444+
// TODO: Set blinded paths
5445+
OfferBuilder::deriving_signing_pubkey(description, node_id, expanded_key, nonce)
5446+
}
5447+
5448+
/// Creates a [`RefundBuilder`] such that the [`Refund`] it builds is recognized by the
5449+
/// [`OnionMessenger`] when handling [`Invoice`] messages for the refund.
5450+
///
5451+
/// [`Refund`]: crate::offers::refund::Refund
5452+
/// [`OnionMessenger`]: crate::onion_message::OnionMessenger
5453+
/// [`Invoice`]: crate::offers::invoice::Invoice
5454+
pub fn create_refund_builder(
5455+
&self, description: String, amount_msats: u64
5456+
) -> Result<RefundBuilder, SemanticError> {
5457+
let node_id = self.get_our_node_id();
5458+
let expanded_key = &self.inbound_payment_key;
5459+
let nonce = inbound_payment::Nonce::from_entropy_source(&*self.entropy_source);
5460+
5461+
// TODO: Set blinded paths
5462+
RefundBuilder::deriving_payer_id(description, node_id, expanded_key, nonce, amount_msats)
5463+
}
5464+
54305465
/// Gets a payment secret and payment hash for use in an invoice given to a third party wishing
54315466
/// to pay us.
54325467
///

lightning/src/ln/inbound_payment.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,10 @@ impl Nonce {
106106
pub const LENGTH: usize = 16;
107107

108108
/// Creates a `Nonce` from the given [`EntropySource`].
109-
#[allow(unused)]
110-
pub fn from_entropy_source<ES: EntropySource>(entropy_source: &ES) -> Self {
109+
pub fn from_entropy_source<ES: Deref>(entropy_source: ES) -> Self
110+
where
111+
ES::Target: EntropySource,
112+
{
111113
let mut bytes = [0u8; Self::LENGTH];
112114
let rand_bytes = entropy_source.get_secure_random_bytes();
113115
bytes.copy_from_slice(&rand_bytes[..Self::LENGTH]);

lightning/src/offers/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ pub mod offer;
1919
pub mod parse;
2020
mod payer;
2121
pub mod refund;
22-
#[allow(unused)]
2322
pub(crate) mod signer;
2423
#[cfg(test)]
2524
mod test_utils;

lightning/src/offers/offer.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
//! published as a QR code to be scanned by a customer. The customer uses the offer to request an
1414
//! invoice from the merchant to be paid.
1515
//!
16+
//! # Example
17+
//!
1618
//! ```
1719
//! extern crate bitcoin;
1820
//! extern crate core;
@@ -65,6 +67,14 @@
6567
//! # Ok(())
6668
//! # }
6769
//! ```
70+
//!
71+
//! # Note
72+
//!
73+
//! If constructing an [`Offer`] for use with a [`ChannelManager`], use
74+
//! [`ChannelManager::create_offer_builder`] instead of [`OfferBuilder::new`].
75+
//!
76+
//! [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
77+
//! [`ChannelManager::create_offer_builder`]: crate::ln::channelmanager::ChannelManager::create_offer_builder
6878
6979
use bitcoin::blockdata::constants::ChainHash;
7080
use bitcoin::network::constants::Network;
@@ -106,6 +116,14 @@ impl OfferBuilder {
106116
/// while the offer is valid.
107117
///
108118
/// Use a different pubkey per offer to avoid correlating offers.
119+
///
120+
/// # Note
121+
///
122+
/// If constructing an [`Offer`] for use with a [`ChannelManager`], use
123+
/// [`ChannelManager::create_offer_builder`] instead of [`OfferBuilder::new`].
124+
///
125+
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
126+
/// [`ChannelManager::create_offer_builder`]: crate::ln::channelmanager::ChannelManager::create_offer_builder
109127
pub fn new(description: String, signing_pubkey: PublicKey) -> Self {
110128
OfferBuilder {
111129
offer: OfferContents {
@@ -127,7 +145,6 @@ impl OfferBuilder {
127145
///
128146
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
129147
/// [`ExpandedKey`]: crate::ln::inbound_payment::ExpandedKey
130-
#[allow(unused)]
131148
pub(crate) fn deriving_signing_pubkey(
132149
description: String, node_id: PublicKey, expanded_key: &ExpandedKey, nonce: Nonce
133150
) -> Self {

lightning/src/offers/refund.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
//! [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
1919
//! [`Offer`]: crate::offers::offer::Offer
2020
//!
21+
//! # Example
22+
//!
2123
//! ```
2224
//! extern crate bitcoin;
2325
//! extern crate core;
@@ -70,6 +72,14 @@
7072
//! # Ok(())
7173
//! # }
7274
//! ```
75+
//!
76+
//! # Note
77+
//!
78+
//! If constructing a [`Refund`] for use with a [`ChannelManager`], use
79+
//! [`ChannelManager::create_refund_builder`] instead of [`RefundBuilder::new`].
80+
//!
81+
//! [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
82+
//! [`ChannelManager::create_refund_builder`]: crate::ln::channelmanager::ChannelManager::create_refund_builder
7383
7484
use bitcoin::blockdata::constants::ChainHash;
7585
use bitcoin::network::constants::Network;
@@ -114,6 +124,14 @@ impl RefundBuilder {
114124
///
115125
/// Additionally, sets the required [`Refund::description`], [`Refund::metadata`], and
116126
/// [`Refund::amount_msats`].
127+
///
128+
/// # Note
129+
///
130+
/// If constructing a [`Refund`] for use with a [`ChannelManager`], use
131+
/// [`ChannelManager::create_refund_builder`] instead of [`RefundBuilder::new`].
132+
///
133+
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
134+
/// [`ChannelManager::create_refund_builder`]: crate::ln::channelmanager::ChannelManager::create_refund_builder
117135
pub fn new(
118136
description: String, metadata: Vec<u8>, payer_id: PublicKey, amount_msats: u64
119137
) -> Result<Self, SemanticError> {
@@ -141,7 +159,6 @@ impl RefundBuilder {
141159
///
142160
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
143161
/// [`ExpandedKey`]: crate::ln::inbound_payment::ExpandedKey
144-
#[allow(unused)]
145162
pub(crate) fn deriving_payer_id(
146163
description: String, node_id: PublicKey, expanded_key: &ExpandedKey, nonce: Nonce,
147164
amount_msats: u64

0 commit comments

Comments
 (0)