From 3a885b3a28fb166bbf12911b6ddaf160a3da5fd5 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Mon, 5 Oct 2020 12:33:05 +0200 Subject: [PATCH] improve open wallet error dialog message --- .../com/sparrowwallet/sparrow/AppController.java | 4 ++-- .../java/com/sparrowwallet/sparrow/io/Storage.java | 14 +++++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index f6c7ce6f..6a1f8336 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -809,7 +809,7 @@ public class AppController implements Initializable { } else { if(!attemptImportWallet(file, password)) { log.error("Error Opening Wallet", exception); - showErrorDialog("Error Opening Wallet", exception.getMessage()); + showErrorDialog("Error Opening Wallet", exception.getMessage() == null ? "Unsupported file format" : exception.getMessage()); } password.clear(); } @@ -822,7 +822,7 @@ public class AppController implements Initializable { } catch(Exception e) { if(!attemptImportWallet(file, null)) { log.error("Error opening wallet", e); - showErrorDialog("Error Opening Wallet", e.getMessage()); + showErrorDialog("Error Opening Wallet", e.getMessage() == null ? "Unsupported file format" : e.getMessage()); } } } diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java index 939a34dd..182c91b0 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java @@ -222,12 +222,16 @@ public class Storage { if(read != BINARY_HEADER_LENGTH) { throw new StorageException("Not a Sparrow wallet - invalid header"); } - byte[] decodedHeader = Base64.getDecoder().decode(header); - byte[] magic = Arrays.copyOfRange(decodedHeader, 0, HEADER_MAGIC_1.length()); - if(!HEADER_MAGIC_1.equals(new String(magic, StandardCharsets.UTF_8))) { - throw new StorageException("Not a Sparrow wallet - invalid magic"); + try { + byte[] decodedHeader = Base64.getDecoder().decode(header); + byte[] magic = Arrays.copyOfRange(decodedHeader, 0, HEADER_MAGIC_1.length()); + if(!HEADER_MAGIC_1.equals(new String(magic, StandardCharsets.UTF_8))) { + throw new StorageException("Not a Sparrow wallet - invalid magic"); + } + salt = Arrays.copyOfRange(decodedHeader, HEADER_MAGIC_1.length(), decodedHeader.length); + } catch(IllegalArgumentException e) { + throw new StorageException("Not a Sparrow wallet - invalid header"); } - salt = Arrays.copyOfRange(decodedHeader, HEADER_MAGIC_1.length(), decodedHeader.length); } else { SecureRandom secureRandom = new SecureRandom(); secureRandom.nextBytes(salt);