KeyChainGroup: Fix NullPointerExceptions in encrypt() for the case of only a basic keychain.

This commit is contained in:
Matthew Leon
2019-03-27 23:13:42 -04:00
committed by Andreas Schildbach
parent 12f1670eea
commit 26b7d8728f
2 changed files with 17 additions and 5 deletions

View File

@@ -667,17 +667,19 @@ public class KeyChainGroup implements KeyBag {
public void encrypt(KeyCrypter keyCrypter, KeyParameter aesKey) {
checkNotNull(keyCrypter);
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.
BasicKeyChain newBasic = basic.toEncrypted(keyCrypter, aesKey);
this.basic = newBasic;
List<DeterministicKeyChain> newChains = new ArrayList<>();
if (chains != null)
if (chains != null) {
for (DeterministicKeyChain chain : chains)
newChains.add(chain.toEncrypted(keyCrypter, aesKey));
this.chains.clear();
this.chains.addAll(newChains);
}
this.keyCrypter = keyCrypter;
basic = newBasic;
this.chains.clear();
this.chains.addAll(newChains);
}
/**

View File

@@ -713,4 +713,14 @@ public class KeyChainGroupTest {
group.currentAddress(KeyPurpose.RECEIVE_FUNDS).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);
}
}