Skip to content

Commit dbf053f

Browse files
committed
lsps: Add service implementation for LSPS0
Implements the LSPS0 service plugin for core lightning Changelog-Added: lsps-plugin: lsps0 service support Signed-off-by: Peter Neuroth <[email protected]>
1 parent 51016a5 commit dbf053f

File tree

4 files changed

+130
-2
lines changed

4 files changed

+130
-2
lines changed

plugins/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ recklessrpc
2121
exposesecret
2222
cln-xpay
2323
cln-lsps-client
24+
cln-lsps-service

plugins/Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,10 @@ plugins/clnrest: target/${RUST_PROFILE}/clnrest
149149
@cp $< $@
150150
plugins/cln-lsps-client: target/${RUST_PROFILE}/cln-lsps-client
151151
@cp $< $@
152+
plugins/cln-lsps-service: target/${RUST_PROFILE}/cln-lsps-service
153+
@cp $< $@
152154

153-
PLUGINS += plugins/cln-grpc plugins/clnrest plugins/cln-lsps-client
155+
PLUGINS += plugins/cln-grpc plugins/clnrest plugins/cln-lsps-client plugins/cln-lsps-service
154156
endif
155157

156158
PLUGIN_COMMON_OBJS := \
@@ -310,10 +312,12 @@ target/${RUST_PROFILE}/clnrest: ${CLN_REST_PLUGIN_SRC}
310312
cargo build ${CARGO_OPTS} --bin clnrest
311313
target/${RUST_PROFILE}/cln-lsps-client: ${CLN_LSPS_PLUGIN_SRC}
312314
cargo build ${CARGO_OPTS} --bin cln-lsps-client
315+
target/${RUST_PROFILE}/cln-lsps-service: ${CLN_LSPS_PLUGIN_SRC}
316+
cargo build ${CARGO_OPTS} --bin cln-lsps-service
313317

314318
ifneq ($(RUST),0)
315319
include plugins/rest-plugin/Makefile
316-
DEFAULT_TARGETS += $(CLN_PLUGIN_EXAMPLES) plugins/cln-grpc plugins/clnrest plugins/cln-lsps-client
320+
DEFAULT_TARGETS += $(CLN_PLUGIN_EXAMPLES) plugins/cln-grpc plugins/clnrest plugins/cln-lsps-client plugins/cln-lsps-service
317321
endif
318322

319323
clean: plugins-clean

plugins/lsps-plugin/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ edition = "2021"
77
name = "cln-lsps-client"
88
path = "src/client.rs"
99

10+
[[bin]]
11+
name = "cln-lsps-service"
12+
path = "src/service.rs"
13+
1014
[dependencies]
1115
anyhow = "1.0"
1216
async-trait = "0.1"

plugins/lsps-plugin/src/service.rs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
use anyhow::anyhow;
2+
use async_trait::async_trait;
3+
use cln_lsps::jsonrpc::server::{JsonRpcResponseWriter, RequestHandler};
4+
use cln_lsps::jsonrpc::{server::JsonRpcServer, JsonRpcRequest};
5+
use cln_lsps::jsonrpc::{JsonRpcResponse, RequestObject, RpcError, TransportError};
6+
use cln_lsps::lsps0;
7+
use cln_lsps::lsps0::model::{Lsps0listProtocolsRequest, Lsps0listProtocolsResponse};
8+
use cln_lsps::lsps0::transport::{self, CustomMsg};
9+
use cln_plugin::options::ConfigOption;
10+
use cln_plugin::{options, Plugin};
11+
use cln_rpc::notifications::CustomMsgNotification;
12+
use cln_rpc::primitives::PublicKey;
13+
use log::debug;
14+
use std::path::{Path, PathBuf};
15+
use std::str::FromStr;
16+
use std::sync::Arc;
17+
18+
const OPTION_ENABLED: options::DefaultBooleanConfigOption = ConfigOption::new_bool_with_default(
19+
"lsps-service",
20+
false,
21+
"Enables an LSPS service on the node.",
22+
);
23+
24+
#[derive(Clone)]
25+
struct State {
26+
lsps_service: JsonRpcServer,
27+
}
28+
29+
#[tokio::main]
30+
async fn main() -> Result<(), anyhow::Error> {
31+
let lsps_service = JsonRpcServer::builder()
32+
.with_handler(
33+
Lsps0listProtocolsRequest::METHOD.to_string(),
34+
Arc::new(Lsps0ListProtocolsHandler),
35+
)
36+
.build();
37+
let state = State { lsps_service };
38+
39+
if let Some(plugin) = cln_plugin::Builder::new(tokio::io::stdin(), tokio::io::stdout())
40+
.option(OPTION_ENABLED)
41+
.hook("custommsg", on_custommsg)
42+
.configure()
43+
.await?
44+
{
45+
if !plugin.option(&OPTION_ENABLED)? {
46+
return plugin.disable("`lsps-service` not enabled").await;
47+
}
48+
49+
let plugin = plugin.start(state).await?;
50+
plugin.join().await
51+
} else {
52+
Ok(())
53+
}
54+
}
55+
56+
async fn on_custommsg(
57+
p: Plugin<State>,
58+
v: serde_json::Value,
59+
) -> Result<serde_json::Value, anyhow::Error> {
60+
// All of this could be done async if needed.
61+
let continue_response = Ok(serde_json::json!({
62+
"result": "continue"
63+
}));
64+
let msg: CustomMsgNotification =
65+
serde_json::from_value(v).map_err(|e| anyhow!("invalid custommsg: {e}"))?;
66+
67+
let req = CustomMsg::from_str(&msg.payload).map_err(|e| anyhow!("invalid payload {e}"))?;
68+
if req.message_type != lsps0::transport::LSPS0_MESSAGE_TYPE {
69+
// We don't care if this is not for us!
70+
return continue_response;
71+
}
72+
73+
let dir = p.configuration().lightning_dir;
74+
let rpc_path = Path::new(&dir).join(&p.configuration().rpc_file);
75+
let mut writer = LspsResponseWriter {
76+
peer_id: msg.peer_id,
77+
rpc_path: rpc_path.try_into()?,
78+
};
79+
80+
let service = p.state().lsps_service.clone();
81+
match service.handle_message(&req.payload, &mut writer).await {
82+
Ok(_) => continue_response,
83+
Err(e) => {
84+
debug!("failed to handle lsps message: {}", e);
85+
continue_response
86+
}
87+
}
88+
}
89+
90+
pub struct LspsResponseWriter {
91+
peer_id: PublicKey,
92+
rpc_path: PathBuf,
93+
}
94+
95+
#[async_trait]
96+
impl JsonRpcResponseWriter for LspsResponseWriter {
97+
async fn write(&mut self, payload: &[u8]) -> cln_lsps::jsonrpc::Result<()> {
98+
let mut client = cln_rpc::ClnRpc::new(&self.rpc_path).await.map_err(|e| {
99+
cln_lsps::jsonrpc::Error::Transport(TransportError::Other(e.to_string()))
100+
})?;
101+
transport::send_custommsg(&mut client, payload.to_vec(), self.peer_id).await
102+
}
103+
}
104+
105+
pub struct Lsps0ListProtocolsHandler;
106+
107+
#[async_trait]
108+
impl RequestHandler for Lsps0ListProtocolsHandler {
109+
async fn handle(&self, payload: &[u8]) -> core::result::Result<Vec<u8>, RpcError> {
110+
let req: RequestObject<Lsps0listProtocolsRequest> =
111+
serde_json::from_slice(payload).unwrap();
112+
if let Some(id) = req.id {
113+
let res = Lsps0listProtocolsResponse { protocols: vec![] }.into_response(id);
114+
let res_vec = serde_json::to_vec(&res).unwrap();
115+
return Ok(res_vec);
116+
}
117+
Ok(vec![])
118+
}
119+
}

0 commit comments

Comments
 (0)