From f407547c4766fd81cea29768e275c250b06cf968 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Mon, 14 Jun 2021 14:53:53 +0200 Subject: [PATCH] preserve id when encrypting or decrypting seeds or private keys --- .../drongo/wallet/DeterministicSeed.java | 3 +++ .../drongo/wallet/MasterPrivateExtendedKey.java | 14 ++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/DeterministicSeed.java b/src/main/java/com/sparrowwallet/drongo/wallet/DeterministicSeed.java index 6005f95..841d905 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/DeterministicSeed.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/DeterministicSeed.java @@ -184,6 +184,7 @@ public class DeterministicSeed extends Persistable implements EncryptableItem { Arrays.fill(mnemonicBytes != null ? mnemonicBytes : new byte[0], (byte)0); DeterministicSeed seed = new DeterministicSeed(encryptedMnemonic, needsPassphrase, creationTimeSeconds, type); + seed.setId(getId()); seed.setPassphrase(passphrase); return seed; @@ -209,6 +210,7 @@ public class DeterministicSeed extends Persistable implements EncryptableItem { KeyDeriver keyDeriver = getEncryptionType().getDeriver().getKeyDeriver(encryptedMnemonicCode.getKeySalt()); Key key = keyDeriver.deriveKey(password); DeterministicSeed seed = decrypt(key); + seed.setId(getId()); key.clear(); return seed; @@ -225,6 +227,7 @@ public class DeterministicSeed extends Persistable implements EncryptableItem { Arrays.fill(decrypted, (byte)0); DeterministicSeed seed = new DeterministicSeed(mnemonic, needsPassphrase, creationTimeSeconds, type); + seed.setId(getId()); seed.setPassphrase(passphrase); return seed; diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/MasterPrivateExtendedKey.java b/src/main/java/com/sparrowwallet/drongo/wallet/MasterPrivateExtendedKey.java index e36d08c..4f5520a 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/MasterPrivateExtendedKey.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/MasterPrivateExtendedKey.java @@ -81,7 +81,10 @@ public class MasterPrivateExtendedKey extends Persistable implements Encryptable EncryptedData encryptedKeyData = keyCrypter.encrypt(secretBytes, null, key); Arrays.fill(secretBytes != null ? secretBytes : new byte[0], (byte)0); - return new MasterPrivateExtendedKey(encryptedKeyData); + MasterPrivateExtendedKey mpek = new MasterPrivateExtendedKey(encryptedKeyData); + mpek.setId(getId()); + + return mpek; } public MasterPrivateExtendedKey decrypt(CharSequence password) { @@ -91,10 +94,11 @@ public class MasterPrivateExtendedKey extends Persistable implements Encryptable KeyDeriver keyDeriver = getEncryptionType().getDeriver().getKeyDeriver(encryptedKey.getKeySalt()); Key key = keyDeriver.deriveKey(password); - MasterPrivateExtendedKey seed = decrypt(key); + MasterPrivateExtendedKey mpek = decrypt(key); + mpek.setId(getId()); key.clear(); - return seed; + return mpek; } public MasterPrivateExtendedKey decrypt(Key key) { @@ -105,7 +109,9 @@ public class MasterPrivateExtendedKey extends Persistable implements Encryptable KeyCrypter keyCrypter = getEncryptionType().getCrypter().getKeyCrypter(); byte[] decrypted = keyCrypter.decrypt(encryptedKey, key); try { - return new MasterPrivateExtendedKey(Arrays.copyOfRange(decrypted, 0, 32), Arrays.copyOfRange(decrypted, 32, 64)); + MasterPrivateExtendedKey mpek = new MasterPrivateExtendedKey(Arrays.copyOfRange(decrypted, 0, 32), Arrays.copyOfRange(decrypted, 32, 64)); + mpek.setId(getId()); + return mpek; } finally { Arrays.fill(decrypted, (byte)0); }