4

This might be a possible duplicate with this.

I want to know a proper way to insert additional string with OP_RETURN and retrieve the transaction from block chain and decode it accordingly.

Thank you

Rasela Don
  • 339
  • 1
  • 3
  • 8

2 Answers2

9

How do I add metadata to the blockchain?

Essentially, one of the output scripts should be

OP_RETURN <the data you want to add>

That's it.

Let me give an example. Say you want to add the string "Melons." to the blockchain.

  1. Convert to binary
    "Melons." -> 4d656c6f6e732e
  2. Add the push-data-onto-stack command at the start. This is the length of your metadata if it's less than 75 bytes, or OP_PUSHDATAx otherwise.
    074d656c6f6e732e
  3. Add OP_RETURN at the start
    6a074d656c6f6e732e
  4. Put this in the scriptPubKey of an output of a transaction. I wrote a guide on this here.

How do I find it again?

That's... trickier. Bitcoin is not optimized for searching the metadata of transactions.

Possibilities:

  • Look through all of the transactions you get with something like bitcointools.
  • Use a website like coinsecrets.org.
Nick ODell
  • 29,396
  • 11
  • 72
  • 130
  • So to find the data again, we should traverse through the blockchain, find OP_RETURN patterns, grab associated data, index them ..., right? – anhldbk Feb 07 '17 at 15:27
1

You might find our OP_RETURN libraries useful, for PHP and Python. If you look at the code you can also see exactly how they work.