Search in sources :

Example 6 with HD_Wallet

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

the class AddressFactory method get.

public HD_Address get(int chain) {
    int idx = 0;
    HD_Address addr = null;
    try {
        HD_Wallet hdw = HD_WalletFactory.getInstance(context).get();
        if (hdw != null) {
            idx = HD_WalletFactory.getInstance(context).get().getAccount(SamouraiWallet.SAMOURAI_ACCOUNT).getChain(chain).getAddrIdx();
            addr = HD_WalletFactory.getInstance(context).get().getAccount(SamouraiWallet.SAMOURAI_ACCOUNT).getChain(chain).getAddressAt(idx);
            if (chain == RECEIVE_CHAIN && canIncReceiveAddress(SamouraiWallet.SAMOURAI_ACCOUNT)) {
                HD_WalletFactory.getInstance(context).get().getAccount(SamouraiWallet.SAMOURAI_ACCOUNT).getChain(chain).incAddrIdx();
            // PayloadUtil.getInstance(context).saveWalletToJSON(new CharSequenceX(AccessFactory.getInstance(context).getGUID() + AccessFactory.getInstance(context).getPIN()));
            }
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
        Toast.makeText(context, "HD wallet error", Toast.LENGTH_SHORT).show();
    } catch (MnemonicException.MnemonicLengthException mle) {
        mle.printStackTrace();
        Toast.makeText(context, "HD wallet error", Toast.LENGTH_SHORT).show();
    }
    return addr;
}
Also used : HD_Wallet(com.samourai.wallet.hd.HD_Wallet) MnemonicException(org.bitcoinj.crypto.MnemonicException) HD_Address(com.samourai.wallet.hd.HD_Address) IOException(java.io.IOException)

Example 7 with HD_Wallet

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

the class AppUtil method wipeApp.

public void wipeApp() {
    try {
        HD_Wallet hdw = HD_WalletFactory.getInstance(context).get();
        String[] s = hdw.getXPUBs();
        for (int i = 0; i < s.length; i++) {
        // APIFactory.getInstance(context).deleteXPUB(s[i], false);
        }
        String _s = BIP49Util.getInstance(context).getWallet().getAccount(0).ypubstr();
        // APIFactory.getInstance(context).deleteXPUB(_s, true);
        PayloadUtil.getInstance(context).wipe();
    } catch (Exception e) {
        e.printStackTrace();
    }
    deleteBackup();
    deleteQR();
    final ComponentName component = new ComponentName(context.getApplicationContext().getPackageName(), "com.samourai.wallet.MainActivity");
    try {
        context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
        PrefsUtil.getInstance(context).setValue(PrefsUtil.ICON_HIDDEN, false);
    } catch (IllegalArgumentException iae) {
        ;
    }
    APIFactory.getInstance(context).setXpubBalance(0L);
    APIFactory.getInstance(context).reset();
    PrefsUtil.getInstance(context).clear();
    BlockedUTXO.getInstance().clear();
    BlockedUTXO.getInstance().clearPostMix();
    RicochetMeta.getInstance(context).empty();
    SendAddressUtil.getInstance().reset();
    SentToFromBIP47Util.getInstance().reset();
    BatchSendUtil.getInstance().clear();
    AccessFactory.getInstance(context).setIsLoggedIn(false);
    TrustedNodeUtil.getInstance().reset();
    WhirlpoolMeta.getInstance(context).setSCODE(null);
}
Also used : HD_Wallet(com.samourai.wallet.hd.HD_Wallet) ComponentName(android.content.ComponentName)

Example 8 with HD_Wallet

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

the class PayloadUtil method restoreWalletfromJSON.

public synchronized HD_Wallet restoreWalletfromJSON(JSONObject obj) throws DecoderException, MnemonicException.MnemonicLengthException {
    // Log.i("PayloadUtil", obj.toString());
    HD_Wallet hdw = null;
    NetworkParameters params = SamouraiWallet.getInstance().getCurrentNetworkParams();
    JSONObject wallet = null;
    JSONObject meta = null;
    try {
        if (obj.has("wallet")) {
            wallet = obj.getJSONObject("wallet");
        } else {
            wallet = obj;
        }
        if (obj.has("meta")) {
            meta = obj.getJSONObject("meta");
        } else {
            meta = obj;
        }
    } catch (JSONException je) {
        ;
    }
    try {
        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
        // Log.i("PayloadUtil", obj.toString());
        if (wallet != null) {
            if (wallet.has("testnet")) {
                SamouraiWallet.getInstance().setCurrentNetworkParams(wallet.getBoolean("testnet") ? TestNet3Params.get() : MainNetParams.get());
                PrefsUtil.getInstance(context).setValue(PrefsUtil.TESTNET, wallet.getBoolean("testnet"));
            } else {
                SamouraiWallet.getInstance().setCurrentNetworkParams(MainNetParams.get());
                PrefsUtil.getInstance(context).removeValue(PrefsUtil.TESTNET);
            }
            hdw = newHDWallet(context, 44, wallet, params);
            hdw.getAccount(SamouraiWallet.SAMOURAI_ACCOUNT).getReceive().setAddrIdx(wallet.has("receiveIdx") ? wallet.getInt("receiveIdx") : 0);
            hdw.getAccount(SamouraiWallet.SAMOURAI_ACCOUNT).getChange().setAddrIdx(wallet.has("changeIdx") ? wallet.getInt("changeIdx") : 0);
            if (wallet.has("accounts")) {
                JSONArray accounts = wallet.getJSONArray("accounts");
                JSONObject account = accounts.getJSONObject(0);
                hdw.getAccount(0).getReceive().setAddrIdx(account.has("receiveIdx") ? account.getInt("receiveIdx") : 0);
                hdw.getAccount(0).getChange().setAddrIdx(account.has("changeIdx") ? account.getInt("changeIdx") : 0);
                AddressFactory.getInstance().account2xpub().put(0, hdw.getAccount(0).xpubstr());
                AddressFactory.getInstance().xpub2account().put(hdw.getAccount(0).xpubstr(), 0);
            }
        }
        if (meta != null) {
            if (meta.has("prev_balance")) {
                APIFactory.getInstance(context).setXpubBalance(meta.getLong("prev_balance"));
            }
            if (meta.has("use_segwit")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.USE_SEGWIT, meta.getBoolean("use_segwit"));
                editor.putBoolean("segwit", meta.getBoolean("use_segwit"));
                editor.commit();
            }
            if (meta.has("use_like_typed_change")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.USE_LIKE_TYPED_CHANGE, meta.getBoolean("use_like_typed_change"));
                editor.putBoolean("likeTypedChange", meta.getBoolean("use_like_typed_change"));
                editor.commit();
            }
            if (meta.has("spend_type")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.SPEND_TYPE, meta.getInt("spend_type"));
                editor.putBoolean("boltzmann", meta.getInt("spend_type") == SendActivity.SPEND_BOLTZMANN ? true : false);
                editor.commit();
            }
            // 
            if (meta.has("rbf_opt_in")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.RBF_OPT_IN, meta.getBoolean("rbf_opt_in"));
                editor.putBoolean("rbf", meta.getBoolean("rbf_opt_in") ? true : false);
                editor.commit();
            }
            if (meta.has("use_ricochet")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.USE_RICOCHET, meta.getBoolean("use_ricochet"));
            }
            if (meta.has("ricochet_staggered_delivery")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.RICOCHET_STAGGERED, meta.getBoolean("ricochet_staggered_delivery"));
            }
            if (meta.has("sent_tos")) {
                SendAddressUtil.getInstance().fromJSON((JSONArray) meta.get("sent_tos"));
            }
            if (meta.has("sent_tos_from_bip47")) {
                SentToFromBIP47Util.getInstance().fromJSON((JSONArray) meta.get("sent_tos_from_bip47"));
            }
            if (meta.has("batch_send")) {
                BatchSendUtil.getInstance().fromJSON((JSONArray) meta.get("batch_send"));
            }
            if (meta.has("bip47")) {
                try {
                    BIP47Meta.getInstance().fromJSON((JSONObject) meta.get("bip47"));
                } catch (ClassCastException cce) {
                    JSONArray _array = (JSONArray) meta.get("bip47");
                    JSONObject _obj = new JSONObject();
                    _obj.put("pcodes", _array);
                    BIP47Meta.getInstance().fromJSON(_obj);
                }
            }
            if (meta.has("pin")) {
                AccessFactory.getInstance().setPIN((String) meta.get("pin"));
            }
            if (meta.has("pin2")) {
                AccessFactory.getInstance().setPIN2((String) meta.get("pin2"));
            }
            if (meta.has("ricochet")) {
                RicochetMeta.getInstance(context).fromJSON((JSONObject) meta.get("ricochet"));
            }
            if (meta.has("cahoots")) {
                CahootsFactory.getInstance().fromJSON((JSONArray) meta.get("cahoots"));
            }
            if (meta.has("trusted_node")) {
                TrustedNodeUtil.getInstance().fromJSON((JSONObject) meta.get("trusted_node"));
            }
            if (meta.has("rbfs")) {
                RBFUtil.getInstance().fromJSON((JSONArray) meta.get("rbfs"));
            }
            if (meta.has("tor")) {
                TorManager.getInstance(context).fromJSON((JSONObject) meta.get("tor"));
            }
            if (meta.has("blocked_utxos")) {
                BlockedUTXO.getInstance().fromJSON((JSONObject) meta.get("blocked_utxos"));
            }
            if (meta.has("utxo_tags")) {
                UTXOUtil.getInstance().fromJSON((JSONArray) meta.get("utxo_tags"));
            }
            if (meta.has("utxo_notes")) {
                UTXOUtil.getInstance().fromJSON_notes((JSONArray) meta.get("utxo_notes"));
            }
            if (meta.has("utxo_scores")) {
                UTXOUtil.getInstance().fromJSON_scores((JSONArray) meta.get("utxo_scores"));
            }
            if (meta.has("whirlpool")) {
                WhirlpoolMeta.getInstance(context).fromJSON((JSONObject) meta.get("whirlpool"));
            }
            if (meta.has("tx0_display")) {
                Tx0DisplayUtil.getInstance().fromJSON((JSONArray) meta.get("tx0_display"));
            }
            if (meta.has("trusted_no")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.ALERT_MOBILE_NO, (String) meta.get("trusted_no"));
                editor.putString("alertSMSNo", meta.getString("trusted_no"));
                editor.commit();
            }
            if (meta.has("scramble_pin")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.SCRAMBLE_PIN, meta.getBoolean("scramble_pin"));
                editor.putBoolean("scramblePin", meta.getBoolean("scramble_pin"));
                editor.commit();
            }
            if (meta.has("haptic_pin")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.HAPTIC_PIN, meta.getBoolean("haptic_pin"));
                editor.putBoolean("haptic", meta.getBoolean("haptic_pin"));
                editor.commit();
            }
            if (meta.has("auto_backup")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.AUTO_BACKUP, meta.getBoolean("auto_backup"));
                editor.putBoolean("auto_backup", meta.getBoolean("auto_backup"));
                editor.commit();
            }
            if (meta.has("remote")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.ACCEPT_REMOTE, meta.getBoolean("remote"));
                editor.putBoolean("stealthRemote", meta.getBoolean("remote"));
                editor.commit();
            }
            if (meta.has("use_trusted")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.TRUSTED_LOCK, meta.getBoolean("use_trusted"));
                editor.putBoolean("trustedLock", meta.getBoolean("use_trusted"));
                editor.commit();
            }
            if (meta.has("check_sim")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.CHECK_SIM, meta.getBoolean("check_sim"));
                editor.putBoolean("sim_switch", meta.getBoolean("check_sim"));
                editor.commit();
                if (meta.getBoolean("check_sim")) {
                    SIMUtil.getInstance(context).setStoredSIM();
                }
            }
            if (meta.has("use_trusted_node")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.USE_TRUSTED_NODE, meta.getBoolean("use_trusted_node"));
            }
            if (meta.has("fee_provider_sel")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.FEE_PROVIDER_SEL, 0);
            }
            if (meta.has("broadcast_tx")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.BROADCAST_TX, meta.getBoolean("broadcast_tx"));
            }
            if (meta.has("xpubreg44")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.XPUB44REG, meta.getBoolean("xpubreg44"));
            }
            if (meta.has("xpubreg49")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.XPUB49REG, meta.getBoolean("xpubreg49"));
            }
            if (meta.has("xpubreg84")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.XPUB84REG, meta.getBoolean("xpubreg84"));
            }
            if (meta.has("xpublock44")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.XPUB44LOCK, meta.getBoolean("xpublock44"));
            }
            if (meta.has("xpublock49")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.XPUB49LOCK, meta.getBoolean("xpublock49"));
            }
            if (meta.has("xpublock84")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.XPUB84LOCK, meta.getBoolean("xpublock84"));
            }
            if (meta.has("xpubprelock")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.XPUBPRELOCK, meta.getBoolean("xpubprelock"));
            }
            if (meta.has("xpubpostlock")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.XPUBPOSTLOCK, meta.getBoolean("xpubpostlock"));
            }
            if (meta.has("paynym_claimed")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.PAYNYM_CLAIMED, meta.getBoolean("paynym_claimed"));
            }
            if (meta.has("paynym_refused")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.PAYNYM_REFUSED, meta.getBoolean("paynym_refused"));
            }
            if (meta.has("paynym_featured_v1")) {
                PrefsUtil.getInstance(context).setValue(PrefsUtil.PAYNYM_FEATURED_SEGWIT, meta.getBoolean("paynym_featured_v1"));
            }
            if (meta.has("user_offline")) {
                AppUtil.getInstance(context).setUserOfflineMode(meta.getBoolean("user_offline"));
            }
            if (meta.has("dojo")) {
                DojoUtil.getInstance(context).fromJSON(meta.getJSONObject("dojo"));
            }
        /*
                if(obj.has("passphrase")) {
                    Log.i("PayloadUtil", (String)obj.get("passphrase"));
                    Toast.makeText(context, "'" + (String)obj.get("passphrase") + "'", Toast.LENGTH_SHORT).show();
                }
                Toast.makeText(context, "'" + hdw.getPassphrase() + "'", Toast.LENGTH_SHORT).show();
                */
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (JSONException je) {
        je.printStackTrace();
    }
    HD_WalletFactory.getInstance(context).getWallets().clear();
    HD_WalletFactory.getInstance(context).getWallets().add(hdw);
    return hdw;
}
Also used : HD_Wallet(com.samourai.wallet.hd.HD_Wallet) JSONObject(org.json.JSONObject) SharedPreferences(android.content.SharedPreferences) NetworkParameters(org.bitcoinj.core.NetworkParameters) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException)

Example 9 with HD_Wallet

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

the class PayloadUtil method newHDWallet.

private HD_Wallet newHDWallet(Context ctx, int purpose, JSONObject jsonobj, NetworkParameters params) throws JSONException, DecoderException, MnemonicException.MnemonicLengthException, IOException {
    byte[] seed = org.apache.commons.codec.binary.Hex.decodeHex(((String) jsonobj.get("seed")).toCharArray());
    String strPassphrase = jsonobj.getString("passphrase");
    MnemonicCode mc = computeMnemonicCode(ctx);
    return new HD_Wallet(purpose, mc, params, seed, strPassphrase, SamouraiWallet.NB_ACCOUNTS);
}
Also used : HD_Wallet(com.samourai.wallet.hd.HD_Wallet) MnemonicCode(org.bitcoinj.crypto.MnemonicCode)

Example 10 with HD_Wallet

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

the class AndroidWhirlpoolWalletService method getOrOpenWhirlpoolWallet.

private WhirlpoolWallet getOrOpenWhirlpoolWallet(Context ctx) throws Exception {
    Optional<WhirlpoolWallet> whirlpoolWalletOpt = getWhirlpoolWallet();
    if (!whirlpoolWalletOpt.isPresent()) {
        // wallet closed => open WhirlpoolWallet
        HD_Wallet bip84w = BIP84Util.getInstance(ctx).getWallet();
        String walletIdentifier = whirlpoolUtils.computeWalletIdentifier(bip84w);
        WhirlpoolWalletConfig config = computeWhirlpoolWalletConfig(ctx, walletIdentifier);
        APIFactory apiFactory = APIFactory.getInstance(ctx);
        WhirlpoolDataService dataService = newDataService(config, apiFactory);
        return openWallet(config, dataService, bip84w);
    }
    // wallet already opened
    return whirlpoolWalletOpt.get();
}
Also used : APIFactory(com.samourai.wallet.api.APIFactory) HD_Wallet(com.samourai.wallet.hd.HD_Wallet)

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