Changeset View
Changeset View
Standalone View
Standalone View
src/test/base58_tests.cpp
Show First 20 Lines • Show All 99 Lines • ▼ Show 20 Lines | for (unsigned int idx = 0; idx < tests.size(); idx++) { | ||||
BOOST_ERROR("Bad test: " << strTest); | BOOST_ERROR("Bad test: " << strTest); | ||||
continue; | continue; | ||||
} | } | ||||
std::string exp_base58string = test[0].get_str(); | std::string exp_base58string = test[0].get_str(); | ||||
std::vector<uint8_t> exp_payload = ParseHex(test[1].get_str()); | std::vector<uint8_t> exp_payload = ParseHex(test[1].get_str()); | ||||
const UniValue &metadata = test[2].get_obj(); | const UniValue &metadata = test[2].get_obj(); | ||||
bool isPrivkey = find_value(metadata, "isPrivkey").get_bool(); | bool isPrivkey = find_value(metadata, "isPrivkey").get_bool(); | ||||
SelectParams(find_value(metadata, "chain").get_str()); | SelectParams(find_value(metadata, "chain").get_str()); | ||||
bool try_case_flip = | |||||
find_value(metadata, "tryCaseFlip").isNull() | |||||
? false | |||||
: find_value(metadata, "tryCaseFlip").get_bool(); | |||||
if (isPrivkey) { | if (isPrivkey) { | ||||
bool isCompressed = find_value(metadata, "isCompressed").get_bool(); | bool isCompressed = find_value(metadata, "isCompressed").get_bool(); | ||||
// Must be valid private key | // Must be valid private key | ||||
privkey = DecodeSecret(exp_base58string); | privkey = DecodeSecret(exp_base58string); | ||||
BOOST_CHECK_MESSAGE(privkey.IsValid(), "!IsValid:" + strTest); | BOOST_CHECK_MESSAGE(privkey.IsValid(), "!IsValid:" + strTest); | ||||
BOOST_CHECK_MESSAGE(privkey.IsCompressed() == isCompressed, | BOOST_CHECK_MESSAGE(privkey.IsCompressed() == isCompressed, | ||||
"compressed mismatch:" + strTest); | "compressed mismatch:" + strTest); | ||||
BOOST_CHECK_MESSAGE(privkey.size() == exp_payload.size() && | BOOST_CHECK_MESSAGE(privkey.size() == exp_payload.size() && | ||||
std::equal(privkey.begin(), privkey.end(), | std::equal(privkey.begin(), privkey.end(), | ||||
exp_payload.begin()), | exp_payload.begin()), | ||||
"key mismatch:" + strTest); | "key mismatch:" + strTest); | ||||
// Private key must be invalid public key | // Private key must be invalid public key | ||||
destination = DecodeLegacyAddr(exp_base58string, Params()); | destination = DecodeLegacyAddr(exp_base58string, Params()); | ||||
BOOST_CHECK_MESSAGE(!IsValidDestination(destination), | BOOST_CHECK_MESSAGE(!IsValidDestination(destination), | ||||
"IsValid privkey as pubkey:" + strTest); | "IsValid privkey as pubkey:" + strTest); | ||||
} else { | } else { | ||||
// Must be valid public key | // Must be valid public key | ||||
destination = DecodeLegacyAddr(exp_base58string, Params()); | destination = DecodeLegacyAddr(exp_base58string, Params()); | ||||
CScript script = GetScriptForDestination(destination); | CScript script = GetScriptForDestination(destination); | ||||
BOOST_CHECK_MESSAGE(IsValidDestination(destination), | BOOST_CHECK_MESSAGE(IsValidDestination(destination), | ||||
"!IsValid:" + strTest); | "!IsValid:" + strTest); | ||||
BOOST_CHECK_EQUAL(HexStr(script), HexStr(exp_payload)); | BOOST_CHECK_EQUAL(HexStr(script), HexStr(exp_payload)); | ||||
// Try flipped case version | |||||
for (char &c : exp_base58string) { | |||||
if (c >= 'a' && c <= 'z') { | |||||
c = (c - 'a') + 'A'; | |||||
} else if (c >= 'A' && c <= 'Z') { | |||||
c = (c - 'A') + 'a'; | |||||
} | |||||
} | |||||
destination = DecodeDestination(exp_base58string, Params()); | |||||
BOOST_CHECK_MESSAGE(IsValidDestination(destination) == | |||||
try_case_flip, | |||||
"!IsValid case flipped:" + strTest); | |||||
if (IsValidDestination(destination)) { | |||||
script = GetScriptForDestination(destination); | |||||
BOOST_CHECK_EQUAL(HexStr(script), HexStr(exp_payload)); | |||||
} | |||||
// Public key must be invalid private key | // Public key must be invalid private key | ||||
privkey = DecodeSecret(exp_base58string); | privkey = DecodeSecret(exp_base58string); | ||||
BOOST_CHECK_MESSAGE(!privkey.IsValid(), | BOOST_CHECK_MESSAGE(!privkey.IsValid(), | ||||
"IsValid pubkey as privkey:" + strTest); | "IsValid pubkey as privkey:" + strTest); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 74 Lines • Show Last 20 Lines |