From 3feb851462d32d70b224cf709264a68d87665714 Mon Sep 17 00:00:00 2001 From: wizard of ozzie Date: Mon, 18 May 2015 12:14:24 +1000 Subject: [PATCH] added mk_opreturn mk_opreturn function returns script or inserts script into rawtx --- bitcoin/transaction.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/bitcoin/transaction.py b/bitcoin/transaction.py index e3728b8a..bff46a37 100644 --- a/bitcoin/transaction.py +++ b/bitcoin/transaction.py @@ -1,6 +1,7 @@ #!/usr/bin/python import binascii, re, json, copy, sys from bitcoin.main import * +from btc.pyspecials import safe_hexlify, from_string_to_bytes, from_int_to_byte, from_string_to_bytes from _functools import reduce ### Hex to bin converter and vice versa for objects @@ -480,3 +481,30 @@ def mksend(*args): outputs2 += [{"address": change, "value": isum-osum-fee}] return mktx(ins, outputs2) + +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)) + data + elif len(data) < 0xff: + return from_int_to_byte(76) + struct.pack(' 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