@Test
 public void serializationEncrypted() throws UnreadableWalletException {
   ECKey key1 = new ECKey();
   chain.importKeys(key1);
   chain = chain.toEncrypted("foo bar");
   key1 = chain.getKeys().get(0);
   List<Protos.Key> keys = chain.serializeToProtobuf();
   assertEquals(1, keys.size());
   assertArrayEquals(key1.getPubKey(), keys.get(0).getPublicKey().toByteArray());
   assertFalse(keys.get(0).hasSecretBytes());
   assertTrue(keys.get(0).hasEncryptedData());
   chain = BasicKeyChain.fromProtobufEncrypted(keys, checkNotNull(chain.getKeyCrypter()));
   assertEquals(key1.getEncryptedPrivateKey(), chain.getKeys().get(0).getEncryptedPrivateKey());
   assertTrue(chain.checkPassword("foo bar"));
 }
  @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();
  }