How do I compose a transaction with OP_RETURN in Python?

5

3

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))

rsmoz

Posted 2015-02-09T06:41:39.100

Reputation: 151

Note that this code will only work on messages of 75 bytes or less.Nick ODell 2015-02-09T07:17:46.440

@NickODell isn't it 40 bytes? (EDIT: you mean 75 bytes total I just realised?)Wizard Of Ozzie 2015-02-09T09:43:13.760

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 2015-02-09T09:48:52.090

Answers

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

Wizard Of Ozzie

Posted 2015-02-09T06:41:39.100

Reputation: 4 535

Did you mean to answer this twice? PS. I sent you a PR on github. :)Nick ODell 2015-05-18T03:08:57.400

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 BTWWizard Of Ozzie 2015-05-18T03:11:03.470

Note the updated urlWizard Of Ozzie 2015-05-19T05:09:16.527

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:

Wizard Of Ozzie

Posted 2015-02-09T06:41:39.100

Reputation: 4 535

1Doesn't my line ID = binascii.hexlify(str(message)) already take care of that?rsmoz 2015-02-09T19:25:16.423

@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 2015-02-10T06:21:46.443

2.7. I'm about to update the code with some changes I've made.rsmoz 2015-02-10T07:01:02.447

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! https://blockchain.info/tx/e44d541fd4cc0c2390a91e7e4d35436524badddd314e905d9113f383ba2a0c62

rsmoz 2015-02-10T19:07:12.307

@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 madeWizard Of Ozzie 2015-02-10T23:15:30.783

Hmm, still having some issues. Now that I've got a lot of inputs to deal with, the transaction isn't being assembled properly.rsmoz 2015-02-12T05:12:05.310

I wanted to add a clarification: when assembling a 40 byte OP_RETURN Txn the code is: 2a6a28 40 byte data, where 2a says push next 42 bytes, 6a is OP_RETURN and 28 says push the 40 bytes. This is the required format for Bitcoincore.Wizard Of Ozzie 2015-02-25T09:25:25.190

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.

Gideon Greenspan

Posted 2015-02-09T06:41:39.100

Reputation: 324

What does the reference number correspond to?Nick ODell 2015-07-01T17:08:47.640

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 2015-07-02T06:15:24.423

@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 2016-07-16T22:19:19.150

Set OP_RETURN_BITCOIN_PORT using an integer in a string rather than an integer.Gideon Greenspan 2016-07-19T07:53:52.077