Skip to content

Commit 70c8257

Browse files
laanwjUdjinM6
authored andcommitted
Merge bitcoin#11372: Address encoding cleanup
92f1f8b Split off key_io_tests from base58_tests (Pieter Wuille) 119b0f8 Split key_io (address/key encodings) off from base58 (Pieter Wuille) ebfe217 Stop using CBase58Data for ext keys (Pieter Wuille) 32e69fa Replace CBitcoinSecret with {Encode,Decode}Secret (Pieter Wuille) Pull request description: This PR contains some of the changes left as TODO in bitcoin#11167 (and built on top of that PR). They are not intended for backporting. This removes the `CBase58`, `CBitcoinSecret`, `CBitcoinExtKey`, and `CBitcoinExtPubKey` classes, in favor of simple `Encode`/`Decode` functions. Furthermore, all Bitcoin-specific logic (addresses, WIF, BIP32) is moved to `key_io.{h,cpp}`, leaving `base58.{h,cpp}` as a pure utility that implements the base58 encoding/decoding logic. Tree-SHA512: a5962c0ed27ad53cbe00f22af432cf11aa530e3efc9798e25c004bc9ed1b5673db5df3956e398ee2c085e3a136ac8da69fe7a7d97a05fb2eb3be0b60d0479655 Make linter happy Dashify
1 parent c9936a5 commit 70c8257

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+456
-526
lines changed

src/Makefile.am

+2
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ BITCOIN_CORE_H = \
177177
interfaces/node.h \
178178
interfaces/wallet.h \
179179
key.h \
180+
key_io.h \
180181
keepass.h \
181182
keystore.h \
182183
dbwrapper.h \
@@ -534,6 +535,7 @@ libdash_common_a_SOURCES = \
534535
core_write.cpp \
535536
hdchain.cpp \
536537
key.cpp \
538+
key_io.cpp \
537539
keystore.cpp \
538540
netaddress.cpp \
539541
netbase.cpp \

src/Makefile.test.include

+6-5
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ TEST_BINARY=test/test_dash$(EXEEXT)
1111

1212
JSON_TEST_FILES = \
1313
test/data/blockfilters.json \
14-
test/data/script_tests.json \
15-
test/data/base58_keys_valid.json \
1614
test/data/base58_encode_decode.json \
17-
test/data/base58_keys_invalid.json \
1815
test/data/bip39_vectors.json \
16+
test/data/key_io_valid.json \
17+
test/data/key_io_invalid.json \
1918
test/data/proposals_valid.json \
2019
test/data/proposals_invalid.json \
20+
test/data/script_tests.json \
21+
test/data/sighash.json \
2122
test/data/tx_invalid.json \
22-
test/data/tx_valid.json \
23-
test/data/sighash.json
23+
test/data/tx_valid.json
2424

2525
RAW_TEST_FILES =
2626

@@ -58,6 +58,7 @@ BITCOIN_TESTS =\
5858
test/getarg_tests.cpp \
5959
test/governance_validators_tests.cpp \
6060
test/hash_tests.cpp \
61+
test/key_io_tests.cpp \
6162
test/key_tests.cpp \
6263
test/limitedmap_tests.cpp \
6364
test/dbwrapper_tests.cpp \

src/base58.cpp

-171
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,8 @@
55
#include <base58.h>
66

77
#include <hash.h>
8-
#include <script/script.h>
98
#include <uint256.h>
109

11-
#include <boost/variant/apply_visitor.hpp>
12-
#include <boost/variant/static_visitor.hpp>
13-
14-
#include <algorithm>
1510
#include <assert.h>
1611
#include <stdint.h>
1712

@@ -166,169 +161,3 @@ bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRe
166161
return DecodeBase58Check(str.c_str(), vchRet);
167162
}
168163

169-
CBase58Data::CBase58Data()
170-
{
171-
vchVersion.clear();
172-
vchData.clear();
173-
}
174-
175-
void CBase58Data::SetData(const std::vector<unsigned char>& vchVersionIn, const void* pdata, size_t nSize)
176-
{
177-
vchVersion = vchVersionIn;
178-
vchData.resize(nSize);
179-
if (!vchData.empty())
180-
memcpy(vchData.data(), pdata, nSize);
181-
}
182-
183-
void CBase58Data::SetData(const std::vector<unsigned char>& vchVersionIn, const unsigned char* pbegin, const unsigned char* pend)
184-
{
185-
SetData(vchVersionIn, (void*)pbegin, pend - pbegin);
186-
}
187-
188-
bool CBase58Data::SetString(const char* psz, unsigned int nVersionBytes)
189-
{
190-
std::vector<unsigned char> vchTemp;
191-
bool rc58 = DecodeBase58Check(psz, vchTemp);
192-
if ((!rc58) || (vchTemp.size() < nVersionBytes)) {
193-
vchData.clear();
194-
vchVersion.clear();
195-
return false;
196-
}
197-
vchVersion.assign(vchTemp.begin(), vchTemp.begin() + nVersionBytes);
198-
vchData.resize(vchTemp.size() - nVersionBytes);
199-
if (!vchData.empty())
200-
memcpy(vchData.data(), vchTemp.data() + nVersionBytes, vchData.size());
201-
memory_cleanse(vchTemp.data(), vchTemp.size());
202-
return true;
203-
}
204-
205-
bool CBase58Data::SetString(const std::string& str)
206-
{
207-
return SetString(str.c_str());
208-
}
209-
210-
std::string CBase58Data::ToString() const
211-
{
212-
std::vector<unsigned char> vch = vchVersion;
213-
vch.insert(vch.end(), vchData.begin(), vchData.end());
214-
return EncodeBase58Check(vch);
215-
}
216-
217-
int CBase58Data::CompareTo(const CBase58Data& b58) const
218-
{
219-
if (vchVersion < b58.vchVersion)
220-
return -1;
221-
if (vchVersion > b58.vchVersion)
222-
return 1;
223-
if (vchData < b58.vchData)
224-
return -1;
225-
if (vchData > b58.vchData)
226-
return 1;
227-
return 0;
228-
}
229-
230-
namespace
231-
{
232-
class DestinationEncoder : public boost::static_visitor<std::string>
233-
{
234-
private:
235-
const CChainParams& m_params;
236-
237-
public:
238-
DestinationEncoder(const CChainParams& params) : m_params(params) {}
239-
240-
std::string operator()(const CKeyID& id) const
241-
{
242-
std::vector<unsigned char> data = m_params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
243-
data.insert(data.end(), id.begin(), id.end());
244-
return EncodeBase58Check(data);
245-
}
246-
247-
std::string operator()(const CScriptID& id) const
248-
{
249-
std::vector<unsigned char> data = m_params.Base58Prefix(CChainParams::SCRIPT_ADDRESS);
250-
data.insert(data.end(), id.begin(), id.end());
251-
return EncodeBase58Check(data);
252-
}
253-
254-
std::string operator()(const CNoDestination& no) const { return ""; }
255-
};
256-
257-
CTxDestination DecodeDestination(const std::string& str, const CChainParams& params)
258-
{
259-
std::vector<unsigned char> data;
260-
uint160 hash;
261-
if (DecodeBase58Check(str, data)) {
262-
// base58-encoded Bitcoin addresses.
263-
// Public-key-hash-addresses have version 0 (or 111 testnet).
264-
// The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
265-
const std::vector<unsigned char>& pubkey_prefix = params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
266-
if (data.size() == hash.size() + pubkey_prefix.size() && std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin())) {
267-
std::copy(data.begin() + pubkey_prefix.size(), data.end(), hash.begin());
268-
return CKeyID(hash);
269-
}
270-
// Script-hash-addresses have version 5 (or 196 testnet).
271-
// The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script.
272-
const std::vector<unsigned char>& script_prefix = params.Base58Prefix(CChainParams::SCRIPT_ADDRESS);
273-
if (data.size() == hash.size() + script_prefix.size() && std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) {
274-
std::copy(data.begin() + script_prefix.size(), data.end(), hash.begin());
275-
return CScriptID(hash);
276-
}
277-
}
278-
return CNoDestination();
279-
}
280-
} // namespace
281-
282-
void CBitcoinSecret::SetKey(const CKey& vchSecret)
283-
{
284-
assert(vchSecret.IsValid());
285-
SetData(Params().Base58Prefix(CChainParams::SECRET_KEY), vchSecret.begin(), vchSecret.size());
286-
if (vchSecret.IsCompressed())
287-
vchData.push_back(1);
288-
}
289-
290-
CKey CBitcoinSecret::GetKey()
291-
{
292-
CKey ret;
293-
assert(vchData.size() >= 32);
294-
ret.Set(vchData.begin(), vchData.begin() + 32, vchData.size() > 32 && vchData[32] == 1);
295-
return ret;
296-
}
297-
298-
bool CBitcoinSecret::IsValid() const
299-
{
300-
bool fExpectedFormat = vchData.size() == 32 || (vchData.size() == 33 && vchData[32] == 1);
301-
bool fCorrectVersion = vchVersion == Params().Base58Prefix(CChainParams::SECRET_KEY);
302-
return fExpectedFormat && fCorrectVersion;
303-
}
304-
305-
bool CBitcoinSecret::SetString(const char* pszSecret)
306-
{
307-
return CBase58Data::SetString(pszSecret) && IsValid();
308-
}
309-
310-
bool CBitcoinSecret::SetString(const std::string& strSecret)
311-
{
312-
return SetString(strSecret.c_str());
313-
}
314-
315-
std::string EncodeDestination(const CTxDestination& dest)
316-
{
317-
return boost::apply_visitor(DestinationEncoder(Params()), dest);
318-
}
319-
320-
CTxDestination DecodeDestination(const std::string& str)
321-
{
322-
return DecodeDestination(str, Params());
323-
}
324-
325-
bool IsValidDestinationString(const std::string& str, const CChainParams& params)
326-
{
327-
return IsValidDestination(DecodeDestination(str, params));
328-
}
329-
330-
bool IsValidDestinationString(const std::string& str)
331-
{
332-
return IsValidDestinationString(str, Params());
333-
}
334-

src/base58.h

+2-91
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@
1414
#ifndef BITCOIN_BASE58_H
1515
#define BITCOIN_BASE58_H
1616

17-
#include <chainparams.h>
18-
#include <key.h>
19-
#include <pubkey.h>
20-
#include <script/standard.h>
21-
#include <support/allocators/zeroafterfree.h>
22-
2317
#include <string>
2418
#include <vector>
2519

@@ -56,95 +50,12 @@ std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn);
5650
* Decode a base58-encoded string (psz) that includes a checksum into a byte
5751
* vector (vchRet), return true if decoding is successful
5852
*/
59-
inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet);
53+
bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet);
6054

6155
/**
6256
* Decode a base58-encoded string (str) that includes a checksum into a byte
6357
* vector (vchRet), return true if decoding is successful
6458
*/
65-
inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet);
66-
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-
97-
/**
98-
* A base58-encoded secret key
99-
*/
100-
class CBitcoinSecret : public CBase58Data
101-
{
102-
public:
103-
void SetKey(const CKey& vchSecret);
104-
CKey GetKey();
105-
bool IsValid() const;
106-
bool SetString(const char* pszSecret);
107-
bool SetString(const std::string& strSecret);
108-
109-
CBitcoinSecret(const CKey& vchSecret) { SetKey(vchSecret); }
110-
CBitcoinSecret() {}
111-
};
112-
113-
template<typename K, int Size, CChainParams::Base58Type Type> class CBitcoinExtKeyBase : public CBase58Data
114-
{
115-
public:
116-
void SetKey(const K &key) {
117-
unsigned char vch[Size];
118-
key.Encode(vch);
119-
SetData(Params().Base58Prefix(Type), vch, vch+Size);
120-
}
121-
122-
K GetKey() {
123-
K ret;
124-
if (vchData.size() == Size) {
125-
// If base58 encoded data does not hold an ext key, return a !IsValid() key
126-
ret.Decode(vchData.data());
127-
}
128-
return ret;
129-
}
130-
131-
CBitcoinExtKeyBase(const K &key) {
132-
SetKey(key);
133-
}
134-
135-
CBitcoinExtKeyBase(const std::string& strBase58c) {
136-
SetString(strBase58c.c_str(), Params().Base58Prefix(Type).size());
137-
}
138-
139-
CBitcoinExtKeyBase() {}
140-
};
141-
142-
typedef CBitcoinExtKeyBase<CExtKey, BIP32_EXTKEY_SIZE, CChainParams::EXT_SECRET_KEY> CBitcoinExtKey;
143-
typedef CBitcoinExtKeyBase<CExtPubKey, BIP32_EXTKEY_SIZE, CChainParams::EXT_PUBLIC_KEY> CBitcoinExtPubKey;
144-
145-
std::string EncodeDestination(const CTxDestination& dest);
146-
CTxDestination DecodeDestination(const std::string& str);
147-
bool IsValidDestinationString(const std::string& str);
148-
bool IsValidDestinationString(const std::string& str, const CChainParams& params);
59+
bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet);
14960

15061
#endif // BITCOIN_BASE58_H

src/core_write.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
#include <core_io.h>
66

7-
#include <base58.h>
7+
#include <consensus/consensus.h>
8+
#include <consensus/validation.h>
9+
#include <key_io.h>
810
#include <primitives/transaction.h>
911
#include <script/script.h>
1012
#include <script/standard.h>

src/dash-tx.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
#include <config/dash-config.h>
77
#endif
88

9-
#include <base58.h>
109
#include <clientversion.h>
1110
#include <coins.h>
1211
#include <consensus/consensus.h>
1312
#include <core_io.h>
13+
#include <key_io.h>
1414
#include <keystore.h>
1515
#include <policy/policy.h>
1616
#include <primitives/transaction.h>
@@ -516,12 +516,10 @@ static void MutateTxSign(CMutableTransaction& tx, const std::string& flagStr)
516516
for (unsigned int kidx = 0; kidx < keysObj.size(); kidx++) {
517517
if (!keysObj[kidx].isStr())
518518
throw std::runtime_error("privatekey not a std::string");
519-
CBitcoinSecret vchSecret;
520-
bool fGood = vchSecret.SetString(keysObj[kidx].getValStr());
521-
if (!fGood)
519+
CKey key = DecodeSecret(keysObj[kidx].getValStr());
520+
if (!key.IsValid()) {
522521
throw std::runtime_error("privatekey not valid");
523-
524-
CKey key = vchSecret.GetKey();
522+
}
525523
tempKeystore.AddKey(key);
526524
}
527525

src/evo/providertx.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <consensus/validation.h>
1010
#include <primitives/transaction.h>
1111

12-
#include <base58.h>
12+
#include <key_io.h>
1313
#include <netaddress.h>
1414
#include <pubkey.h>
1515
#include <univalue.h>

src/governance/governance-validators.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <governance/governance-object.h>
66
#include <governance/governance-validators.h>
77

8-
#include <base58.h>
8+
#include <key_io.h>
99
#include <timedata.h>
1010
#include <tinyformat.h>
1111
#include <utilstrencodings.h>

0 commit comments

Comments
 (0)