Skip to content

Commit 5055bf0

Browse files
committed
f Add Bolt12Offer/Bolt12Refund variants, add offer_id
1 parent ee7b9bc commit 5055bf0

File tree

4 files changed

+57
-10
lines changed

4 files changed

+57
-10
lines changed

bindings/ldk_node.udl

Lines changed: 6 additions & 1 deletion
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,7 +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);
228-
Bolt12(PaymentHash? hash, PaymentPreimage? preimage, PaymentSecret? secret);
229+
Bolt12Offer(PaymentHash? hash, PaymentPreimage? preimage, PaymentSecret? secret, OfferId offer_id);
230+
Bolt12Refund(PaymentHash? hash, PaymentPreimage? preimage, PaymentSecret? secret);
229231
Spontaneous(PaymentHash hash, PaymentPreimage? preimage);
230232
};
231233

@@ -372,6 +374,9 @@ typedef string Address;
372374
[Custom]
373375
typedef string Bolt11Invoice;
374376

377+
[Custom]
378+
typedef string OfferId;
379+
375380
[Custom]
376381
typedef string PaymentId;
377382

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: 29 additions & 9 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,11 +175,23 @@ pub enum PaymentKind {
176175
/// [`LdkChannelConfig::accept_underpaying_htlcs`]: lightning::util::config::ChannelConfig::accept_underpaying_htlcs
177176
lsp_fee_limits: LSPFeeLimits,
178177
},
179-
/// A [BOLT 12] payment.
178+
/// A [BOLT 12] 'offer' payment, i.e., a payment in response to an offer.
180179
///
181180
/// [BOLT 12]: https://github.com/lightning/bolts/blob/master/12-offer-encoding.md
182-
// TODO: Bolt12 { offer: Option<Offer>, refund: Option<Refund> },
183-
Bolt12 {
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 {
184195
/// The payment hash, i.e., the hash of the `preimage`.
185196
hash: Option<PaymentHash>,
186197
/// The pre-image used by the payment.
@@ -210,14 +221,20 @@ impl_writeable_tlv_based_enum!(PaymentKind,
210221
(4, secret, option),
211222
(6, lsp_fee_limits, required),
212223
},
213-
(6, Bolt12) => {
224+
(6, Bolt12Offer) => {
214225
(0, hash, option),
215226
(2, preimage, option),
216227
(4, secret, option),
228+
(6, offer_id, required),
217229
},
218230
(8, Spontaneous) => {
219231
(0, hash, required),
220232
(2, preimage, option),
233+
},
234+
(10, Bolt12Refund) => {
235+
(0, hash, option),
236+
(2, preimage, option),
237+
(4, secret, option),
221238
};
222239
);
223240

@@ -327,15 +344,17 @@ where
327344
if let Some(payment) = locked_payments.get_mut(&update.id) {
328345
if let Some(hash_opt) = update.hash {
329346
match payment.kind {
330-
PaymentKind::Bolt12 { ref mut hash, .. } => *hash = hash_opt,
347+
PaymentKind::Bolt12Offer { ref mut hash, .. } => *hash = hash_opt,
348+
PaymentKind::Bolt12Refund { ref mut hash, .. } => *hash = hash_opt,
331349
_ => {},
332350
}
333351
}
334352
if let Some(preimage_opt) = update.preimage {
335353
match payment.kind {
336354
PaymentKind::Bolt11 { ref mut preimage, .. } => *preimage = preimage_opt,
337355
PaymentKind::Bolt11Jit { ref mut preimage, .. } => *preimage = preimage_opt,
338-
PaymentKind::Bolt12 { ref mut preimage, .. } => *preimage = preimage_opt,
356+
PaymentKind::Bolt12Offer { ref mut preimage, .. } => *preimage = preimage_opt,
357+
PaymentKind::Bolt12Refund { ref mut preimage, .. } => *preimage = preimage_opt,
339358
PaymentKind::Spontaneous { ref mut preimage, .. } => *preimage = preimage_opt,
340359
_ => {},
341360
}
@@ -345,7 +364,8 @@ where
345364
match payment.kind {
346365
PaymentKind::Bolt11 { ref mut secret, .. } => *secret = secret_opt,
347366
PaymentKind::Bolt11Jit { ref mut secret, .. } => *secret = secret_opt,
348-
PaymentKind::Bolt12 { ref mut secret, .. } => *secret = secret_opt,
367+
PaymentKind::Bolt12Offer { ref mut secret, .. } => *secret = secret_opt,
368+
PaymentKind::Bolt12Refund { ref mut secret, .. } => *secret = secret_opt,
349369
_ => {},
350370
}
351371
}

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)