Skip to content

Commit b5bfc13

Browse files
committed
Remove hashtype form EvalScript routines
Probably existed in the Satoshi code to check that a given hashtype matched what was expected; removing for now until we have an application that actually needs the functionality so we can figure out the right way to do it, clean-sheet. Also added some more documentation.
1 parent 06587e4 commit b5bfc13

File tree

3 files changed

+39
-24
lines changed

3 files changed

+39
-24
lines changed

bitcoin/core/scripteval.py

+35-20
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,21 @@ def _FindAndDelete(script, sig):
8484
return CScript(script_bytes)
8585

8686

87-
def _CheckSig(sig, pubkey, script, txTo, inIdx, hashtype):
87+
def _CheckSig(sig, pubkey, script, txTo, inIdx):
8888
key = bitcoin.core.key.CKey()
8989
key.set_pubkey(pubkey)
9090

9191
if len(sig) == 0:
9292
return False
93-
if hashtype == 0:
94-
hashtype = bord(sig[-1])
95-
elif hashtype != bord(sig[-1]):
96-
return False
93+
hashtype = bord(sig[-1])
9794
sig = sig[:-1]
9895

9996
# Raw signature hash due to the SIGHASH_SINGLE bug
10097
(h, err) = RawSignatureHash(script, txTo, inIdx, hashtype)
10198
return key.verify(h, sig)
10299

103100

104-
def _CheckMultiSig(opcode, script, stack, txTo, inIdx, hashtype):
101+
def _CheckMultiSig(opcode, script, stack, txTo, inIdx):
105102
i = 1
106103
if len(stack) < i:
107104
raise _MissingOpArgumentsError(opcode, stack, i)
@@ -138,7 +135,7 @@ def _CheckMultiSig(opcode, script, stack, txTo, inIdx, hashtype):
138135
sig = stack[-isig]
139136
pubkey = stack[-ikey]
140137

141-
if _CheckSig(sig, pubkey, script, txTo, inIdx, hashtype):
138+
if _CheckSig(sig, pubkey, script, txTo, inIdx):
142139
isig += 1
143140
sigs_count -= 1
144141

@@ -296,7 +293,15 @@ def _CheckExec(vfExec):
296293
return True
297294

298295

299-
def _EvalScript(stack, scriptIn, txTo, inIdx, hashtype, flags=()):
296+
def _EvalScript(stack, scriptIn, txTo, inIdx, flags=()):
297+
"""Evaluate a script
298+
299+
stack - Initial stack
300+
scriptIn - Script
301+
txTo - Transaction the script is a part of
302+
inIdx - txin index of the scriptSig
303+
flags - SCRIPT_VERIFY_* flags to apply
304+
"""
300305
if len(scriptIn) > MAX_SCRIPT_SIZE:
301306
raise EvalScriptError('script too large; got %d bytes; maximum %d bytes' %
302307
(len(scriptIn), MAX_SCRIPT_SIZE))
@@ -391,8 +396,7 @@ def check_args(n):
391396

392397
elif fExec and sop == OP_CHECKMULTISIG or sop == OP_CHECKMULTISIGVERIFY:
393398
tmpScript = CScript(scriptIn[pbegincodehash:])
394-
ok = _CheckMultiSig(sop, tmpScript, stack, txTo,
395-
inIdx, hashtype)
399+
ok = _CheckMultiSig(sop, tmpScript, stack, txTo, inIdx)
396400
if not ok:
397401
return False
398402

@@ -409,7 +413,7 @@ def check_args(n):
409413
tmpScript = _FindAndDelete(tmpScript, vchSig)
410414

411415
ok = _CheckSig(vchSig, vchPubKey, tmpScript,
412-
txTo, inIdx, hashtype)
416+
txTo, inIdx)
413417
if ok:
414418
if sop != OP_CHECKSIGVERIFY:
415419
stack.append(b"\x01")
@@ -598,22 +602,29 @@ def check_args(n):
598602

599603
return True
600604

601-
def EvalScript(stack, scriptIn, txTo, inIdx, hashtype, flags=()):
605+
def EvalScript(stack, scriptIn, txTo, inIdx, flags=()):
602606
try:
603-
return _EvalScript(stack, scriptIn, txTo, inIdx, hashtype, flags=flags)
607+
return _EvalScript(stack, scriptIn, txTo, inIdx, flags=flags)
604608
except CScriptInvalidError:
605609
return False
606610
except EvalScriptError:
607611
return False
608612

609613

610-
def VerifyScript(scriptSig, scriptPubKey, txTo, inIdx, hashtype, flags=()):
614+
def VerifyScript(scriptSig, scriptPubKey, txTo, inIdx, flags=()):
615+
"""Verify a scriptSig satisfies a scriptPubKey
616+
617+
scriptSig - Signature
618+
scriptPubKey - PubKey
619+
txTo - Spending transaction
620+
inIdx - Index of the transaction input containing scriptSig
621+
"""
611622
stack = []
612-
if not EvalScript(stack, scriptSig, txTo, inIdx, hashtype, flags=flags):
623+
if not EvalScript(stack, scriptSig, txTo, inIdx, flags=flags):
613624
return False
614625
if SCRIPT_VERIFY_P2SH in flags:
615626
stackCopy = list(stack)
616-
if not EvalScript(stack, scriptPubKey, txTo, inIdx, hashtype, flags=flags):
627+
if not EvalScript(stack, scriptPubKey, txTo, inIdx, flags=flags):
617628
return False
618629
if len(stack) == 0:
619630
return False
@@ -632,7 +643,7 @@ def VerifyScript(scriptSig, scriptPubKey, txTo, inIdx, hashtype, flags=()):
632643

633644
pubKey2 = CScript(stackCopy.pop())
634645

635-
if not EvalScript(stackCopy, pubKey2, txTo, inIdx, hashtype, flags=flags):
646+
if not EvalScript(stackCopy, pubKey2, txTo, inIdx, flags=flags):
636647
return False
637648

638649
if not len(stackCopy):
@@ -642,7 +653,12 @@ def VerifyScript(scriptSig, scriptPubKey, txTo, inIdx, hashtype, flags=()):
642653

643654
return True
644655

645-
def VerifySignature(txFrom, txTo, inIdx, hashtype):
656+
def VerifySignature(txFrom, txTo, inIdx):
657+
"""Verify a scriptSig signature
658+
659+
Verifies that the scriptSig in txTo.vin[inIdx] is a valid scriptSig for the
660+
corresponding COutPoint in transaction txFrom.
661+
"""
646662
if inIdx >= len(txTo.vin):
647663
return False
648664
txin = txTo.vin[inIdx]
@@ -656,8 +672,7 @@ def VerifySignature(txFrom, txTo, inIdx, hashtype):
656672
if txin.prevout.hash != txFrom.sha256:
657673
return False
658674

659-
if not VerifyScript(txin.scriptSig, txout.scriptPubKey, txTo, inIdx,
660-
hashtype):
675+
if not VerifyScript(txin.scriptSig, txout.scriptPubKey, txTo, inIdx):
661676
return False
662677

663678
return True

bitcoin/tests/test_scripteval.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ class Test_EvalScript(unittest.TestCase):
5858
flags = (SCRIPT_VERIFY_P2SH, SCRIPT_VERIFY_STRICTENC)
5959
def test_script_valid(self):
6060
for scriptSig, scriptPubKey, comment, test_case in load_test_vectors('script_valid.json'):
61-
if not VerifyScript(scriptSig, scriptPubKey, None, 0, SIGHASH_NONE, flags=self.flags):
61+
if not VerifyScript(scriptSig, scriptPubKey, None, 0, flags=self.flags):
6262
self.fail('Script FAILED: %r %r %r' % (scriptSig, scriptPubKey, comment))
6363

6464
def test_script_invalid(self):
6565
for scriptSig, scriptPubKey, comment, test_case in load_test_vectors('script_invalid.json'):
66-
if VerifyScript(scriptSig, scriptPubKey, None, 0, SIGHASH_NONE, flags=self.flags):
66+
if VerifyScript(scriptSig, scriptPubKey, None, 0, flags=self.flags):
6767
self.fail('Script FAILED: (by succeeding) %r %r %r' % (scriptSig, scriptPubKey, comment))

bitcoin/tests/test_transactions.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def test_tx_valid(self):
9595
if enforceP2SH:
9696
flags.add(SCRIPT_VERIFY_P2SH)
9797

98-
valid = VerifyScript(tx.vin[i].scriptSig, prevouts[tx.vin[i].prevout], tx, i, 0, flags)
98+
valid = VerifyScript(tx.vin[i].scriptSig, prevouts[tx.vin[i].prevout], tx, i, flags=flags)
9999
if not valid:
100100
import pdb; pdb.set_trace()
101101
break
@@ -118,7 +118,7 @@ def test_tx_invalid(self):
118118
if enforceP2SH:
119119
flags.add(SCRIPT_VERIFY_P2SH)
120120

121-
valid = VerifyScript(tx.vin[i].scriptSig, prevouts[tx.vin[i].prevout], tx, i, 0, flags)
121+
valid = VerifyScript(tx.vin[i].scriptSig, prevouts[tx.vin[i].prevout], tx, i, flags=flags)
122122
if not valid:
123123
break
124124

0 commit comments

Comments
 (0)