Consensus-correct SignatureHash Returns (hash, err) to precisely match the consensus-critical behavior of the SIGHASH_SINGLE bug. (inIdx is *not* checked for validity)
(script, txTo, inIdx, hashtype)
| 858 | |
| 859 | |
| 860 | def SignatureHash(script, txTo, inIdx, hashtype): |
| 861 | """Consensus-correct SignatureHash |
| 862 | |
| 863 | Returns (hash, err) to precisely match the consensus-critical behavior of |
| 864 | the SIGHASH_SINGLE bug. (inIdx is *not* checked for validity) |
| 865 | """ |
| 866 | 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' |
| 867 | |
| 868 | if inIdx >= len(txTo.vin): |
| 869 | return (HASH_ONE, "inIdx %d out of range (%d)" % (inIdx, len(txTo.vin))) |
| 870 | txtmp = CTransaction(txTo) |
| 871 | |
| 872 | for txin in txtmp.vin: |
| 873 | txin.scriptSig = b'' |
| 874 | txtmp.vin[inIdx].scriptSig = FindAndDelete(script, CScript([OP_CODESEPARATOR])) |
| 875 | |
| 876 | if (hashtype & 0x1f) == SIGHASH_NONE: |
| 877 | txtmp.vout = [] |
| 878 | |
| 879 | for i in range(len(txtmp.vin)): |
| 880 | if i != inIdx: |
| 881 | txtmp.vin[i].nSequence = 0 |
| 882 | |
| 883 | elif (hashtype & 0x1f) == SIGHASH_SINGLE: |
| 884 | outIdx = inIdx |
| 885 | if outIdx >= len(txtmp.vout): |
| 886 | return (HASH_ONE, "outIdx %d out of range (%d)" % (outIdx, len(txtmp.vout))) |
| 887 | |
| 888 | tmp = txtmp.vout[outIdx] |
| 889 | txtmp.vout = [] |
| 890 | for i in range(outIdx): |
| 891 | txtmp.vout.append(CTxOut(-1)) |
| 892 | txtmp.vout.append(tmp) |
| 893 | |
| 894 | for i in range(len(txtmp.vin)): |
| 895 | if i != inIdx: |
| 896 | txtmp.vin[i].nSequence = 0 |
| 897 | |
| 898 | if hashtype & SIGHASH_ANYONECANPAY: |
| 899 | tmp = txtmp.vin[inIdx] |
| 900 | txtmp.vin = [] |
| 901 | txtmp.vin.append(tmp) |
| 902 | |
| 903 | s = txtmp.serialize() |
| 904 | s += struct.pack(b"<I", hashtype) |
| 905 | |
| 906 | hash = hash256(s) |
| 907 | |
| 908 | return (hash, None) |
| 909 | |
| 910 | # TODO: Allow cached hashPrevouts/hashSequence/hashOutputs to be provided. |
| 911 | # Performance optimization probably not necessary for python tests, however. |
nothing calls this directly
no test coverage detected