22// Distributed under the MIT software license, see the accompanying
33// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44
5+ #include < script/names.h>
56#include < script/script.h>
67#include < test/test_bitcoin.h>
78
@@ -14,78 +15,95 @@ BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Valid)
1415 uint256 dummy;
1516 CScript p2wsh;
1617 p2wsh << OP_0 << ToByteVector (dummy);
17- BOOST_CHECK (p2wsh.IsPayToWitnessScriptHash ());
18+ BOOST_CHECK (p2wsh.IsPayToWitnessScriptHash (false ));
1819
1920 std::vector<unsigned char > bytes = {OP_0, 32 };
2021 bytes.insert (bytes.end (), 32 , 0 );
21- BOOST_CHECK (CScript (bytes.begin (), bytes.end ()).IsPayToWitnessScriptHash ());
22+ BOOST_CHECK (CScript (bytes.begin (), bytes.end ()).IsPayToWitnessScriptHash (false ));
2223}
2324
2425BOOST_AUTO_TEST_CASE (IsPayToWitnessScriptHash_Invalid_NotOp0)
2526{
2627 uint256 dummy;
2728 CScript notp2wsh;
2829 notp2wsh << OP_1 << ToByteVector (dummy);
29- BOOST_CHECK (!notp2wsh.IsPayToWitnessScriptHash ());
30+ BOOST_CHECK (!notp2wsh.IsPayToWitnessScriptHash (false ));
3031}
3132
3233BOOST_AUTO_TEST_CASE (IsPayToWitnessScriptHash_Invalid_Size)
3334{
3435 uint160 dummy;
3536 CScript notp2wsh;
3637 notp2wsh << OP_0 << ToByteVector (dummy);
37- BOOST_CHECK (!notp2wsh.IsPayToWitnessScriptHash ());
38+ BOOST_CHECK (!notp2wsh.IsPayToWitnessScriptHash (false ));
3839}
3940
4041BOOST_AUTO_TEST_CASE (IsPayToWitnessScriptHash_Invalid_Nop)
4142{
4243 uint256 dummy;
4344 CScript notp2wsh;
4445 notp2wsh << OP_0 << OP_NOP << ToByteVector (dummy);
45- BOOST_CHECK (!notp2wsh.IsPayToWitnessScriptHash ());
46+ BOOST_CHECK (!notp2wsh.IsPayToWitnessScriptHash (false ));
4647}
4748
4849BOOST_AUTO_TEST_CASE (IsPayToWitnessScriptHash_Invalid_EmptyScript)
4950{
5051 CScript notp2wsh;
51- BOOST_CHECK (!notp2wsh.IsPayToWitnessScriptHash ());
52+ BOOST_CHECK (!notp2wsh.IsPayToWitnessScriptHash (false ));
5253}
5354
5455BOOST_AUTO_TEST_CASE (IsPayToWitnessScriptHash_Invalid_Pushdata)
5556{
5657 // A script is not P2WSH if OP_PUSHDATA is used to push the hash.
5758 std::vector<unsigned char > bytes = {OP_0, OP_PUSHDATA1, 32 };
5859 bytes.insert (bytes.end (), 32 , 0 );
59- BOOST_CHECK (!CScript (bytes.begin (), bytes.end ()).IsPayToWitnessScriptHash ());
60+ BOOST_CHECK (!CScript (bytes.begin (), bytes.end ()).IsPayToWitnessScriptHash (false ));
6061
6162 bytes = {OP_0, OP_PUSHDATA2, 32 , 0 };
6263 bytes.insert (bytes.end (), 32 , 0 );
63- BOOST_CHECK (!CScript (bytes.begin (), bytes.end ()).IsPayToWitnessScriptHash ());
64+ BOOST_CHECK (!CScript (bytes.begin (), bytes.end ()).IsPayToWitnessScriptHash (false ));
6465
6566 bytes = {OP_0, OP_PUSHDATA4, 32 , 0 , 0 , 0 };
6667 bytes.insert (bytes.end (), 32 , 0 );
67- BOOST_CHECK (!CScript (bytes.begin (), bytes.end ()).IsPayToWitnessScriptHash ());
68+ BOOST_CHECK (!CScript (bytes.begin (), bytes.end ()).IsPayToWitnessScriptHash (false ));
69+ }
70+
71+ BOOST_AUTO_TEST_CASE (IsPayToWitnessScriptHash_NamePrefix)
72+ {
73+ uint256 dummy;
74+ CScript p2wsh;
75+ p2wsh << OP_0 << ToByteVector (dummy);
76+
77+ BOOST_CHECK (p2wsh.IsPayToWitnessScriptHash (true ));
78+ BOOST_CHECK (p2wsh.IsPayToWitnessScriptHash (false ));
79+
80+ const valtype name (10 , ' a' );
81+ const valtype value (20 , ' b' );
82+ const CScript nameP2WSH = CNameScript::buildNameUpdate (p2wsh, name, value);
83+
84+ BOOST_CHECK (nameP2WSH.IsPayToWitnessScriptHash (true ));
85+ BOOST_CHECK (!nameP2WSH.IsPayToWitnessScriptHash (false ));
6886}
6987
7088namespace {
7189
72- bool IsExpectedWitnessProgram (const CScript& script, const int expectedVersion, const std::vector<unsigned char >& expectedProgram)
90+ bool IsExpectedWitnessProgram (const bool allowNames, const CScript& script, const int expectedVersion, const std::vector<unsigned char >& expectedProgram)
7391{
7492 int actualVersion;
7593 std::vector<unsigned char > actualProgram;
76- if (!script.IsWitnessProgram (actualVersion, actualProgram)) {
94+ if (!script.IsWitnessProgram (allowNames, actualVersion, actualProgram)) {
7795 return false ;
7896 }
7997 BOOST_CHECK_EQUAL (actualVersion, expectedVersion);
8098 BOOST_CHECK (actualProgram == expectedProgram);
8199 return true ;
82100}
83101
84- bool IsNoWitnessProgram (const CScript& script)
102+ bool IsNoWitnessProgram (const bool allowNames, const CScript& script)
85103{
86104 int dummyVersion;
87105 std::vector<unsigned char > dummyProgram;
88- return !script.IsWitnessProgram (dummyVersion, dummyProgram);
106+ return !script.IsWitnessProgram (allowNames, dummyVersion, dummyProgram);
89107}
90108
91109} // anonymous namespace
@@ -95,68 +113,106 @@ BOOST_AUTO_TEST_CASE(IsWitnessProgram_Valid)
95113 std::vector<unsigned char > program = {42 , 18 };
96114 CScript wit;
97115 wit << OP_0 << program;
98- BOOST_CHECK (IsExpectedWitnessProgram (wit, 0 , program));
116+ BOOST_CHECK (IsExpectedWitnessProgram (false , wit, 0 , program));
99117
100118 wit.clear ();
101119 program.resize (40 );
102120 wit << OP_16 << program;
103- BOOST_CHECK (IsExpectedWitnessProgram (wit, 16 , program));
121+ BOOST_CHECK (IsExpectedWitnessProgram (false , wit, 16 , program));
104122
105123 program.resize (32 );
106124 std::vector<unsigned char > bytes = {OP_5, static_cast <unsigned char >(program.size ())};
107125 bytes.insert (bytes.end (), program.begin (), program.end ());
108- BOOST_CHECK (IsExpectedWitnessProgram (CScript (bytes.begin (), bytes.end ()), 5 , program));
126+ BOOST_CHECK (IsExpectedWitnessProgram (false , CScript (bytes.begin (), bytes.end ()), 5 , program));
109127}
110128
111129BOOST_AUTO_TEST_CASE (IsWitnessProgram_Invalid_Version)
112130{
113131 std::vector<unsigned char > program (10 );
114132 CScript nowit;
115133 nowit << OP_1NEGATE << program;
116- BOOST_CHECK (IsNoWitnessProgram (nowit));
134+ BOOST_CHECK (IsNoWitnessProgram (false , nowit));
117135}
118136
119137BOOST_AUTO_TEST_CASE (IsWitnessProgram_Invalid_Size)
120138{
121139 std::vector<unsigned char > program (1 );
122140 CScript nowit;
123141 nowit << OP_0 << program;
124- BOOST_CHECK (IsNoWitnessProgram (nowit));
142+ BOOST_CHECK (IsNoWitnessProgram (false , nowit));
125143
126144 nowit.clear ();
127145 program.resize (41 );
128146 nowit << OP_0 << program;
129- BOOST_CHECK (IsNoWitnessProgram (nowit));
147+ BOOST_CHECK (IsNoWitnessProgram (false , nowit));
130148}
131149
132150BOOST_AUTO_TEST_CASE (IsWitnessProgram_Invalid_Nop)
133151{
134152 std::vector<unsigned char > program (10 );
135153 CScript nowit;
136154 nowit << OP_0 << OP_NOP << program;
137- BOOST_CHECK (IsNoWitnessProgram (nowit));
155+ BOOST_CHECK (IsNoWitnessProgram (false , nowit));
138156}
139157
140158BOOST_AUTO_TEST_CASE (IsWitnessProgram_Invalid_EmptyScript)
141159{
142160 CScript nowit;
143- BOOST_CHECK (IsNoWitnessProgram (nowit));
161+ BOOST_CHECK (IsNoWitnessProgram (false , nowit));
144162}
145163
146164BOOST_AUTO_TEST_CASE (IsWitnessProgram_Invalid_Pushdata)
147165{
148166 // A script is no witness program if OP_PUSHDATA is used to push the hash.
149167 std::vector<unsigned char > bytes = {OP_0, OP_PUSHDATA1, 32 };
150168 bytes.insert (bytes.end (), 32 , 0 );
151- BOOST_CHECK (IsNoWitnessProgram (CScript (bytes.begin (), bytes.end ())));
169+ BOOST_CHECK (IsNoWitnessProgram (false , CScript (bytes.begin (), bytes.end ())));
152170
153171 bytes = {OP_0, OP_PUSHDATA2, 32 , 0 };
154172 bytes.insert (bytes.end (), 32 , 0 );
155- BOOST_CHECK (IsNoWitnessProgram (CScript (bytes.begin (), bytes.end ())));
173+ BOOST_CHECK (IsNoWitnessProgram (false , CScript (bytes.begin (), bytes.end ())));
156174
157175 bytes = {OP_0, OP_PUSHDATA4, 32 , 0 , 0 , 0 };
158176 bytes.insert (bytes.end (), 32 , 0 );
159- BOOST_CHECK (IsNoWitnessProgram (CScript (bytes.begin (), bytes.end ())));
177+ BOOST_CHECK (IsNoWitnessProgram (false , CScript (bytes.begin (), bytes.end ())));
178+ }
179+
180+ BOOST_AUTO_TEST_CASE (IsWitnessProgram_WithNamePrefix)
181+ {
182+ const std::vector<unsigned char > program (20 , 42 );
183+ CScript wit;
184+ wit << OP_0 << program;
185+
186+ BOOST_CHECK (IsExpectedWitnessProgram (true , wit, 0 , program));
187+ BOOST_CHECK (IsExpectedWitnessProgram (false , wit, 0 , program));
188+
189+ const valtype name (10 , ' a' );
190+ const valtype value (20 , ' b' );
191+ const CScript nameWit = CNameScript::buildNameUpdate (wit, name, value);
192+
193+ BOOST_CHECK (IsExpectedWitnessProgram (true , nameWit, 0 , program));
194+ BOOST_CHECK (!IsExpectedWitnessProgram (false , nameWit, 0 , program));
195+ }
196+
197+ BOOST_AUTO_TEST_CASE (IsWitnessProgram_NamePrefixNotMisinterpreted)
198+ {
199+ /* Name prefixes themselves start with OP_1 to OP_3, which is also
200+ a valid start for a witness program. Make sure that they are not
201+ misinterpreted as witness programs. */
202+
203+ const valtype name (10 , ' a' );
204+ const valtype value (20 , ' b' );
205+ const valtype rand (15 , ' x' );
206+
207+ const CScript nameNew = CNameScript::buildNameNew (CScript (), name, rand);
208+ const CScript nameFirst = CNameScript::buildNameFirstupdate (CScript (), name, value, rand);
209+ const CScript nameUpdate = CNameScript::buildNameUpdate (CScript (), name, value);
210+
211+ for (const auto & scr : {nameNew, nameFirst, nameUpdate})
212+ {
213+ BOOST_CHECK (IsNoWitnessProgram (true , scr));
214+ BOOST_CHECK (IsNoWitnessProgram (false , scr));
215+ }
160216}
161217
162218BOOST_AUTO_TEST_SUITE_END ()
0 commit comments