diff --git a/core/src/main/java/com/google/bitcoin/core/Wallet.java b/core/src/main/java/com/google/bitcoin/core/Wallet.java index ff96f6fd..c166b8ed 100644 --- a/core/src/main/java/com/google/bitcoin/core/Wallet.java +++ b/core/src/main/java/com/google/bitcoin/core/Wallet.java @@ -2770,6 +2770,23 @@ public class Wallet implements Serializable, BlockChainListener { } } + /** + * Sets the wallet's KeyCrypter. + * Note that this does not encrypt the wallet, and should only be used if the keyCrypter can not be included in the + * constructor during initial wallet loading. + * Note that if the keyCrypter was not properly set during wallet load, {@link Wallet#getEncryptionType()} and + * {@link Wallet#isEncrypted()} will not return the correct results. + */ + public void setKeyCrypter(KeyCrypter keyCrypter) { + lock.lock(); + try { + checkState(this.keyCrypter == null); + this.keyCrypter = keyCrypter; + } finally { + lock.unlock(); + } + } + /** * Get the type of encryption used for this wallet. * @@ -2976,7 +2993,7 @@ public class Wallet implements Serializable, BlockChainListener { if (extensions.containsKey(id)) throw new IllegalStateException("Cannot add two extensions with the same ID: " + id); extensions.put(id, extension); - invokeOnWalletChanged(); + queueAutoSave(); } finally { lock.unlock(); } @@ -2993,7 +3010,7 @@ public class Wallet implements Serializable, BlockChainListener { if (previousExtension != null) return previousExtension; extensions.put(id, extension); - invokeOnWalletChanged(); + queueAutoSave(); return extension; } finally { lock.unlock(); @@ -3002,15 +3019,15 @@ public class Wallet implements Serializable, BlockChainListener { /** * Either adds extension as a new extension or replaces the existing extension if one already exists with the same - * id. This also calls onWalletChanged, triggering wallet saving, so may be useful even when called with the same - * extension as is already present. + * id. This also triggers wallet auto-saving, so may be useful even when called with the same extension as is + * already present. */ public void addOrUpdateExtension(WalletExtension extension) { String id = checkNotNull(extension).getWalletExtensionID(); lock.lock(); try { extensions.put(id, extension); - invokeOnWalletChanged(); + queueAutoSave(); } finally { lock.unlock(); } diff --git a/core/src/main/java/com/google/bitcoin/store/WalletProtobufSerializer.java b/core/src/main/java/com/google/bitcoin/store/WalletProtobufSerializer.java index 461c4554..55ce20fe 100644 --- a/core/src/main/java/com/google/bitcoin/store/WalletProtobufSerializer.java +++ b/core/src/main/java/com/google/bitcoin/store/WalletProtobufSerializer.java @@ -306,16 +306,8 @@ public class WalletProtobufSerializer { // System.out.println(TextFormat.printToString(walletProto)); - // Read the scrypt parameters that specify how encryption and decryption is performed. - // TODO: Why is the key crypter special? This should just be added to the wallet after construction as well. - KeyCrypter keyCrypter = null; - if (walletProto.hasEncryptionParameters()) { - Protos.ScryptParameters encryptionParameters = walletProto.getEncryptionParameters(); - keyCrypter = new KeyCrypterScrypt(encryptionParameters); - } - NetworkParameters params = NetworkParameters.fromID(walletProto.getNetworkIdentifier()); - Wallet wallet = new Wallet(params, keyCrypter); + Wallet wallet = new Wallet(params); readWallet(walletProto, wallet); return wallet; } @@ -330,6 +322,12 @@ public class WalletProtobufSerializer { */ public void readWallet(Protos.Wallet walletProto, Wallet wallet) throws IOException { // TODO: This method should throw more specific exception types than IllegalArgumentException. + // Read the scrypt parameters that specify how encryption and decryption is performed. + if (walletProto.hasEncryptionParameters()) { + Protos.ScryptParameters encryptionParameters = walletProto.getEncryptionParameters(); + wallet.setKeyCrypter(new KeyCrypterScrypt(encryptionParameters)); + } + if (walletProto.hasDescription()) { wallet.setDescription(walletProto.getDescription()); }