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());