Crate cln_rpc

source ·
Expand description

§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();
})

Re-exports§

Modules§

  • The codec is used to encode and decode messages received from and sent to the main daemon. The protocol uses stdout and stdin to exchange JSON formatted messages. Each message is separated by an empty line and we’re guaranteed that no other empty line is present in the messages.
  • Common structs to handle JSON-RPC decoding and encoding. They are generic over the Notification and Request types.
  • A collection of models to describe requests and responses.
  • Primitive types representing Amounts, PublicKeys, …

Structs§

  • An RPC-client for Core Lightning
  • The Error type, a wrapper around a dynamic error type.