Skip to content

Commit 73cad4b

Browse files
committed
Add Bolt12 variant to PaymentKind
1 parent 7c9216e commit 73cad4b

File tree

4 files changed

+95
-4
lines changed

4 files changed

+95
-4
lines changed

bindings/ldk_node.udl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ enum NodeError {
139139
"InvalidSocketAddress",
140140
"InvalidPublicKey",
141141
"InvalidSecretKey",
142+
"InvalidOfferId",
142143
"InvalidPaymentId",
143144
"InvalidPaymentHash",
144145
"InvalidPaymentPreimage",
@@ -225,6 +226,8 @@ interface PaymentKind {
225226
Onchain();
226227
Bolt11(PaymentHash hash, PaymentPreimage? preimage, PaymentSecret? secret);
227228
Bolt11Jit(PaymentHash hash, PaymentPreimage? preimage, PaymentSecret? secret, LSPFeeLimits lsp_fee_limits);
229+
Bolt12Offer(PaymentHash? hash, PaymentPreimage? preimage, PaymentSecret? secret, OfferId offer_id);
230+
Bolt12Refund(PaymentHash? hash, PaymentPreimage? preimage, PaymentSecret? secret);
228231
Spontaneous(PaymentHash hash, PaymentPreimage? preimage);
229232
};
230233

@@ -371,6 +374,9 @@ typedef string Address;
371374
[Custom]
372375
typedef string Bolt11Invoice;
373376

377+
[Custom]
378+
typedef string OfferId;
379+
374380
[Custom]
375381
typedef string PaymentId;
376382

src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ pub enum Error {
4747
InvalidPublicKey,
4848
/// The given secret key is invalid.
4949
InvalidSecretKey,
50+
/// The given offer id is invalid.
51+
InvalidOfferId,
5052
/// The given payment id is invalid.
5153
InvalidPaymentId,
5254
/// The given payment hash is invalid.
@@ -102,6 +104,7 @@ impl fmt::Display for Error {
102104
Self::InvalidSocketAddress => write!(f, "The given network address is invalid."),
103105
Self::InvalidPublicKey => write!(f, "The given public key is invalid."),
104106
Self::InvalidSecretKey => write!(f, "The given secret key is invalid."),
107+
Self::InvalidOfferId => write!(f, "The given offer id is invalid."),
105108
Self::InvalidPaymentId => write!(f, "The given payment id is invalid."),
106109
Self::InvalidPaymentHash => write!(f, "The given payment hash is invalid."),
107110
Self::InvalidPaymentPreimage => write!(f, "The given payment preimage is invalid."),

src/payment/store.rs

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::Error;
99
use lightning::ln::channelmanager::PaymentId;
1010
use lightning::ln::msgs::DecodeError;
1111
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
12+
use lightning::offers::offer::OfferId;
1213
use lightning::util::ser::{Readable, Writeable};
1314
use lightning::{
1415
_init_and_read_len_prefixed_tlv_fields, impl_writeable_tlv_based,
@@ -145,7 +146,6 @@ pub enum PaymentKind {
145146
/// A [BOLT 11] payment.
146147
///
147148
/// [BOLT 11]: https://github.com/lightning/bolts/blob/master/11-payment-encoding.md
148-
// TODO: Bolt11 { invoice: Option<Bolt11Invoice> },
149149
Bolt11 {
150150
/// The payment hash, i.e., the hash of the `preimage`.
151151
hash: PaymentHash,
@@ -158,7 +158,6 @@ pub enum PaymentKind {
158158
///
159159
/// [BOLT 11]: https://github.com/lightning/bolts/blob/master/11-payment-encoding.md
160160
/// [LSPS 2]: https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md
161-
// TODO: Bolt11Jit { invoice: Option<Bolt11Invoice> },
162161
Bolt11Jit {
163162
/// The payment hash, i.e., the hash of the `preimage`.
164163
hash: PaymentHash,
@@ -176,6 +175,30 @@ pub enum PaymentKind {
176175
/// [`LdkChannelConfig::accept_underpaying_htlcs`]: lightning::util::config::ChannelConfig::accept_underpaying_htlcs
177176
lsp_fee_limits: LSPFeeLimits,
178177
},
178+
/// A [BOLT 12] 'offer' payment, i.e., a payment in response to an offer.
179+
///
180+
/// [BOLT 12]: https://github.com/lightning/bolts/blob/master/12-offer-encoding.md
181+
Bolt12Offer {
182+
/// The payment hash, i.e., the hash of the `preimage`.
183+
hash: Option<PaymentHash>,
184+
/// The pre-image used by the payment.
185+
preimage: Option<PaymentPreimage>,
186+
/// The secret used by the payment.
187+
secret: Option<PaymentSecret>,
188+
/// The ID of the offer this payment is for.
189+
offer_id: OfferId,
190+
},
191+
/// A [BOLT 12] 'refund' payment, i.e., a payment without a prior offer.
192+
///
193+
/// [BOLT 12]: https://github.com/lightning/bolts/blob/master/12-offer-encoding.md
194+
Bolt12Refund {
195+
/// The payment hash, i.e., the hash of the `preimage`.
196+
hash: Option<PaymentHash>,
197+
/// The pre-image used by the payment.
198+
preimage: Option<PaymentPreimage>,
199+
/// The secret used by the payment.
200+
secret: Option<PaymentSecret>,
201+
},
179202
/// A spontaneous ("keysend") payment.
180203
Spontaneous {
181204
/// The payment hash, i.e., the hash of the `preimage`.
@@ -198,9 +221,20 @@ impl_writeable_tlv_based_enum!(PaymentKind,
198221
(4, secret, option),
199222
(6, lsp_fee_limits, required),
200223
},
224+
(6, Bolt12Offer) => {
225+
(0, hash, option),
226+
(2, preimage, option),
227+
(4, secret, option),
228+
(6, offer_id, required),
229+
},
201230
(8, Spontaneous) => {
202231
(0, hash, required),
203232
(2, preimage, option),
233+
},
234+
(10, Bolt12Refund) => {
235+
(0, hash, option),
236+
(2, preimage, option),
237+
(4, secret, option),
204238
};
205239
);
206240

@@ -227,6 +261,7 @@ impl_writeable_tlv_based!(LSPFeeLimits, {
227261
#[derive(Clone, Debug, PartialEq, Eq)]
228262
pub(crate) struct PaymentDetailsUpdate {
229263
pub id: PaymentId,
264+
pub hash: Option<Option<PaymentHash>>,
230265
pub preimage: Option<Option<PaymentPreimage>>,
231266
pub secret: Option<Option<PaymentSecret>>,
232267
pub amount_msat: Option<Option<u64>>,
@@ -236,7 +271,15 @@ pub(crate) struct PaymentDetailsUpdate {
236271

237272
impl PaymentDetailsUpdate {
238273
pub fn new(id: PaymentId) -> Self {
239-
Self { id, preimage: None, secret: None, amount_msat: None, direction: None, status: None }
274+
Self {
275+
id,
276+
hash: None,
277+
preimage: None,
278+
secret: None,
279+
amount_msat: None,
280+
direction: None,
281+
status: None,
282+
}
240283
}
241284
}
242285

@@ -299,10 +342,29 @@ where
299342
let mut locked_payments = self.payments.lock().unwrap();
300343

301344
if let Some(payment) = locked_payments.get_mut(&update.id) {
345+
if let Some(hash_opt) = update.hash {
346+
match payment.kind {
347+
PaymentKind::Bolt12Offer { ref mut hash, .. } => {
348+
debug_assert_eq!(payment.direction, PaymentDirection::Outbound,
349+
"We should only ever override payment hash for outbound BOLT 12 payments");
350+
*hash = hash_opt
351+
},
352+
PaymentKind::Bolt12Refund { ref mut hash, .. } => {
353+
debug_assert_eq!(payment.direction, PaymentDirection::Outbound,
354+
"We should only ever override payment hash for outbound BOLT 12 payments");
355+
*hash = hash_opt
356+
},
357+
_ => {
358+
// We can omit updating the hash for BOLT11 payments as the payment has will always be known from the beginning.
359+
},
360+
}
361+
}
302362
if let Some(preimage_opt) = update.preimage {
303363
match payment.kind {
304364
PaymentKind::Bolt11 { ref mut preimage, .. } => *preimage = preimage_opt,
305365
PaymentKind::Bolt11Jit { ref mut preimage, .. } => *preimage = preimage_opt,
366+
PaymentKind::Bolt12Offer { ref mut preimage, .. } => *preimage = preimage_opt,
367+
PaymentKind::Bolt12Refund { ref mut preimage, .. } => *preimage = preimage_opt,
306368
PaymentKind::Spontaneous { ref mut preimage, .. } => *preimage = preimage_opt,
307369
_ => {},
308370
}
@@ -312,6 +374,8 @@ where
312374
match payment.kind {
313375
PaymentKind::Bolt11 { ref mut secret, .. } => *secret = secret_opt,
314376
PaymentKind::Bolt11Jit { ref mut secret, .. } => *secret = secret_opt,
377+
PaymentKind::Bolt12Offer { ref mut secret, .. } => *secret = secret_opt,
378+
PaymentKind::Bolt12Refund { ref mut secret, .. } => *secret = secret_opt,
315379
_ => {},
316380
}
317381
}
@@ -327,7 +391,6 @@ where
327391
self.persist_info(&update.id, payment)?;
328392
updated = true;
329393
}
330-
331394
Ok(updated)
332395
}
333396

src/uniffi_types.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub use crate::payment::store::{LSPFeeLimits, PaymentDirection, PaymentKind, Pay
22

33
pub use lightning::events::{ClosureReason, PaymentFailureReason};
44
pub use lightning::ln::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
5+
pub use lightning::offers::offer::OfferId;
56
pub use lightning::util::string::UntrustedString;
67

78
pub use lightning_invoice::Bolt11Invoice;
@@ -75,6 +76,24 @@ impl UniffiCustomTypeConverter for Bolt11Invoice {
7576
}
7677
}
7778

79+
impl UniffiCustomTypeConverter for OfferId {
80+
type Builtin = String;
81+
82+
fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
83+
if let Some(bytes_vec) = hex_utils::to_vec(&val) {
84+
let bytes_res = bytes_vec.try_into();
85+
if let Ok(bytes) = bytes_res {
86+
return Ok(OfferId(bytes));
87+
}
88+
}
89+
Err(Error::InvalidOfferId.into())
90+
}
91+
92+
fn from_custom(obj: Self) -> Self::Builtin {
93+
hex_utils::to_string(&obj.0)
94+
}
95+
}
96+
7897
impl UniffiCustomTypeConverter for PaymentId {
7998
type Builtin = String;
8099

0 commit comments

Comments
 (0)