Skip to content

Commit eaf10ae

Browse files
committed
Introduce Bolt12Payment API
1 parent fd46040 commit eaf10ae

File tree

6 files changed

+343
-2
lines changed

6 files changed

+343
-2
lines changed

bindings/ldk_node.udl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ interface Node {
5454
PublicKey node_id();
5555
sequence<SocketAddress>? listening_addresses();
5656
Bolt11Payment bolt11_payment();
57+
Bolt12Payment bolt12_payment();
5758
SpontaneousPayment spontaneous_payment();
5859
OnchainPayment onchain_payment();
5960
[Throws=NodeError]
@@ -99,6 +100,17 @@ interface Bolt11Payment {
99100
Bolt11Invoice receive_variable_amount_via_jit_channel([ByRef]string description, u32 expiry_secs, u64? max_proportional_lsp_fee_limit_ppm_msat);
100101
};
101102

103+
interface Bolt12Payment {
104+
[Throws=NodeError]
105+
PaymentId send([ByRef]Offer offer, string? payer_note);
106+
[Throws=NodeError]
107+
PaymentId send_using_amount([ByRef]Offer offer, string? payer_note, u64 amount_msat);
108+
[Throws=NodeError]
109+
Offer receive(u64 amount_msat, [ByRef]string description);
110+
[Throws=NodeError]
111+
Offer receive_variable_amount([ByRef]string description);
112+
};
113+
102114
interface SpontaneousPayment {
103115
[Throws=NodeError]
104116
PaymentId send(u64 amount_msat, PublicKey node_id);
@@ -122,6 +134,7 @@ enum NodeError {
122134
"OnchainTxCreationFailed",
123135
"ConnectionFailed",
124136
"InvoiceCreationFailed",
137+
"OfferCreationFailed",
125138
"PaymentSendingFailed",
126139
"ProbeSendingFailed",
127140
"ChannelCreationFailed",
@@ -146,9 +159,11 @@ enum NodeError {
146159
"InvalidPaymentSecret",
147160
"InvalidAmount",
148161
"InvalidInvoice",
162+
"InvalidOffer",
149163
"InvalidChannelId",
150164
"InvalidNetwork",
151165
"DuplicatePayment",
166+
"UnsupportedCurrency",
152167
"InsufficientFunds",
153168
"LiquiditySourceUnavailable",
154169
"LiquidityFeeTooHigh",
@@ -374,6 +389,9 @@ typedef string Address;
374389
[Custom]
375390
typedef string Bolt11Invoice;
376391

392+
[Custom]
393+
typedef string Offer;
394+
377395
[Custom]
378396
typedef string OfferId;
379397

src/error.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pub enum Error {
1313
ConnectionFailed,
1414
/// Invoice creation failed.
1515
InvoiceCreationFailed,
16+
/// Offer creation failed.
17+
OfferCreationFailed,
1618
/// Sending a payment has failed.
1719
PaymentSendingFailed,
1820
/// Sending a payment probe has failed.
@@ -61,12 +63,16 @@ pub enum Error {
6163
InvalidAmount,
6264
/// The given invoice is invalid.
6365
InvalidInvoice,
66+
/// The given offer is invalid.
67+
InvalidOffer,
6468
/// The given channel ID is invalid.
6569
InvalidChannelId,
6670
/// The given network is invalid.
6771
InvalidNetwork,
6872
/// A payment with the given hash has already been initiated.
6973
DuplicatePayment,
74+
/// The provided offer was denonminated in an unsupported currency.
75+
UnsupportedCurrency,
7076
/// The available funds are insufficient to complete the given operation.
7177
InsufficientFunds,
7278
/// The given operation failed due to the required liquidity source being unavailable.
@@ -85,6 +91,7 @@ impl fmt::Display for Error {
8591
},
8692
Self::ConnectionFailed => write!(f, "Network connection closed."),
8793
Self::InvoiceCreationFailed => write!(f, "Failed to create invoice."),
94+
Self::OfferCreationFailed => write!(f, "Failed to create offer."),
8895
Self::PaymentSendingFailed => write!(f, "Failed to send the given payment."),
8996
Self::ProbeSendingFailed => write!(f, "Failed to send the given payment probe."),
9097
Self::ChannelCreationFailed => write!(f, "Failed to create channel."),
@@ -111,6 +118,7 @@ impl fmt::Display for Error {
111118
Self::InvalidPaymentSecret => write!(f, "The given payment secret is invalid."),
112119
Self::InvalidAmount => write!(f, "The given amount is invalid."),
113120
Self::InvalidInvoice => write!(f, "The given invoice is invalid."),
121+
Self::InvalidOffer => write!(f, "The given offer is invalid."),
114122
Self::InvalidChannelId => write!(f, "The given channel ID is invalid."),
115123
Self::InvalidNetwork => write!(f, "The given network is invalid."),
116124
Self::DuplicatePayment => {
@@ -119,6 +127,9 @@ impl fmt::Display for Error {
119127
Self::InsufficientFunds => {
120128
write!(f, "The available funds are insufficient to complete the given operation.")
121129
},
130+
Self::UnsupportedCurrency => {
131+
write!(f, "The provided offer was denonminated in an unsupported currency.")
132+
},
122133
Self::LiquiditySourceUnavailable => {
123134
write!(f, "The given operation failed due to the required liquidity source being unavailable.")
124135
},

src/lib.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ use event::{EventHandler, EventQueue};
130130
use gossip::GossipSource;
131131
use liquidity::LiquiditySource;
132132
use payment::store::PaymentStore;
133-
use payment::{Bolt11Payment, OnchainPayment, PaymentDetails, SpontaneousPayment};
133+
use payment::{Bolt11Payment, Bolt12Payment, OnchainPayment, PaymentDetails, SpontaneousPayment};
134134
use peer_store::{PeerInfo, PeerStore};
135135
use types::{
136136
Broadcaster, ChainMonitor, ChannelManager, DynStore, FeeEstimator, KeysManager, NetworkGraph,
@@ -846,6 +846,32 @@ impl Node {
846846
))
847847
}
848848

849+
/// Returns a payment handler allowing to create and pay [BOLT 12] offers and refunds.
850+
///
851+
/// [BOLT 12]: https://github.com/lightning/bolts/blob/master/12-offer-encoding.md
852+
#[cfg(not(feature = "uniffi"))]
853+
pub fn bolt12_payment(&self) -> Arc<Bolt12Payment> {
854+
Arc::new(Bolt12Payment::new(
855+
Arc::clone(&self.runtime),
856+
Arc::clone(&self.channel_manager),
857+
Arc::clone(&self.payment_store),
858+
Arc::clone(&self.logger),
859+
))
860+
}
861+
862+
/// Returns a payment handler allowing to create and pay [BOLT 12] offers and refunds.
863+
///
864+
/// [BOLT 12]: https://github.com/lightning/bolts/blob/master/12-offer-encoding.md
865+
#[cfg(feature = "uniffi")]
866+
pub fn bolt12_payment(&self) -> Arc<Bolt12Payment> {
867+
Arc::new(Bolt12Payment::new(
868+
Arc::clone(&self.runtime),
869+
Arc::clone(&self.channel_manager),
870+
Arc::clone(&self.payment_store),
871+
Arc::clone(&self.logger),
872+
))
873+
}
874+
849875
/// Returns a payment handler allowing to send spontaneous ("keysend") payments.
850876
#[cfg(not(feature = "uniffi"))]
851877
pub fn spontaneous_payment(&self) -> SpontaneousPayment {

0 commit comments

Comments
 (0)