Changeset View
Changeset View
Standalone View
Standalone View
src/test/cashaddrenc_tests.cpp
Show First 20 Lines • Show All 220 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(check_type) { | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* We ensure size is extracted and checked properly. | * We ensure size is extracted and checked properly. | ||||
*/ | */ | ||||
BOOST_AUTO_TEST_CASE(check_size) { | BOOST_AUTO_TEST_CASE(check_size) { | ||||
const CTxDestination nodst = CNoDestination{}; | const CTxDestination nodst = CNoDestination{}; | ||||
const std::string prefix = "ecash"; | const auto prefixes = {"bitcoincash", "ecash"}; | ||||
std::vector<uint8_t> data; | std::vector<uint8_t> data; | ||||
for (const auto &prefix : prefixes) { | |||||
for (auto ps : valid_sizes) { | for (auto ps : valid_sizes) { | ||||
// Number of bytes required for a 5-bit packed version of a hash, | // Number of bytes required for a 5-bit packed version of a hash, | ||||
// with version byte. Add half a byte(4) so integer math provides | // with version byte. Add half a byte(4) so integer math provides | ||||
// the next multiple-of-5 that would fit all the data. | // the next multiple-of-5 that would fit all the data. | ||||
size_t expectedSize = (8 * (1 + ps.second) + 4) / 5; | size_t expectedSize = (8 * (1 + ps.second) + 4) / 5; | ||||
data.resize(expectedSize); | data.resize(expectedSize); | ||||
std::fill(begin(data), end(data), 0); | std::fill(begin(data), end(data), 0); | ||||
// After conversion from 8 bit packing to 5 bit packing, the size | // After conversion from 8 bit packing to 5 bit packing, the size | ||||
// will be in the second 5-bit group, shifted left twice. | // will be in the second 5-bit group, shifted left twice. | ||||
data[1] = ps.first << 2; | data[1] = ps.first << 2; | ||||
auto content = | auto content = | ||||
DecodeCashAddrContent(cashaddr::Encode(prefix, data), prefix); | DecodeCashAddrContent(cashaddr::Encode(prefix, data), prefix); | ||||
BOOST_CHECK_EQUAL(content.type, 0); | BOOST_CHECK_EQUAL(content.type, 0); | ||||
BOOST_CHECK_EQUAL(content.hash.size(), ps.second); | BOOST_CHECK_EQUAL(content.hash.size(), ps.second); | ||||
data.push_back(0); | data.push_back(0); | ||||
content = DecodeCashAddrContent(cashaddr::Encode(prefix, data), prefix); | content = | ||||
DecodeCashAddrContent(cashaddr::Encode(prefix, data), prefix); | |||||
BOOST_CHECK_EQUAL(content.type, 0); | BOOST_CHECK_EQUAL(content.type, 0); | ||||
BOOST_CHECK_EQUAL(content.hash.size(), 0UL); | BOOST_CHECK_EQUAL(content.hash.size(), 0UL); | ||||
data.pop_back(); | data.pop_back(); | ||||
data.pop_back(); | data.pop_back(); | ||||
content = DecodeCashAddrContent(cashaddr::Encode(prefix, data), prefix); | content = | ||||
DecodeCashAddrContent(cashaddr::Encode(prefix, data), prefix); | |||||
BOOST_CHECK_EQUAL(content.type, 0); | BOOST_CHECK_EQUAL(content.type, 0); | ||||
BOOST_CHECK_EQUAL(content.hash.size(), 0UL); | BOOST_CHECK_EQUAL(content.hash.size(), 0UL); | ||||
} | } | ||||
} | } | ||||
} | |||||
BOOST_AUTO_TEST_CASE(test_encode_address) { | BOOST_AUTO_TEST_CASE(test_encode_address) { | ||||
const auto params = CreateChainParams(CBaseChainParams::MAIN); | const auto params = CreateChainParams(CBaseChainParams::MAIN); | ||||
std::vector<std::vector<uint8_t>> hash{ | std::vector<std::vector<uint8_t>> hash{ | ||||
{118, 160, 64, 83, 189, 160, 168, 139, 218, 81, | {118, 160, 64, 83, 189, 160, 168, 139, 218, 81, | ||||
119, 184, 106, 21, 195, 178, 159, 85, 152, 115}, | 119, 184, 106, 21, 195, 178, 159, 85, 152, 115}, | ||||
{203, 72, 18, 50, 41, 156, 213, 116, 49, 81, | {203, 72, 18, 50, 41, 156, 213, 116, 49, 81, | ||||
Show All 18 Lines | for (size_t i = 0; i < hash.size(); ++i) { | ||||
BOOST_CHECK_EQUAL(pubkey[i], EncodeCashAddr("ecash", keyContent)); | BOOST_CHECK_EQUAL(pubkey[i], EncodeCashAddr("ecash", keyContent)); | ||||
const CTxDestination dstScript = ScriptHash(uint160(hash[i])); | const CTxDestination dstScript = ScriptHash(uint160(hash[i])); | ||||
BOOST_CHECK_EQUAL(script[i], EncodeCashAddr(dstScript, *params)); | BOOST_CHECK_EQUAL(script[i], EncodeCashAddr(dstScript, *params)); | ||||
CashAddrContent scriptContent{SCRIPT_TYPE, hash[i]}; | CashAddrContent scriptContent{SCRIPT_TYPE, hash[i]}; | ||||
BOOST_CHECK_EQUAL(script[i], EncodeCashAddr("ecash", scriptContent)); | BOOST_CHECK_EQUAL(script[i], EncodeCashAddr("ecash", scriptContent)); | ||||
} | } | ||||
std::vector<std::string> backward_compat_pubkey = { | |||||
"bitcoincash:qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a", | |||||
"bitcoincash:qr95sy3j9xwd2ap32xkykttr4cvcu7as4y0qverfuy", | |||||
"bitcoincash:qqq3728yw0y47sqn6l2na30mcw6zm78dzqre909m2r"}; | |||||
std::vector<std::string> backward_compat_script = { | |||||
"bitcoincash:ppm2qsznhks23z7629mms6s4cwef74vcwvn0h829pq", | |||||
"bitcoincash:pr95sy3j9xwd2ap32xkykttr4cvcu7as4yc93ky28e", | |||||
"bitcoincash:pqq3728yw0y47sqn6l2na30mcw6zm78dzq5ucqzc37"}; | |||||
for (size_t i = 0; i < hash.size(); ++i) { | |||||
CashAddrContent keyContent{PUBKEY_TYPE, hash[i]}; | |||||
BOOST_CHECK_EQUAL(backward_compat_pubkey[i], | |||||
EncodeCashAddr("bitcoincash", keyContent)); | |||||
CashAddrContent scriptContent{SCRIPT_TYPE, hash[i]}; | |||||
BOOST_CHECK_EQUAL(backward_compat_script[i], | |||||
EncodeCashAddr("bitcoincash", scriptContent)); | |||||
} | |||||
} | } | ||||
struct CashAddrTestVector { | struct CashAddrTestVector { | ||||
std::string prefix; | std::string prefix; | ||||
CashAddrType type; | CashAddrType type; | ||||
std::vector<uint8_t> hash; | std::vector<uint8_t> hash; | ||||
std::string addr; | std::string addr; | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 150 Lines • Show Last 20 Lines |