Search in sources :

Example 1 with SegwitAddress

use of com.samourai.sentinel.segwit.SegwitAddress in project sentinel-android by Samourai-Wallet.

the class SweepUtil method sweep.

public void sweep(final PrivKeyReader privKeyReader, final String strReceiveAddress, final int type) {
    new Thread(new Runnable() {

        @Override
        public void run() {
            Looper.prepare();
            try {
                if (privKeyReader == null || privKeyReader.getKey() == null || !privKeyReader.getKey().hasPrivKey()) {
                    Toast.makeText(context, R.string.cannot_recognize_privkey, Toast.LENGTH_SHORT).show();
                    return;
                }
                String address = null;
                UTXO utxo = null;
                if (type == TYPE_P2SH_P2WPKH) {
                    utxo = utxoP2SH_P2WPKH;
                    address = addressP2SH_P2WPKH;
                } else if (type == TYPE_P2WPKH) {
                    utxo = utxoP2WPKH;
                    address = addressP2WPKH;
                } else {
                    addressP2PKH = privKeyReader.getKey().toAddress(MainNetParams.get()).toString();
                    Log.d("SweepUtil", "address derived P2PKH:" + addressP2PKH);
                    addressP2SH_P2WPKH = new P2SH_P2WPKH(privKeyReader.getKey(), MainNetParams.get()).getAddressAsString();
                    Log.d("SweepUtil", "address derived P2SH_P2WPKH:" + addressP2SH_P2WPKH);
                    addressP2WPKH = new SegwitAddress(privKeyReader.getKey(), MainNetParams.get()).getBech32AsString();
                    Log.d("SweepUtil", "address derived P2WPKH:" + addressP2WPKH);
                    utxoP2PKH = APIFactory.getInstance(context).getUnspentOutputsForSweep(addressP2PKH);
                    utxoP2SH_P2WPKH = APIFactory.getInstance(context).getUnspentOutputsForSweep(addressP2SH_P2WPKH);
                    utxoP2WPKH = APIFactory.getInstance(context).getUnspentOutputsForSweep(addressP2WPKH);
                    utxo = utxoP2PKH;
                    address = addressP2PKH;
                }
                if (utxo != null && utxo.getOutpoints().size() > 0) {
                    long total_value = 0L;
                    final List<MyTransactionOutPoint> outpoints = utxo.getOutpoints();
                    for (MyTransactionOutPoint outpoint : outpoints) {
                        total_value += outpoint.getValue().longValue();
                    }
                    FeeUtil.getInstance().setSuggestedFee(FeeUtil.getInstance().getNormalFee());
                    final BigInteger fee;
                    if (type == TYPE_P2SH_P2WPKH) {
                        fee = FeeUtil.getInstance().estimatedFeeSegwit(0, outpoints.size(), 1);
                    } else if (type == TYPE_P2PKH) {
                        fee = FeeUtil.getInstance().estimatedFeeSegwit(0, 0, outpoints.size(), 1);
                    } else {
                        fee = FeeUtil.getInstance().estimatedFee(outpoints.size(), 1);
                    }
                    final long amount = total_value - fee.longValue();
                    Log.d("SweepUtil", "Total value:" + total_value);
                    Log.d("SweepUtil", "Amount:" + amount);
                    Log.d("SweepUtil", "Fee:" + fee.toString());
                    Log.d("SweepUtil", "Receive address:" + strReceiveAddress);
                    String message = "Sweep " + Coin.valueOf(amount).toPlainString() + " from " + address + " (fee:" + Coin.valueOf(fee.longValue()).toPlainString() + ")?";
                    new AlertDialog.Builder(context).setTitle(R.string.app_name).setMessage(message).setCancelable(false).setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {

                        public void onClick(final DialogInterface dialog, int whichButton) {
                            Log.d("SweepUtil", "start sweep");
                            final HashMap<String, BigInteger> receivers = new HashMap<String, BigInteger>();
                            receivers.put(strReceiveAddress, BigInteger.valueOf(amount));
                            Transaction tx = SendFactory.getInstance(context).makeTransaction(0, outpoints, receivers);
                            Log.d("SweepUtil", "tx is " + ((tx == null) ? "null" : "not null"));
                            tx = SendFactory.getInstance(context).signTransactionForSweep(tx, privKeyReader);
                            final String hexTx = new String(Hex.encode(tx.bitcoinSerialize()));
                            Log.d("SweepUtil", hexTx);
                            // 
                            String response = null;
                            try {
                                response = PushTx.getInstance(context).samourai(hexTx);
                                if (response != null) {
                                    JSONObject jsonObject = new org.json.JSONObject(response);
                                    if (jsonObject.has("status")) {
                                        if (jsonObject.getString("status").equals("ok")) {
                                            Toast.makeText(context, R.string.tx_ok, Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                } else {
                                    Toast.makeText(context, R.string.pushtx_returns_null, Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException je) {
                                Toast.makeText(context, "pushTx:" + je.getMessage(), Toast.LENGTH_SHORT).show();
                            }
                            dialog.dismiss();
                        }
                    }).setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {

                        public void onClick(final DialogInterface dialog, int whichButton) {
                            dialog.dismiss();
                        }
                    }).show();
                } else if (type == TYPE_P2SH_P2WPKH) {
                    sweep(privKeyReader, strReceiveAddress, TYPE_P2WPKH);
                } else if (type == TYPE_P2PKH) {
                    sweep(privKeyReader, strReceiveAddress, TYPE_P2SH_P2WPKH);
                } else if (type == TYPE_P2WPKH) {
                    ;
                }
            } catch (Exception e) {
                Log.d("SweepUtil", e.getMessage());
                Toast.makeText(context, context.getText(R.string.cannot_sweep_privkey) + ", " + e.getMessage(), Toast.LENGTH_SHORT).show();
            }
            Looper.loop();
        }
    }).start();
}
Also used : SegwitAddress(com.samourai.sentinel.segwit.SegwitAddress) DialogInterface(android.content.DialogInterface) HashMap(java.util.HashMap) JSONException(org.json.JSONException) JSONException(org.json.JSONException) P2SH_P2WPKH(com.samourai.sentinel.segwit.P2SH_P2WPKH) JSONObject(org.json.JSONObject) Transaction(org.bitcoinj.core.Transaction) JSONObject(org.json.JSONObject) BigInteger(java.math.BigInteger)

Aggregations

DialogInterface (android.content.DialogInterface)1 P2SH_P2WPKH (com.samourai.sentinel.segwit.P2SH_P2WPKH)1 SegwitAddress (com.samourai.sentinel.segwit.SegwitAddress)1 BigInteger (java.math.BigInteger)1 HashMap (java.util.HashMap)1 Transaction (org.bitcoinj.core.Transaction)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1