16 #include <boost/test/unit_test.hpp> 30 for (
unsigned int idx = 0; idx < tests.
size(); idx++) {
32 std::string strTest = test.
write();
33 if (test.
size() < 3) {
34 BOOST_ERROR(
"Bad test: " << strTest);
37 std::string exp_base58string = test[0].
get_str();
38 const std::vector<std::byte> exp_payload{ParseHex<std::byte>(test[1].
get_str())};
47 BOOST_CHECK_MESSAGE(privkey.
IsValid(),
"!IsValid:" + strTest);
48 BOOST_CHECK_MESSAGE(privkey.
IsCompressed() == isCompressed,
"compressed mismatch:" + strTest);
49 BOOST_CHECK_MESSAGE(
Span{privkey} ==
Span{exp_payload},
"key mismatch:" + strTest);
53 BOOST_CHECK_MESSAGE(!
IsValidDestination(destination),
"IsValid privkey as pubkey:" + strTest);
62 for (
char& c : exp_base58string) {
63 if (c >=
'a' && c <=
'z') {
65 }
else if (c >=
'A' && c <=
'Z') {
70 BOOST_CHECK_MESSAGE(
IsValidDestination(destination) == try_case_flip,
"!IsValid case flipped:" + strTest);
78 BOOST_CHECK_MESSAGE(!privkey.
IsValid(),
"IsValid pubkey as privkey:" + strTest);
88 for (
unsigned int idx = 0; idx < tests.
size(); idx++) {
90 std::string strTest = test.
write();
93 BOOST_ERROR(
"Bad test: " << strTest);
96 std::string exp_base58string = test[0].
get_str();
97 std::vector<unsigned char> exp_payload =
ParseHex(test[1].get_str());
104 key.
Set(exp_payload.begin(), exp_payload.end(), isCompressed);
106 BOOST_CHECK_MESSAGE(
EncodeSecret(key) == exp_base58string,
"result mismatch: " + strTest);
109 CScript exp_script(exp_payload.begin(), exp_payload.end());
128 for (
unsigned int idx = 0; idx < tests.
size(); idx++) {
130 std::string strTest = test.
write();
133 BOOST_ERROR(
"Bad test: " << strTest);
136 std::string exp_base58string = test[0].
get_str();
142 BOOST_CHECK_MESSAGE(!
IsValidDestination(destination),
"IsValid pubkey in mainnet:" + strTest);
144 BOOST_CHECK_MESSAGE(!privkey.
IsValid(),
"IsValid privkey in mainnet:" + strTest);
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
BOOST_AUTO_TEST_CASE(key_io_valid_parse)
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination corresponds to one with an address.
const std::string & get_str() const
const UniValue & find_value(std::string_view key) const
UniValue read_json(const std::string &jsondata)
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a scriptPubKey for the destination.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
BOOST_AUTO_TEST_SUITE_END()
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
#define BOOST_CHECK_EQUAL(v1, v2)
Serialized script, used inside transaction inputs and outputs.
const UniValue & get_obj() const
static const std::string key_io_invalid
std::optional< ChainType > ChainTypeFromString(std::string_view chain)
static const std::string key_io_valid
std::string EncodeDestination(const CTxDestination &dest)
An encapsulated private key.
A Span is an object that can refer to a contiguous sequence of objects.
CKey DecodeSecret(const std::string &str)
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg, std::vector< int > *error_locations)
std::string EncodeSecret(const CKey &key)
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.
bool IsValid() const
Check whether this private key is valid.
#define BOOST_CHECK(expr)