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