Skip to content

Commit

Permalink
Introduce Bolt12PaymentHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Mar 5, 2024
1 parent f2bbafc commit 5f28108
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
4 changes: 4 additions & 0 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ interface Node {
PublicKey node_id();
sequence<SocketAddress>? listening_addresses();
Bolt11PaymentHandler bolt11_payment();
Bolt12PaymentHandler bolt12_payment();
SpontaneousPaymentHandler spontaneous_payment();
OnchainPaymentHandler onchain_payment();
[Throws=NodeError]
Expand Down Expand Up @@ -98,6 +99,9 @@ interface Bolt11PaymentHandler {
Bolt11Invoice receive_variable_amount_via_jit_channel([ByRef]string description, u32 expiry_secs, u64? max_proportional_lsp_fee_limit_ppm_msat);
};

interface Bolt12PaymentHandler {
};

interface SpontaneousPaymentHandler {
[Throws=NodeError]
PaymentHash send(u64 amount_msat, PublicKey node_id);
Expand Down
19 changes: 18 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ use gossip::GossipSource;
use liquidity::LiquiditySource;
use payment::payment_store::PaymentStore;
use payment::{
Bolt11PaymentHandler, OnchainPaymentHandler, PaymentDetails, SpontaneousPaymentHandler,
Bolt11PaymentHandler, Bolt12PaymentHandler, OnchainPaymentHandler, PaymentDetails,
SpontaneousPaymentHandler,
};
use peer_store::{PeerInfo, PeerStore};
use types::{
Expand Down Expand Up @@ -758,6 +759,22 @@ impl Node {
))
}

/// Returns a payment handler allowing to create and pay [BOLT 12] offers and refunds.
///
/// [BOLT 12]: https://github.com/lightning/bolts/blob/master/12-offer-encoding.md
pub fn bolt12_payment(&self) -> Arc<Bolt12PaymentHandler> {
Arc::new(Bolt12PaymentHandler::new(
Arc::clone(&self.runtime),
Arc::clone(&self.channel_manager),
Arc::clone(&self.connection_manager),
Arc::clone(&self.keys_manager),
Arc::clone(&self.payment_store),
Arc::clone(&self.peer_store),
Arc::clone(&self.config),
Arc::clone(&self.logger),
))
}

/// Returns a payment handler allowing to send spontaneous ("keysend") payments.
pub fn spontaneous_payment(&self) -> Arc<SpontaneousPaymentHandler> {
Arc::new(SpontaneousPaymentHandler::new(
Expand Down
51 changes: 51 additions & 0 deletions src/payment/bolt12.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//! Holds a payment handler allowing to create and pay [BOLT 12] offers and refunds.
//!
//! [BOLT 12]: https://github.com/lightning/bolts/blob/master/12-offer-encoding.md
use crate::config::Config;
use crate::connection::ConnectionManager;
use crate::logger::FilesystemLogger;
use crate::payment::payment_store::PaymentStore;
use crate::peer_store::PeerStore;
use crate::types::{ChannelManager, KeysManager};

use std::sync::{Arc, RwLock};

/// A payment handler allowing to create and pay [BOLT 12] offers and refunds.
///
/// Should be retrieved by calling [`Node::bolt12_payment`].
///
/// [BOLT 12]: https://github.com/lightning/bolts/blob/master/12-offer-encoding.md
/// [`Node::bolt12_payment`]: crate::Node::bolt12_payment
pub struct Bolt12PaymentHandler {
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
channel_manager: Arc<ChannelManager>,
connection_manager: Arc<ConnectionManager<Arc<FilesystemLogger>>>,
keys_manager: Arc<KeysManager>,
payment_store: Arc<PaymentStore<Arc<FilesystemLogger>>>,
peer_store: Arc<PeerStore<Arc<FilesystemLogger>>>,
config: Arc<Config>,
logger: Arc<FilesystemLogger>,
}

impl Bolt12PaymentHandler {
pub(crate) fn new(
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
channel_manager: Arc<ChannelManager>,
connection_manager: Arc<ConnectionManager<Arc<FilesystemLogger>>>,
keys_manager: Arc<KeysManager>, payment_store: Arc<PaymentStore<Arc<FilesystemLogger>>>,
peer_store: Arc<PeerStore<Arc<FilesystemLogger>>>, config: Arc<Config>,
logger: Arc<FilesystemLogger>,
) -> Self {
Self {
runtime,
channel_manager,
connection_manager,
keys_manager,
payment_store,
peer_store,
config,
logger,
}
}
}
2 changes: 2 additions & 0 deletions src/payment/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! Objects for different types of payments.
mod bolt11;
mod bolt12;
mod onchain;
pub(crate) mod payment_store;
mod spontaneous;

pub use bolt11::Bolt11PaymentHandler;
pub use bolt12::Bolt12PaymentHandler;
pub use onchain::OnchainPaymentHandler;
pub use payment_store::{LSPFeeLimits, PaymentDetails, PaymentDirection, PaymentStatus};
pub use spontaneous::SpontaneousPaymentHandler;

0 comments on commit 5f28108

Please sign in to comment.