diff --git a/src/cashaddrenc.h b/src/cashaddrenc.h --- a/src/cashaddrenc.h +++ b/src/cashaddrenc.h @@ -11,10 +11,12 @@ class CChainParams; +enum CashAddrType : uint8_t { PUBKEY_TYPE = 0, SCRIPT_TYPE = 1 }; + std::string EncodeCashAddr(const CTxDestination &, const CChainParams &); struct CashAddrContent { - uint8_t type; + CashAddrType type; std::vector hash; }; @@ -24,4 +26,5 @@ const CChainParams ¶ms); CTxDestination DecodeCashAddrDestination(const CashAddrContent &content); +std::vector PackCashAddrContent(const CashAddrContent &content); #endif diff --git a/src/cashaddrenc.cpp b/src/cashaddrenc.cpp --- a/src/cashaddrenc.cpp +++ b/src/cashaddrenc.cpp @@ -12,30 +12,53 @@ #include -const uint8_t PUBKEY_TYPE = 0; -const uint8_t SCRIPT_TYPE = 1; - -// Size of data-part in a pubkey/script cash address. -// Consists of: 8 bits version + 160 bits hash. -const size_t CASHADDR_GROUPED_SIZE = 34; /* 5 bit representation */ -const size_t CASHADDR_BYTES = 21; /* 8 bit representation */ - namespace { // Convert the data part to a 5 bit representation. template -std::vector PackAddrData(const T &id, uint8_t type, - size_t expectedSize) { - std::vector data = {uint8_t(type << 3)}; - data.insert(data.end(), id.begin(), id.end()); +std::vector PackAddrData(const T &id, uint8_t type) { + uint8_t version_byte(type << 3); + size_t size = id.size(); + uint8_t encoded_size = 0; + switch (size * 8) { + case 160: + encoded_size = 0; + break; + case 192: + encoded_size = 1; + break; + case 224: + encoded_size = 2; + break; + case 256: + encoded_size = 3; + break; + case 320: + encoded_size = 4; + break; + case 384: + encoded_size = 5; + break; + case 448: + encoded_size = 6; + break; + case 512: + encoded_size = 7; + break; + default: + throw std::runtime_error( + "Error packing cashaddr: invalid address length"); + } + version_byte |= encoded_size; + std::vector data = {version_byte}; + data.insert(data.end(), std::begin(id), std::end(id)); std::vector converted; - converted.reserve(expectedSize); - ConvertBits<8, 5, true>(converted, begin(data), end(data)); - - if (converted.size() != expectedSize) { - throw std::runtime_error("Error packing cashaddr"); - } + // Reserve the 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 + // the next multiple-of-5 that would fit all the data. + converted.reserve(((size + 1) * 8 + 4) / 5); + ConvertBits<8, 5, true>(converted, std::begin(data), std::end(data)); return converted; } @@ -46,14 +69,12 @@ CashAddrEncoder(const CChainParams &p) : params(p) {} std::string operator()(const CKeyID &id) const { - std::vector data = - PackAddrData(id, PUBKEY_TYPE, CASHADDR_GROUPED_SIZE); + std::vector data = PackAddrData(id, PUBKEY_TYPE); return cashaddr::Encode(params.CashAddrPrefix(), data); } std::string operator()(const CScriptID &id) const { - std::vector data = - PackAddrData(id, SCRIPT_TYPE, CASHADDR_GROUPED_SIZE); + std::vector data = PackAddrData(id, SCRIPT_TYPE); return cashaddr::Encode(params.CashAddrPrefix(), data); } @@ -108,7 +129,7 @@ } std::vector data; - data.reserve(CASHADDR_BYTES); + data.reserve(cashaddr.second.size() * 5 / 8); ConvertBits<5, 8, false>(data, begin(cashaddr.second), end(cashaddr.second)); @@ -119,7 +140,7 @@ return {}; } - uint8_t type = (version >> 3) & 0x1f; + auto type = CashAddrType((version >> 3) & 0x1f); uint32_t hash_size = 20 + 4 * (version & 0x03); if (version & 0x04) { hash_size *= 2; @@ -154,3 +175,9 @@ return CNoDestination{}; } } + +// PackCashAddrContent allows for testing PackAddrData in unittests due to +// template definitions. +std::vector PackCashAddrContent(const CashAddrContent &content) { + return PackAddrData(content.hash, content.type); +} diff --git a/src/test/cashaddrenc_tests.cpp b/src/test/cashaddrenc_tests.cpp --- a/src/test/cashaddrenc_tests.cpp +++ b/src/test/cashaddrenc_tests.cpp @@ -26,6 +26,17 @@ return n; } +std::vector insecure_GetRandomByteArray(FastRandomContext &rand, + size_t n) { + std::vector out; + out.reserve(n); + + for (size_t i = 0; i < n; i++) { + out.push_back(uint8_t(rand.randbits(8))); + } + return out; +} + class DstTypeChecker : public boost::static_visitor { public: void operator()(const CKeyID &id) { isKey = true; } @@ -50,10 +61,49 @@ bool isScript; }; +// Map all possible size bits in the version to the expected size of the +// hash in bytes. +const std::array, 8> valid_sizes = { + {{0, 20}, {1, 24}, {2, 28}, {3, 32}, {4, 40}, {5, 48}, {6, 56}, {7, 64}}}; + } // anon ns BOOST_FIXTURE_TEST_SUITE(cashaddrenc_tests, BasicTestingSetup) +BOOST_AUTO_TEST_CASE(encode_decode_all_sizes) { + FastRandomContext rand(true); + const CChainParams ¶ms = Params(CBaseChainParams::MAIN); + + for (auto ps : valid_sizes) { + std::vector data = + insecure_GetRandomByteArray(rand, ps.second); + CashAddrContent content = {PUBKEY_TYPE, data}; + std::vector packed_data = PackCashAddrContent(content); + + // Check that the packed size is correct + BOOST_CHECK_EQUAL(packed_data[1] >> 2, ps.first); + std::string address = + cashaddr::Encode(params.CashAddrPrefix(), packed_data); + + // Check that the address decodes properly + CashAddrContent decoded = DecodeCashAddrContent(address, params); + BOOST_CHECK_EQUAL_COLLECTIONS( + std::begin(content.hash), std::end(content.hash), + std::begin(decoded.hash), std::end(decoded.hash)); + } +} + +BOOST_AUTO_TEST_CASE(check_packaddr_throws) { + FastRandomContext rand(true); + + for (auto ps : valid_sizes) { + std::vector data = + insecure_GetRandomByteArray(rand, ps.second - 1); + CashAddrContent content = {PUBKEY_TYPE, data}; + BOOST_CHECK_THROW(PackCashAddrContent(content), std::runtime_error); + } +} + BOOST_AUTO_TEST_CASE(encode_decode) { std::vector toTest = {CNoDestination{}, CKeyID(uint160S("badf00d")), @@ -126,7 +176,7 @@ data.push_back(1); } - BOOST_CHECK_EQUAL(data.size(), 34); + BOOST_CHECK_EQUAL(data.size(), 34UL); const CTxDestination nodst = CNoDestination{}; const CChainParams params = Params(CBaseChainParams::MAIN); @@ -161,14 +211,14 @@ auto content = DecodeCashAddrContent( cashaddr::Encode(params.CashAddrPrefix(), data), params); BOOST_CHECK_EQUAL(content.type, v); - BOOST_CHECK_EQUAL(content.hash.size(), 20); + BOOST_CHECK_EQUAL(content.hash.size(), 20UL); // Check that using the reserved bit result in a failure. data[0] |= 0x10; content = DecodeCashAddrContent( cashaddr::Encode(params.CashAddrPrefix(), data), params); BOOST_CHECK_EQUAL(content.type, 0); - BOOST_CHECK_EQUAL(content.hash.size(), 0); + BOOST_CHECK_EQUAL(content.hash.size(), 0UL); } } @@ -179,15 +229,9 @@ const CTxDestination nodst = CNoDestination{}; const CChainParams params = Params(CBaseChainParams::MAIN); - // Mapp all possible size bits in the version to the expected size of the - // hash in bytes. - std::vector> sizes = { - {0, 20}, {1, 24}, {2, 28}, {3, 32}, {4, 40}, {5, 48}, {6, 56}, {7, 64}, - }; - std::vector data; - for (auto ps : sizes) { + for (auto ps : valid_sizes) { // 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 the next // multiple-of-5 that would fit all the data. @@ -209,7 +253,7 @@ cashaddr::Encode(params.CashAddrPrefix(), data), params); BOOST_CHECK_EQUAL(content.type, 0); - BOOST_CHECK_EQUAL(content.hash.size(), 0); + BOOST_CHECK_EQUAL(content.hash.size(), 0UL); data.pop_back(); data.pop_back(); @@ -217,7 +261,7 @@ cashaddr::Encode(params.CashAddrPrefix(), data), params); BOOST_CHECK_EQUAL(content.type, 0); - BOOST_CHECK_EQUAL(content.hash.size(), 0); + BOOST_CHECK_EQUAL(content.hash.size(), 0UL); } }