<p>You might find our python-OP_RETURN library helpful, either to use out-of-the-box, or to look inside to see how we are building OP_RETURN transactions.</p>

<p><a href="https://github.com/coinspark/python-OP_RETURN" rel="nofollow">https://github.com/coinspark/python-OP_RETURN</a></p>

<p>It also has a neat feature to store arbitrary-sized data in the blockchain using multiple chained transactions with OP_RETURNs, and to retrieve that data using a single 12-digit reference number.</p>

CC BY-SA 3.0
2015-07-01T23:08:18.723Z
Looks like this could help , thanks. I don't plan on downloading bitcoin core however. From what I can see it can be used as a library ? Where does the private key go ?
Jay Sea
CC BY-SA 3.0
2015-07-02T06:14:38.573Z
Currently it requires Bitcoin Core. Although we are also working on a version of SparkBit (a lightweight SPV client) which will be usable together with python-OP_RETURN to do this without requiring Bitcoin Core.
Gideon Greenspan
CC BY-SA 3.0
2015-07-01T08:03:54.537Z
2015-07-01T08:03:54.537Z
Gideon Greenspan
<p>From <a href="https://blockchain.info/tx/e90acad12b19d7b9a4760a5af4603a9da06e6231e699072bcb71aced795abd62" rel="nofollow">e90acad12b19d7b9a4760a5af4603a9da06e6231e699072bcb71aced795abd62</a>:</p>

<p><code>deserialize(str(blockr_fetchtx("e90acad12b19d7b9a4760a5af4603a9da06e6231e699072bcb71aced795abd62")))</code></p>

<pre><code>{'ins': [{'outpoint': {'hash': 'a5221ad4b031d9487dfb2c334b3d84a3c33d908e5f1089141a72b2f4c98baf55',
    'index': 0},
   'script': '483045022100e684df2de15562b09f267d6ec25a1f8043e0162fb7426e40ee87a1a0b51a4ab8022009069aced5a4a33ac720dbbafad37b36e1
087ea9578b1ec9cd14ea4367debc0f01410466c719272fda4368a44cde914dd210b3dcf1913b700b1fdd2948f6d44acb404c250d172705bd297d9a3fcc4ddb9e
0d3fe2c52b870e31eb035f650921bd68d73c',
   'sequence': 4294967295L},
  {'outpoint': {'hash': 'a5221ad4b031d9487dfb2c334b3d84a3c33d908e5f1089141a72b2f4c98baf55',
    'index': 2},
   'script': '483045022008e0edd19548f7098db140d703fd60e00b184c7d4746da57fdb36f5ff573bc080221008203612b6bf02e9e15bde7e412085323e4
d561761ddc2632f5f68a0dc057aebd01410466c719272fda4368a44cde914dd210b3dcf1913b700b1fdd2948f6d44acb404c250d172705bd297d9a3fcc4ddb9e
0d3fe2c52b870e31eb035f650921bd68d73c',
   'sequence': 4294967295L}],
 'locktime': 0,
 'outs': [{'script': '76a91497734226bd4c9c7764771a077e775e8581e030e988ac',
   'value': 50000},
  {'script': '6a4c0848656c6c6f526179', 'value': 0},
  {'script': '76a91497734226bd4c9c7764771a077e775e8581e030e988ac',
   'value': 210477}],
 'version': 1}
</code></pre>

<p><code>{'script': '6a4c0848656c6c6f526179', 'value': 0}</code></p>

<p>That OP_RETURN is Non-Standard, which is the error message BCI is throwing. It's the <code>4c</code> preceding <code>6a</code> that's the issue: if a push data code is not needed, then the Tx is non-standard. It should just read <code>6a48656c6c6f526179</code>, or in the serialized Tx it'd be <code>096a48656c6c6f526179</code> instead of <code>0b6a4c0848656c6c6f526179</code>. </p>

CC BY-SA 3.0
2015-04-29T16:47:59.627Z
That is one of the transactions that went through. I was simply messing around trying to see if adding things to the beginning of the script made a difference. You can see that in the #snd = "6a4c"+MessageLen+ID. I have no idea why that one worked. As the program looks now the script is correct but still not uploading anything even when its 6a0848656c6c6f726179. Any thoughts ?
Jay Sea
CC BY-SA 3.0
2015-04-30T05:22:34.277Z
Have you got the `0b` in front of it? That's crucial as it's saying *push 11 bytes* (the message, op_return and varint = 9+1+1)
Wizard Of Ozzie
CC BY-SA 3.0
2015-04-30T15:08:11.543Z
There is no 0b in front. I am simply running the code I posted above. I can message you the private key with a few dollars on it if you would like to run it yourself.
Jay Sea
CC BY-SA 3.0
2015-05-01T04:33:00.400Z
read the comment. I'll email you tomorrow. Thanks.
Jay Sea
CC BY-SA 3.0
2015-05-19T05:15:44.493Z
@jaysea I've forked pybitcointools so you can use the `mk_opreturn` function, as discussed: https:// github.com/simcity4242/pybitcointools. I'll detail a signed example Tx here shortly
Wizard Of Ozzie
CC BY-SA 3.0
2015-04-29T11:18:33.687Z
2015-04-29T11:18:33.687Z
Wizard Of Ozzie
<p>I'm trying to make a simple program that can reliably upload an OP_RETURN transaction. I have found some examples online, but so far they don't work consistently. The ones that go through seem to be random. Here are the transactions that went through </p>

<p><a href="https://blockchain.info/address/1Eoo7G7u97EMwptSxjYjpvdx9zYcqJBn9y" rel="nofollow">https://blockchain.info/address/1Eoo7G7u97EMwptSxjYjpvdx9zYcqJBn9y</a></p>

<p>It is very hit or miss every time I try. I have been entirely unable to upload any transactions using the script, however once in a while if I upload the raw transaction using blockchain they go through. Any ideas? Here is the error message. </p>

<pre><code>Traceback (most recent call last):
  File "/Users/JC/Documents/DevelopmentStuff/programming /Python/bit/op-return_test2.py", line 51, in &lt;module&gt;
    print(pushtx(tx))
  File "/Library/Python/2.7/site-packages/bitcoin/bci.py", line 237, in pushtx
    return f(*args)
  File "/Library/Python/2.7/site-packages/bitcoin/bci.py", line 192, in bci_pushtx
    return make_request('https://blockchain.info/pushtx', 'tx='+tx)
  File "/Library/Python/2.7/site-packages/bitcoin/bci.py", line 23, in make_request
    raise Exception(p)
Exception: None Standard Script Output OP_RETURN 48656c6c6f
</code></pre>

<p>Here is the code. I would really appreciate any help. Thanks</p>

<pre><code># coding: utf-8

from bitcoin import *
import binascii
from test import *



priv = "Private key"

pub = "1Eoo7G7u97EMwptSxjYjpvdx9zYcqJBn9y"

addr= "1Eoo7G7u97EMwptSxjYjpvdx9zYcqJBn9y"
inputs = unspent(addr)

message = "Hello"
FullLen = format(len(message)+2,'x').rjust(2,'0') 
MessageLen = format(len(message),'x').rjust(2,'0')
ID = binascii.hexlify(str(message))
#snd = "6a4c28"+ID
snd = "6a"+MessageLen+ID
print hexlify(chr(len(message)))
print MessageLen
print snd
#snd = "6a4c"+MessageLen+ID
#print snd
newScriptPubKey = "6a" + hexlify(chr(len(message)))+ hexlify(message)
#print newScriptPubKey

outputs = [{'value': 50000, 'address': addr}, {'value': 0, 'script': snd}]

fee = 10000

tx = mksend(inputs, outputs, addr, fee)

dt = deserialize(tx)
ins = dt['ins']

#print addr
#print ins

print tx
for ind, elm in enumerate(ins):
    print elm
    tx = sign(tx, ind, priv)

print tx



print(pushtx(tx)) # Error is raised on this line
</code></pre>

CC BY-SA 3.0
2015-04-29T03:35:33.533Z
What line specifically does it error on? Can you post the full traceback?
Nick ODell
CC BY-SA 3.0
2015-04-29T03:47:12.703Z
In the future, please edit it in instead of putting it in a comment.
Nick ODell
CC BY-SA 3.0
2015-04-29T03:49:20.287Z
Yeah sorry I'm trying to reformat it. How do I do this in the comments ? I just deleted it so I can format it correctly.
Jay Sea
CC BY-SA 3.0
2015-04-29T03:50:01.380Z
You can't edit comments.
Nick ODell
CC BY-SA 3.0
2015-04-29T03:52:25.287Z
Alright well thanks to whoever added it to the original post.
Jay Sea
<python><op-return>
OP_RETURN using python
Jay Sea
# OP_RETURN using python

(Asked by: username_0 on 2015-04-29)

I'm trying to make a simple program that can reliably upload an OP\_RETURN transaction. I have found some examples online, but so far they don't work consistently. The ones that go through seem to be random. Here are the transactions that went through

<https://blockchain.info/address/1Eoo7G7u97EMwptSxjYjpvdx9zYcqJBn9y>

It is very hit or miss every time I try. I have been entirely unable to upload any transactions using the script, however once in a while if I upload the raw transaction using blockchain they go through. Any ideas? Here is the error message.

```
Traceback (most recent call last):
  File "/Users/JC/Documents/DevelopmentStuff/programming /Python/bit/op-return_test2.py", line 51, in <module>
    print(pushtx(tx))
  File "/Library/Python/2.7/site-packages/bitcoin/bci.py", line 237, in pushtx
    return f(*args)
  File "/Library/Python/2.7/site-packages/bitcoin/bci.py", line 192, in bci_pushtx
    return make_request('https://blockchain.info/pushtx', 'tx='+tx)
  File "/Library/Python/2.7/site-packages/bitcoin/bci.py", line 23, in make_request
    raise Exception(p)
Exception: None Standard Script Output OP_RETURN 48656c6c6f

```

Here is the code. I would really appreciate any help. Thanks

```
# coding: utf-8

from bitcoin import *
import binascii
from test import *

priv = "Private key"

pub = "1Eoo7G7u97EMwptSxjYjpvdx9zYcqJBn9y"

addr= "1Eoo7G7u97EMwptSxjYjpvdx9zYcqJBn9y"
inputs = unspent(addr)

message = "Hello"
FullLen = format(len(message)+2,'x').rjust(2,'0') 
MessageLen = format(len(message),'x').rjust(2,'0')
ID = binascii.hexlify(str(message))
#snd = "6a4c28"+ID
snd = "6a"+MessageLen+ID
print hexlify(chr(len(message)))
print MessageLen
print snd
#snd = "6a4c"+MessageLen+ID
#print snd
newScriptPubKey = "6a" + hexlify(chr(len(message)))+ hexlify(message)
#print newScriptPubKey

outputs = [{'value': 50000, 'address': addr}, {'value': 0, 'script': snd}]

fee = 10000

tx = mksend(inputs, outputs, addr, fee)

dt = deserialize(tx)
ins = dt['ins']

#print addr
#print ins

print tx
for ind, elm in enumerate(ins):
    print elm
    tx = sign(tx, ind, priv)

print tx

print(pushtx(tx)) # Error is raised on this line

```

**Tags:** python, op-return

> **Comments:**
> **username_1**: What line specifically does it error on? Can you post the full traceback?  
> **username_1**: In the future, please edit it in instead of putting it in a comment.  
> **username_0**: Yeah sorry I'm trying to reformat it. How do I do this in the comments ? I just deleted it so I can format it correctly.  
> **username_1**: You can't edit comments.  
> **username_0**: Alright well thanks to whoever added it to the original post.  

---

## 2 answers

### Answer by username_2 on 2015-07-01. Score: 1

You might find our python-OP\_RETURN library helpful, either to use out-of-the-box, or to look inside to see how we are building OP\_RETURN transactions.

<https://github.com/coinspark/python-OP_RETURN>

It also has a neat feature to store arbitrary-sized data in the blockchain using multiple chained transactions with OP\_RETURNs, and to retrieve that data using a single 12-digit reference number.

> **Comments:**
> **username_0**: Looks like this could help , thanks. I don't plan on downloading bitcoin core however. From what I can see it can be used as a library ? Where does the private key go ?  
> **username_2**: Currently it requires Bitcoin Core. Although we are also working on a version of SparkBit (a lightweight SPV client) which will be usable together with python-OP_RETURN to do this without requiring Bitcoin Core.  

---

### Answer by username_3 on 2015-04-29. Score: 1

From [e90acad12b19d7b9a4760a5af4603a9da06e6231e699072bcb71aced795abd62](https://blockchain.info/tx/e90acad12b19d7b9a4760a5af4603a9da06e6231e699072bcb71aced795abd62):

`deserialize(str(blockr_fetchtx("e90acad12b19d7b9a4760a5af4603a9da06e6231e699072bcb71aced795abd62")))`

```
{'ins': [{'outpoint': {'hash': 'a5221ad4b031d9487dfb2c334b3d84a3c33d908e5f1089141a72b2f4c98baf55',
    'index': 0},
   'script': '483045022100e684df2de15562b09f267d6ec25a1f8043e0162fb7426e40ee87a1a0b51a4ab8022009069aced5a4a33ac720dbbafad37b36e1
087ea9578b1ec9cd14ea4367debc0f01410466c719272fda4368a44cde914dd210b3dcf1913b700b1fdd2948f6d44acb404c250d172705bd297d9a3fcc4ddb9e
0d3fe2c52b870e31eb035f650921bd68d73c',
   'sequence': 4294967295L},
  {'outpoint': {'hash': 'a5221ad4b031d9487dfb2c334b3d84a3c33d908e5f1089141a72b2f4c98baf55',
    'index': 2},
   'script': '483045022008e0edd19548f7098db140d703fd60e00b184c7d4746da57fdb36f5ff573bc080221008203612b6bf02e9e15bde7e412085323e4
d561761ddc2632f5f68a0dc057aebd01410466c719272fda4368a44cde914dd210b3dcf1913b700b1fdd2948f6d44acb404c250d172705bd297d9a3fcc4ddb9e
0d3fe2c52b870e31eb035f650921bd68d73c',
   'sequence': 4294967295L}],
 'locktime': 0,
 'outs': [{'script': '76a91497734226bd4c9c7764771a077e775e8581e030e988ac',
   'value': 50000},
  {'script': '6a4c0848656c6c6f526179', 'value': 0},
  {'script': '76a91497734226bd4c9c7764771a077e775e8581e030e988ac',
   'value': 210477}],
 'version': 1}

```

`{'script': '6a4c0848656c6c6f526179', 'value': 0}`

That OP\_RETURN is Non-Standard, which is the error message BCI is throwing. It's the `4c` preceding `6a` that's the issue: if a push data code is not needed, then the Tx is non-standard. It should just read `6a48656c6c6f526179`, or in the serialized Tx it'd be `096a48656c6c6f526179` instead of `0b6a4c0848656c6c6f526179`.

> **Comments:**
> **username_0**: That is one of the transactions that went through. I was simply messing around trying to see if adding things to the beginning of the script made a difference. You can see that in the #snd = "6a4c"+MessageLen+ID. I have no idea why that one worked. As the program looks now the script is correct but still not uploading anything even when its 6a0848656c6c6f726179. Any thoughts ?  
> **username_3**: Have you got the `0b` in front of it? That's crucial as it's saying *push 11 bytes* (the message, op_return and varint = 9+1+1)  
> **username_0**: There is no 0b in front. I am simply running the code I posted above. I can message you the private key with a few dollars on it if you would like to run it yourself.  
> **username_0**: read the comment. I'll email you tomorrow. Thanks.  
> **username_3**: @username_0 I've forked pybitcointools so you can use the `mk_opreturn` function, as discussed: https:// github.com/simcity4242/pybitcointools. I'll detail a signed example Tx here shortly  

---

