Skip to content

Commit eac71b5

Browse files
committed
Finish Encode/Decode destination functions move from base58 to key_io.
1 parent 2e9376c commit eac71b5

37 files changed

+163
-181
lines changed

src/base58.cpp

Lines changed: 1 addition & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66
#include "base58.h"
77

88
#include "hash.h"
9-
#include "script/script.h"
10-
#include "uint256.h"
119

12-
#include <boost/variant/apply_visitor.hpp>
13-
#include <boost/variant/static_visitor.hpp>
10+
#include "uint256.h"
1411

1512
#include <algorithm>
1613
#include <assert.h>
@@ -167,96 +164,3 @@ bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRe
167164
{
168165
return DecodeBase58Check(str.c_str(), vchRet, max_ret);
169166
}
170-
171-
namespace
172-
{
173-
class DestinationEncoder : public boost::static_visitor<std::string>
174-
{
175-
private:
176-
const CChainParams& m_params;
177-
const CChainParams::Base58Type m_addrType;
178-
179-
public:
180-
DestinationEncoder(const CChainParams& params, const CChainParams::Base58Type _addrType = CChainParams::PUBKEY_ADDRESS) : m_params(params), m_addrType(_addrType) {}
181-
182-
std::string operator()(const CKeyID& id) const
183-
{
184-
std::vector<unsigned char> data = m_params.Base58Prefix(m_addrType);
185-
data.insert(data.end(), id.begin(), id.end());
186-
return EncodeBase58Check(data);
187-
}
188-
189-
std::string operator()(const CScriptID& id) const
190-
{
191-
std::vector<unsigned char> data = m_params.Base58Prefix(CChainParams::SCRIPT_ADDRESS);
192-
data.insert(data.end(), id.begin(), id.end());
193-
return EncodeBase58Check(data);
194-
}
195-
196-
std::string operator()(const CNoDestination& no) const { return ""; }
197-
};
198-
199-
CTxDestination DecodeDestination(const std::string& str, const CChainParams& params, bool& isStaking)
200-
{
201-
std::vector<unsigned char> data;
202-
uint160 hash;
203-
if (DecodeBase58Check(str, data)) {
204-
// base58-encoded PIVX addresses.
205-
// Public-key-hash-addresses have version 30 (or 139 testnet).
206-
// The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
207-
const std::vector<unsigned char>& pubkey_prefix = params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
208-
if (data.size() == hash.size() + pubkey_prefix.size() && std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin())) {
209-
std::copy(data.begin() + pubkey_prefix.size(), data.end(), hash.begin());
210-
return CKeyID(hash);
211-
}
212-
// Public-key-hash-coldstaking-addresses have version 63 (or 73 testnet).
213-
const std::vector<unsigned char>& staking_prefix = params.Base58Prefix(CChainParams::STAKING_ADDRESS);
214-
if (data.size() == hash.size() + staking_prefix.size() && std::equal(staking_prefix.begin(), staking_prefix.end(), data.begin())) {
215-
isStaking = true;
216-
std::copy(data.begin() + staking_prefix.size(), data.end(), hash.begin());
217-
return CKeyID(hash);
218-
}
219-
// Script-hash-addresses have version 13 (or 19 testnet).
220-
// The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script.
221-
const std::vector<unsigned char>& script_prefix = params.Base58Prefix(CChainParams::SCRIPT_ADDRESS);
222-
if (data.size() == hash.size() + script_prefix.size() && std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) {
223-
std::copy(data.begin() + script_prefix.size(), data.end(), hash.begin());
224-
return CScriptID(hash);
225-
}
226-
}
227-
return CNoDestination();
228-
}
229-
230-
} // anon namespace
231-
232-
std::string EncodeDestination(const CTxDestination& dest, bool isStaking)
233-
{
234-
return EncodeDestination(dest, isStaking ? CChainParams::STAKING_ADDRESS : CChainParams::PUBKEY_ADDRESS);
235-
}
236-
237-
std::string EncodeDestination(const CTxDestination& dest, const CChainParams::Base58Type addrType)
238-
{
239-
return boost::apply_visitor(DestinationEncoder(Params(), addrType), dest);
240-
}
241-
242-
CTxDestination DecodeDestination(const std::string& str)
243-
{
244-
bool isStaking;
245-
return DecodeDestination(str, Params(), isStaking);
246-
}
247-
248-
CTxDestination DecodeDestination(const std::string& str, bool& isStaking)
249-
{
250-
return DecodeDestination(str, Params(), isStaking);
251-
}
252-
253-
bool IsValidDestinationString(const std::string& str, bool fStaking, const CChainParams& params)
254-
{
255-
bool isStaking = false;
256-
return IsValidDestination(DecodeDestination(str, params, isStaking)) && (isStaking == fStaking);
257-
}
258-
259-
bool IsValidDestinationString(const std::string& str, bool isStaking)
260-
{
261-
return IsValidDestinationString(str, isStaking, Params());
262-
}

src/base58.h

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -71,42 +71,4 @@ bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet, int
7171
*/
7272
bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet, int max_ret_len = std::numeric_limits<int>::max());
7373

74-
std::string EncodeDestination(const CTxDestination& dest, bool isStaking);
75-
std::string EncodeDestination(const CTxDestination& dest, const CChainParams::Base58Type addrType = CChainParams::PUBKEY_ADDRESS);
76-
// DecodeDestinationisStaking flag is set to true when the string arg is from an staking address
77-
CTxDestination DecodeDestination(const std::string& str, bool& isStaking);
78-
CTxDestination DecodeDestination(const std::string& str);
79-
80-
// 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).
81-
bool IsValidDestinationString(const std::string& str, bool fStaking);
82-
bool IsValidDestinationString(const std::string& str, bool fStaking, const CChainParams& params);
83-
84-
/**
85-
* Wrapper class for every supported address
86-
*/
87-
struct Destination {
88-
public:
89-
explicit Destination() {}
90-
explicit Destination(const CTxDestination& _dest, bool _isP2CS) : dest(_dest), isP2CS(_isP2CS) {}
91-
92-
CTxDestination dest{CNoDestination()};
93-
bool isP2CS{false};
94-
95-
Destination& operator=(const Destination& from)
96-
{
97-
this->dest = from.dest;
98-
this->isP2CS = from.isP2CS;
99-
return *this;
100-
}
101-
102-
std::string ToString()
103-
{
104-
if (!IsValidDestination(dest)) {
105-
// Invalid address
106-
return "";
107-
}
108-
return EncodeDestination(dest, isP2CS ? CChainParams::STAKING_ADDRESS : CChainParams::PUBKEY_ADDRESS);
109-
}
110-
};
111-
11274
#endif // BITCOIN_BASE58_H

src/bip38.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
#include "base58.h"
88
#include "crypto/aes.h"
9+
#include "key_io.h"
910
#include "hash.h"
1011
#include "pubkey.h"
11-
#include "util/system.h"
1212
#include "utilstrencodings.h"
1313
#include "random.h"
1414

src/core_write.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include "core_io.h"
77

8-
#include "base58.h"
8+
#include "key_io.h"
99
#include "primitives/transaction.h"
1010
#include "script/script.h"
1111
#include "script/standard.h"

src/destination_io.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// file COPYING or https://www.opensource.org/licenses/mit-license.php.
44

55
#include "destination_io.h"
6-
#include "base58.h"
6+
#include "key_io.h"
77
#include "sapling/key_io_sapling.h"
88

99
namespace Standard {

src/evo/deterministicmns.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
#include "evo/deterministicmns.h"
77

8-
#include "base58.h"
98
#include "chainparams.h"
109
#include "consensus/upgrades.h"
1110
#include "core_io.h"
1211
#include "evo/specialtx.h"
12+
#include "key_io.h"
1313
#include "guiinterface.h"
1414
#include "masternode.h" // for MasternodeCollateralMinConf
1515
#include "masternodeman.h" // for mnodeman (!TODO: remove)

src/evo/providertx.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55

66
#include "evo/providertx.h"
77

8-
#include "base58.h"
98
#include "core_io.h"
109
#include "evo/deterministicmns.h"
11-
#include "masternode.h" // MN_COLL_AMT
10+
#include "key_io.h"
1211
#include "messagesigner.h"
1312
#include "evo/specialtx.h"
1413
#include "tinyformat.h"

src/httprpc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
#include "httprpc.h"
77

8-
#include "base58.h"
98
#include "chainparams.h"
109
#include "crypto/hmac_sha256.h"
1110
#include "httpserver.h"
11+
#include "key_io.h"
1212
#include "rpc/protocol.h"
1313
#include "rpc/server.h"
1414
#include "random.h"

src/key_io.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,107 @@
55
#include "key_io.h"
66

77
#include "base58.h"
8+
#include "script/script.h"
89
#include <boost/variant/apply_visitor.hpp>
910
#include <boost/variant/static_visitor.hpp>
1011

1112
#include <assert.h>
1213
#include <string.h>
1314
#include <algorithm>
1415

16+
namespace
17+
{
18+
class DestinationEncoder : public boost::static_visitor<std::string>
19+
{
20+
private:
21+
const CChainParams& m_params;
22+
const CChainParams::Base58Type m_addrType;
23+
24+
public:
25+
DestinationEncoder(const CChainParams& params, const CChainParams::Base58Type _addrType = CChainParams::PUBKEY_ADDRESS) : m_params(params), m_addrType(_addrType) {}
26+
27+
std::string operator()(const CKeyID& id) const
28+
{
29+
std::vector<unsigned char> data = m_params.Base58Prefix(m_addrType);
30+
data.insert(data.end(), id.begin(), id.end());
31+
return EncodeBase58Check(data);
32+
}
33+
34+
std::string operator()(const CScriptID& id) const
35+
{
36+
std::vector<unsigned char> data = m_params.Base58Prefix(CChainParams::SCRIPT_ADDRESS);
37+
data.insert(data.end(), id.begin(), id.end());
38+
return EncodeBase58Check(data);
39+
}
40+
41+
std::string operator()(const CNoDestination& no) const { return ""; }
42+
};
43+
44+
CTxDestination DecodeDestination(const std::string& str, const CChainParams& params, bool& isStaking)
45+
{
46+
std::vector<unsigned char> data;
47+
uint160 hash;
48+
if (DecodeBase58Check(str, data)) {
49+
// base58-encoded PIVX addresses.
50+
// Public-key-hash-addresses have version 30 (or 139 testnet).
51+
// The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
52+
const std::vector<unsigned char>& pubkey_prefix = params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
53+
if (data.size() == hash.size() + pubkey_prefix.size() && std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin())) {
54+
std::copy(data.begin() + pubkey_prefix.size(), data.end(), hash.begin());
55+
return CKeyID(hash);
56+
}
57+
// Public-key-hash-coldstaking-addresses have version 63 (or 73 testnet).
58+
const std::vector<unsigned char>& staking_prefix = params.Base58Prefix(CChainParams::STAKING_ADDRESS);
59+
if (data.size() == hash.size() + staking_prefix.size() && std::equal(staking_prefix.begin(), staking_prefix.end(), data.begin())) {
60+
isStaking = true;
61+
std::copy(data.begin() + staking_prefix.size(), data.end(), hash.begin());
62+
return CKeyID(hash);
63+
}
64+
// Script-hash-addresses have version 13 (or 19 testnet).
65+
// The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script.
66+
const std::vector<unsigned char>& script_prefix = params.Base58Prefix(CChainParams::SCRIPT_ADDRESS);
67+
if (data.size() == hash.size() + script_prefix.size() && std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) {
68+
std::copy(data.begin() + script_prefix.size(), data.end(), hash.begin());
69+
return CScriptID(hash);
70+
}
71+
}
72+
return CNoDestination();
73+
}
74+
75+
} // anon namespace
76+
77+
std::string EncodeDestination(const CTxDestination& dest, bool isStaking)
78+
{
79+
return EncodeDestination(dest, isStaking ? CChainParams::STAKING_ADDRESS : CChainParams::PUBKEY_ADDRESS);
80+
}
81+
82+
std::string EncodeDestination(const CTxDestination& dest, const CChainParams::Base58Type addrType)
83+
{
84+
return boost::apply_visitor(DestinationEncoder(Params(), addrType), dest);
85+
}
86+
87+
CTxDestination DecodeDestination(const std::string& str)
88+
{
89+
bool isStaking;
90+
return DecodeDestination(str, Params(), isStaking);
91+
}
92+
93+
CTxDestination DecodeDestination(const std::string& str, bool& isStaking)
94+
{
95+
return DecodeDestination(str, Params(), isStaking);
96+
}
97+
98+
bool IsValidDestinationString(const std::string& str, bool fStaking, const CChainParams& params)
99+
{
100+
bool isStaking = false;
101+
return IsValidDestination(DecodeDestination(str, params, isStaking)) && (isStaking == fStaking);
102+
}
103+
104+
bool IsValidDestinationString(const std::string& str, bool isStaking)
105+
{
106+
return IsValidDestinationString(str, isStaking, Params());
107+
}
108+
15109
namespace KeyIO {
16110

17111
CKey DecodeSecret(const std::string &str) {

src/key_io.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@
1313

1414
#include <string>
1515

16+
std::string EncodeDestination(const CTxDestination& dest, bool isStaking);
17+
std::string EncodeDestination(const CTxDestination& dest, const CChainParams::Base58Type addrType = CChainParams::PUBKEY_ADDRESS);
18+
// DecodeDestinationisStaking flag is set to true when the string arg is from an staking address
19+
CTxDestination DecodeDestination(const std::string& str, bool& isStaking);
20+
CTxDestination DecodeDestination(const std::string& str);
21+
22+
// 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).
23+
bool IsValidDestinationString(const std::string& str, bool fStaking);
24+
bool IsValidDestinationString(const std::string& str, bool fStaking, const CChainParams& params);
25+
1626
namespace KeyIO {
1727

1828
CKey DecodeSecret(const std::string &str);
@@ -28,4 +38,32 @@ namespace KeyIO {
2838

2939
}
3040

41+
/**
42+
* Wrapper class for every supported address
43+
*/
44+
struct Destination {
45+
public:
46+
explicit Destination() {}
47+
explicit Destination(const CTxDestination& _dest, bool _isP2CS) : dest(_dest), isP2CS(_isP2CS) {}
48+
49+
CTxDestination dest{CNoDestination()};
50+
bool isP2CS{false};
51+
52+
Destination& operator=(const Destination& from)
53+
{
54+
this->dest = from.dest;
55+
this->isP2CS = from.isP2CS;
56+
return *this;
57+
}
58+
59+
std::string ToString()
60+
{
61+
if (!IsValidDestination(dest)) {
62+
// Invalid address
63+
return "";
64+
}
65+
return EncodeDestination(dest, isP2CS ? CChainParams::STAKING_ADDRESS : CChainParams::PUBKEY_ADDRESS);
66+
}
67+
};
68+
3169
#endif //PIVX_KEY_IO_H

0 commit comments

Comments
 (0)