diff --git a/src/test/data/script_tests.json b/src/test/data/script_tests.json --- a/src/test/data/script_tests.json +++ b/src/test/data/script_tests.json @@ -1879,6 +1879,26 @@ "ILLEGAL_FORKID", "P2PK INVALID FORKID" ], +[ + [ + 123450.00000000 + ], + "0x47 0x3044022037611855758bff48e49f99ad521223506d175364b652a5a4d02282f1e48367cc02200b513f17ee300d64f339872594c3e2ee3411242ef506ad4cc7dbdbd029ca7f2f41", + "0x41 0x0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 CHECKSIG", + "REPLAY_PROTECTION,SIGHASH_FORKID", + "OK", + "P2PK REPLAY PROTECTED" +], +[ + [ + 123450.00000000 + ], + "0x47 0x30440220368d68340dfbebf99d5ec87d77fba899763e466c0a7ab2fa0221fb868ab0f3ef0220266c1a52a8e5b7b597613b80cf53814d3925dfb6715dce712c8e7a25e63a044041", + "0x41 0x0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 CHECKSIG", + "REPLAY_PROTECTION,SIGHASH_FORKID", + "EVAL_FALSE", + "P2PK REPLAY PROTECTED" +], ["CHECKSEQUENCEVERIFY tests"], ["", "CHECKSEQUENCEVERIFY", "CHECKSEQUENCEVERIFY", "INVALID_STACK_OPERATION", "CSV automatically fails on a empty stack"], diff --git a/src/test/script_tests.cpp b/src/test/script_tests.cpp --- a/src/test/script_tests.cpp +++ b/src/test/script_tests.cpp @@ -312,9 +312,10 @@ TestBuilder &PushSig(const CKey &key, SigHashType sigHashType = SigHashType(), unsigned int lenR = 32, unsigned int lenS = 32, - Amount amount = Amount(0)) { + Amount amount = Amount(0), + uint32_t flags = SCRIPT_ENABLE_SIGHASH_FORKID) { uint256 hash = SignatureHash(script, CTransaction(spendTx), 0, - sigHashType, amount); + sigHashType, amount, nullptr, flags); std::vector vchSig, r, s; uint32_t iter = 0; do { @@ -1038,6 +1039,27 @@ .PushSig(keys.key0, SigHashType().withForkId(), 32, 32, TEST_AMOUNT) .ScriptError(SCRIPT_ERR_ILLEGAL_FORKID)); + // Test replay protection + tests.push_back( + TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG, + "P2PK REPLAY PROTECTED", + SCRIPT_ENABLE_SIGHASH_FORKID | + SCRIPT_ENABLE_REPLAY_PROTECTION, + false, TEST_AMOUNT) + .PushSig(keys.key0, SigHashType().withForkId(), 32, 32, TEST_AMOUNT, + SCRIPT_ENABLE_SIGHASH_FORKID | + SCRIPT_ENABLE_REPLAY_PROTECTION)); + + tests.push_back( + TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG, + "P2PK REPLAY PROTECTED", + SCRIPT_ENABLE_SIGHASH_FORKID | + SCRIPT_ENABLE_REPLAY_PROTECTION, + false, TEST_AMOUNT) + .PushSig(keys.key0, SigHashType().withForkId(), 32, 32, TEST_AMOUNT, + SCRIPT_ENABLE_SIGHASH_FORKID) + .ScriptError(SCRIPT_ERR_EVAL_FALSE)); + std::set tests_set; { diff --git a/src/test/scriptflags.cpp b/src/test/scriptflags.cpp --- a/src/test/scriptflags.cpp +++ b/src/test/scriptflags.cpp @@ -32,6 +32,7 @@ SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM}, {"COMPRESSED_PUBKEYTYPE", SCRIPT_VERIFY_COMPRESSED_PUBKEYTYPE}, {"SIGHASH_FORKID", SCRIPT_ENABLE_SIGHASH_FORKID}, + {"REPLAY_PROTECTION", SCRIPT_ENABLE_REPLAY_PROTECTION}, }; uint32_t ParseScriptFlags(std::string strFlags) {