Skip to content

Commit f8b5ec4

Browse files
committed
Introduce Bolt12Payment API
1 parent 73cad4b commit f8b5ec4

File tree

6 files changed

+342
-2
lines changed

6 files changed

+342
-2
lines changed

bindings/ldk_node.udl

Lines changed: 19 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,8 @@ enum NodeError {
122134
"OnchainTxCreationFailed",
123135
"ConnectionFailed",
124136
"InvoiceCreationFailed",
137+
"InvoiceRequestCreationFailed",
138+
"OfferCreationFailed",
125139
"PaymentSendingFailed",
126140
"ProbeSendingFailed",
127141
"ChannelCreationFailed",
@@ -146,9 +160,11 @@ enum NodeError {
146160
"InvalidPaymentSecret",
147161
"InvalidAmount",
148162
"InvalidInvoice",
163+
"InvalidOffer",
149164
"InvalidChannelId",
150165
"InvalidNetwork",
151166
"DuplicatePayment",
167+
"UnsupportedCurrency",
152168
"InsufficientFunds",
153169
"LiquiditySourceUnavailable",
154170
"LiquidityFeeTooHigh",
@@ -374,6 +390,9 @@ typedef string Address;
374390
[Custom]
375391
typedef string Bolt11Invoice;
376392

393+
[Custom]
394+
typedef string Offer;
395+
377396
[Custom]
378397
typedef string OfferId;
379398

src/error.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ pub enum Error {
1313
ConnectionFailed,
1414
/// Invoice creation failed.
1515
InvoiceCreationFailed,
16+
/// Invoice request creation failed.
17+
InvoiceRequestCreationFailed,
18+
/// Offer creation failed.
19+
OfferCreationFailed,
1620
/// Sending a payment has failed.
1721
PaymentSendingFailed,
1822
/// Sending a payment probe has failed.
@@ -61,12 +65,16 @@ pub enum Error {
6165
InvalidAmount,
6266
/// The given invoice is invalid.
6367
InvalidInvoice,
68+
/// The given offer is invalid.
69+
InvalidOffer,
6470
/// The given channel ID is invalid.
6571
InvalidChannelId,
6672
/// The given network is invalid.
6773
InvalidNetwork,
6874
/// A payment with the given hash has already been initiated.
6975
DuplicatePayment,
76+
/// The provided offer was denonminated in an unsupported currency.
77+
UnsupportedCurrency,
7078
/// The available funds are insufficient to complete the given operation.
7179
InsufficientFunds,
7280
/// The given operation failed due to the required liquidity source being unavailable.
@@ -85,6 +93,8 @@ impl fmt::Display for Error {
8593
},
8694
Self::ConnectionFailed => write!(f, "Network connection closed."),
8795
Self::InvoiceCreationFailed => write!(f, "Failed to create invoice."),
96+
Self::InvoiceRequestCreationFailed => write!(f, "Failed to create invoice request."),
97+
Self::OfferCreationFailed => write!(f, "Failed to create offer."),
8898
Self::PaymentSendingFailed => write!(f, "Failed to send the given payment."),
8999
Self::ProbeSendingFailed => write!(f, "Failed to send the given payment probe."),
90100
Self::ChannelCreationFailed => write!(f, "Failed to create channel."),
@@ -111,6 +121,7 @@ impl fmt::Display for Error {
111121
Self::InvalidPaymentSecret => write!(f, "The given payment secret is invalid."),
112122
Self::InvalidAmount => write!(f, "The given amount is invalid."),
113123
Self::InvalidInvoice => write!(f, "The given invoice is invalid."),
124+
Self::InvalidOffer => write!(f, "The given offer is invalid."),
114125
Self::InvalidChannelId => write!(f, "The given channel ID is invalid."),
115126
Self::InvalidNetwork => write!(f, "The given network is invalid."),
116127
Self::DuplicatePayment => {
@@ -119,6 +130,9 @@ impl fmt::Display for Error {
119130
Self::InsufficientFunds => {
120131
write!(f, "The available funds are insufficient to complete the given operation.")
121132
},
133+
Self::UnsupportedCurrency => {
134+
write!(f, "The provided offer was denonminated in an unsupported currency.")
135+
},
122136
Self::LiquiditySourceUnavailable => {
123137
write!(f, "The given operation failed due to the required liquidity source being unavailable.")
124138
},

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)