Skip to content

Commit

Permalink
Stop using CBase58Data for ext keys
Browse files Browse the repository at this point in the history
Adaptation of btc@ebfe217b15d21656a173e5c102f826d17c6c8be4
  • Loading branch information
furszy committed Jun 28, 2021
1 parent e861cda commit 4d4160e
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 130 deletions.
63 changes: 0 additions & 63 deletions src/base58.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,69 +166,6 @@ bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRe
return DecodeBase58Check(str.c_str(), vchRet);
}

CBase58Data::CBase58Data()
{
vchVersion.clear();
vchData.clear();
}

void CBase58Data::SetData(const std::vector<unsigned char>& vchVersionIn, const void* pdata, size_t nSize)
{
vchVersion = vchVersionIn;
vchData.resize(nSize);
if (!vchData.empty())
memcpy(vchData.data(), pdata, nSize);
}

void CBase58Data::SetData(const std::vector<unsigned char>& vchVersionIn, const unsigned char* pbegin, const unsigned char* pend)
{
SetData(vchVersionIn, (void*)pbegin, pend - pbegin);
}

bool CBase58Data::SetString(const char* psz, unsigned int nVersionBytes)
{
std::vector<unsigned char> vchTemp;
bool rc58 = DecodeBase58Check(psz, vchTemp);
if ((!rc58) || (vchTemp.size() < nVersionBytes)) {
vchData.clear();
vchVersion.clear();
return false;
}
vchVersion.assign(vchTemp.begin(), vchTemp.begin() + nVersionBytes);
vchData.resize(vchTemp.size() - nVersionBytes);
if (!vchData.empty())
memcpy(vchData.data(), vchTemp.data() + nVersionBytes, vchData.size());
memory_cleanse(vchTemp.data(), vchTemp.size());
return true;
}

bool CBase58Data::SetString(const std::string& str)
{
if (str.empty())
return false;
return SetString(str.c_str());
}

std::string CBase58Data::ToString() const
{
std::vector<unsigned char> vch = vchVersion;
vch.insert(vch.end(), vchData.begin(), vchData.end());
return EncodeBase58Check(vch);
}

int CBase58Data::CompareTo(const CBase58Data& b58) const
{
if (vchVersion < b58.vchVersion)
return -1;
if (vchVersion > b58.vchVersion)
return 1;
if (vchData < b58.vchData)
return -1;
if (vchData > b58.vchData)
return 1;
return 0;
}

namespace
{
class DestinationEncoder : public boost::static_visitor<std::string>
Expand Down
67 changes: 0 additions & 67 deletions src/base58.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,76 +70,9 @@ inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRe
*/
bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet);

/**
* Base class for all base58-encoded data
*/
class CBase58Data
{
protected:
//! the version byte(s)
std::vector<unsigned char> vchVersion;

//! the actually encoded data
typedef std::vector<unsigned char, zero_after_free_allocator<unsigned char> > vector_uchar;
vector_uchar vchData;

CBase58Data();
void SetData(const std::vector<unsigned char>& vchVersionIn, const void* pdata, size_t nSize);
void SetData(const std::vector<unsigned char>& vchVersionIn, const unsigned char* pbegin, const unsigned char* pend);

public:
bool SetString(const char* psz, unsigned int nVersionBytes = 1);
bool SetString(const std::string& str);
std::string ToString() const;
int CompareTo(const CBase58Data& b58) const;

bool operator==(const CBase58Data& b58) const { return CompareTo(b58) == 0; }
bool operator<=(const CBase58Data& b58) const { return CompareTo(b58) <= 0; }
bool operator>=(const CBase58Data& b58) const { return CompareTo(b58) >= 0; }
bool operator<(const CBase58Data& b58) const { return CompareTo(b58) < 0; }
bool operator>(const CBase58Data& b58) const { return CompareTo(b58) > 0; }
};

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

template <typename K, int Size, CChainParams::Base58Type Type>
class CBitcoinExtKeyBase : public CBase58Data
{
public:
void SetKey(const K& key)
{
unsigned char vch[Size];
key.Encode(vch);
SetData(Params().Base58Prefix(Type), vch, vch + Size);
}

K GetKey()
{
K ret;
if (vchData.size() == Size) {
// If base58 encoded data does not hold an ext key, return a !IsValid() key
ret.Decode(vchData.data());
}
return ret;
}

CBitcoinExtKeyBase(const K& key)
{
SetKey(key);
}

CBitcoinExtKeyBase(const std::string& strBase58c) {
SetString(strBase58c.c_str(), Params().Base58Prefix(Type).size());
}

CBitcoinExtKeyBase() {}
};

typedef CBitcoinExtKeyBase<CExtKey, BIP32_EXTKEY_SIZE, CChainParams::EXT_SECRET_KEY> CBitcoinExtKey;
typedef CBitcoinExtKeyBase<CExtPubKey, BIP32_EXTKEY_SIZE, CChainParams::EXT_PUBLIC_KEY> CBitcoinExtPubKey;


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
Expand Down

0 comments on commit 4d4160e

Please sign in to comment.