<p>Sure :)</p>

<p>My solution was pretty simple:</p>

<pre><code>//String to a private key
DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, 
        privKey);
ECKey key = dumpedPrivateKey.getKey();

//String to an address
Address address2 = Address.fromBase58(params, address);

Transaction tx = new Transaction(params);
//value is a sum of all inputs, fee is 4013
tx.addOutput(Coin.valueOf(amount-4013), address2);

//utxos is an array of inputs from my wallet
for(UTXO utxo : utxos)
{
    TransactionOutPoint outPoint = new TransactionOutPoint(params, utxo.getIndex(), utxo.getHash());
    //YOU HAVE TO CHANGE THIS
    tx.addSignedInput(outPoint, utxo.getScript(), key, Transaction.SigHash.ALL, true);
}

tx.getConfidence().setSource(TransactionConfidence.Source.SELF);
tx.setPurpose(Transaction.Purpose.USER_PAYMENT);

System.out.println(tx.getHashAsString());
b_peerGroup.GetPeerGroup().broadcastTransaction(tx);
</code></pre>

CC BY-SA 4.0
2020-08-18T12:50:20.473Z
How you create UTXO?
Pawan Maurya
CC BY-SA 4.0
2020-11-22T21:07:24.117Z
Pawan, you need to fetch it before by listunspent api request
Oleksandr Loushkin
CC BY-SA 3.0
2016-09-22T08:06:03.783Z
2016-09-22T08:06:03.783Z
Dragomirus
<p>I've started creating my own wallet in bitcoinj and I have serious problem. I want to create raw transaction without wallet object. So I wrote this piece of code:</p>

<pre><code>    //String to a private key
    DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, 
            privKey);
    ECKey key = dumpedPrivateKey.getKey();

    //String to an address
    Address address2 = Address.fromBase58(params, address);

    Transaction tx = new Transaction(params);
    //value is a sum of all inputs, fee is 4013
    tx.addOutput(Coin.valueOf(amount-4013), address2);

    //utxos is an array of inputs from my wallet
    for(UTXO utxo : utxos)
    {
        TransactionOutPoint outPoint = new TransactionOutPoint(params, utxo.getIndex(), utxo.getHash());
        tx.addSignedInput(outPoint, utxo.getScript(), key);
    }

    tx.getConfidence().setSource(TransactionConfidence.Source.SELF);
    tx.setPurpose(Transaction.Purpose.USER_PAYMENT);

    System.out.println(tx.getHashAsString());
    b_peerGroup.GetPeerGroup().broadcastTransaction(tx);
</code></pre>

<p>But this ends with:</p>

<blockquote>
  <p>mandatory-script-verify-flag-failed (Script evaluated without error but finished with a false/empty top stack e</p>
</blockquote>

<p>What is wrong with my code?</p>

<p>EDIT:
I found a solution. I've changed only this line: tx.addSignedInput(outPoint, utxo.getScript(), key, Transaction.SigHash.ALL, true);</p>

CC BY-SA 3.0
2016-09-07T09:14:03.290Z
http://bitcoin.stackexchange.com/questions/47928/how-can-i-create-a-raw-transaction-in-hex-format-with-bitcoinj This might help?
hartmut
CC BY-SA 3.0
2016-09-07T10:06:01.463Z
Unfortunately I've already used this code :(
Dragomirus
CC BY-SA 3.0
2016-09-07T16:21:00.930Z
I believe this means that you provided the right public key, but the signature does not match the transaction. Is the transaction being modified between when it is signed and when it is broadcast?
Nick ODell
CC BY-SA 3.0
2016-09-08T08:26:40.230Z
Already found the solution :)
All is under edit
Dragomirus
CC BY-SA 3.0
2016-09-21T14:05:36.997Z
Could you please describe your solution in an answer post rather instead of putting it in the question? Thanks.
Murch
<transactions><bitcoinj><raw>
How to create raw transaction
Dragomirus
# How to create raw transaction

(Asked by: username_0 on 2016-09-07)

I've started creating my own wallet in bitcoinj and I have serious problem. I want to create raw transaction without wallet object. So I wrote this piece of code:

```
    //String to a private key
    DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, 
            privKey);
    ECKey key = dumpedPrivateKey.getKey();

    //String to an address
    Address address2 = Address.fromBase58(params, address);

    Transaction tx = new Transaction(params);
    //value is a sum of all inputs, fee is 4013
    tx.addOutput(Coin.valueOf(amount-4013), address2);

    //utxos is an array of inputs from my wallet
    for(UTXO utxo : utxos)
    {
        TransactionOutPoint outPoint = new TransactionOutPoint(params, utxo.getIndex(), utxo.getHash());
        tx.addSignedInput(outPoint, utxo.getScript(), key);
    }

    tx.getConfidence().setSource(TransactionConfidence.Source.SELF);
    tx.setPurpose(Transaction.Purpose.USER_PAYMENT);

    System.out.println(tx.getHashAsString());
    b_peerGroup.GetPeerGroup().broadcastTransaction(tx);

```

But this ends with:

> mandatory-script-verify-flag-failed (Script evaluated without error but finished with a false/empty top stack e

What is wrong with my code?

EDIT:
I found a solution. I've changed only this line: tx.addSignedInput(outPoint, utxo.getScript(), key, Transaction.SigHash.ALL, true);

**Tags:** transactions, bitcoinj, raw

> **Comments:**
> **username_1**: http://bitcoin.stackexchange.com/questions/47928/how-can-i-create-a-raw-transaction-in-hex-format-with-bitcoinj This might help?  
> **username_0**: Unfortunately I've already used this code :(  
> **username_2**: I believe this means that you provided the right public key, but the signature does not match the transaction. Is the transaction being modified between when it is signed and when it is broadcast?  
> **username_0**: Already found the solution :) All is under edit  
> **username_3**: Could you please describe your solution in an answer post rather instead of putting it in the question? Thanks.  

---

## 1 answers

### Answer by username_0 on 2016-09-22. Score: 2

Sure :)

My solution was pretty simple:

```
//String to a private key
DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, 
        privKey);
ECKey key = dumpedPrivateKey.getKey();

//String to an address
Address address2 = Address.fromBase58(params, address);

Transaction tx = new Transaction(params);
//value is a sum of all inputs, fee is 4013
tx.addOutput(Coin.valueOf(amount-4013), address2);

//utxos is an array of inputs from my wallet
for(UTXO utxo : utxos)
{
    TransactionOutPoint outPoint = new TransactionOutPoint(params, utxo.getIndex(), utxo.getHash());
    //YOU HAVE TO CHANGE THIS
    tx.addSignedInput(outPoint, utxo.getScript(), key, Transaction.SigHash.ALL, true);
}

tx.getConfidence().setSource(TransactionConfidence.Source.SELF);
tx.setPurpose(Transaction.Purpose.USER_PAYMENT);

System.out.println(tx.getHashAsString());
b_peerGroup.GetPeerGroup().broadcastTransaction(tx);

```

> **Comments:**
> **username_4**: How you create UTXO?  
> **username_5**: username_4, you need to fetch it before by listunspent api request  

---

