Skip to content

Commit a06b91e

Browse files
committed
Add serialization logic for LSPS1 PeerState types
We add the serializations for all types that will be persisted as part of the `PeerState`.
1 parent 38b0372 commit a06b91e

File tree

3 files changed

+148
-1
lines changed

3 files changed

+148
-1
lines changed

lightning-liquidity/src/lsps1/msgs.rs

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ use crate::lsps0::ser::{
1919
};
2020

2121
use bitcoin::{Address, FeeRate, OutPoint};
22-
2322
use lightning::offers::offer::Offer;
23+
use lightning::util::ser::{Readable, Writeable};
24+
use lightning::{impl_writeable_tlv_based, impl_writeable_tlv_based_enum};
2425
use lightning_invoice::Bolt11Invoice;
2526

2627
use serde::{Deserialize, Serialize};
@@ -39,6 +40,23 @@ pub(crate) const LSPS1_GET_ORDER_REQUEST_ORDER_NOT_FOUND_ERROR_CODE: i32 = 101;
3940
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Hash)]
4041
pub struct LSPS1OrderId(pub String);
4142

43+
impl Writeable for LSPS1OrderId {
44+
fn write<W: lightning::util::ser::Writer>(
45+
&self, writer: &mut W,
46+
) -> Result<(), lightning::io::Error> {
47+
self.0.write(writer)
48+
}
49+
}
50+
51+
impl Readable for LSPS1OrderId {
52+
fn read<R: bitcoin::io::Read>(
53+
reader: &mut R,
54+
) -> Result<Self, lightning::ln::msgs::DecodeError> {
55+
let inner = Readable::read(reader)?;
56+
Ok(Self(inner))
57+
}
58+
}
59+
4260
/// A request made to an LSP to retrieve the supported options.
4361
///
4462
/// Please refer to the [bLIP-51 / LSPS1
@@ -128,6 +146,16 @@ pub struct LSPS1OrderParams {
128146
pub announce_channel: bool,
129147
}
130148

149+
impl_writeable_tlv_based!(LSPS1OrderParams, {
150+
(0, lsp_balance_sat, required),
151+
(2, client_balance_sat, required),
152+
(4, required_channel_confirmations, required),
153+
(6, funding_confirms_within_blocks, required),
154+
(8, channel_expiry_blocks, required),
155+
(10, token, option),
156+
(12, announce_channel, required),
157+
});
158+
131159
/// A response to a [`LSPS1CreateOrderRequest`].
132160
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
133161
pub struct LSPS1CreateOrderResponse {
@@ -158,6 +186,12 @@ pub enum LSPS1OrderState {
158186
Failed,
159187
}
160188

189+
impl_writeable_tlv_based_enum!(LSPS1OrderState,
190+
(0, Created) => {},
191+
(2, Completed) => {},
192+
(4, Failed) => {}
193+
);
194+
161195
/// Details regarding how to pay for an order.
162196
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
163197
pub struct LSPS1PaymentInfo {
@@ -169,6 +203,12 @@ pub struct LSPS1PaymentInfo {
169203
pub onchain: Option<LSPS1OnchainPaymentInfo>,
170204
}
171205

206+
impl_writeable_tlv_based!(LSPS1PaymentInfo, {
207+
(0, bolt11, option),
208+
(2, bolt12, option),
209+
(4, onchain, option),
210+
});
211+
172212
/// A Lightning payment using BOLT 11.
173213
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
174214
pub struct LSPS1Bolt11PaymentInfo {
@@ -186,6 +226,14 @@ pub struct LSPS1Bolt11PaymentInfo {
186226
pub invoice: Bolt11Invoice,
187227
}
188228

229+
impl_writeable_tlv_based!(LSPS1Bolt11PaymentInfo, {
230+
(0, state, required),
231+
(2, expires_at, required),
232+
(4, fee_total_sat, required),
233+
(6, order_total_sat, required),
234+
(8, invoice, required),
235+
});
236+
189237
/// A Lightning payment using BOLT 12.
190238
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
191239
pub struct LSPS1Bolt12PaymentInfo {
@@ -204,6 +252,14 @@ pub struct LSPS1Bolt12PaymentInfo {
204252
pub offer: Offer,
205253
}
206254

255+
impl_writeable_tlv_based!(LSPS1Bolt12PaymentInfo, {
256+
(0, state, required),
257+
(2, expires_at, required),
258+
(4, fee_total_sat, required),
259+
(6, order_total_sat, required),
260+
(8, offer, required),
261+
});
262+
207263
/// An onchain payment.
208264
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
209265
pub struct LSPS1OnchainPaymentInfo {
@@ -235,6 +291,17 @@ pub struct LSPS1OnchainPaymentInfo {
235291
pub refund_onchain_address: Option<Address>,
236292
}
237293

294+
impl_writeable_tlv_based!(LSPS1OnchainPaymentInfo, {
295+
(0, state, required),
296+
(2, expires_at, required),
297+
(4, fee_total_sat, required),
298+
(6, order_total_sat, required),
299+
(8, address, required),
300+
(10, min_onchain_payment_confirmations, option),
301+
(12, min_fee_for_0conf, required),
302+
(14, refund_onchain_address, option),
303+
});
304+
238305
/// The state of a payment.
239306
///
240307
/// *Note*: Previously, the spec also knew a `CANCELLED` state for BOLT11 payments, which has since
@@ -251,6 +318,12 @@ pub enum LSPS1PaymentState {
251318
Refunded,
252319
}
253320

321+
impl_writeable_tlv_based_enum!(LSPS1PaymentState,
322+
(0, ExpectPayment) => {},
323+
(2, Paid) => {},
324+
(4, Refunded) => {}
325+
);
326+
254327
/// Details regarding a detected on-chain payment.
255328
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
256329
pub struct LSPS1OnchainPayment {
@@ -274,6 +347,12 @@ pub struct LSPS1ChannelInfo {
274347
pub expires_at: LSPSDateTime,
275348
}
276349

350+
impl_writeable_tlv_based!(LSPS1ChannelInfo, {
351+
(0, funded_at, required),
352+
(2, funding_outpoint, required),
353+
(4, expires_at, required),
354+
});
355+
277356
/// A request made to an LSP to retrieve information about an previously made order.
278357
///
279358
/// Please refer to the [bLIP-51 / LSPS1

lightning-liquidity/src/lsps1/peer_state.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ use super::msgs::{
1717
use crate::lsps0::ser::{LSPSDateTime, LSPSRequestId};
1818
use crate::prelude::HashMap;
1919

20+
use lightning::impl_writeable_tlv_based;
21+
use lightning::util::hash_tables::new_hash_map;
22+
2023
use core::fmt;
2124

2225
#[derive(Default)]
@@ -87,6 +90,11 @@ impl PeerState {
8790
}
8891
}
8992

93+
impl_writeable_tlv_based!(PeerState, {
94+
(0, outbound_channels_by_order_id, required),
95+
(_unused, pending_requests, (static_value, new_hash_map())),
96+
});
97+
9098
#[derive(Debug, Copy, Clone)]
9199
pub(super) enum PeerStateError {
92100
UnknownRequestId,
@@ -112,3 +120,11 @@ pub(super) struct ChannelOrder {
112120
pub(super) payment_details: LSPS1PaymentInfo,
113121
pub(super) channel_details: Option<LSPS1ChannelInfo>,
114122
}
123+
124+
impl_writeable_tlv_based!(ChannelOrder, {
125+
(0, order_params, required),
126+
(2, order_state, required),
127+
(4, created_at, required),
128+
(6, payment_details, required),
129+
(8, channel_details, option),
130+
});

lightning/src/util/ser.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ use crate::sync::{Mutex, RwLock};
2222
use core::cmp;
2323
use core::hash::Hash;
2424
use core::ops::Deref;
25+
use core::str::FromStr;
2526

2627
use alloc::collections::BTreeMap;
2728

2829
use bitcoin::absolute::LockTime as AbsoluteLockTime;
30+
use bitcoin::address::Address;
2931
use bitcoin::amount::{Amount, SignedAmount};
3032
use bitcoin::consensus::Encodable;
3133
use bitcoin::constants::ChainHash;
@@ -41,10 +43,13 @@ use bitcoin::secp256k1::ecdsa;
4143
use bitcoin::secp256k1::schnorr;
4244
use bitcoin::secp256k1::{PublicKey, SecretKey};
4345
use bitcoin::transaction::{OutPoint, Transaction, TxOut};
46+
use bitcoin::FeeRate;
4447
use bitcoin::{consensus, Sequence, TxIn, Weight, Witness};
4548

4649
use dnssec_prover::rr::Name;
4750

51+
use lightning_invoice::Bolt11Invoice;
52+
4853
use crate::chain::ClaimId;
4954
#[cfg(taproot)]
5055
use crate::ln::msgs::PartialSignatureWithNonce;
@@ -1477,6 +1482,53 @@ impl Readable for OutPoint {
14771482
}
14781483
}
14791484

1485+
impl Writeable for Address {
1486+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
1487+
self.to_string().write(w)?;
1488+
Ok(())
1489+
}
1490+
}
1491+
1492+
impl Readable for Address {
1493+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
1494+
let addr_string: String = Readable::read(r)?;
1495+
let addr = Address::from_str(&addr_string)
1496+
.map_err(|_| DecodeError::InvalidValue)?
1497+
.assume_checked();
1498+
Ok(addr)
1499+
}
1500+
}
1501+
1502+
impl Writeable for FeeRate {
1503+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
1504+
self.to_sat_per_kwu().write(w)?;
1505+
Ok(())
1506+
}
1507+
}
1508+
1509+
impl Readable for FeeRate {
1510+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
1511+
let sat_per_kwu: u64 = Readable::read(r)?;
1512+
Ok(FeeRate::from_sat_per_kwu(sat_per_kwu))
1513+
}
1514+
}
1515+
1516+
impl Writeable for Bolt11Invoice {
1517+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
1518+
self.to_string().write(w)?;
1519+
Ok(())
1520+
}
1521+
}
1522+
1523+
impl Readable for Bolt11Invoice {
1524+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
1525+
let invoice_string: String = Readable::read(r)?;
1526+
let invoice =
1527+
Bolt11Invoice::from_str(&invoice_string).map_err(|_| DecodeError::InvalidValue)?;
1528+
Ok(invoice)
1529+
}
1530+
}
1531+
14801532
macro_rules! impl_consensus_ser {
14811533
($bitcoin_type: ty) => {
14821534
impl Writeable for $bitcoin_type {

0 commit comments

Comments
 (0)