Search in sources :

Example 11 with CharSequenceX

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

the class SettingsActivity2 method doWhirlpoolGUIPairing.

private void doWhirlpoolGUIPairing() {
    final JSONObject pairingObj = new JSONObject();
    final JSONObject jsonObj = new JSONObject();
    final JSONObject dojoObj = new JSONObject();
    try {
        if (DojoUtil.getInstance(SettingsActivity2.this).getDojoParams() != null) {
            String params = DojoUtil.getInstance(SettingsActivity2.this).getDojoParams();
            String url = DojoUtil.getInstance(SettingsActivity2.this).getUrl(params);
            String apiKey = DojoUtil.getInstance(SettingsActivity2.this).getApiKey(params);
            if (url != null && apiKey != null && url.length() > 0 && apiKey.length() > 0) {
                dojoObj.put("apikey", apiKey);
                dojoObj.put("url", url);
            }
        }
        jsonObj.put("type", "whirlpool.gui");
        jsonObj.put("version", "3.0.0");
        jsonObj.put("network", SamouraiWallet.getInstance().isTestNet() ? "testnet" : "mainnet");
        String mnemonic = HD_WalletFactory.getInstance(SettingsActivity2.this).get().getMnemonic();
        if (SamouraiWallet.getInstance().hasPassphrase(SettingsActivity2.this)) {
            String encrypted = AESUtil.encrypt(mnemonic, new CharSequenceX(HD_WalletFactory.getInstance(SettingsActivity2.this).get().getPassphrase()), AESUtil.DefaultPBKDF2Iterations);
            jsonObj.put("mnemonic", encrypted);
            jsonObj.put("passphrase", true);
            pairingObj.put("pairing", jsonObj);
            if (dojoObj.has("url") && dojoObj.has("apikey")) {
                String apiKey = dojoObj.getString("apikey");
                String encryptedApiKey = AESUtil.encrypt(apiKey, new CharSequenceX(HD_WalletFactory.getInstance(SettingsActivity2.this).get().getPassphrase()), AESUtil.DefaultPBKDF2Iterations);
                dojoObj.put("apikey", encryptedApiKey);
                pairingObj.put("dojo", dojoObj);
            }
            displayWhirlpoolGUIPairing(pairingObj);
        } else {
            final EditText password = new EditText(SettingsActivity2.this);
            password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            password.setHint(R.string.create_password);
            new AlertDialog.Builder(SettingsActivity2.this).setTitle(R.string.app_name).setMessage(R.string.pairing_password).setView(password).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) {
                    final String pw = password.getText().toString();
                    if (pw != null && pw.length() >= AppUtil.MIN_BACKUP_PW_LENGTH && pw.length() <= AppUtil.MAX_BACKUP_PW_LENGTH) {
                        final EditText password2 = new EditText(SettingsActivity2.this);
                        password2.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                        password2.setHint(R.string.confirm_password);
                        new AlertDialog.Builder(SettingsActivity2.this).setTitle(R.string.app_name).setMessage(R.string.pairing_password).setView(password2).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int whichButton) {
                                final String pw2 = password2.getText().toString();
                                if (pw2 != null && pw2.equals(pw)) {
                                    try {
                                        String encrypted = AESUtil.encrypt(mnemonic, new CharSequenceX(pw2), AESUtil.DefaultPBKDF2Iterations);
                                        jsonObj.put("mnemonic", encrypted);
                                        jsonObj.put("passphrase", false);
                                        pairingObj.put("pairing", jsonObj);
                                        if (dojoObj.has("url") && dojoObj.has("apikey")) {
                                            String apiKey = dojoObj.getString("apikey");
                                            String encryptedApiKey = AESUtil.encrypt(apiKey, new CharSequenceX(pw2), AESUtil.DefaultPBKDF2Iterations);
                                            dojoObj.put("apikey", encryptedApiKey);
                                            pairingObj.put("dojo", dojoObj);
                                        }
                                        displayWhirlpoolGUIPairing(pairingObj);
                                    } catch (Exception e) {
                                        ;
                                    }
                                } else {
                                    Toast.makeText(SettingsActivity2.this, R.string.password_error, Toast.LENGTH_SHORT).show();
                                }
                            }
                        }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int whichButton) {
                                ;
                            }
                        }).show();
                    } else {
                        Toast.makeText(SettingsActivity2.this, R.string.password_invalid, Toast.LENGTH_SHORT).show();
                    }
                }
            }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) {
                    ;
                }
            }).show();
        }
    } catch (Exception e) {
        Toast.makeText(SettingsActivity2.this, R.string.cannot_pair_whirlpool_gui, Toast.LENGTH_SHORT).show();
        return;
    }
}
Also used : EditText(android.widget.EditText) AlertDialog(android.app.AlertDialog) CharSequenceX(com.samourai.wallet.util.CharSequenceX) JSONObject(org.json.JSONObject) DialogInterface(android.content.DialogInterface) WriterException(com.google.zxing.WriterException) JSONException(org.json.JSONException) FileNotFoundException(java.io.FileNotFoundException) MnemonicException(org.bitcoinj.crypto.MnemonicException) DecryptionException(com.samourai.wallet.crypto.DecryptionException) IOException(java.io.IOException)

Example 12 with CharSequenceX

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

the class SettingsActivity2 method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle extras = getIntent().getExtras();
    if (extras != null && extras.containsKey("branch")) {
        String strBranch = extras.getString("branch");
        if (strBranch.equals("txs")) {
            addPreferencesFromResource(R.xml.settings_txs);
            final CheckBoxPreference cbPref0 = (CheckBoxPreference) findPreference("segwit");
            cbPref0.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    if (cbPref0.isChecked()) {
                        PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.USE_SEGWIT, false);
                    } else {
                        PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.USE_SEGWIT, true);
                    }
                    return true;
                }
            });
            final CheckBoxPreference cbPref15 = (CheckBoxPreference) findPreference("likeTypedChange");
            cbPref15.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    if (cbPref15.isChecked()) {
                        PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.USE_LIKE_TYPED_CHANGE, false);
                    } else {
                        PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.USE_LIKE_TYPED_CHANGE, true);
                    }
                    return true;
                }
            });
            final CheckBoxPreference cbPref9 = (CheckBoxPreference) findPreference("rbf");
            cbPref9.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    if (cbPref9.isChecked()) {
                        PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.RBF_OPT_IN, false);
                    } else {
                        PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.RBF_OPT_IN, true);
                    }
                    return true;
                }
            });
            Preference trustedNodePref = (Preference) findPreference("trustedNode");
            trustedNodePref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    getTrustedNode();
                    return true;
                }
            });
            final CheckBoxPreference cbPref8 = (CheckBoxPreference) findPreference("useTrustedNode");
            if (TrustedNodeUtil.getInstance().isSet() && TrustedNodeUtil.getInstance().isValidated()) {
                cbPref8.setEnabled(true);
            } else {
                cbPref8.setEnabled(false);
            }
            cbPref8.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    if (cbPref8.isChecked()) {
                        PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.USE_TRUSTED_NODE, false);
                    } else if (TrustedNodeUtil.getInstance().isSet() && TrustedNodeUtil.getInstance().isValidated()) {
                        PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.USE_TRUSTED_NODE, true);
                    } else {
                        Toast.makeText(SettingsActivity2.this, R.string.trusted_node_not_valid, Toast.LENGTH_SHORT).show();
                        cbPref8.setEnabled(false);
                        PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.USE_TRUSTED_NODE, false);
                    }
                    return true;
                }
            });
            Preference feeproviderPref = (Preference) findPreference("feeProvider");
            feeproviderPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    getFeeProvider();
                    return true;
                }
            });
            final CheckBoxPreference cbPref10 = (CheckBoxPreference) findPreference("broadcastTx");
            cbPref10.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    if (cbPref10.isChecked()) {
                        PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.BROADCAST_TX, false);
                    } else {
                        PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.BROADCAST_TX, true);
                    }
                    return true;
                }
            });
            Preference broadcastHexPref = (Preference) findPreference("broadcastHex");
            broadcastHexPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    doBroadcastHex();
                    return true;
                }
            });
            Preference psbtPref = (Preference) findPreference("psbt");
            psbtPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    doPSBT();
                    return true;
                }
            });
            Preference whirlpoolGUIPref = (Preference) findPreference("whirlpool_gui");
            whirlpoolGUIPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    doWhirlpoolGUIPairing();
                    return true;
                }
            });
        } else if (strBranch.equals("stealth")) {
            addPreferencesFromResource(R.xml.settings_stealth);
            final CheckBoxPreference cbPref1 = (CheckBoxPreference) findPreference("stealthDisplay");
            cbPref1.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    final ComponentName component = new ComponentName(getApplicationContext().getPackageName(), "com.samourai.wallet.Launcher");
                    if (component != null) {
                        if (cbPref1.isChecked()) {
                            getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
                            PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.ICON_HIDDEN, false);
                            ReceiversUtil.getInstance(SettingsActivity2.this).initReceivers();
                            AppUtil.getInstance(SettingsActivity2.this).restartApp();
                        } else {
                            String strMsg = SettingsActivity2.this.getString(R.string.options_stealth_display2);
                            new AlertDialog.Builder(SettingsActivity2.this).setIcon(R.drawable.ic_launcher).setTitle(R.string.options_stealth_display).setMessage(strMsg).setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {

                                // @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    steathActivating = true;
                                    getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
                                    PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.ICON_HIDDEN, true);
                                    ReceiversUtil.getInstance(SettingsActivity2.this).initReceivers();
                                    try {
                                        PayloadUtil.getInstance(SettingsActivity2.this).saveWalletToJSON(new CharSequenceX(AccessFactory.getInstance(SettingsActivity2.this).getGUID() + AccessFactory.getInstance(SettingsActivity2.this).getPIN()));
                                    } catch (Exception e) {
                                        ;
                                    }
                                }
                            }).setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {

                                // @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    cbPref1.setChecked(false);
                                }
                            }).show();
                        }
                    }
                    return true;
                }
            });
            final Preference remotePinPref = (Preference) findPreference("remote_pin");
            remotePinPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    if (steathActivating) {
                        Toast.makeText(SettingsActivity2.this, R.string.alternative_pin_wait, Toast.LENGTH_SHORT).show();
                    } else {
                        new AlertDialog.Builder(SettingsActivity2.this).setTitle(R.string.app_name).setMessage(R.string.alternative_pin_create).setCancelable(false).setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int whichButton) {
                                final EditText pin = new EditText(SettingsActivity2.this);
                                pin.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                                new AlertDialog.Builder(SettingsActivity2.this).setTitle(R.string.app_name).setMessage(R.string.pin_5_8).setView(pin).setCancelable(false).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                                    public void onClick(DialogInterface dialog, int whichButton) {
                                        final String _pin = pin.getText().toString();
                                        if (_pin != null && _pin.length() >= AccessFactory.MIN_PIN_LENGTH && _pin.length() <= AccessFactory.MAX_PIN_LENGTH) {
                                            final EditText pin2 = new EditText(SettingsActivity2.this);
                                            pin2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                                            new AlertDialog.Builder(SettingsActivity2.this).setTitle(R.string.app_name).setMessage(R.string.pin_5_8_confirm).setView(pin2).setCancelable(false).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                                                public void onClick(DialogInterface dialog, int whichButton) {
                                                    String _pin2 = pin2.getText().toString();
                                                    if (_pin2 != null && _pin2.equals(_pin)) {
                                                        String hash = AccessFactory.getInstance(SettingsActivity2.this).getHash(AccessFactory.getInstance(SettingsActivity2.this).getGUID(), new CharSequenceX(_pin), AESUtil.DefaultPBKDF2Iterations);
                                                        PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.ACCESS_HASH2, hash);
                                                        AccessFactory.getInstance(SettingsActivity2.this).setPIN2(_pin2);
                                                        try {
                                                            PayloadUtil.getInstance(SettingsActivity2.this).saveWalletToJSON(new CharSequenceX(AccessFactory.getInstance(SettingsActivity2.this).getGUID() + AccessFactory.getInstance(SettingsActivity2.this).getPIN()));
                                                        } catch (JSONException je) {
                                                            je.printStackTrace();
                                                        } catch (IOException ioe) {
                                                            ioe.printStackTrace();
                                                        } catch (MnemonicException.MnemonicLengthException mle) {
                                                            mle.printStackTrace();
                                                        } catch (DecryptionException de) {
                                                            de.printStackTrace();
                                                        } finally {
                                                            Toast.makeText(SettingsActivity2.this.getApplicationContext(), R.string.success_change_pin, Toast.LENGTH_SHORT).show();
                                                        }
                                                    }
                                                }
                                            }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                                                public void onClick(DialogInterface dialog, int whichButton) {
                                                    ;
                                                }
                                            }).show();
                                        } else {
                                            AccessFactory.getInstance(SettingsActivity2.this).setPIN2("");
                                            PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.ACCESS_HASH2, PrefsUtil.getInstance(SettingsActivity2.this).getValue(PrefsUtil.ACCESS_HASH, ""));
                                            Toast.makeText(SettingsActivity2.this, R.string.alternative_pin_deleted, Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                                    public void onClick(DialogInterface dialog, int whichButton) {
                                        ;
                                    }
                                }).show();
                            }
                        }).setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int whichButton) {
                                ;
                            }
                        }).show();
                    }
                    return true;
                }
            });
        } else if (strBranch.equals("remote")) {
            addPreferencesFromResource(R.xml.settings_remote);
            final CheckBoxPreference cbPref2 = (CheckBoxPreference) findPreference("stealthRemote");
            final CheckBoxPreference cbPref3 = (CheckBoxPreference) findPreference("trustedLock");
            final CheckBoxPreference cbPref4 = (CheckBoxPreference) findPreference("sim_switch");
            final EditTextPreference textPref1 = (EditTextPreference) findPreference("alertSMSNo");
            cbPref2.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    if (cbPref2.isChecked()) {
                        PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.ACCEPT_REMOTE, false);
                        ReceiversUtil.getInstance(SettingsActivity2.this).initReceivers();
                    } else {
                        PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.ACCEPT_REMOTE, true);
                        ReceiversUtil.getInstance(SettingsActivity2.this).initReceivers();
                    }
                    return true;
                }
            });
            cbPref3.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    if (cbPref3.isChecked()) {
                        PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.TRUSTED_LOCK, false);
                    } else {
                        PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.TRUSTED_LOCK, true);
                    }
                    return true;
                }
            });
            cbPref4.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    if (cbPref4.isChecked()) {
                        PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.CHECK_SIM, false);
                    } else {
                        SIMUtil.getInstance(SettingsActivity2.this).setStoredSIM();
                        PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.CHECK_SIM, true);
                    }
                    return true;
                }
            });
            textPref1.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    String telno = newValue.toString();
                    if (telno != null && telno.length() > 0) {
                        String s = telno.replaceAll("[^\\+0-9]", "");
                        if (s.matches("^\\+[0-9]+$")) {
                            PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.ALERT_MOBILE_NO, s);
                            cbPref3.setEnabled(true);
                            cbPref4.setEnabled(true);
                        } else {
                            Toast.makeText(SettingsActivity2.this, "Use international dialing format. Ex.:'+447385555555'", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        cbPref3.setEnabled(false);
                        cbPref3.setChecked(false);
                        cbPref4.setEnabled(false);
                        cbPref4.setChecked(false);
                    }
                    return true;
                }
            });
        } else if (strBranch.equals("wallet")) {
            addPreferencesFromResource(R.xml.settings_wallet);
            Preference mnemonicPref = (Preference) findPreference("mnemonic");
            mnemonicPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    getHDSeed(true);
                    return true;
                }
            });
            Preference xpubPref = (Preference) findPreference("xpub");
            xpubPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    getXPUB(44, 0);
                    return true;
                }
            });
            Preference ypubPref = (Preference) findPreference("ypub");
            ypubPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    getXPUB(49, 0);
                    return true;
                }
            });
            Preference zpubPref = (Preference) findPreference("zpub");
            zpubPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    getXPUB(84, 0);
                    return true;
                }
            });
            Preference zpubPrePref = (Preference) findPreference("zpub_pre");
            zpubPrePref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    getXPUB(84, WhirlpoolMeta.getInstance(SettingsActivity2.this).getWhirlpoolPremixAccount());
                    return true;
                }
            });
            Preference zpubPostPref = (Preference) findPreference("zpub_post");
            zpubPostPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    getXPUB(84, WhirlpoolMeta.getInstance(SettingsActivity2.this).getWhirlpoolPostmix());
                    return true;
                }
            });
            Preference zpubBadBankPref = (Preference) findPreference("zpub_badbank");
            zpubBadBankPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    getXPUB(84, WhirlpoolMeta.getInstance(SettingsActivity2.this).getWhirlpoolBadBank());
                    return true;
                }
            });
            Preference wipePref = (Preference) findPreference("wipe");
            wipePref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    new AlertDialog.Builder(SettingsActivity2.this).setTitle(R.string.app_name).setMessage(R.string.sure_to_erase).setCancelable(false).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int whichButton) {
                            final ProgressDialog progress = new ProgressDialog(SettingsActivity2.this);
                            progress.setTitle(R.string.app_name);
                            progress.setMessage(SettingsActivity2.this.getResources().getString(R.string.securely_wiping_wait));
                            progress.setCancelable(false);
                            progress.show();
                            new Thread(new Runnable() {

                                @Override
                                public void run() {
                                    Looper.prepare();
                                    AppUtil.getInstance(SettingsActivity2.this).wipeApp();
                                    Toast.makeText(SettingsActivity2.this, R.string.wallet_erased, Toast.LENGTH_SHORT).show();
                                    AppUtil.getInstance(SettingsActivity2.this).restartApp();
                                    if (progress != null && progress.isShowing()) {
                                        progress.dismiss();
                                    }
                                    Looper.loop();
                                }
                            }).start();
                        }
                    }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int whichButton) {
                            ;
                        }
                    }).show();
                    return true;
                }
            });
            final CheckBoxPreference cbPref5 = (CheckBoxPreference) findPreference("scramblePin");
            cbPref5.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    if (cbPref5.isChecked()) {
                        PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.SCRAMBLE_PIN, false);
                    } else {
                        PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.SCRAMBLE_PIN, true);
                    }
                    return true;
                }
            });
            final CheckBoxPreference cbPref11 = (CheckBoxPreference) findPreference("haptic");
            cbPref11.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    if (cbPref11.isChecked()) {
                        PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.HAPTIC_PIN, false);
                    } else {
                        PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.HAPTIC_PIN, true);
                    }
                    return true;
                }
            });
            Preference changePinPref = (Preference) findPreference("change_pin");
            changePinPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    new AlertDialog.Builder(SettingsActivity2.this).setTitle(R.string.app_name).setMessage(R.string.confirm_change_pin).setCancelable(false).setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int whichButton) {
                            final EditText pin = new EditText(SettingsActivity2.this);
                            pin.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                            new AlertDialog.Builder(SettingsActivity2.this).setTitle(R.string.app_name).setMessage(R.string.pin_enter).setView(pin).setCancelable(false).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog, int whichButton) {
                                    String _pin = pin.getText().toString();
                                    if (_pin != null && _pin.length() >= AccessFactory.MIN_PIN_LENGTH && _pin.length() <= AccessFactory.MAX_PIN_LENGTH) {
                                        String hash = PrefsUtil.getInstance(SettingsActivity2.this).getValue(PrefsUtil.ACCESS_HASH, "");
                                        if (AccessFactory.getInstance(SettingsActivity2.this).validateHash(hash, AccessFactory.getInstance(SettingsActivity2.this).getGUID(), new CharSequenceX(_pin), AESUtil.DefaultPBKDF2Iterations)) {
                                            final EditText pin = new EditText(SettingsActivity2.this);
                                            pin.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                                            new AlertDialog.Builder(SettingsActivity2.this).setTitle(R.string.app_name).setMessage(R.string.pin_5_8).setView(pin).setCancelable(false).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                                                public void onClick(DialogInterface dialog, int whichButton) {
                                                    final String _pin = pin.getText().toString();
                                                    if (_pin != null && _pin.length() >= AccessFactory.MIN_PIN_LENGTH && _pin.length() <= AccessFactory.MAX_PIN_LENGTH) {
                                                        final EditText pin2 = new EditText(SettingsActivity2.this);
                                                        pin2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                                                        new AlertDialog.Builder(SettingsActivity2.this).setTitle(R.string.app_name).setMessage(R.string.pin_5_8_confirm).setView(pin2).setCancelable(false).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                                                            public void onClick(DialogInterface dialog, int whichButton) {
                                                                String _pin2 = pin2.getText().toString();
                                                                if (_pin2 != null && _pin2.equals(_pin)) {
                                                                    String accessHash = PrefsUtil.getInstance(SettingsActivity2.this).getValue(PrefsUtil.ACCESS_HASH, "");
                                                                    String accessHash2 = PrefsUtil.getInstance(SettingsActivity2.this).getValue(PrefsUtil.ACCESS_HASH2, "");
                                                                    String hash = AccessFactory.getInstance(SettingsActivity2.this).getHash(AccessFactory.getInstance(SettingsActivity2.this).getGUID(), new CharSequenceX(_pin), AESUtil.DefaultPBKDF2Iterations);
                                                                    PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.ACCESS_HASH, hash);
                                                                    if (accessHash.equals(accessHash2)) {
                                                                        PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.ACCESS_HASH2, hash);
                                                                    }
                                                                    AccessFactory.getInstance(SettingsActivity2.this).setPIN(_pin2);
                                                                    try {
                                                                        PayloadUtil.getInstance(SettingsActivity2.this).saveWalletToJSON(new CharSequenceX(AccessFactory.getInstance(SettingsActivity2.this).getGUID() + _pin));
                                                                    } catch (JSONException je) {
                                                                        je.printStackTrace();
                                                                    } catch (IOException ioe) {
                                                                        ioe.printStackTrace();
                                                                    } catch (MnemonicException.MnemonicLengthException mle) {
                                                                        mle.printStackTrace();
                                                                    } catch (DecryptionException de) {
                                                                        de.printStackTrace();
                                                                    } finally {
                                                                        Toast.makeText(SettingsActivity2.this.getApplicationContext(), R.string.success_change_pin, Toast.LENGTH_SHORT).show();
                                                                    }
                                                                }
                                                            }
                                                        }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                                                            public void onClick(DialogInterface dialog, int whichButton) {
                                                                ;
                                                            }
                                                        }).show();
                                                    }
                                                }
                                            }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                                                public void onClick(DialogInterface dialog, int whichButton) {
                                                    ;
                                                }
                                            }).show();
                                        } else {
                                            Toast.makeText(SettingsActivity2.this.getApplicationContext(), R.string.pin_error, Toast.LENGTH_SHORT).show();
                                        }
                                    } else {
                                        Toast.makeText(SettingsActivity2.this.getApplicationContext(), R.string.pin_error, Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog, int whichButton) {
                                    ;
                                }
                            }).show();
                        }
                    }).setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int whichButton) {
                            ;
                        }
                    }).show();
                    return true;
                }
            });
            final CheckBoxPreference cbPref6 = (CheckBoxPreference) findPreference("autoBackup");
            if (!SamouraiWallet.getInstance().hasPassphrase(SettingsActivity2.this)) {
                cbPref6.setChecked(false);
                cbPref6.setEnabled(false);
            } else {
                cbPref6.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

                    public boolean onPreferenceChange(Preference preference, Object newValue) {
                        if (cbPref6.isChecked()) {
                            PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.AUTO_BACKUP, false);
                        } else {
                            PrefsUtil.getInstance(SettingsActivity2.this).setValue(PrefsUtil.AUTO_BACKUP, true);
                        }
                        return true;
                    }
                });
            }
        } else if (strBranch.equals("networking")) {
            addPreferencesFromResource(R.xml.settings_networking);
            Preference vpnPref = (Preference) findPreference("VPN");
            vpnPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    PackageManager pm = SettingsActivity2.this.getPackageManager();
                    try {
                        pm.getPackageInfo(AppUtil.OPENVPN_PACKAGE_ID, 0);
                        Intent intent = getPackageManager().getLaunchIntentForPackage(AppUtil.OPENVPN_PACKAGE_ID);
                        startActivity(intent);
                    } catch (PackageManager.NameNotFoundException nnfe) {
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + AppUtil.OPENVPN_PACKAGE_ID));
                        startActivity(intent);
                    }
                    return true;
                }
            });
        } else if (strBranch.equals("troubleshoot")) {
            addPreferencesFromResource(R.xml.settings_troubleshoot);
            Preference troubleshootPref = (Preference) findPreference("troubleshoot");
            troubleshootPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    doTroubleshoot();
                    return true;
                }
            });
            Preference sendBackupPref = (Preference) findPreference("send_backup_support");
            sendBackupPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    new AlertDialog.Builder(SettingsActivity2.this).setTitle(R.string.app_name).setMessage(R.string.prompt_send_backup_to_support).setCancelable(false).setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int whichButton) {
                            doSendBackup();
                        }
                    }).setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int whichButton) {
                            ;
                        }
                    }).show();
                    return true;
                }
            });
            Preference prunePref = (Preference) findPreference("prune");
            prunePref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    doPrune();
                    return true;
                }
            });
            Preference idxPref = (Preference) findPreference("idx");
            idxPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    doIndexes();
                    return true;
                }
            });
            Preference addressCalcPref = (Preference) findPreference("acalc");
            addressCalcPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    doAddressCalc();
                    return true;
                }
            });
            Preference paynymCalcPref = (Preference) findPreference("pcalc");
            paynymCalcPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    doPayNymCalc();
                    return true;
                }
            });
        } else if (strBranch.equals("other")) {
            addPreferencesFromResource(R.xml.settings_other);
            Preference hashPref = (Preference) findPreference("hash");
            hashPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    try {
                        File apk = new File(SettingsActivity2.this.getPackageCodePath());
                        MessageDigest md = MessageDigest.getInstance("SHA-256");
                        FileInputStream fis = new FileInputStream(apk);
                        byte[] dataBytes = new byte[1024 * 8];
                        int nread = 0;
                        while ((nread = fis.read(dataBytes)) != -1) {
                            md.update(dataBytes, 0, nread);
                        }
                        ;
                        byte[] hval = md.digest();
                        String hash = Hex.toHexString(hval);
                        TextView showText = new TextView(SettingsActivity2.this);
                        showText.setText(hash);
                        showText.setTextIsSelectable(true);
                        showText.setPadding(40, 10, 40, 10);
                        showText.setTextSize(18.0f);
                        new AlertDialog.Builder(SettingsActivity2.this).setTitle(R.string.app_name).setView(showText).setCancelable(false).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int whichButton) {
                                ;
                            }
                        }).show();
                    } catch (Exception e) {
                        ;
                    }
                    return true;
                }
            });
            Preference aboutPref = (Preference) findPreference("about");
            aboutPref.setSummary("Samourai," + " " + getResources().getString(R.string.version_name));
            aboutPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                    Intent intent = new Intent(SettingsActivity2.this, AboutActivity.class);
                    startActivity(intent);
                    return true;
                }
            });
        } else {
            finish();
        }
    } else {
        finish();
    }
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
}
Also used : AlertDialog(android.app.AlertDialog) CharSequenceX(com.samourai.wallet.util.CharSequenceX) DialogInterface(android.content.DialogInterface) ProgressDialog(android.app.ProgressDialog) OnPreferenceClickListener(android.preference.Preference.OnPreferenceClickListener) MnemonicException(org.bitcoinj.crypto.MnemonicException) PackageManager(android.content.pm.PackageManager) ComponentName(android.content.ComponentName) TextView(android.widget.TextView) MessageDigest(java.security.MessageDigest) ActionBar(android.app.ActionBar) EditText(android.widget.EditText) CheckBoxPreference(android.preference.CheckBoxPreference) Bundle(android.os.Bundle) JSONException(org.json.JSONException) Intent(android.content.Intent) IOException(java.io.IOException) EditTextPreference(android.preference.EditTextPreference) WriterException(com.google.zxing.WriterException) JSONException(org.json.JSONException) FileNotFoundException(java.io.FileNotFoundException) MnemonicException(org.bitcoinj.crypto.MnemonicException) DecryptionException(com.samourai.wallet.crypto.DecryptionException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) EditTextPreference(android.preference.EditTextPreference) CheckBoxPreference(android.preference.CheckBoxPreference) Preference(android.preference.Preference) JSONObject(org.json.JSONObject) DecryptionException(com.samourai.wallet.crypto.DecryptionException) File(java.io.File)

Example 13 with CharSequenceX

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

the class SettingsActivity2 method doPrune.

private void doPrune() {
    AlertDialog.Builder dlg = new AlertDialog.Builder(SettingsActivity2.this).setTitle(R.string.app_name).setMessage(R.string.prune_backup).setCancelable(false).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int whichButton) {
            try {
                // BIP47Meta.getInstance().pruneIncoming();
                SendAddressUtil.getInstance().reset();
                RicochetMeta.getInstance(SettingsActivity2.this).empty();
                BatchSendUtil.getInstance().clear();
                RBFUtil.getInstance().clear();
                PayloadUtil.getInstance(SettingsActivity2.this).saveWalletToJSON(new CharSequenceX(AccessFactory.getInstance(SettingsActivity2.this).getGUID() + AccessFactory.getInstance(SettingsActivity2.this).getPIN()));
            } catch (JSONException je) {
                je.printStackTrace();
                Toast.makeText(SettingsActivity2.this, R.string.error_reading_payload, Toast.LENGTH_SHORT).show();
            } catch (MnemonicException.MnemonicLengthException mle) {
                ;
            } catch (IOException ioe) {
                ;
            } catch (DecryptionException de) {
                ;
            }
        }
    }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int whichButton) {
            ;
        }
    });
    if (!isFinishing()) {
        dlg.show();
    }
}
Also used : AlertDialog(android.app.AlertDialog) MnemonicException(org.bitcoinj.crypto.MnemonicException) CharSequenceX(com.samourai.wallet.util.CharSequenceX) DialogInterface(android.content.DialogInterface) JSONException(org.json.JSONException) IOException(java.io.IOException) DecryptionException(com.samourai.wallet.crypto.DecryptionException)

Example 14 with CharSequenceX

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

the class PinEntryActivity method doBackupRestore.

void doBackupRestore() {
    File file = PayloadUtil.getInstance(PinEntryActivity.this).getBackupFile();
    if (file != null && file.exists()) {
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8"));
            String str = null;
            while ((str = in.readLine()) != null) {
                sb.append(str);
            }
            in.close();
        } catch (FileNotFoundException fnfe) {
            ;
        } catch (IOException ioe) {
            ;
        }
        final String data = sb.toString();
        if (data != null && data.length() > 0) {
            final EditText passphrase = new EditText(PinEntryActivity.this);
            passphrase.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
            passphrase.setHint(R.string.passphrase);
            AlertDialog.Builder dlg = new AlertDialog.Builder(PinEntryActivity.this).setTitle(R.string.app_name).setView(passphrase).setMessage(R.string.restore_wallet_from_backup).setCancelable(false).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) {
                    final String pw = passphrase.getText().toString();
                    if (pw == null || pw.length() < 1) {
                        Toast.makeText(PinEntryActivity.this, R.string.invalid_passphrase, Toast.LENGTH_SHORT).show();
                        AppUtil.getInstance(PinEntryActivity.this).restartApp(getIntent().getExtras());
                        finish();
                    }
                    final String decrypted = PayloadUtil.getInstance(PinEntryActivity.this).getDecryptedBackupPayload(data, new CharSequenceX(pw));
                    if (decrypted == null || decrypted.length() < 1) {
                        Toast.makeText(PinEntryActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
                        AppUtil.getInstance(PinEntryActivity.this).restartApp(getIntent().getExtras());
                        finish();
                    }
                    progressBar.setVisibility(View.VISIBLE);
                    new Thread(new Runnable() {

                        @Override
                        public void run() {
                            Looper.prepare();
                            try {
                                JSONObject json = new JSONObject(decrypted);
                                HD_Wallet hdw = PayloadUtil.getInstance(PinEntryActivity.this).restoreWalletfromJSON(json);
                                HD_WalletFactory.getInstance(PinEntryActivity.this).set(hdw);
                                String guid = AccessFactory.getInstance(PinEntryActivity.this).createGUID();
                                String hash = AccessFactory.getInstance(PinEntryActivity.this).getHash(guid, new CharSequenceX(AccessFactory.getInstance(PinEntryActivity.this).getPIN()), AESUtil.DefaultPBKDF2Iterations);
                                PrefsUtil.getInstance(PinEntryActivity.this).setValue(PrefsUtil.ACCESS_HASH, hash);
                                PrefsUtil.getInstance(PinEntryActivity.this).setValue(PrefsUtil.ACCESS_HASH2, hash);
                                PayloadUtil.getInstance(PinEntryActivity.this).saveWalletToJSON(new CharSequenceX(guid + AccessFactory.getInstance().getPIN()));
                            } catch (MnemonicException.MnemonicLengthException mle) {
                                mle.printStackTrace();
                                Toast.makeText(PinEntryActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
                            } catch (DecoderException de) {
                                de.printStackTrace();
                                Toast.makeText(PinEntryActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
                            } catch (JSONException je) {
                                je.printStackTrace();
                                Toast.makeText(PinEntryActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
                            } catch (IOException ioe) {
                                ioe.printStackTrace();
                                Toast.makeText(PinEntryActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
                            } catch (java.lang.NullPointerException npe) {
                                npe.printStackTrace();
                                Toast.makeText(PinEntryActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
                            } catch (DecryptionException de) {
                                de.printStackTrace();
                                Toast.makeText(PinEntryActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
                            } finally {
                                runOnUiThread(() -> {
                                    progressBar.setVisibility(View.INVISIBLE);
                                });
                                new AlertDialog.Builder(PinEntryActivity.this).setTitle(R.string.app_name).setMessage(getString(R.string.pin_reminder) + "\n\n" + AccessFactory.getInstance(PinEntryActivity.this).getPIN()).setCancelable(false).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                                    public void onClick(DialogInterface dialog, int whichButton) {
                                        dialog.dismiss();
                                        AppUtil.getInstance(PinEntryActivity.this).restartApp(getIntent().getExtras());
                                        finish();
                                    }
                                }).show();
                            }
                            Looper.loop();
                        }
                    }).start();
                }
            }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) {
                    AppUtil.getInstance(PinEntryActivity.this).restartApp(getIntent().getExtras());
                    finish();
                }
            });
            if (!isFinishing()) {
                dlg.show();
            }
        }
    } else {
        Intent intent = new Intent(PinEntryActivity.this, PinEntryActivity.class);
        // intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        if (getIntent().getExtras() != null)
            intent.putExtras(getIntent().getExtras());
        startActivity(intent);
        finish();
    }
}
Also used : AlertDialog(android.app.AlertDialog) HD_Wallet(com.samourai.wallet.hd.HD_Wallet) CharSequenceX(com.samourai.wallet.util.CharSequenceX) DialogInterface(android.content.DialogInterface) FileNotFoundException(java.io.FileNotFoundException) MnemonicException(org.bitcoinj.crypto.MnemonicException) EditText(android.widget.EditText) InputStreamReader(java.io.InputStreamReader) JSONException(org.json.JSONException) Intent(android.content.Intent) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) DecoderException(org.apache.commons.codec.DecoderException) JSONObject(org.json.JSONObject) BufferedReader(java.io.BufferedReader) File(java.io.File) DecryptionException(com.samourai.wallet.crypto.DecryptionException)

Example 15 with CharSequenceX

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

the class PinEntryActivity method initThread.

private void initThread(final boolean create, final String pin, final String passphrase, final String seed) {
    new Thread(new Runnable() {

        @Override
        public void run() {
            Looper.prepare();
            String guid = AccessFactory.getInstance(PinEntryActivity.this).createGUID();
            String hash = AccessFactory.getInstance(PinEntryActivity.this).getHash(guid, new CharSequenceX(pin), AESUtil.DefaultPBKDF2Iterations);
            PrefsUtil.getInstance(PinEntryActivity.this).setValue(PrefsUtil.ACCESS_HASH, hash);
            PrefsUtil.getInstance(PinEntryActivity.this).setValue(PrefsUtil.ACCESS_HASH2, hash);
            if (create) {
                try {
                    HD_WalletFactory.getInstance(PinEntryActivity.this).newWallet(12, passphrase, SamouraiWallet.NB_ACCOUNTS);
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                } catch (MnemonicException.MnemonicLengthException mle) {
                    mle.printStackTrace();
                } finally {
                    ;
                }
            } else if (seed == null) {
                ;
            } else {
                try {
                    HD_WalletFactory.getInstance(PinEntryActivity.this).restoreWallet(seed, passphrase, SamouraiWallet.NB_ACCOUNTS);
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                } catch (DecoderException de) {
                    de.printStackTrace();
                } catch (AddressFormatException afe) {
                    afe.printStackTrace();
                } catch (MnemonicException.MnemonicLengthException mle) {
                    mle.printStackTrace();
                } catch (MnemonicException.MnemonicChecksumException mce) {
                    mce.printStackTrace();
                } catch (MnemonicException.MnemonicWordException mwe) {
                    mwe.printStackTrace();
                } finally {
                    ;
                }
            }
            PrefsUtil.getInstance(PinEntryActivity.this).setValue(PrefsUtil.SCRAMBLE_PIN, true);
            try {
                String msg = null;
                if (HD_WalletFactory.getInstance(PinEntryActivity.this).get() != null) {
                    if (create) {
                        msg = getString(R.string.wallet_created_ok);
                    } else {
                        msg = getString(R.string.wallet_restored_ok);
                    }
                    try {
                        AccessFactory.getInstance(PinEntryActivity.this).setPIN(pin);
                        PayloadUtil.getInstance(PinEntryActivity.this).saveWalletToJSON(new CharSequenceX(AccessFactory.getInstance(PinEntryActivity.this).getGUID() + pin));
                        if (create) {
                            PrefsUtil.getInstance(PinEntryActivity.this).setValue(PrefsUtil.WALLET_ORIGIN, "new");
                            PrefsUtil.getInstance(PinEntryActivity.this).setValue(PrefsUtil.FIRST_RUN, true);
                        } else {
                            PrefsUtil.getInstance(PinEntryActivity.this).setValue(PrefsUtil.WALLET_ORIGIN, "restored");
                            PrefsUtil.getInstance(PinEntryActivity.this).setValue(PrefsUtil.FIRST_RUN, true);
                        }
                    } catch (JSONException je) {
                        je.printStackTrace();
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    } catch (DecryptionException de) {
                        de.printStackTrace();
                    } finally {
                        ;
                    }
                    for (int i = 0; i < 2; i++) {
                        AddressFactory.getInstance().account2xpub().put(i, HD_WalletFactory.getInstance(PinEntryActivity.this).get().getAccount(i).xpubstr());
                        AddressFactory.getInstance().xpub2account().put(HD_WalletFactory.getInstance(PinEntryActivity.this).get().getAccount(i).xpubstr(), i);
                    }
                    // 
                    if (create) {
                        String seed = null;
                        try {
                            seed = HD_WalletFactory.getInstance(PinEntryActivity.this).get().getMnemonic();
                        } catch (IOException ioe) {
                            ioe.printStackTrace();
                        } catch (MnemonicException.MnemonicLengthException mle) {
                            mle.printStackTrace();
                        }
                        Intent intent = new Intent(PinEntryActivity.this, RecoveryWordsActivity.class);
                        intent.putExtra("BIP39_WORD_LIST", seed);
                        startActivity(intent);
                        finish();
                    } else {
                        AccessFactory.getInstance(PinEntryActivity.this).setIsLoggedIn(true);
                        TimeOutUtil.getInstance().updatePin();
                        AppUtil.getInstance(PinEntryActivity.this).restartApp(getIntent().getExtras());
                        finish();
                    }
                } else {
                    if (create) {
                        msg = getString(R.string.wallet_created_ko);
                    } else {
                        msg = getString(R.string.wallet_restored_ko);
                    }
                }
                Toast.makeText(PinEntryActivity.this, msg, Toast.LENGTH_SHORT).show();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            } catch (MnemonicException.MnemonicLengthException mle) {
                mle.printStackTrace();
            } finally {
                ;
            }
            progressBar.setVisibility(View.INVISIBLE);
            Looper.loop();
        }
    }).start();
}
Also used : AddressFormatException(org.bitcoinj.core.AddressFormatException) CharSequenceX(com.samourai.wallet.util.CharSequenceX) JSONException(org.json.JSONException) Intent(android.content.Intent) IOException(java.io.IOException) DecoderException(org.apache.commons.codec.DecoderException) MnemonicException(org.bitcoinj.crypto.MnemonicException) DecryptionException(com.samourai.wallet.crypto.DecryptionException)

Aggregations

CharSequenceX (com.samourai.wallet.util.CharSequenceX)51 IOException (java.io.IOException)38 JSONException (org.json.JSONException)37 DecryptionException (com.samourai.wallet.crypto.DecryptionException)35 MnemonicException (org.bitcoinj.crypto.MnemonicException)35 Intent (android.content.Intent)22 AlertDialog (android.app.AlertDialog)14 DialogInterface (android.content.DialogInterface)14 JSONObject (org.json.JSONObject)14 AddressFormatException (org.bitcoinj.core.AddressFormatException)13 DecoderException (org.bouncycastle.util.encoders.DecoderException)10 DecoderException (org.apache.commons.codec.DecoderException)9 EditText (android.widget.EditText)8 PrivKeyReader (com.samourai.wallet.util.PrivKeyReader)8 MyTransactionOutPoint (com.samourai.wallet.send.MyTransactionOutPoint)7 ProgressDialog (android.app.ProgressDialog)6 PaymentCode (com.samourai.wallet.bip47.rpc.PaymentCode)6 FileNotFoundException (java.io.FileNotFoundException)6 ConcurrentModificationException (java.util.ConcurrentModificationException)6 ClipData (android.content.ClipData)4