Skip to content

Commit d2161d9

Browse files
committed
Move pay_for_offer_human_readable to OffersMessageFlow
1 parent e148fda commit d2161d9

File tree

3 files changed

+116
-68
lines changed

3 files changed

+116
-68
lines changed

lightning-dns-resolver/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ mod test {
404404
let retry = Retry::Attempts(0);
405405
let amt = 42_000;
406406
nodes[0]
407-
.node
407+
.offers_handler
408408
.pay_for_offer_from_human_readable_name(name, amt, payment_id, retry, None, resolvers)
409409
.unwrap();
410410

lightning/src/ln/channelmanager.rs

+18-67
Original file line numberDiff line numberDiff line change
@@ -9542,6 +9542,16 @@ where
95429542
self.pending_offers_messages.lock().expect("Mutex is locked by other thread.")
95439543
}
95449544

9545+
#[cfg(feature = "dnssec")]
9546+
fn get_pending_dns_onion_messages(&self) -> MutexGuard<'_, Vec<(DNSResolverMessage, MessageSendInstructions)>> {
9547+
self.pending_dns_onion_messages.lock().expect("Mutex is locked by other thread.")
9548+
}
9549+
9550+
#[cfg(feature = "dnssec")]
9551+
fn get_hrn_resolver(&self) -> &OMNameResolver {
9552+
&self.hrn_resolver
9553+
}
9554+
95459555
fn sign_bolt12_invoice(
95469556
&self, invoice: &UnsignedBolt12Invoice,
95479557
) -> Result<schnorr::Signature, ()> {
@@ -9756,6 +9766,14 @@ where
97569766
)
97579767
}
97589768

9769+
#[cfg(feature = "dnssec")]
9770+
fn add_new_awaiting_offer(
9771+
&self, payment_id: PaymentId, expiration: StaleExpiration, retry_strategy: Retry,
9772+
max_total_routing_fee_msat: Option<u64>, amount_msats: u64,
9773+
) -> Result<(), ()> {
9774+
self.pending_outbound_payments.add_new_awaiting_offer(payment_id, expiration, retry_strategy, max_total_routing_fee_msat, amount_msats)
9775+
}
9776+
97599777
fn pay_for_offer_intern<CPP: FnOnce(&InvoiceRequest, Nonce) -> Result<(), Bolt12SemanticError>>(
97609778
&self, offer: &Offer, quantity: Option<u64>, amount_msats: Option<u64>,
97619779
payer_note: Option<String>, payment_id: PaymentId,
@@ -9823,73 +9841,6 @@ where
98239841
MR::Target: MessageRouter,
98249842
L::Target: Logger,
98259843
{
9826-
/// Pays for an [`Offer`] looked up using [BIP 353] Human Readable Names resolved by the DNS
9827-
/// resolver(s) at `dns_resolvers` which resolve names according to bLIP 32.
9828-
///
9829-
/// If the wallet supports paying on-chain schemes, you should instead use
9830-
/// [`OMNameResolver::resolve_name`] and [`OMNameResolver::handle_dnssec_proof_for_uri`] (by
9831-
/// implementing [`DNSResolverMessageHandler`]) directly to look up a URI and then delegate to
9832-
/// your normal URI handling.
9833-
///
9834-
/// If `max_total_routing_fee_msat` is not specified, the default from
9835-
/// [`RouteParameters::from_payment_params_and_value`] is applied.
9836-
///
9837-
/// # Payment
9838-
///
9839-
/// The provided `payment_id` is used to ensure that only one invoice is paid for the request
9840-
/// when received. See [Avoiding Duplicate Payments] for other requirements once the payment has
9841-
/// been sent.
9842-
///
9843-
/// To revoke the request, use [`ChannelManager::abandon_payment`] prior to receiving the
9844-
/// invoice. If abandoned, or an invoice isn't received in a reasonable amount of time, the
9845-
/// payment will fail with an [`Event::InvoiceRequestFailed`].
9846-
///
9847-
/// # Privacy
9848-
///
9849-
/// For payer privacy, uses a derived payer id and uses [`MessageRouter::create_blinded_paths`]
9850-
/// to construct a [`BlindedPath`] for the reply path. For further privacy implications, see the
9851-
/// docs of the parameterized [`Router`], which implements [`MessageRouter`].
9852-
///
9853-
/// # Limitations
9854-
///
9855-
/// Requires a direct connection to the given [`Destination`] as well as an introduction node in
9856-
/// [`Offer::paths`] or to [`Offer::signing_pubkey`], if empty. A similar restriction applies to
9857-
/// the responding [`Bolt12Invoice::payment_paths`].
9858-
///
9859-
/// # Errors
9860-
///
9861-
/// Errors if:
9862-
/// - a duplicate `payment_id` is provided given the caveats in the aforementioned link,
9863-
///
9864-
/// [`Bolt12Invoice::payment_paths`]: crate::offers::invoice::Bolt12Invoice::payment_paths
9865-
/// [Avoiding Duplicate Payments]: #avoiding-duplicate-payments
9866-
#[cfg(feature = "dnssec")]
9867-
pub fn pay_for_offer_from_human_readable_name(
9868-
&self, name: HumanReadableName, amount_msats: u64, payment_id: PaymentId,
9869-
retry_strategy: Retry, max_total_routing_fee_msat: Option<u64>,
9870-
dns_resolvers: Vec<Destination>,
9871-
) -> Result<(), ()> {
9872-
let (onion_message, context) =
9873-
self.hrn_resolver.resolve_name(payment_id, name, &*self.entropy_source)?;
9874-
let reply_paths = self.create_blinded_paths(MessageContext::DNSResolver(context))?;
9875-
let expiration = StaleExpiration::TimerTicks(1);
9876-
self.pending_outbound_payments.add_new_awaiting_offer(payment_id, expiration, retry_strategy, max_total_routing_fee_msat, amount_msats)?;
9877-
let message_params = dns_resolvers
9878-
.iter()
9879-
.flat_map(|destination| reply_paths.iter().map(move |path| (path, destination)))
9880-
.take(OFFERS_MESSAGE_REQUEST_LIMIT);
9881-
for (reply_path, destination) in message_params {
9882-
self.pending_dns_onion_messages.lock().unwrap().push((
9883-
DNSResolverMessage::DNSSECQuery(onion_message.clone()),
9884-
MessageSendInstructions::WithSpecifiedReplyPath {
9885-
destination: destination.clone(),
9886-
reply_path: reply_path.clone(),
9887-
},
9888-
));
9889-
}
9890-
Ok(())
9891-
}
9892-
98939844
/// Gets a [`PaymentSecret`] for a given [`PaymentHash`], for which the payment preimage is
98949845
/// stored external to LDK.
98959846
///

lightning/src/offers/flow.rs

+97
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ use {
6262
crate::offers::refund::RefundMaybeWithDerivedMetadataBuilder,
6363
};
6464

65+
#[cfg(feature = "dnssec")]
66+
use crate::onion_message::dns_resolution::{DNSResolverMessage, HumanReadableName, OMNameResolver};
67+
6568
/// Functions commonly shared in usage between [`ChannelManager`] & `OffersMessageFlow`
6669
///
6770
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
@@ -71,6 +74,16 @@ pub trait OffersMessageCommons {
7174
&self,
7275
) -> MutexGuard<'_, Vec<(OffersMessage, MessageSendInstructions)>>;
7376

77+
#[cfg(feature = "dnssec")]
78+
/// Get pending DNS onion messages
79+
fn get_pending_dns_onion_messages(
80+
&self,
81+
) -> MutexGuard<'_, Vec<(DNSResolverMessage, MessageSendInstructions)>>;
82+
83+
#[cfg(feature = "dnssec")]
84+
/// Get hrn resolver
85+
fn get_hrn_resolver(&self) -> &OMNameResolver;
86+
7487
/// Signs the [`TaggedHash`] of a BOLT 12 invoice.
7588
///
7689
/// May be called by a function passed to [`UnsignedBolt12Invoice::sign`] where `invoice` is the
@@ -193,6 +206,13 @@ pub trait OffersMessageCommons {
193206
retryable_invoice_request: Option<RetryableInvoiceRequest>,
194207
) -> Result<(), ()>;
195208

209+
#[cfg(feature = "dnssec")]
210+
/// Add new awaiting offer
211+
fn add_new_awaiting_offer(
212+
&self, payment_id: PaymentId, expiration: StaleExpiration, retry_strategy: Retry,
213+
max_total_routing_fee_msat: Option<u64>, amount_msats: u64,
214+
) -> Result<(), ()>;
215+
196216
/// Internal pay_for_offer
197217
fn pay_for_offer_intern<
198218
CPP: FnOnce(&InvoiceRequest, Nonce) -> Result<(), Bolt12SemanticError>,
@@ -1234,4 +1254,81 @@ where
12341254
Err(()) => Err(Bolt12SemanticError::InvalidAmount),
12351255
}
12361256
}
1257+
1258+
/// Pays for an [`Offer`] looked up using [BIP 353] Human Readable Names resolved by the DNS
1259+
/// resolver(s) at `dns_resolvers` which resolve names according to bLIP 32.
1260+
///
1261+
/// If the wallet supports paying on-chain schemes, you should instead use
1262+
/// [`OMNameResolver::resolve_name`] and [`OMNameResolver::handle_dnssec_proof_for_uri`] (by
1263+
/// implementing [`DNSResolverMessageHandler`]) directly to look up a URI and then delegate to
1264+
/// your normal URI handling.
1265+
///
1266+
/// If `max_total_routing_fee_msat` is not specified, the default from
1267+
/// [`RouteParameters::from_payment_params_and_value`] is applied.
1268+
///
1269+
/// # Payment
1270+
///
1271+
/// The provided `payment_id` is used to ensure that only one invoice is paid for the request
1272+
/// when received. See [Avoiding Duplicate Payments] for other requirements once the payment has
1273+
/// been sent.
1274+
///
1275+
/// To revoke the request, use [`ChannelManager::abandon_payment`] prior to receiving the
1276+
/// invoice. If abandoned, or an invoice isn't received in a reasonable amount of time, the
1277+
/// payment will fail with an [`Event::InvoiceRequestFailed`].
1278+
///
1279+
/// # Privacy
1280+
///
1281+
/// For payer privacy, uses a derived payer id and uses [`MessageRouter::create_blinded_paths`]
1282+
/// to construct a [`BlindedPath`] for the reply path. For further privacy implications, see the
1283+
/// docs of the parameterized [`Router`], which implements [`MessageRouter`].
1284+
///
1285+
/// # Limitations
1286+
///
1287+
/// Requires a direct connection to the given [`Destination`] as well as an introduction node in
1288+
/// [`Offer::paths`] or to [`Offer::signing_pubkey`], if empty. A similar restriction applies to
1289+
/// the responding [`Bolt12Invoice::payment_paths`].
1290+
///
1291+
/// # Errors
1292+
///
1293+
/// Errors if:
1294+
/// - a duplicate `payment_id` is provided given the caveats in the aforementioned link,
1295+
///
1296+
/// [`Bolt12Invoice::payment_paths`]: crate::offers::invoice::Bolt12Invoice::payment_paths
1297+
/// [Avoiding Duplicate Payments]: #avoiding-duplicate-payments
1298+
#[cfg(feature = "dnssec")]
1299+
pub fn pay_for_offer_from_human_readable_name(
1300+
&self, name: HumanReadableName, amount_msats: u64, payment_id: PaymentId,
1301+
retry_strategy: Retry, max_total_routing_fee_msat: Option<u64>,
1302+
dns_resolvers: Vec<Destination>,
1303+
) -> Result<(), ()> {
1304+
let (onion_message, context) = self.commons.get_hrn_resolver().resolve_name(
1305+
payment_id,
1306+
name,
1307+
&*self.entropy_source,
1308+
)?;
1309+
let reply_paths =
1310+
self.commons.create_blinded_paths(MessageContext::DNSResolver(context))?;
1311+
let expiration = StaleExpiration::TimerTicks(1);
1312+
self.commons.add_new_awaiting_offer(
1313+
payment_id,
1314+
expiration,
1315+
retry_strategy,
1316+
max_total_routing_fee_msat,
1317+
amount_msats,
1318+
)?;
1319+
let message_params = dns_resolvers
1320+
.iter()
1321+
.flat_map(|destination| reply_paths.iter().map(move |path| (path, destination)))
1322+
.take(OFFERS_MESSAGE_REQUEST_LIMIT);
1323+
for (reply_path, destination) in message_params {
1324+
self.commons.get_pending_dns_onion_messages().push((
1325+
DNSResolverMessage::DNSSECQuery(onion_message.clone()),
1326+
MessageSendInstructions::WithSpecifiedReplyPath {
1327+
destination: destination.clone(),
1328+
reply_path: reply_path.clone(),
1329+
},
1330+
));
1331+
}
1332+
Ok(())
1333+
}
12371334
}

0 commit comments

Comments
 (0)