MCPcopy
hub / github.com/petertodd/python-bitcoinlib / SignatureHash

Function SignatureHash

bitcoin/core/script.py:977–1030  ·  view source on GitHub ↗

Calculate a signature hash 'Cooked' version that checks if inIdx is out of bounds - this is *not* consensus-correct behavior, but is what you probably want for general wallet use.

(script, txTo, inIdx, hashtype, amount=None, sigversion=SIGVERSION_BASE)

Source from the content-addressed store, hash-verified

975SIGVERSION_WITNESS_V0 = 1
976
977def SignatureHash(script, txTo, inIdx, hashtype, amount=None, sigversion=SIGVERSION_BASE):
978 """Calculate a signature hash
979
980 'Cooked' version that checks if inIdx is out of bounds - this is *not*
981 consensus-correct behavior, but is what you probably want for general
982 wallet use.
983 """
984
985 if sigversion == SIGVERSION_WITNESS_V0:
986 hashPrevouts = b'\x00'*32
987 hashSequence = b'\x00'*32
988 hashOutputs = b'\x00'*32
989
990 if not (hashtype & SIGHASH_ANYONECANPAY):
991 serialize_prevouts = bytes()
992 for i in txTo.vin:
993 serialize_prevouts += i.prevout.serialize()
994 hashPrevouts = bitcoin.core.Hash(serialize_prevouts)
995
996 if (not (hashtype & SIGHASH_ANYONECANPAY) and (hashtype & 0x1f) != SIGHASH_SINGLE and (hashtype & 0x1f) != SIGHASH_NONE):
997 serialize_sequence = bytes()
998 for i in txTo.vin:
999 serialize_sequence += struct.pack("<I", i.nSequence)
1000 hashSequence = bitcoin.core.Hash(serialize_sequence)
1001
1002 if ((hashtype & 0x1f) != SIGHASH_SINGLE and (hashtype & 0x1f) != SIGHASH_NONE):
1003 serialize_outputs = bytes()
1004 for o in txTo.vout:
1005 serialize_outputs += o.serialize()
1006 hashOutputs = bitcoin.core.Hash(serialize_outputs)
1007 elif ((hashtype & 0x1f) == SIGHASH_SINGLE and inIdx < len(txTo.vout)):
1008 serialize_outputs = txTo.vout[inIdx].serialize()
1009 hashOutputs = bitcoin.core.Hash(serialize_outputs)
1010
1011 f = BytesIO()
1012 f.write(struct.pack("<i", txTo.nVersion))
1013 f.write(hashPrevouts)
1014 f.write(hashSequence)
1015 txTo.vin[inIdx].prevout.stream_serialize(f)
1016 BytesSerializer.stream_serialize(script, f)
1017 f.write(struct.pack("<q", amount))
1018 f.write(struct.pack("<I", txTo.vin[inIdx].nSequence))
1019 f.write(hashOutputs)
1020 f.write(struct.pack("<i", txTo.nLockTime))
1021 f.write(struct.pack("<i", hashtype))
1022
1023 return bitcoin.core.Hash(f.getvalue())
1024
1025 assert not script.is_witness_scriptpubkey()
1026
1027 (h, err) = RawSignatureHash(script, txTo, inIdx, hashtype)
1028 if err is not None:
1029 raise ValueError(err)
1030 return h
1031
1032
1033__all__ = (

Calls 4

RawSignatureHashFunction · 0.85
serializeMethod · 0.45
stream_serializeMethod · 0.45