gui: use rpcauth in installer for internal bitcoind

This commit is contained in:
jp1ac4 2024-03-05 11:58:15 +00:00
parent 7584b6347b
commit 5e5c3330ee
No known key found for this signature in database
GPG Key ID: A7ACD32423568D7B

View File

@ -24,8 +24,8 @@ use crate::{
bitcoind::{ bitcoind::{
self, bitcoind_network_dir, internal_bitcoind_datadir, internal_bitcoind_directory, self, bitcoind_network_dir, internal_bitcoind_datadir, internal_bitcoind_directory,
Bitcoind, ConfigField, InternalBitcoindConfig, InternalBitcoindConfigError, Bitcoind, ConfigField, InternalBitcoindConfig, InternalBitcoindConfigError,
InternalBitcoindNetworkConfig, RpcAuthType, RpcAuthValues, StartInternalBitcoindError, InternalBitcoindNetworkConfig, RpcAuth, RpcAuthType, RpcAuthValues,
VERSION, StartInternalBitcoindError, VERSION,
}, },
download, download,
hw::HardwareWallets, hw::HardwareWallets,
@ -598,9 +598,12 @@ impl Step for InternalBitcoindStep {
return Command::none(); return Command::none();
} }
}; };
// Insert entry for network if not present. let (rpc_port, p2p_port) = if let Some(network_conf) =
if conf.networks.get(&self.network).is_none() { conf.networks.get(&self.network)
let network_conf = match (get_available_port(), get_available_port()) { {
(network_conf.rpc_port, network_conf.p2p_port)
} else {
match (get_available_port(), get_available_port()) {
(Ok(rpc_port), Ok(p2p_port)) => { (Ok(rpc_port), Ok(p2p_port)) => {
// In case ports are the same, user will need to click button again for another attempt. // In case ports are the same, user will need to click button again for another attempt.
if rpc_port == p2p_port { if rpc_port == p2p_port {
@ -610,12 +613,7 @@ impl Step for InternalBitcoindStep {
); );
return Command::none(); return Command::none();
} }
InternalBitcoindNetworkConfig { (rpc_port, p2p_port)
rpc_port,
p2p_port,
prune: PRUNE_DEFAULT,
rpc_auth: None,
}
} }
(Ok(_), Err(e)) | (Err(e), Ok(_)) => { (Ok(_), Err(e)) | (Err(e), Ok(_)) => {
self.error = Some(format!("Could not get available port: {}.", e)); self.error = Some(format!("Could not get available port: {}.", e));
@ -626,9 +624,27 @@ impl Step for InternalBitcoindStep {
Some(format!("Could not get available ports: {}; {}.", e1, e2)); Some(format!("Could not get available ports: {}; {}.", e1, e2));
return Command::none(); return Command::none();
} }
}; }
conf.networks.insert(self.network, network_conf); };
} let (rpc_auth, rpc_password) = match RpcAuth::new("liana") {
Ok((rpc_auth, password)) => (rpc_auth, password),
Err(e) => {
self.error = Some(e.to_string());
return Command::none();
}
};
let bitcoind_config = BitcoindConfig {
rpc_auth: BitcoindRpcAuth::UserPass(rpc_auth.user.clone(), rpc_password),
addr: internal_bitcoind_address(rpc_port),
};
let network_conf = InternalBitcoindNetworkConfig {
rpc_port,
p2p_port,
prune: PRUNE_DEFAULT,
// Overwrite any previous entry for this network as we would no longer know the RPC password.
rpc_auth: Some(rpc_auth),
};
conf.networks.insert(self.network, network_conf);
if let Err(e) = conf.to_file(&bitcoind::internal_bitcoind_config_path( if let Err(e) = conf.to_file(&bitcoind::internal_bitcoind_config_path(
&self.bitcoind_datadir, &self.bitcoind_datadir,
)) { )) {
@ -636,7 +652,8 @@ impl Step for InternalBitcoindStep {
return Command::none(); return Command::none();
}; };
self.error = None; self.error = None;
self.internal_bitcoind_config = Some(conf.clone()); self.internal_bitcoind_config = Some(conf);
self.bitcoind_config = Some(bitcoind_config);
return Command::perform(async {}, |_| { return Command::perform(async {}, |_| {
Message::InternalBitcoind(message::InternalBitcoindMsg::Reload) Message::InternalBitcoind(message::InternalBitcoindMsg::Reload)
}); });
@ -693,43 +710,18 @@ impl Step for InternalBitcoindStep {
} }
} }
message::InternalBitcoindMsg::Start => { message::InternalBitcoindMsg::Start => {
let bitcoind_datadir = match self.bitcoind_datadir.canonicalize() { if let Err(e) = self.bitcoind_datadir.canonicalize() {
Ok(path) => path, self.started = Some(Err(
Err(e) => { StartInternalBitcoindError::CouldNotCanonicalizeDataDir(e.to_string()),
self.started = Some(Err( ));
StartInternalBitcoindError::CouldNotCanonicalizeDataDir( return Command::none();
e.to_string(),
),
));
return Command::none();
}
}; };
// Pass the canonicalized `bitcoind_datadir` so that the cookie path returned let bitcoind_config = self
// by `internal_bitcoind_cookie_path` is also canonicalized. This way, the .bitcoind_config
// canonicalized path will later be saved to the config file.
// We cannot use `cookie_path.canonicalize()` as we have not yet started
// bitcoind and so the path does not exist.
let cookie_path =
bitcoind::internal_bitcoind_cookie_path(&bitcoind_datadir, &self.network);
let rpc_port = self
.internal_bitcoind_config
.as_ref() .as_ref()
.expect("Already added") .expect("already added")
.clone() .clone();
.networks match Bitcoind::start(&self.network, bitcoind_config, &self.liana_datadir) {
.get(&self.network)
.expect("Already added")
.rpc_port;
match Bitcoind::start(
&self.network,
BitcoindConfig {
rpc_auth: BitcoindRpcAuth::CookieFile(cookie_path),
addr: internal_bitcoind_address(rpc_port),
},
&self.liana_datadir,
) {
Err(e) => { Err(e) => {
self.started = self.started =
Some(Err(StartInternalBitcoindError::CommandError(e.to_string()))); Some(Err(StartInternalBitcoindError::CommandError(e.to_string())));
@ -737,7 +729,6 @@ impl Step for InternalBitcoindStep {
} }
Ok(bitcoind) => { Ok(bitcoind) => {
self.error = None; self.error = None;
self.bitcoind_config = Some(bitcoind.config.clone());
self.started = Some(Ok(())); self.started = Some(Ok(()));
self.internal_bitcoind = Some(bitcoind); self.internal_bitcoind = Some(bitcoind);
} }