mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2024-11-05 05:46:44 +00:00
ensure cleanup of migrated wallets when importing
This commit is contained in:
parent
9b8f97c041
commit
09f6c9ef81
2 changed files with 25 additions and 6 deletions
|
@ -83,23 +83,38 @@ public class Sparrow implements WalletImport, WalletExport {
|
|||
|
||||
@Override
|
||||
public Wallet importWallet(InputStream inputStream, String password) throws ImportException {
|
||||
Storage storage = null;
|
||||
Wallet wallet = null;
|
||||
File tempFile = null;
|
||||
try {
|
||||
tempFile = File.createTempFile("sparrow", null);
|
||||
java.nio.file.Files.copy(inputStream, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
Storage storage = new Storage(PersistenceType.JSON, tempFile);
|
||||
storage = new Storage(PersistenceType.JSON, tempFile);
|
||||
if(!isEncrypted(tempFile)) {
|
||||
return storage.loadUnencryptedWallet().getWallet();
|
||||
wallet = storage.loadUnencryptedWallet().getWallet();
|
||||
} else {
|
||||
WalletBackupAndKey walletBackupAndKey = storage.loadEncryptedWallet(password);
|
||||
walletBackupAndKey.getWallet().decrypt(walletBackupAndKey.getKey());
|
||||
return walletBackupAndKey.getWallet();
|
||||
wallet = walletBackupAndKey.getWallet();
|
||||
wallet.decrypt(walletBackupAndKey.getKey());
|
||||
}
|
||||
|
||||
return wallet;
|
||||
} catch(IOException | StorageException e) {
|
||||
log.error("Error importing Sparrow wallet", e);
|
||||
throw new ImportException("Error importing Sparrow wallet", e);
|
||||
} finally {
|
||||
if(storage != null) {
|
||||
storage.close();
|
||||
}
|
||||
|
||||
if(tempFile != null) {
|
||||
if(wallet != null) {
|
||||
File migratedWalletFile = Storage.getExistingWallet(tempFile.getParentFile(), wallet.getName());
|
||||
if(migratedWalletFile != null) {
|
||||
migratedWalletFile.delete();
|
||||
}
|
||||
}
|
||||
|
||||
tempFile.delete();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -289,13 +289,17 @@ public class Storage {
|
|||
}
|
||||
|
||||
public static File getExistingWallet(String walletName) {
|
||||
File encrypted = new File(getWalletsDir(), walletName.trim());
|
||||
return getExistingWallet(getWalletsDir(), walletName);
|
||||
}
|
||||
|
||||
public static File getExistingWallet(File walletsDir, String walletName) {
|
||||
File encrypted = new File(walletsDir, walletName.trim());
|
||||
if(encrypted.exists()) {
|
||||
return encrypted;
|
||||
}
|
||||
|
||||
for(PersistenceType persistenceType : PersistenceType.values()) {
|
||||
File unencrypted = new File(getWalletsDir(), walletName.trim() + "." + persistenceType.getExtension());
|
||||
File unencrypted = new File(walletsDir, walletName.trim() + "." + persistenceType.getExtension());
|
||||
if(unencrypted.exists()) {
|
||||
return unencrypted;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue