Raven Core  3.0.0
P2P Digital Currency
base58.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin Core developers
3 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
15 #ifndef RAVEN_BASE58_H
16 #define RAVEN_BASE58_H
17 
18 #include "chainparams.h"
19 #include "key.h"
20 #include "pubkey.h"
21 #include "script/standard.h"
23 
24 #include <string>
25 #include <vector>
26 
31 std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend);
32 
36 std::string EncodeBase58(const std::vector<unsigned char>& vch);
37 
43 bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet);
44 
49 bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet);
50 
54 std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn);
55 
60 inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet);
61 
66 inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet);
67 
72 {
73 protected:
75  std::vector<unsigned char> vchVersion;
76 
78  typedef std::vector<unsigned char, zero_after_free_allocator<unsigned char> > vector_uchar;
79  vector_uchar vchData;
80 
81  CBase58Data();
82  void SetData(const std::vector<unsigned char> &vchVersionIn, const void* pdata, size_t nSize);
83  void SetData(const std::vector<unsigned char> &vchVersionIn, const unsigned char *pbegin, const unsigned char *pend);
84 
85 public:
86  bool SetString(const char* psz, unsigned int nVersionBytes = 1);
87  bool SetString(const std::string& str);
88  std::string ToString() const;
89  int CompareTo(const CBase58Data& b58) const;
90 
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  bool operator> (const CBase58Data& b58) const { return CompareTo(b58) > 0; }
96 };
103 class CRavenAddress : public CBase58Data {
104 public:
105  bool Set(const CKeyID &id);
106  bool Set(const CScriptID &id);
107  bool Set(const CTxDestination &dest);
108  bool IsValid() const;
109  bool IsValid(const CChainParams &params) const;
110 
112  CRavenAddress(const CTxDestination &dest) { Set(dest); }
113  CRavenAddress(const std::string& strAddress) { SetString(strAddress); }
114  CRavenAddress(const char* pszAddress) { SetString(pszAddress); }
115 
116  CTxDestination Get() const;
117  bool GetIndexKey(uint160& hashBytes, int& type) const;
118 };
119 
123 class CRavenSecret : public CBase58Data
124 {
125 public:
126  void SetKey(const CKey& vchSecret);
127  CKey GetKey();
128  bool IsValid() const;
129  bool SetString(const char* pszSecret);
130  bool SetString(const std::string& strSecret);
131 
132  CRavenSecret(const CKey& vchSecret) { SetKey(vchSecret); }
134 };
135 
136 template<typename K, int Size, CChainParams::Base58Type Type> class CRavenExtKeyBase : public CBase58Data
137 {
138 public:
139  void SetKey(const K &key) {
140  unsigned char vch[Size];
141  key.Encode(vch);
142  SetData(Params().Base58Prefix(Type), vch, vch+Size);
143  }
144 
145  K GetKey() {
146  K ret;
147  if (vchData.size() == Size) {
148  // If base58 encoded data does not hold an ext key, return a !IsValid() key
149  ret.Decode(vchData.data());
150  }
151  return ret;
152  }
153 
154  CRavenExtKeyBase(const K &key) {
155  SetKey(key);
156  }
157 
158  CRavenExtKeyBase(const std::string& strBase58c) {
159  SetString(strBase58c.c_str(), Params().Base58Prefix(Type).size());
160  }
161 
163 };
164 
167 
168 std::string EncodeDestination(const CTxDestination& dest);
169 CTxDestination DecodeDestination(const std::string& str);
170 bool IsValidDestinationString(const std::string& str);
171 bool IsValidDestinationString(const std::string& str, const CChainParams& params);
172 
173 #endif // RAVEN_BASE58_H
void SetKey(const K &key)
Definition: base58.h:139
bool IsValidDestinationString(const std::string &str)
Definition: base58.cpp:343
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:89
CRavenSecret()
Definition: base58.h:133
CRavenAddress()
Definition: base58.h:111
bool DecodeBase58(const char *psz, std::vector< unsigned char > &vchRet)
Decode a base58-encoded string (psz) into a byte vector (vchRet).
Definition: base58.cpp:22
vector_uchar vchData
Definition: base58.h:79
int CompareTo(const CBase58Data &b58) const
Definition: base58.cpp:201
CChainParams defines various tweakable parameters of a given instance of the Raven system...
Definition: chainparams.h:48
bool DecodeBase58Check(const char *psz, std::vector< unsigned char > &vchRet)
Decode a base58-encoded string (psz) that includes a checksum into a byte vector (vchRet), return true if decoding is successful.
Definition: base58.cpp:131
void SetData(const std::vector< unsigned char > &vchVersionIn, const void *pdata, size_t nSize)
Definition: base58.cpp:159
std::string ToString() const
Definition: base58.cpp:194
CRavenExtKeyBase(const std::string &strBase58c)
Definition: base58.h:158
std::vector< unsigned char, zero_after_free_allocator< unsigned char > > vector_uchar
the actually encoded data
Definition: base58.h:78
Base class for all base58-encoded data.
Definition: base58.h:71
bool operator<=(const CBase58Data &b58) const
Definition: base58.h:92
bool operator==(const CBase58Data &b58) const
Definition: base58.h:91
base58-encoded Raven addresses.
Definition: base58.h:103
bool operator>=(const CBase58Data &b58) const
Definition: base58.h:93
CRavenExtKeyBase(const K &key)
Definition: base58.h:154
CRavenSecret(const CKey &vchSecret)
Definition: base58.h:132
bool SetString(const char *psz, unsigned int nVersionBytes=1)
Definition: base58.cpp:172
CRavenAddress(const CTxDestination &dest)
Definition: base58.h:112
std::string EncodeBase58Check(const std::vector< unsigned char > &vchIn)
Encode a byte vector into a base58-encoded string, including checksum.
Definition: base58.cpp:122
CRavenExtKeyBase< CExtKey, BIP32_EXTKEY_SIZE, CChainParams::EXT_SECRET_KEY > CRavenExtKey
Definition: base58.h:165
std::string EncodeDestination(const CTxDestination &dest)
Definition: base58.cpp:326
CRavenAddress(const std::string &strAddress)
Definition: base58.h:113
CRavenExtKeyBase< CExtPubKey, BIP32_EXTKEY_SIZE, CChainParams::EXT_PUBLIC_KEY > CRavenExtPubKey
Definition: base58.h:166
const CChainParams & Params()
Return the currently selected parameters.
A base58-encoded secret key.
Definition: base58.h:123
CTxDestination DecodeDestination(const std::string &str)
Definition: base58.cpp:333
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:30
160-bit opaque blob.
Definition: uint256.h:112
std::string EncodeBase58(const unsigned char *pbegin, const unsigned char *pend)
Why base-58 instead of standard base-64 encoding?
Definition: base58.cpp:72
bool operator>(const CBase58Data &b58) const
Definition: base58.h:95
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:23
An encapsulated private key.
Definition: key.h:36
bool operator<(const CBase58Data &b58) const
Definition: base58.h:94
std::vector< unsigned char > vchVersion
the version byte(s)
Definition: base58.h:75
CRavenAddress(const char *pszAddress)
Definition: base58.h:114
const std::vector< unsigned char > & Base58Prefix(Base58Type type) const
Definition: chainparams.h:77