Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2012-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <script/script.h>6#include <test/util/setup_common.h>8#include <boost/test/unit_test.hpp>10BOOST_FIXTURE_TEST_SUITE(script_segwit_tests, BasicTestingSetup)12BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Valid)13{14uint256 dummy;15CScript p2wsh;16p2wsh << OP_0 << ToByteVector(dummy);17BOOST_CHECK(p2wsh.IsPayToWitnessScriptHash());19std::vector<unsigned char> bytes = {OP_0, 32};20bytes.insert(bytes.end(), 32, 0);21BOOST_CHECK(CScript(bytes.begin(), bytes.end()).IsPayToWitnessScriptHash());22}24BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Invalid_NotOp0)25{26uint256 dummy;27CScript notp2wsh;28notp2wsh << OP_1 << ToByteVector(dummy);29BOOST_CHECK(!notp2wsh.IsPayToWitnessScriptHash());30}32BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Invalid_Size)33{34uint160 dummy;35CScript notp2wsh;36notp2wsh << OP_0 << ToByteVector(dummy);37BOOST_CHECK(!notp2wsh.IsPayToWitnessScriptHash());38}40BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Invalid_Nop)41{42uint256 dummy;43CScript notp2wsh;44notp2wsh << OP_0 << OP_NOP << ToByteVector(dummy);45BOOST_CHECK(!notp2wsh.IsPayToWitnessScriptHash());46}48BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Invalid_EmptyScript)49{50CScript notp2wsh;51BOOST_CHECK(!notp2wsh.IsPayToWitnessScriptHash());52}54BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Invalid_Pushdata)55{56// A script is not P2WSH if OP_PUSHDATA is used to push the hash.57std::vector<unsigned char> bytes = {OP_0, OP_PUSHDATA1, 32};58bytes.insert(bytes.end(), 32, 0);59BOOST_CHECK(!CScript(bytes.begin(), bytes.end()).IsPayToWitnessScriptHash());61bytes = {OP_0, OP_PUSHDATA2, 32, 0};62bytes.insert(bytes.end(), 32, 0);63BOOST_CHECK(!CScript(bytes.begin(), bytes.end()).IsPayToWitnessScriptHash());65bytes = {OP_0, OP_PUSHDATA4, 32, 0, 0, 0};66bytes.insert(bytes.end(), 32, 0);67BOOST_CHECK(!CScript(bytes.begin(), bytes.end()).IsPayToWitnessScriptHash());68}70namespace {72bool IsExpectedWitnessProgram(const CScript& script, const int expectedVersion, const std::vector<unsigned char>& expectedProgram)73{74int actualVersion;75std::vector<unsigned char> actualProgram;76if (!script.IsWitnessProgram(actualVersion, actualProgram)) {77return false;78}79BOOST_CHECK_EQUAL(actualVersion, expectedVersion);80BOOST_CHECK(actualProgram == expectedProgram);81return true;82}84bool IsNoWitnessProgram(const CScript& script)85{86int dummyVersion;87std::vector<unsigned char> dummyProgram;88return !script.IsWitnessProgram(dummyVersion, dummyProgram);89}91} // anonymous namespace93BOOST_AUTO_TEST_CASE(IsWitnessProgram_Valid)94{95// Witness programs have a minimum data push of 2 bytes.96std::vector<unsigned char> program = {42, 18};97CScript wit;98wit << OP_0 << program;99BOOST_CHECK(IsExpectedWitnessProgram(wit, 0, program));101wit.clear();102// Witness programs have a maximum data push of 40 bytes.103program.resize(40);104wit << OP_16 << program;105BOOST_CHECK(IsExpectedWitnessProgram(wit, 16, program));107program.resize(32);108std::vector<unsigned char> bytes = {OP_5, static_cast<unsigned char>(program.size())};109bytes.insert(bytes.end(), program.begin(), program.end());110BOOST_CHECK(IsExpectedWitnessProgram(CScript(bytes.begin(), bytes.end()), 5, program));111}113BOOST_AUTO_TEST_CASE(IsWitnessProgram_Invalid_Version)114{115std::vector<unsigned char> program(10);116CScript nowit;117nowit << OP_1NEGATE << program;118BOOST_CHECK(IsNoWitnessProgram(nowit));119}121BOOST_AUTO_TEST_CASE(IsWitnessProgram_Invalid_Size)122{123std::vector<unsigned char> program(1);124CScript nowit;125nowit << OP_0 << program;126BOOST_CHECK(IsNoWitnessProgram(nowit));128nowit.clear();129program.resize(41);130nowit << OP_0 << program;131BOOST_CHECK(IsNoWitnessProgram(nowit));132}134BOOST_AUTO_TEST_CASE(IsWitnessProgram_Invalid_Nop)135{136std::vector<unsigned char> program(10);137CScript nowit;138nowit << OP_0 << OP_NOP << program;139BOOST_CHECK(IsNoWitnessProgram(nowit));140}142BOOST_AUTO_TEST_CASE(IsWitnessProgram_Invalid_EmptyScript)143{144CScript nowit;145BOOST_CHECK(IsNoWitnessProgram(nowit));146}148BOOST_AUTO_TEST_CASE(IsWitnessProgram_Invalid_Pushdata)149{150// A script is no witness program if OP_PUSHDATA is used to push the hash.151std::vector<unsigned char> bytes = {OP_0, OP_PUSHDATA1, 32};152bytes.insert(bytes.end(), 32, 0);153BOOST_CHECK(IsNoWitnessProgram(CScript(bytes.begin(), bytes.end())));155bytes = {OP_0, OP_PUSHDATA2, 32, 0};156bytes.insert(bytes.end(), 32, 0);157BOOST_CHECK(IsNoWitnessProgram(CScript(bytes.begin(), bytes.end())));159bytes = {OP_0, OP_PUSHDATA4, 32, 0, 0, 0};160bytes.insert(bytes.end(), 32, 0);161BOOST_CHECK(IsNoWitnessProgram(CScript(bytes.begin(), bytes.end())));162}164BOOST_AUTO_TEST_SUITE_END()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.