Skip to content

Commit b07ab3f

Browse files
authored
Merge pull request #2634 from div72/cleanup-key-io
refactor, misc: remove CBitcoin(Address|Secret)
2 parents 526d71d + cff5c14 commit b07ab3f

Some content is hidden

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

65 files changed

+834
-1268
lines changed

src/Makefile.test.include

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ TEST_SRCDIR = test
88
TEST_BINARY=test/test_gridcoin$(EXEEXT)
99

1010
JSON_TEST_FILES = \
11-
test/data/base58_keys_valid.json \
1211
test/data/base58_encode_decode.json \
13-
test/data/base58_keys_invalid.json \
12+
test/data/key_io_invalid.json \
13+
test/data/key_io_valid.json \
1414
test/data/script_valid.json \
1515
test/data/script_invalid.json \
1616
test/data/tx_invalid.json \
@@ -59,6 +59,7 @@ GRIDCOIN_TESTS =\
5959
test/gridcoin/sidestake_tests.cpp \
6060
test/gridcoin/superblock_tests.cpp \
6161
test/key_tests.cpp \
62+
test/key_io_tests.cpp \
6263
test/merkle_tests.cpp \
6364
test/mruset_tests.cpp \
6465
test/multisig_tests.cpp \

src/base58.h

Lines changed: 0 additions & 288 deletions
Original file line numberDiff line numberDiff line change
@@ -60,292 +60,4 @@ std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn);
6060
[[nodiscard]] bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet,
6161
int max_ret_len = std::numeric_limits<int>::max() - 4);
6262

63-
64-
/** Base class for all base58-encoded data */
65-
class CBase58Data
66-
{
67-
protected:
68-
// the version byte
69-
unsigned char nVersion;
70-
71-
// the actually encoded data
72-
std::vector<unsigned char> vchData;
73-
74-
CBase58Data()
75-
{
76-
nVersion = 0;
77-
vchData.clear();
78-
}
79-
80-
~CBase58Data()
81-
{
82-
// zero the memory, as it may contain sensitive data
83-
if (!vchData.empty())
84-
memory_cleanse(&vchData[0], vchData.size());
85-
}
86-
87-
void SetData(int nVersionIn, const void* pdata, size_t nSize)
88-
{
89-
nVersion = nVersionIn;
90-
vchData.resize(nSize);
91-
if (!vchData.empty())
92-
memcpy(&vchData[0], pdata, nSize);
93-
}
94-
95-
void SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend)
96-
{
97-
SetData(nVersionIn, (void*)pbegin, pend - pbegin);
98-
}
99-
100-
public:
101-
bool SetString(const char* psz)
102-
{
103-
std::vector<unsigned char> vchTemp;
104-
if (!DecodeBase58Check(psz, vchTemp))
105-
{
106-
vchData.clear();
107-
nVersion = 0;
108-
return false;
109-
}
110-
nVersion = vchTemp[0];
111-
vchData.resize(vchTemp.size() - 1);
112-
if (!vchData.empty())
113-
memcpy(&vchData[0], &vchTemp[1], vchData.size());
114-
memory_cleanse(&vchTemp[0], vchTemp.size());
115-
return true;
116-
}
117-
118-
bool SetString(const std::string& str)
119-
{
120-
return SetString(str.c_str());
121-
}
122-
123-
std::string ToString() const
124-
{
125-
std::vector<unsigned char> vch(1, nVersion);
126-
vch.insert(vch.end(), vchData.begin(), vchData.end());
127-
return EncodeBase58Check(vch);
128-
}
129-
130-
int CompareTo(const CBase58Data& b58) const
131-
{
132-
if (nVersion < b58.nVersion) return -1;
133-
if (nVersion > b58.nVersion) return 1;
134-
if (vchData < b58.vchData) return -1;
135-
if (vchData > b58.vchData) return 1;
136-
return 0;
137-
}
138-
139-
bool operator==(const CBase58Data& b58) const { return CompareTo(b58) == 0; }
140-
bool operator<=(const CBase58Data& b58) const { return CompareTo(b58) <= 0; }
141-
bool operator>=(const CBase58Data& b58) const { return CompareTo(b58) >= 0; }
142-
bool operator< (const CBase58Data& b58) const { return CompareTo(b58) < 0; }
143-
bool operator> (const CBase58Data& b58) const { return CompareTo(b58) > 0; }
144-
};
145-
146-
/** base58-encoded addresses.
147-
* Public-key-hash-addresses have version 25 (or 111 testnet).
148-
* The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
149-
* Script-hash-addresses have version 85 (or 196 testnet).
150-
* The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script.
151-
*/
152-
class CBitcoinAddress;
153-
class CBitcoinAddressVisitor
154-
{
155-
private:
156-
CBitcoinAddress *addr;
157-
public:
158-
CBitcoinAddressVisitor(CBitcoinAddress *addrIn) : addr(addrIn) { }
159-
bool operator()(const CKeyID &id) const;
160-
bool operator()(const CScriptID &id) const;
161-
bool operator()(const CNoDestination &no) const;
162-
};
163-
164-
class CBitcoinAddress : public CBase58Data
165-
{
166-
public:
167-
enum
168-
{
169-
//Base58Gridcoin:
170-
PUBKEY_ADDRESS = 62, // Gridcoin Research addresses start with R - 62, (Classic Starts with G: 37)
171-
SCRIPT_ADDRESS = 85,
172-
PUBKEY_ADDRESS_TEST = 111,
173-
SCRIPT_ADDRESS_TEST = 196,
174-
};
175-
176-
bool Set(const CKeyID &id) {
177-
SetData(fTestNet ? PUBKEY_ADDRESS_TEST : PUBKEY_ADDRESS, &id, 20);
178-
return true;
179-
}
180-
181-
bool Set(const CScriptID &id) {
182-
SetData(fTestNet ? SCRIPT_ADDRESS_TEST : SCRIPT_ADDRESS, &id, 20);
183-
return true;
184-
}
185-
186-
bool Set(const CTxDestination &dest)
187-
{
188-
return std::visit(CBitcoinAddressVisitor(this), dest);
189-
}
190-
191-
bool IsValid() const
192-
{
193-
unsigned int nExpectedSize = 20;
194-
bool fExpectTestNet = false;
195-
switch(nVersion)
196-
{
197-
case PUBKEY_ADDRESS:
198-
nExpectedSize = 20; // Hash of public key
199-
fExpectTestNet = false;
200-
break;
201-
case SCRIPT_ADDRESS:
202-
nExpectedSize = 20; // Hash of CScript
203-
fExpectTestNet = false;
204-
break;
205-
206-
case PUBKEY_ADDRESS_TEST:
207-
nExpectedSize = 20;
208-
fExpectTestNet = true;
209-
break;
210-
case SCRIPT_ADDRESS_TEST:
211-
nExpectedSize = 20;
212-
fExpectTestNet = true;
213-
break;
214-
215-
default:
216-
return false;
217-
}
218-
return fExpectTestNet == fTestNet && vchData.size() == nExpectedSize;
219-
}
220-
221-
CBitcoinAddress()
222-
{
223-
}
224-
225-
CBitcoinAddress(const CTxDestination &dest)
226-
{
227-
Set(dest);
228-
}
229-
230-
CBitcoinAddress(const std::string& strAddress)
231-
{
232-
SetString(strAddress);
233-
}
234-
235-
CBitcoinAddress(const char* pszAddress)
236-
{
237-
SetString(pszAddress);
238-
}
239-
240-
CTxDestination Get() const {
241-
if (!IsValid())
242-
return CNoDestination();
243-
switch (nVersion) {
244-
case PUBKEY_ADDRESS:
245-
case PUBKEY_ADDRESS_TEST: {
246-
uint160 id;
247-
memcpy(&id, &vchData[0], 20);
248-
return CKeyID(id);
249-
}
250-
case SCRIPT_ADDRESS:
251-
case SCRIPT_ADDRESS_TEST: {
252-
uint160 id;
253-
memcpy(&id, &vchData[0], 20);
254-
return CScriptID(id);
255-
}
256-
}
257-
return CNoDestination();
258-
}
259-
260-
bool GetKeyID(CKeyID &keyID) const {
261-
if (!IsValid())
262-
return false;
263-
switch (nVersion) {
264-
case PUBKEY_ADDRESS:
265-
case PUBKEY_ADDRESS_TEST: {
266-
uint160 id;
267-
memcpy(&id, &vchData[0], 20);
268-
keyID = CKeyID(id);
269-
return true;
270-
}
271-
default: return false;
272-
}
273-
}
274-
275-
bool IsScript() const {
276-
if (!IsValid())
277-
return false;
278-
switch (nVersion) {
279-
case SCRIPT_ADDRESS:
280-
case SCRIPT_ADDRESS_TEST: {
281-
return true;
282-
}
283-
default: return false;
284-
}
285-
}
286-
};
287-
288-
bool inline CBitcoinAddressVisitor::operator()(const CKeyID &id) const { return addr->Set(id); }
289-
bool inline CBitcoinAddressVisitor::operator()(const CScriptID &id) const { return addr->Set(id); }
290-
bool inline CBitcoinAddressVisitor::operator()(const CNoDestination &id) const { return false; }
291-
292-
/** A base58-encoded secret key */
293-
class CBitcoinSecret : public CBase58Data
294-
{
295-
public:
296-
void SetSecret(const CSecret& vchSecret, bool fCompressed)
297-
{
298-
assert(vchSecret.size() == 32);
299-
SetData(128 + (fTestNet ? CBitcoinAddress::PUBKEY_ADDRESS_TEST : CBitcoinAddress::PUBKEY_ADDRESS), &vchSecret[0], vchSecret.size());
300-
if (fCompressed)
301-
vchData.push_back(1);
302-
}
303-
304-
CSecret GetSecret(bool &fCompressedOut)
305-
{
306-
CSecret vchSecret;
307-
vchSecret.resize(32);
308-
memcpy(&vchSecret[0], &vchData[0], 32);
309-
fCompressedOut = vchData.size() == 33;
310-
return vchSecret;
311-
}
312-
313-
bool IsValid() const
314-
{
315-
bool fExpectTestNet = false;
316-
switch(nVersion)
317-
{
318-
case (128 + CBitcoinAddress::PUBKEY_ADDRESS):
319-
break;
320-
321-
case (128 + CBitcoinAddress::PUBKEY_ADDRESS_TEST):
322-
fExpectTestNet = true;
323-
break;
324-
325-
default:
326-
return false;
327-
}
328-
return fExpectTestNet == fTestNet && (vchData.size() == 32 || (vchData.size() == 33 && vchData[32] == 1));
329-
}
330-
331-
bool SetString(const char* pszSecret)
332-
{
333-
return CBase58Data::SetString(pszSecret) && IsValid();
334-
}
335-
336-
bool SetString(const std::string& strSecret)
337-
{
338-
return SetString(strSecret.c_str());
339-
}
340-
341-
CBitcoinSecret(const CSecret& vchSecret, bool fCompressed)
342-
{
343-
SetSecret(vchSecret, fCompressed);
344-
}
345-
346-
CBitcoinSecret()
347-
{
348-
}
349-
};
350-
35163
#endif

src/chainparams.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class CMainParams : public CChainParams {
102102

103103
base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,62);
104104
base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,85);
105-
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1,0); // TODO: What should be the exact value here? 128 + PUBKEY_ADDRESS as 4 bytes?
105+
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1,190);
106106
base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x88, 0xB2, 0x1E};
107107
base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x88, 0xAD, 0xE4};
108108

@@ -213,7 +213,7 @@ class CTestNetParams : public CChainParams {
213213

214214
base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,111);
215215
base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,196);
216-
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1,0); // TODO: What should be the exact value here? 128 + PUBKEY_ADDRESS as 4 bytes?
216+
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1,239);
217217
base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x35, 0x87, 0xCF};
218218
base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x35, 0x83, 0x94};
219219

src/crypter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingM
114114
}
115115

116116

117-
bool EncryptSecret(CKeyingMaterial& vMasterKey, const CSecret &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
117+
bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
118118
{
119119
CCrypter cKeyCrypter;
120120
std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
@@ -124,7 +124,7 @@ bool EncryptSecret(CKeyingMaterial& vMasterKey, const CSecret &vchPlaintext, con
124124
return cKeyCrypter.Encrypt((CKeyingMaterial)vchPlaintext, vchCiphertext);
125125
}
126126

127-
bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CSecret& vchPlaintext)
127+
bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext)
128128
{
129129
CCrypter cKeyCrypter;
130130
std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);

src/crypter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ class CCrypter
120120
};
121121

122122

123-
bool EncryptSecret(CKeyingMaterial& vMasterKey, const CSecret &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext);
124-
bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char> &vchCiphertext, const uint256& nIV, CSecret &vchPlaintext);
123+
bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext);
124+
bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char> &vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext);
125125
bool DecryptKey(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key);
126126

127127
#endif // BITCOIN_CRYPTER_H

src/gridcoin/backup.cpp

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -336,29 +336,3 @@ bool GRC::MaintainBackups(fs::path wallet_backup_path, std::vector<std::string>
336336
return true;
337337
}
338338

339-
bool GRC::BackupPrivateKeys(const CWallet& wallet, std::string& sTarget, std::string& sErrors)
340-
{
341-
if (wallet.IsLocked() || fWalletUnlockStakingOnly)
342-
{
343-
sErrors = "Wallet needs to be fully unlocked to backup private keys.";
344-
return false;
345-
}
346-
fs::path PrivateKeysTarget = GetBackupFilename("keys.dat");
347-
fs::create_directories(PrivateKeysTarget.parent_path());
348-
sTarget = PrivateKeysTarget.string();
349-
fsbridge::ofstream myBackup;
350-
myBackup.open(PrivateKeysTarget);
351-
std::string sError;
352-
for(const auto& keyPair : wallet.GetAllPrivateKeys(sError))
353-
{
354-
if (!sError.empty())
355-
{
356-
sErrors = sError;
357-
return false;
358-
}
359-
myBackup << "Address: " << keyPair.first.ToString() << ", Secret: " << keyPair.second.ToString() << std::endl;
360-
}
361-
LogPrintf("BackupPrivateKeys: Backup made to %s", PrivateKeysTarget.string());
362-
myBackup.close();
363-
return true;
364-
}

src/gridcoin/backup.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ bool BackupConfigFile(const std::string& strDest);
2323
bool MaintainBackups(fs::path wallet_backup_path, std::vector<std::string> backup_file_type,
2424
unsigned int retention_by_num, unsigned int retention_by_days, std::vector<std::string>& files_removed);
2525
bool BackupWallet(const CWallet& wallet, const std::string& strDest);
26-
bool BackupPrivateKeys(const CWallet& wallet, std::string& sTarget, std::string& sErrors);
2726
}
2827

2928
#endif // GRIDCOIN_BACKUP_H

0 commit comments

Comments
 (0)