Skip to content

Commit b718728

Browse files
committed
Move pay_for_offer_human_readable to OffersMessageFlow
1 parent 3871ea1 commit b718728

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
@@ -9464,6 +9464,16 @@ where
94649464
self.pending_offers_messages.lock().expect("Mutex is locked by other thread.")
94659465
}
94669466

9467+
#[cfg(feature = "dnssec")]
9468+
fn get_pending_dns_onion_messages(&self) -> MutexGuard<'_, Vec<(DNSResolverMessage, MessageSendInstructions)>> {
9469+
self.pending_dns_onion_messages.lock().expect("Mutex is locked by other thread.")
9470+
}
9471+
9472+
#[cfg(feature = "dnssec")]
9473+
fn get_hrn_resolver(&self) -> &OMNameResolver {
9474+
&self.hrn_resolver
9475+
}
9476+
94679477
fn sign_bolt12_invoice(
94689478
&self, invoice: &UnsignedBolt12Invoice,
94699479
) -> Result<schnorr::Signature, ()> {
@@ -9678,6 +9688,14 @@ where
96789688
)
96799689
}
96809690

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

lightning/src/offers/flow.rs

Lines changed: 97 additions & 0 deletions
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)