Skip to content

Commit

Permalink
better handling of cookie file on signet/mutinynet
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisguida committed Sep 29, 2024
1 parent 3fb3823 commit efdab67
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 75 deletions.
63 changes: 11 additions & 52 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ clap = { version = "4.4.0", features = ["derive"] }
cln-plugin = { git = "https://github.com/elementsproject/lightning", version = "0.1.4" }
# cln-plugin = { path = "../../lightning/plugins" }
# cln-plugin = { git = "https://github.com/chrisguida/lightning", version = "0.1.4", branch = "feat/cln-plugin-send-notifs" }
cln-rpc = "0.1.3"
cln-rpc = "0.2.0"
home = "0.5.5"
log = "0.4.18"
serde = "1.0.159"
Expand Down
68 changes: 46 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ extern crate serde_json;

use bdk::chain::tx_graph::CanonicalTx;
use bdk::chain::ConfirmationTimeAnchor;
use bitcoincore_rpc::Auth;
// use bitcoincore_rpc::Auth;
use bitcoincore_rpc::{Auth, Client, RpcApi};
use clap::error::ErrorKind;
use clap::{CommandFactory, Parser, Subcommand};
use cln_rpc::model::DatastoreMode;
use cln_rpc::model::requests::DatastoreMode;
use cln_rpc::{
model::requests::{DatastoreRequest, ListdatastoreRequest},
ClnRpc, Request, Response,
Expand Down Expand Up @@ -86,21 +87,24 @@ async fn main() -> Result<(), anyhow::Error> {
} else {
return Ok(());
};
log::debug!(
"Configuration from CLN main daemon: {:?}",
configured_plugin.configuration()
);
log::debug!(
"smaug_network = {:?}, cln_network = {}",
configured_plugin.option("smaug_network"),
configured_plugin.configuration().network
);
let cln_network = configured_plugin.configuration().network.clone();
if log::log_enabled!(log::Level::Debug) {
eprintln!(
"Configuration from CLN main daemon: {:?}",
configured_plugin.configuration()
);
eprintln!(
"smaug_network = {:?}, cln_network = {}",
configured_plugin.option("smaug_network").unwrap().as_str(),
&cln_network,
);
}
let network = match configured_plugin.option("smaug_network") {
Some(smaug_network) => match smaug_network.as_str() {
Some(sn) => sn.to_owned(),
None => configured_plugin.configuration().network,
None => cln_network.clone(),
},
None => configured_plugin.configuration().network,
None => cln_network.clone(),
};
let brpc_host = match configured_plugin.option("smaug_brpc_host") {
Some(smaug_brpc_host) => match smaug_brpc_host.as_str() {
Expand Down Expand Up @@ -144,23 +148,43 @@ async fn main() -> Result<(), anyhow::Error> {
if let Auth::None = brpc_auth {
if let Some(smaug_brpc_cookie_dir) = configured_plugin.option("smaug_brpc_cookie_dir") {
if let Some(sbcd) = smaug_brpc_cookie_dir.as_str() {
brpc_auth = Auth::CookieFile(PathBuf::from(sbcd).join(".cookie"))
let cf_path = PathBuf::from(sbcd)
.join(".cookie");
if !cf_path.exists() {
return Err(anyhow!("Nonexistent cookie file specified in smaug_brpc_cookie_dir: {}", cf_path.display()));
}
brpc_auth = Auth::CookieFile(PathBuf::from(sbcd).join(".cookie"));
} else {
if network == "regtest" {
brpc_auth = Auth::CookieFile(
home_dir()
.expect("cannot determine home dir")
.join(".bitcoin/regtest")
.join(".cookie"),
);
let cf_path = home_dir()
.expect("cannot determine home dir")
.join(format!(".bitcoin/{}", cln_network.clone()))
.join(".cookie");
if cf_path.exists() {
brpc_auth = Auth::CookieFile(cf_path);
}
}
}
}
if let Auth::None = brpc_auth {
return Err(anyhow!("must specify either `smaug_bprc_cookie_dir` or `smaug_brpc_user` and `smaug_brpc_pass`"));
} else {
if log::log_enabled!(log::Level::Debug) {
eprintln!("using auth info: {:?}", brpc_auth);
}
let rpc_client = Client::new(
&format!("http://{}:{}", brpc_host.clone(), brpc_port.clone()),
brpc_auth.clone(),
)?;

let _ = match rpc_client.get_connection_count() {
Ok(cc) => cc,
Err(e) => {
return Err(anyhow!("Cannot connect to bitcoind, ensure your `smaug_bprc_cookie_dir` or `smaug_brpc_user` and `smaug_brpc_pass` are correct
and that your node is active and accepting rpc connections"))
},
};
}
log::trace!("using auth info: {:?}", brpc_auth);

let ln_dir: PathBuf = configured_plugin.configuration().lightning_dir.into();
// Create data dir if it does not exist
fs::create_dir_all(ln_dir.join(SMAUG_DATADIR)).unwrap_or_else(|e| {
Expand Down

0 comments on commit efdab67

Please sign in to comment.