Пример #1
0
 @Test
 public void encryptionWhilstEmpty() throws Exception {
   group = new KeyChainGroup(params);
   group.setLookaheadSize(5);
   KeyCrypterScrypt scrypt = new KeyCrypterScrypt(2);
   final KeyParameter aesKey = scrypt.deriveKey("password");
   group.encrypt(scrypt, aesKey);
   assertTrue(group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS).isEncrypted());
   final ECKey key = group.currentKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
   group.decrypt(aesKey);
   assertFalse(checkNotNull(group.findKeyFromPubKey(key.getPubKey())).isEncrypted());
 }
Пример #2
0
  @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.
  }
Пример #3
0
  @Test
  public void freshCurrentKeysForMarriedKeychain() throws Exception {
    group = createMarriedKeyChainGroup();

    try {
      group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
      fail();
    } catch (UnsupportedOperationException e) {
    }

    try {
      group.currentKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
      fail();
    } catch (UnsupportedOperationException e) {
    }
  }
Пример #4
0
  @Test
  public void freshCurrentKeys() throws Exception {
    int numKeys =
        ((group.getLookaheadSize() + group.getLookaheadThreshold())
                * 2) // * 2 because of internal/external
            + 1 // keys issued
            + 3 /* account key + int/ext parent keys */;
    assertEquals(numKeys, group.numKeys());
    assertEquals(2 * numKeys, group.getBloomFilterElementCount());
    ECKey r1 = group.currentKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    assertEquals(numKeys, group.numKeys());
    assertEquals(2 * numKeys, group.getBloomFilterElementCount());

    ECKey i1 = new ECKey();
    group.importKeys(i1);
    numKeys++;
    assertEquals(numKeys, group.numKeys());
    assertEquals(2 * numKeys, group.getBloomFilterElementCount());

    ECKey r2 = group.currentKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    assertEquals(r1, r2);
    ECKey c1 = group.currentKey(KeyChain.KeyPurpose.CHANGE);
    assertNotEquals(r1, c1);
    ECKey r3 = group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    assertNotEquals(r1, r3);
    ECKey c2 = group.freshKey(KeyChain.KeyPurpose.CHANGE);
    assertNotEquals(r3, c2);
    // Current key has not moved and will not under marked as used.
    ECKey r4 = group.currentKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    assertEquals(r2, r4);
    ECKey c3 = group.currentKey(KeyChain.KeyPurpose.CHANGE);
    assertEquals(c1, c3);
    // Mark as used. Current key is now different.
    group.markPubKeyAsUsed(r4.getPubKey());
    ECKey r5 = group.currentKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    assertNotEquals(r4, r5);
  }