[WTB] Teach me to manually create an OP_RETURN tx

Okay, I’ll try. I created f3784ea6df802af5de7bfd8dd6af8eb07cf317b873f09f895586cd09892e897b a few minutes ago and will use this as example.

Start up your Bitcoin-Qt/bitcoind v0.9 client and go Help – Debug. Enter: “getrawtransaction f3784ea6df802af5de7bfd8dd6af8eb07cf317b873f09f895586cd09892e897b”.

You will then see the transaction as hex*:

Code:
01000000015d475fab463fee15d6d32b33151748cbc4eb7c8f06166f50ad77224ddc07bbc8000000006b483045022100fd8c5227976363b79c6c2c72592e98148090ca26d89707f60dfaa19d5c59519f02207a34c47f2a55b9513d152520dd035fb9842d689c0c3350f4c0145e827e61604d01210391b373843e77f5ac1f05db4afb5151190e67cfee5a48f7925d71da7c5e919422ffffffff0100000000000000004e6a4c4b6f64617920697320466562203232203230313420616e642074686973206973206465785820402062697477617463682e636f2e2043686565727320616e64207374617920666c756666792100000000

This is the raw transaction with descriptions:

Code:
01000000          < Version
01                < Number of inputs
5d475fab463fee15d6d32b33151748cbc4eb7c8f06166f50ad77224ddc07bbc8  < Input transaction id
00000000          < Vout
00                < scriptSig length*
ffffffff          < Sequence number
01                < Number of outputs
0000000000000000  < Value
4e                < Push 78 byte (it's 78 hex encoded)
6a                < OP_RETURN 
4c                < Push 76 byte (it's 76 hex encoded)
4974277320466562203232203230313420616e642074686973206973206465785820402062697477617463682e636f2e2043686565727320616c6c20616e64207374617920666c7566667921  < Payload with length of 76 byte
00000000          < Lock time

*I removed the actual scriptSig for this example.

You may notice that some data is flipped, for example the input transaction hash (compare: c8bb07dc4d2277ad506f16068f7cebc4cb481715332bd3d615ee3f46ab5f475d vs. 5d475fab463fee15d6d32b33151748cbc4eb7c8f06166f50ad77224ddc07bbc8).

To create an OP_RETURN transaction yourself, you may use this template:

Code:
0100000001AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABB00000000ffffffff0100000000000000004e6a4cCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC00000000

Replace AA…A with the input transaction hash flipped, BB with vout, CC…C with your payload/message.

Go back to your debug console and type “decoderawtransaction XXX”, whereby XXX is your modified transaction hex. If everything is fine, this should return some information about your transaction.

You may adjust the payload length by changing the 4e and 4c. Use a calculator to convert the numbers.

http://brainwallet.org/#converter has a hex to text and vice versa converter.

When you are finished, type: “signrawtransaction XXX”, whereby XXX is your transaction hex. It should show a longer hex followed by “complete”.

Copy that longer hash and type “sendrawtransaction YYY”, whereby YYY is the signed transaction hex.

Last note: make sure you use only an input with 0.0001-ish BTC. It will all be considered as miner fee unless you add more outputs.

Hope that helps. Smiley

Edit: max length is now 40 byte, so you must adjust the length.

 

Hm? No. OP should use a fresh, unused input.

 

FWIW: you probably know the max. length to push was reduced to 40 byte on most pools.

A few days ago gidgreen published a small PHP tool to construct OP_RETURN transactions:

https://github.com/coinspark/php-OP_RETURN

That being said, abusing the blockchain as message storage is not very popular. Wink

Hi shamoons,

currently only one OP_RETURN output with a size of maximal 40 byte is considered as “accepted standard”. Rather recently the default size of Bitcoin Core was raised to 80 byte, although I have no data whether this is adopted on a broader scale at this moment. The size is not a strict rule, and you’ll find some OP_RETURN transactions with more than 200 byte on testnet, but on mainnet such transactions usually won’t be relayed.

Hey luv2drnkbr,

thanks for the follow up! I kinda feel bad for “getting paid” for this kind of information, because I believe it serves the greater good, but you may use 1BzW1ETp2qZsi6ogAufmGsadnQzJcmVXet.

Thanks a lot! Smiley

And please let me know, if any step is unclear or needs to be refined.

As a related note:

The RPC “createrawtransaction” of Bitcoin Core 0.12 will be extended, and directly supports to create OP_RETURN transactions as follows:

Code:
bitcoin-cli "createrawtransaction" '[{"txid":"hash","vout":n}, ...] '{"data": "0123..."}'

Could you do an example for what would be entered for “data” and “0123….”?  It looks like the same format for a receiving address as a raw transaction.

Code:
createrawtransaction '[{"txid":"","vout":}]' '{"receive_address":0.0008}'

So instead of entering a receiving address and an amount one would enter ‘{“data”:40_bytes}’ ?

Yes, basically it comes down to this. Smiley

Here is an example:

1. First, I list unspent outputs of the address I’m going to use.

Code:
bitcoin-cli listunspent 0 999999 '["1BxtgEa8UcrMzVZaW32zVyJh4Sg4KGFzxA"]'

 

Code:
[
  {
    "txid": "7425deeaebc2b13ff6c6d3cac78332693c86f46433403f65aef028bc88050583",
    "vout": 4,
    "address": "1BxtgEa8UcrMzVZaW32zVyJh4Sg4KGFzxA",
    "account": "Sender15",
    "scriptPubKey": "76a914784345e76ea29fd7bfe31f6f34622e154d0bb8fc88ac",
    "amount": 0.00110691,
    "confirmations": 558,
    "spendable": true
  }
]

2. Then I create the raw transaction. Note that I manually add a change output with a value of 0.00100691, so I pay a fee of exactly 0.0001 BTC. The payload for this transaction is the hex-encoded message “dexX7 @ bitcointalk.org”.

Code:
bitcoin-cli createrawtransaction '[{"txid":"7425deeaebc2b13ff6c6d3cac78332693c86f46433403f65aef028bc88050583","vout":4}]' '{"data":"6465785837204020626974636f696e74616c6b2e6f7267","1BxtgEa8UcrMzVZaW32zVyJh4Sg4KGFzxA":0.00100691}'

 

Code:
010000000183050588bc28f0ae653f403364f4863c693283c7cad3c6f63fb1c2ebeade25740400000000ffffffff020000000000000000196a176465785837204020626974636f696e74616c6b2e6f726753890100000000001976a914784345e76ea29fd7bfe31f6f34622e154d0bb8fc88ac00000000

3. Then I sign the raw transaction.

Code:
bitcoin-cli signrawtransaction 010000000183050588bc28f0ae653f403364f4863c693283c7cad3c6f63fb1c2ebeade25740400000000ffffffff020000000000000000196a176465785837204020626974636f696e74616c6b2e6f726753890100000000001976a914784345e76ea29fd7bfe31f6f34622e154d0bb8fc88ac00000000

 

Code:
{
  "hex": "010000000183050588bc28f0ae653f403364f4863c693283c7cad3c6f63fb1c2ebeade2574040000006a47304402206eaf4d9c526a59bc391329dd62b6231035920ed23ba758a4d9b02d7738c0a77b0220404f00253e7d0202e64a7a8288dc83276218b91454d45ab618408f27b7135ba7012103c86e3c49a99dd688c6c6ad05d725b6a04ef19836b13d3e90ef282fc4f2f9ee71ffffffff020000000000000000196a176465785837204020626974636f696e74616c6b2e6f726753890100000000001976a914784345e76ea29fd7bfe31f6f34622e154d0bb8fc88ac00000000",
  "complete": true
}

4. And finally I broadcast the signed raw transaction.

Code:
bitcoin-cli sendrawtransaction 010000000183050588bc28f0ae653f403364f4863c693283c7cad3c6f63fb1c2ebeade2574040000006a47304402206eaf4d9c526a59bc391329dd62b6231035920ed23ba758a4d9b02d7738c0a77b0220404f00253e7d0202e64a7a8288dc83276218b91454d45ab618408f27b7135ba7012103c86e3c49a99dd688c6c6ad05d725b6a04ef19836b13d3e90ef282fc4f2f9ee71ffffffff020000000000000000196a176465785837204020626974636f696e74616c6b2e6f726753890100000000001976a914784345e76ea29fd7bfe31f6f34622e154d0bb8fc88ac00000000

 

Code:
b394e47f8141f725f25854d57e91fb67d91782dbe581b2f449990ba25a116d2d

5. Here is the result:

https://live.blockcypher.com/btc/tx/b394e47f8141f725f25854d57e91fb67d91782dbe581b2f449990ba25a116d2d/

来源URL:https://bitcointalk.org/index.php?topic=453086.0