Skip to content

Commit 76774a6

Browse files
committed
Fix Trezor device selection in wallet-cli
1 parent 3bccead commit 76774a6

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

  • wallet

wallet/wallet-cli-commands/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ impl CreateWalletDeviceSelectMenu {
10461046

10471047
impl ChoiceMenu for CreateWalletDeviceSelectMenu {
10481048
fn header(&self) -> &str {
1049-
"Please chose one of the available Trezor devices:"
1049+
"Please choose one of the available Trezor devices:"
10501050
}
10511051

10521052
fn choice_list(&self) -> Vec<String> {
@@ -1103,7 +1103,7 @@ impl OpenWalletDeviceSelectMenu {
11031103

11041104
impl ChoiceMenu for OpenWalletDeviceSelectMenu {
11051105
fn header(&self) -> &str {
1106-
"Please chose one of the available Trezor devices:"
1106+
"Please choose one of the available Trezor devices:"
11071107
}
11081108

11091109
fn choice_list(&self) -> Vec<String> {

wallet/wallet-controller/src/lib.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,7 @@ where
317317
.map(|w| w.map_wallet(RuntimeWallet::Trezor)),
318318
};
319319

320-
if res.is_err() {
321-
let _ = fs::remove_file(file_path);
322-
}
323-
320+
Self::delete_wallet_file_on_wallet_creation_failure(&res, file_path);
324321
res
325322
}
326323

@@ -338,10 +335,10 @@ where
338335
)
339336
);
340337

341-
let db = wallet::wallet::open_or_create_wallet_file(file_path)
338+
let db = wallet::wallet::open_or_create_wallet_file(file_path.as_ref())
342339
.map_err(ControllerError::WalletError)?;
343340

344-
match args {
341+
let res = match args {
345342
WalletTypeArgsComputed::Software {
346343
mnemonic,
347344
passphrase,
@@ -382,6 +379,31 @@ where
382379
.map_err(ControllerError::WalletError)?;
383380
Ok(wallet.map_wallet(RuntimeWallet::Trezor))
384381
}
382+
};
383+
384+
Self::delete_wallet_file_on_wallet_creation_failure(&res, file_path);
385+
res
386+
}
387+
388+
/// If wallet creation/recovery didn't succeed (e.g. due to a hard error, or because
389+
/// user intervention is required), we must delete the wallet file.
390+
fn delete_wallet_file_on_wallet_creation_failure(
391+
result: &Result<WalletCreation<RuntimeWallet<DefaultBackend>>, ControllerError<N>>,
392+
file_path: impl AsRef<Path>,
393+
) {
394+
let must_remove_wallet_file = match result {
395+
Err(_) => true,
396+
Ok(wallet_creation) => match wallet_creation {
397+
// Wallet was created successfully.
398+
WalletCreation::Wallet(_) => false,
399+
// Wallet was not created successfully. The caller will need to handle this result
400+
// and either fail or try again.
401+
WalletCreation::MultipleAvailableTrezorDevices(_) => true,
402+
},
403+
};
404+
405+
if must_remove_wallet_file {
406+
let _ = fs::remove_file(file_path);
385407
}
386408
}
387409

0 commit comments

Comments
 (0)