1- use anyhow:: anyhow;
1+ use anyhow:: { anyhow, bail } ;
22use async_trait:: async_trait;
33use cln_lsps:: jsonrpc:: server:: JsonRpcResponseWriter ;
44use cln_lsps:: jsonrpc:: TransportError ;
@@ -11,21 +11,14 @@ use cln_lsps::lsps2::handler::{ClnApiRpc, HtlcAcceptedHookHandler};
1111use cln_lsps:: lsps2:: model:: { Lsps2BuyRequest , Lsps2GetInfoRequest } ;
1212use cln_lsps:: util:: wrap_payload_with_peer_id;
1313use cln_lsps:: { lsps0, lsps2, util, LSP_FEATURE_BIT } ;
14- use cln_plugin:: options:: ConfigOption ;
15- use cln_plugin:: { options, Plugin } ;
14+ use cln_plugin:: Plugin ;
1615use cln_rpc:: notifications:: CustomMsgNotification ;
1716use cln_rpc:: primitives:: PublicKey ;
1817use log:: debug;
1918use std:: path:: { Path , PathBuf } ;
2019use std:: str:: FromStr ;
2120use std:: sync:: Arc ;
2221
23- /// An option to enable this service.
24- const OPTION_ENABLED : options:: FlagConfigOption = ConfigOption :: new_flag (
25- "dev-lsps-service-enabled" ,
26- "Enables an LSPS service on the node." ,
27- ) ;
28-
2922#[ derive( Clone ) ]
3023struct State {
3124 lsps_service : JsonRpcServer ,
@@ -35,7 +28,6 @@ struct State {
3528#[ tokio:: main]
3629async fn main ( ) -> Result < ( ) , anyhow:: Error > {
3730 if let Some ( plugin) = cln_plugin:: Builder :: new ( tokio:: io:: stdin ( ) , tokio:: io:: stdout ( ) )
38- . option ( OPTION_ENABLED )
3931 . option ( lsps2:: OPTION_ENABLED )
4032 . option ( lsps2:: OPTION_PROMISE_SECRET )
4133 . featurebits (
@@ -54,23 +46,9 @@ async fn main() -> Result<(), anyhow::Error> {
5446 let rpc_path =
5547 Path :: new ( & plugin. configuration ( ) . lightning_dir ) . join ( & plugin. configuration ( ) . rpc_file ) ;
5648
57- if !plugin. option ( & OPTION_ENABLED ) ? {
58- return plugin
59- . disable ( & format ! ( "`{}` not enabled" , OPTION_ENABLED . name) )
60- . await ;
61- }
62-
63- let mut lsps_builder = JsonRpcServer :: builder ( ) . with_handler (
64- Lsps0listProtocolsRequest :: METHOD . to_string ( ) ,
65- Arc :: new ( Lsps0ListProtocolsHandler {
66- lsps2_enabled : plugin. option ( & lsps2:: OPTION_ENABLED ) ?,
67- } ) ,
68- ) ;
69-
70- let lsps2_enabled = if plugin. option ( & lsps2:: OPTION_ENABLED ) ? {
71- log:: debug!( "lsps2 enabled" ) ;
72- let secret_hex = plugin. option ( & lsps2:: OPTION_PROMISE_SECRET ) ?;
73- if let Some ( secret_hex) = secret_hex {
49+ if plugin. option ( & lsps2:: OPTION_ENABLED ) ? {
50+ log:: debug!( "lsps2-service enabled" ) ;
51+ if let Some ( secret_hex) = plugin. option ( & lsps2:: OPTION_PROMISE_SECRET ) ? {
7452 let secret_hex = secret_hex. trim ( ) . to_lowercase ( ) ;
7553
7654 let decoded_bytes = match hex:: decode ( & secret_hex) {
@@ -97,6 +75,13 @@ async fn main() -> Result<(), anyhow::Error> {
9775 }
9876 } ;
9977
78+ let mut lsps_builder = JsonRpcServer :: builder ( ) . with_handler (
79+ Lsps0listProtocolsRequest :: METHOD . to_string ( ) ,
80+ Arc :: new ( Lsps0ListProtocolsHandler {
81+ lsps2_enabled : plugin. option ( & lsps2:: OPTION_ENABLED ) ?,
82+ } ) ,
83+ ) ;
84+
10085 let cln_api_rpc = lsps2:: handler:: ClnApiRpc :: new ( rpc_path) ;
10186 let getinfo_handler =
10287 lsps2:: handler:: Lsps2GetInfoHandler :: new ( cln_api_rpc. clone ( ) , secret) ;
@@ -107,20 +92,23 @@ async fn main() -> Result<(), anyhow::Error> {
10792 Arc :: new ( getinfo_handler) ,
10893 )
10994 . with_handler ( Lsps2BuyRequest :: METHOD . to_string ( ) , Arc :: new ( buy_handler) ) ;
110- }
111- true
112- } else {
113- false
114- } ;
11595
116- let lsps_service = lsps_builder. build ( ) ;
96+ let lsps_service = lsps_builder. build ( ) ;
11797
118- let state = State {
119- lsps_service,
120- lsps2_enabled,
121- } ;
122- let plugin = plugin. start ( state) . await ?;
123- plugin. join ( ) . await
98+ let state = State {
99+ lsps_service,
100+ lsps2_enabled : true ,
101+ } ;
102+ let plugin = plugin. start ( state) . await ?;
103+ plugin. join ( ) . await
104+ } else {
105+ bail ! ( "lsps2 enabled but no promise-secret set." ) ;
106+ }
107+ } else {
108+ return plugin
109+ . disable ( & format ! ( "`{}` not enabled" , & lsps2:: OPTION_ENABLED . name) )
110+ . await ;
111+ }
124112 } else {
125113 Ok ( ( ) )
126114 }
0 commit comments