Search in sources :

Example 6 with PaymentCode

use of com.samourai.wallet.bip47.rpc.PaymentCode in project samourai-wallet-android by Samourai-Wallet.

the class BatchSendActivity method processPCode.

private void processPCode(String pcode, String meta) {
    if (FormatsUtil.getInstance().isValidPaymentCode(pcode)) {
        if (BIP47Meta.getInstance().getOutgoingStatus(pcode) == BIP47Meta.STATUS_SENT_CFM) {
            try {
                PaymentCode _pcode = new PaymentCode(pcode);
                PaymentAddress paymentAddress = BIP47Util.getInstance(BatchSendActivity.this).getSendAddress(_pcode, BIP47Meta.getInstance().getOutgoingIdx(pcode));
                if (BIP47Meta.getInstance().getSegwit(pcode)) {
                    SegwitAddress segwitAddress = new SegwitAddress(paymentAddress.getSendECKey(), SamouraiWallet.getInstance().getCurrentNetworkParams());
                    strDestinationBTCAddress = segwitAddress.getBech32AsString();
                } else {
                    strDestinationBTCAddress = paymentAddress.getSendECKey().toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString();
                }
                strPCode = _pcode.toString();
                edAddress.setText(BIP47Meta.getInstance().getDisplayLabel(strPCode));
                edAddress.setEnabled(false);
            } catch (Exception e) {
                Toast.makeText(BatchSendActivity.this, R.string.error_payment_code, Toast.LENGTH_SHORT).show();
            }
        } else {
            if (meta != null && meta.startsWith("?") && meta.length() > 1) {
                meta = meta.substring(1);
            }
            Intent intent = new Intent(BatchSendActivity.this, PayNymDetailsActivity.class);
            intent.putExtra("pcode", pcode);
            if (meta != null && meta.length() > 0) {
                if (meta.startsWith("?") && meta.length() > 1) {
                    meta = meta.substring(1);
                    if (meta.length() > 0) {
                        String _meta = null;
                        Map<String, String> map = new HashMap<String, String>();
                        meta.length();
                        try {
                            _meta = URLDecoder.decode(meta, "UTF-8");
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        map = Splitter.on('&').trimResults().withKeyValueSeparator("=").split(_meta);
                        intent.putExtra("label", map.containsKey("title") ? map.get("title").trim() : "");
                    }
                }
            }
            startActivity(intent);
        }
    } else {
        Toast.makeText(BatchSendActivity.this, R.string.invalid_payment_code, Toast.LENGTH_SHORT).show();
    }
}
Also used : PaymentCode(com.samourai.wallet.bip47.rpc.PaymentCode) SegwitAddress(com.samourai.wallet.segwit.SegwitAddress) HashMap(java.util.HashMap) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Intent(android.content.Intent) PaymentAddress(com.samourai.wallet.bip47.rpc.PaymentAddress) WriterException(com.google.zxing.WriterException) ParseException(java.text.ParseException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MnemonicException(org.bitcoinj.crypto.MnemonicException) IOException(java.io.IOException)

Example 7 with PaymentCode

use of com.samourai.wallet.bip47.rpc.PaymentCode in project samourai-wallet-android by Samourai-Wallet.

the class BIP47Meta method getUnspentAddresses.

public ArrayList<String> getUnspentAddresses(Context ctx, String pcode) {
    ArrayList<String> ret = new ArrayList<String>();
    try {
        ArrayList<Integer> idxs = getUnspent(pcode);
        if (idxs != null) {
            for (int i = 0; i < idxs.size(); i++) {
                info("BIP47Meta", "address has unspents:" + BIP47Util.getInstance(ctx).getReceivePubKey(new PaymentCode(pcode), idxs.get(i)));
                ret.add(BIP47Util.getInstance(ctx).getReceivePubKey(new PaymentCode(pcode), idxs.get(i)));
            }
        }
    } catch (Exception e) {
        ;
    }
    return ret;
}
Also used : PaymentCode(com.samourai.wallet.bip47.rpc.PaymentCode) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException)

Example 8 with PaymentCode

use of com.samourai.wallet.bip47.rpc.PaymentCode in project samourai-wallet-android by Samourai-Wallet.

the class BIP47Meta method getIncomingLookAhead.

public synchronized String[] getIncomingLookAhead(Context ctx) {
    Set<String> pcodes = pcodeIncomingIdxs.keySet();
    Iterator<String> it = pcodes.iterator();
    ArrayList<String> addrs = new ArrayList<String>();
    while (it.hasNext()) {
        String pcode = it.next();
        if (getArchived(pcode)) {
            continue;
        }
        int idx = getIncomingIdx(pcode);
        for (int i = idx; i < (idx + INCOMING_LOOKAHEAD); i++) {
            try {
                info("APIFactory", "receive from " + i + ":" + BIP47Util.getInstance(ctx).getReceivePubKey(new PaymentCode(pcode), i));
                BIP47Meta.getInstance().getIdx4AddrLookup().put(BIP47Util.getInstance(ctx).getReceivePubKey(new PaymentCode(pcode), i), i);
                BIP47Meta.getInstance().getPCode4AddrLookup().put(BIP47Util.getInstance(ctx).getReceivePubKey(new PaymentCode(pcode), i), pcode.toString());
                addrs.add(BIP47Util.getInstance(ctx).getReceivePubKey(new PaymentCode(pcode), i));
            } catch (Exception e) {
                ;
            }
        }
    }
    String[] s = addrs.toArray(new String[addrs.size()]);
    return s;
}
Also used : PaymentCode(com.samourai.wallet.bip47.rpc.PaymentCode) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException)

Example 9 with PaymentCode

use of com.samourai.wallet.bip47.rpc.PaymentCode in project samourai-wallet-android by Samourai-Wallet.

the class RefreshService method onHandleIntent.

@Override
protected void onHandleIntent(Intent intent) {
    // 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, FOREGROUND_SERVICE_CHANNEL_ID).setPriority(NotificationCompat.PRIORITY_LOW).setContentTitle("Updating Wallet...").setSmallIcon(R.drawable.ic_samourai_logo_trans2x).setOngoing(true).setCategory(Notification.CATEGORY_SERVICE).setAutoCancel(true);
        Notification notification = builder.build();
        startForeground(1001, notification);
    }
    dragged = intent.getBooleanExtra("dragged", false);
    launch = intent.getBooleanExtra("launch", false);
    notifTx = intent.getBooleanExtra("notifTx", false);
    APIFactory.getInstance(RefreshService.this).stayingAlive();
    APIFactory.getInstance(RefreshService.this).initWallet();
    try {
        int acc = 0;
        if (AddressFactory.getInstance().getHighestTxReceiveIdx(acc) > HD_WalletFactory.getInstance(RefreshService.this).get().getAccount(acc).getReceive().getAddrIdx()) {
            HD_WalletFactory.getInstance(RefreshService.this).get().getAccount(acc).getReceive().setAddrIdx(AddressFactory.getInstance().getHighestTxReceiveIdx(acc));
        }
        if (AddressFactory.getInstance().getHighestTxChangeIdx(acc) > HD_WalletFactory.getInstance(RefreshService.this).get().getAccount(acc).getChange().getAddrIdx()) {
            HD_WalletFactory.getInstance(RefreshService.this).get().getAccount(acc).getChange().setAddrIdx(AddressFactory.getInstance().getHighestTxChangeIdx(acc));
        }
        if (AddressFactory.getInstance().getHighestBIP49ReceiveIdx() > BIP49Util.getInstance(RefreshService.this).getWallet().getAccount(0).getReceive().getAddrIdx()) {
            BIP49Util.getInstance(RefreshService.this).getWallet().getAccount(0).getReceive().setAddrIdx(AddressFactory.getInstance().getHighestBIP49ReceiveIdx());
        }
        if (AddressFactory.getInstance().getHighestBIP49ChangeIdx() > BIP49Util.getInstance(RefreshService.this).getWallet().getAccount(0).getChange().getAddrIdx()) {
            BIP49Util.getInstance(RefreshService.this).getWallet().getAccount(0).getChange().setAddrIdx(AddressFactory.getInstance().getHighestBIP49ChangeIdx());
        }
        if (AddressFactory.getInstance().getHighestBIP84ReceiveIdx() > BIP84Util.getInstance(RefreshService.this).getWallet().getAccount(0).getReceive().getAddrIdx()) {
            BIP84Util.getInstance(RefreshService.this).getWallet().getAccount(0).getReceive().setAddrIdx(AddressFactory.getInstance().getHighestBIP84ReceiveIdx());
        }
        if (AddressFactory.getInstance().getHighestBIP84ChangeIdx() > BIP84Util.getInstance(RefreshService.this).getWallet().getAccount(0).getChange().getAddrIdx()) {
            BIP84Util.getInstance(RefreshService.this).getWallet().getAccount(0).getChange().setAddrIdx(AddressFactory.getInstance().getHighestBIP84ChangeIdx());
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (MnemonicException.MnemonicLengthException mle) {
        mle.printStackTrace();
    } catch (NullPointerException npe) {
        npe.printStackTrace();
    } finally {
        Intent _intent = new Intent("com.samourai.wallet.BalanceFragment.DISPLAY");
        LocalBroadcastManager.getInstance(RefreshService.this).sendBroadcast(_intent);
    }
    PrefsUtil.getInstance(RefreshService.this).setValue(PrefsUtil.FIRST_RUN, false);
    if (notifTx && !AppUtil.getInstance(RefreshService.this).isOfflineMode()) {
        // 
        try {
            PaymentCode pcode = BIP47Util.getInstance(RefreshService.this).getPaymentCode();
            // Log.i("BalanceFragment", "payment code:" + pcode.toString());
            // Log.i("BalanceFragment", "notification address:" + pcode.notificationAddress().getAddressString());
            APIFactory.getInstance(RefreshService.this).getNotifAddress(pcode.notificationAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).getAddressString());
        } catch (AddressFormatException afe) {
            afe.printStackTrace();
            Toast.makeText(RefreshService.this, "HD wallet error", Toast.LENGTH_SHORT).show();
        }
        // 
        // check on outgoing payment code notification tx
        // 
        List<Pair<String, String>> outgoingUnconfirmed = BIP47Meta.getInstance().getOutgoingUnconfirmed();
        // Log.i("BalanceFragment", "outgoingUnconfirmed:" + outgoingUnconfirmed.size());
        for (Pair<String, String> pair : outgoingUnconfirmed) {
            // Log.i("BalanceFragment", "outgoing payment code:" + pair.getLeft());
            // Log.i("BalanceFragment", "outgoing payment code tx:" + pair.getRight());
            int confirmations = APIFactory.getInstance(RefreshService.this).getNotifTxConfirmations(pair.getRight());
            if (confirmations > 0) {
                BIP47Meta.getInstance().setOutgoingStatus(pair.getLeft(), BIP47Meta.STATUS_SENT_CFM);
            }
            if (confirmations == -1) {
                BIP47Meta.getInstance().setOutgoingStatus(pair.getLeft(), BIP47Meta.STATUS_NOT_SENT);
            }
        }
        Intent _intent = new Intent("com.samourai.wallet.MainActivity2.RESTART_SERVICE");
        LocalBroadcastManager.getInstance(RefreshService.this).sendBroadcast(_intent);
    }
    if (launch) {
        if (PrefsUtil.getInstance(RefreshService.this).getValue(PrefsUtil.GUID_V, 0) < 4) {
            Log.i("RefreshService", "guid_v < 4");
            try {
                String _guid = AccessFactory.getInstance(RefreshService.this).createGUID();
                String _hash = AccessFactory.getInstance(RefreshService.this).getHash(_guid, new CharSequenceX(AccessFactory.getInstance(RefreshService.this).getPIN()), AESUtil.DefaultPBKDF2Iterations);
                PayloadUtil.getInstance(RefreshService.this).saveWalletToJSON(new CharSequenceX(_guid + AccessFactory.getInstance().getPIN()));
                PrefsUtil.getInstance(RefreshService.this).setValue(PrefsUtil.ACCESS_HASH, _hash);
                PrefsUtil.getInstance(RefreshService.this).setValue(PrefsUtil.ACCESS_HASH2, _hash);
                Log.i("RefreshService", "guid_v == 4");
            } catch (MnemonicException.MnemonicLengthException | IOException | JSONException | DecryptionException e) {
                ;
            }
        }
        if (PrefsUtil.getInstance(RefreshService.this).getValue(PrefsUtil.XPUB44LOCK, false) == false) {
            try {
                String[] s = HD_WalletFactory.getInstance(RefreshService.this).get().getXPUBs();
                APIFactory.getInstance(RefreshService.this).lockXPUB(s[0], 44, null);
            } catch (IOException | MnemonicException.MnemonicLengthException e) {
                ;
            }
        }
        if (PrefsUtil.getInstance(RefreshService.this).getValue(PrefsUtil.XPUB49LOCK, false) == false) {
            String ypub = BIP49Util.getInstance(RefreshService.this).getWallet().getAccount(0).ypubstr();
            APIFactory.getInstance(RefreshService.this).lockXPUB(ypub, 49, null);
        }
        if (PrefsUtil.getInstance(RefreshService.this).getValue(PrefsUtil.XPUB84LOCK, false) == false) {
            String zpub = BIP84Util.getInstance(RefreshService.this).getWallet().getAccount(0).zpubstr();
            APIFactory.getInstance(RefreshService.this).lockXPUB(zpub, 84, null);
        }
        if (PrefsUtil.getInstance(RefreshService.this).getValue(PrefsUtil.XPUBPRELOCK, false) == false) {
            String zpub = BIP84Util.getInstance(RefreshService.this).getWallet().getAccountAt(WhirlpoolMeta.getInstance(RefreshService.this).getWhirlpoolPremixAccount()).zpubstr();
            APIFactory.getInstance(RefreshService.this).lockXPUB(zpub, 84, PrefsUtil.XPUBPRELOCK);
        }
        if (PrefsUtil.getInstance(RefreshService.this).getValue(PrefsUtil.XPUBPOSTLOCK, false) == false) {
            String zpub = BIP84Util.getInstance(RefreshService.this).getWallet().getAccountAt(WhirlpoolMeta.getInstance(RefreshService.this).getWhirlpoolPostmix()).zpubstr();
            APIFactory.getInstance(RefreshService.this).lockXPUB(zpub, 84, PrefsUtil.XPUBPRELOCK);
        }
        if (PrefsUtil.getInstance(RefreshService.this).getValue(PrefsUtil.XPUBBADBANKLOCK, false) == false) {
            String zpub = BIP84Util.getInstance(RefreshService.this).getWallet().getAccountAt(WhirlpoolMeta.getInstance(RefreshService.this).getWhirlpoolBadBank()).zpubstr();
            APIFactory.getInstance(RefreshService.this).lockXPUB(zpub, 84, PrefsUtil.XPUBBADBANKLOCK);
        }
    } else {
        try {
            PayloadUtil.getInstance(RefreshService.this).saveWalletToJSON(new CharSequenceX(AccessFactory.getInstance(RefreshService.this).getGUID() + AccessFactory.getInstance(RefreshService.this).getPIN()));
        } catch (Exception e) {
            ;
        }
    }
    Intent _intent = new Intent("com.samourai.wallet.BalanceFragment.DISPLAY");
    LocalBroadcastManager.getInstance(RefreshService.this).sendBroadcast(_intent);
}
Also used : AddressFormatException(org.bitcoinj.core.AddressFormatException) PaymentCode(com.samourai.wallet.bip47.rpc.PaymentCode) CharSequenceX(com.samourai.wallet.util.CharSequenceX) JSONException(org.json.JSONException) Intent(android.content.Intent) IOException(java.io.IOException) Notification(android.app.Notification) MnemonicException(org.bitcoinj.crypto.MnemonicException) JSONException(org.json.JSONException) DecryptionException(com.samourai.wallet.crypto.DecryptionException) AddressFormatException(org.bitcoinj.core.AddressFormatException) IOException(java.io.IOException) MnemonicException(org.bitcoinj.crypto.MnemonicException) NotificationCompat(android.support.v4.app.NotificationCompat) DecryptionException(com.samourai.wallet.crypto.DecryptionException) Pair(org.apache.commons.lang3.tuple.Pair)

Example 10 with PaymentCode

use of com.samourai.wallet.bip47.rpc.PaymentCode in project samourai-wallet-android by Samourai-Wallet.

the class SendActivity method processPCode.

private void processPCode(String pcode, String meta) {
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            setBalance();
        }
    }, 2000);
    if (FormatsUtil.getInstance().isValidPaymentCode(pcode)) {
        if (BIP47Meta.getInstance().getOutgoingStatus(pcode) == BIP47Meta.STATUS_SENT_CFM) {
            try {
                PaymentCode _pcode = new PaymentCode(pcode);
                PaymentAddress paymentAddress = BIP47Util.getInstance(this).getSendAddress(_pcode, BIP47Meta.getInstance().getOutgoingIdx(pcode));
                if (BIP47Meta.getInstance().getSegwit(pcode)) {
                    SegwitAddress segwitAddress = new SegwitAddress(paymentAddress.getSendECKey(), SamouraiWallet.getInstance().getCurrentNetworkParams());
                    strDestinationBTCAddress = segwitAddress.getBech32AsString();
                } else {
                    strDestinationBTCAddress = paymentAddress.getSendECKey().toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString();
                }
                strPCode = _pcode.toString();
                setToAddress(BIP47Meta.getInstance().getDisplayLabel(strPCode));
                toAddressEditText.setEnabled(false);
                validateSpend();
            } catch (Exception e) {
                Toast.makeText(this, R.string.error_payment_code, Toast.LENGTH_SHORT).show();
            }
        } else {
            // Toast.makeText(SendActivity.this, "Payment must be added and notification tx sent", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(this, PayNymDetailsActivity.class);
            intent.putExtra("pcode", pcode);
            intent.putExtra("label", "");
            if (meta != null && meta.startsWith("?") && meta.length() > 1) {
                meta = meta.substring(1);
                if (meta.length() > 0) {
                    String _meta = null;
                    Map<String, String> map = new HashMap<String, String>();
                    meta.length();
                    try {
                        _meta = URLDecoder.decode(meta, "UTF-8");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    map = Splitter.on('&').trimResults().withKeyValueSeparator("=").split(_meta);
                    intent.putExtra("label", map.containsKey("title") ? map.get("title").trim() : "");
                }
            }
            if (!openedPaynym) {
                startActivity(intent);
                openedPaynym = true;
            }
        }
    } else {
        Toast.makeText(this, R.string.invalid_payment_code, Toast.LENGTH_SHORT).show();
    }
}
Also used : PaymentCode(com.samourai.wallet.bip47.rpc.PaymentCode) SegwitAddress(com.samourai.wallet.segwit.SegwitAddress) HashMap(java.util.HashMap) Handler(android.os.Handler) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Intent(android.content.Intent) PaymentAddress(com.samourai.wallet.bip47.rpc.PaymentAddress) JSONException(org.json.JSONException) IOException(java.io.IOException) ParseException(java.text.ParseException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MnemonicException(org.bitcoinj.crypto.MnemonicException)

Aggregations

PaymentCode (com.samourai.wallet.bip47.rpc.PaymentCode)21 IOException (java.io.IOException)15 MnemonicException (org.bitcoinj.crypto.MnemonicException)15 JSONException (org.json.JSONException)14 PaymentAddress (com.samourai.wallet.bip47.rpc.PaymentAddress)12 ArrayList (java.util.ArrayList)10 AddressFormatException (org.bitcoinj.core.AddressFormatException)10 MyTransactionOutPoint (com.samourai.wallet.send.MyTransactionOutPoint)9 Intent (android.content.Intent)8 DecryptionException (com.samourai.wallet.crypto.DecryptionException)8 ECKey (org.bitcoinj.core.ECKey)8 InvalidKeyException (java.security.InvalidKeyException)7 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)7 NoSuchProviderException (java.security.NoSuchProviderException)7 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)7 CharSequenceX (com.samourai.wallet.util.CharSequenceX)6 SegwitAddress (com.samourai.wallet.segwit.SegwitAddress)5 Script (org.bitcoinj.script.Script)5 JSONObject (org.json.JSONObject)5 NotSecp256k1Exception (com.samourai.wallet.bip47.rpc.NotSecp256k1Exception)4