use of org.bitcoinj.crypto.KeyCrypterException in project bitcoin-wallet by bitcoin-wallet.
the class DeriveKeyTask method deriveKey.
public final void deriveKey(final Wallet wallet, final String password) {
checkState(wallet.isEncrypted());
final KeyCrypter keyCrypter = checkNotNull(wallet.getKeyCrypter());
backgroundHandler.post(new Runnable() {
@Override
public void run() {
org.bitcoinj.core.Context.propagate(Constants.CONTEXT);
// Key derivation takes time.
KeyParameter key = keyCrypter.deriveKey(password);
boolean wasChanged = false;
// If the key isn't derived using the desired parameters, derive a new key.
if (keyCrypter instanceof KeyCrypterScrypt) {
final long scryptIterations = ((KeyCrypterScrypt) keyCrypter).getScryptParameters().getN();
if (scryptIterations != scryptIterationsTarget) {
log.info("upgrading scrypt iterations from {} to {}; re-encrypting wallet", scryptIterations, scryptIterationsTarget);
final KeyCrypterScrypt newKeyCrypter = new KeyCrypterScrypt(scryptIterationsTarget);
final KeyParameter newKey = newKeyCrypter.deriveKey(password);
// Re-encrypt wallet with new key.
try {
wallet.changeEncryptionKey(newKeyCrypter, key, newKey);
key = newKey;
wasChanged = true;
log.info("scrypt upgrade succeeded");
} catch (final KeyCrypterException x) {
log.info("scrypt upgrade failed: {}", x.getMessage());
}
}
}
// Hand back the (possibly changed) encryption key.
final KeyParameter keyToReturn = key;
final boolean keyToReturnWasChanged = wasChanged;
callbackHandler.post(new Runnable() {
@Override
public void run() {
onSuccess(keyToReturn, keyToReturnWasChanged);
}
});
}
});
}
use of org.bitcoinj.crypto.KeyCrypterException in project bitcoin-wallet by bitcoin-wallet.
the class SendCoinsOfflineTask method sendCoinsOffline.
public final void sendCoinsOffline(final SendRequest sendRequest) {
backgroundHandler.post(new Runnable() {
@Override
public void run() {
org.bitcoinj.core.Context.propagate(Constants.CONTEXT);
try {
log.info("sending: {}", sendRequest);
// can take long
final Transaction transaction = wallet.sendCoinsOffline(sendRequest);
log.info("send successful, transaction committed: {}", transaction.getHashAsString());
callbackHandler.post(new Runnable() {
@Override
public void run() {
onSuccess(transaction);
}
});
} catch (final InsufficientMoneyException x) {
final Coin missing = x.missing;
if (missing != null)
log.info("send failed, {} missing", missing.toFriendlyString());
else
log.info("send failed, insufficient coins");
callbackHandler.post(new Runnable() {
@Override
public void run() {
onInsufficientMoney(x.missing);
}
});
} catch (final ECKey.KeyIsEncryptedException x) {
log.info("send failed, key is encrypted: {}", x.getMessage());
callbackHandler.post(new Runnable() {
@Override
public void run() {
onFailure(x);
}
});
} catch (final KeyCrypterException x) {
log.info("send failed, key crypter exception: {}", x.getMessage());
final boolean isEncrypted = wallet.isEncrypted();
callbackHandler.post(new Runnable() {
@Override
public void run() {
if (isEncrypted)
onInvalidEncryptionKey();
else
onFailure(x);
}
});
} catch (final CouldNotAdjustDownwards x) {
log.info("send failed, could not adjust downwards: {}", x.getMessage());
callbackHandler.post(new Runnable() {
@Override
public void run() {
onEmptyWalletFailed();
}
});
} catch (final CompletionException x) {
log.info("send failed, cannot complete: {}", x.getMessage());
callbackHandler.post(new Runnable() {
@Override
public void run() {
onFailure(x);
}
});
}
}
});
}
use of org.bitcoinj.crypto.KeyCrypterException in project bitcoin-wallet by bitcoin-wallet.
the class EncryptKeysDialogFragment method handleGo.
private void handleGo() {
final String oldPassword = Strings.emptyToNull(oldPasswordView.getText().toString().trim());
final String newPassword = Strings.emptyToNull(newPasswordView.getText().toString().trim());
if (oldPassword != null && newPassword != null)
log.info("changing spending password");
else if (newPassword != null)
log.info("setting spending password");
else if (oldPassword != null)
log.info("removing spending password");
else
throw new IllegalStateException();
state = State.CRYPTING;
updateView();
backgroundHandler.post(new Runnable() {
@Override
public void run() {
// For the old key, we use the key crypter that was used to derive the password in the first
// place.
final KeyParameter oldKey = oldPassword != null ? wallet.getKeyCrypter().deriveKey(oldPassword) : null;
// For the new key, we create a new key crypter according to the desired parameters.
final KeyCrypterScrypt keyCrypter = new KeyCrypterScrypt(application.scryptIterationsTarget());
final KeyParameter newKey = newPassword != null ? keyCrypter.deriveKey(newPassword) : null;
handler.post(new Runnable() {
@Override
public void run() {
if (wallet.isEncrypted()) {
if (oldKey == null) {
log.info("wallet is encrypted, but did not provide spending password");
state = State.INPUT;
oldPasswordView.requestFocus();
} else {
try {
wallet.decrypt(oldKey);
state = State.DONE;
log.info("wallet successfully decrypted");
} catch (final KeyCrypterException x) {
log.info("wallet decryption failed: " + x.getMessage());
badPasswordView.setVisibility(View.VISIBLE);
state = State.INPUT;
oldPasswordView.requestFocus();
}
}
}
if (newKey != null && !wallet.isEncrypted()) {
wallet.encrypt(keyCrypter, newKey);
log.info("wallet successfully encrypted, using key derived by new spending password ({} scrypt iterations)", keyCrypter.getScryptParameters().getN());
state = State.DONE;
}
updateView();
if (state == State.DONE) {
application.backupWallet();
delayedDismiss();
}
}
private void delayedDismiss() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
dismiss();
}
}, 2000);
}
});
}
});
}
use of org.bitcoinj.crypto.KeyCrypterException in project bitcoin-wallet by bitcoin-wallet.
the class RaiseFeeDialogFragment method doRaiseFee.
private void doRaiseFee(final KeyParameter encryptionKey) {
// construct child-pays-for-parent
final TransactionOutput outputToSpend = checkNotNull(findSpendableOutput(wallet, transaction, feeRaise));
final Transaction transactionToSend = new Transaction(Constants.NETWORK_PARAMETERS);
transactionToSend.addInput(outputToSpend);
transactionToSend.addOutput(outputToSpend.getValue().subtract(feeRaise), wallet.freshAddress(KeyPurpose.CHANGE));
transactionToSend.setPurpose(Transaction.Purpose.RAISE_FEE);
final SendRequest sendRequest = SendRequest.forTx(transactionToSend);
sendRequest.aesKey = encryptionKey;
try {
wallet.signTransaction(sendRequest);
log.info("raise fee: cpfp {}", transactionToSend);
wallet.commitTx(transactionToSend);
BlockchainService.broadcastTransaction(activity, transactionToSend);
state = State.DONE;
updateView();
dismiss();
} catch (final KeyCrypterException x) {
badPasswordView.setVisibility(View.VISIBLE);
state = State.INPUT;
updateView();
passwordView.requestFocus();
log.info("raise fee: bad spending password");
}
}
Aggregations