This repository has been archived by the owner on Nov 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: lnd client + rust binary to run btcd and lnd (#43)
- Loading branch information
Showing
21 changed files
with
3,725 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use bitcoind::bitcoincore_rpc::RpcApi; | ||
use lnd::Lnd; | ||
use lnd::LndConf; | ||
use std::sync::mpsc::channel; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
println!("Downloading bitcoind..."); | ||
let bitcoind = bitcoind::BitcoinD::from_downloaded().unwrap(); | ||
assert_eq!(0, bitcoind.client.get_blockchain_info().unwrap().blocks); | ||
println!("Done"); | ||
println!( | ||
"bitcoind is running. Listening to {:?}", | ||
bitcoind.params.rpc_socket | ||
); | ||
|
||
println!("Downloading lnd..."); | ||
let lnd_conf = LndConf::default(); | ||
let mut lnd = Lnd::with_conf( | ||
lnd::exe_path().unwrap(), | ||
&lnd_conf, | ||
bitcoind | ||
.params | ||
.cookie_file | ||
.clone() | ||
.into_os_string() | ||
.into_string() | ||
.unwrap(), | ||
bitcoind.params.rpc_socket.to_string(), | ||
&bitcoind, | ||
) | ||
.await | ||
.unwrap(); | ||
assert!(lnd | ||
.client | ||
.lightning() | ||
.get_info(tonic_lnd::lnrpc::GetInfoRequest {}) | ||
.await | ||
.is_ok()); | ||
println!("Done"); | ||
std::fs::write( | ||
".env", | ||
format!( | ||
"export LND_URL={:?}\nexport LND_CERT={:?}\nexport LND_MACAROON={:?}", | ||
lnd.grpc_url, | ||
lnd.tls_cert_path(), | ||
lnd.admin_macaroon_path() | ||
), | ||
) | ||
.unwrap(); | ||
|
||
let (tx, rx) = channel(); | ||
|
||
ctrlc::set_handler(move || tx.send(()).expect("Could not send signal on channel.")) | ||
.expect("Error setting Ctrl-C handler"); | ||
|
||
println!("Press ctrl+c to exit"); | ||
|
||
rx.recv().expect("Could not receive from channel."); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
defmodule Cashubrew.LightingNetwork.Lnd do | ||
@moduledoc """ | ||
Client to interact with lnd | ||
""" | ||
use GenServer | ||
require Logger | ||
|
||
def start_link(arg) do | ||
GenServer.start_link(__MODULE__, arg, name: __MODULE__) | ||
end | ||
|
||
def init(args) do | ||
node_url = URI.parse(System.get_env("LND_URL")) | ||
creds = get_creds(System.get_env("LND_CERT")) | ||
macaroon = get_macaroon(System.get_env("LND_MACAROON")) | ||
|
||
Logger.debug("gRPC client connecting to gateway at #{node_url}") | ||
|
||
case GRPC.Stub.connect("#{node_url.host}:#{node_url.port}", | ||
cred: creds | ||
) do | ||
{:error, error} -> | ||
Logger.critical("Could not connect. Retrying... #{error}") | ||
init(args) | ||
|
||
{:ok, channel} -> | ||
Logger.info("Connected to the gateway at #{node_url}") | ||
{:ok, %{channel: channel, macaroon: macaroon}} | ||
end | ||
end | ||
|
||
# one day | ||
def validity, do: 86_400 | ||
|
||
def create_invoice!(amount, unit, description) do | ||
GenServer.call(__MODULE__, {:create_invoice, amount, unit, description}, __MODULE__) | ||
end | ||
|
||
def handle_call({:create_invoice, amount, unit, description}, _from, state) do | ||
if unit != "sat" do | ||
raise "UnsuportedUnit" | ||
end | ||
|
||
amount_ms = amount * 1000 | ||
|
||
expiry = validity() + System.os_time(:second) | ||
|
||
request = %Cashubrew.Lnrpc.Invoice{ | ||
memo: description, | ||
value_msat: amount_ms, | ||
expiry: expiry | ||
} | ||
|
||
{:ok, response} = Cashubrew.Lnrpc.Lightning.Stub.add_invoice(state["channel"], request) | ||
{:reply, response, state} | ||
end | ||
|
||
defp get_creds(cert_path) do | ||
filename = Path.expand(cert_path) | ||
|
||
# ++ [verify: true/:verify_none] | ||
ssl_opts = [cacertfile: filename] | ||
|
||
GRPC.Credential.new(ssl: ssl_opts) | ||
end | ||
|
||
defp get_macaroon(macaroon_path) do | ||
filename = Path.expand(macaroon_path) | ||
|
||
File.read!(filename) |> Base.encode16() | ||
end | ||
end |
Oops, something went wrong.