From 01181d0293dd527f0f30b33eba21e3abf3170b72 Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Tue, 27 Jun 2017 12:01:50 +0200 Subject: [PATCH] Fixes signature_form for SIGHASH_SINGLE and SIGHAS_NONE. --- bitcoin/transaction.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/bitcoin/transaction.py b/bitcoin/transaction.py index 9ca546d6..528ff440 100644 --- a/bitcoin/transaction.py +++ b/bitcoin/transaction.py @@ -137,14 +137,24 @@ def signature_form(tx, i, script, hashcode=SIGHASH_ALL): if hashcode == SIGHASH_NONE: newtx["outs"] = [] elif hashcode == SIGHASH_SINGLE: - newtx["outs"] = newtx["outs"][:len(newtx["ins"])] - for out in newtx["outs"][:len(newtx["ins"]) - 1]: - out['value'] = 2**64 - 1 - out['script'] = "" + if i >= len(newtx["outs"]): + raise Exception("You are trying to use SIGHASH_SINGLE to sign an input that does not have a " + "corresponding output (" + str(i) + "). This could lead to a irreversible lose " + "of funds. Signature process aborted.") + else: + newtx["outs"] = newtx["outs"][:i+1] + for out in newtx["outs"][:i]: + out["value"] = 2**64 - 1 + out["script"] = "" elif hashcode == SIGHASH_ANYONECANPAY: newtx["ins"] = [newtx["ins"][i]] else: pass + + if hashcode in [SIGHASH_NONE, SIGHASH_SINGLE]: + for inp in xrange(len(newtx["ins"])): + if inp is not i: + newtx["ins"][inp]['sequence'] = 0 return newtx # Making the actual signatures