mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2024-12-25 05:06:45 +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
|
@Override
|
||||||
public Wallet importWallet(InputStream inputStream, String password) throws ImportException {
|
public Wallet importWallet(InputStream inputStream, String password) throws ImportException {
|
||||||
|
Storage storage = null;
|
||||||
|
Wallet wallet = null;
|
||||||
File tempFile = null;
|
File tempFile = null;
|
||||||
try {
|
try {
|
||||||
tempFile = File.createTempFile("sparrow", null);
|
tempFile = File.createTempFile("sparrow", null);
|
||||||
java.nio.file.Files.copy(inputStream, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
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)) {
|
if(!isEncrypted(tempFile)) {
|
||||||
return storage.loadUnencryptedWallet().getWallet();
|
wallet = storage.loadUnencryptedWallet().getWallet();
|
||||||
} else {
|
} else {
|
||||||
WalletBackupAndKey walletBackupAndKey = storage.loadEncryptedWallet(password);
|
WalletBackupAndKey walletBackupAndKey = storage.loadEncryptedWallet(password);
|
||||||
walletBackupAndKey.getWallet().decrypt(walletBackupAndKey.getKey());
|
wallet = walletBackupAndKey.getWallet();
|
||||||
return walletBackupAndKey.getWallet();
|
wallet.decrypt(walletBackupAndKey.getKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return wallet;
|
||||||
} catch(IOException | StorageException e) {
|
} catch(IOException | StorageException e) {
|
||||||
log.error("Error importing Sparrow wallet", e);
|
log.error("Error importing Sparrow wallet", e);
|
||||||
throw new ImportException("Error importing Sparrow wallet", e);
|
throw new ImportException("Error importing Sparrow wallet", e);
|
||||||
} finally {
|
} finally {
|
||||||
|
if(storage != null) {
|
||||||
|
storage.close();
|
||||||
|
}
|
||||||
|
|
||||||
if(tempFile != null) {
|
if(tempFile != null) {
|
||||||
|
if(wallet != null) {
|
||||||
|
File migratedWalletFile = Storage.getExistingWallet(tempFile.getParentFile(), wallet.getName());
|
||||||
|
if(migratedWalletFile != null) {
|
||||||
|
migratedWalletFile.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
tempFile.delete();
|
tempFile.delete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -289,13 +289,17 @@ public class Storage {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static File getExistingWallet(String walletName) {
|
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()) {
|
if(encrypted.exists()) {
|
||||||
return encrypted;
|
return encrypted;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(PersistenceType persistenceType : PersistenceType.values()) {
|
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()) {
|
if(unencrypted.exists()) {
|
||||||
return unencrypted;
|
return unencrypted;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue