/** Takes a list of keys and a password, then encrypts and imports them in one step using the current keycrypter. */ public int importKeysAndEncrypt(final List<ECKey> keys, CharSequence password) { keyChainGroupLock.lock(); try { checkNotNull(getKeyCrypter(), "Wallet is not encrypted"); return importKeysAndEncrypt(keys, getKeyCrypter().deriveKey(password)); } finally { keyChainGroupLock.unlock(); } }
/** Takes a list of keys and a password, then encrypts and imports them in one step using the current keycrypter. */ public int importKeysAndEncrypt(final List<ECKey> keys, CharSequence password) { keyChainGroupLock.lock(); try { checkNotNull(getKeyCrypter(), "Wallet is not encrypted"); return importKeysAndEncrypt(keys, getKeyCrypter().deriveKey(password)); } finally { keyChainGroupLock.unlock(); } }
/** Takes a list of keys and a password, then encrypts and imports them in one step using the current keycrypter. */ public int importKeysAndEncrypt(final List<ECKey> keys, CharSequence password) { keyChainGroupLock.lock(); try { checkNotNull(getKeyCrypter(), "Wallet is not encrypted"); return importKeysAndEncrypt(keys, getKeyCrypter().deriveKey(password)); } finally { keyChainGroupLock.unlock(); } }
/** Takes a list of keys and a password, then encrypts and imports them in one step using the current keycrypter. */ public int importKeysAndEncrypt(final List<ECKey> keys, CharSequence password) { keyChainGroupLock.lock(); try { checkNotNull(getKeyCrypter(), "Wallet is not encrypted"); return importKeysAndEncrypt(keys, getKeyCrypter().deriveKey(password)); } finally { keyChainGroupLock.unlock(); } }
@Nullable private static KeyParameter passwordToKey(boolean printError) { if (password == null) { if (printError) System.err.println("You must provide a password."); return null; } if (!wallet.checkPassword(password)) { if (printError) System.err.println("The password is incorrect."); return null; } return checkNotNull(wallet.getKeyCrypter()).deriveKey(password); }
if (aesKey == null) return; // Error message already printed. key = key.encrypt(checkNotNull(wallet.getKeyCrypter()), aesKey);
@Test public void testEncryptedClientWallet() throws Exception { // Encrypt the client wallet String mySecretPw = "MySecret"; wallet.encrypt(mySecretPw); KeyParameter userKeySetup = wallet.getKeyCrypter().deriveKey(mySecretPw); exectuteSimpleChannelTest(userKeySetup); }
@Test(expected = KeyCrypterException.class) public void addEncryptedKeyToUnencryptedWallet() throws Exception { Wallet encryptedWallet = new Wallet(PARAMS); encryptedWallet.encrypt(PASSWORD1); KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter(); ECKey key1 = new ECKey(); key1 = key1.encrypt(keyCrypter, keyCrypter.deriveKey("PASSWORD!")); wallet.importKey(key1); }
@Test public void importAndEncrypt() throws InsufficientMoneyException { Wallet encryptedWallet = new Wallet(PARAMS); encryptedWallet.encrypt(PASSWORD1); final ECKey key = new ECKey(); encryptedWallet.importKeysAndEncrypt(ImmutableList.of(key), PASSWORD1); assertEquals(1, encryptedWallet.getImportedKeys().size()); assertEquals(key.getPubKeyPoint(), encryptedWallet.getImportedKeys().get(0).getPubKeyPoint()); sendMoneyToWallet(encryptedWallet, AbstractBlockChain.NewBlockType.BEST_CHAIN, Coin.COIN, key.toAddress(PARAMS)); assertEquals(Coin.COIN, encryptedWallet.getBalance()); SendRequest req = SendRequest.emptyWallet(OTHER_ADDRESS); req.aesKey = checkNotNull(encryptedWallet.getKeyCrypter()).deriveKey(PASSWORD1); encryptedWallet.sendCoinsOffline(req); }
KeyCrypter keyCrypter = wallet.getKeyCrypter(); if (keyCrypter == null) {
KeyCrypter keyCrypter = wallet.getKeyCrypter(); if (keyCrypter == null) {
KeyCrypter keyCrypter = wallet.getKeyCrypter(); if (keyCrypter == null) {
KeyCrypter keyCrypter = wallet.getKeyCrypter(); if (keyCrypter == null) {
@Test(expected = KeyCrypterException.class) public void mismatchedCrypter() throws Exception { Wallet encryptedWallet = new Wallet(PARAMS); encryptedWallet.encrypt(PASSWORD1); KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter(); KeyParameter aesKey = keyCrypter.deriveKey(PASSWORD1); // Try added an ECKey that was encrypted with a differenct ScryptParameters (i.e. a non-homogenous key). // This is not allowed as the ScryptParameters is stored at the Wallet level. Protos.ScryptParameters.Builder scryptParametersBuilder = Protos.ScryptParameters.newBuilder() .setSalt(ByteString.copyFrom(KeyCrypterScrypt.randomSalt())); Protos.ScryptParameters scryptParameters = scryptParametersBuilder.build(); KeyCrypter keyCrypterDifferent = new KeyCrypterScrypt(scryptParameters); ECKey ecKeyDifferent = new ECKey(); ecKeyDifferent = ecKeyDifferent.encrypt(keyCrypterDifferent, aesKey); encryptedWallet.importKey(ecKeyDifferent); }
@Test public void changeAesKeyTest() { Wallet encryptedWallet = new Wallet(PARAMS); encryptedWallet.encrypt(PASSWORD1); KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter(); KeyParameter aesKey = keyCrypter.deriveKey(PASSWORD1); CharSequence newPassword = "My name is Tom"; KeyParameter newAesKey = keyCrypter.deriveKey(newPassword); encryptedWallet.changeEncryptionKey(keyCrypter, aesKey, newAesKey); assertTrue(encryptedWallet.checkAESKey(newAesKey)); assertFalse(encryptedWallet.checkAESKey(aesKey)); }
@Test public void encryptionDecryptionAESBasic() throws Exception { Wallet encryptedWallet = new Wallet(PARAMS); encryptedWallet.encrypt(PASSWORD1); KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter(); KeyParameter aesKey = keyCrypter.deriveKey(PASSWORD1); assertEquals(EncryptionType.ENCRYPTED_SCRYPT_AES, encryptedWallet.getEncryptionType()); assertTrue(encryptedWallet.checkPassword(PASSWORD1)); assertTrue(encryptedWallet.checkAESKey(aesKey)); assertFalse(encryptedWallet.checkPassword(WRONG_PASSWORD)); assertNotNull("The keyCrypter is missing but should not be", keyCrypter); encryptedWallet.decrypt(aesKey); // Wallet should now be unencrypted. assertNull("Wallet is not an unencrypted wallet", encryptedWallet.getKeyCrypter()); try { encryptedWallet.checkPassword(PASSWORD1); fail(); } catch (IllegalStateException e) { } }
@Test public void encryptionDecryptionBadPassword() throws Exception { Wallet encryptedWallet = new Wallet(PARAMS); encryptedWallet.encrypt(PASSWORD1); KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter(); KeyParameter wrongAesKey = keyCrypter.deriveKey(WRONG_PASSWORD); // Check the wallet is currently encrypted assertEquals("Wallet is not an encrypted wallet", EncryptionType.ENCRYPTED_SCRYPT_AES, encryptedWallet.getEncryptionType()); assertFalse(encryptedWallet.checkAESKey(wrongAesKey)); // Check that the wrong password does not decrypt the wallet. try { encryptedWallet.decrypt(wrongAesKey); fail("Incorrectly decoded wallet with wrong password"); } catch (KeyCrypterException ede) { // Expected. } }
@Test public void encryptionDecryptionPasswordBasic() throws Exception { Wallet encryptedWallet = new Wallet(PARAMS); encryptedWallet.encrypt(PASSWORD1); assertTrue(encryptedWallet.isEncrypted()); encryptedWallet.decrypt(PASSWORD1); assertFalse(encryptedWallet.isEncrypted()); // Wallet should now be unencrypted. assertNull("Wallet is not an unencrypted wallet", encryptedWallet.getKeyCrypter()); try { encryptedWallet.checkPassword(PASSWORD1); fail(); } catch (IllegalStateException e) { } }
Wallet encryptedWallet = new Wallet(PARAMS); encryptedWallet.encrypt(PASSWORD1); KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter(); KeyParameter aesKey = keyCrypter.deriveKey(PASSWORD1); assertNull("Wallet is not an unencrypted wallet", encryptedWallet.getKeyCrypter());
KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter(); KeyParameter aesKey = keyCrypter.deriveKey(PASSWORD1); KeyParameter wrongAesKey = keyCrypter.deriveKey(WRONG_PASSWORD);