|
|
@ -10,14 +10,15 @@ |
|
|
|
|
|
|
|
|
|
|
|
#include "key.h" |
|
|
|
#include "key.h" |
|
|
|
#include "script/script.h" |
|
|
|
#include "script/script.h" |
|
|
|
|
|
|
|
#include "test/test_bitcoin.h" |
|
|
|
#include "uint256.h" |
|
|
|
#include "uint256.h" |
|
|
|
#include "util.h" |
|
|
|
#include "util.h" |
|
|
|
#include "utilstrencodings.h" |
|
|
|
#include "utilstrencodings.h" |
|
|
|
#include "test/test_bitcoin.h" |
|
|
|
|
|
|
|
|
|
|
|
#include <univalue.h> |
|
|
|
|
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp> |
|
|
|
#include <boost/test/unit_test.hpp> |
|
|
|
|
|
|
|
|
|
|
|
#include <univalue.h> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
extern UniValue read_json(const std::string& jsondata); |
|
|
|
extern UniValue read_json(const std::string& jsondata); |
|
|
|
|
|
|
|
|
|
|
@ -72,50 +73,6 @@ BOOST_AUTO_TEST_CASE(base58_DecodeBase58) |
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end()); |
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Visitor to check address type
|
|
|
|
|
|
|
|
class TestAddrTypeVisitor : public boost::static_visitor<bool> |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
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<bool> |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
private: |
|
|
|
|
|
|
|
std::vector<unsigned char> exp_payload; |
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
explicit TestPayloadVisitor(std::vector<unsigned char> &_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
|
|
|
|
// Goal: check that parsed keys match test payload
|
|
|
|
BOOST_AUTO_TEST_CASE(base58_keys_valid_parse) |
|
|
|
BOOST_AUTO_TEST_CASE(base58_keys_valid_parse) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -127,8 +84,7 @@ BOOST_AUTO_TEST_CASE(base58_keys_valid_parse) |
|
|
|
for (unsigned int idx = 0; idx < tests.size(); idx++) { |
|
|
|
for (unsigned int idx = 0; idx < tests.size(); idx++) { |
|
|
|
UniValue test = tests[idx]; |
|
|
|
UniValue test = tests[idx]; |
|
|
|
std::string strTest = test.write(); |
|
|
|
std::string strTest = test.write(); |
|
|
|
if (test.size() < 3) // Allow for extra stuff (useful for comments)
|
|
|
|
if (test.size() < 3) { // Allow for extra stuff (useful for comments)
|
|
|
|
{ |
|
|
|
|
|
|
|
BOOST_ERROR("Bad test: " << strTest); |
|
|
|
BOOST_ERROR("Bad test: " << strTest); |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
@ -136,13 +92,13 @@ BOOST_AUTO_TEST_CASE(base58_keys_valid_parse) |
|
|
|
std::vector<unsigned char> exp_payload = ParseHex(test[1].get_str()); |
|
|
|
std::vector<unsigned char> 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(); |
|
|
|
bool isTestnet = find_value(metadata, "isTestnet").get_bool(); |
|
|
|
bool isTestnet = find_value(metadata, "chain").get_str() == "testnet"; |
|
|
|
if (isTestnet) |
|
|
|
if (isTestnet) { |
|
|
|
SelectParams(CBaseChainParams::TESTNET); |
|
|
|
SelectParams(CBaseChainParams::TESTNET); |
|
|
|
else |
|
|
|
} else { |
|
|
|
SelectParams(CBaseChainParams::MAIN); |
|
|
|
SelectParams(CBaseChainParams::MAIN); |
|
|
|
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
|
|
|
|
BOOST_CHECK_MESSAGE(secret.SetString(exp_base58string), "!SetString:"+ strTest); |
|
|
|
BOOST_CHECK_MESSAGE(secret.SetString(exp_base58string), "!SetString:"+ strTest); |
|
|
@ -154,15 +110,12 @@ BOOST_AUTO_TEST_CASE(base58_keys_valid_parse) |
|
|
|
// Private key must be invalid public key
|
|
|
|
// Private key must be invalid public key
|
|
|
|
destination = DecodeDestination(exp_base58string); |
|
|
|
destination = DecodeDestination(exp_base58string); |
|
|
|
BOOST_CHECK_MESSAGE(!IsValidDestination(destination), "IsValid privkey as pubkey:" + strTest); |
|
|
|
BOOST_CHECK_MESSAGE(!IsValidDestination(destination), "IsValid privkey as pubkey:" + strTest); |
|
|
|
} |
|
|
|
} else { |
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
std::string exp_addrType = find_value(metadata, "addrType").get_str(); // "script" or "pubkey"
|
|
|
|
|
|
|
|
// Must be valid public key
|
|
|
|
// Must be valid public key
|
|
|
|
destination = DecodeDestination(exp_base58string); |
|
|
|
destination = DecodeDestination(exp_base58string); |
|
|
|
|
|
|
|
CScript script = GetScriptForDestination(destination); |
|
|
|
BOOST_CHECK_MESSAGE(IsValidDestination(destination), "!IsValid:" + strTest); |
|
|
|
BOOST_CHECK_MESSAGE(IsValidDestination(destination), "!IsValid:" + strTest); |
|
|
|
BOOST_CHECK_MESSAGE((boost::get<CScriptID>(&destination) != nullptr) == (exp_addrType == "script"), "isScript mismatch" + strTest); |
|
|
|
BOOST_CHECK_EQUAL(HexStr(script), HexStr(exp_payload)); |
|
|
|
BOOST_CHECK_MESSAGE(boost::apply_visitor(TestAddrTypeVisitor(exp_addrType), destination), "addrType mismatch" + strTest); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Public key must be invalid private key
|
|
|
|
// Public key must be invalid private key
|
|
|
|
secret.SetString(exp_base58string); |
|
|
|
secret.SetString(exp_base58string); |
|
|
@ -188,13 +141,13 @@ BOOST_AUTO_TEST_CASE(base58_keys_valid_gen) |
|
|
|
std::vector<unsigned char> exp_payload = ParseHex(test[1].get_str()); |
|
|
|
std::vector<unsigned char> 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(); |
|
|
|
bool isTestnet = find_value(metadata, "isTestnet").get_bool(); |
|
|
|
bool isTestnet = find_value(metadata, "chain").get_str() == "testnet"; |
|
|
|
if (isTestnet) |
|
|
|
if (isTestnet) { |
|
|
|
SelectParams(CBaseChainParams::TESTNET); |
|
|
|
SelectParams(CBaseChainParams::TESTNET); |
|
|
|
else |
|
|
|
} else { |
|
|
|
SelectParams(CBaseChainParams::MAIN); |
|
|
|
SelectParams(CBaseChainParams::MAIN); |
|
|
|
if(isPrivkey) |
|
|
|
} |
|
|
|
{ |
|
|
|
if (isPrivkey) { |
|
|
|
bool isCompressed = find_value(metadata, "isCompressed").get_bool(); |
|
|
|
bool isCompressed = find_value(metadata, "isCompressed").get_bool(); |
|
|
|
CKey key; |
|
|
|
CKey key; |
|
|
|
key.Set(exp_payload.begin(), exp_payload.end(), isCompressed); |
|
|
|
key.Set(exp_payload.begin(), exp_payload.end(), isCompressed); |
|
|
@ -202,30 +155,12 @@ BOOST_AUTO_TEST_CASE(base58_keys_valid_gen) |
|
|
|
CBitcoinSecret secret; |
|
|
|
CBitcoinSecret secret; |
|
|
|
secret.SetKey(key); |
|
|
|
secret.SetKey(key); |
|
|
|
BOOST_CHECK_MESSAGE(secret.ToString() == exp_base58string, "result mismatch: " + strTest); |
|
|
|
BOOST_CHECK_MESSAGE(secret.ToString() == exp_base58string, "result mismatch: " + strTest); |
|
|
|
} |
|
|
|
} else { |
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
std::string exp_addrType = find_value(metadata, "addrType").get_str(); |
|
|
|
|
|
|
|
CTxDestination dest; |
|
|
|
CTxDestination dest; |
|
|
|
if(exp_addrType == "pubkey") |
|
|
|
CScript exp_script(exp_payload.begin(), exp_payload.end()); |
|
|
|
{ |
|
|
|
ExtractDestination(exp_script, dest); |
|
|
|
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; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
std::string address = EncodeDestination(dest); |
|
|
|
std::string address = EncodeDestination(dest); |
|
|
|
BOOST_CHECK_MESSAGE(address == exp_base58string, "mismatch: " + strTest); |
|
|
|
BOOST_CHECK_EQUAL(address, exp_base58string); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|