6 stable releases

2.4.0 Feb 28, 2024
2.3.1 Dec 16, 2023
2.3.0 Sep 29, 2023
2.2.0 Aug 2, 2023
2.0.0 Feb 28, 2023

#1304 in Asynchronous

Download history 87/week @ 2024-02-22 67/week @ 2024-02-29 43/week @ 2024-03-07 15/week @ 2024-03-14 22/week @ 2024-03-28 16/week @ 2024-04-04

57 downloads per month
Used in lightning-probing

MIT license

93KB
359 lines

LND gRPC Client in Rust.

Rust implementation of LND RPC client using async gRPC library tonic_openssl.

This is a fork from https://github.com/yzernik/tonic_openssl_lnd but uses hex credentials over paths to be able to connect to LND remotely without directly reading from macaroon and cert files.

About

Warning: this crate is in early development and may have unknown problems! Review it before using with mainnet funds!

This crate implements LND GRPC using tonic_openssl and prost. Apart from being up-to-date at the time of writing (:D) it also allows async usage. It contains vendored *.proto files so LND source code is not required but accepts an environment variable LND_REPO_DIR which overrides the vendored *.proto files. This can be used to test new features in non-released lnd. (Actually, the motivating project using this library was that case. :))

Usage

There's no setup needed beyond adding the crate to your Cargo.toml. If you need to change the *.proto files from which the client is generated, set the environment variable LND_REPO_DIR to a directory with cloned lnd during build.

Here's an example of retrieving information from LND ([getinfo](https://api.lightning.community/#getinfo) call). You can find the same example in crate root for your convenience.

Connect function takes cert and macaroon in hex format.

use std::fs;

#[tokio::main]
async fn main() {
        // Read the contents of the file into a vector of bytes
        let cert_bytes = fs::read("/path/to/tls.cert").expect("FailedToReadTlsCertFile");
        let mac_bytes = fs::read("path/to/macaroon").expect("FailedToReadMacaroonFile");
    
    
            // Convert the bytes to a hex string
        let cert = buffer_as_hex(cert_bytes);
        let macaroon = buffer_as_hex(mac_bytes);
    
        let socket = "localhost:10001".to_string();
    
        let mut client = lnd_grpc_rust::connect(cert, macaroon, socket)
        .await
        .expect("failed to connect");

    let info = client
        .lightning()
        // All calls require at least empty parameter
        .get_info(lnd_grpc_rust::lnrpc::GetInfoRequest {})
        .await
        .expect("failed to get info");

    // We only print it here, note that in real-life code you may want to call `.into_inner()` on
    // the response to get the message.
    println!("{:#?}", info);
}

fn buffer_as_hex(bytes: Vec<u8>) -> String {
    let hex_str = bytes.iter().map(|b| format!("{:02x}", b)).collect::<String>();

    return hex_str;
}

License

MIT

Dependencies

~12–25MB
~335K SLoC