3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-31 23:32:16 +00:00

Fix wallet extensions by adding setKeyCrypter and using auto-save.

This commit is contained in:
Matt Corallo 2013-06-11 22:49:59 +02:00 committed by Mike Hearn
parent e2e72a275d
commit 1e24dea569
2 changed files with 29 additions and 14 deletions

View File

@ -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();
}

View File

@ -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());
}