Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for full LND RPC API #20

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ keywords = ["LND", "rpc", "grpc", "tonic", "async"]
categories = ["api-bindings", "asynchronous", "cryptography::cryptocurrencies", "network-programming"]
license = "MITNFA"

[lib]
doctest = false

[dependencies]
tonic = { version = "0.6.2", features = ["transport", "tls"] }
prost = "0.9.0"
rustls = { version = "0.19.0", features = ["dangerous_configuration"] }
webpki = "0.21.3"
rustls-pemfile = "1.0.0"
tonic = "0.7"
tonic-openssl = { version = "0.2" }
hyper = "0.14"
hyper-openssl = "0.9"
prost = "0.10"
tokio = { version = "1", features = ["full"] }
tokio-stream = { version = "0.1", features = ["net"] }
openssl = "0.10"
tower = "0.4"
pretty_env_logger = "0.4.0"
hex = "0.4.3"
tokio = { version = "1.7.1", features = ["fs"] }
tracing = { version = "0.1", features = ["log"], optional = true }

[build-dependencies]
tonic-build = "0.5.2"

[dev-dependencies]
tokio = { version = "1.7.1", features = ["rt-multi-thread"] }
tonic-build = "0.7"
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,29 @@ You can find the same example in crate root for your convenience.
async fn main() {
let mut args = std::env::args_os();
args.next().expect("not even zeroth arg given");
let address = args.next().expect("missing arguments: address, cert file, macaroon file");
let cert_file = args.next().expect("missing arguments: cert file, macaroon file");
let macaroon_file = args.next().expect("missing argument: macaroon file");
let address = args
.next()
.expect("missing arguments: address, cert file, macaroon file");
let cert_file = args
.next()
.expect("missing arguments: cert file, macaroon file")
.into_string()
.expect("cert_file is not UTF-8");
let macaroon_file = args
.next()
.expect("missing argument: macaroon file")
.into_string()
.expect("cert_file is not UTF-8");
let address = address.into_string().expect("address is not UTF-8");

// Connecting to LND requires only address, cert file, and macaroon file
let mut client = tonic_lnd::connect(address, cert_file, macaroon_file)
let mut client = tonic_lnd::connect_lightning(address, cert_file, macaroon_file)
.await
.expect("failed to connect");

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

Expand Down
39 changes: 31 additions & 8 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::{Path, PathBuf};
use std::path::PathBuf;

fn main() -> std::io::Result<()> {
println!("cargo:rerun-if-env-changed=LND_REPO_DIR");
Expand All @@ -8,17 +8,40 @@ fn main() -> std::io::Result<()> {
let mut lnd_rpc_dir = PathBuf::from(lnd_repo_path);
lnd_rpc_dir.push("lnrpc");
lnd_rpc_dir_owned = lnd_rpc_dir;
&*lnd_rpc_dir_owned
},
None => Path::new("vendor"),
lnd_rpc_dir_owned.display().to_string()
}
None => "vendor".to_string(),
};

let lnd_rpc_proto_file = dir.join("lightning.proto");
println!("cargo:rerun-if-changed={}", lnd_rpc_proto_file.display());
let protos = vec![
"autopilotrpc/autopilot.proto",
"chainrpc/chainnotifier.proto",
"devrpc/dev.proto",
"invoicesrpc/invoices.proto",
"lightning.proto",
"lnclipb/lncli.proto",
"neutrinorpc/neutrino.proto",
"peersrpc/peers.proto",
"routerrpc/router.proto",
"signrpc/signer.proto",
"verrpc/verrpc.proto",
"walletrpc/walletkit.proto",
"watchtowerrpc/watchtower.proto",
"wtclientrpc/wtclient.proto",
];

let proto_paths: Vec<_> = protos
.iter()
.map(|proto| {
let mut path = PathBuf::from(&dir);
path.push(proto);
path.display().to_string()
})
.collect();

tonic_build::configure()
.build_client(true)
.build_server(false)
.format(false)
.compile(&[&*lnd_rpc_proto_file], &[dir])
.compile(&proto_paths, &[dir])?;
Ok(())
}
40 changes: 40 additions & 0 deletions examples/addholdinvoice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// This program accepts three arguments: address, cert file, macaroon file
// The address must start with `https://`!

#[tokio::main]
async fn main() {
let mut args = std::env::args_os();
args.next().expect("not even zeroth arg given");
let address = args
.next()
.expect("missing arguments: address, cert file, macaroon file");
let cert_file = args
.next()
.expect("missing arguments: cert file, macaroon file")
.into_string()
.expect("cert_file is not UTF-8");
let macaroon_file = args
.next()
.expect("missing argument: macaroon file")
.into_string()
.expect("cert_file is not UTF-8");
let address = address.into_string().expect("address is not UTF-8");

// Connecting to LND requires only address, cert file, macaroon file
let mut invoices_client = tonic_lnd::connect_invoices(address, cert_file, macaroon_file)
.await
.expect("failed to connect");

let add_hold_invoice_resp = invoices_client
.add_hold_invoice(tonic_lnd::invoicesrpc::AddHoldInvoiceRequest {
hash: vec![0; 32],
value: 5555,
..Default::default()
})
.await
.expect("failed to add hold invoice");

// 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!("{:#?}", add_hold_invoice_resp);
}
20 changes: 15 additions & 5 deletions examples/getinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,29 @@
async fn main() {
let mut args = std::env::args_os();
args.next().expect("not even zeroth arg given");
let address = args.next().expect("missing arguments: address, cert file, macaroon file");
let cert_file = args.next().expect("missing arguments: cert file, macaroon file");
let macaroon_file = args.next().expect("missing argument: macaroon file");
let address = args
.next()
.expect("missing arguments: address, cert file, macaroon file");
let cert_file = args
.next()
.expect("missing arguments: cert file, macaroon file")
.into_string()
.expect("cert_file is not UTF-8");
let macaroon_file = args
.next()
.expect("missing argument: macaroon file")
.into_string()
.expect("cert_file is not UTF-8");
let address = address.into_string().expect("address is not UTF-8");

// Connecting to LND requires only address, cert file, and macaroon file
let mut client = tonic_lnd::connect(address, cert_file, macaroon_file)
let mut client = tonic_lnd::connect_lightning(address, cert_file, macaroon_file)
.await
.expect("failed to connect");

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

Expand Down
18 changes: 12 additions & 6 deletions examples/subscribe_invoices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,23 @@ async fn main() {
.expect("missing arguments: address, cert file, macaroon file");
let cert_file = args
.next()
.expect("missing arguments: cert file, macaroon file");
let macaroon_file = args.next().expect("missing argument: macaroon file");
.expect("missing arguments: cert file, macaroon file")
.into_string()
.expect("cert_file is not UTF-8");
let macaroon_file = args
.next()
.expect("missing argument: macaroon file")
.into_string()
.expect("cert_file is not UTF-8");
let address = address.into_string().expect("address is not UTF-8");

// Connecting to LND requires only address, cert file, and macaroon file
let mut client = tonic_lnd::connect(address, cert_file, macaroon_file)
let mut client = tonic_lnd::connect_lightning(address, cert_file, macaroon_file)
.await
.expect("failed to connect");

let mut invoice_stream = client
.subscribe_invoices(tonic_lnd::rpc::InvoiceSubscription {
.subscribe_invoices(tonic_lnd::lnrpc::InvoiceSubscription {
add_index: 0,
settle_index: 0,
})
Expand All @@ -34,9 +40,9 @@ async fn main() {
.await
.expect("Failed to receive invoices")
{
if let Some(state) = tonic_lnd::rpc::invoice::InvoiceState::from_i32(invoice.state) {
if let Some(state) = tonic_lnd::lnrpc::invoice::InvoiceState::from_i32(invoice.state) {
// If this invoice was Settled we can do something with it
if state == tonic_lnd::rpc::invoice::InvoiceState::Settled {
if state == tonic_lnd::lnrpc::invoice::InvoiceState::Settled {
println!("{:?}", invoice);
}
}
Expand Down
21 changes: 5 additions & 16 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@ pub struct ConnectError {

impl From<InternalConnectError> for ConnectError {
fn from(value: InternalConnectError) -> Self {
ConnectError {
internal: value,
}
ConnectError { internal: value }
}
}

#[derive(Debug)]
pub(crate) enum InternalConnectError {
ReadFile { file: PathBuf, error: std::io::Error, },
ParseCert { file: PathBuf, error: std::io::Error, },
InvalidAddress { address: String, error: Box<dyn std::error::Error + Send + Sync + 'static>, },
TlsConfig(tonic::transport::Error),
Connect { address: String, error: tonic::transport::Error, }
ReadFile {
file: PathBuf,
error: std::io::Error,
},
}

impl fmt::Display for ConnectError {
Expand All @@ -34,10 +31,6 @@ impl fmt::Display for ConnectError {

match &self.internal {
ReadFile { file, .. } => write!(f, "failed to read file {}", file.display()),
ParseCert { file, .. } => write!(f, "failed to parse certificate {}", file.display()),
InvalidAddress { address, .. } => write!(f, "invalid address {}", address),
TlsConfig(_) => write!(f, "failed to configure TLS"),
Connect { address, .. } => write!(f, "failed to connect to {}", address),
}
}
}
Expand All @@ -48,10 +41,6 @@ impl std::error::Error for ConnectError {

match &self.internal {
ReadFile { error, .. } => Some(error),
ParseCert { error, .. } => Some(error),
InvalidAddress { error, .. } => Some(&**error),
TlsConfig(error) => Some(error),
Connect { error, .. } => Some(error),
}
}
}
Loading