cln: init the rgb manager

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
This commit is contained in:
Vincenzo Palazzo 2024-02-21 13:57:24 +01:00
parent 9548429b62
commit 6c9f0fed06
Signed by: vincenzopalazzo
GPG Key ID: 8B6DC2B870B80D5F
2 changed files with 77 additions and 4 deletions

View File

@ -1,19 +1,58 @@
//! Plugin implementation
//!
//! Author: Vincenzo Palazzo <vincenzopalazzo@member.fsf.org>
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<Arc<RGBManager>>,
/// CLN RPC
cln_rpc: Option<Arc<Client>>,
}
impl State {
pub fn new() -> Self {
State
State {
rgb_manager: None,
cln_rpc: None,
}
}
pub(crate) fn rpc(&self) -> Arc<Client> {
self.cln_rpc.clone().unwrap()
}
pub(crate) fn manager(&self) -> Arc<RGBManager> {
self.rgb_manager.clone().unwrap()
}
pub fn call<T: Serialize, U: DeserializeOwned + Debug>(
&self,
method: &str,
payload: T,
) -> anyhow::Result<U> {
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<Plugin<State>> {
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<State>) -> 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<GetInfo> = 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;

View File

@ -12,6 +12,12 @@ pub struct RGBManager {
wallet: Arc<Mutex<Wallet>>,
}
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<Self> {
let client = proxy::get_blocking_client();