| Crates.io | embedded-lnd |
| lib.rs | embedded-lnd |
| version | 0.1.3 |
| created_at | 2024-09-22 01:30:48.288145+00 |
| updated_at | 2024-09-24 12:35:35.720592+00 |
| description | embedded-lnd is a Rust library that provides a high-level, safe interface for interacting with an embedded LND node. |
| homepage | |
| repository | https://github.com/hsjoberg/embedded-lnd/tree/master/rust |
| max_upload_size | |
| id | 1382672 |
| size | 109,359 |
embedded-lnd is a Rust library that provides a high-level, safe interface for interacting with an embedded LND node.
You can compile LND using CGO for Linux, MacOS and Windows and embed it into your application and interact with it.
At compile time, build.rs takes the liblnd.h file and generates a bindings.rs file for Rust <-> C FFI.
You can then import protobuf types and LND grpc methods from the library and just call them.
Refer to LND API docs for methods and types.
cargo add embedded-lnd
# In your app, set the env variable LND_LIB_DIR=/path/to/static/lnd/binary
Here's a basic example of how to use embedded-lnd:
use embedded_lnd::{LndClient, lnrpc, getInfo};
use std::sync::Arc;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Arc::new(LndClient::new());
let start_args = "--lnddir=./lnd \
--noseedbackup \
--nolisten \
--bitcoin.active \
--bitcoin.regtest \
--bitcoin.node=neutrino \
--feeurl=\"https://nodes.lightning.computer/fees/v1/btc-fee-estimates.json\" \
--routing.assumechanvalid \
--tlsdisableautofill \
--db.bolt.auto-compact \
--db.bolt.auto-compact-min-age=0 \
--neutrino.connect=localhost:19444";
// Start LND
client.start(start_args)?;
// Get node info
let info: lnrpc::GetInfoResponse = client.call_lnd_method(lnrpc::GetInfoRequest {}, getInfo)?;
println!("Node info: {:?}", info);
Ok(())
}
use embedded_lnd::{LndClient, lnrpc, addInvoice};
let client = Arc::new(LndClient::new());
let invoice = lnrpc::Invoice {
memo: "Test invoice".to_string(),
value: 1000,
..Default::default()
};
let response: lnrpc::AddInvoiceResponse = client.call_lnd_method(invoice, addInvoice)?;
println!("Invoice created: {:?}", response);
use embedded_lnd::{LndClient, lnrpc, subscribePeerEvents};
let client = Arc::new(LndClient::new());
client.subscribe_events::<lnrpc::PeerEvent, lnrpc::PeerEventSubscription>(subscribePeerEvents)
.on_event(|event_result| match event_result {
Ok(event) => println!("Received peer event: {:?}", event.pub_key),
Err(e) => eprintln!("Peer event error: {}", e),
})
.with_request(lnrpc::PeerEventSubscription::default())
.subscribe()?;
use embedded_lnd::{LndClient, lnrpc, channelAcceptor};
let client = Arc::new(LndClient::new());
let acceptor = client
.bidi_stream::<lnrpc::ChannelAcceptRequest, lnrpc::ChannelAcceptResponse>(channelAcceptor)
.on_request(|request_result| {
match request_result {
Ok(request) => println!("Received channel request: {:?}", request),
Err(e) => println!("Error: {}", e),
}
})
.get_response(|request| {
request.map(|req| {
lnrpc::ChannelAcceptResponse {
accept: false,
pending_chan_id: req.pending_chan_id,
error: "Channel not accepted".to_string(),
..Default::default()
}
})
})
.build()?;
For detailed API documentation, run cargo doc --open in your project directory.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License.
This software is in beta and should not be used in production environments without proper review and testing.
If you have any questions or feedback, please open an issue on the GitHub repository.