Skip to content

Commit 035ca82

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. Also tracks future payments using the given PaymentId such that the corresponding Invoice is paid only once.
1 parent 7e89634 commit 035ca82

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ use crate::ln::msgs::{ChannelMessageHandler, DecodeError, LightningError};
5555
use crate::ln::outbound_payment;
5656
use crate::ln::outbound_payment::{OutboundPayments, PaymentAttempts, PendingOutboundPayment, SendAlongPathArgs};
5757
use crate::ln::wire::Encode;
58+
use crate::offers::offer::{DerivedMetadata, OfferBuilder};
59+
use crate::offers::parse::Bolt12SemanticError;
60+
use crate::offers::refund::RefundBuilder;
5861
use crate::sign::{EntropySource, KeysManager, NodeSigner, Recipient, SignerProvider, WriteableEcdsaChannelSigner};
5962
use crate::util::config::{UserConfig, ChannelConfig, ChannelConfigUpdate};
6063
use crate::util::wakers::{Future, Notifier};
@@ -7067,6 +7070,50 @@ where
70677070
}
70687071
}
70697072

7073+
/// Creates an [`OfferBuilder`] such that the [`Offer`] it builds is recognized by the
7074+
/// [`ChannelManager`] when handling [`InvoiceRequest`] messages for the offer.
7075+
///
7076+
/// [`Offer`]: crate::offers::offer::Offer
7077+
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
7078+
pub fn create_offer_builder(
7079+
&self, description: String
7080+
) -> OfferBuilder<DerivedMetadata, secp256k1::All> {
7081+
let node_id = self.get_our_node_id();
7082+
let expanded_key = &self.inbound_payment_key;
7083+
let entropy = &*self.entropy_source;
7084+
let secp_ctx = &self.secp_ctx;
7085+
7086+
// TODO: Set blinded paths
7087+
OfferBuilder::deriving_signing_pubkey(description, node_id, expanded_key, entropy, secp_ctx)
7088+
}
7089+
7090+
/// Creates a [`RefundBuilder`] such that the [`Refund`] it builds is recognized by the
7091+
/// [`ChannelManager`] when handling [`Bolt12Invoice`] messages for the refund.
7092+
///
7093+
/// The provided `payment_id` is used to ensure that only one invoice is paid for the refund.
7094+
///
7095+
/// [`Refund`]: crate::offers::refund::Refund
7096+
/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
7097+
pub fn create_refund_builder(
7098+
&self, description: String, amount_msats: u64, payment_id: PaymentId, retry_strategy: Retry,
7099+
max_total_routing_fee_msat: Option<u64>
7100+
) -> Result<RefundBuilder<secp256k1::All>, Bolt12SemanticError> {
7101+
let node_id = self.get_our_node_id();
7102+
let expanded_key = &self.inbound_payment_key;
7103+
let entropy = &*self.entropy_source;
7104+
let secp_ctx = &self.secp_ctx;
7105+
7106+
// TODO: Set blinded paths
7107+
let builder = RefundBuilder::deriving_payer_id(
7108+
description, node_id, expanded_key, entropy, secp_ctx, amount_msats, payment_id
7109+
)?;
7110+
self.pending_outbound_payments
7111+
.add_new_awaiting_invoice(payment_id, retry_strategy, max_total_routing_fee_msat)
7112+
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;
7113+
7114+
Ok(builder)
7115+
}
7116+
70707117
/// Gets a payment secret and payment hash for use in an invoice given to a third party wishing
70717118
/// to pay us.
70727119
///

lightning/src/offers/offer.rs

Lines changed: 18 additions & 0 deletions
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;
@@ -132,6 +142,14 @@ impl<'a> OfferBuilder<'a, ExplicitMetadata, secp256k1::SignOnly> {
132142
/// while the offer is valid.
133143
///
134144
/// Use a different pubkey per offer to avoid correlating offers.
145+
///
146+
/// # Note
147+
///
148+
/// If constructing an [`Offer`] for use with a [`ChannelManager`], use
149+
/// [`ChannelManager::create_offer_builder`] instead of [`OfferBuilder::new`].
150+
///
151+
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
152+
/// [`ChannelManager::create_offer_builder`]: crate::ln::channelmanager::ChannelManager::create_offer_builder
135153
pub fn new(description: String, signing_pubkey: PublicKey) -> Self {
136154
OfferBuilder {
137155
offer: OfferContents {

lightning/src/offers/parse.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ pub enum Bolt12SemanticError {
179179
MissingPayerMetadata,
180180
/// A payer id was expected but was missing.
181181
MissingPayerId,
182+
/// The payment id for a refund or request is already in use.
183+
DuplicatePaymentId,
182184
/// Blinded paths were expected but were missing.
183185
MissingPaths,
184186
/// The blinded payinfo given does not match the number of blinded path hops.

lightning/src/offers/refund.rs

Lines changed: 18 additions & 0 deletions
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;
@@ -120,6 +130,14 @@ impl<'a> RefundBuilder<'a, secp256k1::SignOnly> {
120130
///
121131
/// Additionally, sets the required [`Refund::description`], [`Refund::payer_metadata`], and
122132
/// [`Refund::amount_msats`].
133+
///
134+
/// # Note
135+
///
136+
/// If constructing a [`Refund`] for use with a [`ChannelManager`], use
137+
/// [`ChannelManager::create_refund_builder`] instead of [`RefundBuilder::new`].
138+
///
139+
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
140+
/// [`ChannelManager::create_refund_builder`]: crate::ln::channelmanager::ChannelManager::create_refund_builder
123141
pub fn new(
124142
description: String, metadata: Vec<u8>, payer_id: PublicKey, amount_msats: u64
125143
) -> Result<Self, Bolt12SemanticError> {

0 commit comments

Comments
 (0)