Skip to content

Commit

Permalink
Clean duplicate usage of DecodeSecret & EncodeSecret.
Browse files Browse the repository at this point in the history
The latest version of these two functions is inside the key_io files and not in base58.
  • Loading branch information
furszy committed Jun 28, 2021
1 parent 4d4160e commit c93e19f
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 68 deletions.
2 changes: 1 addition & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,6 @@ libbitcoin_wallet_a_SOURCES = \
interfaces/wallet.cpp \
addressbook.cpp \
crypter.cpp \
key_io.cpp \
legacy/stakemodifier.cpp \
kernel.cpp \
wallet/db.cpp \
Expand Down Expand Up @@ -498,6 +497,7 @@ libbitcoin_common_a_SOURCES = \
coins.cpp \
compressor.cpp \
consensus/merkle.cpp \
key_io.cpp \
primitives/block.cpp \
primitives/transaction.cpp \
zpiv/zerocoin.cpp \
Expand Down
29 changes: 0 additions & 29 deletions src/base58.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,35 +227,6 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par

} // anon namespace

CKey DecodeSecret(const std::string& str)
{
CKey key;
std::vector<unsigned char> data;
if (DecodeBase58Check(str, data)) {
const std::vector<unsigned char>& privkey_prefix = 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(), data.begin())) {
bool compressed = data.size() == 33 + privkey_prefix.size();
key.Set(data.begin() + privkey_prefix.size(), data.begin() + privkey_prefix.size() + 32, compressed);
}
}
memory_cleanse(data.data(), data.size());
return key;
}

std::string EncodeSecret(const CKey& key)
{
assert(key.IsValid());
std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::SECRET_KEY);
data.insert(data.end(), key.begin(), key.end());
if (key.IsCompressed()) {
data.push_back(1);
}
std::string ret = EncodeBase58Check(data);
memory_cleanse(data.data(), data.size());
return ret;
}

std::string EncodeDestination(const CTxDestination& dest, bool isStaking)
{
return EncodeDestination(dest, isStaking ? CChainParams::STAKING_ADDRESS : CChainParams::PUBKEY_ADDRESS);
Expand Down
6 changes: 1 addition & 5 deletions src/base58.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,12 @@ inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRe
*/
bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet);

CKey DecodeSecret(const std::string& str);
std::string EncodeSecret(const CKey& key);

std::string EncodeDestination(const CTxDestination& dest, bool isStaking);
std::string EncodeDestination(const CTxDestination& dest, const CChainParams::Base58Type addrType = CChainParams::PUBKEY_ADDRESS);
// DecodeDestinationisStaking flag is set to true when the string arg is from an staking address
CTxDestination DecodeDestination(const std::string& str, bool& isStaking);
CTxDestination DecodeDestination(const std::string& str);
// Return true if the address is valid without care on the type.
bool IsValidDestinationString(const std::string& str);

// Return true if the address is valid and is following the fStaking flag type (true means that the destination must be a staking address, false the opposite).
bool IsValidDestinationString(const std::string& str, bool fStaking);
bool IsValidDestinationString(const std::string& str, bool fStaking, const CChainParams& params);
Expand Down
3 changes: 2 additions & 1 deletion src/messagesigner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "base58.h"
#include "hash.h"
#include "key_io.h"
#include "messagesigner.h"
#include "tinyformat.h"
#include "util/system.h"
Expand All @@ -14,7 +15,7 @@ const std::string strMessageMagic = "DarkNet Signed Message:\n";

bool CMessageSigner::GetKeysFromSecret(const std::string& strSecret, CKey& keyRet, CPubKey& pubkeyRet)
{
keyRet = DecodeSecret(strSecret);
keyRet = KeyIO::DecodeSecret(strSecret);
if (!keyRet.IsValid())
return false;

Expand Down
4 changes: 2 additions & 2 deletions src/pivx-tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include "coins.h"
#include "core_io.h"
#include "keystore.h"
#include "key_io.h"
#include "policy/policy.h"
#include "primitives/block.h" // for MAX_BLOCK_SIZE
#include "primitives/transaction.h"
#include "script/script.h"
#include "script/sign.h"
Expand Down Expand Up @@ -472,7 +472,7 @@ static void MutateTxSign(CMutableTransaction& tx, const std::string& flagStr)
for (unsigned int kidx = 0; kidx < keysObj.size(); kidx++) {
if (!keysObj[kidx].isStr())
throw std::runtime_error("privatekey not a string");
CKey key = DecodeSecret(keysObj[kidx].getValStr());
CKey key = KeyIO::DecodeSecret(keysObj[kidx].getValStr());
if (!key.IsValid()) {
throw std::runtime_error("privatekey not valid");
}
Expand Down
3 changes: 2 additions & 1 deletion src/qt/pivx/masternodewizarddialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "activemasternode.h"
#include "clientmodel.h"
#include "key_io.h"
#include "optionsmodel.h"
#include "pairresult.h"
#include "qt/pivx/mnmodel.h"
Expand Down Expand Up @@ -212,7 +213,7 @@ bool MasterNodeWizardDialog::createMN()
// create the mn key
CKey secret;
secret.MakeNewKey(false);
std::string mnKeyString = EncodeSecret(secret);
std::string mnKeyString = KeyIO::EncodeSecret(secret);

// Look for a valid collateral utxo
COutPoint collateralOut;
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/masternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
#include "db.h"
#include "evo/deterministicmns.h"
#include "init.h"
#include "key_io.h"
#include "masternode-payments.h"
#include "masternode-sync.h"
#include "masternodeconfig.h"
#include "masternodeman.h"
#include "netbase.h"
#include "rpc/server.h"
#include "utilmoneystr.h"
#ifdef ENABLE_WALLET
#include "wallet/rpcwallet.h"
#endif
Expand Down Expand Up @@ -554,7 +554,7 @@ UniValue createmasternodekey(const JSONRPCRequest& request)
CKey secret;
secret.MakeNewKey(false);

return EncodeSecret(secret);
return KeyIO::EncodeSecret(secret);
}

UniValue getmasternodeoutputs(const JSONRPCRequest& request)
Expand Down
3 changes: 2 additions & 1 deletion src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "evo/providertx.h"
#include "init.h"
#include "keystore.h"
#include "key_io.h"
#include "validationinterface.h"
#include "net.h"
#include "policy/policy.h"
Expand Down Expand Up @@ -612,7 +613,7 @@ UniValue signrawtransaction(const JSONRPCRequest& request)
UniValue keys = request.params[2].get_array();
for (unsigned int idx = 0; idx < keys.size(); idx++) {
UniValue k = keys[idx];
CKey key = DecodeSecret(k.get_str());
CKey key = KeyIO::DecodeSecret(k.get_str());
if (!key.IsValid())
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
tempKeystore.AddKey(key);
Expand Down
3 changes: 2 additions & 1 deletion src/rpc/rpcevo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "evo/deterministicmns.h"
#include "evo/specialtx.h"
#include "evo/providertx.h"
#include "key_io.h"
#include "masternode.h"
#include "messagesigner.h"
#include "netbase.h"
Expand Down Expand Up @@ -180,7 +181,7 @@ static CKey ParsePrivKey(CWallet* pwallet, const std::string &strKeyOrAddress, b
#endif // ENABLE_WALLET
}

CKey key = DecodeSecret(strKeyOrAddress);
CKey key = KeyIO::DecodeSecret(strKeyOrAddress);
if (!key.IsValid()) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding");
return key;
}
Expand Down
11 changes: 5 additions & 6 deletions src/test/base58_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
#include "data/base58_keys_valid.json.h"

#include "key.h"
#include "script/script.h"
#include "key_io.h"
#include "uint256.h"
#include "util/system.h"
#include "utilstrencodings.h"
#include "test/test_pivx.h"

Expand Down Expand Up @@ -146,7 +145,7 @@ BOOST_AUTO_TEST_CASE(base58_keys_valid_parse)
if(isPrivkey) {
bool isCompressed = find_value(metadata, "isCompressed").get_bool();
// Must be valid private key
privkey = DecodeSecret(exp_base58string);
privkey = KeyIO::DecodeSecret(exp_base58string);
BOOST_CHECK_MESSAGE(privkey.IsValid(), "!IsValid:" + strTest);
BOOST_CHECK_MESSAGE(privkey.IsCompressed() == isCompressed, "compressed mismatch:" + strTest);
BOOST_CHECK_MESSAGE(privkey.size() == exp_payload.size() && std::equal(privkey.begin(), privkey.end(), exp_payload.begin()), "key mismatch:" + strTest);
Expand All @@ -163,7 +162,7 @@ BOOST_AUTO_TEST_CASE(base58_keys_valid_parse)
BOOST_CHECK_MESSAGE(boost::apply_visitor(TestAddrTypeVisitor(exp_addrType), destination), "addrType mismatch" + strTest);

// Public key must be invalid private key
privkey = DecodeSecret(exp_base58string);
privkey = KeyIO::DecodeSecret(exp_base58string);
BOOST_CHECK_MESSAGE(!privkey.IsValid(), "IsValid pubkey as privkey:" + strTest);
}
}
Expand Down Expand Up @@ -198,7 +197,7 @@ BOOST_AUTO_TEST_CASE(base58_keys_valid_gen)
CKey key;
key.Set(exp_payload.begin(), exp_payload.end(), isCompressed);
assert(key.IsValid());
BOOST_CHECK_MESSAGE(EncodeSecret(key) == exp_base58string, "result mismatch: " + strTest);
BOOST_CHECK_MESSAGE(KeyIO::EncodeSecret(key) == exp_base58string, "result mismatch: " + strTest);
}
else
{
Expand Down Expand Up @@ -250,7 +249,7 @@ BOOST_AUTO_TEST_CASE(base58_keys_invalid)
// must be invalid as public and as private key
destination = DecodeDestination(exp_base58string);
BOOST_CHECK_MESSAGE(!IsValidDestination(destination), "IsValid pubkey:" + strTest);
privkey = DecodeSecret(exp_base58string);
privkey = KeyIO::DecodeSecret(exp_base58string);
BOOST_CHECK_MESSAGE(!privkey.IsValid(), "IsValid privkey:" + strTest);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/bloom_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ BOOST_AUTO_TEST_CASE(bloom_create_insert_serialize_with_tweak)
BOOST_AUTO_TEST_CASE(bloom_create_insert_key)
{
std::string strSecret = std::string("5Kg1gnAjaLfKiwhhPpGS3QfRg2m6awQvaj98JCZBZQ5SuS2F15C");
CKey key = DecodeSecret(strSecret);
CKey key = KeyIO::DecodeSecret(strSecret);
CPubKey pubkey = key.GetPubKey();
std::vector<unsigned char> vchPubKey(pubkey.begin(), pubkey.end());

Expand Down
14 changes: 7 additions & 7 deletions src/test/key_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "key.h"

#include "base58.h"
#include "script/script.h"
#include "key_io.h"
#include "uint256.h"
#include "util/system.h"
#include "utilstrencodings.h"
Expand Down Expand Up @@ -44,7 +44,7 @@ void dumpKeyInfo(uint256 privkey)

for (int nCompressed=0; nCompressed<2; nCompressed++)
{
printf(" * secret (base58): %s\n", EncodeSecret(secret));
printf(" * secret (base58): %s\n", KeyIO::EncodeSecret(secret));
CKey key;
key.SetSecret(secret, fCompressed);
std::vector<unsigned char> vchPubKey = key.GetPubKey();
Expand All @@ -58,15 +58,15 @@ BOOST_FIXTURE_TEST_SUITE(key_tests, TestingSetup)

BOOST_AUTO_TEST_CASE(key_test1)
{
CKey key1 = DecodeSecret(strSecret1);
CKey key1 = KeyIO::DecodeSecret(strSecret1);
BOOST_CHECK(key1.IsValid() && !key1.IsCompressed());
CKey key2 = DecodeSecret(strSecret2);
CKey key2 = KeyIO::DecodeSecret(strSecret2);
BOOST_CHECK(key2.IsValid() && !key2.IsCompressed());
CKey key1C = DecodeSecret(strSecret1C);
CKey key1C = KeyIO::DecodeSecret(strSecret1C);
BOOST_CHECK(key1C.IsValid() && key1C.IsCompressed());
CKey key2C = DecodeSecret(strSecret2C);
CKey key2C = KeyIO::DecodeSecret(strSecret2C);
BOOST_CHECK(key2C.IsValid() && key2C.IsCompressed());
CKey bad_key = DecodeSecret(strAddressBad);
CKey bad_key = KeyIO::DecodeSecret(strAddressBad);
BOOST_CHECK(!bad_key.IsValid());

CPubKey pubkey1 = key1. GetPubKey();
Expand Down
11 changes: 5 additions & 6 deletions src/test/script_P2CS_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Copyright (c) 2020 The PIVX developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://www.opensource.org/licenses/mit-license.php.
#include "test/test_pivx.h"

#include "base58.h"
#include "key.h"
#include "key_io.h"
#include "policy/policy.h"
#include "wallet/test/wallet_test_fixture.h"
#include "wallet/wallet.h"
Expand Down Expand Up @@ -59,8 +58,8 @@ BOOST_AUTO_TEST_CASE(extract_cold_staking_destination_keys)

static CScript GetNewP2CS(CKey& stakerKey, CKey& ownerKey, bool fLastOutFree)
{
stakerKey = DecodeSecret("YNdsth3BsW53DYmCiR12SofWSAt2utXQUSGoin3PekVQCMbzfS7E");
ownerKey = DecodeSecret("YUo8oW3y8cUQdQxQxCdnUJ4Ww5H7nHBEMwD2bNDpBbuLM59t4rvd");
stakerKey = KeyIO::DecodeSecret("YNdsth3BsW53DYmCiR12SofWSAt2utXQUSGoin3PekVQCMbzfS7E");
ownerKey = KeyIO::DecodeSecret("YUo8oW3y8cUQdQxQxCdnUJ4Ww5H7nHBEMwD2bNDpBbuLM59t4rvd");
return fLastOutFree ? GetScriptForStakeDelegationLOF(stakerKey.GetPubKey().GetID(), ownerKey.GetPubKey().GetID())
: GetScriptForStakeDelegation(stakerKey.GetPubKey().GetID(), ownerKey.GetPubKey().GetID());
}
Expand Down Expand Up @@ -149,7 +148,7 @@ BOOST_AUTO_TEST_CASE(coldstake_lof_script)
SignColdStake(tx, 0, scriptP2CS, stakerKey, true);
BOOST_CHECK(CheckP2CSScript(tx.vin[0].scriptSig, scriptP2CS, tx, err));

const CKey& dummyKey = DecodeSecret("YNdsth3BsW53DYmCiR12SofWSAt2utXQUSGoin3PekVQCMbzfS7E");
const CKey& dummyKey = KeyIO::DecodeSecret("YNdsth3BsW53DYmCiR12SofWSAt2utXQUSGoin3PekVQCMbzfS7E");
const CKeyID& dummyKeyID = dummyKey.GetPubKey().GetID();
const CScript& dummyP2PKH = GetDummyP2PKH(dummyKeyID);

Expand Down Expand Up @@ -225,7 +224,7 @@ BOOST_AUTO_TEST_CASE(coldstake_script)
SignColdStake(tx, 0, scriptP2CS, stakerKey, true);
BOOST_CHECK(CheckP2CSScript(tx.vin[0].scriptSig, scriptP2CS, tx, err));

const CKey& dummyKey = DecodeSecret("YNdsth3BsW53DYmCiR12SofWSAt2utXQUSGoin3PekVQCMbzfS7E");
const CKey& dummyKey = KeyIO::DecodeSecret("YNdsth3BsW53DYmCiR12SofWSAt2utXQUSGoin3PekVQCMbzfS7E");
const CKeyID& dummyKeyID = dummyKey.GetPubKey().GetID();
const CScript& dummyP2PKH = GetDummyP2PKH(dummyKeyID);

Expand Down
10 changes: 5 additions & 5 deletions src/wallet/rpcdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ UniValue importprivkey(const JSONRPCRequest& request)

const bool fStakingAddress = (request.params.size() > 3 ? request.params[3].get_bool() : false);

CKey key = DecodeSecret(strSecret);
CKey key = KeyIO::DecodeSecret(strSecret);
if (!key.IsValid()) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding");

CPubKey pubkey = key.GetPubKey();
Expand Down Expand Up @@ -402,7 +402,7 @@ UniValue importwallet(const JSONRPCRequest& request)
}
}

CKey key = DecodeSecret(vstr[0]);
CKey key = KeyIO::DecodeSecret(vstr[0]);
if (!key.IsValid())
continue;
CPubKey pubkey = key.GetPubKey();
Expand Down Expand Up @@ -490,7 +490,7 @@ UniValue dumpprivkey(const JSONRPCRequest& request)
CKey vchSecret;
if (!pwallet->GetKey(*keyID, vchSecret))
throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known");
return EncodeSecret(vchSecret);
return KeyIO::EncodeSecret(vchSecret);
}

UniValue dumpwallet(const JSONRPCRequest& request)
Expand Down Expand Up @@ -761,7 +761,7 @@ static UniValue processImport(CWallet* const pwallet, const UniValue& data, cons
if (keys.size()) {
for (size_t i = 0; i < keys.size(); i++) {
const std::string& privkey = keys[i].get_str();
CKey key = DecodeSecret(privkey);
CKey key = KeyIO::DecodeSecret(privkey);

if (!key.IsValid()) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding");
Expand Down Expand Up @@ -858,7 +858,7 @@ static UniValue processImport(CWallet* const pwallet, const UniValue& data, cons
// Import private keys.
if (keys.size()) {
const std::string& strPrivkey = keys[0].get_str();
CKey key = DecodeSecret(strPrivkey);
CKey key = KeyIO::DecodeSecret(strPrivkey);

if (!key.IsValid()) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding");
Expand Down

0 comments on commit c93e19f

Please sign in to comment.