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

better handling of cookie file on signet/mutinynet #64

Merged
merged 2 commits into from
Oct 2, 2024
Merged
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
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
70 changes: 48 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 All @@ -118,11 +122,13 @@ async fn main() -> Result<(), anyhow::Error> {
Some(sbp) => u16::try_from(sbp)?,
None => match network.as_str() {
"regtest" => 18443,
"signet" | "mutinynet" => 38332,
_ => 8332,
},
},
None => match network.as_str() {
"regtest" => 18443,
"signet" | "mutinynet" => 38332,
_ => 8332,
},
};
Expand All @@ -144,23 +150,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
Loading