Search in sources :

Example 26 with SegwitAddress

use of com.samourai.wallet.segwit.SegwitAddress in project samourai-wallet-android by Samourai-Wallet.

the class STONEWALLx2 method doStep2.

// 
// sender
// 
protected boolean doStep2(HashMap<_TransactionOutPoint, Triple<byte[], byte[], String>> inputs, HashMap<_TransactionOutput, Triple<byte[], byte[], String>> outputs) throws Exception {
    Transaction transaction = psbt.getTransaction();
    Log.d("STONEWALLx2", "step2 tx:" + transaction.toString());
    Log.d("STONEWALLx2", "step2 tx:" + Hex.toHexString(transaction.bitcoinSerialize()));
    for (_TransactionOutPoint outpoint : inputs.keySet()) {
        Log.d("STONEWALLx2", "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);
    }
    TransactionOutput _output = null;
    if (FormatsUtil.getInstance().isValidBitcoinAddress(strDestination)) {
        if (FormatsUtil.getInstance().isValidBech32(strDestination)) {
            Pair<Byte, byte[]> pair = Bech32Segwit.decode(params instanceof TestNet3Params ? "tb" : "bc", strDestination);
            byte[] scriptPubKey = Bech32Segwit.getScriptPubkey(pair.getLeft(), pair.getRight());
            _output = new TransactionOutput(params, null, Coin.valueOf(spendAmount), scriptPubKey);
        } else {
            Script toOutputScript = ScriptBuilder.createOutputScript(org.bitcoinj.core.Address.fromBase58(SamouraiWallet.getInstance().getCurrentNetworkParams(), strDestination));
            _output = new TransactionOutput(SamouraiWallet.getInstance().getCurrentNetworkParams(), null, Coin.valueOf(spendAmount), toOutputScript.getProgram());
        }
        transaction.addOutput(_output);
    } else {
        return false;
    }
    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 : Script(org.bitcoinj.script.Script) SegwitAddress(com.samourai.wallet.segwit.SegwitAddress) Triple(org.apache.commons.lang3.tuple.Triple) TestNet3Params(org.bitcoinj.params.TestNet3Params)

Example 27 with SegwitAddress

use of com.samourai.wallet.segwit.SegwitAddress in project samourai-wallet-android by Samourai-Wallet.

the class STONEWALLx2 method doStep1.

// 
// counterparty
// 
protected boolean doStep1(HashMap<_TransactionOutPoint, Triple<byte[], byte[], String>> inputs, HashMap<_TransactionOutput, Triple<byte[], byte[], String>> outputs) throws Exception {
    if (this.getStep() != 0 || this.getSpendAmount() == 0L) {
        return false;
    }
    if (this.getType() == Cahoots.CAHOOTS_STONEWALLx2 && outputs == null) {
        return false;
    }
    Transaction transaction = new Transaction(params);
    for (_TransactionOutPoint outpoint : inputs.keySet()) {
        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 psbt = new PSBT(transaction);
    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, cptyAccount, 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, cptyAccount, Integer.valueOf(s[1]), Integer.valueOf(s[2])));
    }
    this.psbt = psbt;
    // Log.d("STONEWALLx2", "input value:" + psbt.getTransaction().getInputs().get(0).getValue().longValue());
    this.setStep(1);
    return true;
}
Also used : PSBT(com.samourai.wallet.cahoots.psbt.PSBT) Triple(org.apache.commons.lang3.tuple.Triple) TestNet3Params(org.bitcoinj.params.TestNet3Params) SegwitAddress(com.samourai.wallet.segwit.SegwitAddress)

Example 28 with SegwitAddress

use of com.samourai.wallet.segwit.SegwitAddress 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 29 with SegwitAddress

use of com.samourai.wallet.segwit.SegwitAddress in project samourai-wallet-android by Samourai-Wallet.

the class AndroidTx0Service method signTx0.

@Override
protected void signTx0(Transaction tx, Collection<UnspentOutputWithKey> inputs, NetworkParameters params) {
    int idx = 0;
    for (UnspentOutputWithKey input : inputs) {
        String address = input.addr;
        ECKey spendFromKey = ECKey.fromPrivate(input.getKey());
        // sign input
        if (FormatsUtil.getInstance().isValidBech32(address) || Address.fromBase58(params, address).isP2SHAddress()) {
            SegwitAddress segwitAddress = new SegwitAddress(spendFromKey.getPubKey(), params);
            final Script redeemScript = segwitAddress.segWitRedeemScript();
            final Script scriptCode = redeemScript.scriptCode();
            TransactionSignature sig = tx.calculateWitnessSignature(idx, spendFromKey, scriptCode, Coin.valueOf(input.value), Transaction.SigHash.ALL, false);
            final TransactionWitness witness = new TransactionWitness(2);
            witness.setPush(0, sig.encodeToBitcoin());
            witness.setPush(1, spendFromKey.getPubKey());
            tx.setWitness(idx, witness);
            if (!FormatsUtil.getInstance().isValidBech32(address) && Address.fromBase58(params, address).isP2SHAddress()) {
                final ScriptBuilder sigScript = new ScriptBuilder();
                sigScript.data(redeemScript.getProgram());
                tx.getInput(idx).setScriptSig(sigScript.build());
            // tx.getInput(idx).getScriptSig().correctlySpends(tx, idx, new Script(Hex.decode(input.script)), Coin.valueOf(input.value), Script.ALL_VERIFY_FLAGS);
            }
        } else {
            TransactionSignature sig = tx.calculateSignature(idx, spendFromKey, new Script(Hex.decode(input.script)), Transaction.SigHash.ALL, false);
            tx.getInput(idx).setScriptSig(ScriptBuilder.createInputScript(sig, spendFromKey));
        }
        idx++;
    }
    super.signTx0(tx, inputs, params);
}
Also used : Script(org.bitcoinj.script.Script) TransactionWitness(org.bitcoinj.core.TransactionWitness) SegwitAddress(com.samourai.wallet.segwit.SegwitAddress) ECKey(org.bitcoinj.core.ECKey) TransactionSignature(org.bitcoinj.crypto.TransactionSignature) ScriptBuilder(org.bitcoinj.script.ScriptBuilder) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint) MyTransactionOutPoint(com.samourai.wallet.send.MyTransactionOutPoint)

Example 30 with SegwitAddress

use of com.samourai.wallet.segwit.SegwitAddress in project samourai-wallet-android by Samourai-Wallet.

the class WhirlpoolWalletTest method testTx0.

@Test
public void testTx0() throws Exception {
    Collection<UnspentOutputWithKey> spendFroms = new LinkedList<>();
    ECKey ecKey = bip84w.getAccountAt(0).getChain(0).getAddressAt(61).getECKey();
    UnspentResponse.UnspentOutput unspentOutput = newUnspentOutput("cc588cdcb368f894a41c372d1f905770b61ecb3fb8e5e01a97e7cedbf5e324ae", 1, 500000000);
    unspentOutput.addr = new SegwitAddress(ecKey, networkParameters).getBech32AsString();
    spendFroms.add(new UnspentOutputWithKey(unspentOutput, ecKey.getPrivKeyBytes()));
    Pool pool = whirlpoolWallet.findPoolById("0.01btc");
    Tx0Config tx0Config = whirlpoolWallet.getTx0Config().setMaxOutputs(1);
    Tx0Preview tx0Preview = whirlpoolWallet.tx0Preview(pool, spendFroms, tx0Config, Tx0FeeTarget.BLOCKS_2);
    Tx0 tx0 = whirlpoolWallet.tx0(pool, spendFroms, tx0Config, Tx0FeeTarget.BLOCKS_2);
    Assert.assertEquals("dc398c99cf9ce18123ea916d69bb99da44a3979a625eeaac5e17837f879a8874", tx0.getTx().getHashAsString());
    Assert.assertEquals("01000000000101ae24e3f5dbcee7971ae0e5b83fcb1eb67057901f2d371ca494f868b3dc8c58cc0100000000ffffffff040000000000000000426a408a9eb379a44ff4d4579118c64b64bbd327cd95ba826ac68f334155fd9ca4e3acd64acdfd75dd7c3cc5bc34d31af6c6e68b4db37eac62b574890f6cfc7b904d9950c300000000000016001441021632871b0f1cf61a7ac7b6a0187e88628291b44b0f00000000001600147e4a4628dd8fbd638681a728e39f7d92ada04070e954bd1d00000000160014df3a4bc83635917ad18621f3ba78cef6469c5f5902483045022100c48f02762ab9877533b5c7b0bc729479ce7809596b89cb9f62b740ea3350068f02205ef46ca67df39d35f940e33223c5ddd56669d953b6ef4948e355c1f3430f32e10121032e46baef8bcde0c3a19cadb378197fa31d69adb21535de3f84de699a1cf88b4500000000", new String(Hex.encode(tx0.getTx().bitcoinSerialize())));
}
Also used : SegwitAddress(com.samourai.wallet.segwit.SegwitAddress) ECKey(org.bitcoinj.core.ECKey) Pool(com.samourai.whirlpool.client.whirlpool.beans.Pool) Tx0Preview(com.samourai.whirlpool.client.tx0.Tx0Preview) UnspentOutputWithKey(com.samourai.whirlpool.client.tx0.UnspentOutputWithKey) Tx0(com.samourai.whirlpool.client.tx0.Tx0) LinkedList(java.util.LinkedList) UnspentResponse(com.samourai.wallet.api.backend.beans.UnspentResponse) Tx0Config(com.samourai.whirlpool.client.tx0.Tx0Config) Test(org.junit.Test)

Aggregations

SegwitAddress (com.samourai.wallet.segwit.SegwitAddress)30 ECKey (org.bitcoinj.core.ECKey)17 HashMap (java.util.HashMap)12 JSONException (org.json.JSONException)12 MyTransactionOutPoint (com.samourai.wallet.send.MyTransactionOutPoint)11 HD_Address (com.samourai.wallet.hd.HD_Address)10 IOException (java.io.IOException)10 MnemonicException (org.bitcoinj.crypto.MnemonicException)10 JSONObject (org.json.JSONObject)9 Triple (org.apache.commons.lang3.tuple.Triple)8 Script (org.bitcoinj.script.Script)8 ArrayList (java.util.ArrayList)7 TransactionOutPoint (org.bitcoinj.core.TransactionOutPoint)7 AlertDialog (android.app.AlertDialog)6 DialogInterface (android.content.DialogInterface)5 WriterException (com.google.zxing.WriterException)5 PaymentAddress (com.samourai.wallet.bip47.rpc.PaymentAddress)5 PaymentCode (com.samourai.wallet.bip47.rpc.PaymentCode)5 UTXO (com.samourai.wallet.send.UTXO)5 TransactionSignature (org.bitcoinj.crypto.TransactionSignature)5