1
1
use std:: {
2
2
collections:: { BTreeSet , HashMap } ,
3
+ fmt:: Display ,
3
4
fs:: { self , File } ,
4
5
io:: { Read , Write } ,
5
6
path:: PathBuf ,
@@ -11,6 +12,7 @@ use std::{
11
12
time,
12
13
} ;
13
14
15
+ use async_hwi:: bitbox:: api:: btc:: Fingerprint ;
14
16
use chrono:: { DateTime , Duration , Utc } ;
15
17
use liana:: {
16
18
descriptors:: LianaDescriptor ,
@@ -32,7 +34,7 @@ use crate::{
32
34
settings:: { KeySetting , Settings } ,
33
35
view,
34
36
} ,
35
- backup:: Backup ,
37
+ backup:: { self , Backup } ,
36
38
daemon:: {
37
39
model:: { HistoryTransaction , Labelled } ,
38
40
Daemon , DaemonBackend , DaemonError ,
@@ -103,6 +105,7 @@ pub enum ImportExportMessage {
103
105
Close ,
104
106
Overwrite ,
105
107
Ignore ,
108
+ UpdateAliases ( HashMap < Fingerprint , String > ) ,
106
109
}
107
110
108
111
impl From < ImportExportMessage > for view:: Message {
@@ -141,6 +144,30 @@ pub enum Error {
141
144
BackupImport ( String ) ,
142
145
LabelsConflict ( SyncSender < bool > ) ,
143
146
KeyAliasesConflict ( SyncSender < bool > ) ,
147
+ Backup ( backup:: Error ) ,
148
+ }
149
+
150
+ impl Display for Error {
151
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
152
+ match self {
153
+ Error :: Io ( e) => write ! ( f, "ImportExport Io Error: {e}" ) ,
154
+ Error :: HandleLost => write ! ( f, "ImportExport: subprocess handle lost" ) ,
155
+ Error :: UnexpectedEnd => write ! ( f, "ImportExport: unexpected end of the process" ) ,
156
+ Error :: JoinError ( e) => write ! ( f, "ImportExport fail to handle.join(): {e} " ) ,
157
+ Error :: ChannelLost => write ! ( f, "ImportExport: the channel have been closed" ) ,
158
+ Error :: NoParentDir => write ! ( f, "ImportExport: there is no parent dir" ) ,
159
+ Error :: Daemon ( e) => write ! ( f, "ImportExport daemon error: {e}" ) ,
160
+ Error :: TxTimeMissing => write ! ( f, "ImportExport: transaction block height missing" ) ,
161
+ Error :: DaemonMissing => write ! ( f, "ImportExport: the daemon is missing" ) ,
162
+ Error :: ParsePsbt => write ! ( f, "ImportExport: fail to parse PSBT" ) ,
163
+ Error :: ParseDescriptor => write ! ( f, "ImportExport: fail to parse descriptor" ) ,
164
+ Error :: Bip329Export ( e) => write ! ( f, "Bip329Export: {e}" ) ,
165
+ Error :: BackupImport ( e) => write ! ( f, "BackupImport: {e}" ) ,
166
+ Error :: Backup ( e) => write ! ( f, "Backup: {e}" ) ,
167
+ Error :: LabelsConflict ( _) => write ! ( f, " " ) ,
168
+ Error :: KeyAliasesConflict ( _) => write ! ( f, " " ) ,
169
+ }
170
+ }
144
171
}
145
172
146
173
#[ derive( Debug , Clone ) ]
@@ -213,6 +240,7 @@ pub enum Progress {
213
240
None ,
214
241
Psbt ( Psbt ) ,
215
242
Descriptor ( LianaDescriptor ) ,
243
+ UpdateAliases ( HashMap < Fingerprint , String > ) ,
216
244
}
217
245
218
246
pub struct Export {
@@ -698,18 +726,6 @@ pub async fn import_backup(
698
726
return ;
699
727
}
700
728
701
- // let labels = account.labels.as_ref().map(|l| l.clone().into_vec());
702
- // let txs = &account.transactions;
703
- // let psbts = &account.psbts;
704
- //
705
- // let mut count = 0usize;
706
- //
707
- // count += txs.len();
708
- // count += psbts.len();
709
- // if let Some(labels) = &labels {
710
- // count += labels.len() * 2;
711
- // }
712
-
713
729
// TODO: check if timestamp matches?
714
730
715
731
// check if labels can be imported w/o conflict only if `overwrite_label` == false
@@ -928,6 +944,7 @@ pub async fn import_backup(
928
944
}
929
945
930
946
settings. wallets . get_mut ( 0 ) . expect ( "already checked" ) . keys = settings_aliases
947
+ . clone ( )
931
948
. into_iter ( )
932
949
. map ( |( k, v) | KeySetting {
933
950
name : v,
@@ -940,11 +957,12 @@ pub async fn import_backup(
940
957
Error :: BackupImport ( "Fail to import keys aliases" . into( ) )
941
958
) ;
942
959
return ;
960
+ } else {
961
+ // Update wallet state
962
+ send_progress ! ( sender, UpdateAliases ( settings_aliases) ) ;
943
963
}
944
964
}
945
965
946
- // TODO: update local wallet state after run
947
-
948
966
send_progress ! ( sender, Progress ( 100.0 ) ) ;
949
967
send_progress ! ( sender, Ended ) ;
950
968
}
0 commit comments