@Test(expected = KeyCrypterException.class)
  public void cannotImportEncryptedKey() {
    final ECKey key1 = new ECKey();
    chain.importKeys(ImmutableList.of(key1));
    chain = chain.toEncrypted("foobar");
    ECKey encryptedKey = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    assertTrue(encryptedKey.isEncrypted());

    BasicKeyChain chain2 = new BasicKeyChain();
    chain2.importKeys(ImmutableList.of(encryptedKey));
  }
  @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();
  }