Search in sources :

Example 1 with PaymentAddress

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

the class BIP47Util method getPaymentAddress.

public PaymentAddress getPaymentAddress(PaymentCode pcode, int idx, HD_Address address) throws AddressFormatException, NotSecp256k1Exception {
    DumpedPrivateKey dpk = new DumpedPrivateKey(SamouraiWallet.getInstance().getCurrentNetworkParams(), address.getPrivateKeyString());
    ECKey eckey = dpk.getKey();
    PaymentAddress paymentAddress = new PaymentAddress(pcode, idx, eckey.getPrivKeyBytes());
    return paymentAddress;
}
Also used : ECKey(org.bitcoinj.core.ECKey) PaymentAddress(com.samourai.wallet.bip47.rpc.PaymentAddress) DumpedPrivateKey(org.bitcoinj.core.DumpedPrivateKey)

Example 2 with PaymentAddress

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

the class SendFactory method getPrivKey.

public static ECKey getPrivKey(String address) {
    ECKey ecKey = null;
    try {
        String path = APIFactory.getInstance(context).getUnspentPaths().get(address);
        // Log.d("APIFactory", "address:" + path);
        if (path != null) {
            String[] s = path.split("/");
            if (Address.fromBase58(SamouraiWallet.getInstance().getCurrentNetworkParams(), address).isP2SHAddress()) {
                // Log.d("APIFactory", "address type:" + "bip49");
                HD_Address addr = BIP49Util.getInstance(context).getWallet().getAccount(0).getChain(Integer.parseInt(s[1])).getAddressAt(Integer.parseInt(s[2]));
                ecKey = addr.getECKey();
            } else {
                // Log.d("APIFactory", "address type:" + "bip44");
                int account_no = APIFactory.getInstance(context).getUnspentAccounts().get(address);
                HD_Address hd_address = AddressFactory.getInstance(context).get(account_no, Integer.parseInt(s[1]), Integer.parseInt(s[2]));
                String strPrivKey = hd_address.getPrivateKeyString();
                DumpedPrivateKey pk = new DumpedPrivateKey(SamouraiWallet.getInstance().getCurrentNetworkParams(), strPrivKey);
                ecKey = pk.getKey();
            }
        } else {
            // Log.d("APIFactory", "address type:" + "bip47");
            String pcode = BIP47Meta.getInstance().getPCode4Addr(address);
            int idx = BIP47Meta.getInstance().getIdx4Addr(address);
            PaymentAddress addr = BIP47Util.getInstance(context).getReceiveAddress(new PaymentCode(pcode), idx);
            ecKey = addr.getReceiveECKey();
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return ecKey;
}
Also used : PaymentCode(com.samourai.wallet.bip47.rpc.PaymentCode) HD_Address(com.samourai.wallet.hd.HD_Address) ECKey(org.bitcoinj.core.ECKey) PaymentAddress(com.samourai.wallet.bip47.rpc.PaymentAddress) DumpedPrivateKey(org.bitcoinj.core.DumpedPrivateKey) MnemonicException(org.bitcoinj.crypto.MnemonicException) ScriptException(org.bitcoinj.script.ScriptException) AddressFormatException(org.bitcoinj.core.AddressFormatException) IOException(java.io.IOException)

Example 3 with PaymentAddress

use of com.samourai.wallet.bip47.rpc.PaymentAddress 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)

Example 4 with PaymentAddress

use of com.samourai.wallet.bip47.rpc.PaymentAddress 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 5 with PaymentAddress

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

the class PayNymCalcActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_paynym_calc);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    edPayNym = (EditText) findViewById(R.id.paynym);
    edIndex = (EditText) findViewById(R.id.index);
    edPayNym.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // final int DRAWABLE_LEFT = 0;
            // final int DRAWABLE_TOP = 1;
            final int DRAWABLE_RIGHT = 2;
            if (event.getAction() == MotionEvent.ACTION_UP && event.getRawX() >= (edPayNym.getRight() - edPayNym.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                final List<String> entries = new ArrayList<String>();
                entries.addAll(BIP47Meta.getInstance().getSortedByLabels(true));
                final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(PayNymCalcActivity.this, android.R.layout.select_dialog_singlechoice);
                for (int i = 0; i < entries.size(); i++) {
                    arrayAdapter.add(BIP47Meta.getInstance().getDisplayLabel(entries.get(i)));
                }
                AlertDialog.Builder dlg = new AlertDialog.Builder(PayNymCalcActivity.this);
                dlg.setIcon(R.drawable.ic_launcher);
                dlg.setTitle(R.string.app_name);
                dlg.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        edPayNym.setText(entries.get(which));
                    }
                });
                dlg.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                dlg.show();
                return true;
            }
            return false;
        }
    });
    btOK = (Button) findViewById(R.id.ok);
    btOK.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            String strPayNym = edPayNym.getText().toString();
            String strIndex = edIndex.getText().toString();
            PaymentCode pcode = new PaymentCode(strPayNym);
            if (pcode == null) {
                Toast.makeText(PayNymCalcActivity.this, R.string.invalid_payment_code, Toast.LENGTH_SHORT).show();
                return;
            }
            if (strIndex == null || strIndex.length() < 1) {
                Toast.makeText(PayNymCalcActivity.this, R.string.invalid_index, Toast.LENGTH_SHORT).show();
                return;
            }
            try {
                int index = Integer.parseInt(strIndex);
                String message = strPayNym;
                final ECKey receiveECKey;
                final SegwitAddress receiveSegwit;
                PaymentAddress receiveAddress = BIP47Util.getInstance(PayNymCalcActivity.this).getReceiveAddress(new PaymentCode(strPayNym), index);
                PaymentAddress sendAddress = BIP47Util.getInstance(PayNymCalcActivity.this).getSendAddress(new PaymentCode(strPayNym), index);
                receiveECKey = receiveAddress.getReceiveECKey();
                ECKey sendECKey = sendAddress.getSendECKey();
                receiveSegwit = new SegwitAddress(receiveECKey, SamouraiWallet.getInstance().getCurrentNetworkParams());
                SegwitAddress sendSegwit = new SegwitAddress(sendECKey, SamouraiWallet.getInstance().getCurrentNetworkParams());
                message += "\n";
                message += index + ":";
                message += "\n";
                message += "\n";
                message += PayNymCalcActivity.this.getText(R.string.receive_addresses).toString() + ":";
                message += "\n";
                message += receiveECKey.toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString();
                message += "\n";
                message += receiveSegwit.getAddressAsString();
                message += "\n";
                message += receiveSegwit.getBech32AsString();
                message += "\n";
                message += PayNymCalcActivity.this.getText(R.string.send_addresses).toString() + ":";
                message += "\n";
                message += sendECKey.toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString();
                message += "\n";
                message += sendSegwit.getAddressAsString();
                message += "\n";
                message += sendSegwit.getBech32AsString();
                message += "\n";
                final TextView tvText = new TextView(getApplicationContext());
                tvText.setTextSize(12);
                tvText.setText(message);
                tvText.setTextIsSelectable(true);
                tvText.setPadding(40, 10, 40, 10);
                tvText.setTextColor(0xffffffff);
                AlertDialog.Builder dlg = new AlertDialog.Builder(PayNymCalcActivity.this).setTitle(R.string.app_name).setView(tvText).setCancelable(true).setPositiveButton(R.string.display_receive_redeem, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {
                        String redeemScript = org.spongycastle.util.encoders.Hex.toHexString(receiveSegwit.segWitRedeemScript().getProgram());
                        TextView showText = new TextView(PayNymCalcActivity.this);
                        showText.setText(redeemScript);
                        showText.setTextIsSelectable(true);
                        showText.setPadding(40, 10, 40, 10);
                        showText.setTextSize(18.0f);
                        new AlertDialog.Builder(PayNymCalcActivity.this).setTitle(R.string.app_name).setView(showText).setCancelable(false).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int whichButton) {
                                dialog.dismiss();
                            }
                        }).show();
                    }
                }).setNeutralButton(R.string.close, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.dismiss();
                    }
                }).setNegativeButton(R.string.display_receive_privkey, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {
                        String strPrivKey = receiveECKey.getPrivateKeyAsWiF(SamouraiWallet.getInstance().getCurrentNetworkParams());
                        ImageView showQR = new ImageView(PayNymCalcActivity.this);
                        Bitmap bitmap = null;
                        QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(strPrivKey, null, Contents.Type.TEXT, BarcodeFormat.QR_CODE.toString(), 500);
                        try {
                            bitmap = qrCodeEncoder.encodeAsBitmap();
                        } catch (WriterException e) {
                            e.printStackTrace();
                        }
                        showQR.setImageBitmap(bitmap);
                        TextView showText = new TextView(PayNymCalcActivity.this);
                        showText.setText(strPrivKey);
                        showText.setTextIsSelectable(true);
                        showText.setPadding(40, 10, 40, 10);
                        showText.setTextSize(18.0f);
                        LinearLayout privkeyLayout = new LinearLayout(PayNymCalcActivity.this);
                        privkeyLayout.setOrientation(LinearLayout.VERTICAL);
                        privkeyLayout.addView(showQR);
                        privkeyLayout.addView(showText);
                        new AlertDialog.Builder(PayNymCalcActivity.this).setTitle(R.string.app_name).setView(privkeyLayout).setCancelable(false).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int whichButton) {
                                ;
                            }
                        }).show();
                    }
                });
                if (!isFinishing()) {
                    dlg.show();
                }
            } catch (Exception e) {
                Toast.makeText(PayNymCalcActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    });
    btCancel = (Button) findViewById(R.id.cancel);
    btCancel.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            finish();
        }
    });
}
Also used : AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) SegwitAddress(com.samourai.wallet.segwit.SegwitAddress) QRCodeEncoder(com.google.zxing.client.android.encode.QRCodeEncoder) ECKey(org.bitcoinj.core.ECKey) Bitmap(android.graphics.Bitmap) ArrayList(java.util.ArrayList) List(java.util.List) TextView(android.widget.TextView) ImageView(android.widget.ImageView) PaymentCode(com.samourai.wallet.bip47.rpc.PaymentCode) PaymentAddress(com.samourai.wallet.bip47.rpc.PaymentAddress) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) WriterException(com.google.zxing.WriterException) MotionEvent(android.view.MotionEvent) ArrayAdapter(android.widget.ArrayAdapter) WriterException(com.google.zxing.WriterException) LinearLayout(android.widget.LinearLayout)

Aggregations

PaymentAddress (com.samourai.wallet.bip47.rpc.PaymentAddress)12 PaymentCode (com.samourai.wallet.bip47.rpc.PaymentCode)11 IOException (java.io.IOException)9 MnemonicException (org.bitcoinj.crypto.MnemonicException)9 JSONException (org.json.JSONException)6 SegwitAddress (com.samourai.wallet.segwit.SegwitAddress)5 ArrayList (java.util.ArrayList)5 ECKey (org.bitcoinj.core.ECKey)5 Intent (android.content.Intent)4 DumpedPrivateKey (org.bitcoinj.core.DumpedPrivateKey)4 WriterException (com.google.zxing.WriterException)3 HD_Address (com.samourai.wallet.hd.HD_Address)3 MyTransactionOutPoint (com.samourai.wallet.send.MyTransactionOutPoint)3 CharSequenceX (com.samourai.wallet.util.CharSequenceX)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ParseException (java.text.ParseException)3 HashMap (java.util.HashMap)3 AddressFormatException (org.bitcoinj.core.AddressFormatException)3 ScriptException (org.bitcoinj.script.ScriptException)3 NotSecp256k1Exception (com.samourai.wallet.bip47.rpc.NotSecp256k1Exception)2