AnswerBun.com

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);
tx.addInput(input)

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);
transaction.addInput(input);

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

Whatsminer event code E011

1  Asked on January 2, 2022

   

Bitcoincore wallet 0.20.0

1  Asked on December 31, 2021 by miner-br99

         

Wasabi CoinJoin Produces Non-Existent Coins/Transactions

1  Asked on December 19, 2021 by ramn-j-romero-y-vigil

   

Bitcoin core full node won’t start on mac reboot!

1  Asked on December 12, 2021 by philip-badboy

   

Importing paper wallet into Electrum

0  Asked on December 10, 2021 by thekevinscott

     

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP