Search in sources :

Example 1 with BIP69InputComparator

use of com.samourai.wallet.bip69.BIP69InputComparator in project samourai-wallet-android by Samourai-Wallet.

the class Stowaway method doStep3.

// 
// receiver
// 
public boolean doStep3(HashMap<String, ECKey> keyBag) {
    Transaction transaction = this.getTransaction();
    List<TransactionInput> inputs = new ArrayList<TransactionInput>();
    inputs.addAll(transaction.getInputs());
    Collections.sort(inputs, new BIP69InputComparator());
    transaction.clearInputs();
    for (TransactionInput input : inputs) {
        transaction.addInput(input);
    }
    List<TransactionOutput> outputs = new ArrayList<TransactionOutput>();
    outputs.addAll(transaction.getOutputs());
    Collections.sort(outputs, new BIP69OutputComparator());
    transaction.clearOutputs();
    for (TransactionOutput output : outputs) {
        transaction.addOutput(output);
    }
    psbt.setTransaction(transaction);
    signTx(keyBag);
    this.setStep(3);
    return true;
}
Also used : BIP69InputComparator(com.samourai.wallet.bip69.BIP69InputComparator) BIP69OutputComparator(com.samourai.wallet.bip69.BIP69OutputComparator)

Example 2 with BIP69InputComparator

use of com.samourai.wallet.bip69.BIP69InputComparator in project samourai-wallet-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(SamouraiWallet.getInstance().getCurrentNetworkParams());
    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 && !FormatsUtil.getInstance().isValidBIP47OpReturn(toAddress))) {
            throw new Exception(context.getString(R.string.invalid_amount));
        }
        TransactionOutput output = null;
        Script toOutputScript = null;
        if (!FormatsUtil.getInstance().isValidBitcoinAddress(toAddress) && FormatsUtil.getInstance().isValidBIP47OpReturn(toAddress)) {
            toOutputScript = new ScriptBuilder().op(ScriptOpCodes.OP_RETURN).data(Hex.decode(toAddress)).build();
            output = new TransactionOutput(SamouraiWallet.getInstance().getCurrentNetworkParams(), null, Coin.valueOf(0L), toOutputScript.getProgram());
        } else if (FormatsUtil.getInstance().isValidBech32(toAddress)) {
            output = Bech32Util.getInstance().getTransactionOutput(toAddress, value.longValue());
        } else {
            toOutputScript = ScriptBuilder.createOutputScript(org.bitcoinj.core.Address.fromBase58(SamouraiWallet.getInstance().getCurrentNetworkParams(), toAddress));
            output = new TransactionOutput(SamouraiWallet.getInstance().getCurrentNetworkParams(), 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(SamouraiWallet.getInstance().getCurrentNetworkParams(), null, new byte[0], outPoint, outPoint.getTxHash().toString(), outPoint.getTxOutputN());
        if (PrefsUtil.getInstance(context).getValue(PrefsUtil.RBF_OPT_IN, false) == true) {
            input.setSequenceNumber(SamouraiWallet.RBF_SEQUENCE_VAL.longValue());
        }
        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) ScriptBuilder(org.bitcoinj.script.ScriptBuilder) ScriptException(org.bitcoinj.script.ScriptException) AddressFormatException(org.bitcoinj.core.AddressFormatException) MnemonicException(org.bitcoinj.crypto.MnemonicException) IOException(java.io.IOException) TransactionInput(org.bitcoinj.core.TransactionInput) Transaction(org.bitcoinj.core.Transaction) BIP69OutputComparator(com.samourai.wallet.bip69.BIP69OutputComparator) BigInteger(java.math.BigInteger) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with BIP69InputComparator

use of com.samourai.wallet.bip69.BIP69InputComparator in project samourai-wallet-android by Samourai-Wallet.

the class STONEWALLx2 method doStep3.

// 
// counterparty
// 
protected boolean doStep3(HashMap<String, ECKey> keyBag) {
    Transaction transaction = this.getTransaction();
    List<TransactionInput> inputs = new ArrayList<TransactionInput>();
    inputs.addAll(transaction.getInputs());
    Collections.sort(inputs, new BIP69InputComparator());
    transaction.clearInputs();
    for (TransactionInput input : inputs) {
        transaction.addInput(input);
    }
    List<TransactionOutput> outputs = new ArrayList<TransactionOutput>();
    outputs.addAll(transaction.getOutputs());
    Collections.sort(outputs, new BIP69OutputComparator());
    transaction.clearOutputs();
    for (TransactionOutput output : outputs) {
        transaction.addOutput(output);
    }
    psbt.setTransaction(transaction);
    signTx(keyBag);
    this.setStep(3);
    return true;
}
Also used : BIP69InputComparator(com.samourai.wallet.bip69.BIP69InputComparator) BIP69OutputComparator(com.samourai.wallet.bip69.BIP69OutputComparator)

Aggregations

BIP69OutputComparator (com.samourai.wallet.bip69.BIP69OutputComparator)3 BIP69InputComparator (com.samourai.wallet.bip69.BIP69InputComparator)2 IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AddressFormatException (org.bitcoinj.core.AddressFormatException)1 Transaction (org.bitcoinj.core.Transaction)1 TransactionInput (org.bitcoinj.core.TransactionInput)1 TransactionOutput (org.bitcoinj.core.TransactionOutput)1 MnemonicException (org.bitcoinj.crypto.MnemonicException)1 Script (org.bitcoinj.script.Script)1 ScriptBuilder (org.bitcoinj.script.ScriptBuilder)1 ScriptException (org.bitcoinj.script.ScriptException)1