5

Currently trying to figure out how to use Python to construct a transaction with OP_RETURN. I tried to encode the message myself, and had no luck. I found a function, OPReturn(), on the internet, but when I attempt to use it, I get the error (from the Blockchain.info broadcast API): Exception: None Standard Script Output OP_RETURN 594f4c4f53574147

Code:

# coding: utf-8

from bitcoin import *
import binascii
from test import *


priv = sha256('brain wallet')

pub = privtopub(priv)

addr = pubtoaddr(pub)

inputs = unspent(addr)

message = "YOLOSWAG"
FullLen = format(len(message)+2,'x').rjust(2,'0')
MessageLen = format(len(message),'x').rjust(2,'0')
ID = binascii.hexlify(str(message))
snd = "6a"+MessageLen+ID

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


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

#print tx



print(pushtx(tx))
  • Note that this code will only work on messages of 75 bytes or less. – Nick ODell Feb 9 '15 at 7:17
  • @NickODell isn't it 40 bytes? (EDIT: you mean 75 bytes total I just realised?) – Wizard Of Ozzie Feb 9 '15 at 9:43
  • What version of Python? I'm pretty sure you're trying to do this on Python 3.x where the struct "q/Q" field is no longer available. Pybitcointools will require Python 2.7 (in fact virtually all Bitcoin Python stuff is version 2.x due to the way it handles strings as bytes without binascii and such. – Wizard Of Ozzie Feb 9 '15 at 9:48
3

I have forked the pybitcointools library to return a properly formatted OP_RETURN hex string, or insert the OP_RETURN into a raw hex transaction.

My fork can be found here. The code is as follows:

from bitcoin.pyspecials import safe_hexlify, from_string_to_bytes, from_int_to_byte, from_string_to_bytes

def mk_opreturn(msg, rawtx=None, json=0):
    def op_push(data):
        import struct
        if len(data) < 0x4c:
            return from_int_to_byte(len(data)) + from_string_to_bytes(data)
        elif len(data) < 0xff:
            return from_int_to_byte(76) + struct.pack('<B', len(data)) + from_string_to_bytes(data)
        elif len(data) < 0xffff:
            return from_int_to_byte(77) + struct.pack('<H', len(data)) + from_string_to_bytes(data)
        elif len(data) < 0xffffffff:
            return from_int_to_byte(78) + struct.pack('<I', len(data)) + from_string_to_bytes(data)
        else: raise Exception("Input data error. Rawtx must be hex chars" \
                            + "0xffffffff > len(data) > 0")

    orhex = safe_hexlify(b'\x6a' + op_push(msg))
    orjson = {'script' : orhex, 'value' : 0}
    if rawtx is not None:
        try:
            txo = deserialize(rawtx)
            if not 'outs' in txo.keys(): raise Exception("OP_Return cannot be the sole output!")
            txo['outs'].append(orjson)
            newrawtx = serialize(txo)
            return newrawtx
        except:
            raise Exception("Raw Tx Error!")
    return orhex if not json else orjson

Note that the module name has been changed to btc (from bitcoin) in my fork.

To run this, you'll use os.chdir("c:/python/pybitcointools") (or whatever directory it's been downloaded to. Then from bitcoin import *. Now, let's use msg = 'The enemy of my enemy is my friend' and rawtx = "01000000016e3cd2b24fcf49259db29888ec5fe6521070041cb8c7bb2017537046f9e00f2b0000000000ffffffff0168d61100000000001976a91469bbbb16301e40b9fb67130e1aa53a2281d60af088ac00000000".

mk_opreturn(msg, rawtx) returns:

01000000016e3cd2b24fcf49259db29888ec5fe6521070041cb8c7bb2017537046f9e00f2b0000000000ffffffff0268d61100000000001976a91469bbbb16301e40b9fb67130e1aa53a2281d60af088ac0000000000000000246a2254686520656e656d79206f66206d7920656e656d79206973206d7920667269656e6400000000

This is the raw Tx with the OP_RETURN properly inserted. Run the function without the rawtx parameter and it returns the string 6a2254686520656e656d79206f66206d7920656e656d79206973206d7920667269656e64

  • Did you mean to answer this twice? PS. I sent you a PR on github. :) – Nick ODell May 18 '15 at 3:08
  • Yes and no :). I felt it needed a separate answer because I'm trying to answer the "how to easily use OP_RETURN with Python using code" question. This answer is more self-contained I feel. Good catch on the PR BTW – Wizard Of Ozzie May 18 '15 at 3:11
  • Note the updated url – Wizard Of Ozzie May 19 '15 at 5:09
1

The script is simply (so to speak) taking a raw transaction and splicing in a hexadecimal number which represents an additional Tx output. So it's looking for ffffffff (sequence) and appending 6a hex encoding of your msg (up to 20 bytes)

The message needs converting to hex (which the code does).

The code works fine for me, although I've yet to try broadcasting. Based on your quoted error

I get the error (from the Blockchain.info broadcast API): Exception: None Standard Script Output OP_RETURN 594f4c4f53574147

I'd have to say BCI is reacting to theMessageLen part of the code. There should be an OP_PUSHDATA1 (0x4c) between OP_RETURN (0x6a) and the 1 byte msg length, 0x08.

Try: 6a4c08594f4c4f53574147

Alternatively, try another service to push the raw Tx, eg:

  • 1
    Doesn't my line ID = binascii.hexlify(str(message)) already take care of that? – rsmoz Feb 9 '15 at 19:25
  • @rsmoz let me have a look for you. Can you confirm the URL for the script and the Python version you're on? – Wizard Of Ozzie Feb 10 '15 at 6:21
  • 2.7. I'm about to update the code with some changes I've made. – rsmoz Feb 10 '15 at 7:01
  • I added the push code, didn't work. Tried Bitpay's tx send, told me my transaction was dust, which I remembered meant the transaction was too small. Made it larger, then used the Bitpay send and it worked! blockchain.info/tx/… – rsmoz Feb 10 '15 at 19:07
  • @rsmoz good stuff! I'm interested to know if it was the 550 Satoshi dust limit or the push op code that did it. I'll edit my answer to remove the unhelpful binascii comments I made – Wizard Of Ozzie Feb 10 '15 at 23:15
0

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.

  • What does the reference number correspond to? – Nick ODell Jul 1 '15 at 17:08
  • It's in the format: [estimated height]-[partial txid] [estimated height] is the block where the first transaction may appear. When storing we set [estimated height] to 1+(current height). When retrieving we try that block then some later and a few from before in case of reorgs. [partial txid] contains 2 adjacent bytes from the txid: position=2*([partial txid] div 65536) ([partial txid] mod 256) is the byte at position ([partial txid] mod 65536) div 256) is the byte at (position+1) You can't use a txid for the reference since most nodes don't index by txid. – Gideon Greenspan Jul 2 '15 at 6:15
  • @Gideon Greenspan -- I tried to use your code but got this error: 'OP_RETURN.py", line 456, in OP_RETURN_bitcoin_cmd if not len(port): TypeError: object of type 'int' has no len()' – moli Jul 16 '16 at 22:19
  • Set OP_RETURN_BITCOIN_PORT using an integer in a string rather than an integer. – Gideon Greenspan Jul 19 '16 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.