Skip to content

Commit ebfe217

Browse files
committed
Stop using CBase58Data for ext keys
1 parent 32e69fa commit ebfe217

File tree

4 files changed

+56
-138
lines changed

4 files changed

+56
-138
lines changed

src/base58.cpp

+47-61
Original file line numberDiff line numberDiff line change
@@ -152,67 +152,6 @@ bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRe
152152
return DecodeBase58Check(str.c_str(), vchRet);
153153
}
154154

155-
CBase58Data::CBase58Data()
156-
{
157-
vchVersion.clear();
158-
vchData.clear();
159-
}
160-
161-
void CBase58Data::SetData(const std::vector<unsigned char>& vchVersionIn, const void* pdata, size_t nSize)
162-
{
163-
vchVersion = vchVersionIn;
164-
vchData.resize(nSize);
165-
if (!vchData.empty())
166-
memcpy(vchData.data(), pdata, nSize);
167-
}
168-
169-
void CBase58Data::SetData(const std::vector<unsigned char>& vchVersionIn, const unsigned char* pbegin, const unsigned char* pend)
170-
{
171-
SetData(vchVersionIn, (void*)pbegin, pend - pbegin);
172-
}
173-
174-
bool CBase58Data::SetString(const char* psz, unsigned int nVersionBytes)
175-
{
176-
std::vector<unsigned char> vchTemp;
177-
bool rc58 = DecodeBase58Check(psz, vchTemp);
178-
if ((!rc58) || (vchTemp.size() < nVersionBytes)) {
179-
vchData.clear();
180-
vchVersion.clear();
181-
return false;
182-
}
183-
vchVersion.assign(vchTemp.begin(), vchTemp.begin() + nVersionBytes);
184-
vchData.resize(vchTemp.size() - nVersionBytes);
185-
if (!vchData.empty())
186-
memcpy(vchData.data(), vchTemp.data() + nVersionBytes, vchData.size());
187-
memory_cleanse(vchTemp.data(), vchTemp.size());
188-
return true;
189-
}
190-
191-
bool CBase58Data::SetString(const std::string& str)
192-
{
193-
return SetString(str.c_str());
194-
}
195-
196-
std::string CBase58Data::ToString() const
197-
{
198-
std::vector<unsigned char> vch = vchVersion;
199-
vch.insert(vch.end(), vchData.begin(), vchData.end());
200-
return EncodeBase58Check(vch);
201-
}
202-
203-
int CBase58Data::CompareTo(const CBase58Data& b58) const
204-
{
205-
if (vchVersion < b58.vchVersion)
206-
return -1;
207-
if (vchVersion > b58.vchVersion)
208-
return 1;
209-
if (vchData < b58.vchData)
210-
return -1;
211-
if (vchData > b58.vchData)
212-
return 1;
213-
return 0;
214-
}
215-
216155
namespace
217156
{
218157
class DestinationEncoder : public boost::static_visitor<std::string>
@@ -352,6 +291,53 @@ std::string EncodeSecret(const CKey& key)
352291
return ret;
353292
}
354293

294+
CExtPubKey DecodeExtPubKey(const std::string& str)
295+
{
296+
CExtPubKey key;
297+
std::vector<unsigned char> data;
298+
if (DecodeBase58Check(str, data)) {
299+
const std::vector<unsigned char>& prefix = Params().Base58Prefix(CChainParams::EXT_PUBLIC_KEY);
300+
if (data.size() == BIP32_EXTKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) {
301+
key.Decode(data.data() + prefix.size());
302+
}
303+
}
304+
return key;
305+
}
306+
307+
std::string EncodeExtPubKey(const CExtPubKey& key)
308+
{
309+
std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::EXT_PUBLIC_KEY);
310+
size_t size = data.size();
311+
data.resize(size + BIP32_EXTKEY_SIZE);
312+
key.Encode(data.data() + size);
313+
std::string ret = EncodeBase58Check(data);
314+
return ret;
315+
}
316+
317+
CExtKey DecodeExtKey(const std::string& str)
318+
{
319+
CExtKey key;
320+
std::vector<unsigned char> data;
321+
if (DecodeBase58Check(str, data)) {
322+
const std::vector<unsigned char>& prefix = Params().Base58Prefix(CChainParams::EXT_SECRET_KEY);
323+
if (data.size() == BIP32_EXTKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) {
324+
key.Decode(data.data() + prefix.size());
325+
}
326+
}
327+
return key;
328+
}
329+
330+
std::string EncodeExtKey(const CExtKey& key)
331+
{
332+
std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::EXT_SECRET_KEY);
333+
size_t size = data.size();
334+
data.resize(size + BIP32_EXTKEY_SIZE);
335+
key.Encode(data.data() + size);
336+
std::string ret = EncodeBase58Check(data);
337+
memory_cleanse(data.data(), data.size());
338+
return ret;
339+
}
340+
355341
std::string EncodeDestination(const CTxDestination& dest)
356342
{
357343
return boost::apply_visitor(DestinationEncoder(Params()), dest);

src/base58.h

+4-61
Original file line numberDiff line numberDiff line change
@@ -64,70 +64,13 @@ inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRe
6464
*/
6565
inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet);
6666

67-
/**
68-
* Base class for all base58-encoded data
69-
*/
70-
class CBase58Data
71-
{
72-
protected:
73-
//! the version byte(s)
74-
std::vector<unsigned char> vchVersion;
75-
76-
//! the actually encoded data
77-
typedef std::vector<unsigned char, zero_after_free_allocator<unsigned char> > vector_uchar;
78-
vector_uchar vchData;
79-
80-
CBase58Data();
81-
void SetData(const std::vector<unsigned char> &vchVersionIn, const void* pdata, size_t nSize);
82-
void SetData(const std::vector<unsigned char> &vchVersionIn, const unsigned char *pbegin, const unsigned char *pend);
83-
84-
public:
85-
bool SetString(const char* psz, unsigned int nVersionBytes = 1);
86-
bool SetString(const std::string& str);
87-
std::string ToString() const;
88-
int CompareTo(const CBase58Data& b58) const;
89-
90-
bool operator==(const CBase58Data& b58) const { return CompareTo(b58) == 0; }
91-
bool operator<=(const CBase58Data& b58) const { return CompareTo(b58) <= 0; }
92-
bool operator>=(const CBase58Data& b58) const { return CompareTo(b58) >= 0; }
93-
bool operator< (const CBase58Data& b58) const { return CompareTo(b58) < 0; }
94-
bool operator> (const CBase58Data& b58) const { return CompareTo(b58) > 0; }
95-
};
96-
9767
CKey DecodeSecret(const std::string& str);
9868
std::string EncodeSecret(const CKey& key);
9969

100-
template<typename K, int Size, CChainParams::Base58Type Type> class CBitcoinExtKeyBase : public CBase58Data
101-
{
102-
public:
103-
void SetKey(const K &key) {
104-
unsigned char vch[Size];
105-
key.Encode(vch);
106-
SetData(Params().Base58Prefix(Type), vch, vch+Size);
107-
}
108-
109-
K GetKey() {
110-
K ret;
111-
if (vchData.size() == Size) {
112-
// If base58 encoded data does not hold an ext key, return a !IsValid() key
113-
ret.Decode(vchData.data());
114-
}
115-
return ret;
116-
}
117-
118-
CBitcoinExtKeyBase(const K &key) {
119-
SetKey(key);
120-
}
121-
122-
CBitcoinExtKeyBase(const std::string& strBase58c) {
123-
SetString(strBase58c.c_str(), Params().Base58Prefix(Type).size());
124-
}
125-
126-
CBitcoinExtKeyBase() {}
127-
};
128-
129-
typedef CBitcoinExtKeyBase<CExtKey, BIP32_EXTKEY_SIZE, CChainParams::EXT_SECRET_KEY> CBitcoinExtKey;
130-
typedef CBitcoinExtKeyBase<CExtPubKey, BIP32_EXTKEY_SIZE, CChainParams::EXT_PUBLIC_KEY> CBitcoinExtPubKey;
70+
CExtKey DecodeExtKey(const std::string& str);
71+
std::string EncodeExtKey(const CExtKey& extkey);
72+
CExtPubKey DecodeExtPubKey(const std::string& str);
73+
std::string EncodeExtPubKey(const CExtPubKey& extpubkey);
13174

13275
std::string EncodeDestination(const CTxDestination& dest);
13376
CTxDestination DecodeDestination(const std::string& str);

src/test/bip32_tests.cpp

+4-12
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,12 @@ void RunTest(const TestVector &test) {
9999
pubkey.Encode(data);
100100

101101
// Test private key
102-
CBitcoinExtKey b58key; b58key.SetKey(key);
103-
BOOST_CHECK(b58key.ToString() == derive.prv);
104-
105-
CBitcoinExtKey b58keyDecodeCheck(derive.prv);
106-
CExtKey checkKey = b58keyDecodeCheck.GetKey();
107-
assert(checkKey == key); //ensure a base58 decoded key also matches
102+
BOOST_CHECK(EncodeExtKey(key) == derive.prv);
103+
BOOST_CHECK(DecodeExtKey(derive.prv) == key); //ensure a base58 decoded key also matches
108104

109105
// Test public key
110-
CBitcoinExtPubKey b58pubkey; b58pubkey.SetKey(pubkey);
111-
BOOST_CHECK(b58pubkey.ToString() == derive.pub);
112-
113-
CBitcoinExtPubKey b58PubkeyDecodeCheck(derive.pub);
114-
CExtPubKey checkPubKey = b58PubkeyDecodeCheck.GetKey();
115-
assert(checkPubKey == pubkey); //ensure a base58 decoded pubkey also matches
106+
BOOST_CHECK(EncodeExtPubKey(pubkey) == derive.pub);
107+
BOOST_CHECK(DecodeExtPubKey(derive.pub) == pubkey); //ensure a base58 decoded pubkey also matches
116108

117109
// Derive new keys
118110
CExtKey keyNew;

src/wallet/rpcdump.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -736,10 +736,7 @@ UniValue dumpwallet(const JSONRPCRequest& request)
736736
CExtKey masterKey;
737737
masterKey.SetMaster(key.begin(), key.size());
738738

739-
CBitcoinExtKey b58extkey;
740-
b58extkey.SetKey(masterKey);
741-
742-
file << "# extended private masterkey: " << b58extkey.ToString() << "\n\n";
739+
file << "# extended private masterkey: " << EncodeExtKey(masterKey) << "\n\n";
743740
}
744741
}
745742
for (std::vector<std::pair<int64_t, CKeyID> >::const_iterator it = vKeyBirth.begin(); it != vKeyBirth.end(); it++) {

0 commit comments

Comments
 (0)