Search in sources :

Example 1 with PrivKeyReader

use of com.samourai.wallet.util.PrivKeyReader in project samourai-wallet-android by Samourai-Wallet.

the class BalanceActivity method doSweepViaScan.

private void doSweepViaScan() {
    CameraFragmentBottomSheet cameraFragmentBottomSheet = new CameraFragmentBottomSheet();
    cameraFragmentBottomSheet.show(getSupportFragmentManager(), cameraFragmentBottomSheet.getTag());
    cameraFragmentBottomSheet.setQrCodeScanLisenter(code -> {
        cameraFragmentBottomSheet.dismissAllowingStateLoss();
        PrivKeyReader privKeyReader = new PrivKeyReader(new CharSequenceX(code.trim()));
        try {
            if (privKeyReader.getFormat() != null) {
                doPrivKey(code.trim());
            } else if (Cahoots.isCahoots(code.trim())) {
                Intent cahootIntent = new Intent(this, ManualCahootsActivity.class);
                cahootIntent.putExtra("payload", code.trim());
                cahootIntent.putExtra("_account", account);
                startActivity(cahootIntent);
            } else if (FormatsUtil.getInstance().isPSBT(code.trim())) {
                CahootsUtil.getInstance(BalanceActivity.this).doPSBT(code.trim());
            } else if (DojoUtil.getInstance(BalanceActivity.this).isValidPairingPayload(code.trim())) {
                Toast.makeText(BalanceActivity.this, "Samourai Dojo full node coming soon.", Toast.LENGTH_SHORT).show();
            } else {
                Intent intent = new Intent(BalanceActivity.this, SendActivity.class);
                intent.putExtra("uri", code.trim());
                intent.putExtra("_account", account);
                startActivity(intent);
            }
        } catch (Exception e) {
        }
    });
}
Also used : CameraFragmentBottomSheet(com.samourai.wallet.fragments.CameraFragmentBottomSheet) CharSequenceX(com.samourai.wallet.util.CharSequenceX) SendActivity(com.samourai.wallet.send.SendActivity) PrivKeyReader(com.samourai.wallet.util.PrivKeyReader) Intent(android.content.Intent) ManualCahootsActivity(com.samourai.wallet.send.cahoots.ManualCahootsActivity) JSONException(org.json.JSONException) ConcurrentModificationException(java.util.ConcurrentModificationException) DecryptionException(com.samourai.wallet.crypto.DecryptionException) IOException(java.io.IOException) MnemonicException(org.bitcoinj.crypto.MnemonicException)

Example 2 with PrivKeyReader

use of com.samourai.wallet.util.PrivKeyReader 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 3 with PrivKeyReader

use of com.samourai.wallet.util.PrivKeyReader in project samourai-wallet-android by Samourai-Wallet.

the class BalanceActivity method doClipboardCheck.

private void doClipboardCheck() {
    final android.content.ClipboardManager clipboard = (android.content.ClipboardManager) BalanceActivity.this.getSystemService(android.content.Context.CLIPBOARD_SERVICE);
    if (clipboard.hasPrimaryClip()) {
        final ClipData clip = clipboard.getPrimaryClip();
        ClipData.Item item = clip.getItemAt(0);
        if (item.getText() != null) {
            String text = item.getText().toString();
            String[] s = text.split("\\s+");
            try {
                for (int i = 0; i < s.length; i++) {
                    PrivKeyReader privKeyReader = new PrivKeyReader(new CharSequenceX(s[i]));
                    if (privKeyReader.getFormat() != null && (privKeyReader.getFormat().equals(PrivKeyReader.WIF_COMPRESSED) || privKeyReader.getFormat().equals(PrivKeyReader.WIF_UNCOMPRESSED) || privKeyReader.getFormat().equals(PrivKeyReader.BIP38))) {
                        new AlertDialog.Builder(BalanceActivity.this).setTitle(R.string.app_name).setMessage(R.string.privkey_clipboard).setCancelable(false).setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int whichButton) {
                                clipboard.setPrimaryClip(ClipData.newPlainText("", ""));
                            }
                        }).setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int whichButton) {
                                ;
                            }
                        }).show();
                    }
                }
            } catch (Exception e) {
                ;
            }
        }
    }
}
Also used : CharSequenceX(com.samourai.wallet.util.CharSequenceX) DialogInterface(android.content.DialogInterface) PrivKeyReader(com.samourai.wallet.util.PrivKeyReader) ScriptBuilder(org.bitcoinj.script.ScriptBuilder) MyTransactionOutPoint(com.samourai.wallet.send.MyTransactionOutPoint) JSONException(org.json.JSONException) AddressFormatException(org.bitcoinj.core.AddressFormatException) DecryptionException(com.samourai.wallet.crypto.DecryptionException) IOException(java.io.IOException) DecoderException(org.bouncycastle.util.encoders.DecoderException) MnemonicException(org.bitcoinj.crypto.MnemonicException) OnClickListener(android.view.View.OnClickListener) ClipData(android.content.ClipData)

Example 4 with PrivKeyReader

use of com.samourai.wallet.util.PrivKeyReader in project samourai-wallet-android by Samourai-Wallet.

the class BalanceActivity method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK && requestCode == SCAN_COLD_STORAGE) {
        if (data != null && data.getStringExtra(ZBarConstants.SCAN_RESULT) != null) {
            final String strResult = data.getStringExtra(ZBarConstants.SCAN_RESULT);
            doPrivKey(strResult);
        }
    } else if (resultCode == Activity.RESULT_CANCELED && requestCode == SCAN_COLD_STORAGE) {
        ;
    } else if (resultCode == Activity.RESULT_OK && requestCode == SCAN_QR) {
        if (data != null && data.getStringExtra(ZBarConstants.SCAN_RESULT) != null) {
            final String strResult = data.getStringExtra(ZBarConstants.SCAN_RESULT);
            PrivKeyReader privKeyReader = new PrivKeyReader(new CharSequenceX(strResult));
            try {
                if (privKeyReader.getFormat() != null) {
                    doPrivKey(strResult);
                } else {
                    Intent intent = new Intent(BalanceActivity.this, SendActivity.class);
                    intent.putExtra("uri", strResult);
                    startActivity(intent);
                }
            } catch (Exception e) {
                ;
            }
        }
    } else if (resultCode == Activity.RESULT_CANCELED && requestCode == SCAN_QR) {
        ;
    } else {
        ;
    }
}
Also used : CharSequenceX(com.samourai.wallet.util.CharSequenceX) PrivKeyReader(com.samourai.wallet.util.PrivKeyReader) Intent(android.content.Intent) JSONException(org.json.JSONException) AddressFormatException(org.bitcoinj.core.AddressFormatException) DecryptionException(com.samourai.wallet.crypto.DecryptionException) IOException(java.io.IOException) DecoderException(org.bouncycastle.util.encoders.DecoderException) MnemonicException(org.bitcoinj.crypto.MnemonicException)

Example 5 with PrivKeyReader

use of com.samourai.wallet.util.PrivKeyReader 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);
            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, false);
                    }
                }
            }).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, false);
        } 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) ScriptBuilder(org.bitcoinj.script.ScriptBuilder) ECKey(org.bitcoinj.core.ECKey) ProgressDialog(android.app.ProgressDialog) JSONException(org.json.JSONException) AddressFormatException(org.bitcoinj.core.AddressFormatException) DecryptionException(com.samourai.wallet.crypto.DecryptionException) IOException(java.io.IOException) DecoderException(org.bouncycastle.util.encoders.DecoderException) MnemonicException(org.bitcoinj.crypto.MnemonicException) MyTransactionOutPoint(com.samourai.wallet.send.MyTransactionOutPoint) OnClickListener(android.view.View.OnClickListener)

Aggregations

PrivKeyReader (com.samourai.wallet.util.PrivKeyReader)9 DecryptionException (com.samourai.wallet.crypto.DecryptionException)8 CharSequenceX (com.samourai.wallet.util.CharSequenceX)8 IOException (java.io.IOException)8 MnemonicException (org.bitcoinj.crypto.MnemonicException)8 JSONException (org.json.JSONException)8 DialogInterface (android.content.DialogInterface)5 Intent (android.content.Intent)5 ConcurrentModificationException (java.util.ConcurrentModificationException)5 MyTransactionOutPoint (com.samourai.wallet.send.MyTransactionOutPoint)4 AlertDialog (android.app.AlertDialog)3 SendActivity (com.samourai.wallet.send.SendActivity)3 ManualCahootsActivity (com.samourai.wallet.send.cahoots.ManualCahootsActivity)3 AddressFormatException (org.bitcoinj.core.AddressFormatException)3 DecoderException (org.bouncycastle.util.encoders.DecoderException)3 ProgressDialog (android.app.ProgressDialog)2 ClipData (android.content.ClipData)2 OnClickListener (android.view.View.OnClickListener)2 EditText (android.widget.EditText)2 CameraFragmentBottomSheet (com.samourai.wallet.fragments.CameraFragmentBottomSheet)2