<p>Don't build the entire parent transaction, just build the outpoint, which is all that is relevant for encoding a transaction to hex. <a href="https://github.com/bitcoinj/bitcoinj/blob/master/core/src/main/java/org/bitcoinj/core/TransactionOutPoint.java#L62" rel="nofollow">Specifically use this constructor</a></p>

<p>EDIT: More specifically, cannot recreate the parent transaction unless you have ALL relevant information for that transaction (inputs, locktime, outputs etc..). To use the constructor I linked below you will need to also provide the output index of the output you are spending on your parent transaction.</p>

CC BY-SA 3.0
2016-08-12T17:14:59.957Z
2016-08-12T17:53:22.957Z
2016-08-12T17:53:22.957Z
Chris Stewart
<p>How can I create a raw transaction when I'm given the input scripts, input indexes, input txids, output script, output address and output amount? The method below works but it requires the method Transaction setHash to be public which it is not. Can I get what I want without having to make any changes to Bitcoinj?</p>

<pre><code>    NetworkParameters params = MainNetParams.get();
    Transaction tx = new Transaction(params);
    tx.addOutput(Coin.ZERO, new Script(HEX.decode(outputScript)));
    try {
        Address addr = new Address(params, toAddress);
        tx.addOutput(Coin.valueOf(toAmount), addr);
    } catch (AddressFormatException name) {
        return null;
    }

    Script script = new Script(HEX.decode(inputScript));
    Address addr = new Address(params, script.getPubKeyHash());

    Transaction parentTx = new Transaction(params);
    for (int j = 0; j &lt; inputIndex; j++) {
        parentTx.addOutput(new TransactionOutput(params, parentTx, Coin.ZERO, addr));
    }
    parentTx.addOutput(new TransactionOutput(params, parentTx, Coin.ZERO, addr));
    parentTx.setHash(Sha256Hash.wrap(txid)); // requires setHash method to be public
    try {
        ECKey key = new DumpedPrivateKey(params, privateKey).getKey();
        tx.addSignedInput(parentTx.getOutput(inputIndex), key);
    } catch (AddressFormatException name) {
        return null;
    }
    return HEX.encode(tx.bitcoinSerialize());
</code></pre>

CC BY-SA 4.0
2020-08-18T12:47:46.947Z
What is inputScript and txid?
Pawan Maurya
<transactions><bitcoinj><java>
How can I create a raw transaction in hex format with Bitcoinj?
user299648
# How can I create a raw transaction in hex format with Bitcoinj?

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

How can I create a raw transaction when I'm given the input scripts, input indexes, input txids, output script, output address and output amount? The method below works but it requires the method Transaction setHash to be public which it is not. Can I get what I want without having to make any changes to Bitcoinj?

```
    NetworkParameters params = MainNetParams.get();
    Transaction tx = new Transaction(params);
    tx.addOutput(Coin.ZERO, new Script(HEX.decode(outputScript)));
    try {
        Address addr = new Address(params, toAddress);
        tx.addOutput(Coin.valueOf(toAmount), addr);
    } catch (AddressFormatException name) {
        return null;
    }

    Script script = new Script(HEX.decode(inputScript));
    Address addr = new Address(params, script.getPubKeyHash());

    Transaction parentTx = new Transaction(params);
    for (int j = 0; j < inputIndex; j++) {
        parentTx.addOutput(new TransactionOutput(params, parentTx, Coin.ZERO, addr));
    }
    parentTx.addOutput(new TransactionOutput(params, parentTx, Coin.ZERO, addr));
    parentTx.setHash(Sha256Hash.wrap(txid)); // requires setHash method to be public
    try {
        ECKey key = new DumpedPrivateKey(params, privateKey).getKey();
        tx.addSignedInput(parentTx.getOutput(inputIndex), key);
    } catch (AddressFormatException name) {
        return null;
    }
    return HEX.encode(tx.bitcoinSerialize());

```

**Tags:** transactions, bitcoinj, java

> **Comments:**
> **username_1**: What is inputScript and txid?  

---

## 1 answers

### Answer by username_2 on 2016-08-12. Score: 2 | Accepted answer

Don't build the entire parent transaction, just build the outpoint, which is all that is relevant for encoding a transaction to hex. [Specifically use this constructor](https://github.com/bitcoinj/bitcoinj/blob/master/core/src/main/java/org/bitcoinj/core/TransactionOutPoint.java#L62)

EDIT: More specifically, cannot recreate the parent transaction unless you have ALL relevant information for that transaction (inputs, locktime, outputs etc..). To use the constructor I linked below you will need to also provide the output index of the output you are spending on your parent transaction.


---

