@Test
  public void serializationUnencrypted() throws UnreadableWalletException {
    Utils.setMockClock();
    Date now = Utils.now();
    final ECKey key1 = new ECKey();
    Utils.rollMockClock(5000);
    final ECKey key2 = new ECKey();
    chain.importKeys(ImmutableList.of(key1, key2));
    List<Protos.Key> keys = chain.serializeToProtobuf();
    assertEquals(2, keys.size());
    assertArrayEquals(key1.getPubKey(), keys.get(0).getPublicKey().toByteArray());
    assertArrayEquals(key2.getPubKey(), keys.get(1).getPublicKey().toByteArray());
    assertArrayEquals(key1.getPrivKeyBytes(), keys.get(0).getSecretBytes().toByteArray());
    assertArrayEquals(key2.getPrivKeyBytes(), keys.get(1).getSecretBytes().toByteArray());
    long normTime = (long) (Math.floor(now.getTime() / 1000) * 1000);
    assertEquals(normTime, keys.get(0).getCreationTimestamp());
    assertEquals(normTime + 5000 * 1000, keys.get(1).getCreationTimestamp());

    chain = BasicKeyChain.fromProtobufUnencrypted(keys);
    assertEquals(2, chain.getKeys().size());
    assertEquals(key1, chain.getKeys().get(0));
    assertEquals(key2, chain.getKeys().get(1));
  }
 @Test
 public void testRoundTripNormalWallet() throws Exception {
   Wallet wallet1 = roundTrip(myWallet);
   assertEquals(0, wallet1.getTransactions(true).size());
   assertEquals(Coin.ZERO, wallet1.getBalance());
   assertArrayEquals(
       myKey.getPubKey(), wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getPubKey());
   assertArrayEquals(
       myKey.getPrivKeyBytes(),
       wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getPrivKeyBytes());
   assertEquals(
       myKey.getCreationTimeSeconds(),
       wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getCreationTimeSeconds());
 }
 @Test
 public void testKeys() throws Exception {
   for (int i = 0; i < 20; i++) {
     myKey = new ECKey();
     myAddress = myKey.toAddress(params);
     myWallet = new Wallet(params);
     myWallet.importKey(myKey);
     Wallet wallet1 = roundTrip(myWallet);
     assertArrayEquals(
         myKey.getPubKey(), wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getPubKey());
     assertArrayEquals(
         myKey.getPrivKeyBytes(),
         wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getPrivKeyBytes());
   }
 }
 @Test
 public void empty() throws Exception {
   // Check the base case of a wallet with one key and no transactions.
   Wallet wallet1 = roundTrip(myWallet);
   assertEquals(0, wallet1.getTransactions(true).size());
   assertEquals(Coin.ZERO, wallet1.getBalance());
   assertArrayEquals(
       myKey.getPubKey(), wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getPubKey());
   assertArrayEquals(
       myKey.getPrivKeyBytes(),
       wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getPrivKeyBytes());
   assertEquals(
       myKey.getCreationTimeSeconds(),
       wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getCreationTimeSeconds());
   assertEquals(mScriptCreationTime, wallet1.getWatchedScripts().get(0).getCreationTimeSeconds());
   assertEquals(1, wallet1.getWatchedScripts().size());
   assertEquals(
       ScriptBuilder.createOutputScript(myWatchedKey.toAddress(params)),
       wallet1.getWatchedScripts().get(0));
   assertEquals(WALLET_DESCRIPTION, wallet1.getDescription());
 }
  @Test
  public void encryptDecrypt() {
    final ECKey key1 = new ECKey();
    chain.importKeys(key1, new ECKey());
    final String PASSWORD = "******";
    chain = chain.toEncrypted(PASSWORD);
    final KeyCrypter keyCrypter = chain.getKeyCrypter();
    assertNotNull(keyCrypter);
    assertTrue(keyCrypter instanceof KeyCrypterScrypt);

    assertTrue(chain.checkPassword(PASSWORD));
    assertFalse(chain.checkPassword("wrong"));
    ECKey key = chain.findKeyFromPubKey(key1.getPubKey());
    assertTrue(key.isEncrypted());
    assertTrue(key.isPubKeyOnly());
    assertFalse(key.isWatching());
    assertNull(key.getSecretBytes());

    try {
      // Don't allow import of an unencrypted key.
      chain.importKeys(new ECKey());
      fail();
    } catch (KeyCrypterException e) {
    }

    try {
      chain.toDecrypted(keyCrypter.deriveKey("wrong"));
      fail();
    } catch (KeyCrypterException e) {
    }
    chain = chain.toDecrypted(PASSWORD);
    key = chain.findKeyFromPubKey(key1.getPubKey());
    assertFalse(key.isEncrypted());
    assertFalse(key.isPubKeyOnly());
    assertFalse(key.isWatching());
    key.getPrivKeyBytes();
  }