From 6c9f0fed06ef2d95670aa221538142e1ff3977de Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Wed, 21 Feb 2024 13:57:24 +0100 Subject: [PATCH] cln: init the rgb manager Signed-off-by: Vincenzo Palazzo --- rgb-cln/src/plugin.rs | 75 +++++++++++++++++++++++++++++++++-- rgb-common/src/rgb_manager.rs | 6 +++ 2 files changed, 77 insertions(+), 4 deletions(-) diff --git a/rgb-cln/src/plugin.rs b/rgb-cln/src/plugin.rs index 47bda1c..532528c 100644 --- a/rgb-cln/src/plugin.rs +++ b/rgb-cln/src/plugin.rs @@ -1,19 +1,58 @@ //! Plugin implementation //! //! Author: Vincenzo Palazzo +use std::{fmt::Debug, sync::Arc}; + +use serde::de::DeserializeOwned; +use serde::{Deserialize, Serialize}; use serde_json as json; +use clightningrpc_common::client::Client; use clightningrpc_plugin::{commands::RPCCommand, plugin::Plugin}; use clightningrpc_plugin_macros::plugin; -use rgb_common::anyhow; +use rgb_common::{anyhow, RGBManager}; #[derive(Clone, Debug)] -pub(crate) struct State; +pub(crate) struct State { + /// The RGB Manager where we ask to do everything + /// related to lightning. + rgb_manager: Option>, + + /// CLN RPC + cln_rpc: Option>, +} impl State { pub fn new() -> Self { - State + State { + rgb_manager: None, + cln_rpc: None, + } + } + + pub(crate) fn rpc(&self) -> Arc { + self.cln_rpc.clone().unwrap() + } + + pub(crate) fn manager(&self) -> Arc { + self.rgb_manager.clone().unwrap() + } + + pub fn call( + &self, + method: &str, + payload: T, + ) -> anyhow::Result { + if let Some(rpc) = &self.cln_rpc { + let response = rpc.send_request(method, payload)?; + log::debug!("cln answer with {:?}", response); + if let Some(err) = response.error { + anyhow::bail!("cln error: {}", err.message); + } + return Ok(response.result.unwrap()); + } + anyhow::bail!("rpc connection to core lightning not available") } } @@ -25,12 +64,40 @@ pub fn build_plugin() -> anyhow::Result> { methods: [], hooks: [], }; - plugin.on_init(|_| json::json!({})); + plugin.on_init(on_init); plugin = plugin.register_hook("onfunding_channel_tx", None, None, OnFundingChannelTx); Ok(plugin) } +// FIXME: move to another part of the code. +#[derive(Debug, Deserialize)] +pub struct GetInfo { + id: String, +} + +fn on_init(plugin: &mut Plugin) -> json::Value { + let config = plugin.configuration.clone().unwrap(); + let rpc_file = format!("{}/{}", config.lightning_dir, config.rpc_file); + + let rpc = Client::new(rpc_file); + plugin.state.cln_rpc = Some(Arc::new(rpc)); + let getinfo: anyhow::Result = plugin.state.call("getinfo", json::json!({})); + if let Err(err) = getinfo { + return json::json!({ "disable": format!("{err}") }); + } + // SAFETY: Safe to unwrap because we unwrap before. + let getinfo = getinfo.unwrap(); + + // FIXME: I can get the public key from the configuration? + let manager = RGBManager::init(&config.lightning_dir, &getinfo.id, &config.network); + if let Err(err) = manager { + return json::json!({ "disable": format!("{err}") }); + } + + json::json!({}) +} + #[derive(Clone, Debug)] struct OnFundingChannelTx; diff --git a/rgb-common/src/rgb_manager.rs b/rgb-common/src/rgb_manager.rs index 37039c6..c91cb0b 100644 --- a/rgb-common/src/rgb_manager.rs +++ b/rgb-common/src/rgb_manager.rs @@ -12,6 +12,12 @@ pub struct RGBManager { wallet: Arc>, } +impl std::fmt::Debug for RGBManager { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + writeln!(f, "RGB manager struct {{ .. }}") + } +} + impl RGBManager { pub fn init(root_dir: &str, pubkey: &str, network: &str) -> anyhow::Result { let client = proxy::get_blocking_client();