Skip to content

Commit 7e2b384

Browse files
committed
gui: use rpcauth in installer for internal bitcoind
1 parent 5c0939b commit 7e2b384

File tree

1 file changed

+42
-51
lines changed

1 file changed

+42
-51
lines changed

gui/src/installer/step/bitcoind.rs

+42-51
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ use crate::{
2424
bitcoind::{
2525
self, bitcoind_network_dir, internal_bitcoind_datadir, internal_bitcoind_directory,
2626
Bitcoind, ConfigField, InternalBitcoindConfig, InternalBitcoindConfigError,
27-
InternalBitcoindNetworkConfig, RpcAuthType, RpcAuthValues, StartInternalBitcoindError,
28-
VERSION,
27+
InternalBitcoindNetworkConfig, RpcAuth, RpcAuthType, RpcAuthValues,
28+
StartInternalBitcoindError, VERSION,
2929
},
3030
download,
3131
hw::HardwareWallets,
@@ -598,9 +598,12 @@ impl Step for InternalBitcoindStep {
598598
return Command::none();
599599
}
600600
};
601-
// Insert entry for network if not present.
602-
if conf.networks.get(&self.network).is_none() {
603-
let network_conf = match (get_available_port(), get_available_port()) {
601+
let (rpc_port, p2p_port) = if let Some(network_conf) =
602+
conf.networks.get(&self.network)
603+
{
604+
(network_conf.rpc_port, network_conf.p2p_port)
605+
} else {
606+
match (get_available_port(), get_available_port()) {
604607
(Ok(rpc_port), Ok(p2p_port)) => {
605608
// In case ports are the same, user will need to click button again for another attempt.
606609
if rpc_port == p2p_port {
@@ -610,12 +613,7 @@ impl Step for InternalBitcoindStep {
610613
);
611614
return Command::none();
612615
}
613-
InternalBitcoindNetworkConfig {
614-
rpc_port,
615-
p2p_port,
616-
prune: PRUNE_DEFAULT,
617-
rpc_auth: None,
618-
}
616+
(rpc_port, p2p_port)
619617
}
620618
(Ok(_), Err(e)) | (Err(e), Ok(_)) => {
621619
self.error = Some(format!("Could not get available port: {}.", e));
@@ -626,17 +624,36 @@ impl Step for InternalBitcoindStep {
626624
Some(format!("Could not get available ports: {}; {}.", e1, e2));
627625
return Command::none();
628626
}
629-
};
630-
conf.networks.insert(self.network, network_conf);
631-
}
627+
}
628+
};
629+
let (rpc_auth, rpc_password) = match RpcAuth::new("liana") {
630+
Ok((rpc_auth, password)) => (rpc_auth, password),
631+
Err(e) => {
632+
self.error = Some(e.to_string());
633+
return Command::none();
634+
}
635+
};
636+
let bitcoind_config = BitcoindConfig {
637+
rpc_auth: BitcoindRpcAuth::UserPass(rpc_auth.user.clone(), rpc_password),
638+
addr: internal_bitcoind_address(rpc_port),
639+
};
640+
let network_conf = InternalBitcoindNetworkConfig {
641+
rpc_port,
642+
p2p_port,
643+
prune: PRUNE_DEFAULT,
644+
// Overwrite any previous entry for this network as we would no longer know the RPC password.
645+
rpc_auth: Some(rpc_auth),
646+
};
647+
conf.networks.insert(self.network, network_conf);
632648
if let Err(e) = conf.to_file(&bitcoind::internal_bitcoind_config_path(
633649
&self.bitcoind_datadir,
634650
)) {
635651
self.error = Some(e.to_string());
636652
return Command::none();
637653
};
638654
self.error = None;
639-
self.internal_bitcoind_config = Some(conf.clone());
655+
self.internal_bitcoind_config = Some(conf);
656+
self.bitcoind_config = Some(bitcoind_config);
640657
return Command::perform(async {}, |_| {
641658
Message::InternalBitcoind(message::InternalBitcoindMsg::Reload)
642659
});
@@ -693,51 +710,25 @@ impl Step for InternalBitcoindStep {
693710
}
694711
}
695712
message::InternalBitcoindMsg::Start => {
696-
let bitcoind_datadir = match self.bitcoind_datadir.canonicalize() {
697-
Ok(path) => path,
698-
Err(e) => {
699-
self.started = Some(Err(
700-
StartInternalBitcoindError::CouldNotCanonicalizeDataDir(
701-
e.to_string(),
702-
),
703-
));
704-
return Command::none();
705-
}
713+
if let Err(e) = self.bitcoind_datadir.canonicalize() {
714+
self.started = Some(Err(
715+
StartInternalBitcoindError::CouldNotCanonicalizeDataDir(e.to_string()),
716+
));
717+
return Command::none();
706718
};
707-
// Pass the canonicalized `bitcoind_datadir` so that the cookie path returned
708-
// by `internal_bitcoind_cookie_path` is also canonicalized. This way, the
709-
// canonicalized path will later be saved to the config file.
710-
// We cannot use `cookie_path.canonicalize()` as we have not yet started
711-
// bitcoind and so the path does not exist.
712-
let cookie_path =
713-
bitcoind::internal_bitcoind_cookie_path(&bitcoind_datadir, &self.network);
714-
715-
let rpc_port = self
716-
.internal_bitcoind_config
719+
let bitcoind_config = self
720+
.bitcoind_config
717721
.as_ref()
718-
.expect("Already added")
719-
.clone()
720-
.networks
721-
.get(&self.network)
722-
.expect("Already added")
723-
.rpc_port;
724-
725-
match Bitcoind::start(
726-
&self.network,
727-
BitcoindConfig {
728-
rpc_auth: BitcoindRpcAuth::CookieFile(cookie_path),
729-
addr: internal_bitcoind_address(rpc_port),
730-
},
731-
&self.liana_datadir,
732-
) {
722+
.expect("already added")
723+
.clone();
724+
match Bitcoind::start(&self.network, bitcoind_config, &self.liana_datadir) {
733725
Err(e) => {
734726
self.started =
735727
Some(Err(StartInternalBitcoindError::CommandError(e.to_string())));
736728
return Command::none();
737729
}
738730
Ok(bitcoind) => {
739731
self.error = None;
740-
self.bitcoind_config = Some(bitcoind.config.clone());
741732
self.started = Some(Ok(()));
742733
self.internal_bitcoind = Some(bitcoind);
743734
}

0 commit comments

Comments
 (0)