Search in sources :

Example 31 with DecoderException

use of org.apache.commons.codec.DecoderException in project samourai-wallet-android by Samourai-Wallet.

the class PinEntryActivity method validateThread.

private void validateThread(final String pin, final String uri) {
    final ProgressDialog progress = new ProgressDialog(PinEntryActivity.this);
    if (progress != null && progress.isShowing()) {
        progress.dismiss();
    }
    progress.setCancelable(false);
    progress.setTitle(R.string.app_name);
    progress.setMessage(getString(R.string.please_wait));
    progress.show();
    new Thread(new Runnable() {

        @Override
        public void run() {
            Looper.prepare();
            if (pin.length() < AccessFactory.MIN_PIN_LENGTH || pin.length() > AccessFactory.MAX_PIN_LENGTH) {
                if (progress != null && progress.isShowing()) {
                    progress.dismiss();
                }
                Toast.makeText(PinEntryActivity.this, R.string.pin_error, Toast.LENGTH_SHORT).show();
                AppUtil.getInstance(PinEntryActivity.this).restartApp();
            }
            String randomKey = AccessFactory.getInstance(PinEntryActivity.this).getGUID();
            if (randomKey.length() < 1) {
                if (progress != null && progress.isShowing()) {
                    progress.dismiss();
                }
                Toast.makeText(PinEntryActivity.this, R.string.random_key_error, Toast.LENGTH_SHORT).show();
                AppUtil.getInstance(PinEntryActivity.this).restartApp();
            }
            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));
                    if (progress != null && progress.isShowing()) {
                        progress.dismiss();
                    }
                    if (hdw == null) {
                        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);
                        }
                    }
                    AccessFactory.getInstance(PinEntryActivity.this).setIsLoggedIn(true);
                    TimeOutUtil.getInstance().updatePin();
                    if (isOpenDime) {
                        Intent intent = new Intent(PinEntryActivity.this, OpenDimeActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    } else if (uri != null) {
                        Log.i("PinEntryActivity", "uri to restartApp()");
                        AppUtil.getInstance(PinEntryActivity.this).restartApp("uri", uri);
                    } else {
                        AppUtil.getInstance(PinEntryActivity.this).restartApp();
                    }
                } catch (MnemonicException.MnemonicLengthException mle) {
                    mle.printStackTrace();
                } catch (DecoderException de) {
                    de.printStackTrace();
                } finally {
                    if (progress != null && progress.isShowing()) {
                        progress.dismiss();
                    }
                }
            } else {
                if (progress != null && progress.isShowing()) {
                    progress.dismiss();
                }
                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);
                }
            }
            if (progress != null && progress.isShowing()) {
                progress.dismiss();
            }
            Looper.loop();
        }
    }).start();
}
Also used : DecoderException(org.apache.commons.codec.DecoderException) HD_Wallet(com.samourai.wallet.hd.HD_Wallet) MnemonicException(org.bitcoinj.crypto.MnemonicException) CharSequenceX(com.samourai.wallet.util.CharSequenceX) Intent(android.content.Intent) ProgressDialog(android.app.ProgressDialog)

Example 32 with DecoderException

use of org.apache.commons.codec.DecoderException in project samourai-wallet-android by Samourai-Wallet.

the class RestoreSeedWalletActivity method RestoreWalletFromSamouraiBackup.

private void RestoreWalletFromSamouraiBackup(final String decrypted) {
    toggleLoading();
    new Thread(new Runnable() {

        @Override
        public void run() {
            Looper.prepare();
            try {
                JSONObject json = new JSONObject(decrypted);
                HD_Wallet hdw = PayloadUtil.getInstance(RestoreSeedWalletActivity.this).restoreWalletfromJSON(json);
                HD_WalletFactory.getInstance(RestoreSeedWalletActivity.this).set(hdw);
                String guid = AccessFactory.getInstance(RestoreSeedWalletActivity.this).createGUID();
                String hash = AccessFactory.getInstance(RestoreSeedWalletActivity.this).getHash(guid, new CharSequenceX(AccessFactory.getInstance(RestoreSeedWalletActivity.this).getPIN()), AESUtil.DefaultPBKDF2Iterations);
                PrefsUtil.getInstance(RestoreSeedWalletActivity.this).setValue(PrefsUtil.ACCESS_HASH, hash);
                PrefsUtil.getInstance(RestoreSeedWalletActivity.this).setValue(PrefsUtil.ACCESS_HASH2, hash);
                PayloadUtil.getInstance(RestoreSeedWalletActivity.this).saveWalletToJSON(new CharSequenceX(guid + AccessFactory.getInstance().getPIN()));
            } catch (MnemonicException.MnemonicLengthException mle) {
                mle.printStackTrace();
                Toast.makeText(RestoreSeedWalletActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
            } catch (DecoderException de) {
                de.printStackTrace();
                Toast.makeText(RestoreSeedWalletActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
            } catch (JSONException je) {
                je.printStackTrace();
                Toast.makeText(RestoreSeedWalletActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
            } catch (IOException ioe) {
                ioe.printStackTrace();
                Toast.makeText(RestoreSeedWalletActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
            } catch (java.lang.NullPointerException npe) {
                npe.printStackTrace();
                Toast.makeText(RestoreSeedWalletActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
            } catch (DecryptionException de) {
                de.printStackTrace();
                Toast.makeText(RestoreSeedWalletActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
            } finally {
                AppUtil.getInstance(RestoreSeedWalletActivity.this).restartApp();
            }
            Looper.loop();
            toggleLoading();
        }
    }).start();
}
Also used : HD_Wallet(com.samourai.wallet.hd.HD_Wallet) CharSequenceX(com.samourai.wallet.util.CharSequenceX) JSONException(org.json.JSONException) IOException(java.io.IOException) DecoderException(org.apache.commons.codec.DecoderException) MnemonicException(org.bitcoinj.crypto.MnemonicException) JSONObject(org.json.JSONObject) DecryptionException(com.samourai.wallet.crypto.DecryptionException)

Example 33 with DecoderException

use of org.apache.commons.codec.DecoderException in project samourai-wallet-android by Samourai-Wallet.

the class RestoreSeedWalletActivity method RestoreFromMnemonic.

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

        @Override
        public void run() {
            Looper.prepare();
            String guid = AccessFactory.getInstance(RestoreSeedWalletActivity.this).createGUID();
            String hash = AccessFactory.getInstance(RestoreSeedWalletActivity.this).getHash(guid, new CharSequenceX(pin), AESUtil.DefaultPBKDF2Iterations);
            PrefsUtil.getInstance(RestoreSeedWalletActivity.this).setValue(PrefsUtil.ACCESS_HASH, hash);
            PrefsUtil.getInstance(RestoreSeedWalletActivity.this).setValue(PrefsUtil.ACCESS_HASH2, hash);
            if (create) {
                try {
                    HD_WalletFactory.getInstance(RestoreSeedWalletActivity.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(RestoreSeedWalletActivity.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(RestoreSeedWalletActivity.this).setValue(PrefsUtil.SCRAMBLE_PIN, true);
            try {
                String msg = null;
                if (HD_WalletFactory.getInstance(RestoreSeedWalletActivity.this).get() != null) {
                    if (create) {
                        msg = getString(R.string.wallet_created_ok);
                    } else {
                        msg = getString(R.string.wallet_restored_ok);
                    }
                    try {
                        AccessFactory.getInstance(RestoreSeedWalletActivity.this).setPIN(pin);
                        PayloadUtil.getInstance(RestoreSeedWalletActivity.this).saveWalletToJSON(new CharSequenceX(AccessFactory.getInstance(RestoreSeedWalletActivity.this).getGUID() + pin));
                        if (create) {
                            PrefsUtil.getInstance(RestoreSeedWalletActivity.this).setValue(PrefsUtil.WALLET_ORIGIN, "new");
                            PrefsUtil.getInstance(RestoreSeedWalletActivity.this).setValue(PrefsUtil.FIRST_RUN, true);
                        } else {
                            PrefsUtil.getInstance(RestoreSeedWalletActivity.this).setValue(PrefsUtil.WALLET_ORIGIN, "restored");
                            PrefsUtil.getInstance(RestoreSeedWalletActivity.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(RestoreSeedWalletActivity.this).get().getAccount(i).xpubstr());
                        AddressFactory.getInstance().xpub2account().put(HD_WalletFactory.getInstance(RestoreSeedWalletActivity.this).get().getAccount(i).xpubstr(), i);
                    }
                    // 
                    if (create) {
                        String seed = null;
                        try {
                            seed = HD_WalletFactory.getInstance(RestoreSeedWalletActivity.this).get().getMnemonic();
                        } catch (IOException ioe) {
                            ioe.printStackTrace();
                        } catch (MnemonicException.MnemonicLengthException mle) {
                            mle.printStackTrace();
                        }
                        Intent intent = new Intent(RestoreSeedWalletActivity.this, RecoveryWordsActivity.class);
                        intent.putExtra("BIP39_WORD_LIST", seed);
                        startActivity(intent);
                        finish();
                    } else {
                        AccessFactory.getInstance(RestoreSeedWalletActivity.this).setIsLoggedIn(true);
                        TimeOutUtil.getInstance().updatePin();
                        AppUtil.getInstance(RestoreSeedWalletActivity.this).restartApp();
                    }
                } else {
                    if (create) {
                        msg = getString(R.string.wallet_created_ko);
                    } else {
                        msg = getString(R.string.wallet_restored_ko);
                    }
                }
                Toast.makeText(RestoreSeedWalletActivity.this, msg, Toast.LENGTH_SHORT).show();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            } catch (MnemonicException.MnemonicLengthException mle) {
                mle.printStackTrace();
            } finally {
                ;
            }
            toggleLoading();
            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)

Example 34 with DecoderException

use of org.apache.commons.codec.DecoderException in project samourai-wallet-android by Samourai-Wallet.

the class LandingActivity method RestoreWalletFromSamouraiBackup.

private void RestoreWalletFromSamouraiBackup(final String decrypted) {
    toggleLoading();
    new Thread(new Runnable() {

        @Override
        public void run() {
            Looper.prepare();
            try {
                JSONObject json = new JSONObject(decrypted);
                HD_Wallet hdw = PayloadUtil.getInstance(LandingActivity.this).restoreWalletfromJSON(json);
                HD_WalletFactory.getInstance(LandingActivity.this).set(hdw);
                String guid = AccessFactory.getInstance(LandingActivity.this).createGUID();
                String hash = AccessFactory.getInstance(LandingActivity.this).getHash(guid, new CharSequenceX(AccessFactory.getInstance(LandingActivity.this).getPIN()), AESUtil.DefaultPBKDF2Iterations);
                PrefsUtil.getInstance(LandingActivity.this).setValue(PrefsUtil.ACCESS_HASH, hash);
                PrefsUtil.getInstance(LandingActivity.this).setValue(PrefsUtil.ACCESS_HASH2, hash);
                PayloadUtil.getInstance(LandingActivity.this).saveWalletToJSON(new CharSequenceX(guid + AccessFactory.getInstance().getPIN()));
            } catch (MnemonicException.MnemonicLengthException mle) {
                mle.printStackTrace();
                Toast.makeText(LandingActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
            } catch (DecoderException de) {
                de.printStackTrace();
                Toast.makeText(LandingActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
            } catch (JSONException je) {
                je.printStackTrace();
                Toast.makeText(LandingActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
            } catch (IOException ioe) {
                ioe.printStackTrace();
                Toast.makeText(LandingActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
            } catch (java.lang.NullPointerException npe) {
                npe.printStackTrace();
                Toast.makeText(LandingActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
            } catch (DecryptionException de) {
                de.printStackTrace();
                Toast.makeText(LandingActivity.this, R.string.decryption_error, Toast.LENGTH_SHORT).show();
            } finally {
                AppUtil.getInstance(LandingActivity.this).restartApp();
            }
            Looper.loop();
            toggleLoading();
        }
    }).start();
}
Also used : HD_Wallet(com.samourai.wallet.hd.HD_Wallet) CharSequenceX(com.samourai.wallet.util.CharSequenceX) JSONException(org.json.JSONException) IOException(java.io.IOException) DecoderException(org.apache.commons.codec.DecoderException) MnemonicException(org.bitcoinj.crypto.MnemonicException) JSONObject(org.json.JSONObject) DecryptionException(com.samourai.wallet.crypto.DecryptionException)

Example 35 with DecoderException

use of org.apache.commons.codec.DecoderException in project titan by thinkaurelius.

the class DistributedStoreManager method getRid.

/**
 * This method attempts to generate Rid in the following three ways, in order,
 * returning the value produced by the first successful attempt in the sequence.
 * <p/>
 * <ol>
 * <li>
 * If {@code config} contains {@see GraphDatabaseConfiguration#INSTANCE_RID_RAW_KEY},
 * then read it as a String value.  Convert the String returned into a char[] and
 * call {@code org.apache.commons.codec.binary.Hex#decodeHex on the char[]}.  The
 * byte[] returned by {@code decodeHex} is then returned as Rid.
 * </li>
 * <li>
 * If {@code config} contains {@see GraphDatabaseConfiguration#INSTANCE_RID_SHORT_KEY},
 * then read it as a short value.  Call {@see java.net.InetAddress#getLocalHost()},
 * and on its return value call {@see java.net.InetAddress#getAddress()} to retrieve
 * the machine's IP address in byte[] form.  The returned Rid is a byte[] containing
 * the localhost address bytes in its lower indices and the short value in its
 * penultimate and final indices.
 * </li>
 * <li>
 * If both of the previous failed, then call
 * {@see java.lang.management.RuntimeMXBean#getName()} and then call
 * {@code String#getBytes()} on the returned value.  Return a Rid as described in the
 * previous point, replacing the short value with the byte[] representing the JVM name.
 * </li>
 * </ol>
 *
 * @param config commons config from which to read Rid-related keys
 * @return A byte array which should uniquely identify this machine
 */
public static byte[] getRid(Configuration config) {
    byte[] tentativeRid = null;
    if (config.containsKey(GraphDatabaseConfiguration.INSTANCE_RID_RAW_KEY)) {
        String ridText = config.getString(GraphDatabaseConfiguration.INSTANCE_RID_RAW_KEY);
        try {
            tentativeRid = Hex.decodeHex(ridText.toCharArray());
        } catch (DecoderException e) {
            throw new TitanConfigurationException("Could not decode hex value", e);
        }
        log.debug("Set rid from hex string: 0x{}", ridText);
    } else {
        final byte[] endBytes;
        if (config.containsKey(GraphDatabaseConfiguration.INSTANCE_RID_SHORT_KEY)) {
            short s = config.getShort(GraphDatabaseConfiguration.INSTANCE_RID_SHORT_KEY);
            endBytes = new byte[2];
            endBytes[0] = (byte) ((s & 0x0000FF00) >> 8);
            endBytes[1] = (byte) (s & 0x000000FF);
        } else {
            // endBytes = ManagementFactory.getRuntimeMXBean().getName().getBytes();
            endBytes = new StringBuilder(String.valueOf(Thread.currentThread().getId())).append("@").append(ManagementFactory.getRuntimeMXBean().getName()).toString().getBytes();
        }
        byte[] addrBytes;
        try {
            addrBytes = Inet4Address.getLocalHost().getAddress();
        } catch (UnknownHostException e) {
            throw new TitanConfigurationException("Unknown host specified", e);
        }
        tentativeRid = new byte[addrBytes.length + endBytes.length];
        System.arraycopy(addrBytes, 0, tentativeRid, 0, addrBytes.length);
        System.arraycopy(endBytes, 0, tentativeRid, addrBytes.length, endBytes.length);
        if (log.isDebugEnabled()) {
            log.debug("Set rid: 0x{}", new String(Hex.encodeHex(tentativeRid)));
        }
    }
    return tentativeRid;
}
Also used : DecoderException(org.apache.commons.codec.DecoderException) UnknownHostException(java.net.UnknownHostException) TitanConfigurationException(com.thinkaurelius.titan.core.TitanConfigurationException)

Aggregations

DecoderException (org.apache.commons.codec.DecoderException)41 IOException (java.io.IOException)16 CharSequenceX (com.samourai.wallet.util.CharSequenceX)8 MnemonicException (org.bitcoinj.crypto.MnemonicException)8 JSONException (org.json.JSONException)7 DecryptionException (com.samourai.wallet.crypto.DecryptionException)6 ArrayList (java.util.ArrayList)6 Intent (android.content.Intent)5 ProgressDialog (android.app.ProgressDialog)4 HD_Wallet (com.samourai.wallet.hd.HD_Wallet)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 HashMap (java.util.HashMap)4 List (java.util.List)4 BufferedReader (java.io.BufferedReader)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 ByteBuffer (java.nio.ByteBuffer)3 Map (java.util.Map)3 Cipher (javax.crypto.Cipher)3 AddressFormatException (org.bitcoinj.core.AddressFormatException)3