Skip to content

Commit 943e9d1

Browse files
committed
Implement serialization for LSPS5 state types
1 parent cbd65d1 commit 943e9d1

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};
@@ -275,6 +277,20 @@ impl From<LSPS5Error> for LSPSResponseError {
275277
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
276278
pub struct LSPS5AppName(UntrustedString);
277279

280+
impl Writeable for LSPS5AppName {
281+
fn write<W: lightning::util::ser::Writer>(
282+
&self, writer: &mut W,
283+
) -> Result<(), lightning::io::Error> {
284+
self.0.write(writer)
285+
}
286+
}
287+
288+
impl Readable for LSPS5AppName {
289+
fn read<R: lightning::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
290+
Ok(Self(Readable::read(reader)?))
291+
}
292+
}
293+
278294
impl LSPS5AppName {
279295
/// Create a new LSPS5 app name.
280296
pub fn new(app_name: String) -> Result<Self, LSPS5Error> {
@@ -417,6 +433,20 @@ impl From<LSPS5WebhookUrl> for String {
417433
}
418434
}
419435

436+
impl Writeable for LSPS5WebhookUrl {
437+
fn write<W: lightning::util::ser::Writer>(
438+
&self, writer: &mut W,
439+
) -> Result<(), lightning::io::Error> {
440+
self.0.write(writer)
441+
}
442+
}
443+
444+
impl Readable for LSPS5WebhookUrl {
445+
fn read<R: lightning::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
446+
Ok(Self(Readable::read(reader)?))
447+
}
448+
}
449+
420450
/// Parameters for `lsps5.set_webhook` request.
421451
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
422452
pub struct SetWebhookRequest {

lightning-liquidity/src/lsps5/service.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use lightning::ln::channelmanager::AChannelManager;
2727
use lightning::ln::msgs::{ErrorAction, LightningError};
2828
use lightning::sign::NodeSigner;
2929
use lightning::util::logger::Level;
30+
use lightning::impl_writeable_tlv_based;
3031

3132
use core::ops::Deref;
3233
use core::time::Duration;
@@ -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, required),
68+
});
69+
6170
/// Server-side configuration options for LSPS5 Webhook Registration.
6271
#[derive(Clone, Debug)]
6372
pub struct LSPS5ServiceConfig {
@@ -617,3 +626,7 @@ impl PeerState {
617626
self.webhooks.is_empty()
618627
}
619628
}
629+
630+
impl_writeable_tlv_based!(PeerState, {
631+
(0, webhooks, required),
632+
});

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.
@@ -68,17 +68,17 @@ impl LSPSUrl {
6868
None => {},
6969
};
7070

71-
Ok(LSPSUrl { url: UntrustedString(url_str) })
71+
Ok(LSPSUrl(UntrustedString(url_str)))
7272
}
7373

7474
/// Returns URL length.
7575
pub fn url_length(&self) -> usize {
76-
self.url.0.chars().count()
76+
self.0 .0.chars().count()
7777
}
7878

7979
/// Returns the full URL string.
8080
pub fn url(&self) -> &str {
81-
self.url.0.as_str()
81+
self.0 .0.as_str()
8282
}
8383

8484
fn is_valid_url_char(c: char) -> bool {
@@ -91,6 +91,20 @@ impl LSPSUrl {
9191
}
9292
}
9393

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

107121
assert!(result.is_ok());
108122
let url = result.unwrap();
109-
assert_eq!(url.url.0.chars().count(), url_chars);
123+
assert_eq!(url.0.0.chars().count(), url_chars);
110124
}
111125

112126
#[test]

0 commit comments

Comments
 (0)