Skip to content

Commit 92320d4

Browse files
committed
Move pay_for_offer_human_readable to OffersMessageFlow
1 parent 724c30a commit 92320d4

File tree

3 files changed

+116
-68
lines changed

3 files changed

+116
-68
lines changed

lightning-dns-resolver/src/lib.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 18 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -9529,6 +9529,16 @@ where
95299529
self.pending_offers_messages.lock().expect("Mutex is locked by other thread.")
95309530
}
95319531

9532+
#[cfg(feature = "dnssec")]
9533+
fn get_pending_dns_onion_messages(&self) -> MutexGuard<'_, Vec<(DNSResolverMessage, MessageSendInstructions)>> {
9534+
self.pending_dns_onion_messages.lock().expect("Mutex is locked by other thread.")
9535+
}
9536+
9537+
#[cfg(feature = "dnssec")]
9538+
fn get_hrn_resolver(&self) -> &OMNameResolver {
9539+
&self.hrn_resolver
9540+
}
9541+
95329542
fn sign_bolt12_invoice(
95339543
&self, invoice: &UnsignedBolt12Invoice,
95349544
) -> Result<schnorr::Signature, ()> {
@@ -9683,6 +9693,14 @@ where
96839693
)
96849694
}
96859695

9696+
#[cfg(feature = "dnssec")]
9697+
fn add_new_awaiting_offer(
9698+
&self, payment_id: PaymentId, expiration: StaleExpiration, retry_strategy: Retry,
9699+
max_total_routing_fee_msat: Option<u64>, amount_msats: u64,
9700+
) -> Result<(), ()> {
9701+
self.pending_outbound_payments.add_new_awaiting_offer(payment_id, expiration, retry_strategy, max_total_routing_fee_msat, amount_msats)
9702+
}
9703+
96869704
fn pay_for_offer_intern<CPP: FnOnce(&InvoiceRequest, Nonce) -> Result<(), Bolt12SemanticError>>(
96879705
&self, offer: &Offer, quantity: Option<u64>, amount_msats: Option<u64>,
96889706
payer_note: Option<String>, payment_id: PaymentId,
@@ -9750,73 +9768,6 @@ where
97509768
MR::Target: MessageRouter,
97519769
L::Target: Logger,
97529770
{
9753-
/// Pays for an [`Offer`] looked up using [BIP 353] Human Readable Names resolved by the DNS
9754-
/// resolver(s) at `dns_resolvers` which resolve names according to bLIP 32.
9755-
///
9756-
/// If the wallet supports paying on-chain schemes, you should instead use
9757-
/// [`OMNameResolver::resolve_name`] and [`OMNameResolver::handle_dnssec_proof_for_uri`] (by
9758-
/// implementing [`DNSResolverMessageHandler`]) directly to look up a URI and then delegate to
9759-
/// your normal URI handling.
9760-
///
9761-
/// If `max_total_routing_fee_msat` is not specified, the default from
9762-
/// [`RouteParameters::from_payment_params_and_value`] is applied.
9763-
///
9764-
/// # Payment
9765-
///
9766-
/// The provided `payment_id` is used to ensure that only one invoice is paid for the request
9767-
/// when received. See [Avoiding Duplicate Payments] for other requirements once the payment has
9768-
/// been sent.
9769-
///
9770-
/// To revoke the request, use [`ChannelManager::abandon_payment`] prior to receiving the
9771-
/// invoice. If abandoned, or an invoice isn't received in a reasonable amount of time, the
9772-
/// payment will fail with an [`Event::InvoiceRequestFailed`].
9773-
///
9774-
/// # Privacy
9775-
///
9776-
/// For payer privacy, uses a derived payer id and uses [`MessageRouter::create_blinded_paths`]
9777-
/// to construct a [`BlindedPath`] for the reply path. For further privacy implications, see the
9778-
/// docs of the parameterized [`Router`], which implements [`MessageRouter`].
9779-
///
9780-
/// # Limitations
9781-
///
9782-
/// Requires a direct connection to the given [`Destination`] as well as an introduction node in
9783-
/// [`Offer::paths`] or to [`Offer::signing_pubkey`], if empty. A similar restriction applies to
9784-
/// the responding [`Bolt12Invoice::payment_paths`].
9785-
///
9786-
/// # Errors
9787-
///
9788-
/// Errors if:
9789-
/// - a duplicate `payment_id` is provided given the caveats in the aforementioned link,
9790-
///
9791-
/// [`Bolt12Invoice::payment_paths`]: crate::offers::invoice::Bolt12Invoice::payment_paths
9792-
/// [Avoiding Duplicate Payments]: #avoiding-duplicate-payments
9793-
#[cfg(feature = "dnssec")]
9794-
pub fn pay_for_offer_from_human_readable_name(
9795-
&self, name: HumanReadableName, amount_msats: u64, payment_id: PaymentId,
9796-
retry_strategy: Retry, max_total_routing_fee_msat: Option<u64>,
9797-
dns_resolvers: Vec<Destination>,
9798-
) -> Result<(), ()> {
9799-
let (onion_message, context) =
9800-
self.hrn_resolver.resolve_name(payment_id, name, &*self.entropy_source)?;
9801-
let reply_paths = self.create_blinded_paths(MessageContext::DNSResolver(context))?;
9802-
let expiration = StaleExpiration::TimerTicks(1);
9803-
self.pending_outbound_payments.add_new_awaiting_offer(payment_id, expiration, retry_strategy, max_total_routing_fee_msat, amount_msats)?;
9804-
let message_params = dns_resolvers
9805-
.iter()
9806-
.flat_map(|destination| reply_paths.iter().map(move |path| (path, destination)))
9807-
.take(OFFERS_MESSAGE_REQUEST_LIMIT);
9808-
for (reply_path, destination) in message_params {
9809-
self.pending_dns_onion_messages.lock().unwrap().push((
9810-
DNSResolverMessage::DNSSECQuery(onion_message.clone()),
9811-
MessageSendInstructions::WithSpecifiedReplyPath {
9812-
destination: destination.clone(),
9813-
reply_path: reply_path.clone(),
9814-
},
9815-
));
9816-
}
9817-
Ok(())
9818-
}
9819-
98209771
/// Gets a [`PaymentSecret`] for a given [`PaymentHash`], for which the payment preimage is
98219772
/// stored external to LDK.
98229773
///

lightning/src/offers/flow.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ use {
5656
crate::offers::refund::RefundMaybeWithDerivedMetadataBuilder,
5757
};
5858

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

71+
#[cfg(feature = "dnssec")]
72+
/// Get pending DNS onion messages
73+
fn get_pending_dns_onion_messages(
74+
&self,
75+
) -> MutexGuard<'_, Vec<(DNSResolverMessage, MessageSendInstructions)>>;
76+
77+
#[cfg(feature = "dnssec")]
78+
/// Get hrn resolver
79+
fn get_hrn_resolver(&self) -> &OMNameResolver;
80+
6881
/// Signs the [`TaggedHash`] of a BOLT 12 invoice.
6982
///
7083
/// May be called by a function passed to [`UnsignedBolt12Invoice::sign`] where `invoice` is the
@@ -181,6 +194,13 @@ pub trait OffersMessageCommons {
181194
retryable_invoice_request: Option<RetryableInvoiceRequest>,
182195
) -> Result<(), ()>;
183196

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

0 commit comments

Comments
 (0)