mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-24 18:23:26 -03:00
Merge bitcoin/bitcoin#30377: refactor: Replace ParseHex with consteval ""_hex literals
Some checks are pending
Some checks are pending
8756ccd712
scripted-diff: Replace ParseHex[<std::byte>]("str") -> "str"_hex[_u8] (Hodlinator)9cb687351f
refactor: Prepare for ParseHex -> ""_hex scripted-diff (Hodlinator)50bc017040
refactor: Hand-replace some ParseHex -> ""_hex (Hodlinator)5b74a849cf
util: Add consteval ""_hex[_v][_u8] literals (l0rinc)dc5f6f6812
test refactor: util_tests - parse_hex clean up (Hodlinator)2b5e6eff36
refactor: Make XOnlyPubKey tolerate constexpr std::arrays (Hodlinator)403d86f1cc
refactor: vector -> span in CCrypter (Hodlinator)bd0830bbd4
refactor: de-Hungarianize CCrypter (Hodlinator)d99c816971
refactor: Improve CCrypter related lines (Hodlinator)7e1d9a8468
refactor: Enforce lowercase hex digits for consteval uint256 (Hodlinator) Pull request description: Motivation: * Validates and converts the hex string into bytes at compile time instead of at runtime like `ParseHex()`. * Eliminates runtime dependencies: https://github.com/bitcoin/bitcoin/pull/30377#issuecomment-2214432177, https://github.com/bitcoin/bitcoin/pull/30048#discussion_r1592108480 * Has stricter requirements than `ParseHex()` (disallows whitespace and uppercase hex digits) and replaces it in a bunch of places. * Makes it possible to derive other compile time constants. * Minor: should shave off a few runtime CPU cycles. `""_hex` produces `std::array<std::byte>` as the momentum in the codebase is to use `std::byte` over `uint8_t`. Also makes `uint256` hex string constructor disallow uppercase hex digits. Discussed: https://github.com/bitcoin/bitcoin/pull/30560#discussion_r1701323070 Surprisingly does not change the size of the Guix **bitcoind** binary (on x86_64-linux-gnu) by 1 single byte. Spawned already merged PRs: #30436, #30482, #30532, #30560. ACKs for top commit: l0rinc: ACK8756ccd712
stickies-v: Rebase re-ACK8756ccd712
, no changes since a096215c9c71a2ec03e76f1fd0bcdda0727996e0 ryanofsky: Code review ACK8756ccd712
, just rebasing since last review and taking advantage of CScript constructors in #29369, also tweaking a code comment Tree-SHA512: 9b2011b7c37e0ef004c669f8601270a214b388916316458370f5902c79c2856790b1b2c7c123efa65decad04886ab5eff95644301e0d84358bb265cf1f8ec195
This commit is contained in:
commit
b52d547361
31 changed files with 481 additions and 360 deletions
|
@ -8,12 +8,13 @@
|
|||
|
||||
#include <vector>
|
||||
|
||||
using namespace util::hex_literals;
|
||||
|
||||
static void Bech32Encode(benchmark::Bench& bench)
|
||||
{
|
||||
std::vector<uint8_t> v = ParseHex("c97f5a67ec381b760aeaf67573bc164845ff39a3bb26a1cee401ac67243b48db");
|
||||
constexpr std::array<uint8_t, 32> v{"c97f5a67ec381b760aeaf67573bc164845ff39a3bb26a1cee401ac67243b48db"_hex_u8};
|
||||
std::vector<unsigned char> tmp = {0};
|
||||
tmp.reserve(1 + 32 * 8 / 5);
|
||||
tmp.reserve(1 + v.size() * 8 / 5);
|
||||
ConvertBits<8, 5, true>([&](unsigned char c) { tmp.push_back(c); }, v.begin(), v.end());
|
||||
bench.batch(v.size()).unit("byte").run([&] {
|
||||
bech32::Encode(bech32::Encoding::BECH32, "bc", tmp);
|
||||
|
|
|
@ -25,6 +25,8 @@
|
|||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
using namespace util::hex_literals;
|
||||
|
||||
// Very simple block filter index sync benchmark, only using coinbase outputs.
|
||||
static void BlockFilterIndexSync(benchmark::Bench& bench)
|
||||
{
|
||||
|
@ -32,7 +34,7 @@ static void BlockFilterIndexSync(benchmark::Bench& bench)
|
|||
|
||||
// Create more blocks
|
||||
int CHAIN_SIZE = 600;
|
||||
CPubKey pubkey{ParseHex("02ed26169896db86ced4cbb7b3ecef9859b5952825adbeab998fb5b307e54949c9")};
|
||||
CPubKey pubkey{"02ed26169896db86ced4cbb7b3ecef9859b5952825adbeab998fb5b307e54949c9"_hex_u8};
|
||||
CScript script = GetScriptForDestination(WitnessV0KeyHash(pubkey));
|
||||
std::vector<CMutableTransaction> noTxns;
|
||||
for (int i = 0; i < CHAIN_SIZE - 100; i++) {
|
||||
|
|
|
@ -26,6 +26,8 @@
|
|||
#include <cstring>
|
||||
#include <type_traits>
|
||||
|
||||
using namespace util::hex_literals;
|
||||
|
||||
// Workaround MSVC bug triggering C7595 when calling consteval constructors in
|
||||
// initializer lists.
|
||||
// A fix may be on the way:
|
||||
|
@ -71,7 +73,7 @@ static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesi
|
|||
static CBlock CreateGenesisBlock(uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward)
|
||||
{
|
||||
const char* pszTimestamp = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks";
|
||||
const CScript genesisOutputScript = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
|
||||
const CScript genesisOutputScript = CScript() << "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"_hex_v_u8 << OP_CHECKSIG;
|
||||
return CreateGenesisBlock(pszTimestamp, genesisOutputScript, nTime, nNonce, nBits, nVersion, genesisReward);
|
||||
}
|
||||
|
||||
|
@ -351,7 +353,7 @@ public:
|
|||
m_assumed_chain_state_size = 0;
|
||||
|
||||
const char* testnet4_genesis_msg = "03/May/2024 000000000000000000001ebd58c244970b3aa9d783bb001011fbe8ea8e98e00e";
|
||||
const CScript testnet4_genesis_script = CScript() << ParseHex("000000000000000000000000000000000000000000000000000000000000000000") << OP_CHECKSIG;
|
||||
const CScript testnet4_genesis_script = CScript() << "000000000000000000000000000000000000000000000000000000000000000000"_hex_v_u8 << OP_CHECKSIG;
|
||||
genesis = CreateGenesisBlock(testnet4_genesis_msg,
|
||||
testnet4_genesis_script,
|
||||
1714777860,
|
||||
|
@ -412,7 +414,7 @@ public:
|
|||
vSeeds.clear();
|
||||
|
||||
if (!options.challenge) {
|
||||
bin = ParseHex("512103ad5e0edad18cb1f0fc0d28a3d4f1f3e445640337489abb10404f2d1e086be430210359ef5021964fe22d6f8e05b2463c9540ce96883fe3b278760f048f5189f2e6c452ae");
|
||||
bin = "512103ad5e0edad18cb1f0fc0d28a3d4f1f3e445640337489abb10404f2d1e086be430210359ef5021964fe22d6f8e05b2463c9540ce96883fe3b278760f048f5189f2e6c452ae"_hex_v_u8;
|
||||
vSeeds.emplace_back("seed.signet.bitcoin.sprovoost.nl.");
|
||||
vSeeds.emplace_back("seed.signet.achownodes.xyz."); // Ava Chow, only supports x1, x5, x9, x49, x809, x849, xd, x400, x404, x408, x448, xc08, xc48, x40c
|
||||
|
||||
|
|
|
@ -54,6 +54,8 @@
|
|||
#include <typeinfo>
|
||||
#include <utility>
|
||||
|
||||
using namespace util::hex_literals;
|
||||
|
||||
/** Headers download timeout.
|
||||
* Timeout = base + per_header * (expected number of headers) */
|
||||
static constexpr auto HEADERS_DOWNLOAD_TIMEOUT_BASE = 15min;
|
||||
|
@ -3931,8 +3933,8 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
|||
|
||||
// If the peer is old enough to have the old alert system, send it the final alert.
|
||||
if (greatest_common_version <= 70012) {
|
||||
const auto finalAlert{ParseHex("60010000000000000000000000ffffff7f00000000ffffff7ffeffff7f01ffffff7f00000000ffffff7f00ffffff7f002f555247454e543a20416c657274206b657920636f6d70726f6d697365642c2075706772616465207265717569726564004630440220653febd6410f470f6bae11cad19c48413becb1ac2c17f908fd0fd53bdc3abd5202206d0e9c96fe88d4a0f01ed9dedae2b6f9e00da94cad0fecaae66ecf689bf71b50")};
|
||||
MakeAndPushMessage(pfrom, "alert", Span{finalAlert});
|
||||
constexpr auto finalAlert{"60010000000000000000000000ffffff7f00000000ffffff7ffeffff7f01ffffff7f00000000ffffff7f00ffffff7f002f555247454e543a20416c657274206b657920636f6d70726f6d697365642c2075706772616465207265717569726564004630440220653febd6410f470f6bae11cad19c48413becb1ac2c17f908fd0fd53bdc3abd5202206d0e9c96fe88d4a0f01ed9dedae2b6f9e00da94cad0fecaae66ecf689bf71b50"_hex};
|
||||
MakeAndPushMessage(pfrom, "alert", finalAlert);
|
||||
}
|
||||
|
||||
// Feeler connections exist only to verify if address is online.
|
||||
|
|
|
@ -18,6 +18,8 @@
|
|||
#include <algorithm>
|
||||
#include <cassert>
|
||||
|
||||
using namespace util::hex_literals;
|
||||
|
||||
namespace {
|
||||
|
||||
struct Secp256k1SelfTester
|
||||
|
@ -190,14 +192,7 @@ int ecdsa_signature_parse_der_lax(secp256k1_ecdsa_signature* sig, const unsigned
|
|||
* For an example script for calculating H, refer to the unit tests in
|
||||
* ./test/functional/test_framework/crypto/secp256k1.py
|
||||
*/
|
||||
static const std::vector<unsigned char> NUMS_H_DATA{ParseHex("50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0")};
|
||||
const XOnlyPubKey XOnlyPubKey::NUMS_H{NUMS_H_DATA};
|
||||
|
||||
XOnlyPubKey::XOnlyPubKey(Span<const unsigned char> bytes)
|
||||
{
|
||||
assert(bytes.size() == 32);
|
||||
std::copy(bytes.begin(), bytes.end(), m_keydata.begin());
|
||||
}
|
||||
constexpr XOnlyPubKey XOnlyPubKey::NUMS_H{"50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0"_hex_u8};
|
||||
|
||||
std::vector<CKeyID> XOnlyPubKey::GetKeyIDs() const
|
||||
{
|
||||
|
|
|
@ -254,7 +254,7 @@ public:
|
|||
bool IsNull() const { return m_keydata.IsNull(); }
|
||||
|
||||
/** Construct an x-only pubkey from exactly 32 bytes. */
|
||||
explicit XOnlyPubKey(Span<const unsigned char> bytes);
|
||||
constexpr explicit XOnlyPubKey(std::span<const unsigned char> bytes) : m_keydata{bytes} {}
|
||||
|
||||
/** Construct an x-only pubkey from a normal pubkey. */
|
||||
explicit XOnlyPubKey(const CPubKey& pubkey) : XOnlyPubKey(Span{pubkey}.subspan(1, 32)) {}
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
#include <string>
|
||||
|
||||
using namespace std::literals;
|
||||
using namespace util::hex_literals;
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(base58_tests, BasicTestingSetup)
|
||||
|
||||
|
@ -72,7 +73,7 @@ BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
|
|||
// check that DecodeBase58 skips whitespace, but still fails with unexpected non-whitespace at the end.
|
||||
BOOST_CHECK(!DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t a", result, 3));
|
||||
BOOST_CHECK( DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t ", result, 3));
|
||||
std::vector<unsigned char> expected = ParseHex("971a55");
|
||||
constexpr auto expected{"971a55"_hex_u8};
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
|
||||
|
||||
BOOST_CHECK(DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oh"s, result, 100));
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -19,6 +19,8 @@
|
|||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
using namespace util::hex_literals;
|
||||
|
||||
int ApplyTxInUndo(Coin&& undo, CCoinsViewCache& view, const COutPoint& out);
|
||||
void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo &txundo, int nHeight);
|
||||
|
||||
|
@ -511,25 +513,25 @@ BOOST_FIXTURE_TEST_CASE(updatecoins_simulation_test, UpdateTest)
|
|||
BOOST_AUTO_TEST_CASE(ccoins_serialization)
|
||||
{
|
||||
// Good example
|
||||
DataStream ss1{ParseHex("97f23c835800816115944e077fe7c803cfa57f29b36bf87c1d35")};
|
||||
DataStream ss1{"97f23c835800816115944e077fe7c803cfa57f29b36bf87c1d35"_hex};
|
||||
Coin cc1;
|
||||
ss1 >> cc1;
|
||||
BOOST_CHECK_EQUAL(cc1.fCoinBase, false);
|
||||
BOOST_CHECK_EQUAL(cc1.nHeight, 203998U);
|
||||
BOOST_CHECK_EQUAL(cc1.out.nValue, CAmount{60000000000});
|
||||
BOOST_CHECK_EQUAL(HexStr(cc1.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160(ParseHex("816115944e077fe7c803cfa57f29b36bf87c1d35"))))));
|
||||
BOOST_CHECK_EQUAL(HexStr(cc1.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160("816115944e077fe7c803cfa57f29b36bf87c1d35"_hex_u8)))));
|
||||
|
||||
// Good example
|
||||
DataStream ss2{ParseHex("8ddf77bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4")};
|
||||
DataStream ss2{"8ddf77bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4"_hex};
|
||||
Coin cc2;
|
||||
ss2 >> cc2;
|
||||
BOOST_CHECK_EQUAL(cc2.fCoinBase, true);
|
||||
BOOST_CHECK_EQUAL(cc2.nHeight, 120891U);
|
||||
BOOST_CHECK_EQUAL(cc2.out.nValue, 110397);
|
||||
BOOST_CHECK_EQUAL(HexStr(cc2.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160(ParseHex("8c988f1a4a4de2161e0f50aac7f17e7f9555caa4"))))));
|
||||
BOOST_CHECK_EQUAL(HexStr(cc2.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160("8c988f1a4a4de2161e0f50aac7f17e7f9555caa4"_hex_u8)))));
|
||||
|
||||
// Smallest possible example
|
||||
DataStream ss3{ParseHex("000006")};
|
||||
DataStream ss3{"000006"_hex};
|
||||
Coin cc3;
|
||||
ss3 >> cc3;
|
||||
BOOST_CHECK_EQUAL(cc3.fCoinBase, false);
|
||||
|
@ -538,7 +540,7 @@ BOOST_AUTO_TEST_CASE(ccoins_serialization)
|
|||
BOOST_CHECK_EQUAL(cc3.out.scriptPubKey.size(), 0U);
|
||||
|
||||
// scriptPubKey that ends beyond the end of the stream
|
||||
DataStream ss4{ParseHex("000007")};
|
||||
DataStream ss4{"000007"_hex};
|
||||
try {
|
||||
Coin cc4;
|
||||
ss4 >> cc4;
|
||||
|
@ -551,7 +553,7 @@ BOOST_AUTO_TEST_CASE(ccoins_serialization)
|
|||
uint64_t x = 3000000000ULL;
|
||||
tmp << VARINT(x);
|
||||
BOOST_CHECK_EQUAL(HexStr(tmp), "8a95c0bb00");
|
||||
DataStream ss5{ParseHex("00008a95c0bb00")};
|
||||
DataStream ss5{"00008a95c0bb00"_hex};
|
||||
try {
|
||||
Coin cc5;
|
||||
ss5 >> cc5;
|
||||
|
|
|
@ -26,6 +26,8 @@
|
|||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
using namespace util::hex_literals;
|
||||
|
||||
namespace crypto_tests {
|
||||
struct CryptoTest : BasicTestingSetup {
|
||||
|
||||
|
@ -830,7 +832,7 @@ BOOST_AUTO_TEST_CASE(chacha20_testvector)
|
|||
|
||||
BOOST_AUTO_TEST_CASE(chacha20_midblock)
|
||||
{
|
||||
auto key = ParseHex<std::byte>("0000000000000000000000000000000000000000000000000000000000000000");
|
||||
auto key = "0000000000000000000000000000000000000000000000000000000000000000"_hex;
|
||||
ChaCha20 c20{key};
|
||||
// get one block of keystream
|
||||
std::byte block[64];
|
||||
|
@ -926,7 +928,7 @@ BOOST_AUTO_TEST_CASE(poly1305_testvector)
|
|||
{
|
||||
// mac of the macs of messages of length 0 to 256, where the key and messages have all
|
||||
// their values set to the length.
|
||||
auto total_key = ParseHex<std::byte>("01020304050607fffefdfcfbfaf9ffffffffffffffffffffffffffff00000000");
|
||||
auto total_key = "01020304050607fffefdfcfbfaf9ffffffffffffffffffffffffffff00000000"_hex;
|
||||
Poly1305 total_ctx(total_key);
|
||||
for (unsigned i = 0; i < 256; ++i) {
|
||||
std::vector<std::byte> key(32, std::byte{uint8_t(i)});
|
||||
|
@ -1269,7 +1271,7 @@ BOOST_AUTO_TEST_CASE(muhash_tests)
|
|||
BOOST_CHECK_EQUAL(HexStr(out), HexStr(out3));
|
||||
|
||||
// Test MuHash3072 overflow, meaning the internal data is larger than the modulus.
|
||||
DataStream ss_max{ParseHex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")};
|
||||
DataStream ss_max{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"_hex};
|
||||
MuHash3072 overflowchk;
|
||||
ss_max >> overflowchk;
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using namespace util::hex_literals;
|
||||
using util::Split;
|
||||
|
||||
namespace {
|
||||
|
@ -952,16 +953,16 @@ BOOST_AUTO_TEST_CASE(descriptor_test)
|
|||
// We can't sign for a script requiring a ripemd160 preimage without providing it.
|
||||
Check("wsh(and_v(v:ripemd160(ff9aa1829c90d26e73301383f549e1497b7d6325),pk(xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc)))", "wsh(and_v(v:ripemd160(ff9aa1829c90d26e73301383f549e1497b7d6325),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", "wsh(and_v(v:ripemd160(ff9aa1829c90d26e73301383f549e1497b7d6325),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", SIGNABLE_FAILS, {{"002001549deda34cbc4a5982263191380f522695a2ddc2f99fc3a65c736264bd6cab"}}, OutputType::BECH32, /*op_desc_id=*/uint256{"1fed6fbd0e408eb4bddfefa075289dc7061e7a3240c84f6ba5b9f294d96a21f4"}, {{}}, /*spender_nlocktime=*/0, /*spender_nsequence=*/CTxIn::SEQUENCE_FINAL, {});
|
||||
// But if we provide it, we can.
|
||||
Check("wsh(and_v(v:ripemd160(ff9aa1829c90d26e73301383f549e1497b7d6325),pk(xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc)))", "wsh(and_v(v:ripemd160(ff9aa1829c90d26e73301383f549e1497b7d6325),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", "wsh(and_v(v:ripemd160(ff9aa1829c90d26e73301383f549e1497b7d6325),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", SIGNABLE, {{"002001549deda34cbc4a5982263191380f522695a2ddc2f99fc3a65c736264bd6cab"}}, OutputType::BECH32, /*op_desc_id=*/uint256{"1fed6fbd0e408eb4bddfefa075289dc7061e7a3240c84f6ba5b9f294d96a21f4"}, {{}}, /*spender_nlocktime=*/0, /*spender_nsequence=*/CTxIn::SEQUENCE_FINAL, {{ParseHex("ff9aa1829c90d26e73301383f549e1497b7d6325"), ParseHex("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f")}});
|
||||
Check("wsh(and_v(v:ripemd160(ff9aa1829c90d26e73301383f549e1497b7d6325),pk(xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc)))", "wsh(and_v(v:ripemd160(ff9aa1829c90d26e73301383f549e1497b7d6325),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", "wsh(and_v(v:ripemd160(ff9aa1829c90d26e73301383f549e1497b7d6325),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", SIGNABLE, {{"002001549deda34cbc4a5982263191380f522695a2ddc2f99fc3a65c736264bd6cab"}}, OutputType::BECH32, /*op_desc_id=*/uint256{"1fed6fbd0e408eb4bddfefa075289dc7061e7a3240c84f6ba5b9f294d96a21f4"}, {{}}, /*spender_nlocktime=*/0, /*spender_nsequence=*/CTxIn::SEQUENCE_FINAL, {{"ff9aa1829c90d26e73301383f549e1497b7d6325"_hex_v_u8, "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"_hex_v_u8}});
|
||||
// Same for sha256
|
||||
Check("wsh(and_v(v:sha256(7426ba0604c3f8682c7016b44673f85c5bd9da2fa6c1080810cf53ae320c9863),pk(xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc)))", "wsh(and_v(v:sha256(7426ba0604c3f8682c7016b44673f85c5bd9da2fa6c1080810cf53ae320c9863),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", "wsh(and_v(v:sha256(7426ba0604c3f8682c7016b44673f85c5bd9da2fa6c1080810cf53ae320c9863),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", SIGNABLE_FAILS, {{"002071f7283dbbb9a55ed43a54cda16ba0efd0f16dc48fe200f299e57bb5d7be8dd4"}}, OutputType::BECH32, /*op_desc_id=*/uint256{"a1809a65ba5ca2f09a06c114d4881eed95d1b62f38743cf126cf71b2dd411374"}, {{}}, /*spender_nlocktime=*/0, /*spender_nsequence=*/CTxIn::SEQUENCE_FINAL, {});
|
||||
Check("wsh(and_v(v:sha256(7426ba0604c3f8682c7016b44673f85c5bd9da2fa6c1080810cf53ae320c9863),pk(xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc)))", "wsh(and_v(v:sha256(7426ba0604c3f8682c7016b44673f85c5bd9da2fa6c1080810cf53ae320c9863),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", "wsh(and_v(v:sha256(7426ba0604c3f8682c7016b44673f85c5bd9da2fa6c1080810cf53ae320c9863),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", SIGNABLE, {{"002071f7283dbbb9a55ed43a54cda16ba0efd0f16dc48fe200f299e57bb5d7be8dd4"}}, OutputType::BECH32, /*op_desc_id=*/uint256{"a1809a65ba5ca2f09a06c114d4881eed95d1b62f38743cf126cf71b2dd411374"}, {{}}, /*spender_nlocktime=*/0, /*spender_nsequence=*/CTxIn::SEQUENCE_FINAL, {{ParseHex("7426ba0604c3f8682c7016b44673f85c5bd9da2fa6c1080810cf53ae320c9863"), ParseHex("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f")}});
|
||||
Check("wsh(and_v(v:sha256(7426ba0604c3f8682c7016b44673f85c5bd9da2fa6c1080810cf53ae320c9863),pk(xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc)))", "wsh(and_v(v:sha256(7426ba0604c3f8682c7016b44673f85c5bd9da2fa6c1080810cf53ae320c9863),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", "wsh(and_v(v:sha256(7426ba0604c3f8682c7016b44673f85c5bd9da2fa6c1080810cf53ae320c9863),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", SIGNABLE, {{"002071f7283dbbb9a55ed43a54cda16ba0efd0f16dc48fe200f299e57bb5d7be8dd4"}}, OutputType::BECH32, /*op_desc_id=*/uint256{"a1809a65ba5ca2f09a06c114d4881eed95d1b62f38743cf126cf71b2dd411374"}, {{}}, /*spender_nlocktime=*/0, /*spender_nsequence=*/CTxIn::SEQUENCE_FINAL, {{"7426ba0604c3f8682c7016b44673f85c5bd9da2fa6c1080810cf53ae320c9863"_hex_v_u8, "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"_hex_v_u8}});
|
||||
// Same for hash160
|
||||
Check("wsh(and_v(v:hash160(292e2df59e3a22109200beed0cdc84b12e66793e),pk(xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc)))", "wsh(and_v(v:hash160(292e2df59e3a22109200beed0cdc84b12e66793e),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", "wsh(and_v(v:hash160(292e2df59e3a22109200beed0cdc84b12e66793e),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", SIGNABLE_FAILS, {{"00209b9d5b45735d0e15df5b41d6594602d3de472262f7b75edc6cf5f3e3fa4e3ae4"}}, OutputType::BECH32, /*op_desc_id=*/uint256{"d7bdbc680503a585925eec72d11fc99396f51855d0a03fce53c90bed4c2e319f"}, {{}}, /*spender_nlocktime=*/0, /*spender_nsequence=*/CTxIn::SEQUENCE_FINAL, {});
|
||||
Check("wsh(and_v(v:hash160(292e2df59e3a22109200beed0cdc84b12e66793e),pk(xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc)))", "wsh(and_v(v:hash160(292e2df59e3a22109200beed0cdc84b12e66793e),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", "wsh(and_v(v:hash160(292e2df59e3a22109200beed0cdc84b12e66793e),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", SIGNABLE, {{"00209b9d5b45735d0e15df5b41d6594602d3de472262f7b75edc6cf5f3e3fa4e3ae4"}}, OutputType::BECH32, /*op_desc_id=*/uint256{"d7bdbc680503a585925eec72d11fc99396f51855d0a03fce53c90bed4c2e319f"}, {{}}, /*spender_nlocktime=*/0, /*spender_nsequence=*/CTxIn::SEQUENCE_FINAL, {{ParseHex("292e2df59e3a22109200beed0cdc84b12e66793e"), ParseHex("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f")}});
|
||||
Check("wsh(and_v(v:hash160(292e2df59e3a22109200beed0cdc84b12e66793e),pk(xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc)))", "wsh(and_v(v:hash160(292e2df59e3a22109200beed0cdc84b12e66793e),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", "wsh(and_v(v:hash160(292e2df59e3a22109200beed0cdc84b12e66793e),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", SIGNABLE, {{"00209b9d5b45735d0e15df5b41d6594602d3de472262f7b75edc6cf5f3e3fa4e3ae4"}}, OutputType::BECH32, /*op_desc_id=*/uint256{"d7bdbc680503a585925eec72d11fc99396f51855d0a03fce53c90bed4c2e319f"}, {{}}, /*spender_nlocktime=*/0, /*spender_nsequence=*/CTxIn::SEQUENCE_FINAL, {{"292e2df59e3a22109200beed0cdc84b12e66793e"_hex_v_u8, "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"_hex_v_u8}});
|
||||
// Same for hash256
|
||||
Check("wsh(and_v(v:hash256(ae253ca2a54debcac7ecf414f6734f48c56421a08bb59182ff9f39a6fffdb588),pk(xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc)))", "wsh(and_v(v:hash256(ae253ca2a54debcac7ecf414f6734f48c56421a08bb59182ff9f39a6fffdb588),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", "wsh(and_v(v:hash256(ae253ca2a54debcac7ecf414f6734f48c56421a08bb59182ff9f39a6fffdb588),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", SIGNABLE_FAILS, {{"0020cf62bf97baf977aec69cbc290c372899f913337a9093e8f066ab59b8657a365c"}}, OutputType::BECH32, /*op_desc_id=*/uint256{"8412ba3ac20ba3a30f81442d10d32e0468fa52814960d04e959bf84a9b813b88"}, {{}}, /*spender_nlocktime=*/0, /*spender_nsequence=*/CTxIn::SEQUENCE_FINAL, {});
|
||||
Check("wsh(and_v(v:hash256(ae253ca2a54debcac7ecf414f6734f48c56421a08bb59182ff9f39a6fffdb588),pk(xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc)))", "wsh(and_v(v:hash256(ae253ca2a54debcac7ecf414f6734f48c56421a08bb59182ff9f39a6fffdb588),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", "wsh(and_v(v:hash256(ae253ca2a54debcac7ecf414f6734f48c56421a08bb59182ff9f39a6fffdb588),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", SIGNABLE, {{"0020cf62bf97baf977aec69cbc290c372899f913337a9093e8f066ab59b8657a365c"}}, OutputType::BECH32, /*op_desc_id=*/uint256{"8412ba3ac20ba3a30f81442d10d32e0468fa52814960d04e959bf84a9b813b88"}, {{}}, /*spender_nlocktime=*/0, /*spender_nsequence=*/CTxIn::SEQUENCE_FINAL, {{ParseHex("ae253ca2a54debcac7ecf414f6734f48c56421a08bb59182ff9f39a6fffdb588"), ParseHex("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f")}});
|
||||
Check("wsh(and_v(v:hash256(ae253ca2a54debcac7ecf414f6734f48c56421a08bb59182ff9f39a6fffdb588),pk(xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc)))", "wsh(and_v(v:hash256(ae253ca2a54debcac7ecf414f6734f48c56421a08bb59182ff9f39a6fffdb588),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", "wsh(and_v(v:hash256(ae253ca2a54debcac7ecf414f6734f48c56421a08bb59182ff9f39a6fffdb588),pk(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL)))", SIGNABLE, {{"0020cf62bf97baf977aec69cbc290c372899f913337a9093e8f066ab59b8657a365c"}}, OutputType::BECH32, /*op_desc_id=*/uint256{"8412ba3ac20ba3a30f81442d10d32e0468fa52814960d04e959bf84a9b813b88"}, {{}}, /*spender_nlocktime=*/0, /*spender_nsequence=*/CTxIn::SEQUENCE_FINAL, {{"ae253ca2a54debcac7ecf414f6734f48c56421a08bb59182ff9f39a6fffdb588"_hex_v_u8, "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"_hex_v_u8}});
|
||||
// Can have a Miniscript expression under tr() if it's alone.
|
||||
Check("tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,thresh(2,pk(L1NKM8dVA1h52mwDrmk1YreTWkAZZTu2vmKLpmLEbFRqGQYjHeEV),s:pk(Kz3iCBy3HNGP5CZWDsAMmnCMFNwqdDohudVN9fvkrN7tAkzKNtM7),adv:older(42)))", "tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,thresh(2,pk(30a6069f344fb784a2b4c99540a91ee727c91e3a25ef6aae867d9c65b5f23529),s:pk(9918d400c1b8c3c478340a40117ced4054b6b58f48cdb3c89b836bdfee1f5766),adv:older(42)))", "tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,thresh(2,pk(30a6069f344fb784a2b4c99540a91ee727c91e3a25ef6aae867d9c65b5f23529),s:pk(9918d400c1b8c3c478340a40117ced4054b6b58f48cdb3c89b836bdfee1f5766),adv:older(42)))", MISSING_PRIVKEYS | XONLY_KEYS | SIGNABLE, {{"512033982eebe204dc66508e4b19cfc31b5ffc6e1bfcbf6e5597dfc2521a52270795"}}, OutputType::BECH32M);
|
||||
// Can have a pkh() expression alone as tr() script path (because pkh() is valid Miniscript).
|
||||
|
@ -973,7 +974,7 @@ BOOST_AUTO_TEST_CASE(descriptor_test)
|
|||
// Can sign for a Miniscript expression containing a hash challenge inside a Taproot tree. (Fails without the
|
||||
// preimages and the sequence, passes with.)
|
||||
Check("tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,{and_v(and_v(v:hash256(ae253ca2a54debcac7ecf414f6734f48c56421a08bb59182ff9f39a6fffdb588),v:pk(KykUPmR5967F4URzMUeCv9kNMU9CNRWycrPmx3ZvfkWoQLabbimL)),older(42)),multi_a(2,adf586a32ad4b0674a86022b000348b681b4c97a811f67eefe4a6e066e55080c,KztMyyi1pXUtuZfJSB7JzVdmJMAz7wfGVFoSRUR5CVZxXxULXuGR)})", "tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,{and_v(and_v(v:hash256(ae253ca2a54debcac7ecf414f6734f48c56421a08bb59182ff9f39a6fffdb588),v:pk(1c9bc926084382e76da33b5a52d17b1fa153c072aae5fb5228ecc2ccf89d79d5)),older(42)),multi_a(2,adf586a32ad4b0674a86022b000348b681b4c97a811f67eefe4a6e066e55080c,14fa4ad085cdee1e2fc73d491b36a96c192382b1d9a21108eb3533f630364f9f)})", "tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,{and_v(and_v(v:hash256(ae253ca2a54debcac7ecf414f6734f48c56421a08bb59182ff9f39a6fffdb588),v:pk(1c9bc926084382e76da33b5a52d17b1fa153c072aae5fb5228ecc2ccf89d79d5)),older(42)),multi_a(2,adf586a32ad4b0674a86022b000348b681b4c97a811f67eefe4a6e066e55080c,14fa4ad085cdee1e2fc73d491b36a96c192382b1d9a21108eb3533f630364f9f)})", MISSING_PRIVKEYS | XONLY_KEYS | SIGNABLE | SIGNABLE_FAILS, {{"51209a3d79db56fbe3ba4d905d827b62e1ed31cd6df1198b8c759d589c0f4efc27bd"}}, OutputType::BECH32M);
|
||||
Check("tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,{and_v(and_v(v:hash256(ae253ca2a54debcac7ecf414f6734f48c56421a08bb59182ff9f39a6fffdb588),v:pk(KykUPmR5967F4URzMUeCv9kNMU9CNRWycrPmx3ZvfkWoQLabbimL)),older(42)),multi_a(2,adf586a32ad4b0674a86022b000348b681b4c97a811f67eefe4a6e066e55080c,KztMyyi1pXUtuZfJSB7JzVdmJMAz7wfGVFoSRUR5CVZxXxULXuGR)})", "tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,{and_v(and_v(v:hash256(ae253ca2a54debcac7ecf414f6734f48c56421a08bb59182ff9f39a6fffdb588),v:pk(1c9bc926084382e76da33b5a52d17b1fa153c072aae5fb5228ecc2ccf89d79d5)),older(42)),multi_a(2,adf586a32ad4b0674a86022b000348b681b4c97a811f67eefe4a6e066e55080c,14fa4ad085cdee1e2fc73d491b36a96c192382b1d9a21108eb3533f630364f9f)})", "tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,{and_v(and_v(v:hash256(ae253ca2a54debcac7ecf414f6734f48c56421a08bb59182ff9f39a6fffdb588),v:pk(1c9bc926084382e76da33b5a52d17b1fa153c072aae5fb5228ecc2ccf89d79d5)),older(42)),multi_a(2,adf586a32ad4b0674a86022b000348b681b4c97a811f67eefe4a6e066e55080c,14fa4ad085cdee1e2fc73d491b36a96c192382b1d9a21108eb3533f630364f9f)})", MISSING_PRIVKEYS | XONLY_KEYS | SIGNABLE, {{"51209a3d79db56fbe3ba4d905d827b62e1ed31cd6df1198b8c759d589c0f4efc27bd"}}, OutputType::BECH32M, /*op_desc_id=*/{}, {{}}, /*spender_nlocktime=*/0, /*spender_nsequence=*/42, /*preimages=*/{{ParseHex("ae253ca2a54debcac7ecf414f6734f48c56421a08bb59182ff9f39a6fffdb588"), ParseHex("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f")}});
|
||||
Check("tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,{and_v(and_v(v:hash256(ae253ca2a54debcac7ecf414f6734f48c56421a08bb59182ff9f39a6fffdb588),v:pk(KykUPmR5967F4URzMUeCv9kNMU9CNRWycrPmx3ZvfkWoQLabbimL)),older(42)),multi_a(2,adf586a32ad4b0674a86022b000348b681b4c97a811f67eefe4a6e066e55080c,KztMyyi1pXUtuZfJSB7JzVdmJMAz7wfGVFoSRUR5CVZxXxULXuGR)})", "tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,{and_v(and_v(v:hash256(ae253ca2a54debcac7ecf414f6734f48c56421a08bb59182ff9f39a6fffdb588),v:pk(1c9bc926084382e76da33b5a52d17b1fa153c072aae5fb5228ecc2ccf89d79d5)),older(42)),multi_a(2,adf586a32ad4b0674a86022b000348b681b4c97a811f67eefe4a6e066e55080c,14fa4ad085cdee1e2fc73d491b36a96c192382b1d9a21108eb3533f630364f9f)})", "tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd,{and_v(and_v(v:hash256(ae253ca2a54debcac7ecf414f6734f48c56421a08bb59182ff9f39a6fffdb588),v:pk(1c9bc926084382e76da33b5a52d17b1fa153c072aae5fb5228ecc2ccf89d79d5)),older(42)),multi_a(2,adf586a32ad4b0674a86022b000348b681b4c97a811f67eefe4a6e066e55080c,14fa4ad085cdee1e2fc73d491b36a96c192382b1d9a21108eb3533f630364f9f)})", MISSING_PRIVKEYS | XONLY_KEYS | SIGNABLE, {{"51209a3d79db56fbe3ba4d905d827b62e1ed31cd6df1198b8c759d589c0f4efc27bd"}}, OutputType::BECH32M, /*op_desc_id=*/{}, {{}}, /*spender_nlocktime=*/0, /*spender_nsequence=*/42, /*preimages=*/{{"ae253ca2a54debcac7ecf414f6734f48c56421a08bb59182ff9f39a6fffdb588"_hex_v_u8, "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"_hex_v_u8}});
|
||||
|
||||
// Basic sh(pkh()) with key origin
|
||||
CheckInferDescriptor("a9141a31ad23bf49c247dd531a623c2ef57da3c400c587", "sh(pkh([deadbeef/0h/0h/0]03a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd))", {"76a9149a1c78a507689f6f54b847ad1cef1e614ee23f1e88ac"}, {{"03a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd", "deadbeef/0h/0h/0"}});
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
using namespace util::hex_literals;
|
||||
using util::ToString;
|
||||
|
||||
static const std::string strSecret1 = "5HxWvvfubhXpYYpS3tJkw6fq9jE9j18THftkZjHHfmFiWtmAbrj";
|
||||
|
@ -143,19 +144,22 @@ BOOST_AUTO_TEST_CASE(key_test1)
|
|||
BOOST_CHECK(key1.Sign(hashMsg, detsig));
|
||||
BOOST_CHECK(key1C.Sign(hashMsg, detsigc));
|
||||
BOOST_CHECK(detsig == detsigc);
|
||||
BOOST_CHECK(detsig == ParseHex("304402205dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d022014ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6"));
|
||||
BOOST_CHECK_EQUAL(HexStr(detsig), "304402205dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d022014ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6");
|
||||
|
||||
BOOST_CHECK(key2.Sign(hashMsg, detsig));
|
||||
BOOST_CHECK(key2C.Sign(hashMsg, detsigc));
|
||||
BOOST_CHECK(detsig == detsigc);
|
||||
BOOST_CHECK(detsig == ParseHex("3044022052d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd5022061d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d"));
|
||||
BOOST_CHECK_EQUAL(HexStr(detsig), "3044022052d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd5022061d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d");
|
||||
|
||||
BOOST_CHECK(key1.SignCompact(hashMsg, detsig));
|
||||
BOOST_CHECK(key1C.SignCompact(hashMsg, detsigc));
|
||||
BOOST_CHECK(detsig == ParseHex("1c5dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d14ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6"));
|
||||
BOOST_CHECK(detsigc == ParseHex("205dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d14ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6"));
|
||||
BOOST_CHECK_EQUAL(HexStr(detsig), "1c5dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d14ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6");
|
||||
BOOST_CHECK_EQUAL(HexStr(detsigc), "205dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d14ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6");
|
||||
|
||||
BOOST_CHECK(key2.SignCompact(hashMsg, detsig));
|
||||
BOOST_CHECK(key2C.SignCompact(hashMsg, detsigc));
|
||||
BOOST_CHECK(detsig == ParseHex("1c52d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd561d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d"));
|
||||
BOOST_CHECK(detsigc == ParseHex("2052d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd561d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d"));
|
||||
BOOST_CHECK_EQUAL(HexStr(detsig), "1c52d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd561d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d");
|
||||
BOOST_CHECK_EQUAL(HexStr(detsigc), "2052d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd561d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(key_signature_tests)
|
||||
|
@ -352,9 +356,9 @@ BOOST_AUTO_TEST_CASE(key_ellswift)
|
|||
|
||||
BOOST_AUTO_TEST_CASE(bip341_test_h)
|
||||
{
|
||||
std::vector<unsigned char> G_uncompressed = ParseHex("0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8");
|
||||
constexpr auto G_uncompressed{"0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8"_hex};
|
||||
HashWriter hw;
|
||||
hw.write(MakeByteSpan(G_uncompressed));
|
||||
hw.write(G_uncompressed);
|
||||
XOnlyPubKey H{hw.GetSHA256()};
|
||||
BOOST_CHECK(XOnlyPubKey::NUMS_H == H);
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
using namespace util::hex_literals;
|
||||
using node::BlockAssembler;
|
||||
using node::CBlockTemplate;
|
||||
|
||||
|
@ -607,7 +608,7 @@ void MinerTestingSetup::TestPrioritisedMining(const CScript& scriptPubKey, const
|
|||
BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
|
||||
{
|
||||
// Note that by default, these tests run with size accounting enabled.
|
||||
CScript scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
|
||||
CScript scriptPubKey = CScript() << "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"_hex_v_u8 << OP_CHECKSIG;
|
||||
std::unique_ptr<CBlockTemplate> pblocktemplate;
|
||||
|
||||
CTxMemPool& tx_mempool{*m_node.mempool};
|
||||
|
|
|
@ -23,6 +23,8 @@
|
|||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using namespace util::hex_literals;
|
||||
|
||||
namespace {
|
||||
|
||||
/** TestData groups various kinds of precomputed data necessary in this test. */
|
||||
|
@ -596,11 +598,11 @@ BOOST_AUTO_TEST_CASE(fixed_tests)
|
|||
// - no pubkey before the CHECKSIG
|
||||
constexpr KeyConverter tap_converter{miniscript::MiniscriptContext::TAPSCRIPT};
|
||||
constexpr KeyConverter wsh_converter{miniscript::MiniscriptContext::P2WSH};
|
||||
const auto no_pubkey{ParseHex("ac519c")};
|
||||
const auto no_pubkey{"ac519c"_hex_u8};
|
||||
BOOST_CHECK(miniscript::FromScript({no_pubkey.begin(), no_pubkey.end()}, tap_converter) == nullptr);
|
||||
const auto incomplete_multi_a{ParseHex("ba20c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5ba519c")};
|
||||
const auto incomplete_multi_a{"ba20c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5ba519c"_hex_u8};
|
||||
BOOST_CHECK(miniscript::FromScript({incomplete_multi_a.begin(), incomplete_multi_a.end()}, tap_converter) == nullptr);
|
||||
const auto incomplete_multi_a_2{ParseHex("ac2079be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ac20c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5ba519c")};
|
||||
const auto incomplete_multi_a_2{"ac2079be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ac20c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5ba519c"_hex_u8};
|
||||
BOOST_CHECK(miniscript::FromScript({incomplete_multi_a_2.begin(), incomplete_multi_a_2.end()}, tap_converter) == nullptr);
|
||||
// Can use multi_a under Tapscript but not P2WSH.
|
||||
Test("and_v(v:multi_a(2,03d01115d548e7561b15c38f004d734633687cf4419620095bc5b0f47070afe85a,025601570cb47f238d2b0286db4a990fa0f3ba28d1a319f5e7cf55c2a2444da7cc),after(1231488000))", "?", "20d01115d548e7561b15c38f004d734633687cf4419620095bc5b0f47070afe85aac205601570cb47f238d2b0286db4a990fa0f3ba28d1a319f5e7cf55c2a2444da7ccba529d0400046749b1", TESTMODE_VALID | TESTMODE_NONMAL | TESTMODE_NEEDSIG | TESTMODE_P2WSH_INVALID, 4, 2, {}, {}, 3);
|
||||
|
@ -645,12 +647,12 @@ BOOST_AUTO_TEST_CASE(fixed_tests)
|
|||
|
||||
// Misc unit tests
|
||||
// A Script with a non minimal push is invalid
|
||||
std::vector<unsigned char> nonminpush = ParseHex("0000210232780000feff00ffffffffffff21ff005f00ae21ae00000000060602060406564c2102320000060900fe00005f00ae21ae00100000060606060606000000000000000000000000000000000000000000000000000000000000000000");
|
||||
constexpr auto nonminpush{"0000210232780000feff00ffffffffffff21ff005f00ae21ae00000000060602060406564c2102320000060900fe00005f00ae21ae00100000060606060606000000000000000000000000000000000000000000000000000000000000000000"_hex_u8};
|
||||
const CScript nonminpush_script(nonminpush.begin(), nonminpush.end());
|
||||
BOOST_CHECK(miniscript::FromScript(nonminpush_script, wsh_converter) == nullptr);
|
||||
BOOST_CHECK(miniscript::FromScript(nonminpush_script, tap_converter) == nullptr);
|
||||
// A non-minimal VERIFY (<key> CHECKSIG VERIFY 1)
|
||||
std::vector<unsigned char> nonminverify = ParseHex("2103a0434d9e47f3c86235477c7b1ae6ae5d3442d49b1943c2b752a68e2a47e247c7ac6951");
|
||||
constexpr auto nonminverify{"2103a0434d9e47f3c86235477c7b1ae6ae5d3442d49b1943c2b752a68e2a47e247c7ac6951"_hex_u8};
|
||||
const CScript nonminverify_script(nonminverify.begin(), nonminverify.end());
|
||||
BOOST_CHECK(miniscript::FromScript(nonminverify_script, wsh_converter) == nullptr);
|
||||
BOOST_CHECK(miniscript::FromScript(nonminverify_script, tap_converter) == nullptr);
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
#include <string>
|
||||
|
||||
using namespace std::literals;
|
||||
using namespace util::hex_literals;
|
||||
using util::ToString;
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(net_tests, RegTestingSetup)
|
||||
|
@ -400,9 +401,9 @@ BOOST_AUTO_TEST_CASE(cnetaddr_unserialize_v2)
|
|||
const auto ser_params{CAddress::V2_NETWORK};
|
||||
|
||||
// Valid IPv4.
|
||||
s << Span{ParseHex("01" // network type (IPv4)
|
||||
"04" // address length
|
||||
"01020304")}; // address
|
||||
s << "01" // network type (IPv4)
|
||||
"04" // address length
|
||||
"01020304"_hex; // address
|
||||
s >> ser_params(addr);
|
||||
BOOST_CHECK(addr.IsValid());
|
||||
BOOST_CHECK(addr.IsIPv4());
|
||||
|
@ -411,35 +412,35 @@ BOOST_AUTO_TEST_CASE(cnetaddr_unserialize_v2)
|
|||
BOOST_REQUIRE(s.empty());
|
||||
|
||||
// Invalid IPv4, valid length but address itself is shorter.
|
||||
s << Span{ParseHex("01" // network type (IPv4)
|
||||
"04" // address length
|
||||
"0102")}; // address
|
||||
s << "01" // network type (IPv4)
|
||||
"04" // address length
|
||||
"0102"_hex; // address
|
||||
BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure, HasReason("end of data"));
|
||||
BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
|
||||
s.clear();
|
||||
|
||||
// Invalid IPv4, with bogus length.
|
||||
s << Span{ParseHex("01" // network type (IPv4)
|
||||
"05" // address length
|
||||
"01020304")}; // address
|
||||
s << "01" // network type (IPv4)
|
||||
"05" // address length
|
||||
"01020304"_hex; // address
|
||||
BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
|
||||
HasReason("BIP155 IPv4 address with length 5 (should be 4)"));
|
||||
BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
|
||||
s.clear();
|
||||
|
||||
// Invalid IPv4, with extreme length.
|
||||
s << Span{ParseHex("01" // network type (IPv4)
|
||||
"fd0102" // address length (513 as CompactSize)
|
||||
"01020304")}; // address
|
||||
s << "01" // network type (IPv4)
|
||||
"fd0102" // address length (513 as CompactSize)
|
||||
"01020304"_hex; // address
|
||||
BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
|
||||
HasReason("Address too long: 513 > 512"));
|
||||
BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
|
||||
s.clear();
|
||||
|
||||
// Valid IPv6.
|
||||
s << Span{ParseHex("02" // network type (IPv6)
|
||||
"10" // address length
|
||||
"0102030405060708090a0b0c0d0e0f10")}; // address
|
||||
s << "02" // network type (IPv6)
|
||||
"10" // address length
|
||||
"0102030405060708090a0b0c0d0e0f10"_hex; // address
|
||||
s >> ser_params(addr);
|
||||
BOOST_CHECK(addr.IsValid());
|
||||
BOOST_CHECK(addr.IsIPv6());
|
||||
|
@ -448,11 +449,10 @@ BOOST_AUTO_TEST_CASE(cnetaddr_unserialize_v2)
|
|||
BOOST_REQUIRE(s.empty());
|
||||
|
||||
// Valid IPv6, contains embedded "internal".
|
||||
s << Span{ParseHex(
|
||||
"02" // network type (IPv6)
|
||||
"10" // address length
|
||||
"fd6b88c08724ca978112ca1bbdcafac2")}; // address: 0xfd + sha256("bitcoin")[0:5] +
|
||||
// sha256(name)[0:10]
|
||||
s << "02" // network type (IPv6)
|
||||
"10" // address length
|
||||
"fd6b88c08724ca978112ca1bbdcafac2"_hex; // address: 0xfd + sha256("bitcoin")[0:5] +
|
||||
// sha256(name)[0:10]
|
||||
s >> ser_params(addr);
|
||||
BOOST_CHECK(addr.IsInternal());
|
||||
BOOST_CHECK(addr.IsAddrV1Compatible());
|
||||
|
@ -460,44 +460,43 @@ BOOST_AUTO_TEST_CASE(cnetaddr_unserialize_v2)
|
|||
BOOST_REQUIRE(s.empty());
|
||||
|
||||
// Invalid IPv6, with bogus length.
|
||||
s << Span{ParseHex("02" // network type (IPv6)
|
||||
"04" // address length
|
||||
"00")}; // address
|
||||
s << "02" // network type (IPv6)
|
||||
"04" // address length
|
||||
"00"_hex; // address
|
||||
BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
|
||||
HasReason("BIP155 IPv6 address with length 4 (should be 16)"));
|
||||
BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
|
||||
s.clear();
|
||||
|
||||
// Invalid IPv6, contains embedded IPv4.
|
||||
s << Span{ParseHex("02" // network type (IPv6)
|
||||
"10" // address length
|
||||
"00000000000000000000ffff01020304")}; // address
|
||||
s << "02" // network type (IPv6)
|
||||
"10" // address length
|
||||
"00000000000000000000ffff01020304"_hex; // address
|
||||
s >> ser_params(addr);
|
||||
BOOST_CHECK(!addr.IsValid());
|
||||
BOOST_REQUIRE(s.empty());
|
||||
|
||||
// Invalid IPv6, contains embedded TORv2.
|
||||
s << Span{ParseHex("02" // network type (IPv6)
|
||||
"10" // address length
|
||||
"fd87d87eeb430102030405060708090a")}; // address
|
||||
s << "02" // network type (IPv6)
|
||||
"10" // address length
|
||||
"fd87d87eeb430102030405060708090a"_hex; // address
|
||||
s >> ser_params(addr);
|
||||
BOOST_CHECK(!addr.IsValid());
|
||||
BOOST_REQUIRE(s.empty());
|
||||
|
||||
// TORv2, no longer supported.
|
||||
s << Span{ParseHex("03" // network type (TORv2)
|
||||
"0a" // address length
|
||||
"f1f2f3f4f5f6f7f8f9fa")}; // address
|
||||
s << "03" // network type (TORv2)
|
||||
"0a" // address length
|
||||
"f1f2f3f4f5f6f7f8f9fa"_hex; // address
|
||||
s >> ser_params(addr);
|
||||
BOOST_CHECK(!addr.IsValid());
|
||||
BOOST_REQUIRE(s.empty());
|
||||
|
||||
// Valid TORv3.
|
||||
s << Span{ParseHex("04" // network type (TORv3)
|
||||
"20" // address length
|
||||
"79bcc625184b05194975c28b66b66b04" // address
|
||||
"69f7f6556fb1ac3189a79b40dda32f1f"
|
||||
)};
|
||||
s << "04" // network type (TORv3)
|
||||
"20" // address length
|
||||
"79bcc625184b05194975c28b66b66b04" // address
|
||||
"69f7f6556fb1ac3189a79b40dda32f1f"_hex;
|
||||
s >> ser_params(addr);
|
||||
BOOST_CHECK(addr.IsValid());
|
||||
BOOST_CHECK(addr.IsTor());
|
||||
|
@ -507,20 +506,19 @@ BOOST_AUTO_TEST_CASE(cnetaddr_unserialize_v2)
|
|||
BOOST_REQUIRE(s.empty());
|
||||
|
||||
// Invalid TORv3, with bogus length.
|
||||
s << Span{ParseHex("04" // network type (TORv3)
|
||||
"00" // address length
|
||||
"00" // address
|
||||
)};
|
||||
s << "04" // network type (TORv3)
|
||||
"00" // address length
|
||||
"00"_hex; // address
|
||||
BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
|
||||
HasReason("BIP155 TORv3 address with length 0 (should be 32)"));
|
||||
BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
|
||||
s.clear();
|
||||
|
||||
// Valid I2P.
|
||||
s << Span{ParseHex("05" // network type (I2P)
|
||||
"20" // address length
|
||||
"a2894dabaec08c0051a481a6dac88b64" // address
|
||||
"f98232ae42d4b6fd2fa81952dfe36a87")};
|
||||
s << "05" // network type (I2P)
|
||||
"20" // address length
|
||||
"a2894dabaec08c0051a481a6dac88b64" // address
|
||||
"f98232ae42d4b6fd2fa81952dfe36a87"_hex;
|
||||
s >> ser_params(addr);
|
||||
BOOST_CHECK(addr.IsValid());
|
||||
BOOST_CHECK(addr.IsI2P());
|
||||
|
@ -530,20 +528,18 @@ BOOST_AUTO_TEST_CASE(cnetaddr_unserialize_v2)
|
|||
BOOST_REQUIRE(s.empty());
|
||||
|
||||
// Invalid I2P, with bogus length.
|
||||
s << Span{ParseHex("05" // network type (I2P)
|
||||
"03" // address length
|
||||
"00" // address
|
||||
)};
|
||||
s << "05" // network type (I2P)
|
||||
"03" // address length
|
||||
"00"_hex; // address
|
||||
BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
|
||||
HasReason("BIP155 I2P address with length 3 (should be 32)"));
|
||||
BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
|
||||
s.clear();
|
||||
|
||||
// Valid CJDNS.
|
||||
s << Span{ParseHex("06" // network type (CJDNS)
|
||||
"10" // address length
|
||||
"fc000001000200030004000500060007" // address
|
||||
)};
|
||||
s << "06" // network type (CJDNS)
|
||||
"10" // address length
|
||||
"fc000001000200030004000500060007"_hex; // address
|
||||
s >> ser_params(addr);
|
||||
BOOST_CHECK(addr.IsValid());
|
||||
BOOST_CHECK(addr.IsCJDNS());
|
||||
|
@ -552,49 +548,44 @@ BOOST_AUTO_TEST_CASE(cnetaddr_unserialize_v2)
|
|||
BOOST_REQUIRE(s.empty());
|
||||
|
||||
// Invalid CJDNS, wrong prefix.
|
||||
s << Span{ParseHex("06" // network type (CJDNS)
|
||||
"10" // address length
|
||||
"aa000001000200030004000500060007" // address
|
||||
)};
|
||||
s << "06" // network type (CJDNS)
|
||||
"10" // address length
|
||||
"aa000001000200030004000500060007"_hex; // address
|
||||
s >> ser_params(addr);
|
||||
BOOST_CHECK(addr.IsCJDNS());
|
||||
BOOST_CHECK(!addr.IsValid());
|
||||
BOOST_REQUIRE(s.empty());
|
||||
|
||||
// Invalid CJDNS, with bogus length.
|
||||
s << Span{ParseHex("06" // network type (CJDNS)
|
||||
"01" // address length
|
||||
"00" // address
|
||||
)};
|
||||
s << "06" // network type (CJDNS)
|
||||
"01" // address length
|
||||
"00"_hex; // address
|
||||
BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
|
||||
HasReason("BIP155 CJDNS address with length 1 (should be 16)"));
|
||||
BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
|
||||
s.clear();
|
||||
|
||||
// Unknown, with extreme length.
|
||||
s << Span{ParseHex("aa" // network type (unknown)
|
||||
"fe00000002" // address length (CompactSize's MAX_SIZE)
|
||||
"01020304050607" // address
|
||||
)};
|
||||
s << "aa" // network type (unknown)
|
||||
"fe00000002" // address length (CompactSize's MAX_SIZE)
|
||||
"01020304050607"_hex; // address
|
||||
BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
|
||||
HasReason("Address too long: 33554432 > 512"));
|
||||
BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
|
||||
s.clear();
|
||||
|
||||
// Unknown, with reasonable length.
|
||||
s << Span{ParseHex("aa" // network type (unknown)
|
||||
"04" // address length
|
||||
"01020304" // address
|
||||
)};
|
||||
s << "aa" // network type (unknown)
|
||||
"04" // address length
|
||||
"01020304"_hex; // address
|
||||
s >> ser_params(addr);
|
||||
BOOST_CHECK(!addr.IsValid());
|
||||
BOOST_REQUIRE(s.empty());
|
||||
|
||||
// Unknown, with zero length.
|
||||
s << Span{ParseHex("aa" // network type (unknown)
|
||||
"00" // address length
|
||||
"" // address
|
||||
)};
|
||||
s << "aa" // network type (unknown)
|
||||
"00" // address length
|
||||
""_hex; // address
|
||||
s >> ser_params(addr);
|
||||
BOOST_CHECK(!addr.IsValid());
|
||||
BOOST_REQUIRE(s.empty());
|
||||
|
|
|
@ -177,7 +177,7 @@ void sanity_check_chainparams(const ArgsManager& args, ChainType chain_type)
|
|||
|
||||
// check max target * 4*nPowTargetTimespan doesn't overflow -- see pow.cpp:CalculateNextWorkRequired()
|
||||
if (!consensus.fPowNoRetargeting) {
|
||||
arith_uint256 targ_max{UintToArith256(uint256{"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"})};
|
||||
arith_uint256 targ_max{UintToArith256(uint256{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"})};
|
||||
targ_max /= consensus.nPowTargetTimespan*4;
|
||||
BOOST_CHECK(UintToArith256(consensus.powLimit) < targ_max);
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
#include <univalue.h>
|
||||
|
||||
using namespace util::hex_literals;
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(script_standard_tests, BasicTestingSetup)
|
||||
|
||||
|
@ -388,9 +389,9 @@ BOOST_AUTO_TEST_CASE(script_standard_taproot_builder)
|
|||
BOOST_CHECK_EQUAL(TaprootBuilder::ValidDepths({128,128,127,126,125,124,123,122,121,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100,99,98,97,96,95,94,93,92,91,90,89,88,87,86,85,84,83,82,81,80,79,78,77,76,75,74,73,72,71,70,69,68,67,66,65,64,63,62,61,60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1}), true);
|
||||
BOOST_CHECK_EQUAL(TaprootBuilder::ValidDepths({129,129,128,127,126,125,124,123,122,121,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100,99,98,97,96,95,94,93,92,91,90,89,88,87,86,85,84,83,82,81,80,79,78,77,76,75,74,73,72,71,70,69,68,67,66,65,64,63,62,61,60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1}), false);
|
||||
|
||||
XOnlyPubKey key_inner{ParseHex("79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798")};
|
||||
XOnlyPubKey key_1{ParseHex("c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5")};
|
||||
XOnlyPubKey key_2{ParseHex("f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9")};
|
||||
XOnlyPubKey key_inner{"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"_hex_u8};
|
||||
XOnlyPubKey key_1{"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5"_hex_u8};
|
||||
XOnlyPubKey key_2{"f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9"_hex_u8};
|
||||
CScript script_1 = CScript() << ToByteVector(key_1) << OP_CHECKSIG;
|
||||
CScript script_2 = CScript() << ToByteVector(key_2) << OP_CHECKSIG;
|
||||
constexpr uint256 hash_3{"31fe7061656bea2a36aa60a2f7ef940578049273746935d296426dc0afd86b68"};
|
||||
|
|
|
@ -35,6 +35,8 @@
|
|||
// Uncomment if you want to output updated JSON tests.
|
||||
// #define UPDATE_JSON_TESTS
|
||||
|
||||
using namespace util::hex_literals;
|
||||
|
||||
static const unsigned int gFlags = SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC;
|
||||
|
||||
unsigned int ParseScriptFlags(std::string strFlags);
|
||||
|
@ -1267,7 +1269,7 @@ BOOST_AUTO_TEST_CASE(sign_invalid_miniscript)
|
|||
|
||||
// Create a Taproot output which contains a leaf in which a non-32 bytes push is used where a public key is expected
|
||||
// by the Miniscript parser. This offending Script was found by the RPC fuzzer.
|
||||
const auto invalid_pubkey{ParseHex("173d36c8c9c9c9ffffffffffff0200000000021e1e37373721361818181818181e1e1e1e19000000000000000000b19292929292926b006c9b9b9292")};
|
||||
const auto invalid_pubkey{"173d36c8c9c9c9ffffffffffff0200000000021e1e37373721361818181818181e1e1e1e19000000000000000000b19292929292926b006c9b9b9292"_hex_u8};
|
||||
TaprootBuilder builder;
|
||||
builder.Add(0, {invalid_pubkey}, 0xc0);
|
||||
builder.Finalize(XOnlyPubKey::NUMS_H);
|
||||
|
@ -1354,10 +1356,16 @@ BOOST_AUTO_TEST_CASE(script_GetScriptAsm)
|
|||
BOOST_CHECK_EQUAL(derSig + "83 " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "83")) << vchPubKey));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
CScript ToScript(const T& byte_container)
|
||||
{
|
||||
auto span{MakeUCharSpan(byte_container)};
|
||||
return {span.begin(), span.end()};
|
||||
}
|
||||
|
||||
static CScript ScriptFromHex(const std::string& str)
|
||||
{
|
||||
std::vector<unsigned char> data = ParseHex(str);
|
||||
return CScript(data.begin(), data.end());
|
||||
return ToScript(*Assert(TryParseHex(str)));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(script_FindAndDelete)
|
||||
|
@ -1385,60 +1393,60 @@ BOOST_AUTO_TEST_CASE(script_FindAndDelete)
|
|||
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 4);
|
||||
BOOST_CHECK(s == expect);
|
||||
|
||||
s = ScriptFromHex("0302ff03"); // PUSH 0x02ff03 onto stack
|
||||
d = ScriptFromHex("0302ff03");
|
||||
s = ToScript("0302ff03"_hex); // PUSH 0x02ff03 onto stack
|
||||
d = ToScript("0302ff03"_hex);
|
||||
expect = CScript();
|
||||
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
|
||||
BOOST_CHECK(s == expect);
|
||||
|
||||
s = ScriptFromHex("0302ff030302ff03"); // PUSH 0x2ff03 PUSH 0x2ff03
|
||||
d = ScriptFromHex("0302ff03");
|
||||
s = ToScript("0302ff030302ff03"_hex); // PUSH 0x02ff03 PUSH 0x02ff03
|
||||
d = ToScript("0302ff03"_hex);
|
||||
expect = CScript();
|
||||
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 2);
|
||||
BOOST_CHECK(s == expect);
|
||||
|
||||
s = ScriptFromHex("0302ff030302ff03");
|
||||
d = ScriptFromHex("02");
|
||||
s = ToScript("0302ff030302ff03"_hex);
|
||||
d = ToScript("02"_hex);
|
||||
expect = s; // FindAndDelete matches entire opcodes
|
||||
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 0);
|
||||
BOOST_CHECK(s == expect);
|
||||
|
||||
s = ScriptFromHex("0302ff030302ff03");
|
||||
d = ScriptFromHex("ff");
|
||||
s = ToScript("0302ff030302ff03"_hex);
|
||||
d = ToScript("ff"_hex);
|
||||
expect = s;
|
||||
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 0);
|
||||
BOOST_CHECK(s == expect);
|
||||
|
||||
// This is an odd edge case: strip of the push-three-bytes
|
||||
// prefix, leaving 02ff03 which is push-two-bytes:
|
||||
s = ScriptFromHex("0302ff030302ff03");
|
||||
d = ScriptFromHex("03");
|
||||
expect = CScript() << ParseHex("ff03") << ParseHex("ff03");
|
||||
s = ToScript("0302ff030302ff03"_hex);
|
||||
d = ToScript("03"_hex);
|
||||
expect = CScript() << "ff03"_hex_v_u8 << "ff03"_hex_v_u8;
|
||||
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 2);
|
||||
BOOST_CHECK(s == expect);
|
||||
|
||||
// Byte sequence that spans multiple opcodes:
|
||||
s = ScriptFromHex("02feed5169"); // PUSH(0xfeed) OP_1 OP_VERIFY
|
||||
d = ScriptFromHex("feed51");
|
||||
s = ToScript("02feed5169"_hex); // PUSH(0xfeed) OP_1 OP_VERIFY
|
||||
d = ToScript("feed51"_hex);
|
||||
expect = s;
|
||||
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 0); // doesn't match 'inside' opcodes
|
||||
BOOST_CHECK(s == expect);
|
||||
|
||||
s = ScriptFromHex("02feed5169"); // PUSH(0xfeed) OP_1 OP_VERIFY
|
||||
d = ScriptFromHex("02feed51");
|
||||
expect = ScriptFromHex("69");
|
||||
s = ToScript("02feed5169"_hex); // PUSH(0xfeed) OP_1 OP_VERIFY
|
||||
d = ToScript("02feed51"_hex);
|
||||
expect = ToScript("69"_hex);
|
||||
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
|
||||
BOOST_CHECK(s == expect);
|
||||
|
||||
s = ScriptFromHex("516902feed5169");
|
||||
d = ScriptFromHex("feed51");
|
||||
s = ToScript("516902feed5169"_hex);
|
||||
d = ToScript("feed51"_hex);
|
||||
expect = s;
|
||||
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 0);
|
||||
BOOST_CHECK(s == expect);
|
||||
|
||||
s = ScriptFromHex("516902feed5169");
|
||||
d = ScriptFromHex("02feed51");
|
||||
expect = ScriptFromHex("516969");
|
||||
s = ToScript("516902feed5169"_hex);
|
||||
d = ToScript("02feed51"_hex);
|
||||
expect = ToScript("516969"_hex);
|
||||
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
|
||||
BOOST_CHECK(s == expect);
|
||||
|
||||
|
@ -1456,15 +1464,15 @@ BOOST_AUTO_TEST_CASE(script_FindAndDelete)
|
|||
|
||||
// Another weird edge case:
|
||||
// End with invalid push (not enough data)...
|
||||
s = ScriptFromHex("0003feed");
|
||||
d = ScriptFromHex("03feed"); // ... can remove the invalid push
|
||||
expect = ScriptFromHex("00");
|
||||
s = ToScript("0003feed"_hex);
|
||||
d = ToScript("03feed"_hex); // ... can remove the invalid push
|
||||
expect = ToScript("00"_hex);
|
||||
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
|
||||
BOOST_CHECK(s == expect);
|
||||
|
||||
s = ScriptFromHex("0003feed");
|
||||
d = ScriptFromHex("00");
|
||||
expect = ScriptFromHex("03feed");
|
||||
s = ToScript("0003feed"_hex);
|
||||
d = ToScript("00"_hex);
|
||||
expect = ToScript("03feed"_hex);
|
||||
BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
|
||||
BOOST_CHECK(s == expect);
|
||||
}
|
||||
|
@ -1473,13 +1481,13 @@ BOOST_AUTO_TEST_CASE(script_HasValidOps)
|
|||
{
|
||||
// Exercise the HasValidOps functionality
|
||||
CScript script;
|
||||
script = ScriptFromHex("76a9141234567890abcdefa1a2a3a4a5a6a7a8a9a0aaab88ac"); // Normal script
|
||||
script = ToScript("76a9141234567890abcdefa1a2a3a4a5a6a7a8a9a0aaab88ac"_hex); // Normal script
|
||||
BOOST_CHECK(script.HasValidOps());
|
||||
script = ScriptFromHex("76a914ff34567890abcdefa1a2a3a4a5a6a7a8a9a0aaab88ac");
|
||||
script = ToScript("76a914ff34567890abcdefa1a2a3a4a5a6a7a8a9a0aaab88ac"_hex);
|
||||
BOOST_CHECK(script.HasValidOps());
|
||||
script = ScriptFromHex("ff88ac"); // Script with OP_INVALIDOPCODE explicit
|
||||
script = ToScript("ff88ac"_hex); // Script with OP_INVALIDOPCODE explicit
|
||||
BOOST_CHECK(!script.HasValidOps());
|
||||
script = ScriptFromHex("88acc0"); // Script with undefined opcode
|
||||
script = ToScript("88acc0"_hex); // Script with undefined opcode
|
||||
BOOST_CHECK(!script.HasValidOps());
|
||||
}
|
||||
|
||||
|
|
|
@ -39,6 +39,7 @@
|
|||
|
||||
#include <univalue.h>
|
||||
|
||||
using namespace util::hex_literals;
|
||||
using util::SplitString;
|
||||
using util::ToString;
|
||||
|
||||
|
@ -851,24 +852,24 @@ BOOST_AUTO_TEST_CASE(test_IsStandard)
|
|||
CheckIsNotStandard(t, "scriptpubkey");
|
||||
|
||||
// MAX_OP_RETURN_RELAY-byte TxoutType::NULL_DATA (standard)
|
||||
t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3804678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
|
||||
t.vout[0].scriptPubKey = CScript() << OP_RETURN << "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3804678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38"_hex_v_u8;
|
||||
BOOST_CHECK_EQUAL(MAX_OP_RETURN_RELAY, t.vout[0].scriptPubKey.size());
|
||||
CheckIsStandard(t);
|
||||
|
||||
// MAX_OP_RETURN_RELAY+1-byte TxoutType::NULL_DATA (non-standard)
|
||||
t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3804678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3800");
|
||||
t.vout[0].scriptPubKey = CScript() << OP_RETURN << "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3804678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3800"_hex_v_u8;
|
||||
BOOST_CHECK_EQUAL(MAX_OP_RETURN_RELAY + 1, t.vout[0].scriptPubKey.size());
|
||||
CheckIsNotStandard(t, "scriptpubkey");
|
||||
|
||||
// Data payload can be encoded in any way...
|
||||
t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("");
|
||||
t.vout[0].scriptPubKey = CScript() << OP_RETURN << ""_hex_v_u8;
|
||||
CheckIsStandard(t);
|
||||
t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("00") << ParseHex("01");
|
||||
t.vout[0].scriptPubKey = CScript() << OP_RETURN << "00"_hex_v_u8 << "01"_hex_v_u8;
|
||||
CheckIsStandard(t);
|
||||
// OP_RESERVED *is* considered to be a PUSHDATA type opcode by IsPushOnly()!
|
||||
t.vout[0].scriptPubKey = CScript() << OP_RETURN << OP_RESERVED << -1 << 0 << ParseHex("01") << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12 << 13 << 14 << 15 << 16;
|
||||
t.vout[0].scriptPubKey = CScript() << OP_RETURN << OP_RESERVED << -1 << 0 << "01"_hex_v_u8 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12 << 13 << 14 << 15 << 16;
|
||||
CheckIsStandard(t);
|
||||
t.vout[0].scriptPubKey = CScript() << OP_RETURN << 0 << ParseHex("01") << 2 << ParseHex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
||||
t.vout[0].scriptPubKey = CScript() << OP_RETURN << 0 << "01"_hex_v_u8 << 2 << "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"_hex_v_u8;
|
||||
CheckIsStandard(t);
|
||||
|
||||
// ...so long as it only contains PUSHDATA's
|
||||
|
@ -882,13 +883,13 @@ BOOST_AUTO_TEST_CASE(test_IsStandard)
|
|||
|
||||
// Only one TxoutType::NULL_DATA permitted in all cases
|
||||
t.vout.resize(2);
|
||||
t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
|
||||
t.vout[0].scriptPubKey = CScript() << OP_RETURN << "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38"_hex_v_u8;
|
||||
t.vout[0].nValue = 0;
|
||||
t.vout[1].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
|
||||
t.vout[1].scriptPubKey = CScript() << OP_RETURN << "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38"_hex_v_u8;
|
||||
t.vout[1].nValue = 0;
|
||||
CheckIsNotStandard(t, "multi-op-return");
|
||||
|
||||
t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
|
||||
t.vout[0].scriptPubKey = CScript() << OP_RETURN << "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38"_hex_v_u8;
|
||||
t.vout[1].scriptPubKey = CScript() << OP_RETURN;
|
||||
CheckIsNotStandard(t, "multi-op-return");
|
||||
|
||||
|
|
|
@ -20,6 +20,8 @@
|
|||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
using namespace util::hex_literals;
|
||||
|
||||
// A fee amount that is above 1sat/vB but below 5sat/vB for most transactions created within these
|
||||
// unit tests.
|
||||
static const CAmount low_fee_amt{200};
|
||||
|
@ -51,21 +53,21 @@ BOOST_AUTO_TEST_CASE(package_hash_tests)
|
|||
{
|
||||
// Random real segwit transaction
|
||||
DataStream stream_1{
|
||||
ParseHex("02000000000101964b8aa63509579ca6086e6012eeaa4c2f4dd1e283da29b67c8eea38b3c6fd220000000000fdffffff0294c618000000000017a9145afbbb42f4e83312666d0697f9e66259912ecde38768fa2c0000000000160014897388a0889390fd0e153a22bb2cf9d8f019faf50247304402200547406380719f84d68cf4e96cc3e4a1688309ef475b150be2b471c70ea562aa02206d255f5acc40fd95981874d77201d2eb07883657ce1c796513f32b6079545cdf0121023ae77335cefcb5ab4c1dc1fb0d2acfece184e593727d7d5906c78e564c7c11d125cf0c00"),
|
||||
"02000000000101964b8aa63509579ca6086e6012eeaa4c2f4dd1e283da29b67c8eea38b3c6fd220000000000fdffffff0294c618000000000017a9145afbbb42f4e83312666d0697f9e66259912ecde38768fa2c0000000000160014897388a0889390fd0e153a22bb2cf9d8f019faf50247304402200547406380719f84d68cf4e96cc3e4a1688309ef475b150be2b471c70ea562aa02206d255f5acc40fd95981874d77201d2eb07883657ce1c796513f32b6079545cdf0121023ae77335cefcb5ab4c1dc1fb0d2acfece184e593727d7d5906c78e564c7c11d125cf0c00"_hex,
|
||||
};
|
||||
CTransaction tx_1(deserialize, TX_WITH_WITNESS, stream_1);
|
||||
CTransactionRef ptx_1{MakeTransactionRef(tx_1)};
|
||||
|
||||
// Random real nonsegwit transaction
|
||||
DataStream stream_2{
|
||||
ParseHex("01000000010b26e9b7735eb6aabdf358bab62f9816a21ba9ebdb719d5299e88607d722c190000000008b4830450220070aca44506c5cef3a16ed519d7c3c39f8aab192c4e1c90d065f37b8a4af6141022100a8e160b856c2d43d27d8fba71e5aef6405b8643ac4cb7cb3c462aced7f14711a0141046d11fee51b0e60666d5049a9101a72741df480b96ee26488a4d3466b95c9a40ac5eeef87e10a5cd336c19a84565f80fa6c547957b7700ff4dfbdefe76036c339ffffffff021bff3d11000000001976a91404943fdd508053c75000106d3bc6e2754dbcff1988ac2f15de00000000001976a914a266436d2965547608b9e15d9032a7b9d64fa43188ac00000000"),
|
||||
"01000000010b26e9b7735eb6aabdf358bab62f9816a21ba9ebdb719d5299e88607d722c190000000008b4830450220070aca44506c5cef3a16ed519d7c3c39f8aab192c4e1c90d065f37b8a4af6141022100a8e160b856c2d43d27d8fba71e5aef6405b8643ac4cb7cb3c462aced7f14711a0141046d11fee51b0e60666d5049a9101a72741df480b96ee26488a4d3466b95c9a40ac5eeef87e10a5cd336c19a84565f80fa6c547957b7700ff4dfbdefe76036c339ffffffff021bff3d11000000001976a91404943fdd508053c75000106d3bc6e2754dbcff1988ac2f15de00000000001976a914a266436d2965547608b9e15d9032a7b9d64fa43188ac00000000"_hex,
|
||||
};
|
||||
CTransaction tx_2(deserialize, TX_WITH_WITNESS, stream_2);
|
||||
CTransactionRef ptx_2{MakeTransactionRef(tx_2)};
|
||||
|
||||
// Random real segwit transaction
|
||||
DataStream stream_3{
|
||||
ParseHex("0200000000010177862801f77c2c068a70372b4c435ef8dd621291c36a64eb4dd491f02218f5324600000000fdffffff014a0100000000000022512035ea312034cfac01e956a269f3bf147f569c2fbb00180677421262da042290d803402be713325ff285e66b0380f53f2fae0d0fb4e16f378a440fed51ce835061437566729d4883bc917632f3cff474d6384bc8b989961a1d730d4a87ed38ad28bd337b20f1d658c6c138b1c312e072b4446f50f01ae0da03a42e6274f8788aae53416a7fac0063036f7264010118746578742f706c61696e3b636861727365743d7574662d3800357b2270223a226272632d3230222c226f70223a226d696e74222c227469636b223a224342414c222c22616d74223a2236393639227d6821c1f1d658c6c138b1c312e072b4446f50f01ae0da03a42e6274f8788aae53416a7f00000000"),
|
||||
"0200000000010177862801f77c2c068a70372b4c435ef8dd621291c36a64eb4dd491f02218f5324600000000fdffffff014a0100000000000022512035ea312034cfac01e956a269f3bf147f569c2fbb00180677421262da042290d803402be713325ff285e66b0380f53f2fae0d0fb4e16f378a440fed51ce835061437566729d4883bc917632f3cff474d6384bc8b989961a1d730d4a87ed38ad28bd337b20f1d658c6c138b1c312e072b4446f50f01ae0da03a42e6274f8788aae53416a7fac0063036f7264010118746578742f706c61696e3b636861727365743d7574662d3800357b2270223a226272632d3230222c226f70223a226d696e74222c227469636b223a224342414c222c22616d74223a2236393639227d6821c1f1d658c6c138b1c312e072b4446f50f01ae0da03a42e6274f8788aae53416a7f00000000"_hex,
|
||||
};
|
||||
CTransaction tx_3(deserialize, TX_WITH_WITNESS, stream_3);
|
||||
CTransactionRef ptx_3{MakeTransactionRef(tx_3)};
|
||||
|
|
|
@ -431,7 +431,7 @@ BOOST_AUTO_TEST_CASE( check_ONE )
|
|||
BOOST_AUTO_TEST_CASE(FromHex_vs_uint256)
|
||||
{
|
||||
auto runtime_uint{uint256::FromHex("4A5E1E4BAAB89F3A32518A88C31BC87F618f76673e2cc77ab2127b7afdeda33b").value()};
|
||||
constexpr uint256 consteval_uint{ "4a5e1e4baab89f3a32518a88c31bc87f618F76673E2CC77AB2127B7AFDEDA33B"};
|
||||
constexpr uint256 consteval_uint{ "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"};
|
||||
BOOST_CHECK_EQUAL(consteval_uint, runtime_uint);
|
||||
}
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -7,6 +7,7 @@
|
|||
#include <hash.h> // For Hash()
|
||||
#include <key.h> // For CKey
|
||||
#include <script/parsing.h>
|
||||
#include <span.h>
|
||||
#include <sync.h>
|
||||
#include <test/util/random.h>
|
||||
#include <test/util/setup_common.h>
|
||||
|
@ -45,6 +46,8 @@
|
|||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
using namespace std::literals;
|
||||
using namespace util::hex_literals;
|
||||
using util::ConstevalHexDigit;
|
||||
using util::Join;
|
||||
using util::RemovePrefix;
|
||||
using util::RemovePrefixView;
|
||||
|
@ -136,46 +139,69 @@ BOOST_AUTO_TEST_CASE(util_criticalsection)
|
|||
} while(0);
|
||||
}
|
||||
|
||||
static const unsigned char ParseHex_expected[65] = {
|
||||
constexpr char HEX_PARSE_INPUT[] = "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f";
|
||||
constexpr uint8_t HEX_PARSE_OUTPUT[] = {
|
||||
0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7,
|
||||
0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde,
|
||||
0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12,
|
||||
0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,
|
||||
0x5f
|
||||
};
|
||||
static_assert((sizeof(HEX_PARSE_INPUT) - 1) == 2 * sizeof(HEX_PARSE_OUTPUT));
|
||||
BOOST_AUTO_TEST_CASE(parse_hex)
|
||||
{
|
||||
std::vector<unsigned char> result;
|
||||
std::vector<unsigned char> expected(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected));
|
||||
|
||||
// Basic test vector
|
||||
result = ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
|
||||
std::vector<unsigned char> expected(std::begin(HEX_PARSE_OUTPUT), std::end(HEX_PARSE_OUTPUT));
|
||||
constexpr std::array<std::byte, 65> hex_literal_array{operator""_hex<util::detail::Hex(HEX_PARSE_INPUT)>()};
|
||||
auto hex_literal_span{MakeUCharSpan(hex_literal_array)};
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_span.begin(), hex_literal_span.end(), expected.begin(), expected.end());
|
||||
|
||||
const std::vector<std::byte> hex_literal_vector{operator""_hex_v<util::detail::Hex(HEX_PARSE_INPUT)>()};
|
||||
hex_literal_span = MakeUCharSpan(hex_literal_vector);
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_span.begin(), hex_literal_span.end(), expected.begin(), expected.end());
|
||||
|
||||
constexpr std::array<uint8_t, 65> hex_literal_array_uint8{operator""_hex_u8<util::detail::Hex(HEX_PARSE_INPUT)>()};
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_array_uint8.begin(), hex_literal_array_uint8.end(), expected.begin(), expected.end());
|
||||
|
||||
result = operator""_hex_v_u8<util::detail::Hex(HEX_PARSE_INPUT)>();
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
|
||||
result = TryParseHex<uint8_t>("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f").value();
|
||||
|
||||
result = ParseHex(HEX_PARSE_INPUT);
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
|
||||
|
||||
result = TryParseHex<uint8_t>(HEX_PARSE_INPUT).value();
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
|
||||
|
||||
// Spaces between bytes must be supported
|
||||
expected = {0x12, 0x34, 0x56, 0x78};
|
||||
result = ParseHex("12 34 56 78");
|
||||
BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
|
||||
result = TryParseHex<uint8_t>("12 34 56 78").value();
|
||||
BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
|
||||
|
||||
// Leading space must be supported (used in BerkeleyEnvironment::Salvage)
|
||||
expected = {0x89, 0x34, 0x56, 0x78};
|
||||
result = ParseHex(" 89 34 56 78");
|
||||
BOOST_CHECK(result.size() == 4 && result[0] == 0x89 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
|
||||
result = TryParseHex<uint8_t>(" 89 34 56 78").value();
|
||||
BOOST_CHECK(result.size() == 4 && result[0] == 0x89 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
|
||||
|
||||
// Mixed case and spaces are supported
|
||||
expected = {0xff, 0xaa};
|
||||
result = ParseHex(" Ff aA ");
|
||||
BOOST_CHECK(result.size() == 2 && result[0] == 0xff && result[1] == 0xaa);
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
|
||||
result = TryParseHex<uint8_t>(" Ff aA ").value();
|
||||
BOOST_CHECK(result.size() == 2 && result[0] == 0xff && result[1] == 0xaa);
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
|
||||
|
||||
// Empty string is supported
|
||||
result = ParseHex("");
|
||||
BOOST_CHECK(result.size() == 0);
|
||||
result = TryParseHex<uint8_t>("").value();
|
||||
BOOST_CHECK(result.size() == 0);
|
||||
static_assert(""_hex.empty());
|
||||
static_assert(""_hex_u8.empty());
|
||||
BOOST_CHECK_EQUAL(""_hex_v.size(), 0);
|
||||
BOOST_CHECK_EQUAL(""_hex_v_u8.size(), 0);
|
||||
BOOST_CHECK_EQUAL(ParseHex("").size(), 0);
|
||||
BOOST_CHECK_EQUAL(TryParseHex<uint8_t>("").value().size(), 0);
|
||||
|
||||
// Spaces between nibbles is treated as invalid
|
||||
BOOST_CHECK_EQUAL(ParseHex("AAF F").size(), 0);
|
||||
|
@ -198,25 +224,25 @@ BOOST_AUTO_TEST_CASE(parse_hex)
|
|||
BOOST_CHECK(!TryParseHex("12 3").has_value());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(consteval_hex_digit)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(ConstevalHexDigit('0'), 0);
|
||||
BOOST_CHECK_EQUAL(ConstevalHexDigit('9'), 9);
|
||||
BOOST_CHECK_EQUAL(ConstevalHexDigit('a'), 0xa);
|
||||
BOOST_CHECK_EQUAL(ConstevalHexDigit('f'), 0xf);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(util_HexStr)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(
|
||||
HexStr(ParseHex_expected),
|
||||
"04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
|
||||
|
||||
BOOST_CHECK_EQUAL(
|
||||
HexStr(Span{ParseHex_expected}.last(0)),
|
||||
"");
|
||||
|
||||
BOOST_CHECK_EQUAL(
|
||||
HexStr(Span{ParseHex_expected}.first(0)),
|
||||
"");
|
||||
BOOST_CHECK_EQUAL(HexStr(HEX_PARSE_OUTPUT), HEX_PARSE_INPUT);
|
||||
BOOST_CHECK_EQUAL(HexStr(Span{HEX_PARSE_OUTPUT}.last(0)), "");
|
||||
BOOST_CHECK_EQUAL(HexStr(Span{HEX_PARSE_OUTPUT}.first(0)), "");
|
||||
|
||||
{
|
||||
const std::vector<char> in_s{ParseHex_expected, ParseHex_expected + 5};
|
||||
constexpr std::string_view out_exp{"04678afdb0"};
|
||||
constexpr std::span in_s{HEX_PARSE_OUTPUT, out_exp.size() / 2};
|
||||
const Span<const uint8_t> in_u{MakeUCharSpan(in_s)};
|
||||
const Span<const std::byte> in_b{MakeByteSpan(in_s)};
|
||||
const std::string out_exp{"04678afdb0"};
|
||||
|
||||
BOOST_CHECK_EQUAL(HexStr(in_u), out_exp);
|
||||
BOOST_CHECK_EQUAL(HexStr(in_s), out_exp);
|
||||
|
|
|
@ -127,20 +127,11 @@ public:
|
|||
template <unsigned int BITS>
|
||||
consteval base_blob<BITS>::base_blob(std::string_view hex_str)
|
||||
{
|
||||
// Non-lookup table version of HexDigit().
|
||||
auto from_hex = [](const char c) -> int8_t {
|
||||
if (c >= '0' && c <= '9') return c - '0';
|
||||
if (c >= 'a' && c <= 'f') return c - 'a' + 0xA;
|
||||
if (c >= 'A' && c <= 'F') return c - 'A' + 0xA;
|
||||
|
||||
assert(false); // Reached if ctor is called with an invalid hex digit.
|
||||
};
|
||||
|
||||
assert(hex_str.length() == m_data.size() * 2); // 2 hex digits per byte.
|
||||
if (hex_str.length() != m_data.size() * 2) throw "Hex string must fit exactly";
|
||||
auto str_it = hex_str.rbegin();
|
||||
for (auto& elem : m_data) {
|
||||
auto lo = from_hex(*(str_it++));
|
||||
elem = (from_hex(*(str_it++)) << 4) | lo;
|
||||
auto lo = util::ConstevalHexDigit(*(str_it++));
|
||||
elem = (util::ConstevalHexDigit(*(str_it++)) << 4) | lo;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -13,6 +13,8 @@
|
|||
#include <span.h>
|
||||
#include <util/string.h>
|
||||
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <charconv>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
@ -365,4 +367,79 @@ std::string Capitalize(std::string str);
|
|||
*/
|
||||
std::optional<uint64_t> ParseByteUnits(std::string_view str, ByteUnit default_multiplier);
|
||||
|
||||
namespace util {
|
||||
/** consteval version of HexDigit() without the lookup table. */
|
||||
consteval uint8_t ConstevalHexDigit(const char c)
|
||||
{
|
||||
if (c >= '0' && c <= '9') return c - '0';
|
||||
if (c >= 'a' && c <= 'f') return c - 'a' + 0xa;
|
||||
|
||||
throw "Only lowercase hex digits are allowed, for consistency";
|
||||
}
|
||||
|
||||
/**
|
||||
* ""_hex is a compile-time user-defined literal returning a
|
||||
* `std::array<std::byte>`, equivalent to ParseHex(). Variants provided:
|
||||
*
|
||||
* - ""_hex_v: Returns `std::vector<std::byte>`, useful for heap allocation or
|
||||
* variable-length serialization.
|
||||
*
|
||||
* - ""_hex_u8: Returns `std::array<uint8_t>`, for cases where `std::byte` is
|
||||
* incompatible.
|
||||
*
|
||||
* - ""_hex_v_u8: Returns `std::vector<uint8_t>`, combining heap allocation with
|
||||
* `uint8_t`.
|
||||
*
|
||||
* @warning It could be necessary to use vector instead of array variants when
|
||||
* serializing, or vice versa, because vectors are assumed to be variable-
|
||||
* length and serialized with a size prefix, while arrays are considered fixed
|
||||
* length and serialized with no prefix.
|
||||
*
|
||||
* @warning It may be preferable to use vector variants to save stack space when
|
||||
* declaring local variables if hex strings are large. Alternatively variables
|
||||
* could be declared constexpr to avoid using stack space.
|
||||
*
|
||||
* @warning Avoid `uint8_t` variants when not necessary, as the codebase
|
||||
* migrates to use `std::byte` instead of `unsigned char` and `uint8_t`.
|
||||
*
|
||||
* @note One reason ""_hex uses `std::array` instead of `std::vector` like
|
||||
* ParseHex() does is because heap-based containers cannot cross the compile-
|
||||
* time/runtime barrier.
|
||||
*/
|
||||
inline namespace hex_literals {
|
||||
namespace detail {
|
||||
|
||||
template <size_t N>
|
||||
struct Hex {
|
||||
std::array<std::byte, N / 2> bytes{};
|
||||
consteval Hex(const char (&hex_str)[N])
|
||||
// 2 hex digits required per byte + implicit null terminator
|
||||
requires(N % 2 == 1)
|
||||
{
|
||||
if (hex_str[N - 1]) throw "null terminator required";
|
||||
for (std::size_t i = 0; i < bytes.size(); ++i) {
|
||||
bytes[i] = static_cast<std::byte>(
|
||||
(ConstevalHexDigit(hex_str[2 * i]) << 4) |
|
||||
ConstevalHexDigit(hex_str[2 * i + 1]));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <util::detail::Hex str>
|
||||
constexpr auto operator""_hex() { return str.bytes; }
|
||||
|
||||
template <util::detail::Hex str>
|
||||
constexpr auto operator""_hex_u8() { return std::bit_cast<std::array<uint8_t, str.bytes.size()>>(str.bytes); }
|
||||
|
||||
template <util::detail::Hex str>
|
||||
constexpr auto operator""_hex_v() { return std::vector<std::byte>{str.bytes.begin(), str.bytes.end()}; }
|
||||
|
||||
template <util::detail::Hex str>
|
||||
inline auto operator""_hex_v_u8() { return std::vector<uint8_t>{UCharCast(str.bytes.data()), UCharCast(str.bytes.data() + str.bytes.size())}; }
|
||||
|
||||
} // inline namespace hex_literals
|
||||
} // namespace util
|
||||
|
||||
#endif // BITCOIN_UTIL_STRENCODINGS_H
|
||||
|
|
|
@ -8,10 +8,11 @@
|
|||
#include <crypto/aes.h>
|
||||
#include <crypto/sha512.h>
|
||||
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
namespace wallet {
|
||||
int CCrypter::BytesToKeySHA512AES(const std::vector<unsigned char>& chSalt, const SecureString& strKeyData, int count, unsigned char *key,unsigned char *iv) const
|
||||
int CCrypter::BytesToKeySHA512AES(const std::span<const unsigned char> salt, const SecureString& key_data, int count, unsigned char* key, unsigned char* iv) const
|
||||
{
|
||||
// This mimics the behavior of openssl's EVP_BytesToKey with an aes256cbc
|
||||
// cipher and sha512 message digest. Because sha512's output size (64b) is
|
||||
|
@ -24,8 +25,8 @@ int CCrypter::BytesToKeySHA512AES(const std::vector<unsigned char>& chSalt, cons
|
|||
unsigned char buf[CSHA512::OUTPUT_SIZE];
|
||||
CSHA512 di;
|
||||
|
||||
di.Write((const unsigned char*)strKeyData.data(), strKeyData.size());
|
||||
di.Write(chSalt.data(), chSalt.size());
|
||||
di.Write(UCharCast(key_data.data()), key_data.size());
|
||||
di.Write(salt.data(), salt.size());
|
||||
di.Finalize(buf);
|
||||
|
||||
for(int i = 0; i != count - 1; i++)
|
||||
|
@ -37,14 +38,16 @@ int CCrypter::BytesToKeySHA512AES(const std::vector<unsigned char>& chSalt, cons
|
|||
return WALLET_CRYPTO_KEY_SIZE;
|
||||
}
|
||||
|
||||
bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
|
||||
bool CCrypter::SetKeyFromPassphrase(const SecureString& key_data, const std::span<const unsigned char> salt, const unsigned int rounds, const unsigned int derivation_method)
|
||||
{
|
||||
if (nRounds < 1 || chSalt.size() != WALLET_CRYPTO_SALT_SIZE)
|
||||
if (rounds < 1 || salt.size() != WALLET_CRYPTO_SALT_SIZE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
if (nDerivationMethod == 0)
|
||||
i = BytesToKeySHA512AES(chSalt, strKeyData, nRounds, vchKey.data(), vchIV.data());
|
||||
if (derivation_method == 0) {
|
||||
i = BytesToKeySHA512AES(salt, key_data, rounds, vchKey.data(), vchIV.data());
|
||||
}
|
||||
|
||||
if (i != (int)WALLET_CRYPTO_KEY_SIZE)
|
||||
{
|
||||
|
@ -57,13 +60,14 @@ bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::v
|
|||
return true;
|
||||
}
|
||||
|
||||
bool CCrypter::SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV)
|
||||
bool CCrypter::SetKey(const CKeyingMaterial& new_key, const std::span<const unsigned char> new_iv)
|
||||
{
|
||||
if (chNewKey.size() != WALLET_CRYPTO_KEY_SIZE || chNewIV.size() != WALLET_CRYPTO_IV_SIZE)
|
||||
if (new_key.size() != WALLET_CRYPTO_KEY_SIZE || new_iv.size() != WALLET_CRYPTO_IV_SIZE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(vchKey.data(), chNewKey.data(), chNewKey.size());
|
||||
memcpy(vchIV.data(), chNewIV.data(), chNewIV.size());
|
||||
memcpy(vchKey.data(), new_key.data(), new_key.size());
|
||||
memcpy(vchIV.data(), new_iv.data(), new_iv.size());
|
||||
|
||||
fKeySet = true;
|
||||
return true;
|
||||
|
@ -87,21 +91,20 @@ bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned
|
|||
return true;
|
||||
}
|
||||
|
||||
bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext) const
|
||||
bool CCrypter::Decrypt(const std::span<const unsigned char> ciphertext, CKeyingMaterial& plaintext) const
|
||||
{
|
||||
if (!fKeySet)
|
||||
return false;
|
||||
|
||||
// plaintext will always be equal to or lesser than length of ciphertext
|
||||
int nLen = vchCiphertext.size();
|
||||
|
||||
vchPlaintext.resize(nLen);
|
||||
plaintext.resize(ciphertext.size());
|
||||
|
||||
AES256CBCDecrypt dec(vchKey.data(), vchIV.data(), true);
|
||||
nLen = dec.Decrypt(vchCiphertext.data(), vchCiphertext.size(), vchPlaintext.data());
|
||||
if(nLen == 0)
|
||||
int len = dec.Decrypt(ciphertext.data(), ciphertext.size(), plaintext.data());
|
||||
if (len == 0) {
|
||||
return false;
|
||||
vchPlaintext.resize(nLen);
|
||||
}
|
||||
plaintext.resize(len);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -115,26 +118,29 @@ bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vch
|
|||
return cKeyCrypter.Encrypt(vchPlaintext, vchCiphertext);
|
||||
}
|
||||
|
||||
bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext)
|
||||
bool DecryptSecret(const CKeyingMaterial& master_key, const std::span<const unsigned char> ciphertext, const uint256& iv, CKeyingMaterial& plaintext)
|
||||
{
|
||||
CCrypter cKeyCrypter;
|
||||
std::vector<unsigned char> chIV(WALLET_CRYPTO_IV_SIZE);
|
||||
memcpy(chIV.data(), &nIV, WALLET_CRYPTO_IV_SIZE);
|
||||
if(!cKeyCrypter.SetKey(vMasterKey, chIV))
|
||||
CCrypter key_crypter;
|
||||
static_assert(WALLET_CRYPTO_IV_SIZE <= std::remove_reference_t<decltype(iv)>::size());
|
||||
const std::span iv_prefix{iv.data(), WALLET_CRYPTO_IV_SIZE};
|
||||
if (!key_crypter.SetKey(master_key, iv_prefix)) {
|
||||
return false;
|
||||
return cKeyCrypter.Decrypt(vchCiphertext, vchPlaintext);
|
||||
}
|
||||
return key_crypter.Decrypt(ciphertext, plaintext);
|
||||
}
|
||||
|
||||
bool DecryptKey(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key)
|
||||
bool DecryptKey(const CKeyingMaterial& master_key, const std::span<const unsigned char> crypted_secret, const CPubKey& pub_key, CKey& key)
|
||||
{
|
||||
CKeyingMaterial vchSecret;
|
||||
if(!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
|
||||
CKeyingMaterial secret;
|
||||
if (!DecryptSecret(master_key, crypted_secret, pub_key.GetHash(), secret)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (vchSecret.size() != 32)
|
||||
if (secret.size() != 32) {
|
||||
return false;
|
||||
}
|
||||
|
||||
key.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed());
|
||||
return key.VerifyPubKey(vchPubKey);
|
||||
key.Set(secret.begin(), secret.end(), pub_key.IsCompressed());
|
||||
return key.VerifyPubKey(pub_key);
|
||||
}
|
||||
} // namespace wallet
|
||||
|
|
|
@ -75,13 +75,13 @@ private:
|
|||
std::vector<unsigned char, secure_allocator<unsigned char>> vchIV;
|
||||
bool fKeySet;
|
||||
|
||||
int BytesToKeySHA512AES(const std::vector<unsigned char>& chSalt, const SecureString& strKeyData, int count, unsigned char *key,unsigned char *iv) const;
|
||||
int BytesToKeySHA512AES(std::span<const unsigned char> salt, const SecureString& key_data, int count, unsigned char* key, unsigned char* iv) const;
|
||||
|
||||
public:
|
||||
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod);
|
||||
bool SetKeyFromPassphrase(const SecureString& key_data, std::span<const unsigned char> salt, const unsigned int rounds, const unsigned int derivation_method);
|
||||
bool Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext) const;
|
||||
bool Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext) const;
|
||||
bool SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV);
|
||||
bool Decrypt(std::span<const unsigned char> ciphertext, CKeyingMaterial& plaintext) const;
|
||||
bool SetKey(const CKeyingMaterial& new_key, std::span<const unsigned char> new_iv);
|
||||
|
||||
void CleanKey()
|
||||
{
|
||||
|
@ -104,8 +104,8 @@ public:
|
|||
};
|
||||
|
||||
bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext);
|
||||
bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext);
|
||||
bool DecryptKey(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key);
|
||||
bool DecryptSecret(const CKeyingMaterial& master_key, std::span<const unsigned char> ciphertext, const uint256& iv, CKeyingMaterial& plaintext);
|
||||
bool DecryptKey(const CKeyingMaterial& master_key, std::span<const unsigned char> crypted_secret, const CPubKey& pub_key, CKey& key);
|
||||
} // namespace wallet
|
||||
|
||||
#endif // BITCOIN_WALLET_CRYPTER_H
|
||||
|
|
|
@ -35,11 +35,11 @@ FUZZ_TARGET(crypter, .init = initialize_crypter)
|
|||
|
||||
const unsigned int derivation_method = fuzzed_data_provider.ConsumeBool() ? 0 : fuzzed_data_provider.ConsumeIntegral<unsigned int>();
|
||||
|
||||
// Limiting the value of nRounds since it is otherwise uselessly expensive and causes a timeout when fuzzing.
|
||||
crypt.SetKeyFromPassphrase(/*strKeyData=*/secure_string,
|
||||
/*chSalt=*/ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_SALT_SIZE),
|
||||
/*nRounds=*/fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 25000),
|
||||
/*nDerivationMethod=*/derivation_method);
|
||||
// Limiting the value of rounds since it is otherwise uselessly expensive and causes a timeout when fuzzing.
|
||||
crypt.SetKeyFromPassphrase(/*key_data=*/secure_string,
|
||||
/*salt=*/ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_SALT_SIZE),
|
||||
/*rounds=*/fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 25000),
|
||||
/*derivation_method=*/derivation_method);
|
||||
}
|
||||
|
||||
LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 100)
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
using namespace util::hex_literals;
|
||||
|
||||
namespace wallet {
|
||||
BOOST_FIXTURE_TEST_SUITE(ismine_tests, BasicTestingSetup)
|
||||
|
@ -683,7 +684,7 @@ BOOST_AUTO_TEST_CASE(ismine_standard)
|
|||
BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
|
||||
|
||||
scriptPubKey.clear();
|
||||
scriptPubKey << OP_0 << ToByteVector(ParseHex("aabb"));
|
||||
scriptPubKey << OP_0 << "aabb"_hex_v_u8;
|
||||
|
||||
result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
|
||||
BOOST_CHECK_EQUAL(result, ISMINE_NO);
|
||||
|
@ -698,7 +699,7 @@ BOOST_AUTO_TEST_CASE(ismine_standard)
|
|||
BOOST_CHECK(keystore.GetLegacyScriptPubKeyMan()->AddKey(keys[0]));
|
||||
|
||||
scriptPubKey.clear();
|
||||
scriptPubKey << OP_16 << ToByteVector(ParseHex("aabb"));
|
||||
scriptPubKey << OP_16 << "aabb"_hex_v_u8;
|
||||
|
||||
result = keystore.GetLegacyScriptPubKeyMan()->IsMine(scriptPubKey);
|
||||
BOOST_CHECK_EQUAL(result, ISMINE_NO);
|
||||
|
|
|
@ -12,6 +12,8 @@
|
|||
#include <test/util/setup_common.h>
|
||||
#include <wallet/test/wallet_test_fixture.h>
|
||||
|
||||
using namespace util::hex_literals;
|
||||
|
||||
namespace wallet {
|
||||
BOOST_FIXTURE_TEST_SUITE(psbt_wallet_tests, WalletTestingSetup)
|
||||
|
||||
|
@ -35,14 +37,14 @@ BOOST_AUTO_TEST_CASE(psbt_updater_test)
|
|||
|
||||
// Create prevtxs and add to wallet
|
||||
DataStream s_prev_tx1{
|
||||
ParseHex("0200000000010158e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd7501000000171600145f275f436b09a8cc9a2eb2a2f528485c68a56323feffffff02d8231f1b0100000017a914aed962d6654f9a2b36608eb9d64d2b260db4f1118700c2eb0b0000000017a914b7f5faf40e3d40a5a459b1db3535f2b72fa921e88702483045022100a22edcc6e5bc511af4cc4ae0de0fcd75c7e04d8c1c3a8aa9d820ed4b967384ec02200642963597b9b1bc22c75e9f3e117284a962188bf5e8a74c895089046a20ad770121035509a48eb623e10aace8bfd0212fdb8a8e5af3c94b0b133b95e114cab89e4f7965000000"),
|
||||
"0200000000010158e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd7501000000171600145f275f436b09a8cc9a2eb2a2f528485c68a56323feffffff02d8231f1b0100000017a914aed962d6654f9a2b36608eb9d64d2b260db4f1118700c2eb0b0000000017a914b7f5faf40e3d40a5a459b1db3535f2b72fa921e88702483045022100a22edcc6e5bc511af4cc4ae0de0fcd75c7e04d8c1c3a8aa9d820ed4b967384ec02200642963597b9b1bc22c75e9f3e117284a962188bf5e8a74c895089046a20ad770121035509a48eb623e10aace8bfd0212fdb8a8e5af3c94b0b133b95e114cab89e4f7965000000"_hex,
|
||||
};
|
||||
CTransactionRef prev_tx1;
|
||||
s_prev_tx1 >> TX_WITH_WITNESS(prev_tx1);
|
||||
m_wallet.mapWallet.emplace(std::piecewise_construct, std::forward_as_tuple(prev_tx1->GetHash()), std::forward_as_tuple(prev_tx1, TxStateInactive{}));
|
||||
|
||||
DataStream s_prev_tx2{
|
||||
ParseHex("0200000001aad73931018bd25f84ae400b68848be09db706eac2ac18298babee71ab656f8b0000000048473044022058f6fc7c6a33e1b31548d481c826c015bd30135aad42cd67790dab66d2ad243b02204a1ced2604c6735b6393e5b41691dd78b00f0c5942fb9f751856faa938157dba01feffffff0280f0fa020000000017a9140fb9463421696b82c833af241c78c17ddbde493487d0f20a270100000017a91429ca74f8a08f81999428185c97b5d852e4063f618765000000"),
|
||||
"0200000001aad73931018bd25f84ae400b68848be09db706eac2ac18298babee71ab656f8b0000000048473044022058f6fc7c6a33e1b31548d481c826c015bd30135aad42cd67790dab66d2ad243b02204a1ced2604c6735b6393e5b41691dd78b00f0c5942fb9f751856faa938157dba01feffffff0280f0fa020000000017a9140fb9463421696b82c833af241c78c17ddbde493487d0f20a270100000017a91429ca74f8a08f81999428185c97b5d852e4063f618765000000"_hex,
|
||||
};
|
||||
CTransactionRef prev_tx2;
|
||||
s_prev_tx2 >> TX_WITH_WITNESS(prev_tx2);
|
||||
|
@ -56,7 +58,7 @@ BOOST_AUTO_TEST_CASE(psbt_updater_test)
|
|||
// Call FillPSBT
|
||||
PartiallySignedTransaction psbtx;
|
||||
DataStream ssData{
|
||||
ParseHex("70736274ff01009a020000000258e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd750000000000ffffffff838d0427d0ec650a68aa46bb0b098aea4422c071b2ca78352a077959d07cea1d0100000000ffffffff0270aaf00800000000160014d85c2b71d0060b09c9886aeb815e50991dda124d00e1f5050000000016001400aea9a2e5f0f876a588df5546e8742d1d87008f000000000000000000"),
|
||||
"70736274ff01009a020000000258e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd750000000000ffffffff838d0427d0ec650a68aa46bb0b098aea4422c071b2ca78352a077959d07cea1d0100000000ffffffff0270aaf00800000000160014d85c2b71d0060b09c9886aeb815e50991dda124d00e1f5050000000016001400aea9a2e5f0f876a588df5546e8742d1d87008f000000000000000000"_hex,
|
||||
};
|
||||
ssData >> psbtx;
|
||||
|
||||
|
|
|
@ -11,64 +11,71 @@
|
|||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
using namespace util::hex_literals;
|
||||
|
||||
namespace wallet {
|
||||
BOOST_FIXTURE_TEST_SUITE(wallet_crypto_tests, BasicTestingSetup)
|
||||
|
||||
class TestCrypter
|
||||
{
|
||||
public:
|
||||
static void TestPassphraseSingle(const std::vector<unsigned char>& vchSalt, const SecureString& passphrase, uint32_t rounds,
|
||||
const std::vector<unsigned char>& correctKey = std::vector<unsigned char>(),
|
||||
const std::vector<unsigned char>& correctIV=std::vector<unsigned char>())
|
||||
static void TestPassphraseSingle(const std::span<const unsigned char> salt, const SecureString& passphrase, uint32_t rounds,
|
||||
const std::span<const unsigned char> correct_key = {},
|
||||
const std::span<const unsigned char> correct_iv = {})
|
||||
{
|
||||
CCrypter crypt;
|
||||
crypt.SetKeyFromPassphrase(passphrase, vchSalt, rounds, 0);
|
||||
crypt.SetKeyFromPassphrase(passphrase, salt, rounds, 0);
|
||||
|
||||
if(!correctKey.empty())
|
||||
BOOST_CHECK_MESSAGE(memcmp(crypt.vchKey.data(), correctKey.data(), crypt.vchKey.size()) == 0, \
|
||||
HexStr(crypt.vchKey) + std::string(" != ") + HexStr(correctKey));
|
||||
if(!correctIV.empty())
|
||||
BOOST_CHECK_MESSAGE(memcmp(crypt.vchIV.data(), correctIV.data(), crypt.vchIV.size()) == 0,
|
||||
HexStr(crypt.vchIV) + std::string(" != ") + HexStr(correctIV));
|
||||
if (!correct_key.empty()) {
|
||||
BOOST_CHECK_MESSAGE(memcmp(crypt.vchKey.data(), correct_key.data(), crypt.vchKey.size()) == 0,
|
||||
HexStr(crypt.vchKey) + std::string(" != ") + HexStr(correct_key));
|
||||
}
|
||||
if (!correct_iv.empty()) {
|
||||
BOOST_CHECK_MESSAGE(memcmp(crypt.vchIV.data(), correct_iv.data(), crypt.vchIV.size()) == 0,
|
||||
HexStr(crypt.vchIV) + std::string(" != ") + HexStr(correct_iv));
|
||||
}
|
||||
}
|
||||
|
||||
static void TestPassphrase(const std::vector<unsigned char>& vchSalt, const SecureString& passphrase, uint32_t rounds,
|
||||
const std::vector<unsigned char>& correctKey = std::vector<unsigned char>(),
|
||||
const std::vector<unsigned char>& correctIV=std::vector<unsigned char>())
|
||||
static void TestPassphrase(const std::span<const unsigned char> salt, const SecureString& passphrase, uint32_t rounds,
|
||||
const std::span<const unsigned char> correct_key = {},
|
||||
const std::span<const unsigned char> correct_iv = {})
|
||||
{
|
||||
TestPassphraseSingle(vchSalt, passphrase, rounds, correctKey, correctIV);
|
||||
for(SecureString::const_iterator i(passphrase.begin()); i != passphrase.end(); ++i)
|
||||
TestPassphraseSingle(vchSalt, SecureString(i, passphrase.end()), rounds);
|
||||
TestPassphraseSingle(salt, passphrase, rounds, correct_key, correct_iv);
|
||||
for (SecureString::const_iterator it{passphrase.begin()}; it != passphrase.end(); ++it) {
|
||||
TestPassphraseSingle(salt, SecureString{it, passphrase.end()}, rounds);
|
||||
}
|
||||
}
|
||||
|
||||
static void TestDecrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchCiphertext, \
|
||||
const std::vector<unsigned char>& vchPlaintext = std::vector<unsigned char>())
|
||||
static void TestDecrypt(const CCrypter& crypt, const std::span<const unsigned char> ciphertext,
|
||||
const std::span<const unsigned char> correct_plaintext = {})
|
||||
{
|
||||
CKeyingMaterial vchDecrypted;
|
||||
crypt.Decrypt(vchCiphertext, vchDecrypted);
|
||||
if (vchPlaintext.size())
|
||||
BOOST_CHECK(CKeyingMaterial(vchPlaintext.begin(), vchPlaintext.end()) == vchDecrypted);
|
||||
CKeyingMaterial decrypted;
|
||||
crypt.Decrypt(ciphertext, decrypted);
|
||||
if (!correct_plaintext.empty()) {
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(decrypted.begin(), decrypted.end(), correct_plaintext.begin(), correct_plaintext.end());
|
||||
}
|
||||
}
|
||||
|
||||
static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& vchPlaintext,
|
||||
const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>())
|
||||
static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& plaintext,
|
||||
const std::span<const unsigned char> correct_ciphertext = {})
|
||||
{
|
||||
std::vector<unsigned char> vchCiphertext;
|
||||
crypt.Encrypt(vchPlaintext, vchCiphertext);
|
||||
std::vector<unsigned char> ciphertext;
|
||||
crypt.Encrypt(plaintext, ciphertext);
|
||||
|
||||
if (!vchCiphertextCorrect.empty())
|
||||
BOOST_CHECK(vchCiphertext == vchCiphertextCorrect);
|
||||
if (!correct_ciphertext.empty()) {
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(ciphertext.begin(), ciphertext.end(), correct_ciphertext.begin(), correct_ciphertext.end());
|
||||
}
|
||||
|
||||
const std::vector<unsigned char> vchPlaintext2(vchPlaintext.begin(), vchPlaintext.end());
|
||||
TestDecrypt(crypt, vchCiphertext, vchPlaintext2);
|
||||
TestDecrypt(crypt, ciphertext, /*correct_plaintext=*/plaintext);
|
||||
}
|
||||
|
||||
static void TestEncrypt(const CCrypter& crypt, const std::vector<unsigned char>& vchPlaintextIn, \
|
||||
const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>())
|
||||
static void TestEncrypt(const CCrypter& crypt, const std::span<const unsigned char> plaintext,
|
||||
const std::span<const unsigned char> correct_ciphertext = {})
|
||||
{
|
||||
TestEncryptSingle(crypt, CKeyingMaterial(vchPlaintextIn.begin(), vchPlaintextIn.end()), vchCiphertextCorrect);
|
||||
for(std::vector<unsigned char>::const_iterator i(vchPlaintextIn.begin()); i != vchPlaintextIn.end(); ++i)
|
||||
TestEncryptSingle(crypt, CKeyingMaterial(i, vchPlaintextIn.end()));
|
||||
TestEncryptSingle(crypt, CKeyingMaterial{plaintext.begin(), plaintext.end()}, correct_ciphertext);
|
||||
for (auto it{plaintext.begin()}; it != plaintext.end(); ++it) {
|
||||
TestEncryptSingle(crypt, CKeyingMaterial{it, plaintext.end()});
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -76,9 +83,9 @@ static void TestEncrypt(const CCrypter& crypt, const std::vector<unsigned char>&
|
|||
BOOST_AUTO_TEST_CASE(passphrase) {
|
||||
// These are expensive.
|
||||
|
||||
TestCrypter::TestPassphrase(ParseHex("0000deadbeef0000"), "test", 25000, \
|
||||
ParseHex("fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"), \
|
||||
ParseHex("cf2f2691526dd1aa220896fb8bf7c369"));
|
||||
TestCrypter::TestPassphrase("0000deadbeef0000"_hex_u8, "test", 25000,
|
||||
"fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"_hex_u8,
|
||||
"cf2f2691526dd1aa220896fb8bf7c369"_hex_u8);
|
||||
|
||||
std::string hash(GetRandHash().ToString());
|
||||
std::vector<unsigned char> vchSalt(8);
|
||||
|
@ -90,33 +97,31 @@ BOOST_AUTO_TEST_CASE(passphrase) {
|
|||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(encrypt) {
|
||||
std::vector<unsigned char> vchSalt = ParseHex("0000deadbeef0000");
|
||||
BOOST_CHECK(vchSalt.size() == WALLET_CRYPTO_SALT_SIZE);
|
||||
constexpr std::array<uint8_t, WALLET_CRYPTO_SALT_SIZE> salt{"0000deadbeef0000"_hex_u8};
|
||||
CCrypter crypt;
|
||||
crypt.SetKeyFromPassphrase("passphrase", vchSalt, 25000, 0);
|
||||
TestCrypter::TestEncrypt(crypt, ParseHex("22bcade09ac03ff6386914359cfe885cfeb5f77ff0d670f102f619687453b29d"));
|
||||
crypt.SetKeyFromPassphrase("passphrase", salt, 25000, 0);
|
||||
TestCrypter::TestEncrypt(crypt, "22bcade09ac03ff6386914359cfe885cfeb5f77ff0d670f102f619687453b29d"_hex_u8);
|
||||
|
||||
for (int i = 0; i != 100; i++)
|
||||
{
|
||||
uint256 hash(GetRandHash());
|
||||
TestCrypter::TestEncrypt(crypt, std::vector<unsigned char>(hash.begin(), hash.end()));
|
||||
TestCrypter::TestEncrypt(crypt, std::span<unsigned char>{hash.begin(), hash.end()});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(decrypt) {
|
||||
std::vector<unsigned char> vchSalt = ParseHex("0000deadbeef0000");
|
||||
BOOST_CHECK(vchSalt.size() == WALLET_CRYPTO_SALT_SIZE);
|
||||
constexpr std::array<uint8_t, WALLET_CRYPTO_SALT_SIZE> salt{"0000deadbeef0000"_hex_u8};
|
||||
CCrypter crypt;
|
||||
crypt.SetKeyFromPassphrase("passphrase", vchSalt, 25000, 0);
|
||||
crypt.SetKeyFromPassphrase("passphrase", salt, 25000, 0);
|
||||
|
||||
// Some corner cases the came up while testing
|
||||
TestCrypter::TestDecrypt(crypt,ParseHex("795643ce39d736088367822cdc50535ec6f103715e3e48f4f3b1a60a08ef59ca"));
|
||||
TestCrypter::TestDecrypt(crypt,ParseHex("de096f4a8f9bd97db012aa9d90d74de8cdea779c3ee8bc7633d8b5d6da703486"));
|
||||
TestCrypter::TestDecrypt(crypt,ParseHex("32d0a8974e3afd9c6c3ebf4d66aa4e6419f8c173de25947f98cf8b7ace49449c"));
|
||||
TestCrypter::TestDecrypt(crypt,ParseHex("e7c055cca2faa78cb9ac22c9357a90b4778ded9b2cc220a14cea49f931e596ea"));
|
||||
TestCrypter::TestDecrypt(crypt,ParseHex("b88efddd668a6801d19516d6830da4ae9811988ccbaf40df8fbb72f3f4d335fd"));
|
||||
TestCrypter::TestDecrypt(crypt,ParseHex("8cae76aa6a43694e961ebcb28c8ca8f8540b84153d72865e8561ddd93fa7bfa9"));
|
||||
TestCrypter::TestDecrypt(crypt,"795643ce39d736088367822cdc50535ec6f103715e3e48f4f3b1a60a08ef59ca"_hex_u8);
|
||||
TestCrypter::TestDecrypt(crypt,"de096f4a8f9bd97db012aa9d90d74de8cdea779c3ee8bc7633d8b5d6da703486"_hex_u8);
|
||||
TestCrypter::TestDecrypt(crypt,"32d0a8974e3afd9c6c3ebf4d66aa4e6419f8c173de25947f98cf8b7ace49449c"_hex_u8);
|
||||
TestCrypter::TestDecrypt(crypt,"e7c055cca2faa78cb9ac22c9357a90b4778ded9b2cc220a14cea49f931e596ea"_hex_u8);
|
||||
TestCrypter::TestDecrypt(crypt,"b88efddd668a6801d19516d6830da4ae9811988ccbaf40df8fbb72f3f4d335fd"_hex_u8);
|
||||
TestCrypter::TestDecrypt(crypt,"8cae76aa6a43694e961ebcb28c8ca8f8540b84153d72865e8561ddd93fa7bfa9"_hex_u8);
|
||||
|
||||
for (int i = 0; i != 100; i++)
|
||||
{
|
||||
|
|
Loading…
Add table
Reference in a new issue