2 unstable releases

0.5.0 Nov 9, 2024
0.4.0 Nov 7, 2023
0.2.0 Nov 9, 2024

#16 in #updated

Download history 17/week @ 2024-09-15 31/week @ 2024-09-22 32/week @ 2024-09-29 9/week @ 2024-10-06 16/week @ 2024-10-13 55/week @ 2024-10-20 29/week @ 2024-10-27 180/week @ 2024-11-03 553/week @ 2024-11-10 633/week @ 2024-11-17 312/week @ 2024-11-24 378/week @ 2024-12-01 579/week @ 2024-12-08 401/week @ 2024-12-15 38/week @ 2024-12-22 137/week @ 2024-12-29

1,172 downloads per month
Used in 4 crates (via fedimint-ln-gateway)

MIT license

400KB
11K SLoC

cln-rpc: Talk to Core Lightning


lib.rs:

A Core Lightning RPC-client

Core Lightning exposes a JSON-RPC interface over unix-domain sockets. The unix-domain socket appears like file and located by default in ~/.lightning/<network>/lightning-rpc.

This crate contains an RPC-client called [ClnRpc] and models for most requests and responses.

The example below shows how to initiate the client and celss the getinfo-rpc method.

use std::path::Path;
use tokio_test;
use cln_rpc::{ClnRpc, TypedRequest};
use cln_rpc::model::requests::GetinfoRequest;
use cln_rpc::model::responses::GetinfoResponse;

tokio_test::block_on( async {
    let path = Path::new("path_to_lightning_dir");
    let mut rpc = ClnRpc::new(path).await.unwrap();
    let request = GetinfoRequest {};
    let response : GetinfoResponse = rpc.call_typed(&request).await.unwrap();
});

If the required model is not available you can implement TypedRequest and use ClnRpc::call_typed without a problem.

use std::path::Path;
use tokio_test;
use cln_rpc::{ClnRpc, TypedRequest};
use serde::{Serialize, Deserialize};

#[derive(Serialize, Debug)]
struct CustomMethodRequest {
    param_a : String
};
#[derive(Deserialize, Debug)]
struct CustomMethodResponse {
    field_a : String
};

impl TypedRequest for CustomMethodRequest {
    type Response = CustomMethodResponse;

    fn method(&self) -> &str {
        "custommethod"
    }
}

tokio_test::block_on( async {
    let path = Path::new("path_to_lightning_dir");
    let mut rpc = ClnRpc::new(path).await.unwrap();

    let request = CustomMethodRequest { param_a : String::from("example")};
    let response = rpc.call_typed(&request).await.unwrap();
})

An alternative is to use ClnRpc::call_raw.

use std::path::Path;
use tokio_test;
use cln_rpc::{ClnRpc, TypedRequest};

tokio_test::block_on( async {
    let path = Path::new("path_to_lightning_dir");
    let mut rpc = ClnRpc::new(path).await.unwrap();
    let method = "custommethod";
    let request = serde_json::json!({"param_a" : "example"});
    let response : serde_json::Value = rpc.call_raw(method, &request).await.unwrap();
})

Dependencies

~11–21MB
~243K SLoC