Expand description
§LNSocket
lnsocket is a minimal, async Lightning Network socket library built on tokio.
It implements the BOLT 8 Noise handshake and typed Lightning wire framing, and
it stays out of your way: no global state, no TLS, no heavy deps.
§What this crate gives you
LNSocket– connect over TCP, perform Noise (act1/2/3), and read/write typed BOLT#1 messages.CommandoClient– a small client for Core Lightning Commando over a liveLNSocket, with a background pump, auto-reconnect, and retry/resend semantics.
§Design philosophy
- Keep the transport tight and explicit. You own key management, policies, and backpressure.
- Avoid surprises: I/O errors return an
Errorthat carriesio::ErrorKindonly.
§Quick starts
§Low-level: just a Lightning socket
use bitcoin::secp256k1::{SecretKey, PublicKey, rand};
use lnsocket::{LNSocket, ln::msgs};
let our_key = SecretKey::new(&mut rand::thread_rng());
let mut sock = LNSocket::connect_and_init(our_key, their_pubkey, "node.example.com:9735").await?;
sock.write(&msgs::Ping { ponglen: 4, byteslen: 8 }).await?;
let _msg = sock.read().await?; // e.g. expect a Pong§Higher-level: Commando over LNSocket
use bitcoin::secp256k1::{SecretKey, PublicKey, rand};
use lnsocket::{LNSocket, CommandoClient};
use serde_json::json;
let key = SecretKey::new(&mut rand::thread_rng());
let sock = LNSocket::connect_and_init(key, their_pubkey, "ln.example.com:9735").await?;
// Spawns a background pump task. IDs are generated internally.
let commando = CommandoClient::spawn(sock, rune);
// Simple call with crate defaults (30s timeout, auto-reconnect, retry up to 3 times).
let info = commando.call("getinfo", json!({})).await?;
println!("getinfo: {}", info);§Footguns & non-goals
- No built-in keepalives/backpressure – handle in your app.
- Reconnection logic lives in
CommandoClient, notLNSocket. LNSocket::perform_initperforms a minimalinitexchange by design.
Re-exports§
pub use commando::CallOpts;pub use commando::CommandoClient;pub use error::Error;pub use error::RpcError;pub use lnsocket::LNSocket;pub use bitcoin;
Modules§
Macros§
- decode_
tlv_ stream - Implements the TLVs deserialization part in a
Readableimplementation of a struct. - encode_
tlv_ stream - Implements the TLVs serialization part in a
Writeableimplementation of a struct. - impl_
writeable_ msg - Implements
LengthReadable/Writeablefor a message struct that may include non-TLV and TLV-encoded parts. - impl_
writeable_ tlv_ based - Implements
Readable/Writeablefor a struct storing it as a set of TLVs. Each TLV is read/written in the order they appear and contains a type number, a field name, and a de/serialization method, from the following: - impl_
writeable_ tlv_ based_ enum - Implement
ReadableandWriteablefor an enum, with struct variants stored as TLVs and tuple variants stored directly. - impl_
writeable_ tlv_ based_ enum_ upgradable - Implement
MaybeReadableandWriteablefor an enum, with struct variants stored as TLVs and tuple variants stored directly. - read_
tlv_ fields - Reads a suffix added by
write_tlv_fields. - write_
tlv_ fields - Writes out a suffix to an object as a length-prefixed TLV stream which contains potentially backwards-compatible, optional fields which old nodes can happily ignore.