Search in sources :

Example 16 with HD_Wallet

use of com.samourai.wallet.hd.HD_Wallet in project samourai-wallet-android by Samourai-Wallet.

the class PinEntryActivity method validateThread.

private void validateThread(final String pin, final String uri) {
    progressBar.setVisibility(View.VISIBLE);
    new Thread(() -> {
        Looper.prepare();
        if (pin.length() < AccessFactory.MIN_PIN_LENGTH || pin.length() > AccessFactory.MAX_PIN_LENGTH) {
            runOnUiThread(() -> {
                progressBar.setVisibility(View.INVISIBLE);
            });
            Toast.makeText(PinEntryActivity.this, R.string.pin_error, Toast.LENGTH_SHORT).show();
            AppUtil.getInstance(PinEntryActivity.this).restartApp(getIntent().getExtras());
            finish();
        }
        String randomKey = AccessFactory.getInstance(PinEntryActivity.this).getGUID();
        if (randomKey.length() < 1) {
            runOnUiThread(() -> {
                progressBar.setVisibility(View.INVISIBLE);
            });
            Toast.makeText(PinEntryActivity.this, R.string.random_key_error, Toast.LENGTH_SHORT).show();
            AppUtil.getInstance(PinEntryActivity.this).restartApp(getIntent().getExtras());
            finish();
        }
        String hash = PrefsUtil.getInstance(PinEntryActivity.this).getValue(PrefsUtil.ACCESS_HASH, "");
        if (AccessFactory.getInstance(PinEntryActivity.this).validateHash(hash, randomKey, new CharSequenceX(pin), AESUtil.DefaultPBKDF2Iterations)) {
            AccessFactory.getInstance(PinEntryActivity.this).setPIN(pin);
            try {
                HD_Wallet hdw = PayloadUtil.getInstance(PinEntryActivity.this).restoreWalletfromJSON(new CharSequenceX(AccessFactory.getInstance(PinEntryActivity.this).getGUID() + pin));
                runOnUiThread(() -> {
                    progressBar.setVisibility(View.INVISIBLE);
                });
                if (hdw == null) {
                    runOnUiThread(() -> {
                        failures++;
                        Toast.makeText(PinEntryActivity.this, PinEntryActivity.this.getText(R.string.login_error) + ":" + failures + "/3", Toast.LENGTH_SHORT).show();
                        if (failures == 3) {
                            failures = 0;
                            doBackupRestore();
                        } else {
                            Intent intent = new Intent(PinEntryActivity.this, PinEntryActivity.class);
                            // intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            finish();
                        }
                    });
                }
                AccessFactory.getInstance(PinEntryActivity.this).setIsLoggedIn(true);
                TimeOutUtil.getInstance().updatePin();
                if (isOpenDime) {
                    runOnUiThread(() -> {
                        Intent intent = new Intent(PinEntryActivity.this, OpenDimeActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    });
                } else {
                    AppUtil.getInstance(PinEntryActivity.this).restartApp(getIntent().getExtras());
                    finish();
                }
            } catch (MnemonicException.MnemonicLengthException mle) {
                mle.printStackTrace();
            } catch (DecoderException de) {
                de.printStackTrace();
            } finally {
                runOnUiThread(() -> {
                    progressBar.setVisibility(View.INVISIBLE);
                });
            }
        } else {
            runOnUiThread(() -> {
                progressBar.setVisibility(View.INVISIBLE);
                failures++;
                Toast.makeText(PinEntryActivity.this, PinEntryActivity.this.getText(R.string.login_error) + ":" + failures + "/3", Toast.LENGTH_SHORT).show();
                if (failures == 3) {
                    failures = 0;
                    doBackupRestore();
                } 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();
                }
            });
        }
        runOnUiThread(() -> {
            progressBar.setVisibility(View.INVISIBLE);
        });
    }).start();
}
Also used : DecoderException(org.apache.commons.codec.DecoderException) HD_Wallet(com.samourai.wallet.hd.HD_Wallet) CharSequenceX(com.samourai.wallet.util.CharSequenceX) Intent(android.content.Intent)

Aggregations

HD_Wallet (com.samourai.wallet.hd.HD_Wallet)16 IOException (java.io.IOException)7 MnemonicException (org.bitcoinj.crypto.MnemonicException)6 JSONException (org.json.JSONException)6 JSONObject (org.json.JSONObject)6 DecryptionException (com.samourai.wallet.crypto.DecryptionException)5 Intent (android.content.Intent)4 CharSequenceX (com.samourai.wallet.util.CharSequenceX)4 HD_Address (com.samourai.wallet.hd.HD_Address)3 DecoderException (org.apache.commons.codec.DecoderException)3 AlertDialog (android.app.AlertDialog)2 DialogInterface (android.content.DialogInterface)2 IntentFilter (android.content.IntentFilter)2 Bundle (android.os.Bundle)2 Handler (android.os.Handler)2 View (android.view.View)2 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 SegwitAddress (com.samourai.wallet.segwit.SegwitAddress)2 MyTransactionOutPoint (com.samourai.wallet.send.MyTransactionOutPoint)2