Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct hash prefixes used in tests and use consistent pubkey, add tests to reject bitcoin addresses #57

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
3c0c6c4
correct python3 comparison operator
slowriot Aug 19, 2021
e027dc7
enable building tests by default, external signer default to match bi…
slowriot Aug 20, 2021
0f69252
reinstate valid base58 test data and test methodology from bitcoin core
slowriot Aug 20, 2021
60bc8a1
fix base58 implementation
slowriot Aug 20, 2021
a0305ee
do not enable DEBUG_LOCKORDER for all debug builds; the code associat…
slowriot Aug 24, 2021
9781fd3
don't disable implicit-fallthrough warning (this is now dealt with in…
slowriot Aug 24, 2021
2f0b88f
enable building tests by default
slowriot Aug 25, 2021
4bf285e
re-enable asserts for signet chain params at creation, with correct g…
slowriot Aug 25, 2021
9225ec0
remove unused include
slowriot Aug 25, 2021
95a1703
add tests to verify test addresses are valid - this also tests the va…
slowriot Aug 25, 2021
5555e69
fix failing key_tests, set correct prefix (10) for addresses - the pr…
slowriot Aug 25, 2021
4d72a07
add a test to ensure a BTC address does not count as a valid destination
slowriot Aug 25, 2021
43306dc
Merge branch 'base58_fixes' into genesis_hashes
slowriot Aug 25, 2021
f4a744d
update benchmark decode address to BGL prefix for consistency
slowriot Aug 25, 2021
2f87ecb
update test data sample BTC address to BGL format with prefix 10
slowriot Aug 25, 2021
28ed84b
test data - update BTC format addresses to their BGL equivalent with …
slowriot Aug 25, 2021
4024b28
some tests use the pubkey of the BTC genesis block - update these to …
slowriot Aug 25, 2021
265f53a
update values used in util_tests fixes to match janus branch, to reso…
slowriot Sep 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ AC_ARG_ENABLE([natpmp-default],
AC_ARG_ENABLE(tests,
AS_HELP_STRING([--disable-tests],[do not compile tests (default is to compile)]),
[use_tests=$enableval],
[use_tests=no])
[use_tests=yes])

AC_ARG_ENABLE(gui-tests,
AS_HELP_STRING([--disable-gui-tests],[do not compile GUI tests (default is to compile if GUI and tests enabled)]),
Expand Down Expand Up @@ -343,7 +343,7 @@ AC_ARG_ENABLE([werror],
AC_ARG_ENABLE([external-signer],
[AS_HELP_STRING([--enable-external-signer],[compile external signer support (default is no, requires Boost::Process)])],
[use_external_signer=$enableval],
[use_external_signer=no])
[use_external_signer=yes])

AC_LANG_PUSH([C++])

Expand Down Expand Up @@ -387,7 +387,7 @@ if test "x$enable_debug" = xyes; then
[[$CXXFLAG_WERROR]])

AX_CHECK_PREPROC_FLAG([-DDEBUG],[[DEBUG_CPPFLAGS="$DEBUG_CPPFLAGS -DDEBUG"]],,[[$CXXFLAG_WERROR]])
AX_CHECK_PREPROC_FLAG([-DDEBUG_LOCKORDER],[[DEBUG_CPPFLAGS="$DEBUG_CPPFLAGS -DDEBUG_LOCKORDER"]],,[[$CXXFLAG_WERROR]])
dnl AX_CHECK_PREPROC_FLAG([-DDEBUG_LOCKORDER],[[DEBUG_CPPFLAGS="$DEBUG_CPPFLAGS -DDEBUG_LOCKORDER"]],,[[$CXXFLAG_WERROR]])
AX_CHECK_COMPILE_FLAG([-ftrapv],[DEBUG_CXXFLAGS="$DEBUG_CXXFLAGS -ftrapv"],,[[$CXXFLAG_WERROR]])
fi

Expand Down
2 changes: 1 addition & 1 deletion contrib/testgen/base58.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def get_bcaddress_version(strAddress):

if __name__ == '__main__':
# Test case (from http://gitorious.org/BGL/python-base58.git)
assert get_bcaddress_version('15VjRaDX9zpbA8LVnbrCAFzrVzN7ixHNsC') is 0
assert get_bcaddress_version('15VjRaDX9zpbA8LVnbrCAFzrVzN7ixHNsC') == 0
_ohai = 'o hai'.encode('ascii')
_tmp = b58encode(_ohai)
assert _tmp == 'DYB3oMS'
Expand Down
4 changes: 2 additions & 2 deletions src/base58.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static const int8_t mapBase58[256] = {
std::vector<unsigned char>::iterator it = b256.begin() + (size - length);
// Copy result into output vector.
vch.reserve(zeroes + (b256.end() - it));
vch.assign(zeroes, 0x0a);
vch.assign(zeroes, 0x00);
while (it != b256.end())
vch.push_back(*(it++));
return true;
Expand All @@ -91,7 +91,7 @@ std::string EncodeBase58(Span<const unsigned char> input)
// Skip & count leading zeroes.
int zeroes = 0;
int length = 0;
while (input.size() > 0 && input[0] == 10) {
while (input.size() > 0 && input[0] == 0) {
input = input.subspan(1);
zeroes++;
}
Expand Down
2 changes: 1 addition & 1 deletion src/bench/base58.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static void Base58CheckEncode(benchmark::Bench& bench)

static void Base58Decode(benchmark::Bench& bench)
{
const char* addr = "17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhem";
const char* addr = "58sbDbzKTt1eVuXLeTJ8FqyWYed9wmP2pD";
std::vector<unsigned char> vch;
bench.batch(strlen(addr)).unit("byte").run([&] {
(void) DecodeBase58(addr, vch, 64);
Expand Down
5 changes: 3 additions & 2 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,9 @@ class SigNetParams : public CChainParams {

genesis = CreateGenesisBlock(1598918400, 52613770, 0x1e0377ae, 1, 50 * COIN);
consensus.hashGenesisBlock = genesis.GetHash();
// assert(consensus.hashGenesisBlock == uint256S("0x00000008819873e925422c1ff0f99f7cc9bbb232af63a077a480a3633bee1ef6"));
// assert(genesis.hashMerkleRoot == uint256S("0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"));

assert(consensus.hashGenesisBlock == uint256S("031d49b33598a1cb9559ae12feba7279cd944cd44b1862497aa38453751771fc"));
assert(genesis.hashMerkleRoot == uint256S("bb46c1c31b5660dd5fcd12aa9730dba0912952e4db9112331965fcdb32be74f5"));

vFixedSeeds.clear();

Expand Down
4 changes: 2 additions & 2 deletions src/test/base58_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

using namespace std::literals;

extern UniValue read_json(const std::string& jsondata);
UniValue read_json(const std::string& jsondata);

BOOST_FIXTURE_TEST_SUITE(base58_tests, BasicTestingSetup)

Expand Down Expand Up @@ -88,7 +88,7 @@ BOOST_AUTO_TEST_CASE(base58_random_encode_decode)
for (int n = 0; n < 1000; ++n) {
unsigned int len = 1 + InsecureRandBits(8);
unsigned int zeroes = InsecureRandBool() ? InsecureRandRange(len + 1) : 0;
auto data = Cat(std::vector<unsigned char>(zeroes, '\010'), g_insecure_rand_ctx.randbytes(len - zeroes));
auto data = Cat(std::vector<unsigned char>(zeroes, '\000'), g_insecure_rand_ctx.randbytes(len - zeroes));
auto encoded = EncodeBase58Check(data);
std::vector<unsigned char> decoded;
auto ok_too_small = DecodeBase58Check(encoded, decoded, InsecureRandRange(len));
Expand Down
8 changes: 4 additions & 4 deletions src/test/data/base58_encode_decode.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
["626262", "a3gV"],
["636363", "aPEr"],
["73696d706c792061206c6f6e6720737472696e67", "2cFupjhnEsSn59qHXstmK2ffpLv2"],
["0aeb15231dfceb60925886b67d065299925915aeb172c06647", "1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L"],
["0aeb15231dfceb60925886b67d065299925915aeb172c06647", "5Pp2xoZZFXwSdcbsjj57BSxerwyUxe3ZJn"],
["516b6fcd0f", "ABnLTmg"],
["bf4f89001e670274dd", "3SEo3LWLoPntC"],
["572e4794", "3EFU7m"],
["ecac89cad93923c02321", "EJDM8drfXA6uyA"],
["10c8511e", "Rt5zm"],
["0a0a0a0a0a0a0a0a0a0a", "1111111111"],
["0a0111d38e5fc9071ffcd20b4a763cc9ae4f252bb4e48fd66a835e252ada93ff480d6dd43dc62a641155a5", "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"],
["0a0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", "1cWB5HCBdLjAuqGGReWE3R3CguuwSjw6RHn39s2yuDRTS5NsBgNiFpWgAnEx6VQi8csexkgYw3mdYrMHr8x9i7aEwP8kZ7vccXWqKDvGv3u1GxFKPuAkn8JCPPGDMf3vMMnbzm6Nh9zh1gcNsMvH3ZNLmP5fSG6DGbbi2tuwMWPthr4boWwCxf7ewSgNQeacyozhKDDQQ1qL5fQFUW52QKUZDZ5fw3KXNQJMcNTcaB723LchjeKun7MuGW5qyCBZYzA1KjofN1gYBV3NqyhQJ3Ns746GNuf9N2pQPmHz4xpnSrrfCvy6TVVz5d4PdrjeshsWQwpZsZGzvbdAdN8MKV5QsBDY"]
["00000000000000000000", "1111111111"],
["000111d38e5fc9071ffcd20b4a763cc9ae4f252bb4e48fd66a835e252ada93ff480d6dd43dc62a641155a5", "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"],
["000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", "1cWB5HCBdLjAuqGGReWE3R3CguuwSjw6RHn39s2yuDRTS5NsBgNiFpWgAnEx6VQi8csexkgYw3mdYrMHr8x9i7aEwP8kZ7vccXWqKDvGv3u1GxFKPuAkn8JCPPGDMf3vMMnbzm6Nh9zh1gcNsMvH3ZNLmP5fSG6DGbbi2tuwMWPthr4boWwCxf7ewSgNQeacyozhKDDQQ1qL5fQFUW52QKUZDZ5fw3KXNQJMcNTcaB723LchjeKun7MuGW5qyCBZYzA1KjofN1gYBV3NqyhQJ3Ns746GNuf9N2pQPmHz4xpnSrrfCvy6TVVz5d4PdrjeshsWQwpZsZGzvbdAdN8MKV5QsBDY"]
]
20 changes: 15 additions & 5 deletions src/test/key_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <test/util/setup_common.h>
#include <uint256.h>
#include <util/strencodings.h>
#include <util/string.h>
#include <util/system.h>

#include <string>
Expand All @@ -21,13 +20,15 @@ static const std::string strSecret1 = "5HxWvvfubhXpYYpS3tJkw6fq9jE9j18THftkZjHHf
static const std::string strSecret2 = "5KC4ejrDjv152FGwP386VD1i2NYc5KkfSMyv1nGy1VGDxGHqVY3";
static const std::string strSecret1C = "Kwr371tjA9u2rFSMZjTNun2PXXP3WPZu2afRHTcta6KxEUdm1vEw";
static const std::string strSecret2C = "L3Hq7a8FEQwJkW1M2GNKDW28546Vp5miewcCzSqUD9kCAXrJdS3g";
static const std::string addr1 = "1QFqqMUD55ZV3PJEJZtaKCsQmjLT8SEhCG";
static const std::string addr2 = "1F5y5E5FMc5YzdJtB9hLaUe43GDxBSJz9r";
static const std::string addr1C = "1NoJrossxPBKfCHuJXT4HadJrXREB1q6GY";
static const std::string addr2C = "1CRj2HyM1CXWzHAXLQtiGLyggNT9VTvr9f";
static const std::string addr1 = "5RdsgST6AtCFDih6YmDmATbH4mutEn52Mi";
static const std::string addr2 = "5GTzvK48TQiKAxhkRM2XRjMvLJoPHn9KKJ";
static const std::string addr1C = "5QBLhtrm4Bp5qXgmYinF8qMB9ZzfHMfRRz";
static const std::string addr2C = "5DoksNxE71AHAcZPacDu7bhYyR2abomBK7";

static const std::string strAddressBad = "1HV9Lc3sNHZxwj4Zk6fB38tEmBryq2cBiF";

static const std::string addr_bitcoin = "1QFqqMUD55ZV3PJEJZtaKCsQmjLT6JkjvJ"; // valid BTC address, not valid BGL address


BOOST_FIXTURE_TEST_SUITE(key_tests, BasicTestingSetup)

Expand Down Expand Up @@ -69,6 +70,15 @@ BOOST_AUTO_TEST_CASE(key_test1)
BOOST_CHECK(!key2C.VerifyPubKey(pubkey2));
BOOST_CHECK(key2C.VerifyPubKey(pubkey2C));

// check the addresses are valid - if they aren't, it can give misleading failures later
BOOST_CHECK(IsValidDestination(DecodeDestination(addr1)));
BOOST_CHECK(IsValidDestination(DecodeDestination(addr2)));
BOOST_CHECK(IsValidDestination(DecodeDestination(addr1C)));
BOOST_CHECK(IsValidDestination(DecodeDestination(addr2C)));

// bitcoin addresses (prefix 0) should NOT be valid
BOOST_CHECK(!IsValidDestination(DecodeDestination(addr_bitcoin)));

CTxDestination dest1 = DecodeDestination(addr1);
CTxDestination dest2 = CTxDestination(PKHash(pubkey1));
BOOST_CHECK(dest1 == dest2);
Expand Down
2 changes: 1 addition & 1 deletion src/test/miner_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
// Note that by default, these tests run with size accounting enabled.
const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
const CChainParams& chainparams = *chainParams;
CScript scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
CScript scriptPubKey = CScript() << ParseHex("04489d8efd89b673459f3ebbe435956c90255d31408dec347e01649c067267a16347c653e7b721d2aacd8290d3c29665280b52605aab9ee7fecd9db31237467411") << OP_CHECKSIG;
std::unique_ptr<CBlockTemplate> pblocktemplate;
CMutableTransaction tx;
CScript script;
Expand Down
28 changes: 13 additions & 15 deletions src/test/util_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@

#include <boost/test/unit_test.hpp>

#include <key_io.h>
#include <key.h>

using namespace std::literals;
static const std::string STRING_WITH_EMBEDDED_NULL_CHAR{"1"s "\0" "1"s};

Expand Down Expand Up @@ -119,7 +116,7 @@ BOOST_AUTO_TEST_CASE(util_ParseHex)
std::vector<unsigned char> result;
std::vector<unsigned char> expected(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected));
// Basic test vector
result = ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
result = ParseHex("04489d8efd89b673459f3ebbe435956c90255d31408dec347e01649c067267a16347c653e7b721d2aacd8290d3c29665280b52605aab9ee7fecd9db31237467411");
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());

// Spaces between bytes must be supported
Expand All @@ -139,7 +136,7 @@ BOOST_AUTO_TEST_CASE(util_HexStr)
{
BOOST_CHECK_EQUAL(
HexStr(ParseHex_expected),
"04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
"04489d8efd89b673459f3ebbe435956c90255d31408dec347e01649c067267a16347c653e7b721d2aacd8290d3c29665280b52605aab9ee7fecd9db31237467411");

BOOST_CHECK_EQUAL(
HexStr(Span<const unsigned char>(
Expand Down Expand Up @@ -2193,7 +2190,7 @@ BOOST_AUTO_TEST_CASE(message_sign)
const std::string message = "Trust no one";

const std::string expected_signature =
"H/sF9RAL8RLRwfaTI8j2o4TFXs2gJNV++mlbQd3/hhggYrFLO9IdN66fOiLxVYbJf+UQdE/rgtjHuYwghBXB0Uw=";
"HyFfKaXqUu1sA+mTuI75Fgmw1VhTL3esRGQsTfp9GlAISSVXMvm94PnRzcvKd4k/p1cmrVeBYBmUZrdl0jCgJ7U=";

CKey privkey;
std::string generated_signature;
Expand Down Expand Up @@ -2226,43 +2223,43 @@ BOOST_AUTO_TEST_CASE(message_verify)

BOOST_CHECK_EQUAL(
MessageVerify(
"1QFqqMUD55ZV3PJEJZtaKCsQmjLT8SEhCG",
"bgl1qapzlteru5p93c6exsqvjmy34pua8q0lws0p4kg",
"signature should be irrelevant",
"message too"),
MessageVerificationResult::ERR_ADDRESS_NO_KEY);

BOOST_CHECK_EQUAL(
MessageVerify(
"1QFqqMUD55ZV3PJEJZtaKCsQmjLT8SEhCG",
"56aToLcrXcuKWqYoNV7UmkMn23CmA6mTN1",
"invalid signature, not in base64 encoding",
"message should be irrelevant"),
MessageVerificationResult::ERR_MALFORMED_SIGNATURE);

BOOST_CHECK_EQUAL(
MessageVerify(
"1QFqqMUD55ZV3PJEJZtaKCsQmjLT8SEhCG",
"56aToLcrXcuKWqYoNV7UmkMn23CmA6mTN1",
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
"message should be irrelevant"),
MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED);

BOOST_CHECK_EQUAL(
MessageVerify(
"1QFqqMUD55ZV3PJEJZtaKCsQmjLT8SEhCG",
"56aToLcrXcuKWqYoNV7UmkMn23CmA6mTN1",
"GyCkhyvkWUMYnUU6vWwEz6J2DXLYDlwl85bD8BX0A2jUEu5Kq66pe53+nA5pponV7egAvlrNs/P/g55xsYBDZaQ=",
"I never signed this"),
MessageVerificationResult::ERR_NOT_SIGNED);

BOOST_CHECK_EQUAL(
MessageVerify(
"1F5y5E5FMc5YzdJtB9hLaUe43GDxBSJz9r",
"G2y9QWPRxMhTay3hzWYlRyAPDMNa7JBkoEKGxjvItT5yEoyFIZN5o9doEhR6ygN+iC3v5KR8V3ZX7qcjAOrmilI=",
"56aToLcrXcuKWqYoNV7UmkMn23CmA6mTN1",
"HyFfKaXqUu1sA+mTuI75Fgmw1VhTL3esRGQsTfp9GlAISSVXMvm94PnRzcvKd4k/p1cmrVeBYBmUZrdl0jCgJ7U=",
"Trust no one"),
MessageVerificationResult::OK);

BOOST_CHECK_EQUAL(
MessageVerify(
"1QFqqMUD55ZV3PJEJZtaKCsQmjLT8SEhCG",
"GyCkhyvkWUMYnUU6vWwEz6J2DXLYDlwl85bD8BX0A2jUEu5Kq66pe53+nA5pponV7egAvlrNs/P/g55xsYBDZaQ=",
"56aToLcrXcuKWqYoNV7UmkMn23CmA6mTN1",
"IGeEjdD3RTVfr9wUnwZR4q+DHz2kVfODqI/fynzhHHmFdOscHe5ePpqUAm0j+frLcIq4OotxNu86NMW8L/d7zMk=",
"Trust me"),
MessageVerificationResult::OK);
}
Expand All @@ -2280,7 +2277,8 @@ BOOST_AUTO_TEST_CASE(message_hash)
const uint256 message_hash1 = Hash(prefixed_message);
const uint256 message_hash2 = MessageHash(unsigned_tx);

BOOST_CHECK_EQUAL(message_hash1, message_hash2);
// Commented out because MessageHash uses keccak algorithm, while Hash uses CSHA256
// BOOST_CHECK_EQUAL(message_hash1, message_hash2);
BOOST_CHECK_NE(message_hash1, signature_hash);
}

Expand Down