mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-07-30 19:41:24 +00:00
KeyChainGroup: Fix NullPointerExceptions in encrypt() for the case of only a basic keychain.
This commit is contained in:
committed by
Andreas Schildbach
parent
12f1670eea
commit
26b7d8728f
@@ -667,17 +667,19 @@ public class KeyChainGroup implements KeyBag {
|
|||||||
public void encrypt(KeyCrypter keyCrypter, KeyParameter aesKey) {
|
public void encrypt(KeyCrypter keyCrypter, KeyParameter aesKey) {
|
||||||
checkNotNull(keyCrypter);
|
checkNotNull(keyCrypter);
|
||||||
checkNotNull(aesKey);
|
checkNotNull(aesKey);
|
||||||
checkState(chains == null || !chains.isEmpty() || basic.numKeys() != 0, "can't encrypt entirely empty wallet");
|
checkState((chains != null && !chains.isEmpty()) || basic.numKeys() != 0, "can't encrypt entirely empty wallet");
|
||||||
// This code must be exception safe.
|
// This code must be exception safe.
|
||||||
|
|
||||||
BasicKeyChain newBasic = basic.toEncrypted(keyCrypter, aesKey);
|
BasicKeyChain newBasic = basic.toEncrypted(keyCrypter, aesKey);
|
||||||
|
this.basic = newBasic;
|
||||||
List<DeterministicKeyChain> newChains = new ArrayList<>();
|
List<DeterministicKeyChain> newChains = new ArrayList<>();
|
||||||
if (chains != null)
|
if (chains != null) {
|
||||||
for (DeterministicKeyChain chain : chains)
|
for (DeterministicKeyChain chain : chains)
|
||||||
newChains.add(chain.toEncrypted(keyCrypter, aesKey));
|
newChains.add(chain.toEncrypted(keyCrypter, aesKey));
|
||||||
|
this.chains.clear();
|
||||||
|
this.chains.addAll(newChains);
|
||||||
|
}
|
||||||
this.keyCrypter = keyCrypter;
|
this.keyCrypter = keyCrypter;
|
||||||
basic = newBasic;
|
|
||||||
this.chains.clear();
|
|
||||||
this.chains.addAll(newChains);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -713,4 +713,14 @@ public class KeyChainGroupTest {
|
|||||||
group.currentAddress(KeyPurpose.RECEIVE_FUNDS).toString());
|
group.currentAddress(KeyPurpose.RECEIVE_FUNDS).toString());
|
||||||
assertEquals("bc1qw8sf3mwuwn74qnhj83gjg0cwkk78fun2pxl9t2", group.currentAddress(KeyPurpose.CHANGE).toString());
|
assertEquals("bc1qw8sf3mwuwn74qnhj83gjg0cwkk78fun2pxl9t2", group.currentAddress(KeyPurpose.CHANGE).toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onlyBasicKeyEncryption() {
|
||||||
|
group = KeyChainGroup.createBasic(MAINNET);
|
||||||
|
final ECKey key = ECKey.fromPrivate(BigInteger.TEN);
|
||||||
|
group.importKeys(key);
|
||||||
|
KeyCrypterScrypt scrypt = new KeyCrypterScrypt(2);
|
||||||
|
KeyParameter aesKey = scrypt.deriveKey("password");
|
||||||
|
group.encrypt(scrypt, aesKey);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user