use of org.bitcoinj.core.Transaction in project samourai-wallet-android by Samourai-Wallet.
the class CahootsUtil method doSTONEWALLx2_3.
//
// counterparty
//
public Cahoots doSTONEWALLx2_3(STONEWALLx2 stonewall2) throws Exception {
HashMap<String, String> utxo2Address = new HashMap<String, String>();
List<UTXO> utxos = null;
if (stonewall2.getCounterpartyAccount() == WhirlpoolMeta.getInstance(context).getWhirlpoolPostmix()) {
utxos = APIFactory.getInstance(context).getUtxosPostMix(true);
} else {
utxos = APIFactory.getInstance(context).getUtxos(true);
}
for (UTXO utxo : utxos) {
for (MyTransactionOutPoint outpoint : utxo.getOutpoints()) {
utxo2Address.put(outpoint.getTxHash().toString() + "-" + outpoint.getTxOutputN(), outpoint.getAddress());
}
}
Transaction transaction = stonewall2.getTransaction();
HashMap<String, ECKey> keyBag_A = new HashMap<String, ECKey>();
for (TransactionInput input : transaction.getInputs()) {
TransactionOutPoint outpoint = input.getOutpoint();
String key = outpoint.getHash().toString() + "-" + outpoint.getIndex();
if (utxo2Address.containsKey(key)) {
String address = utxo2Address.get(key);
ECKey eckey = SendFactory.getPrivKey(address, stonewall2.getCounterpartyAccount());
keyBag_A.put(outpoint.toString(), eckey);
}
}
STONEWALLx2 stonewall3 = new STONEWALLx2(stonewall2);
stonewall3.inc(null, null, keyBag_A);
return stonewall3;
}
use of org.bitcoinj.core.Transaction in project samourai-wallet-android by Samourai-Wallet.
the class CahootsUtil method doSTONEWALLx2_4.
//
// sender
//
public Cahoots doSTONEWALLx2_4(STONEWALLx2 stonewall3) throws Exception {
HashMap<String, String> utxo2Address = new HashMap<String, String>();
List<UTXO> utxos = null;
if (stonewall3.getAccount() == WhirlpoolMeta.getInstance(context).getWhirlpoolPostmix()) {
utxos = APIFactory.getInstance(context).getUtxosPostMix(true);
} else {
utxos = APIFactory.getInstance(context).getUtxos(true);
}
for (UTXO utxo : utxos) {
for (MyTransactionOutPoint outpoint : utxo.getOutpoints()) {
utxo2Address.put(outpoint.getTxHash().toString() + "-" + outpoint.getTxOutputN(), outpoint.getAddress());
}
}
Transaction transaction = stonewall3.getTransaction();
HashMap<String, ECKey> keyBag_B = new HashMap<String, ECKey>();
for (TransactionInput input : transaction.getInputs()) {
TransactionOutPoint outpoint = input.getOutpoint();
String key = outpoint.getHash().toString() + "-" + outpoint.getIndex();
if (utxo2Address.containsKey(key)) {
String address = utxo2Address.get(key);
ECKey eckey = SendFactory.getPrivKey(address, stonewall3.getAccount());
keyBag_B.put(outpoint.toString(), eckey);
}
}
STONEWALLx2 stonewall4 = new STONEWALLx2(stonewall3);
stonewall4.inc(null, null, keyBag_B);
return stonewall4;
}
use of org.bitcoinj.core.Transaction in project samourai-wallet-android by Samourai-Wallet.
the class CahootsUtil method doStowaway3.
//
// receiver
//
public Cahoots doStowaway3(Stowaway stowaway2) throws Exception {
debug("CahootsUtil", "sender account (3):" + stowaway2.getAccount());
HashMap<String, String> utxo2Address = new HashMap<String, String>();
List<UTXO> utxos = APIFactory.getInstance(context).getUtxos(true);
for (UTXO utxo : utxos) {
for (MyTransactionOutPoint outpoint : utxo.getOutpoints()) {
utxo2Address.put(outpoint.getTxHash().toString() + "-" + outpoint.getTxOutputN(), outpoint.getAddress());
debug("CahootsUtil", "outpoint address:" + outpoint.getTxHash().toString() + "-" + outpoint.getTxOutputN() + "," + outpoint.getAddress());
}
}
Transaction transaction = stowaway2.getPSBT().getTransaction();
HashMap<String, ECKey> keyBag_A = new HashMap<String, ECKey>();
for (TransactionInput input : transaction.getInputs()) {
TransactionOutPoint outpoint = input.getOutpoint();
String key = outpoint.getHash().toString() + "-" + outpoint.getIndex();
if (utxo2Address.containsKey(key)) {
String address = utxo2Address.get(key);
ECKey eckey = SendFactory.getPrivKey(address, 0);
keyBag_A.put(outpoint.toString(), eckey);
}
}
Stowaway stowaway3 = new Stowaway(stowaway2);
stowaway3.inc(null, null, keyBag_A);
return stowaway3;
}
use of org.bitcoinj.core.Transaction in project sentinel-android by Samourai-Wallet.
the class SendFactory method makeTransaction.
/*
Used by spends
*/
private Transaction makeTransaction(int accountIdx, HashMap<String, BigInteger> receivers, List<MyTransactionOutPoint> unspent) throws Exception {
BigInteger amount = BigInteger.ZERO;
for (Iterator<Map.Entry<String, BigInteger>> iterator = receivers.entrySet().iterator(); iterator.hasNext(); ) {
Map.Entry<String, BigInteger> mapEntry = iterator.next();
amount = amount.add(mapEntry.getValue());
}
List<TransactionOutput> outputs = new ArrayList<TransactionOutput>();
Transaction tx = new Transaction(MainNetParams.get());
for (Iterator<Map.Entry<String, BigInteger>> iterator = receivers.entrySet().iterator(); iterator.hasNext(); ) {
Map.Entry<String, BigInteger> mapEntry = iterator.next();
String toAddress = mapEntry.getKey();
BigInteger value = mapEntry.getValue();
/*
if(value.compareTo(SamouraiWallet.bDust) < 1) {
throw new Exception(context.getString(R.string.dust_amount));
}
*/
if (value == null || value.compareTo(BigInteger.ZERO) <= 0) {
throw new Exception(context.getString(R.string.invalid_amount));
}
Script toOutputScript = ScriptBuilder.createOutputScript(org.bitcoinj.core.Address.fromBase58(MainNetParams.get(), toAddress));
TransactionOutput output = new TransactionOutput(MainNetParams.get(), null, Coin.valueOf(value.longValue()), toOutputScript.getProgram());
outputs.add(output);
}
List<MyTransactionInput> inputs = new ArrayList<MyTransactionInput>();
for (MyTransactionOutPoint outPoint : unspent) {
Script script = new Script(outPoint.getScriptBytes());
if (script.getScriptType() == Script.ScriptType.NO_TYPE) {
continue;
}
MyTransactionInput input = new MyTransactionInput(MainNetParams.get(), null, new byte[0], outPoint, outPoint.getTxHash().toString(), outPoint.getTxOutputN());
inputs.add(input);
}
//
// deterministically sort inputs and outputs, see BIP69 (OBPP)
//
Collections.sort(inputs, new BIP69InputComparator());
for (TransactionInput input : inputs) {
tx.addInput(input);
}
Collections.sort(outputs, new BIP69OutputComparator());
for (TransactionOutput to : outputs) {
tx.addOutput(to);
}
return tx;
}
use of org.bitcoinj.core.Transaction 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();
}
Aggregations