Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/script.py
Show First 20 Lines • Show All 468 Lines • ▼ Show 20 Lines | def __new__(cls, value=b''): | ||||
if isinstance(value, bytes) or isinstance(value, bytearray): | if isinstance(value, bytes) or isinstance(value, bytearray): | ||||
return super(CScript, cls).__new__(cls, value) | return super(CScript, cls).__new__(cls, value) | ||||
else: | else: | ||||
def coerce_iterable(iterable): | def coerce_iterable(iterable): | ||||
for instance in iterable: | for instance in iterable: | ||||
yield cls.__coerce_instance(instance) | yield cls.__coerce_instance(instance) | ||||
# Annoyingly on both python2 and python3 bytes.join() always | # Annoyingly on both python2 and python3 bytes.join() always | ||||
# returns a bytes instance even when subclassed. | # returns a bytes instance even when subclassed. | ||||
return super(CScript, cls).__new__(cls, b''.join(coerce_iterable(value))) | return super(CScript, cls).__new__( | ||||
cls, b''.join(coerce_iterable(value))) | |||||
def raw_iter(self): | def raw_iter(self): | ||||
"""Raw iteration | """Raw iteration | ||||
Yields tuples of (opcode, data, sop_idx) so that the different possible | Yields tuples of (opcode, data, sop_idx) so that the different possible | ||||
PUSHDATA encodings can be accurately distinguished, as well as | PUSHDATA encodings can be accurately distinguished, as well as | ||||
determining the exact opcode byte indexes. (sop_idx) | determining the exact opcode byte indexes. (sop_idx) | ||||
""" | """ | ||||
▲ Show 20 Lines • Show All 148 Lines • ▼ Show 20 Lines | def SignatureHash(script, txTo, inIdx, hashtype): | ||||
"""Consensus-correct SignatureHash | """Consensus-correct SignatureHash | ||||
Returns (hash, err) to precisely match the consensus-critical behavior of | Returns (hash, err) to precisely match the consensus-critical behavior of | ||||
the SIGHASH_SINGLE bug. (inIdx is *not* checked for validity) | the SIGHASH_SINGLE bug. (inIdx is *not* checked for validity) | ||||
""" | """ | ||||
HASH_ONE = b'\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' | HASH_ONE = b'\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' | ||||
if inIdx >= len(txTo.vin): | if inIdx >= len(txTo.vin): | ||||
return (HASH_ONE, "inIdx {} out of range ({})".format(inIdx, len(txTo.vin))) | return (HASH_ONE, "inIdx {} out of range ({})".format( | ||||
inIdx, len(txTo.vin))) | |||||
txtmp = CTransaction(txTo) | txtmp = CTransaction(txTo) | ||||
for txin in txtmp.vin: | for txin in txtmp.vin: | ||||
txin.scriptSig = b'' | txin.scriptSig = b'' | ||||
txtmp.vin[inIdx].scriptSig = FindAndDelete( | txtmp.vin[inIdx].scriptSig = FindAndDelete( | ||||
script, CScript([OP_CODESEPARATOR])) | script, CScript([OP_CODESEPARATOR])) | ||||
if (hashtype & 0x1f) == SIGHASH_NONE: | if (hashtype & 0x1f) == SIGHASH_NONE: | ||||
txtmp.vout = [] | txtmp.vout = [] | ||||
for i in range(len(txtmp.vin)): | for i in range(len(txtmp.vin)): | ||||
if i != inIdx: | if i != inIdx: | ||||
txtmp.vin[i].nSequence = 0 | txtmp.vin[i].nSequence = 0 | ||||
elif (hashtype & 0x1f) == SIGHASH_SINGLE: | elif (hashtype & 0x1f) == SIGHASH_SINGLE: | ||||
outIdx = inIdx | outIdx = inIdx | ||||
if outIdx >= len(txtmp.vout): | if outIdx >= len(txtmp.vout): | ||||
return (HASH_ONE, "outIdx {} out of range ({})".format(outIdx, len(txtmp.vout))) | return (HASH_ONE, "outIdx {} out of range ({})".format( | ||||
outIdx, len(txtmp.vout))) | |||||
tmp = txtmp.vout[outIdx] | tmp = txtmp.vout[outIdx] | ||||
txtmp.vout = [] | txtmp.vout = [] | ||||
for i in range(outIdx): | for i in range(outIdx): | ||||
txtmp.vout.append(CTxOut(-1)) | txtmp.vout.append(CTxOut(-1)) | ||||
txtmp.vout.append(tmp) | txtmp.vout.append(tmp) | ||||
for i in range(len(txtmp.vin)): | for i in range(len(txtmp.vin)): | ||||
Show All 23 Lines | def SignatureHashForkId(script, txTo, inIdx, hashtype, amount): | ||||
hashOutputs = 0 | hashOutputs = 0 | ||||
if not (hashtype & SIGHASH_ANYONECANPAY): | if not (hashtype & SIGHASH_ANYONECANPAY): | ||||
serialize_prevouts = bytes() | serialize_prevouts = bytes() | ||||
for i in txTo.vin: | for i in txTo.vin: | ||||
serialize_prevouts += i.prevout.serialize() | serialize_prevouts += i.prevout.serialize() | ||||
hashPrevouts = uint256_from_str(hash256(serialize_prevouts)) | hashPrevouts = uint256_from_str(hash256(serialize_prevouts)) | ||||
if (not (hashtype & SIGHASH_ANYONECANPAY) and (hashtype & 0x1f) != SIGHASH_SINGLE and (hashtype & 0x1f) != SIGHASH_NONE): | if (not (hashtype & SIGHASH_ANYONECANPAY) and (hashtype & 0x1f) | ||||
!= SIGHASH_SINGLE and (hashtype & 0x1f) != SIGHASH_NONE): | |||||
serialize_sequence = bytes() | serialize_sequence = bytes() | ||||
for i in txTo.vin: | for i in txTo.vin: | ||||
serialize_sequence += struct.pack("<I", i.nSequence) | serialize_sequence += struct.pack("<I", i.nSequence) | ||||
hashSequence = uint256_from_str(hash256(serialize_sequence)) | hashSequence = uint256_from_str(hash256(serialize_sequence)) | ||||
if ((hashtype & 0x1f) != SIGHASH_SINGLE and (hashtype & 0x1f) != SIGHASH_NONE): | if ((hashtype & 0x1f) != SIGHASH_SINGLE and ( | ||||
hashtype & 0x1f) != SIGHASH_NONE): | |||||
serialize_outputs = bytes() | serialize_outputs = bytes() | ||||
for o in txTo.vout: | for o in txTo.vout: | ||||
serialize_outputs += o.serialize() | serialize_outputs += o.serialize() | ||||
hashOutputs = uint256_from_str(hash256(serialize_outputs)) | hashOutputs = uint256_from_str(hash256(serialize_outputs)) | ||||
elif ((hashtype & 0x1f) == SIGHASH_SINGLE and inIdx < len(txTo.vout)): | elif ((hashtype & 0x1f) == SIGHASH_SINGLE and inIdx < len(txTo.vout)): | ||||
serialize_outputs = txTo.vout[inIdx].serialize() | serialize_outputs = txTo.vout[inIdx].serialize() | ||||
hashOutputs = uint256_from_str(hash256(serialize_outputs)) | hashOutputs = uint256_from_str(hash256(serialize_outputs)) | ||||
Show All 13 Lines |