| 1162 | } // anon namespace |
| 1163 | |
| 1164 | uint256 SignatureHash(const CScript& scriptCode, const CTransaction& txTo, unsigned int nIn, uint32_t nHashType, |
| 1165 | const CAmount &amount, unsigned int flags) |
| 1166 | { |
| 1167 | if ((nHashType & SIGHASH_FORKID) && |
| 1168 | (flags & SCRIPT_ENABLE_SIGHASH_FORKID)) { |
| 1169 | uint256 hashPrevouts; |
| 1170 | uint256 hashSequence; |
| 1171 | uint256 hashOutputs; |
| 1172 | |
| 1173 | if (!(nHashType & SIGHASH_ANYONECANPAY)) { |
| 1174 | hashPrevouts = GetPrevoutHash(txTo); |
| 1175 | } |
| 1176 | |
| 1177 | if (!(nHashType & SIGHASH_ANYONECANPAY) && |
| 1178 | (nHashType & 0x1f) != SIGHASH_SINGLE && |
| 1179 | (nHashType & 0x1f) != SIGHASH_NONE) { |
| 1180 | hashSequence = GetSequenceHash(txTo); |
| 1181 | } |
| 1182 | |
| 1183 | if ((nHashType & 0x1f) != SIGHASH_SINGLE && |
| 1184 | (nHashType & 0x1f) != SIGHASH_NONE) { |
| 1185 | hashOutputs = GetOutputsHash(txTo); |
| 1186 | } else if ((nHashType & 0x1f) == SIGHASH_SINGLE && |
| 1187 | nIn < txTo.vout.size()) { |
| 1188 | CHashWriter ss(SER_GETHASH, 0); |
| 1189 | ss << txTo.vout[nIn]; |
| 1190 | hashOutputs = ss.GetHash(); |
| 1191 | } |
| 1192 | |
| 1193 | CHashWriter ss(SER_GETHASH, 0); |
| 1194 | // Version |
| 1195 | ss << txTo.nVersion; |
| 1196 | // Input prevouts/nSequence (none/all, depending on flags) |
| 1197 | ss << hashPrevouts; |
| 1198 | ss << hashSequence; |
| 1199 | // The input being signed (replacing the scriptSig with scriptCode + |
| 1200 | // amount). The prevout may already be contained in hashPrevout, and the |
| 1201 | // nSequence may already be contain in hashSequence. |
| 1202 | ss << txTo.vin[nIn].prevout; |
| 1203 | ss << scriptCode; |
| 1204 | ss << amount; |
| 1205 | ss << txTo.vin[nIn].nSequence; |
| 1206 | // Outputs (none/one/all, depending on flags) |
| 1207 | ss << hashOutputs; |
| 1208 | // Locktime |
| 1209 | ss << txTo.nLockTime; |
| 1210 | // Sighash type |
| 1211 | ss << nHashType; |
| 1212 | |
| 1213 | return ss.GetHash(); |
| 1214 | } |
| 1215 | |
| 1216 | static const uint256 one(uint256S("0000000000000000000000000000000000000000000000000000000000000001")); |
| 1217 | if (nIn >= txTo.vin.size()) { |
| 1218 | // nIn out of range |
| 1219 | return one; |
| 1220 | } |
| 1221 | |