19 #include <boost/test/unit_test.hpp>
30 UniValue tests =
read_json(std::string(json_tests::base58_encode_decode, json_tests::base58_encode_decode +
sizeof(json_tests::base58_encode_decode)));
31 for (
unsigned int idx = 0; idx < tests.
size(); idx++) {
33 std::string strTest = test.
write();
36 BOOST_ERROR(
"Bad test: " << strTest);
39 std::vector<unsigned char> sourcedata =
ParseHex(test[0].get_str());
40 std::string base58string = test[1].
get_str();
42 EncodeBase58(sourcedata.data(), sourcedata.data() + sourcedata.size()) == base58string,
50 UniValue tests =
read_json(std::string(json_tests::base58_encode_decode, json_tests::base58_encode_decode +
sizeof(json_tests::base58_encode_decode)));
51 std::vector<unsigned char> result;
53 for (
unsigned int idx = 0; idx < tests.
size(); idx++) {
55 std::string strTest = test.
write();
58 BOOST_ERROR(
"Bad test: " << strTest);
61 std::vector<unsigned char> expected =
ParseHex(test[0].get_str());
62 std::string base58string = test[1].
get_str();
63 BOOST_CHECK_MESSAGE(
DecodeBase58(base58string, result, 256), strTest);
64 BOOST_CHECK_MESSAGE(result.size() == expected.size() && std::equal(result.begin(), result.end(), expected.begin()), strTest);
79 std::vector<unsigned char> expected =
ParseHex(
"971a55");
80 BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
123 return exp_key == id;
128 return exp_key == id;
139 UniValue tests =
read_json(std::string(json_tests::base58_keys_valid, json_tests::base58_keys_valid +
sizeof(json_tests::base58_keys_valid)));
140 std::vector<unsigned char> result;
145 for (
unsigned int idx = 0; idx < tests.
size(); idx++) {
147 std::string strTest = test.
write();
150 BOOST_ERROR(
"Bad test: " << strTest);
153 std::string exp_base58string = test[0].
get_str();
154 std::vector<unsigned char> exp_payload =
ParseHex(test[1].get_str());
166 BOOST_CHECK_MESSAGE(privkey.
IsValid(),
"!IsValid:" + strTest);
167 BOOST_CHECK_MESSAGE(privkey.
IsCompressed() == isCompressed,
"compressed mismatch:" + strTest);
168 BOOST_CHECK_MESSAGE(privkey.
size() == exp_payload.size() && std::equal(privkey.
begin(), privkey.
end(), exp_payload.begin()),
"key mismatch:" + strTest);
172 BOOST_CHECK_MESSAGE(!
IsValidDestination(destination),
"IsValid privkey as pubkey:" + strTest);
178 BOOST_CHECK_MESSAGE((boost::get<CScriptID>(&destination) !=
nullptr) == (exp_addrType ==
"script"),
"isScript mismatch" + strTest);
179 BOOST_CHECK_MESSAGE(boost::apply_visitor(
TestAddrTypeVisitor(exp_addrType), destination),
"addrType mismatch" + strTest);
183 BOOST_CHECK_MESSAGE(!privkey.
IsValid(),
"IsValid pubkey as privkey:" + strTest);
191 UniValue tests =
read_json(std::string(json_tests::base58_keys_valid, json_tests::base58_keys_valid +
sizeof(json_tests::base58_keys_valid)));
192 std::vector<unsigned char> result;
194 for (
unsigned int idx = 0; idx < tests.
size(); idx++) {
196 std::string strTest = test.
write();
199 BOOST_ERROR(
"Bad test: " << strTest);
202 std::string exp_base58string = test[0].
get_str();
203 std::vector<unsigned char> exp_payload =
ParseHex(test[1].get_str());
215 key.
Set(exp_payload.begin(), exp_payload.end(), isCompressed);
217 BOOST_CHECK_MESSAGE(
KeyIO::EncodeSecret(key) == exp_base58string,
"result mismatch: " + strTest);
223 if(exp_addrType ==
"pubkey")
227 else if(exp_addrType ==
"script")
231 else if(exp_addrType ==
"none")
237 BOOST_ERROR(
"Bad addrtype: " << strTest);
241 BOOST_CHECK_MESSAGE(address == exp_base58string,
"mismatch: " + strTest);
251 UniValue tests =
read_json(std::string(json_tests::base58_keys_invalid, json_tests::base58_keys_invalid +
sizeof(json_tests::base58_keys_invalid)));
252 std::vector<unsigned char> result;
256 for (
unsigned int idx = 0; idx < tests.
size(); idx++) {
258 std::string strTest = test.
write();
261 BOOST_ERROR(
"Bad test: " << strTest);
264 std::string exp_base58string = test[0].
get_str();
270 BOOST_CHECK_MESSAGE(!privkey.
IsValid(),
"IsValid privkey:" + strTest);
276 for (
int n = 0; n < 1000; ++n) {
277 unsigned int len = 1 + InsecureRandBits(8);
278 unsigned int zeroes = InsecureRandBool() ? InsecureRandRange(len + 1) : 0;
281 std::vector<unsigned char> decoded;
284 auto ok =
DecodeBase58Check(encoded, decoded, len + InsecureRandRange(257 - len));
std::string EncodeBase58(const unsigned char *pbegin, const unsigned char *pend)
Why base-58 instead of standard base-64 encoding?
bool DecodeBase58Check(const char *psz, std::vector< unsigned char > &vchRet, int max_ret_len)
Decode a base58-encoded string (psz) that includes a checksum into a byte vector (vchRet),...
std::string EncodeBase58Check(const std::vector< unsigned char > &vchIn)
Encode a byte vector into a base58-encoded string, including checksum.
bool DecodeBase58(const char *psz, std::vector< unsigned char > &vch, int max_ret_len)
Decode a base58-encoded string (psz) into a byte vector (vchRet).
UniValue read_json(const std::string &jsondata)
BOOST_AUTO_TEST_CASE(base58_EncodeBase58)
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given chain name.
static const std::string TESTNET
static const std::string MAIN
Chain name strings.
A reference to a CKey: the Hash160 of its serialized public key, special case for exchange key.
An encapsulated private key.
const unsigned char * end() const
unsigned int size() const
Simple read-only vector-like interface.
bool IsValid() const
Check whether this private key is valid.
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.
const unsigned char * begin() const
A reference to a CKey: the Hash160 of its serialized public key.
A reference to a CScript: the Hash160 of its serialization (see script.h)
std::vector< unsigned char > randbytes(size_t len)
Generate random bytes.
bool operator()(const CScriptID &id) const
bool operator()(const CKeyID &id) const
bool operator()(const CExchangeKeyID &id) const
TestAddrTypeVisitor(const std::string &exp_addrType)
bool operator()(const CNoDestination &no) const
std::vector< unsigned char > exp_payload
TestPayloadVisitor(std::vector< unsigned char > &exp_payload)
bool operator()(const CKeyID &id) const
bool operator()(const CScriptID &id) const
bool operator()(const CNoDestination &no) const
const std::string & get_str() const
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
const UniValue & get_obj() const
BOOST_AUTO_TEST_SUITE_END()
std::string EncodeSecret(const CKey &key)
CKey DecodeSecret(const std::string &str)
bool IsValidDestination(const CWDestination &address)
std::string EncodeDestination(const CWDestination &address, const CChainParams::Base58Type addrType)
CWDestination DecodeDestination(const std::string &strAddress)
#define BOOST_FIXTURE_TEST_SUITE(a, b)
#define BOOST_CHECK(expr)
boost::variant< CNoDestination, CKeyID, CScriptID, CExchangeKeyID > CTxDestination
A txout script template with a specific destination.
FastRandomContext g_insecure_rand_ctx
const UniValue & find_value(const UniValue &obj, const std::string &name)
std::vector< unsigned char > ParseHex(const char *psz)
V Cat(V v1, V &&v2)
Concatenate two vectors, moving elements.