@@ -24,8 +24,8 @@ use crate::{
24
24
bitcoind:: {
25
25
self , bitcoind_network_dir, internal_bitcoind_datadir, internal_bitcoind_directory,
26
26
Bitcoind , ConfigField , InternalBitcoindConfig , InternalBitcoindConfigError ,
27
- InternalBitcoindNetworkConfig , RpcAuthType , RpcAuthValues , StartInternalBitcoindError ,
28
- VERSION ,
27
+ InternalBitcoindNetworkConfig , RpcAuth , RpcAuthType , RpcAuthValues ,
28
+ StartInternalBitcoindError , VERSION ,
29
29
} ,
30
30
download,
31
31
hw:: HardwareWallets ,
@@ -598,9 +598,12 @@ impl Step for InternalBitcoindStep {
598
598
return Command :: none ( ) ;
599
599
}
600
600
} ;
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 ( ) ) {
604
607
( Ok ( rpc_port) , Ok ( p2p_port) ) => {
605
608
// In case ports are the same, user will need to click button again for another attempt.
606
609
if rpc_port == p2p_port {
@@ -610,12 +613,7 @@ impl Step for InternalBitcoindStep {
610
613
) ;
611
614
return Command :: none ( ) ;
612
615
}
613
- InternalBitcoindNetworkConfig {
614
- rpc_port,
615
- p2p_port,
616
- prune : PRUNE_DEFAULT ,
617
- rpc_auth : None ,
618
- }
616
+ ( rpc_port, p2p_port)
619
617
}
620
618
( Ok ( _) , Err ( e) ) | ( Err ( e) , Ok ( _) ) => {
621
619
self . error = Some ( format ! ( "Could not get available port: {}." , e) ) ;
@@ -626,17 +624,36 @@ impl Step for InternalBitcoindStep {
626
624
Some ( format ! ( "Could not get available ports: {}; {}." , e1, e2) ) ;
627
625
return Command :: none ( ) ;
628
626
}
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) ;
632
648
if let Err ( e) = conf. to_file ( & bitcoind:: internal_bitcoind_config_path (
633
649
& self . bitcoind_datadir ,
634
650
) ) {
635
651
self . error = Some ( e. to_string ( ) ) ;
636
652
return Command :: none ( ) ;
637
653
} ;
638
654
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) ;
640
657
return Command :: perform ( async { } , |_| {
641
658
Message :: InternalBitcoind ( message:: InternalBitcoindMsg :: Reload )
642
659
} ) ;
@@ -693,51 +710,25 @@ impl Step for InternalBitcoindStep {
693
710
}
694
711
}
695
712
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 ( ) ;
706
718
} ;
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
717
721
. 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 ) {
733
725
Err ( e) => {
734
726
self . started =
735
727
Some ( Err ( StartInternalBitcoindError :: CommandError ( e. to_string ( ) ) ) ) ;
736
728
return Command :: none ( ) ;
737
729
}
738
730
Ok ( bitcoind) => {
739
731
self . error = None ;
740
- self . bitcoind_config = Some ( bitcoind. config . clone ( ) ) ;
741
732
self . started = Some ( Ok ( ( ) ) ) ;
742
733
self . internal_bitcoind = Some ( bitcoind) ;
743
734
}
0 commit comments