@@ -1089,17 +1089,19 @@ namespace {
1089
1089
* Wrapper that serializes like CTransaction, but with the modifications
1090
1090
* required for the signature hash done in-place
1091
1091
*/
1092
- class CTransactionSignatureSerializer {
1092
+ template <class T >
1093
+ class CTransactionSignatureSerializer
1094
+ {
1093
1095
private:
1094
- const CTransaction & txTo; // !< reference to the spending transaction (the one being serialized)
1096
+ const T & txTo; // !< reference to the spending transaction (the one being serialized)
1095
1097
const CScript& scriptCode; // !< output script being consumed
1096
1098
const unsigned int nIn; // !< input index of txTo being signed
1097
1099
const bool fAnyoneCanPay ; // !< whether the hashtype has the SIGHASH_ANYONECANPAY flag set
1098
1100
const bool fHashSingle ; // !< whether the hashtype is SIGHASH_SINGLE
1099
1101
const bool fHashNone ; // !< whether the hashtype is SIGHASH_NONE
1100
1102
1101
1103
public:
1102
- CTransactionSignatureSerializer (const CTransaction & txToIn, const CScript & scriptCodeIn, unsigned int nInIn, int nHashTypeIn) :
1104
+ CTransactionSignatureSerializer (const T& txToIn, const CScript& scriptCodeIn, unsigned int nInIn, int nHashTypeIn) :
1103
1105
txTo (txToIn), scriptCode(scriptCodeIn), nIn(nInIn),
1104
1106
fAnyoneCanPay (!!(nHashTypeIn & SIGHASH_ANYONECANPAY)),
1105
1107
fHashSingle ((nHashTypeIn & 0x1f ) == SIGHASH_SINGLE),
@@ -1180,23 +1182,29 @@ class CTransactionSignatureSerializer {
1180
1182
}
1181
1183
};
1182
1184
1183
- uint256 GetPrevoutHash (const CTransaction& txTo) {
1185
+ template <class T >
1186
+ uint256 GetPrevoutHash (const T& txTo)
1187
+ {
1184
1188
CHashWriter ss (SER_GETHASH, 0 );
1185
1189
for (const auto & txin : txTo.vin ) {
1186
1190
ss << txin.prevout ;
1187
1191
}
1188
1192
return ss.GetHash ();
1189
1193
}
1190
1194
1191
- uint256 GetSequenceHash (const CTransaction& txTo) {
1195
+ template <class T >
1196
+ uint256 GetSequenceHash (const T& txTo)
1197
+ {
1192
1198
CHashWriter ss (SER_GETHASH, 0 );
1193
1199
for (const auto & txin : txTo.vin ) {
1194
1200
ss << txin.nSequence ;
1195
1201
}
1196
1202
return ss.GetHash ();
1197
1203
}
1198
1204
1199
- uint256 GetOutputsHash (const CTransaction& txTo) {
1205
+ template <class T >
1206
+ uint256 GetOutputsHash (const T& txTo)
1207
+ {
1200
1208
CHashWriter ss (SER_GETHASH, 0 );
1201
1209
for (const auto & txout : txTo.vout ) {
1202
1210
ss << txout;
@@ -1206,7 +1214,8 @@ uint256 GetOutputsHash(const CTransaction& txTo) {
1206
1214
1207
1215
} // namespace
1208
1216
1209
- PrecomputedTransactionData::PrecomputedTransactionData (const CTransaction& txTo)
1217
+ template <class T >
1218
+ PrecomputedTransactionData::PrecomputedTransactionData (const T& txTo)
1210
1219
{
1211
1220
// Cache is calculated only for transactions with witness
1212
1221
if (txTo.HasWitness ()) {
@@ -1217,7 +1226,12 @@ PrecomputedTransactionData::PrecomputedTransactionData(const CTransaction& txTo)
1217
1226
}
1218
1227
}
1219
1228
1220
- uint256 SignatureHash (const CScript& scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType, const CAmount& amount, SigVersion sigversion, const PrecomputedTransactionData* cache)
1229
+ // explicit instantiation
1230
+ template PrecomputedTransactionData::PrecomputedTransactionData(const CTransaction& txTo);
1231
+ template PrecomputedTransactionData::PrecomputedTransactionData(const CMutableTransaction& txTo);
1232
+
1233
+ template <class T >
1234
+ uint256 SignatureHash (const CScript& scriptCode, const T& txTo, unsigned int nIn, int nHashType, const CAmount& amount, SigVersion sigversion, const PrecomputedTransactionData* cache)
1221
1235
{
1222
1236
assert (nIn < txTo.vin .size ());
1223
1237
@@ -1278,20 +1292,22 @@ uint256 SignatureHash(const CScript& scriptCode, const CTransaction& txTo, unsig
1278
1292
}
1279
1293
1280
1294
// Wrapper to serialize only the necessary parts of the transaction being signed
1281
- CTransactionSignatureSerializer txTmp (txTo, scriptCode, nIn, nHashType);
1295
+ CTransactionSignatureSerializer<T> txTmp (txTo, scriptCode, nIn, nHashType);
1282
1296
1283
1297
// Serialize and hash
1284
1298
CHashWriter ss (SER_GETHASH, 0 );
1285
1299
ss << txTmp << nHashType;
1286
1300
return ss.GetHash ();
1287
1301
}
1288
1302
1289
- bool TransactionSignatureChecker::VerifySignature (const std::vector<unsigned char >& vchSig, const CPubKey& pubkey, const uint256& sighash) const
1303
+ template <class T >
1304
+ bool GenericTransactionSignatureChecker<T>::VerifySignature(const std::vector<unsigned char >& vchSig, const CPubKey& pubkey, const uint256& sighash) const
1290
1305
{
1291
1306
return pubkey.Verify (sighash, vchSig);
1292
1307
}
1293
1308
1294
- bool TransactionSignatureChecker::CheckSig (const std::vector<unsigned char >& vchSigIn, const std::vector<unsigned char >& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const
1309
+ template <class T >
1310
+ bool GenericTransactionSignatureChecker<T>::CheckSig(const std::vector<unsigned char >& vchSigIn, const std::vector<unsigned char >& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const
1295
1311
{
1296
1312
CPubKey pubkey (vchPubKey);
1297
1313
if (!pubkey.IsValid ())
@@ -1312,7 +1328,8 @@ bool TransactionSignatureChecker::CheckSig(const std::vector<unsigned char>& vch
1312
1328
return true ;
1313
1329
}
1314
1330
1315
- bool TransactionSignatureChecker::CheckLockTime (const CScriptNum& nLockTime) const
1331
+ template <class T >
1332
+ bool GenericTransactionSignatureChecker<T>::CheckLockTime(const CScriptNum& nLockTime) const
1316
1333
{
1317
1334
// There are two kinds of nLockTime: lock-by-blockheight
1318
1335
// and lock-by-blocktime, distinguished by whether
@@ -1348,7 +1365,8 @@ bool TransactionSignatureChecker::CheckLockTime(const CScriptNum& nLockTime) con
1348
1365
return true ;
1349
1366
}
1350
1367
1351
- bool TransactionSignatureChecker::CheckSequence (const CScriptNum& nSequence) const
1368
+ template <class T >
1369
+ bool GenericTransactionSignatureChecker<T>::CheckSequence(const CScriptNum& nSequence) const
1352
1370
{
1353
1371
// Relative lock times are supported by comparing the passed
1354
1372
// in operand to the sequence number of the input.
@@ -1394,6 +1412,10 @@ bool TransactionSignatureChecker::CheckSequence(const CScriptNum& nSequence) con
1394
1412
return true ;
1395
1413
}
1396
1414
1415
+ // explicit instantiation
1416
+ template class GenericTransactionSignatureChecker <CTransaction>;
1417
+ template class GenericTransactionSignatureChecker <CMutableTransaction>;
1418
+
1397
1419
static bool VerifyWitnessProgram (const CScriptWitness& witness, int witversion, const std::vector<unsigned char >& program, unsigned int flags, const BaseSignatureChecker& checker, ScriptError* serror)
1398
1420
{
1399
1421
std::vector<std::vector<unsigned char > > stack;
0 commit comments