Search in sources :

Example 1 with KeyCrypter

use of org.bitcoinj.crypto.KeyCrypter 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);
                }
            });
        }
    });
}
Also used : KeyCrypter(org.bitcoinj.crypto.KeyCrypter) KeyParameter(org.spongycastle.crypto.params.KeyParameter) KeyCrypterScrypt(org.bitcoinj.crypto.KeyCrypterScrypt) KeyCrypterException(org.bitcoinj.crypto.KeyCrypterException)

Aggregations

KeyCrypter (org.bitcoinj.crypto.KeyCrypter)1 KeyCrypterException (org.bitcoinj.crypto.KeyCrypterException)1 KeyCrypterScrypt (org.bitcoinj.crypto.KeyCrypterScrypt)1 KeyParameter (org.spongycastle.crypto.params.KeyParameter)1