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

optional locktime for mktx, preparetx, and preparemultitx #99

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion bitcoin/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def preparemultitx(frm, *args, **kwargs):
u = unspent(frm, **kwargs)
u2 = select(u, int(outvalue)+int(fee))
argz = u2 + outs + [frm, fee]
return mksend(*argz)
return mksend(*argz, **kwargs)


# BIP32 hierarchical deterministic multisig script
Expand Down
14 changes: 7 additions & 7 deletions bitcoin/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def json_is_base(obj, base):
if not is_python2 and isinstance(obj, bytes):
return False

alpha = get_code_string(base)
if isinstance(obj, string_types):
for i in range(len(obj)):
Expand Down Expand Up @@ -58,7 +58,7 @@ def read_as_int(bytez):

def read_var_int():
pos[0] += 1

val = from_byte_to_int(tx[pos[0]-1])
if val < 253:
return val
Expand Down Expand Up @@ -300,7 +300,7 @@ def serialize_script(script):
if json_is_base(script, 16):
return safe_hexlify(serialize_script(json_changebase(script,
lambda x: binascii.unhexlify(x))))

result = bytes()
for b in map(serialize_script_unit, script):
result += b if isinstance(b, bytes) else bytes(b, 'utf-8')
Expand Down Expand Up @@ -387,7 +387,7 @@ def is_inp(arg):
return len(arg) > 64 or "output" in arg or "outpoint" in arg


def mktx(*args):
def mktx(*args, **kwargs):
# [in0, in1...],[out0, out1...] or in0, in1 ... out0 out1 ...
ins, outs = [], []
for arg in args:
Expand All @@ -396,7 +396,7 @@ def mktx(*args):
else:
(ins if is_inp(arg) else outs).append(arg)

txobj = {"locktime": 0, "version": 1, "ins": [], "outs": []}
txobj = {"locktime": kwargs.get('locktime', 0), "version": 1, "ins": [], "outs": []}
for i in ins:
if isinstance(i, dict) and "outpoint" in i:
txobj["ins"].append(i)
Expand Down Expand Up @@ -451,7 +451,7 @@ def select(unspent, value):
# Only takes inputs of the form { "output": blah, "value": foo }


def mksend(*args):
def mksend(*args, **kwargs):
argz, change, fee = args[:-2], args[-2], int(args[-1])
ins, outs = [], []
for arg in argz:
Expand Down Expand Up @@ -479,4 +479,4 @@ def mksend(*args):
elif isum > osum+fee+5430:
outputs2 += [{"address": change, "value": isum-osum-fee}]

return mktx(ins, outputs2)
return mktx(ins, outputs2, **kwargs)
14 changes: 13 additions & 1 deletion test.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,11 @@ def test_all(self):
addresses = [pubtoaddr(pub) for pub in pubs]
mscript = mk_multisig_script(pubs[1:], 2, 3)
msigaddr = p2sh_scriptaddr(mscript)
tx = mktx(['01'*32+':1', '23'*32+':2'], [msigaddr+':20202', addresses[0]+':40404'])
tx = mktx(['01'*32+':1', '23'*32+':2'], [msigaddr+':20202', addresses[0]+':40404'], locktime=2222222222)
tx1 = sign(tx, 1, privs[0])

self.assertEqual(deserialize(tx)['locktime'], 2222222222, "Locktime incorrect")

sig1 = multisign(tx, 0, mscript, privs[1])
self.assertTrue(verify_tx_input(tx1, 0, mscript, sig1, pubs[1]), "Verification Error")

Expand All @@ -236,6 +238,16 @@ def test_multisig(self):
self.assertEqual(p2sh_scriptaddr(script, 0xc4), "2MuABMvWTgpZRd4tAG25KW6YzvcoGVZDZYP")
self.assertEqual(p2sh_scriptaddr(script, 196), "2MuABMvWTgpZRd4tAG25KW6YzvcoGVZDZYP")

def test_preparetx(self):
hextx = preparetx('12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX', '1HLoD9E4SDFFPDiYfNYnkBLQ85Y51J3Zb1', 13)
tx = deserialize(hextx)
self.assertEqual(tx['locktime'], 0, "Locktime incorrect")
self.assertEqual(tx['outs'][0]['value'], 13, "Value incorrect")

hextx = preparetx('12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX', '1HLoD9E4SDFFPDiYfNYnkBLQ85Y51J3Zb1', 13, locktime=2222222222)
tx = deserialize(hextx)
self.assertEqual(tx['locktime'], 2222222222, "Locktime incorrect")
self.assertEqual(tx['outs'][0]['value'], 13, "Value incorrect")

class TestDeterministicGenerate(unittest.TestCase):
@classmethod
Expand Down