mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 22:50:59 +01:00
tests: implement BIP341 test vectors
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <test/data/bip341_wallet_vectors.json.h>
|
||||
|
||||
#include <key.h>
|
||||
#include <key_io.h>
|
||||
#include <script/script.h>
|
||||
@@ -12,6 +14,8 @@
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include <univalue.h>
|
||||
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(script_standard_tests, BasicTestingSetup)
|
||||
|
||||
@@ -385,4 +389,46 @@ BOOST_AUTO_TEST_CASE(script_standard_taproot_builder)
|
||||
BOOST_CHECK_EQUAL(EncodeDestination(builder.GetOutput()), "bc1pj6gaw944fy0xpmzzu45ugqde4rz7mqj5kj0tg8kmr5f0pjq8vnaqgynnge");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(bip341_spk_test_vectors)
|
||||
{
|
||||
using control_set = decltype(TaprootSpendData::scripts)::mapped_type;
|
||||
|
||||
UniValue tests;
|
||||
tests.read((const char*)json_tests::bip341_wallet_vectors, sizeof(json_tests::bip341_wallet_vectors));
|
||||
|
||||
const auto& vectors = tests["scriptPubKey"];
|
||||
|
||||
for (const auto& vec : vectors.getValues()) {
|
||||
TaprootBuilder spktest;
|
||||
std::map<std::pair<CScript, int>, int> scriptposes;
|
||||
std::function<void (const UniValue&, int)> parse_tree = [&](const UniValue& node, int depth) {
|
||||
if (node.isNull()) return;
|
||||
if (node.isObject()) {
|
||||
auto script_bytes = ParseHex(node["script"].get_str());
|
||||
CScript script(script_bytes.begin(), script_bytes.end());
|
||||
int idx = node["id"].get_int();
|
||||
int leaf_version = node["leafVersion"].get_int();
|
||||
scriptposes[{script, leaf_version}] = idx;
|
||||
spktest.Add(depth, script, leaf_version);
|
||||
} else {
|
||||
parse_tree(node[0], depth + 1);
|
||||
parse_tree(node[1], depth + 1);
|
||||
}
|
||||
};
|
||||
parse_tree(vec["given"]["scriptTree"], 0);
|
||||
spktest.Finalize(XOnlyPubKey(ParseHex(vec["given"]["internalPubkey"].get_str())));
|
||||
BOOST_CHECK_EQUAL(HexStr(GetScriptForDestination(spktest.GetOutput())), vec["expected"]["scriptPubKey"].get_str());
|
||||
BOOST_CHECK_EQUAL(EncodeDestination(spktest.GetOutput()), vec["expected"]["bip350Address"].get_str());
|
||||
auto spend_data = spktest.GetSpendData();
|
||||
BOOST_CHECK_EQUAL(vec["intermediary"]["merkleRoot"].isNull(), spend_data.merkle_root.IsNull());
|
||||
if (!spend_data.merkle_root.IsNull()) {
|
||||
BOOST_CHECK_EQUAL(vec["intermediary"]["merkleRoot"].get_str(), HexStr(spend_data.merkle_root));
|
||||
}
|
||||
BOOST_CHECK_EQUAL(spend_data.scripts.size(), scriptposes.size());
|
||||
for (const auto& scriptpos : scriptposes) {
|
||||
BOOST_CHECK(spend_data.scripts[scriptpos.first] == control_set{ParseHex(vec["expected"]["scriptPathControlBlocks"][scriptpos.second].get_str())});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
Reference in New Issue
Block a user