From 6aca74ec1357777ad07c71ae613f739b8d2b88a4 Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Thu, 21 Nov 2024 15:01:43 +0100 Subject: [PATCH 1/2] test: add missing segwitv1 test cases to `script_standard_tests` --- src/test/script_standard_tests.cpp | 38 +++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/src/test/script_standard_tests.cpp b/src/test/script_standard_tests.cpp index e9ce82ca8a6..ff7c98f7fe2 100644 --- a/src/test/script_standard_tests.cpp +++ b/src/test/script_standard_tests.cpp @@ -197,10 +197,14 @@ BOOST_AUTO_TEST_CASE(script_standard_Solver_failure) s << OP_RETURN << std::vector({75}) << OP_ADD; BOOST_CHECK_EQUAL(Solver(s, solutions), TxoutType::NONSTANDARD); - // TxoutType::WITNESS_UNKNOWN with incorrect program size + // TxoutType::WITNESS_V0_{KEY,SCRIPT}HASH with incorrect program size (-> consensus-invalid, i.e. non-standard) s.clear(); s << OP_0 << std::vector(19, 0x01); BOOST_CHECK_EQUAL(Solver(s, solutions), TxoutType::NONSTANDARD); + // TxoutType::WITNESS_V1_TAPROOT with incorrect program size (-> undefined, but still policy-valid) + s.clear(); + s << OP_1 << std::vector(33, 0x01); + BOOST_CHECK_EQUAL(Solver(s, solutions), TxoutType::WITNESS_UNKNOWN); // TxoutType::ANCHOR but wrong witness version s.clear(); @@ -268,11 +272,25 @@ BOOST_AUTO_TEST_CASE(script_standard_ExtractDestination) BOOST_CHECK(ExtractDestination(s, address)); BOOST_CHECK(std::get(address) == scripthash); + // TxoutType::WITNESS_V1_TAPROOT + s.clear(); + auto xpk = XOnlyPubKey(pubkey); + s << OP_1 << ToByteVector(xpk); + BOOST_CHECK(ExtractDestination(s, address)); + BOOST_CHECK(std::get(address) == WitnessV1Taproot(xpk)); + + // TxoutType::ANCHOR + std::vector anchor_bytes{0x4e, 0x73}; + s.clear(); + s << OP_1 << anchor_bytes; + BOOST_CHECK(ExtractDestination(s, address)); + BOOST_CHECK(std::get(address) == PayToAnchor()); + // TxoutType::WITNESS_UNKNOWN with unknown version s.clear(); - s << OP_1 << ToByteVector(pubkey); + s << OP_2 << ToByteVector(xpk); BOOST_CHECK(ExtractDestination(s, address)); - WitnessUnknown unk{1, ToByteVector(pubkey)}; + WitnessUnknown unk{2, ToByteVector(xpk)}; BOOST_CHECK(std::get(address) == unk); } @@ -341,6 +359,20 @@ BOOST_AUTO_TEST_CASE(script_standard_GetScriptFor_) expected << OP_0 << ToByteVector(scriptHash); result = GetScriptForDestination(WitnessV0ScriptHash(witnessScript)); BOOST_CHECK(result == expected); + + // WitnessV1Taproot + auto xpk = XOnlyPubKey(pubkeys[0]); + expected.clear(); + expected << OP_1 << ToByteVector(xpk); + result = GetScriptForDestination(WitnessV1Taproot(xpk)); + BOOST_CHECK(result == expected); + + // PayToAnchor + std::vector anchor_bytes{0x4e, 0x73}; + expected.clear(); + expected << OP_1 << anchor_bytes; + result = GetScriptForDestination(PayToAnchor()); + BOOST_CHECK(result == expected); } BOOST_AUTO_TEST_CASE(script_standard_taproot_builder) From a3a4d199e298a76725d0c0424195ae54c7e95fe0 Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Thu, 21 Nov 2024 16:04:56 +0100 Subject: [PATCH 2/2] refactor: deduplicate anchor witness program bytes (`0x4e,0x73`) Co-authored-by: Gregory Sanders --- src/addresstype.h | 7 +++++-- src/test/script_standard_tests.cpp | 6 ++---- src/test/transaction_tests.cpp | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/addresstype.h b/src/addresstype.h index 93cdf66c5b1..09f3063c610 100644 --- a/src/addresstype.h +++ b/src/addresstype.h @@ -117,10 +117,13 @@ public: } }; +/** Witness program for Pay-to-Anchor output script type */ +static const std::vector anchor_bytes{0x4e, 0x73}; + struct PayToAnchor : public WitnessUnknown { - PayToAnchor() : WitnessUnknown(1, {0x4e, 0x73}) { - Assume(CScript::IsPayToAnchor(1, {0x4e, 0x73})); + PayToAnchor() : WitnessUnknown(1, anchor_bytes) { + Assume(CScript::IsPayToAnchor(1, anchor_bytes)); }; }; diff --git a/src/test/script_standard_tests.cpp b/src/test/script_standard_tests.cpp index ff7c98f7fe2..3d3822542a6 100644 --- a/src/test/script_standard_tests.cpp +++ b/src/test/script_standard_tests.cpp @@ -4,6 +4,7 @@ #include +#include #include #include #include