Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added mk_opreturn #94

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions bitcoin/transaction.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the json parameter actually needs to be renamed jsonfmt, since return orhex if not json else orjson will never return orhex (since json is the name of the module)

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('<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