0
A part of my MSc thesis is storing the hash of a string with OP_RETURN tx in the Blockchain to exist as a proof.
My main concerns are:
-how can i send this tx successfully to my regtest network
-how can i see (with a RPC) if this tx happend
I have already stored the string to a file and hash it afterwards. But I have difficulties with connecting my Java with the Regtest network.
Here is my code:
public class Main {
public static RegTestParams params = RegTestParams.get();
static BlockStore bs;
static Block b;
public static Context context = new Context(params);
public static WalletAppKit bitcoin;
public static void main(String[] args) throws InterruptedException, ExecutionException, BlockStoreException {
WalletAppKit kit = new WalletAppKit(RegTestParams.get(), new java.io.File("."), "test");
kit.startAsync();
Wallet wallet = new Wallet(params);
BlockChain chain = kit.chain();
bs = chain.getBlockStore();
Peer peer = kit.peerGroup().getDownloadPeer();
b = peer.getBlock(bs.getChainHead().getHeader().getHash()).get();
peer.addWallet(wallet);
Address myAddress = new Address(params, "n4MN27Lk7Yh3pwfjCiAbRXtRVjs4Uk67fG");
Writer w = new Writer();
final File results = new File("C:\\Users\\Maria\\workspace\\blockInfo\\results.txt");
File writtenFile;
Sha256Hash resultHash;
// write the results of my draw to a file
writtenFile = w.writeToFile(results);
// hash the contents of the file
resultHash = w.hashTheFile(writtenFile);
System.out.println(resultHash);
SendRequest req;
Transaction transaction = new Transaction(RegTestParams.get());
// the following statement will help to create an OP_RETURN with
// resultHash as the message
transaction.addOutput(Coin.ZERO, ScriptBuilder.createOpReturnScript(resultHash.getBytes()));
req = SendRequest.forTx(transaction);
}
}
I think you're still missing a few steps, for example I can't see where you set the funding transaction hash. Apart from that you can check the mempool of your bitcoin node using: bitcoin-cli -regtest getrawmempool. If it's empty then your tx wasn't accepted. – sipwiz – 2017-11-10T11:50:30.817
@sipwiz the RPC worked and of course it is yet empty. How can i set the funding transaction hash?Could you give a short snippet of code with OP_RETURN? – gtopal – 2017-11-10T16:10:35.810
There are a few steps required to work with the regtest network. Have you seen https://bitcoin.org/en/developer-examples#regtest-mode?
– sipwiz – 2017-11-10T21:03:20.373@sipwiz I have seen the examples. My problem is how to establish a proper connection with Java in order to catch the results afterwards in regtest network. The RPCs can work alone apart from java code. – gtopal – 2017-11-12T09:27:36.653
I've just an added an answer which is hopefully close to what you are attempting to do. The main thing you should look at is the getting an input for your transaction. You can't spend an output if you don't fund the tx. – sipwiz – 2017-11-12T09:59:39.047