diff --git a/src/key_io.h b/src/key_io.h --- a/src/key_io.h +++ b/src/key_io.h @@ -16,7 +16,9 @@ class CChainParams; CKey DecodeSecret(const std::string &str); +CKey DecodeSecret(const std::string &str, const CChainParams ¶ms); std::string EncodeSecret(const CKey &key); +std::string EncodeSecret(const CKey &key, const CChainParams ¶ms); CExtKey DecodeExtKey(const std::string &str); std::string EncodeExtKey(const CExtKey &extkey); diff --git a/src/key_io.cpp b/src/key_io.cpp --- a/src/key_io.cpp +++ b/src/key_io.cpp @@ -78,11 +78,15 @@ } // namespace CKey DecodeSecret(const std::string &str) { + return DecodeSecret(str, Params()); +} + +CKey DecodeSecret(const std::string &str, const CChainParams ¶ms) { CKey key; std::vector data; if (DecodeBase58Check(str, data, 34)) { const std::vector &privkey_prefix = - Params().Base58Prefix(CChainParams::SECRET_KEY); + params.Base58Prefix(CChainParams::SECRET_KEY); if ((data.size() == 32 + privkey_prefix.size() || (data.size() == 33 + privkey_prefix.size() && data.back() == 1)) && std::equal(privkey_prefix.begin(), privkey_prefix.end(), @@ -99,8 +103,12 @@ } std::string EncodeSecret(const CKey &key) { + return EncodeSecret(key, Params()); +} + +std::string EncodeSecret(const CKey &key, const CChainParams ¶ms) { assert(key.IsValid()); - std::vector data = Params().Base58Prefix(CChainParams::SECRET_KEY); + std::vector data = params.Base58Prefix(CChainParams::SECRET_KEY); data.insert(data.end(), key.begin(), key.end()); if (key.IsCompressed()) { data.push_back(1); diff --git a/src/test/key_tests.cpp b/src/test/key_tests.cpp --- a/src/test/key_tests.cpp +++ b/src/test/key_tests.cpp @@ -23,6 +23,8 @@ "5KC4ejrDjv152FGwP386VD1i2NYc5KkfSMyv1nGy1VGDxGHqVY3"; static const std::string strSecret1C = "Kwr371tjA9u2rFSMZjTNun2PXXP3WPZu2afRHTcta6KxEUdm1vEw"; +static const std::string strTestSecret1C = + "cND2ZvtabDbJ1gucx9GWH6XT9kgTAqfb6cotPt5Q5CyxVDhid2EN"; static const std::string strSecret2C = "L3Hq7a8FEQwJkW1M2GNKDW28546Vp5miewcCzSqUD9kCAXrJdS3g"; static const std::string addr1 = "1QFqqMUD55ZV3PJEJZtaKCsQmjLT6JkjvJ"; @@ -81,6 +83,47 @@ "24c22e00b7bc7944a1f78")); } +BOOST_AUTO_TEST_CASE(encode_decode_secret_test) { + const auto mainParams = CreateChainParams(CBaseChainParams::MAIN); + const auto testParams = CreateChainParams(CBaseChainParams::TESTNET); + const auto regParams = CreateChainParams(CBaseChainParams::TESTNET); + + { + // Check the mainnet base58 key + CKey mainKey = DecodeSecret(strSecret1C, *mainParams); + BOOST_CHECK(mainKey.IsValid() && mainKey.IsCompressed()); + + CKey testKey = DecodeSecret(strSecret1C, *testParams); + BOOST_CHECK(!testKey.IsValid()); + + CKey regKey = DecodeSecret(strSecret1C, *regParams); + BOOST_CHECK(!regKey.IsValid()); + } + + { + // Check the testnet and regnet base58 key + CKey mainKey = DecodeSecret(strTestSecret1C, *mainParams); + BOOST_CHECK(!mainKey.IsValid()); + + CKey testKey = DecodeSecret(strTestSecret1C, *testParams); + BOOST_CHECK(testKey.IsValid() && testKey.IsCompressed()); + + CKey regKey = DecodeSecret(strTestSecret1C, *regParams); + BOOST_CHECK(regKey.IsValid() && regKey.IsCompressed()); + } + + CKey mainKey = DecodeSecret(strSecret1C, *mainParams); + CKey testKey = DecodeSecret(strTestSecret1C, *testParams); + + // Check key conversion. + BOOST_CHECK_EQUAL(EncodeSecret(mainKey, *mainParams), strSecret1C); + BOOST_CHECK_EQUAL(EncodeSecret(mainKey, *testParams), strTestSecret1C); + BOOST_CHECK_EQUAL(EncodeSecret(mainKey, *regParams), strTestSecret1C); + BOOST_CHECK_EQUAL(EncodeSecret(testKey, *mainParams), strSecret1C); + BOOST_CHECK_EQUAL(EncodeSecret(testKey, *testParams), strTestSecret1C); + BOOST_CHECK_EQUAL(EncodeSecret(testKey, *regParams), strTestSecret1C); +} + BOOST_AUTO_TEST_CASE(key_test1) { CKey key1 = DecodeSecret(strSecret1); BOOST_CHECK(key1.IsValid() && !key1.IsCompressed());