use of org.bitcoinj.core.TransactionInput in project samourai-wallet-android by Samourai-Wallet.
the class SendFactory method signTransaction.
public Transaction signTransaction(Transaction unsignedTx) {
HashMap<String, ECKey> keyBag = new HashMap<String, ECKey>();
for (TransactionInput input : unsignedTx.getInputs()) {
try {
byte[] scriptBytes = input.getOutpoint().getConnectedPubKeyScript();
String address = new Script(scriptBytes).getToAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString();
// Log.i("address from script", address);
ECKey ecKey = null;
ecKey = getPrivKey(address);
if (ecKey != null) {
keyBag.put(input.getOutpoint().toString(), ecKey);
} else {
throw new RuntimeException("ECKey error: cannot process private key");
// Log.i("ECKey error", "cannot process private key");
}
} catch (ScriptException se) {
;
} catch (Exception e) {
;
}
}
Transaction signedTx = signTransaction(unsignedTx, keyBag);
if (signedTx == null) {
return null;
} else {
String hexString = new String(Hex.encode(signedTx.bitcoinSerialize()));
if (hexString.length() > (100 * 1024)) {
Toast.makeText(context, R.string.tx_length_error, Toast.LENGTH_SHORT).show();
// Log.i("SendFactory", "Transaction length too long");
}
return signedTx;
}
}
use of org.bitcoinj.core.TransactionInput in project samourai-wallet-android by Samourai-Wallet.
the class SendFactory method signTransactionForSweep.
public Transaction signTransactionForSweep(Transaction unsignedTx, PrivKeyReader privKeyReader) {
HashMap<String, ECKey> keyBag = new HashMap<String, ECKey>();
for (TransactionInput input : unsignedTx.getInputs()) {
try {
byte[] scriptBytes = input.getOutpoint().getConnectedPubKeyScript();
// String address = new BitcoinScript(scriptBytes).getAddress().toString();
String address = new Script(scriptBytes).getToAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString();
// Log.i("address from script", address);
ECKey ecKey = null;
try {
DumpedPrivateKey pk = new DumpedPrivateKey(SamouraiWallet.getInstance().getCurrentNetworkParams(), privKeyReader.getKey().getPrivateKeyAsWiF(SamouraiWallet.getInstance().getCurrentNetworkParams()));
ecKey = pk.getKey();
// Log.i("SendFactory", "ECKey address:" + ecKey.toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString());
} catch (AddressFormatException afe) {
afe.printStackTrace();
continue;
}
if (ecKey != null) {
keyBag.put(input.getOutpoint().toString(), ecKey);
} else {
Toast.makeText(context, R.string.cannot_recognize_privkey, Toast.LENGTH_SHORT).show();
// Log.i("ECKey error", "cannot process private key");
}
} catch (ScriptException se) {
;
} catch (Exception e) {
;
}
}
Transaction signedTx = signTransaction(unsignedTx, keyBag);
if (signedTx == null) {
return null;
} else {
String hexString = new String(Hex.encode(signedTx.bitcoinSerialize()));
if (hexString.length() > (100 * 1024)) {
Toast.makeText(context, R.string.tx_length_error, Toast.LENGTH_SHORT).show();
// Log.i("SendFactory", "Transaction length too long");
}
return signedTx;
}
}
use of org.bitcoinj.core.TransactionInput 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.TransactionInput in project sentinel-android by Samourai-Wallet.
the class SendFactory method signTransactionForSweep.
public Transaction signTransactionForSweep(Transaction unsignedTx, PrivKeyReader privKeyReader) {
HashMap<String, ECKey> keyBag = new HashMap<String, ECKey>();
for (TransactionInput input : unsignedTx.getInputs()) {
try {
byte[] scriptBytes = input.getOutpoint().getConnectedPubKeyScript();
// String address = new BitcoinScript(scriptBytes).getAddress().toString();
String address = new Script(scriptBytes).getToAddress(MainNetParams.get()).toString();
// Log.i("address from script", address);
ECKey ecKey = null;
try {
DumpedPrivateKey pk = new DumpedPrivateKey(MainNetParams.get(), privKeyReader.getKey().getPrivateKeyAsWiF(MainNetParams.get()));
ecKey = pk.getKey();
// Log.i("SendFactory", "ECKey address:" + ecKey.toAddress(MainNetParams.get()).toString());
} catch (AddressFormatException afe) {
afe.printStackTrace();
continue;
}
if (ecKey != null) {
keyBag.put(input.getOutpoint().toString(), ecKey);
} else {
Toast.makeText(context, R.string.cannot_recognize_privkey, Toast.LENGTH_SHORT).show();
// Log.i("ECKey error", "cannot process private key");
}
} catch (ScriptException se) {
;
} catch (Exception e) {
;
}
}
Transaction signedTx = signTransaction(unsignedTx, keyBag);
if (signedTx == null) {
return null;
} else {
String hexString = new String(Hex.encode(signedTx.bitcoinSerialize()));
if (hexString.length() > (100 * 1024)) {
Toast.makeText(context, R.string.tx_length_error, Toast.LENGTH_SHORT).show();
// Log.i("SendFactory", "Transaction length too long");
}
return signedTx;
}
}
use of org.bitcoinj.core.TransactionInput in project catena-java by alinush.
the class TxUtils method findDoubleSpendsAgainst.
/**
* Checks if any of the txs in candidates double spends any of the outputs spent by the transaction in tx.
* (i.e., checks if tx and candidates conflict by spending the same output(s))
*
* Copied from the wallet class. Need it to check for lies.
*
* @param tx
* @param candidates
* @return
*/
public static Set<Transaction> findDoubleSpendsAgainst(Transaction tx, Map<Sha256Hash, Transaction> candidates) {
// Coinbase TXs cannot double spend
if (tx.isCoinBase())
return Sets.newHashSet();
// Compile a set of outpoints that are spent by tx.
HashSet<TransactionOutPoint> outpoints = new HashSet<TransactionOutPoint>();
for (TransactionInput input : tx.getInputs()) {
outpoints.add(input.getOutpoint());
}
// Now for each candidate transaction, see if it spends any outpoints as this tx.
Set<Transaction> doubleSpendTxns = Sets.newHashSet();
for (Transaction p : candidates.values()) {
for (TransactionInput input : p.getInputs()) {
TransactionOutPoint outpoint = input.getOutpoint();
if (outpoints.contains(outpoint)) {
// It does, it's a double spend against the candidates, which makes it relevant.
doubleSpendTxns.add(p);
}
}
}
return doubleSpendTxns;
}
Aggregations