Search in sources :

Example 1 with PSBTEntry

use of com.samourai.wallet.cahoots.psbt.PSBTEntry 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)

Aggregations

PSBTEntry (com.samourai.wallet.cahoots.psbt.PSBTEntry)1 SegwitAddress (com.samourai.wallet.segwit.SegwitAddress)1 Triple (org.apache.commons.lang3.tuple.Triple)1 TestNet3Params (org.bitcoinj.params.TestNet3Params)1