diff --git a/src/test/base58_tests.cpp b/src/test/base58_tests.cpp --- a/src/test/base58_tests.cpp +++ b/src/test/base58_tests.cpp @@ -16,10 +16,10 @@ #include #include -#include - #include +#include + BOOST_FIXTURE_TEST_SUITE(base58_tests, BasicTestingSetup) // Goal: test low-level base58 encoding functionality @@ -81,46 +81,6 @@ expected.begin(), expected.end()); } -// Visitor to check address type -class TestAddrTypeVisitor : public boost::static_visitor { -private: - std::string exp_addrType; - -public: - explicit TestAddrTypeVisitor(const std::string &_exp_addrType) - : exp_addrType(_exp_addrType) {} - bool operator()(const CKeyID &id) const { - return (exp_addrType == "pubkey"); - } - bool operator()(const CScriptID &id) const { - return (exp_addrType == "script"); - } - bool operator()(const CNoDestination &no) const { - return (exp_addrType == "none"); - } -}; - -// Visitor to check address payload -class TestPayloadVisitor : public boost::static_visitor { -private: - std::vector exp_payload; - -public: - explicit TestPayloadVisitor(std::vector &_exp_payload) - : exp_payload(_exp_payload) {} - bool operator()(const CKeyID &id) const { - uint160 exp_key(exp_payload); - return exp_key == id; - } - bool operator()(const CScriptID &id) const { - uint160 exp_key(exp_payload); - return exp_key == id; - } - bool operator()(const CNoDestination &no) const { - return exp_payload.size() == 0; - } -}; - // Goal: check that parsed keys match test payload BOOST_AUTO_TEST_CASE(base58_keys_valid_parse) { UniValue tests = read_json(std::string( @@ -142,11 +102,7 @@ std::vector exp_payload = ParseHex(test[1].get_str()); const UniValue &metadata = test[2].get_obj(); bool isPrivkey = find_value(metadata, "isPrivkey").get_bool(); - bool isTestnet = find_value(metadata, "isTestnet").get_bool(); - if (isTestnet) - SelectParams(CBaseChainParams::TESTNET); - else - SelectParams(CBaseChainParams::MAIN); + SelectParams(find_value(metadata, "chain").get_str()); if (isPrivkey) { bool isCompressed = find_value(metadata, "isCompressed").get_bool(); // Must be valid private key @@ -166,20 +122,12 @@ BOOST_CHECK_MESSAGE(!IsValidDestination(destination), "IsValid privkey as pubkey:" + strTest); } else { - // "script" or "pubkey" - std::string exp_addrType = - find_value(metadata, "addrType").get_str(); // Must be valid public key destination = DecodeLegacyAddr(exp_base58string, Params()); + CScript script = GetScriptForDestination(destination); BOOST_CHECK_MESSAGE(IsValidDestination(destination), "!IsValid:" + strTest); - BOOST_CHECK_MESSAGE((boost::get(&destination) != - nullptr) == (exp_addrType == "script"), - "isScript mismatch" + strTest); - BOOST_CHECK_MESSAGE( - boost::apply_visitor(TestAddrTypeVisitor(exp_addrType), - destination), - "addrType mismatch" + strTest); + BOOST_CHECK_EQUAL(HexStr(script), HexStr(exp_payload)); // Public key must be invalid private key secret.SetString(exp_base58string); @@ -207,11 +155,7 @@ std::vector exp_payload = ParseHex(test[1].get_str()); const UniValue &metadata = test[2].get_obj(); bool isPrivkey = find_value(metadata, "isPrivkey").get_bool(); - bool isTestnet = find_value(metadata, "isTestnet").get_bool(); - if (isTestnet) - SelectParams(CBaseChainParams::TESTNET); - else - SelectParams(CBaseChainParams::MAIN); + SelectParams(find_value(metadata, "chain").get_str()); if (isPrivkey) { bool isCompressed = find_value(metadata, "isCompressed").get_bool(); CKey key; @@ -222,22 +166,11 @@ BOOST_CHECK_MESSAGE(secret.ToString() == exp_base58string, "result mismatch: " + strTest); } else { - std::string exp_addrType = - find_value(metadata, "addrType").get_str(); CTxDestination dest; - if (exp_addrType == "pubkey") { - dest = CKeyID(uint160(exp_payload)); - } else if (exp_addrType == "script") { - dest = CScriptID(uint160(exp_payload)); - } else if (exp_addrType == "none") { - dest = CNoDestination(); - } else { - BOOST_ERROR("Bad addrtype: " << strTest); - continue; - } + CScript exp_script(exp_payload.begin(), exp_payload.end()); + ExtractDestination(exp_script, dest); std::string address = EncodeLegacyAddr(dest, Params()); - BOOST_CHECK_MESSAGE(address == exp_base58string, - "mismatch: " + strTest); + BOOST_CHECK_EQUAL(address, exp_base58string); } } @@ -266,11 +199,16 @@ std::string exp_base58string = test[0].get_str(); // must be invalid as public and as private key - destination = DecodeLegacyAddr(exp_base58string, Params()); - BOOST_CHECK_MESSAGE(!IsValidDestination(destination), - "IsValid pubkey:" + strTest); - secret.SetString(exp_base58string); - BOOST_CHECK_MESSAGE(!secret.IsValid(), "IsValid privkey:" + strTest); + for (auto chain : {CBaseChainParams::MAIN, CBaseChainParams::TESTNET, + CBaseChainParams::REGTEST}) { + SelectParams(chain); + destination = DecodeLegacyAddr(exp_base58string, Params()); + BOOST_CHECK_MESSAGE(!IsValidDestination(destination), + "IsValid pubkey:" + strTest); + secret.SetString(exp_base58string); + BOOST_CHECK_MESSAGE(!secret.IsValid(), + "IsValid privkey:" + strTest); + } } } diff --git a/src/test/data/base58_keys_valid.json b/src/test/data/base58_keys_valid.json --- a/src/test/data/base58_keys_valid.json +++ b/src/test/data/base58_keys_valid.json @@ -1,38 +1,42 @@ [ [ "1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i", - "65a16059864a2fdbc7c99a4723a8395bc6f188eb", + "76a91465a16059864a2fdbc7c99a4723a8395bc6f188eb88ac", { - "addrType": "pubkey", "isPrivkey": false, - "isTestnet": false + "chain": "main" } ], [ "3CMNFxN1oHBc4R1EpboAL5yzHGgE611Xou", - "74f209f6ea907e2ea48f74fae05782ae8a665257", + "a91474f209f6ea907e2ea48f74fae05782ae8a66525787", { - "addrType": "script", "isPrivkey": false, - "isTestnet": false + "chain": "main" } ], [ "mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs", - "53c0307d6851aa0ce7825ba883c6bd9ad242b486", + "76a91453c0307d6851aa0ce7825ba883c6bd9ad242b48688ac", { - "addrType": "pubkey", "isPrivkey": false, - "isTestnet": true + "chain": "test" + } + ], + [ + "mo9ncXisMeAoXwqcV5EWuyncbmCcQN4rVs", + "76a91453c0307d6851aa0ce7825ba883c6bd9ad242b48688ac", + { + "isPrivkey": false, + "chain": "regtest" } ], [ "2N2JD6wb56AfK4tfmM6PwdVmoYk2dCKf4Br", - "6349a418fc4578d10a372b54b45c280cc8c4382f", + "a9146349a418fc4578d10a372b54b45c280cc8c4382f87", { - "addrType": "script", "isPrivkey": false, - "isTestnet": true + "chain": "test" } ], [ @@ -41,7 +45,7 @@ { "isCompressed": false, "isPrivkey": true, - "isTestnet": false + "chain": "main" } ], [ @@ -50,7 +54,7 @@ { "isCompressed": true, "isPrivkey": true, - "isTestnet": false + "chain": "main" } ], [ @@ -59,7 +63,25 @@ { "isCompressed": false, "isPrivkey": true, - "isTestnet": true + "chain": "test" + } + ], + [ + "9213qJab2HNEpMpYNBa7wHGFKKbkDn24jpANDs2huN3yi4J11ko", + "36cb93b9ab1bdabf7fb9f2c04f1b9cc879933530ae7842398eef5a63a56800c2", + { + "isCompressed": false, + "isPrivkey": true, + "chain": "regtest" + } + ], + [ + "cTpB4YiyKiBcPxnefsDpbnDxFDffjqJob8wGCEDXxgQ7zQoMXJdH", + "b9f4892c9e8282028fea1d2667c4dc5213564d41fc5783896a0d843fc15089f3", + { + "isCompressed": true, + "isPrivkey": true, + "chain": "test" } ], [ @@ -68,43 +90,39 @@ { "isCompressed": true, "isPrivkey": true, - "isTestnet": true + "chain": "regtest" } ], [ "1Ax4gZtb7gAit2TivwejZHYtNNLT18PUXJ", - "6d23156cbbdcc82a5a47eee4c2c7c583c18b6bf4", + "76a9146d23156cbbdcc82a5a47eee4c2c7c583c18b6bf488ac", { - "addrType": "pubkey", "isPrivkey": false, - "isTestnet": false + "chain": "main" } ], [ "3QjYXhTkvuj8qPaXHTTWb5wjXhdsLAAWVy", - "fcc5460dd6e2487c7d75b1963625da0e8f4c5975", + "a914fcc5460dd6e2487c7d75b1963625da0e8f4c597587", { - "addrType": "script", "isPrivkey": false, - "isTestnet": false + "chain": "main" } ], [ "n3ZddxzLvAY9o7184TB4c6FJasAybsw4HZ", - "f1d470f9b02370fdec2e6b708b08ac431bf7a5f7", + "76a914f1d470f9b02370fdec2e6b708b08ac431bf7a5f788ac", { - "addrType": "pubkey", "isPrivkey": false, - "isTestnet": true + "chain": "test" } ], [ "2NBFNJTktNa7GZusGbDbGKRZTxdK9VVez3n", - "c579342c2c4c9220205e2cdc285617040c924a0a", + "a914c579342c2c4c9220205e2cdc285617040c924a0a87", { - "addrType": "script", "isPrivkey": false, - "isTestnet": true + "chain": "test" } ], [ @@ -113,7 +131,7 @@ { "isCompressed": false, "isPrivkey": true, - "isTestnet": false + "chain": "main" } ], [ @@ -122,7 +140,7 @@ { "isCompressed": true, "isPrivkey": true, - "isTestnet": false + "chain": "main" } ], [ @@ -131,7 +149,7 @@ { "isCompressed": false, "isPrivkey": true, - "isTestnet": true + "chain": "test" } ], [ @@ -140,43 +158,39 @@ { "isCompressed": true, "isPrivkey": true, - "isTestnet": true + "chain": "test" } ], [ "1C5bSj1iEGUgSTbziymG7Cn18ENQuT36vv", - "7987ccaa53d02c8873487ef919677cd3db7a6912", + "76a9147987ccaa53d02c8873487ef919677cd3db7a691288ac", { - "addrType": "pubkey", "isPrivkey": false, - "isTestnet": false + "chain": "main" } ], [ "3AnNxabYGoTxYiTEZwFEnerUoeFXK2Zoks", - "63bcc565f9e68ee0189dd5cc67f1b0e5f02f45cb", + "a91463bcc565f9e68ee0189dd5cc67f1b0e5f02f45cb87", { - "addrType": "script", "isPrivkey": false, - "isTestnet": false + "chain": "main" } ], [ "n3LnJXCqbPjghuVs8ph9CYsAe4Sh4j97wk", - "ef66444b5b17f14e8fae6e7e19b045a78c54fd79", + "76a914ef66444b5b17f14e8fae6e7e19b045a78c54fd7988ac", { - "addrType": "pubkey", "isPrivkey": false, - "isTestnet": true + "chain": "test" } ], [ "2NB72XtkjpnATMggui83aEtPawyyKvnbX2o", - "c3e55fceceaa4391ed2a9677f4a4d34eacd021a0", + "a914c3e55fceceaa4391ed2a9677f4a4d34eacd021a087", { - "addrType": "script", "isPrivkey": false, - "isTestnet": true + "chain": "test" } ], [ @@ -185,7 +199,7 @@ { "isCompressed": false, "isPrivkey": true, - "isTestnet": false + "chain": "main" } ], [ @@ -194,7 +208,7 @@ { "isCompressed": true, "isPrivkey": true, - "isTestnet": false + "chain": "main" } ], [ @@ -203,7 +217,7 @@ { "isCompressed": false, "isPrivkey": true, - "isTestnet": true + "chain": "test" } ], [ @@ -212,43 +226,39 @@ { "isCompressed": true, "isPrivkey": true, - "isTestnet": true + "chain": "test" } ], [ "1Gqk4Tv79P91Cc1STQtU3s1W6277M2CVWu", - "adc1cc2081a27206fae25792f28bbc55b831549d", + "76a914adc1cc2081a27206fae25792f28bbc55b831549d88ac", { - "addrType": "pubkey", "isPrivkey": false, - "isTestnet": false + "chain": "main" } ], [ "33vt8ViH5jsr115AGkW6cEmEz9MpvJSwDk", - "188f91a931947eddd7432d6e614387e32b244709", + "a914188f91a931947eddd7432d6e614387e32b24470987", { - "addrType": "script", "isPrivkey": false, - "isTestnet": false + "chain": "main" } ], [ "mhaMcBxNh5cqXm4aTQ6EcVbKtfL6LGyK2H", - "1694f5bc1a7295b600f40018a618a6ea48eeb498", + "76a9141694f5bc1a7295b600f40018a618a6ea48eeb49888ac", { - "addrType": "pubkey", "isPrivkey": false, - "isTestnet": true + "chain": "test" } ], [ "2MxgPqX1iThW3oZVk9KoFcE5M4JpiETssVN", - "3b9b3fd7a50d4f08d1a5b0f62f644fa7115ae2f3", + "a9143b9b3fd7a50d4f08d1a5b0f62f644fa7115ae2f387", { - "addrType": "script", "isPrivkey": false, - "isTestnet": true + "chain": "test" } ], [ @@ -257,7 +267,7 @@ { "isCompressed": false, "isPrivkey": true, - "isTestnet": false + "chain": "main" } ], [ @@ -266,7 +276,16 @@ { "isCompressed": true, "isPrivkey": true, - "isTestnet": false + "chain": "main" + } + ], + [ + "92xFEve1Z9N8Z641KQQS7ByCSb8kGjsDzw6fAmjHN1LZGKQXyMq", + "b4204389cef18bbe2b353623cbf93e8678fbc92a475b664ae98ed594e6cf0856", + { + "isCompressed": false, + "isPrivkey": true, + "chain": "test" } ], [ @@ -275,7 +294,7 @@ { "isCompressed": false, "isPrivkey": true, - "isTestnet": true + "chain": "regtest" } ], [ @@ -284,43 +303,39 @@ { "isCompressed": true, "isPrivkey": true, - "isTestnet": true + "chain": "test" } ], [ "1JwMWBVLtiqtscbaRHai4pqHokhFCbtoB4", - "c4c1b72491ede1eedaca00618407ee0b772cad0d", + "76a914c4c1b72491ede1eedaca00618407ee0b772cad0d88ac", { - "addrType": "pubkey", "isPrivkey": false, - "isTestnet": false + "chain": "main" } ], [ "3QCzvfL4ZRvmJFiWWBVwxfdaNBT8EtxB5y", - "f6fe69bcb548a829cce4c57bf6fff8af3a5981f9", + "a914f6fe69bcb548a829cce4c57bf6fff8af3a5981f987", { - "addrType": "script", "isPrivkey": false, - "isTestnet": false + "chain": "main" } ], [ "mizXiucXRCsEriQCHUkCqef9ph9qtPbZZ6", - "261f83568a098a8638844bd7aeca039d5f2352c0", + "76a914261f83568a098a8638844bd7aeca039d5f2352c088ac", { - "addrType": "pubkey", "isPrivkey": false, - "isTestnet": true + "chain": "test" } ], [ "2NEWDzHWwY5ZZp8CQWbB7ouNMLqCia6YRda", - "e930e1834a4d234702773951d627cce82fbb5d2e", + "a914e930e1834a4d234702773951d627cce82fbb5d2e87", { - "addrType": "script", "isPrivkey": false, - "isTestnet": true + "chain": "test" } ], [ @@ -329,7 +344,7 @@ { "isCompressed": false, "isPrivkey": true, - "isTestnet": false + "chain": "main" } ], [ @@ -338,7 +353,7 @@ { "isCompressed": true, "isPrivkey": true, - "isTestnet": false + "chain": "main" } ], [ @@ -347,7 +362,7 @@ { "isCompressed": false, "isPrivkey": true, - "isTestnet": true + "chain": "test" } ], [ @@ -356,43 +371,39 @@ { "isCompressed": true, "isPrivkey": true, - "isTestnet": true + "chain": "test" } ], [ "19dcawoKcZdQz365WpXWMhX6QCUpR9SY4r", - "5eadaf9bb7121f0f192561a5a62f5e5f54210292", + "76a9145eadaf9bb7121f0f192561a5a62f5e5f5421029288ac", { - "addrType": "pubkey", "isPrivkey": false, - "isTestnet": false + "chain": "main" } ], [ "37Sp6Rv3y4kVd1nQ1JV5pfqXccHNyZm1x3", - "3f210e7277c899c3a155cc1c90f4106cbddeec6e", + "a9143f210e7277c899c3a155cc1c90f4106cbddeec6e87", { - "addrType": "script", "isPrivkey": false, - "isTestnet": false + "chain": "main" } ], [ "myoqcgYiehufrsnnkqdqbp69dddVDMopJu", - "c8a3c2a09a298592c3e180f02487cd91ba3400b5", + "76a914c8a3c2a09a298592c3e180f02487cd91ba3400b588ac", { - "addrType": "pubkey", "isPrivkey": false, - "isTestnet": true + "chain": "test" } ], [ "2N7FuwuUuoTBrDFdrAZ9KxBmtqMLxce9i1C", - "99b31df7c9068d1481b596578ddbb4d3bd90baeb", + "a91499b31df7c9068d1481b596578ddbb4d3bd90baeb87", { - "addrType": "script", "isPrivkey": false, - "isTestnet": true + "chain": "test" } ], [ @@ -401,7 +412,7 @@ { "isCompressed": false, "isPrivkey": true, - "isTestnet": false + "chain": "main" } ], [ @@ -410,7 +421,7 @@ { "isCompressed": true, "isPrivkey": true, - "isTestnet": false + "chain": "main" } ], [ @@ -419,7 +430,7 @@ { "isCompressed": false, "isPrivkey": true, - "isTestnet": true + "chain": "test" } ], [ @@ -428,25 +439,23 @@ { "isCompressed": true, "isPrivkey": true, - "isTestnet": true + "chain": "test" } ], [ "13p1ijLwsnrcuyqcTvJXkq2ASdXqcnEBLE", - "1ed467017f043e91ed4c44b4e8dd674db211c4e6", + "76a9141ed467017f043e91ed4c44b4e8dd674db211c4e688ac", { - "addrType": "pubkey", "isPrivkey": false, - "isTestnet": false + "chain": "main" } ], [ "3ALJH9Y951VCGcVZYAdpA3KchoP9McEj1G", - "5ece0cadddc415b1980f001785947120acdb36fc", + "a9145ece0cadddc415b1980f001785947120acdb36fc87", { - "addrType": "script", "isPrivkey": false, - "isTestnet": false + "chain": "main" } ] ]