Skip to content

Commit cd793ae

Browse files
committed
f: move blinded path methods to bolt12payment
1 parent 42d398d commit cd793ae

File tree

9 files changed

+95
-58
lines changed

9 files changed

+95
-58
lines changed

bindings/ldk_node.udl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,6 @@ interface Node {
158158
boolean verify_signature([ByRef]sequence<u8> msg, [ByRef]string sig, [ByRef]PublicKey pkey);
159159
[Throws=NodeError]
160160
bytes export_pathfinding_scores();
161-
[Throws=NodeError]
162-
void set_paths_to_static_invoice_server(bytes paths);
163-
[Throws=NodeError]
164-
bytes blinded_paths_for_async_recipient(bytes recipient_id);
165161
};
166162

167163
[Enum]
@@ -216,6 +212,10 @@ interface Bolt12Payment {
216212
Refund initiate_refund(u64 amount_msat, u32 expiry_secs, u64? quantity, string? payer_note);
217213
[Throws=NodeError]
218214
Offer receive_async();
215+
[Throws=NodeError]
216+
void set_paths_to_static_invoice_server(bytes paths);
217+
[Throws=NodeError]
218+
bytes blinded_paths_for_async_recipient(bytes recipient_id);
219219
};
220220

221221
interface SpontaneousPayment {

src/event.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
66
// accordance with one or both of these licenses.
77

8-
use crate::payment::static_invoice_store::StaticInvoiceStore;
8+
use crate::payment::asynchronous::static_invoice_store::StaticInvoiceStore;
99
use crate::types::{CustomTlvRecord, DynStore, PaymentStore, Sweeper, Wallet};
1010
use crate::{
1111
hex_utils, BumpTransactionEventHandler, ChannelManager, Error, Graph, PeerInfo, PeerStore,

src/lib.rs

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ use gossip::GossipSource;
136136
use graph::NetworkGraph;
137137
use io::utils::write_node_metrics;
138138
use liquidity::{LSPS1Liquidity, LiquiditySource};
139-
use payment::static_invoice_store::StaticInvoiceStore;
139+
use payment::asynchronous::static_invoice_store::StaticInvoiceStore;
140140
use payment::{
141141
Bolt11Payment, Bolt12Payment, OnchainPayment, PaymentDetails, SpontaneousPayment,
142142
UnifiedQrPayment,
@@ -151,16 +151,13 @@ pub use types::{ChannelDetails, CustomTlvRecord, PeerDetails, UserChannelId};
151151

152152
use logger::{log_debug, log_error, log_info, log_trace, LdkLogger, Logger};
153153

154-
use lightning::blinded_path::message::BlindedMessagePath;
155154
use lightning::chain::BestBlock;
156155
use lightning::events::bump_transaction::Wallet as LdkWallet;
157156
use lightning::impl_writeable_tlv_based;
158157
use lightning::ln::channel_state::ChannelShutdownState;
159158
use lightning::ln::channelmanager::PaymentId;
160159
use lightning::ln::msgs::SocketAddress;
161160
use lightning::routing::gossip::NodeAlias;
162-
use lightning::util::ser::Readable;
163-
use lightning::util::ser::Writeable;
164161

165162
use lightning_background_processor::process_events_async_with_kv_store_sync;
166163

@@ -829,6 +826,7 @@ impl Node {
829826
Bolt12Payment::new(
830827
Arc::clone(&self.channel_manager),
831828
Arc::clone(&self.payment_store),
829+
self.config.async_payment_services_enabled,
832830
Arc::clone(&self.is_running),
833831
Arc::clone(&self.logger),
834832
)
@@ -842,6 +840,7 @@ impl Node {
842840
Arc::new(Bolt12Payment::new(
843841
Arc::clone(&self.channel_manager),
844842
Arc::clone(&self.payment_store),
843+
self.config.async_payment_services_enabled,
845844
Arc::clone(&self.is_running),
846845
Arc::clone(&self.logger),
847846
))
@@ -1492,43 +1491,6 @@ impl Node {
14921491
Error::PersistenceFailed
14931492
})
14941493
}
1495-
1496-
/// Sets the [`BlindedMessagePath`]s that we will use as an async recipient to interactively build [`Offer`]s with a
1497-
/// static invoice server, so the server can serve [`StaticInvoice`]s to payers on our behalf when we're offline.
1498-
///
1499-
/// [`Offer`]: lightning::offers::offer::Offer
1500-
/// [`StaticInvoice`]: lightning::offers::static_invoice::StaticInvoice
1501-
pub fn set_paths_to_static_invoice_server(&self, paths: Vec<u8>) -> Result<(), Error> {
1502-
let decoded_paths = <Vec<BlindedMessagePath> as Readable>::read(&mut &paths[..])
1503-
.or(Err(Error::InvalidBlindedPaths))?;
1504-
1505-
self.channel_manager
1506-
.set_paths_to_static_invoice_server(decoded_paths)
1507-
.or(Err(Error::InvalidBlindedPaths))
1508-
}
1509-
1510-
/// [`BlindedMessagePath`]s for an async recipient to communicate with this node and interactively
1511-
/// build [`Offer`]s and [`StaticInvoice`]s for receiving async payments.
1512-
///
1513-
/// [`Offer`]: lightning::offers::offer::Offer
1514-
/// [`StaticInvoice`]: lightning::offers::static_invoice::StaticInvoice
1515-
pub fn blinded_paths_for_async_recipient(
1516-
&self, recipient_id: Vec<u8>,
1517-
) -> Result<Vec<u8>, Error> {
1518-
if !self.config.async_payment_services_enabled {
1519-
return Err(Error::AsyncPaymentServicesDisabled);
1520-
}
1521-
1522-
let paths = self
1523-
.channel_manager
1524-
.blinded_paths_for_async_recipient(recipient_id, None)
1525-
.or(Err(Error::InvalidBlindedPaths))?;
1526-
1527-
let mut bytes = Vec::new();
1528-
paths.write(&mut bytes).or(Err(Error::InvalidBlindedPaths))?;
1529-
1530-
Ok(bytes)
1531-
}
15321494
}
15331495

15341496
impl Drop for Node {

src/payment/asynchronous/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mod rate_limiter;
2+
pub(crate) mod static_invoice_store;

src/payment/rate_limiter.rs renamed to src/payment/asynchronous/rate_limiter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl RateLimiter {
6363

6464
#[cfg(test)]
6565
mod tests {
66-
use crate::payment::rate_limiter::RateLimiter;
66+
use crate::payment::asynchronous::rate_limiter::RateLimiter;
6767

6868
use std::time::Duration;
6969

src/payment/static_invoice_store.rs renamed to src/payment/asynchronous/static_invoice_store.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::hex_utils;
22
use crate::io::STATIC_INVOICES_PRIMARY_NAMESPACE;
3-
use crate::payment::rate_limiter::RateLimiter;
3+
use crate::payment::asynchronous::rate_limiter::RateLimiter;
44
use crate::types::DynStore;
55

66
use bitcoin::hashes::sha256::Hash as Sha256;
@@ -122,7 +122,8 @@ mod tests {
122122
use lightning::util::test_utils::TestStore;
123123
use lightning_types::features::BlindedHopFeatures;
124124

125-
use crate::{payment::static_invoice_store::StaticInvoiceStore, types::DynStore};
125+
use crate::payment::asynchronous::static_invoice_store::StaticInvoiceStore;
126+
use crate::types::DynStore;
126127

127128
#[tokio::test]
128129
async fn static_invoice_store_test() {

src/payment/bolt12.rs

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ use crate::logger::{log_error, log_info, LdkLogger, Logger};
1616
use crate::payment::store::{PaymentDetails, PaymentDirection, PaymentKind, PaymentStatus};
1717
use crate::types::{ChannelManager, PaymentStore};
1818

19+
use lightning::blinded_path::message::BlindedMessagePath;
1920
use lightning::ln::channelmanager::{PaymentId, Retry};
2021
use lightning::offers::offer::{Amount, Offer as LdkOffer, Quantity};
2122
use lightning::offers::parse::Bolt12SemanticError;
2223
use lightning::routing::router::RouteParametersConfig;
2324

25+
#[cfg(feature = "uniffi")]
26+
use lightning::util::ser::{Readable, Writeable};
2427
use lightning_types::string::UntrustedString;
2528

2629
use rand::RngCore;
@@ -54,15 +57,16 @@ pub struct Bolt12Payment {
5457
channel_manager: Arc<ChannelManager>,
5558
payment_store: Arc<PaymentStore>,
5659
is_running: Arc<RwLock<bool>>,
60+
async_payment_services_enabled: bool,
5761
logger: Arc<Logger>,
5862
}
5963

6064
impl Bolt12Payment {
6165
pub(crate) fn new(
6266
channel_manager: Arc<ChannelManager>, payment_store: Arc<PaymentStore>,
63-
is_running: Arc<RwLock<bool>>, logger: Arc<Logger>,
67+
async_payment_services_enabled: bool, is_running: Arc<RwLock<bool>>, logger: Arc<Logger>,
6468
) -> Self {
65-
Self { channel_manager, payment_store, is_running, logger }
69+
Self { channel_manager, payment_store, async_payment_services_enabled, is_running, logger }
6670
}
6771

6872
/// Send a payment given an offer.
@@ -453,12 +457,11 @@ impl Bolt12Payment {
453457

454458
/// Retrieve an [`Offer`] for receiving async payments as an often-offline recipient.
455459
///
456-
/// Will only return an offer if [`Node::set_paths_to_static_invoice_server`] was called and we succeeded in
457-
/// interactively building a [`StaticInvoice`] with the static invoice server.
460+
/// Will only return an offer if [`Bolt12Payment::set_paths_to_static_invoice_server`] was called and we succeeded
461+
/// in interactively building a [`StaticInvoice`] with the static invoice server.
458462
///
459463
/// Useful for posting offers to receive payments later, such as posting an offer on a website.
460464
///
461-
/// [`Node::set_paths_to_static_invoice_server`]: crate::Node::set_paths_to_static_invoice_server
462465
/// [`StaticInvoice`]: lightning::offers::static_invoice::StaticInvoice
463466
/// [`Offer`]: lightning::offers::offer::Offer
464467
pub fn receive_async(&self) -> Result<Offer, Error> {
@@ -467,4 +470,73 @@ impl Bolt12Payment {
467470
.map(maybe_wrap)
468471
.or(Err(Error::OfferCreationFailed))
469472
}
473+
474+
/// Sets the [`BlindedMessagePath`]s that we will use as an async recipient to interactively build [`Offer`]s with a
475+
/// static invoice server, so the server can serve [`StaticInvoice`]s to payers on our behalf when we're offline.
476+
///
477+
/// [`Offer`]: lightning::offers::offer::Offer
478+
/// [`StaticInvoice`]: lightning::offers::static_invoice::StaticInvoice
479+
#[cfg(not(feature = "uniffi"))]
480+
pub fn set_paths_to_static_invoice_server(
481+
&self, paths: Vec<BlindedMessagePath>,
482+
) -> Result<(), Error> {
483+
self.channel_manager
484+
.set_paths_to_static_invoice_server(paths)
485+
.or(Err(Error::InvalidBlindedPaths))
486+
}
487+
488+
/// Sets the [`BlindedMessagePath`]s that we will use as an async recipient to interactively build [`Offer`]s with a
489+
/// static invoice server, so the server can serve [`StaticInvoice`]s to payers on our behalf when we're offline.
490+
///
491+
/// [`Offer`]: lightning::offers::offer::Offer
492+
/// [`StaticInvoice`]: lightning::offers::static_invoice::StaticInvoice
493+
#[cfg(feature = "uniffi")]
494+
pub fn set_paths_to_static_invoice_server(&self, paths: Vec<u8>) -> Result<(), Error> {
495+
let decoded_paths = <Vec<BlindedMessagePath> as Readable>::read(&mut &paths[..])
496+
.or(Err(Error::InvalidBlindedPaths))?;
497+
498+
self.channel_manager
499+
.set_paths_to_static_invoice_server(decoded_paths)
500+
.or(Err(Error::InvalidBlindedPaths))
501+
}
502+
503+
/// [`BlindedMessagePath`]s for an async recipient to communicate with this node and interactively
504+
/// build [`Offer`]s and [`StaticInvoice`]s for receiving async payments.
505+
///
506+
/// [`Offer`]: lightning::offers::offer::Offer
507+
/// [`StaticInvoice`]: lightning::offers::static_invoice::StaticInvoice
508+
#[cfg(not(feature = "uniffi"))]
509+
pub fn blinded_paths_for_async_recipient(
510+
&self, recipient_id: Vec<u8>,
511+
) -> Result<Vec<BlindedMessagePath>, Error> {
512+
self.blinded_paths_for_async_recipient_internal(recipient_id)
513+
}
514+
515+
/// [`BlindedMessagePath`]s for an async recipient to communicate with this node and interactively
516+
/// build [`Offer`]s and [`StaticInvoice`]s for receiving async payments.
517+
///
518+
/// [`Offer`]: lightning::offers::offer::Offer
519+
/// [`StaticInvoice`]: lightning::offers::static_invoice::StaticInvoice
520+
#[cfg(feature = "uniffi")]
521+
pub fn blinded_paths_for_async_recipient(
522+
&self, recipient_id: Vec<u8>,
523+
) -> Result<Vec<u8>, Error> {
524+
let paths = self.blinded_paths_for_async_recipient_internal(recipient_id)?;
525+
526+
let mut bytes = Vec::new();
527+
paths.write(&mut bytes).or(Err(Error::InvalidBlindedPaths))?;
528+
Ok(bytes)
529+
}
530+
531+
fn blinded_paths_for_async_recipient_internal(
532+
&self, recipient_id: Vec<u8>,
533+
) -> Result<Vec<BlindedMessagePath>, Error> {
534+
if !self.async_payment_services_enabled {
535+
return Err(Error::AsyncPaymentServicesDisabled);
536+
}
537+
538+
self.channel_manager
539+
.blinded_paths_for_async_recipient(recipient_id, None)
540+
.or(Err(Error::InvalidBlindedPaths))
541+
}
470542
}

src/payment/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77

88
//! Objects for different types of payments.
99
10+
pub(crate) mod asynchronous;
1011
mod bolt11;
1112
mod bolt12;
1213
mod onchain;
13-
mod rate_limiter;
1414
mod spontaneous;
15-
pub(crate) mod static_invoice_store;
1615
pub(crate) mod store;
1716
mod unified_qr;
1817

tests/integration_tests_rust.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,8 +1211,9 @@ fn static_invoice_server() {
12111211
}
12121212

12131213
let recipient_id = vec![1, 2, 3];
1214-
let blinded_paths = node_receiver_lsp.blinded_paths_for_async_recipient(recipient_id).unwrap();
1215-
node_receiver.set_paths_to_static_invoice_server(blinded_paths).unwrap();
1214+
let blinded_paths =
1215+
node_receiver_lsp.bolt12_payment().blinded_paths_for_async_recipient(recipient_id).unwrap();
1216+
node_receiver.bolt12_payment().set_paths_to_static_invoice_server(blinded_paths).unwrap();
12161217

12171218
let offer = loop {
12181219
if let Ok(offer) = node_receiver.bolt12_payment().receive_async() {

0 commit comments

Comments
 (0)