Skip to content

Commit 85d47a5

Browse files
committed
plugins: lsps: remove lsps2 module
Separate concerns of the lsps2 modules into their respective layers: core for tlv, as it is lightning related, not specifically core-lightning. cln_adapters for core-lightning related types. Signed-off-by: Peter Neuroth <pet.v.ne@gmail.com>
1 parent 1fa86a7 commit 85d47a5

File tree

10 files changed

+866
-870
lines changed

10 files changed

+866
-870
lines changed

plugins/lsps-plugin/src/client.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ use anyhow::{anyhow, bail, Context};
22
use bitcoin::hashes::{hex::FromHex, sha256, Hash};
33
use chrono::{Duration, Utc};
44
use cln_lsps::{
5-
cln_adapters::{hooks, sender::ClnSender, state::ClientState},
5+
cln_adapters::{
6+
hooks,
7+
sender::ClnSender,
8+
state::ClientState,
9+
types::{
10+
HtlcAcceptedRequest, HtlcAcceptedResponse, InvoicePaymentRequest, OpenChannelRequest,
11+
},
12+
},
613
core::{
714
client::LspsClient,
15+
tlv::{encode_tu64, TLV_FORWARD_AMT, TLV_PAYMENT_SECRET},
816
transport::{MultiplexedTransport, PendingRequests},
917
},
10-
lsps2::cln::{
11-
tlv::encode_tu64, HtlcAcceptedRequest, HtlcAcceptedResponse, InvoicePaymentRequest,
12-
OpenChannelRequest, TLV_FORWARD_AMT, TLV_PAYMENT_SECRET,
13-
},
1418
proto::{
1519
lsps0::{Msat, LSP_FEATURE_BIT},
1620
lsps2::{compute_opening_fee, Lsps2BuyResponse, Lsps2GetInfoResponse, OpeningFeeParams},

plugins/lsps-plugin/src/cln_adapters/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ pub mod hooks;
22
pub mod rpc;
33
pub mod sender;
44
pub mod state;
5+
pub mod types;
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
//! Backfill structs for missing or incomplete Core Lightning types.
2+
//!
3+
//! This module provides struct implementations that are not available or
4+
//! fully accessible in the core-lightning crate, enabling better compatibility
5+
//! and interoperability with Core Lightning's RPC interface.
6+
use cln_rpc::primitives::{Amount, ShortChannelId};
7+
use hex::FromHex;
8+
use serde::{Deserialize, Deserializer, Serialize, Serializer};
9+
10+
use crate::core::tlv::TlvStream;
11+
12+
#[derive(Debug, Deserialize)]
13+
#[allow(unused)]
14+
pub struct Onion {
15+
pub forward_msat: Option<Amount>,
16+
#[serde(deserialize_with = "from_hex")]
17+
pub next_onion: Vec<u8>,
18+
pub outgoing_cltv_value: Option<u32>,
19+
pub payload: TlvStream,
20+
// pub payload: TlvStream,
21+
#[serde(deserialize_with = "from_hex")]
22+
pub shared_secret: Vec<u8>,
23+
pub short_channel_id: Option<ShortChannelId>,
24+
pub total_msat: Option<Amount>,
25+
#[serde(rename = "type")]
26+
pub type_: Option<String>,
27+
}
28+
29+
#[derive(Debug, Deserialize)]
30+
#[allow(unused)]
31+
pub struct Htlc {
32+
pub amount_msat: Amount,
33+
pub cltv_expiry: u32,
34+
pub cltv_expiry_relative: u16,
35+
pub id: u64,
36+
#[serde(deserialize_with = "from_hex")]
37+
pub payment_hash: Vec<u8>,
38+
pub short_channel_id: ShortChannelId,
39+
pub extra_tlvs: Option<TlvStream>,
40+
}
41+
42+
#[derive(Debug, Serialize, PartialEq, Eq)]
43+
#[serde(rename_all = "snake_case")]
44+
pub enum HtlcAcceptedResult {
45+
Continue,
46+
Fail,
47+
Resolve,
48+
}
49+
50+
impl std::fmt::Display for HtlcAcceptedResult {
51+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52+
let s = match self {
53+
HtlcAcceptedResult::Continue => "continue",
54+
HtlcAcceptedResult::Fail => "fail",
55+
HtlcAcceptedResult::Resolve => "resolve",
56+
};
57+
write!(f, "{s}")
58+
}
59+
}
60+
61+
#[derive(Debug, Deserialize)]
62+
pub struct HtlcAcceptedRequest {
63+
pub htlc: Htlc,
64+
pub onion: Onion,
65+
pub forward_to: Option<String>,
66+
}
67+
68+
#[derive(Debug, Serialize)]
69+
pub struct HtlcAcceptedResponse {
70+
pub result: HtlcAcceptedResult,
71+
#[serde(skip_serializing_if = "Option::is_none")]
72+
pub payment_key: Option<String>,
73+
#[serde(skip_serializing_if = "Option::is_none", serialize_with = "to_hex")]
74+
pub payload: Option<Vec<u8>>,
75+
#[serde(skip_serializing_if = "Option::is_none", serialize_with = "to_hex")]
76+
pub forward_to: Option<Vec<u8>>,
77+
#[serde(skip_serializing_if = "Option::is_none", serialize_with = "to_hex")]
78+
pub extra_tlvs: Option<Vec<u8>>,
79+
#[serde(skip_serializing_if = "Option::is_none")]
80+
pub failure_message: Option<String>,
81+
#[serde(skip_serializing_if = "Option::is_none", serialize_with = "to_hex")]
82+
pub failure_onion: Option<Vec<u8>>,
83+
}
84+
85+
impl HtlcAcceptedResponse {
86+
pub fn continue_(
87+
payload: Option<Vec<u8>>,
88+
forward_to: Option<Vec<u8>>,
89+
extra_tlvs: Option<Vec<u8>>,
90+
) -> Self {
91+
Self {
92+
result: HtlcAcceptedResult::Continue,
93+
payment_key: None,
94+
payload,
95+
forward_to,
96+
extra_tlvs,
97+
failure_message: None,
98+
failure_onion: None,
99+
}
100+
}
101+
102+
pub fn fail(failure_message: Option<String>, failure_onion: Option<Vec<u8>>) -> Self {
103+
Self {
104+
result: HtlcAcceptedResult::Fail,
105+
payment_key: None,
106+
payload: None,
107+
forward_to: None,
108+
extra_tlvs: None,
109+
failure_message,
110+
failure_onion,
111+
}
112+
}
113+
}
114+
115+
#[derive(Debug, Deserialize)]
116+
pub struct InvoicePaymentRequest {
117+
pub payment: InvoicePaymentRequestPayment,
118+
}
119+
120+
#[derive(Debug, Deserialize)]
121+
pub struct InvoicePaymentRequestPayment {
122+
pub label: String,
123+
pub preimage: String,
124+
pub msat: u64,
125+
}
126+
127+
#[derive(Debug, Deserialize)]
128+
pub struct OpenChannelRequest {
129+
pub openchannel: OpenChannelRequestOpenChannel,
130+
}
131+
132+
#[derive(Debug, Deserialize)]
133+
pub struct OpenChannelRequestOpenChannel {
134+
pub id: String,
135+
pub funding_msat: u64,
136+
pub push_msat: u64,
137+
pub dust_limit_msat: u64,
138+
pub max_htlc_value_in_flight_msat: u64,
139+
pub channel_reserve_msat: u64,
140+
pub htlc_minimum_msat: u64,
141+
pub feerate_per_kw: u32,
142+
pub to_self_delay: u32,
143+
pub max_accepted_htlcs: u32,
144+
pub channel_flags: u64,
145+
}
146+
147+
/// Deserializes a lowercase hex string to a `Vec<u8>`.
148+
pub fn from_hex<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
149+
where
150+
D: Deserializer<'de>,
151+
{
152+
use serde::de::Error;
153+
String::deserialize(deserializer)
154+
.and_then(|string| Vec::from_hex(string).map_err(|err| Error::custom(err.to_string())))
155+
}
156+
157+
pub fn to_hex<S>(bytes: &Option<Vec<u8>>, serializer: S) -> Result<S::Ok, S::Error>
158+
where
159+
S: Serializer,
160+
{
161+
match bytes {
162+
Some(data) => serializer.serialize_str(&hex::encode(data)),
163+
None => serializer.serialize_none(),
164+
}
165+
}

plugins/lsps-plugin/src/core/lsps2/htlc.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use crate::{
2-
core::lsps2::provider::{DatastoreProvider, LightningProvider, Lsps2OfferProvider},
3-
lsps2::cln::{HtlcAcceptedRequest, HtlcAcceptedResponse, TLV_FORWARD_AMT},
2+
cln_adapters::types::{HtlcAcceptedRequest, HtlcAcceptedResponse},
3+
core::{
4+
lsps2::provider::{DatastoreProvider, LightningProvider, Lsps2OfferProvider},
5+
tlv::TLV_FORWARD_AMT,
6+
},
47
proto::{
58
lsps0::Msat,
69
lsps2::{
@@ -195,10 +198,8 @@ impl<A: DatastoreProvider + Lsps2OfferProvider + LightningProvider> HtlcAccepted
195198
#[cfg(test)]
196199
mod tests {
197200
use super::*;
198-
use crate::lsps2::cln::tlv::TlvStream;
199-
use crate::lsps2::cln::Htlc;
200-
use crate::lsps2::cln::HtlcAcceptedResult;
201-
use crate::lsps2::cln::Onion;
201+
use crate::cln_adapters::types::{Htlc, HtlcAcceptedResult, Onion};
202+
use crate::core::tlv::TlvStream;
202203
use crate::proto::lsps0::{Msat, Ppm, ShortChannelId};
203204
use crate::proto::lsps2::{
204205
DatastoreEntry, Lsps2PolicyGetChannelCapacityResponse, Lsps2PolicyGetInfoRequest,

plugins/lsps-plugin/src/core/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ pub mod client;
22
pub mod lsps2;
33
pub mod router;
44
pub mod server;
5+
pub mod tlv;
56
pub mod transport;

0 commit comments

Comments
 (0)