Skip to content

Commit b1ca4e2

Browse files
committed
f: move blinded path methods to bolt12payment
1 parent b120a03 commit b1ca4e2

File tree

9 files changed

+93
-55
lines changed

9 files changed

+93
-55
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
))
@@ -1487,43 +1486,6 @@ impl Node {
14871486
Error::PersistenceFailed
14881487
})
14891488
}
1490-
1491-
/// Sets the [`BlindedMessagePath`]s that we will use as an async recipient to interactively build [`Offer`]s with a
1492-
/// static invoice server, so the server can serve [`StaticInvoice`]s to payers on our behalf when we're offline.
1493-
///
1494-
/// [`Offer`]: lightning::offers::offer::Offer
1495-
/// [`StaticInvoice`]: lightning::offers::static_invoice::StaticInvoice
1496-
pub fn set_paths_to_static_invoice_server(&self, paths: Vec<u8>) -> Result<(), Error> {
1497-
let decoded_paths = <Vec<BlindedMessagePath> as Readable>::read(&mut &paths[..])
1498-
.or(Err(Error::InvalidBlindedPaths))?;
1499-
1500-
self.channel_manager
1501-
.set_paths_to_static_invoice_server(decoded_paths)
1502-
.or(Err(Error::InvalidBlindedPaths))
1503-
}
1504-
1505-
/// [`BlindedMessagePath`]s for an async recipient to communicate with this node and interactively
1506-
/// build [`Offer`]s and [`StaticInvoice`]s for receiving async payments.
1507-
///
1508-
/// [`Offer`]: lightning::offers::offer::Offer
1509-
/// [`StaticInvoice`]: lightning::offers::static_invoice::StaticInvoice
1510-
pub fn blinded_paths_for_async_recipient(
1511-
&self, recipient_id: Vec<u8>,
1512-
) -> Result<Vec<u8>, Error> {
1513-
if !self.config.async_payment_services_enabled {
1514-
return Err(Error::AsyncPaymentServicesDisabled);
1515-
}
1516-
1517-
let paths = self
1518-
.channel_manager
1519-
.blinded_paths_for_async_recipient(recipient_id, None)
1520-
.or(Err(Error::InvalidBlindedPaths))?;
1521-
1522-
let mut bytes = Vec::new();
1523-
paths.write(&mut bytes).or(Err(Error::InvalidBlindedPaths))?;
1524-
1525-
Ok(bytes)
1526-
}
15271489
}
15281490

15291491
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: 75 additions & 2 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.
@@ -467,4 +471,73 @@ impl Bolt12Payment {
467471
.map(maybe_wrap)
468472
.or(Err(Error::OfferCreationFailed))
469473
}
474+
475+
/// Sets the [`BlindedMessagePath`]s that we will use as an async recipient to interactively build [`Offer`]s with a
476+
/// static invoice server, so the server can serve [`StaticInvoice`]s to payers on our behalf when we're offline.
477+
///
478+
/// [`Offer`]: lightning::offers::offer::Offer
479+
/// [`StaticInvoice`]: lightning::offers::static_invoice::StaticInvoice
480+
#[cfg(not(feature = "uniffi"))]
481+
pub fn set_paths_to_static_invoice_server(
482+
&self, paths: Vec<BlindedMessagePath>,
483+
) -> Result<(), Error> {
484+
self.channel_manager
485+
.set_paths_to_static_invoice_server(paths)
486+
.or(Err(Error::InvalidBlindedPaths))
487+
}
488+
489+
/// Sets the [`BlindedMessagePath`]s that we will use as an async recipient to interactively build [`Offer`]s with a
490+
/// static invoice server, so the server can serve [`StaticInvoice`]s to payers on our behalf when we're offline.
491+
///
492+
/// [`Offer`]: lightning::offers::offer::Offer
493+
/// [`StaticInvoice`]: lightning::offers::static_invoice::StaticInvoice
494+
#[cfg(feature = "uniffi")]
495+
pub fn set_paths_to_static_invoice_server(&self, paths: Vec<u8>) -> Result<(), Error> {
496+
let decoded_paths = <Vec<BlindedMessagePath> as Readable>::read(&mut &paths[..])
497+
.or(Err(Error::InvalidBlindedPaths))?;
498+
499+
self.channel_manager
500+
.set_paths_to_static_invoice_server(decoded_paths)
501+
.or(Err(Error::InvalidBlindedPaths))
502+
}
503+
504+
/// [`BlindedMessagePath`]s for an async recipient to communicate with this node and interactively
505+
/// build [`Offer`]s and [`StaticInvoice`]s for receiving async payments.
506+
///
507+
/// [`Offer`]: lightning::offers::offer::Offer
508+
/// [`StaticInvoice`]: lightning::offers::static_invoice::StaticInvoice
509+
#[cfg(not(feature = "uniffi"))]
510+
pub fn blinded_paths_for_async_recipient(
511+
&self, recipient_id: Vec<u8>,
512+
) -> Result<Vec<BlindedMessagePath>, Error> {
513+
self.blinded_paths_internal(recipient_id)
514+
}
515+
516+
/// [`BlindedMessagePath`]s for an async recipient to communicate with this node and interactively
517+
/// build [`Offer`]s and [`StaticInvoice`]s for receiving async payments.
518+
///
519+
/// [`Offer`]: lightning::offers::offer::Offer
520+
/// [`StaticInvoice`]: lightning::offers::static_invoice::StaticInvoice
521+
#[cfg(feature = "uniffi")]
522+
pub fn blinded_paths_for_async_recipient(
523+
&self, recipient_id: Vec<u8>,
524+
) -> Result<Vec<u8>, Error> {
525+
let paths = self.blinded_paths_internal(recipient_id)?;
526+
527+
let mut bytes = Vec::new();
528+
paths.write(&mut bytes).or(Err(Error::InvalidBlindedPaths))?;
529+
Ok(bytes)
530+
}
531+
532+
fn blinded_paths_internal(
533+
&self, recipient_id: Vec<u8>,
534+
) -> Result<Vec<BlindedMessagePath>, Error> {
535+
if !self.async_payment_services_enabled {
536+
return Err(Error::AsyncPaymentServicesDisabled);
537+
}
538+
539+
self.channel_manager
540+
.blinded_paths_for_async_recipient(recipient_id, None)
541+
.or(Err(Error::InvalidBlindedPaths))
542+
}
470543
}

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)