Search in sources :

Example 1 with HD_Account

use of com.samourai.sentinel.hd.HD_Account 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") || xpub.startsWith("zpub") || xpub.startsWith("tpub") || xpub.startsWith("upub") || xpub.startsWith("vpub")) {
                    _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") || xpubList.get(0).startsWith("tpub")) && 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") || xpubList.get(0).startsWith("tpub") || xpubList.get(0).startsWith("upub")) && SamouraiSentinel.getInstance(BalanceActivity.this).getBIP49().containsKey(xpubList.get(0))) {
                            account_selections[0] = SamouraiSentinel.getInstance(BalanceActivity.this).getBIP49().get(xpubList.get(0));
                        } else if ((xpubList.get(0).startsWith("zpub") || xpubList.get(0).startsWith("vpub")) && SamouraiSentinel.getInstance(BalanceActivity.this).getBIP84().containsKey(xpubList.get(0))) {
                            account_selections[0] = SamouraiSentinel.getInstance(BalanceActivity.this).getBIP84().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") || xpubList.get(i).startsWith("tpub")) && 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") || xpubList.get(i).startsWith("tpub") || xpubList.get(i).startsWith("upub")) && SamouraiSentinel.getInstance(BalanceActivity.this).getBIP49().containsKey(xpubList.get(i))) {
                                account_selections[i + 1] = SamouraiSentinel.getInstance(BalanceActivity.this).getBIP49().get(xpubList.get(i));
                            } else if ((xpubList.get(i).startsWith("zpub") || xpubList.get(i).startsWith("vpub")) && SamouraiSentinel.getInstance(BalanceActivity.this).getBIP84().containsKey(xpubList.get(i))) {
                                account_selections[i + 1] = SamouraiSentinel.getInstance(BalanceActivity.this).getBIP84().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)

Aggregations

ProgressDialog (android.app.ProgressDialog)1 Handler (android.os.Handler)1 ArrayAdapter (android.widget.ArrayAdapter)1 HD_Account (com.samourai.sentinel.hd.HD_Account)1 HD_Wallet (com.samourai.sentinel.hd.HD_Wallet)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 DecoderException (org.apache.commons.codec.DecoderException)1 AddressFormatException (org.bitcoinj.core.AddressFormatException)1 JSONException (org.json.JSONException)1