Search in sources :

Example 1 with BIP38PrivateKey

use of org.bitcoinj.crypto.BIP38PrivateKey in project sentinel-android by Samourai-Wallet.

the class BalanceActivity method doPrivKey.

private void doPrivKey(final String data) {
    // Log.d("BalanceActivity", "privkey:" + data);
    PrivKeyReader privKeyReader = null;
    String format = null;
    try {
        privKeyReader = new PrivKeyReader(new CharSequenceX(data), null);
        format = privKeyReader.getFormat();
    // Log.d("BalanceActivity", "privkey format:" + format);
    } catch (Exception e) {
        Toast.makeText(BalanceActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
        return;
    }
    if (format != null) {
        if (format.equals(PrivKeyReader.BIP38)) {
            final PrivKeyReader pvr = privKeyReader;
            final EditText password38 = new EditText(BalanceActivity.this);
            AlertDialog.Builder dlg = new AlertDialog.Builder(BalanceActivity.this).setTitle(R.string.app_name).setMessage(R.string.bip38_pw).setView(password38).setCancelable(false).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.dismiss();
                    String password = password38.getText().toString();
                    ProgressDialog progress = new ProgressDialog(BalanceActivity.this);
                    progress.setCancelable(false);
                    progress.setTitle(R.string.app_name);
                    progress.setMessage(getString(R.string.decrypting_bip38));
                    progress.show();
                    boolean keyDecoded = false;
                    try {
                        BIP38PrivateKey bip38 = new BIP38PrivateKey(MainNetParams.get(), data);
                        final ECKey ecKey = bip38.decrypt(password);
                        if (ecKey != null && ecKey.hasPrivKey()) {
                            if (progress != null && progress.isShowing()) {
                                progress.cancel();
                            }
                            pvr.setPassword(new CharSequenceX(password));
                            keyDecoded = true;
                            Toast.makeText(BalanceActivity.this, pvr.getFormat(), Toast.LENGTH_SHORT).show();
                            Toast.makeText(BalanceActivity.this, pvr.getKey().toAddress(MainNetParams.get()).toString(), Toast.LENGTH_SHORT).show();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(BalanceActivity.this, R.string.bip38_pw_error, Toast.LENGTH_SHORT).show();
                    }
                    if (progress != null && progress.isShowing()) {
                        progress.cancel();
                    }
                    if (keyDecoded) {
                        String strReceiveAddress = SamouraiSentinel.getInstance(BalanceActivity.this).getReceiveAddress();
                        if (strReceiveAddress != null) {
                            SweepUtil.getInstance(BalanceActivity.this).sweep(pvr, strReceiveAddress, SweepUtil.TYPE_P2PKH);
                        }
                    }
                }
            }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.dismiss();
                    Toast.makeText(BalanceActivity.this, R.string.bip38_pw_error, Toast.LENGTH_SHORT).show();
                }
            });
            if (!isFinishing()) {
                dlg.show();
            }
        } else if (privKeyReader != null) {
            String strReceiveAddress = SamouraiSentinel.getInstance(BalanceActivity.this).getReceiveAddress();
            if (strReceiveAddress != null) {
                Log.d("BalanceActivity", "receive address:" + strReceiveAddress);
                SweepUtil.getInstance(BalanceActivity.this).sweep(privKeyReader, strReceiveAddress, SweepUtil.TYPE_P2PKH);
            }
        } else {
            ;
        }
    } else {
        Toast.makeText(BalanceActivity.this, R.string.cannot_recognize_privkey, Toast.LENGTH_SHORT).show();
    }
}
Also used : EditText(android.widget.EditText) AlertDialog(android.app.AlertDialog) CharSequenceX(com.samourai.sentinel.util.CharSequenceX) DialogInterface(android.content.DialogInterface) BIP38PrivateKey(org.bitcoinj.crypto.BIP38PrivateKey) PrivKeyReader(com.samourai.sentinel.sweep.PrivKeyReader) ECKey(org.bitcoinj.core.ECKey) ProgressDialog(android.app.ProgressDialog) JSONException(org.json.JSONException) AddressFormatException(org.bitcoinj.core.AddressFormatException) DecoderException(org.apache.commons.codec.DecoderException) MnemonicException(org.bitcoinj.crypto.MnemonicException) IOException(java.io.IOException) OnClickListener(android.view.View.OnClickListener)

Example 2 with BIP38PrivateKey

use of org.bitcoinj.crypto.BIP38PrivateKey in project sentinel-android by Samourai-Wallet.

the class PrivKeyReader method parseBIP38.

private ECKey parseBIP38(String encryptedKey, CharSequenceX password) {
    if (password == null) {
        return null;
    }
    try {
        BIP38PrivateKey bip38 = new BIP38PrivateKey(MainNetParams.get(), encryptedKey);
        final ECKey ecKey = bip38.decrypt(password.toString());
        if (ecKey != null && ecKey.hasPrivKey()) {
            return ecKey;
        }
    } catch (Exception e) {
        ;
    }
    return null;
}
Also used : BIP38PrivateKey(org.bitcoinj.crypto.BIP38PrivateKey) ECKey(org.bitcoinj.core.ECKey)

Example 3 with BIP38PrivateKey

use of org.bitcoinj.crypto.BIP38PrivateKey in project bitcoin-wallet by bitcoin-wallet.

the class SweepWalletFragment method maybeDecodeKey.

private void maybeDecodeKey() {
    checkState(viewModel.state == SweepWalletViewModel.State.DECODE_KEY);
    final PrefixedChecksummedBytes privateKeyToSweep = viewModel.privateKeyToSweep.getValue();
    checkState(privateKeyToSweep != null);
    if (privateKeyToSweep instanceof DumpedPrivateKey) {
        final ECKey key = ((DumpedPrivateKey) privateKeyToSweep).getKey();
        askConfirmSweep(key);
    } else if (privateKeyToSweep instanceof BIP38PrivateKey) {
        badPasswordView.setVisibility(View.INVISIBLE);
        final String password = passwordView.getText().toString().trim();
        // get rid of it asap
        passwordView.setText(null);
        if (!password.isEmpty()) {
            viewModel.progress.setValue(getString(R.string.sweep_wallet_fragment_decrypt_progress));
            new DecodePrivateKeyTask(backgroundHandler) {

                @Override
                protected void onSuccess(ECKey decryptedKey) {
                    log.info("successfully decoded BIP38 private key");
                    viewModel.progress.setValue(null);
                    askConfirmSweep(decryptedKey);
                }

                @Override
                protected void onBadPassphrase() {
                    log.info("failed decoding BIP38 private key (bad password)");
                    viewModel.progress.setValue(null);
                    badPasswordView.setVisibility(View.VISIBLE);
                    passwordView.requestFocus();
                }
            }.decodePrivateKey((BIP38PrivateKey) privateKeyToSweep, password);
        }
    } else {
        throw new IllegalStateException("cannot handle type: " + privateKeyToSweep.getClass().getName());
    }
}
Also used : PrefixedChecksummedBytes(org.bitcoinj.core.PrefixedChecksummedBytes) BIP38PrivateKey(org.bitcoinj.crypto.BIP38PrivateKey) ECKey(org.bitcoinj.core.ECKey) DumpedPrivateKey(org.bitcoinj.core.DumpedPrivateKey)

Example 4 with BIP38PrivateKey

use of org.bitcoinj.crypto.BIP38PrivateKey in project samourai-wallet-android by Samourai-Wallet.

the class BalanceActivity method doPrivKey.

private void doPrivKey(final String data) {
    PrivKeyReader privKeyReader = null;
    String format = null;
    try {
        privKeyReader = new PrivKeyReader(new CharSequenceX(data), null);
        format = privKeyReader.getFormat();
    } catch (Exception e) {
        Toast.makeText(BalanceActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
        return;
    }
    if (format != null) {
        if (format.equals(PrivKeyReader.BIP38)) {
            final PrivKeyReader pvr = privKeyReader;
            final EditText password38 = new EditText(BalanceActivity.this);
            password38.setSingleLine(true);
            password38.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
            AlertDialog.Builder dlg = new AlertDialog.Builder(BalanceActivity.this).setTitle(R.string.app_name).setMessage(R.string.bip38_pw).setView(password38).setCancelable(false).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) {
                    String password = password38.getText().toString();
                    ProgressDialog progress = new ProgressDialog(BalanceActivity.this);
                    progress.setCancelable(false);
                    progress.setTitle(R.string.app_name);
                    progress.setMessage(getString(R.string.decrypting_bip38));
                    progress.show();
                    boolean keyDecoded = false;
                    try {
                        BIP38PrivateKey bip38 = new BIP38PrivateKey(SamouraiWallet.getInstance().getCurrentNetworkParams(), data);
                        final ECKey ecKey = bip38.decrypt(password);
                        if (ecKey != null && ecKey.hasPrivKey()) {
                            if (progress != null && progress.isShowing()) {
                                progress.cancel();
                            }
                            pvr.setPassword(new CharSequenceX(password));
                            keyDecoded = true;
                            Toast.makeText(BalanceActivity.this, pvr.getFormat(), Toast.LENGTH_SHORT).show();
                            Toast.makeText(BalanceActivity.this, pvr.getKey().toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString(), Toast.LENGTH_SHORT).show();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(BalanceActivity.this, R.string.bip38_pw_error, Toast.LENGTH_SHORT).show();
                    }
                    if (progress != null && progress.isShowing()) {
                        progress.cancel();
                    }
                    if (keyDecoded) {
                        SweepUtil.getInstance(BalanceActivity.this).sweep(pvr, SweepUtil.TYPE_P2PKH);
                    }
                }
            }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) {
                    Toast.makeText(BalanceActivity.this, R.string.bip38_pw_error, Toast.LENGTH_SHORT).show();
                }
            });
            if (!isFinishing()) {
                dlg.show();
            }
        } else if (privKeyReader != null) {
            SweepUtil.getInstance(BalanceActivity.this).sweep(privKeyReader, SweepUtil.TYPE_P2PKH);
        } else {
            ;
        }
    } else {
        Toast.makeText(BalanceActivity.this, R.string.cannot_recognize_privkey, Toast.LENGTH_SHORT).show();
    }
}
Also used : EditText(android.widget.EditText) AlertDialog(android.app.AlertDialog) CharSequenceX(com.samourai.wallet.util.CharSequenceX) DialogInterface(android.content.DialogInterface) BIP38PrivateKey(org.bitcoinj.crypto.BIP38PrivateKey) PrivKeyReader(com.samourai.wallet.util.PrivKeyReader) ECKey(org.bitcoinj.core.ECKey) ProgressDialog(android.app.ProgressDialog) JSONException(org.json.JSONException) ConcurrentModificationException(java.util.ConcurrentModificationException) DecryptionException(com.samourai.wallet.crypto.DecryptionException) IOException(java.io.IOException) MnemonicException(org.bitcoinj.crypto.MnemonicException) MyTransactionOutPoint(com.samourai.wallet.send.MyTransactionOutPoint)

Example 5 with BIP38PrivateKey

use of org.bitcoinj.crypto.BIP38PrivateKey in project samourai-wallet-android by Samourai-Wallet.

the class PrivKeyReader method parseBIP38.

private ECKey parseBIP38(String encryptedKey, CharSequenceX password) {
    if (password == null) {
        return null;
    }
    try {
        BIP38PrivateKey bip38 = new BIP38PrivateKey(SamouraiWallet.getInstance().getCurrentNetworkParams(), encryptedKey);
        final ECKey ecKey = bip38.decrypt(password.toString());
        if (ecKey != null && ecKey.hasPrivKey()) {
            return ecKey;
        }
    } catch (Exception e) {
        ;
    }
    return null;
}
Also used : BIP38PrivateKey(org.bitcoinj.crypto.BIP38PrivateKey) ECKey(org.bitcoinj.core.ECKey)

Aggregations

ECKey (org.bitcoinj.core.ECKey)7 BIP38PrivateKey (org.bitcoinj.crypto.BIP38PrivateKey)7 AlertDialog (android.app.AlertDialog)3 ProgressDialog (android.app.ProgressDialog)3 DialogInterface (android.content.DialogInterface)3 EditText (android.widget.EditText)3 IOException (java.io.IOException)3 MnemonicException (org.bitcoinj.crypto.MnemonicException)3 JSONException (org.json.JSONException)3 OnClickListener (android.view.View.OnClickListener)2 DecryptionException (com.samourai.wallet.crypto.DecryptionException)2 MyTransactionOutPoint (com.samourai.wallet.send.MyTransactionOutPoint)2 CharSequenceX (com.samourai.wallet.util.CharSequenceX)2 PrivKeyReader (com.samourai.wallet.util.PrivKeyReader)2 AddressFormatException (org.bitcoinj.core.AddressFormatException)2 PrivKeyReader (com.samourai.sentinel.sweep.PrivKeyReader)1 CharSequenceX (com.samourai.sentinel.util.CharSequenceX)1 ConcurrentModificationException (java.util.ConcurrentModificationException)1 DecoderException (org.apache.commons.codec.DecoderException)1 DumpedPrivateKey (org.bitcoinj.core.DumpedPrivateKey)1