Create Raw Transaction in BitcoinJ offline starting from UTXO info

Bitcoin Asked by Andrean on December 19, 2020

I would to create offline Raw Transaction (without signing it) in BitcoinJ. The information I know about the inout is UTXO’s hash, index, amount, address, and script. I also know the receiver address of the final transaction, the change address, the amount and the fee.

I’m fighting with the TransactionOutput constructors to build the input for my transaction. I don’t know where to set UTXO hash and index – i think in the "parent" parameter, but i’m failing. This is my snippet:

Transaction parent = ??? 
Coin value = Coin.valueOf(satoshi(utxo.getAmount()));
Address to = Address.fromBase58(params, utxo.getOwnerAddress());
//where do i put utxo hash and index?
TransactionOutput input = new TransactionOutput(params, parent, value, to);

One Answer

I'm fighting with the TransactionOutput constructors to build the input for my transaction. I don't know where to set UTXO hash and index

I think you can build the input based on a previous transaction output (outpoint) as follows:

byte[] scriptBytes = {};
Sha256Hash vinTxId = Sha256Hash.wrap(txId);
TransactionOutPoint outpoint = new TransactionOutPoint(params, index, vinTxId);
Coin value = null;
TransactionInput input = new TransactionInput(params, transaction, scriptBytes, outpoint, value);

Not sure if this is the best ways to do it.

EDIT: This does the same as the code above.

transaction.addInput(Sha256Hash.wrap(txId), index, ScriptBuilder.createEmpty());

Correct answer by fatdoor on December 19, 2020

Add your own answers!

Related Questions

What kinds of wallets are there?

3  Asked on December 23, 2020 by karlito


From Edge to Poloniex

1  Asked on December 22, 2020 by don-myers


What is the longest firstbits?

1  Asked on December 17, 2020 by lurf-jurv


Proof of address ownership

1  Asked on December 15, 2020 by theruss


Stale block verification at node level

2  Asked on December 10, 2020 by user87441


How to automatically get transaction while sending BTC?

0  Asked on December 2, 2020 by abhishek-pandey


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP