@Test public void findKey() throws Exception { ECKey a = group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS); ECKey b = group.freshKey(KeyChain.KeyPurpose.CHANGE); ECKey c = new ECKey(); ECKey d = new ECKey(); // Not imported. group.importKeys(c); assertTrue(group.hasKey(a)); assertTrue(group.hasKey(b)); assertTrue(group.hasKey(c)); assertFalse(group.hasKey(d)); ECKey result = group.findKeyFromPubKey(a.getPubKey()); assertEquals(a, result); result = group.findKeyFromPubKey(b.getPubKey()); assertEquals(b, result); result = group.findKeyFromPubHash(a.getPubKeyHash()); assertEquals(a, result); result = group.findKeyFromPubHash(b.getPubKeyHash()); assertEquals(b, result); result = group.findKeyFromPubKey(c.getPubKey()); assertEquals(c, result); result = group.findKeyFromPubHash(c.getPubKeyHash()); assertEquals(c, result); assertNull(group.findKeyFromPubKey(d.getPubKey())); assertNull(group.findKeyFromPubHash(d.getPubKeyHash())); }
@Test public void serialization() throws Exception { assertEquals(INITIAL_KEYS + 1 /* for the seed */, group.serializeToProtobuf().size()); group = KeyChainGroup.fromProtobufUnencrypted(params, group.serializeToProtobuf()); group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS); DeterministicKey key1 = group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS); DeterministicKey key2 = group.freshKey(KeyChain.KeyPurpose.CHANGE); group.getBloomFilterElementCount(); List<Protos.Key> protoKeys1 = group.serializeToProtobuf(); assertEquals( INITIAL_KEYS + ((LOOKAHEAD_SIZE + 1) * 2) + 1 /* for the seed */ + 1, protoKeys1.size()); group.importKeys(new ECKey()); List<Protos.Key> protoKeys2 = group.serializeToProtobuf(); assertEquals( INITIAL_KEYS + ((LOOKAHEAD_SIZE + 1) * 2) + 1 /* for the seed */ + 2, protoKeys2.size()); group = KeyChainGroup.fromProtobufUnencrypted(params, protoKeys1); assertEquals( INITIAL_KEYS + ((LOOKAHEAD_SIZE + 1) * 2) + 1 /* for the seed */ + 1, protoKeys1.size()); assertTrue(group.hasKey(key1)); assertTrue(group.hasKey(key2)); assertEquals(key2, group.currentKey(KeyChain.KeyPurpose.CHANGE)); assertEquals(key1, group.currentKey(KeyChain.KeyPurpose.RECEIVE_FUNDS)); group = KeyChainGroup.fromProtobufUnencrypted(params, protoKeys2); assertEquals( INITIAL_KEYS + ((LOOKAHEAD_SIZE + 1) * 2) + 1 /* for the seed */ + 2, protoKeys2.size()); assertTrue(group.hasKey(key1)); assertTrue(group.hasKey(key2)); KeyCrypterScrypt scrypt = new KeyCrypterScrypt(2); final KeyParameter aesKey = scrypt.deriveKey("password"); group.encrypt(scrypt, aesKey); List<Protos.Key> protoKeys3 = group.serializeToProtobuf(); group = KeyChainGroup.fromProtobufEncrypted(params, protoKeys3, scrypt); assertTrue(group.isEncrypted()); assertTrue(group.checkPassword("password")); group.decrypt(aesKey); // No need for extensive contents testing here, as that's done in the keychain class tests. }