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