private void checkEncryptedKeyChain(SimpleHDKeyChain encChain, DeterministicKey key1) { // Check we can look keys up and extend the chain without the AES key being provided. DeterministicKey encKey1 = encChain.findKeyFromPubKey(key1.getPubKey()); DeterministicKey encKey2 = encChain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS); assertFalse(key1.isEncrypted()); assertTrue(encKey1.isEncrypted()); assertEquals(encKey1.getPubKeyPoint(), key1.getPubKeyPoint()); final KeyParameter aesKey = checkNotNull(encChain.getKeyCrypter()).deriveKey("open secret"); encKey1.sign(Sha256Hash.ZERO_HASH, aesKey); encKey2.sign(Sha256Hash.ZERO_HASH, aesKey); assertTrue(encChain.checkAESKey(aesKey)); assertFalse(encChain.checkPassword("access denied")); assertTrue(encChain.checkPassword("open secret")); }
public void serializeUnencrypted(SimpleHDKeyChain keyChain, String expectedSerialization) throws UnreadableWalletException { keyChain.setLookaheadSize(10); keyChain.maybeLookAhead(); DeterministicKey key1 = keyChain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS); DeterministicKey key2 = keyChain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS); DeterministicKey key3 = keyChain.getKey(KeyChain.KeyPurpose.CHANGE); List<Protos.Key> keys = keyChain.toProtobuf(); // 1 master key, 1 account key, 2 internal keys, 3 derived, 20 lookahead and 5 lookahead // threshold. int numItems = 1 // master key/account key + 2 // ext/int parent keys + (keyChain.getLookaheadSize() + keyChain.getLookaheadThreshold()) * 2 // lookahead zone on each chain ; assertEquals(numItems, keys.size()); // Get another key that will be lost during round-tripping, to ensure we can derive it again. DeterministicKey key4 = keyChain.getKey(KeyChain.KeyPurpose.CHANGE); String sb = protoToString(keys); assertEquals(expectedSerialization, sb); // Round trip the data back and forth to check it is preserved. int oldLookaheadSize = keyChain.getLookaheadSize(); keyChain = SimpleHDKeyChain.fromProtobuf(keys, null); assertEquals(expectedSerialization, protoToString(keyChain.toProtobuf())); assertEquals(key1, keyChain.findKeyFromPubHash(key1.getPubKeyHash())); assertEquals(key2, keyChain.findKeyFromPubHash(key2.getPubKeyHash())); assertEquals(key3, keyChain.findKeyFromPubHash(key3.getPubKeyHash())); assertEquals(key4, keyChain.getKey(KeyChain.KeyPurpose.CHANGE)); key1.sign(Sha256Hash.ZERO_HASH); key2.sign(Sha256Hash.ZERO_HASH); key3.sign(Sha256Hash.ZERO_HASH); key4.sign(Sha256Hash.ZERO_HASH); assertEquals(oldLookaheadSize, keyChain.getLookaheadSize()); }