Transaction with `OP_RETURN` to a particular address

4

How do I formulate a transaction with OP_RETURN to a particular address using the bitcoinj library?

SendRequest req;
Transaction transaction = new Transaction(Main.getNetWorkParameters());
transaction.addOutput(Coin.ZERO, ScriptBuilder.createOpReturnScript(
    "thanks for the coins".toString().getBytes()));

// add a receiving address for this transaction
req = SendRequest.forTx(transaction);

jgm

Posted 2016-06-05T22:58:56.907

Reputation: 748

Answers

3

You can't make an OP_RETURN-based output out to any specific address, since the OP_RETURN opcode marks an output as invalid (thus provably unspendable). In fact, an OP_RETURN-based output will not even enter the UTXO.

A zero amount is okay (and even encouraged) for OP_RETURN. The OP_RETURN is paid for via the fee; putting a non-zero amount in OP_RETURN would just destroy bitcoins, so the miners do not profit from mining it.

However, you can create a transaction that has multiple output scripts: One can be a standard P2PKH of the form OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG while the other can be an output with OP_RETURN. Just call addOutput twice, once with an address and the amount desired, and a second time with parameters for an OP_RETURN script (and a non-zero output value, I would presume).

jgm

Posted 2016-06-05T22:58:56.907

Reputation: 748