Skip to content

Commit ede5898

Browse files
committed
Implement serialization for LSPS5 state types
1 parent 53ebf78 commit ede5898

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

lightning-liquidity/src/lsps5/msgs.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use crate::lsps0::ser::LSPSResponseError;
1616

1717
use super::url_utils::LSPSUrl;
1818

19+
use lightning::ln::msgs::DecodeError;
20+
use lightning::util::ser::{Readable, Writeable};
1921
use lightning_types::string::UntrustedString;
2022

2123
use serde::de::{self, Deserializer, MapAccess, Visitor};
@@ -288,6 +290,20 @@ impl From<LSPS5Error> for LSPSResponseError {
288290
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
289291
pub struct LSPS5AppName(UntrustedString);
290292

293+
impl Writeable for LSPS5AppName {
294+
fn write<W: lightning::util::ser::Writer>(
295+
&self, writer: &mut W,
296+
) -> Result<(), lightning::io::Error> {
297+
self.0.write(writer)
298+
}
299+
}
300+
301+
impl Readable for LSPS5AppName {
302+
fn read<R: lightning::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
303+
Ok(Self(Readable::read(reader)?))
304+
}
305+
}
306+
291307
impl LSPS5AppName {
292308
/// Create a new LSPS5 app name.
293309
pub fn new(app_name: String) -> Result<Self, LSPS5Error> {
@@ -430,6 +446,20 @@ impl From<LSPS5WebhookUrl> for String {
430446
}
431447
}
432448

449+
impl Writeable for LSPS5WebhookUrl {
450+
fn write<W: lightning::util::ser::Writer>(
451+
&self, writer: &mut W,
452+
) -> Result<(), lightning::io::Error> {
453+
self.0.write(writer)
454+
}
455+
}
456+
457+
impl Readable for LSPS5WebhookUrl {
458+
fn read<R: lightning::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
459+
Ok(Self(Readable::read(reader)?))
460+
}
461+
}
462+
433463
/// Parameters for `lsps5.set_webhook` request.
434464
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
435465
pub struct SetWebhookRequest {

lightning-liquidity/src/lsps5/service.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::utils::time::TimeProvider;
2323

2424
use bitcoin::secp256k1::PublicKey;
2525

26+
use lightning::impl_writeable_tlv_based;
2627
use lightning::ln::channelmanager::AChannelManager;
2728
use lightning::ln::msgs::{ErrorAction, LightningError};
2829
use lightning::sign::NodeSigner;
@@ -58,6 +59,14 @@ struct Webhook {
5859
last_notification_sent: Option<LSPSDateTime>,
5960
}
6061

62+
impl_writeable_tlv_based!(Webhook, {
63+
(0, _app_name, required),
64+
(2, url, required),
65+
(4, _counterparty_node_id, required),
66+
(6, last_used, required),
67+
(8, last_notification_sent, option),
68+
});
69+
6170
/// Server-side configuration options for LSPS5 Webhook Registration.
6271
#[derive(Clone, Debug)]
6372
pub struct LSPS5ServiceConfig {
@@ -647,3 +656,7 @@ impl PeerState {
647656
self.webhooks.is_empty()
648657
}
649658
}
659+
660+
impl_writeable_tlv_based!(PeerState, {
661+
(0, webhooks, required_vec),
662+
});

lightning-liquidity/src/lsps5/url_utils.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
1212
use super::msgs::LSPS5ProtocolError;
1313

14+
use lightning::ln::msgs::DecodeError;
15+
use lightning::util::ser::{Readable, Writeable};
1416
use lightning_types::string::UntrustedString;
1517

1618
use alloc::string::String;
1719

1820
/// Represents a parsed URL for LSPS5 webhook notifications.
1921
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
20-
pub struct LSPSUrl {
21-
url: UntrustedString,
22-
}
22+
pub struct LSPSUrl(UntrustedString);
2323

2424
impl LSPSUrl {
2525
/// Parses a URL string into a URL instance.
@@ -66,17 +66,17 @@ impl LSPSUrl {
6666
None => {},
6767
};
6868

69-
Ok(LSPSUrl { url: UntrustedString(url_str) })
69+
Ok(LSPSUrl(UntrustedString(url_str)))
7070
}
7171

7272
/// Returns URL length.
7373
pub fn url_length(&self) -> usize {
74-
self.url.0.chars().count()
74+
self.0 .0.chars().count()
7575
}
7676

7777
/// Returns the full URL string.
7878
pub fn url(&self) -> &str {
79-
self.url.0.as_str()
79+
self.0 .0.as_str()
8080
}
8181

8282
fn is_valid_url_char(c: char) -> bool {
@@ -89,6 +89,20 @@ impl LSPSUrl {
8989
}
9090
}
9191

92+
impl Writeable for LSPSUrl {
93+
fn write<W: lightning::util::ser::Writer>(
94+
&self, writer: &mut W,
95+
) -> Result<(), lightning::io::Error> {
96+
self.0.write(writer)
97+
}
98+
}
99+
100+
impl Readable for LSPSUrl {
101+
fn read<R: lightning::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
102+
Ok(Self(Readable::read(reader)?))
103+
}
104+
}
105+
92106
#[cfg(test)]
93107
mod tests {
94108
use super::*;
@@ -104,7 +118,7 @@ mod tests {
104118

105119
assert!(result.is_ok());
106120
let url = result.unwrap();
107-
assert_eq!(url.url.0.chars().count(), url_chars);
121+
assert_eq!(url.0 .0.chars().count(), url_chars);
108122
}
109123

110124
#[test]

0 commit comments

Comments
 (0)