Search in sources :

Example 11 with DecoderException

use of org.apache.commons.codec.DecoderException 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();
                    }
                } 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 {
                ;
            }
            if (progress != null && progress.isShowing()) {
                progress.dismiss();
                progress = null;
            }
            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 12 with DecoderException

use of org.apache.commons.codec.DecoderException 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();
                    }
                    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();
                    }
                    progress = new ProgressDialog(PinEntryActivity.this);
                    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();
                            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 {
                                if (progress != null && progress.isShowing()) {
                                    progress.dismiss();
                                    progress = null;
                                }
                                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();
                                    }
                                }).show();
                            }
                            Looper.loop();
                        }
                    }).start();
                }
            }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) {
                    AppUtil.getInstance(PinEntryActivity.this).restartApp();
                }
            });
            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);
        startActivity(intent);
    }
}
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) ProgressDialog(android.app.ProgressDialog) 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) OnClickListener(android.view.View.OnClickListener) File(java.io.File) DecryptionException(com.samourai.wallet.crypto.DecryptionException)

Example 13 with DecoderException

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

the class BalanceActivity method restoreWatchOnly.

private void restoreWatchOnly() {
    final List<String> xpubList = SamouraiSentinel.getInstance(BalanceActivity.this).getAllAddrsSorted();
    final Handler handler = new Handler();
    if (progress != null && progress.isShowing()) {
        progress.dismiss();
        progress = null;
    }
    progress = new ProgressDialog(BalanceActivity.this);
    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();
            List<String> _xpubs = new ArrayList<String>();
            for (String xpub : xpubList) {
                if (xpub.startsWith("xpub") || xpub.startsWith("ypub")) {
                    _xpubs.add(xpub);
                }
            }
            if (_xpubs.size() > 0) {
                try {
                    String xpubs = StringUtils.join(_xpubs.toArray(new String[_xpubs.size()]), ":");
                    // Log.i("BalanceActivity", xpubs);
                    if (_xpubs.size() > 0) {
                        HD_Wallet hdw = HD_WalletFactory.getInstance(BalanceActivity.this).restoreWallet(xpubs, null, 1);
                        if (hdw != null) {
                            List<HD_Account> accounts = hdw.getAccounts();
                            for (int i = 0; i < accounts.size(); i++) {
                                AddressFactory.getInstance().account2xpub().put(i, _xpubs.get(i));
                                AddressFactory.getInstance().xpub2account().put(_xpubs.get(i), i);
                            }
                        }
                    }
                } catch (DecoderException de) {
                    PrefsUtil.getInstance(BalanceActivity.this).clear();
                    Toast.makeText(BalanceActivity.this, R.string.xpub_error, Toast.LENGTH_SHORT).show();
                    de.printStackTrace();
                } catch (AddressFormatException afe) {
                    PrefsUtil.getInstance(BalanceActivity.this).clear();
                    Toast.makeText(BalanceActivity.this, R.string.xpub_error, Toast.LENGTH_SHORT).show();
                    afe.printStackTrace();
                } finally {
                    ;
                }
            }
            if (progress != null && progress.isShowing()) {
                progress.dismiss();
                progress = null;
            }
            handler.post(new Runnable() {

                @Override
                public void run() {
                    if (xpubList.size() == 1) {
                        account_selections = new String[1];
                        if (xpubList.get(0).startsWith("xpub") && SamouraiSentinel.getInstance(BalanceActivity.this).getXPUBs().containsKey(xpubList.get(0))) {
                            account_selections[0] = SamouraiSentinel.getInstance(BalanceActivity.this).getXPUBs().get(xpubList.get(0));
                        } else if ((xpubList.get(0).startsWith("xpub") || xpubList.get(0).startsWith("ypub")) && SamouraiSentinel.getInstance(BalanceActivity.this).getBIP49().containsKey(xpubList.get(0))) {
                            account_selections[0] = SamouraiSentinel.getInstance(BalanceActivity.this).getBIP49().get(xpubList.get(0));
                        } else {
                            account_selections[0] = SamouraiSentinel.getInstance(BalanceActivity.this).getLegacy().get(xpubList.get(0));
                        }
                    } else {
                        account_selections = new String[xpubList.size() + 1];
                        account_selections[0] = BalanceActivity.this.getString(R.string.total_title);
                        for (int i = 0; i < xpubList.size(); i++) {
                            if (xpubList.get(i).startsWith("xpub") && SamouraiSentinel.getInstance(BalanceActivity.this).getXPUBs().containsKey(xpubList.get(i))) {
                                account_selections[i + 1] = SamouraiSentinel.getInstance(BalanceActivity.this).getXPUBs().get(xpubList.get(i));
                            } else if ((xpubList.get(i).startsWith("xpub") || xpubList.get(i).startsWith("ypub")) && SamouraiSentinel.getInstance(BalanceActivity.this).getBIP49().containsKey(xpubList.get(i))) {
                                account_selections[i + 1] = SamouraiSentinel.getInstance(BalanceActivity.this).getBIP49().get(xpubList.get(i));
                            } else {
                                account_selections[i + 1] = SamouraiSentinel.getInstance(BalanceActivity.this).getLegacy().get(xpubList.get(i));
                            }
                        }
                    }
                    adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, account_selections);
                    getActionBar().setListNavigationCallbacks(adapter, navigationListener);
                    adapter.notifyDataSetChanged();
                    if (account_selections.length == 1) {
                        SamouraiSentinel.getInstance(BalanceActivity.this).setCurrentSelectedAccount(0);
                    }
                    refreshTx(false);
                    try {
                        SamouraiSentinel.getInstance(BalanceActivity.this).serialize(SamouraiSentinel.getInstance(BalanceActivity.this).toJSON(), null);
                    } catch (IOException ioe) {
                        ;
                    } catch (JSONException je) {
                        ;
                    }
                }
            });
            Looper.loop();
        }
    }).start();
}
Also used : AddressFormatException(org.bitcoinj.core.AddressFormatException) HD_Wallet(com.samourai.sentinel.hd.HD_Wallet) ArrayList(java.util.ArrayList) Handler(android.os.Handler) JSONException(org.json.JSONException) IOException(java.io.IOException) ProgressDialog(android.app.ProgressDialog) DecoderException(org.apache.commons.codec.DecoderException) HD_Account(com.samourai.sentinel.hd.HD_Account) ArrayAdapter(android.widget.ArrayAdapter)

Example 14 with DecoderException

use of org.apache.commons.codec.DecoderException in project accumulo by apache.

the class Tablet method getCompactionID.

public Pair<Long, UserCompactionConfig> getCompactionID() throws NoNodeException {
    try {
        String zTablePath = Constants.ZROOT + "/" + tabletServer.getInstance().getInstanceID() + Constants.ZTABLES + "/" + extent.getTableId() + Constants.ZTABLE_COMPACT_ID;
        String[] tokens = new String(ZooReaderWriter.getInstance().getData(zTablePath, null), UTF_8).split(",");
        long compactID = Long.parseLong(tokens[0]);
        UserCompactionConfig compactionConfig = new UserCompactionConfig();
        if (tokens.length > 1) {
            Hex hex = new Hex();
            ByteArrayInputStream bais = new ByteArrayInputStream(hex.decode(tokens[1].split("=")[1].getBytes(UTF_8)));
            DataInputStream dis = new DataInputStream(bais);
            try {
                compactionConfig.readFields(dis);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            KeyExtent ke = new KeyExtent(extent.getTableId(), compactionConfig.getEndRow(), compactionConfig.getStartRow());
            if (!ke.overlaps(extent)) {
                // only use iterators if compaction range overlaps
                compactionConfig = new UserCompactionConfig();
            }
        }
        return new Pair<>(compactID, compactionConfig);
    } catch (InterruptedException | DecoderException | NumberFormatException e) {
        throw new RuntimeException(e);
    } catch (KeeperException ke) {
        if (ke instanceof NoNodeException) {
            throw (NoNodeException) ke;
        } else {
            throw new RuntimeException(ke);
        }
    }
}
Also used : NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) UserCompactionConfig(org.apache.accumulo.server.master.tableOps.UserCompactionConfig) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) KeyExtent(org.apache.accumulo.core.data.impl.KeyExtent) IterationInterruptedException(org.apache.accumulo.core.iterators.IterationInterruptedException) DecoderException(org.apache.commons.codec.DecoderException) ByteArrayInputStream(java.io.ByteArrayInputStream) Hex(org.apache.commons.codec.binary.Hex) KeeperException(org.apache.zookeeper.KeeperException) Pair(org.apache.accumulo.core.util.Pair)

Example 15 with DecoderException

use of org.apache.commons.codec.DecoderException in project MIDIBridge by abudaan.

the class QuotedPrintableCodec method decodeQuotedPrintable.

/**
 * Decodes an array quoted-printable characters into an array of original bytes. Escaped characters are converted
 * back to their original representation.
 *
 * <p>
 * This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in
 * RFC 1521.
 * </p>
 *
 * @param bytes
 *                  array of quoted-printable characters
 * @return array of original bytes
 * @throws DecoderException
 *                  Thrown if quoted-printable decoding is unsuccessful
 */
public static final byte[] decodeQuotedPrintable(byte[] bytes) throws DecoderException {
    if (bytes == null) {
        return null;
    }
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    for (int i = 0; i < bytes.length; i++) {
        int b = bytes[i];
        if (b == ESCAPE_CHAR) {
            try {
                int u = Utils.digit16(bytes[++i]);
                int l = Utils.digit16(bytes[++i]);
                buffer.write((char) ((u << 4) + l));
            } catch (ArrayIndexOutOfBoundsException e) {
                throw new DecoderException("Invalid quoted-printable encoding", e);
            }
        } else {
            buffer.write(b);
        }
    }
    return buffer.toByteArray();
}
Also used : DecoderException(org.apache.commons.codec.DecoderException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

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