Search in sources :

Example 11 with TransactionInput

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;
    }
}
Also used : Script(org.bitcoinj.script.Script) ScriptException(org.bitcoinj.script.ScriptException) Transaction(org.bitcoinj.core.Transaction) HashMap(java.util.HashMap) ECKey(org.bitcoinj.core.ECKey) TransactionInput(org.bitcoinj.core.TransactionInput) MnemonicException(org.bitcoinj.crypto.MnemonicException) ScriptException(org.bitcoinj.script.ScriptException) AddressFormatException(org.bitcoinj.core.AddressFormatException) IOException(java.io.IOException)

Example 12 with TransactionInput

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;
    }
}
Also used : Script(org.bitcoinj.script.Script) AddressFormatException(org.bitcoinj.core.AddressFormatException) ScriptException(org.bitcoinj.script.ScriptException) Transaction(org.bitcoinj.core.Transaction) HashMap(java.util.HashMap) ECKey(org.bitcoinj.core.ECKey) DumpedPrivateKey(org.bitcoinj.core.DumpedPrivateKey) TransactionInput(org.bitcoinj.core.TransactionInput) MnemonicException(org.bitcoinj.crypto.MnemonicException) ScriptException(org.bitcoinj.script.ScriptException) AddressFormatException(org.bitcoinj.core.AddressFormatException) IOException(java.io.IOException)

Example 13 with TransactionInput

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;
}
Also used : Script(org.bitcoinj.script.Script) TransactionOutput(org.bitcoinj.core.TransactionOutput) ArrayList(java.util.ArrayList) ScriptException(org.bitcoinj.script.ScriptException) AddressFormatException(org.bitcoinj.core.AddressFormatException) TransactionInput(org.bitcoinj.core.TransactionInput) Transaction(org.bitcoinj.core.Transaction) BigInteger(java.math.BigInteger) HashMap(java.util.HashMap) Map(java.util.Map)

Example 14 with TransactionInput

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;
    }
}
Also used : Script(org.bitcoinj.script.Script) AddressFormatException(org.bitcoinj.core.AddressFormatException) ScriptException(org.bitcoinj.script.ScriptException) Transaction(org.bitcoinj.core.Transaction) HashMap(java.util.HashMap) ECKey(org.bitcoinj.core.ECKey) DumpedPrivateKey(org.bitcoinj.core.DumpedPrivateKey) TransactionInput(org.bitcoinj.core.TransactionInput) ScriptException(org.bitcoinj.script.ScriptException) AddressFormatException(org.bitcoinj.core.AddressFormatException)

Example 15 with TransactionInput

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;
}
Also used : Transaction(org.bitcoinj.core.Transaction) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint) TransactionInput(org.bitcoinj.core.TransactionInput) HashSet(java.util.HashSet)

Aggregations

TransactionInput (org.bitcoinj.core.TransactionInput)36 Transaction (org.bitcoinj.core.Transaction)29 TransactionOutput (org.bitcoinj.core.TransactionOutput)20 TransactionOutPoint (org.bitcoinj.core.TransactionOutPoint)18 Script (org.bitcoinj.script.Script)16 ECKey (org.bitcoinj.core.ECKey)11 HashMap (java.util.HashMap)10 Address (org.bitcoinj.core.Address)9 Coin (org.bitcoinj.core.Coin)9 ArrayList (java.util.ArrayList)7 AddressFormatException (org.bitcoinj.core.AddressFormatException)7 RawTransactionInput (bisq.core.btc.data.RawTransactionInput)6 TransactionSignature (org.bitcoinj.crypto.TransactionSignature)6 ScriptException (org.bitcoinj.script.ScriptException)6 MyTransactionOutPoint (com.samourai.wallet.send.MyTransactionOutPoint)5 IOException (java.io.IOException)5 Sha256Hash (org.bitcoinj.core.Sha256Hash)5 MnemonicException (org.bitcoinj.crypto.MnemonicException)5 SendRequest (org.bitcoinj.wallet.SendRequest)5 BlockedUTXO (com.samourai.wallet.send.BlockedUTXO)4