Search in sources :

Example 6 with TestNet3Params

use of org.bitcoinj.params.TestNet3Params in project samourai-wallet-android by Samourai-Wallet.

the class Stowaway method doStep2.

// 
// sender
// 
public boolean doStep2(HashMap<_TransactionOutPoint, Triple<byte[], byte[], String>> inputs, HashMap<_TransactionOutput, Triple<byte[], byte[], String>> outputs) throws Exception {
    Transaction transaction = psbt.getTransaction();
    Log.d("Stowaway", "step2 tx:" + transaction.toString());
    Log.d("Stowaway", "step2 tx:" + Hex.toHexString(transaction.bitcoinSerialize()));
    // tx: modify spend output
    long contributedAmount = 0L;
    /*
        for(TransactionInput input : transaction.getInputs())   {
//            Log.d("Stowaway", input.getOutpoint().toString());
            contributedAmount += input.getOutpoint().getValue().longValue();
        }
        */
    for (String outpoint : outpoints.keySet()) {
        contributedAmount += outpoints.get(outpoint);
    }
    long outputAmount = transaction.getOutput(0).getValue().longValue();
    TransactionOutput spendOutput = transaction.getOutput(0);
    spendOutput.setValue(Coin.valueOf(outputAmount + contributedAmount));
    transaction.clearOutputs();
    transaction.addOutput(spendOutput);
    for (_TransactionOutPoint outpoint : inputs.keySet()) {
        Log.d("Stowaway", "outpoint value:" + outpoint.getValue().longValue());
        TransactionInput input = new TransactionInput(params, null, new byte[0], outpoint, outpoint.getValue());
        transaction.addInput(input);
        outpoints.put(outpoint.getTxHash().toString() + "-" + outpoint.getTxOutputN(), outpoint.getValue().longValue());
    }
    for (_TransactionOutput output : outputs.keySet()) {
        transaction.addOutput(output);
    }
    // psbt: modify spend output
    List<PSBTEntry> updatedEntries = new ArrayList<PSBTEntry>();
    for (PSBTEntry entry : psbt.getPsbtInputs()) {
        if (entry.getKeyType()[0] == PSBT.PSBT_IN_WITNESS_UTXO) {
            byte[] data = entry.getData();
            byte[] scriptPubKey = new byte[data.length - Long.BYTES];
            System.arraycopy(data, Long.BYTES, scriptPubKey, 0, scriptPubKey.length);
            entry.setData(PSBT.writeSegwitInputUTXO(outputAmount + contributedAmount, scriptPubKey));
        }
        updatedEntries.add(entry);
    }
    psbt.setPsbtInputs(updatedEntries);
    for (_TransactionOutPoint outpoint : inputs.keySet()) {
        Triple triple = inputs.get(outpoint);
        // input type 1
        SegwitAddress segwitAddress = new SegwitAddress((byte[]) triple.getLeft(), params);
        psbt.addInput(PSBT.PSBT_IN_WITNESS_UTXO, null, PSBT.writeSegwitInputUTXO(outpoint.getValue().longValue(), segwitAddress.segWitRedeemScript().getProgram()));
        // input type 6
        String[] s = ((String) triple.getRight()).split("/");
        psbt.addInput(PSBT.PSBT_IN_BIP32_DERIVATION, (byte[]) triple.getLeft(), PSBT.writeBIP32Derivation((byte[]) triple.getMiddle(), 84, params instanceof TestNet3Params ? 1 : 0, account, Integer.valueOf(s[1]), Integer.valueOf(s[2])));
    }
    for (_TransactionOutput output : outputs.keySet()) {
        Triple triple = outputs.get(output);
        // output type 2
        String[] s = ((String) triple.getRight()).split("/");
        psbt.addOutput(PSBT.PSBT_OUT_BIP32_DERIVATION, (byte[]) triple.getLeft(), PSBT.writeBIP32Derivation((byte[]) triple.getMiddle(), 84, params instanceof TestNet3Params ? 1 : 0, account, Integer.valueOf(s[1]), Integer.valueOf(s[2])));
    }
    psbt.setTransaction(transaction);
    this.setStep(2);
    return true;
}
Also used : SegwitAddress(com.samourai.wallet.segwit.SegwitAddress) PSBTEntry(com.samourai.wallet.cahoots.psbt.PSBTEntry) Triple(org.apache.commons.lang3.tuple.Triple) TestNet3Params(org.bitcoinj.params.TestNet3Params)

Example 7 with TestNet3Params

use of org.bitcoinj.params.TestNet3Params in project samourai-wallet-android by Samourai-Wallet.

the class WhirlpoolTx0 method make.

public void make() throws Exception {
    tx0 = null;
    debug("WhirlpoolTx0", "make: ");
    // 
    if (getChange() < 0L) {
        debug("WhirlpoolTx0", "Cannot make premix: negative change:" + getAmountSelected());
        throw new Exception("Cannot make premix: negative change:" + getAmountSelected());
    }
    if (nbPossiblePremix() < 1) {
        debug("WhirlpoolTx0", "Cannot make premix: insufficient selected amount:" + getAmountSelected());
        throw new Exception("Cannot make premix: insufficient selected amount:" + getAmountSelected());
    }
    debug("WhirlpoolTx0", "amount selected:" + getAmountSelected() / 1e8);
    debug("WhirlpoolTx0", "amount requested:" + ((getPremixRequested() * getPool()) / 1e8));
    debug("WhirlpoolTx0", "nb premix possible:" + nbPossiblePremix());
    debug("WhirlpoolTx0", "amount after Whirlpool fee:" + getAmountAfterWhirlpoolFee() / 1e8);
    debug("WhirlpoolTx0", "fee samourai:" + new DecimalFormat("0.########").format(getFeeSamourai() / 1e8));
    debug("WhirlpoolTx0", "fee miners:" + new DecimalFormat("0.########").format(getFee() / 1e8));
    debug("WhirlpoolTx0", "change amount:" + getChange() / 1e8);
    // [WIP] stub;
    tx0 = new Transaction(SamouraiWallet.getInstance().getCurrentNetworkParams() instanceof TestNet3Params ? TestNet3Params.get() : MainNetParams.get());
}
Also used : TestNet3Params(org.bitcoinj.params.TestNet3Params) Transaction(org.bitcoinj.core.Transaction) DecimalFormat(java.text.DecimalFormat)

Aggregations

TestNet3Params (org.bitcoinj.params.TestNet3Params)7 SegwitAddress (com.samourai.wallet.segwit.SegwitAddress)5 Triple (org.apache.commons.lang3.tuple.Triple)4 PSBT (com.samourai.wallet.cahoots.psbt.PSBT)2 Transaction (org.bitcoinj.core.Transaction)2 Script (org.bitcoinj.script.Script)2 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 PaymentAddress (com.samourai.wallet.bip47.rpc.PaymentAddress)1 PaymentCode (com.samourai.wallet.bip47.rpc.PaymentCode)1 PSBTEntry (com.samourai.wallet.cahoots.psbt.PSBTEntry)1 MyTransactionOutPoint (com.samourai.wallet.send.MyTransactionOutPoint)1 UTXO (com.samourai.wallet.send.UTXO)1 IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 SecureRandom (java.security.SecureRandom)1 DecimalFormat (java.text.DecimalFormat)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1