Fabcoin Core  0.16.2
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 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
14 #ifndef FABCOIN_BASE58_H
15 #define FABCOIN_BASE58_H
16 
17 #include <chainparams.h>
18 #include <key.h>
19 #include <pubkey.h>
20 #include <script/script.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 };
97 
104 class CFabcoinAddress : public CBase58Data {
105 public:
106  bool Set(const CKeyID &id);
107  bool Set(const CScriptID &id);
108  bool Set(const CTxDestination &dest);
109  bool IsValid() const;
110  bool IsValid(const CChainParams &params) const;
111 
113  CFabcoinAddress(const CTxDestination &dest) { Set(dest); }
114  CFabcoinAddress(const std::string& strAddress) { SetString(strAddress); }
115  CFabcoinAddress(const char* pszAddress) { SetString(pszAddress); }
116 
117  CTxDestination Get() const;
118  bool GetKeyID(CKeyID &keyID) const;
119  bool IsScript() const;
120  bool IsPubKeyHash() const;
121 };
122 
127 {
128 public:
129  void SetKey(const CKey& vchSecret);
130  CKey GetKey();
131  bool IsValid() const;
132  bool SetString(const char* pszSecret);
133  bool SetString(const std::string& strSecret);
134 
135  CFabcoinSecret(const CKey& vchSecret) { SetKey(vchSecret); }
137 };
138 
139 template<typename K, int Size, CChainParams::Base58Type Type> class CFabcoinExtKeyBase : public CBase58Data
140 {
141 public:
142  void SetKey(const K &key) {
143  unsigned char vch[Size];
144  key.Encode(vch);
145  SetData(Params().Base58Prefix(Type), vch, vch+Size);
146  }
147 
148  K GetKey() {
149  K ret;
150  if (vchData.size() == Size) {
151  // If base58 encoded data does not hold an ext key, return a !IsValid() key
152  ret.Decode(vchData.data());
153  }
154  return ret;
155  }
156 
157  CFabcoinExtKeyBase(const K &key) {
158  SetKey(key);
159  }
160 
161  CFabcoinExtKeyBase(const std::string& strBase58c) {
162  SetString(strBase58c.c_str(), Params().Base58Prefix(Type).size());
163  }
164 
166 };
167 
170 
171 #endif // FABCOIN_BASE58_H
A base58-encoded secret key.
Definition: base58.h:126
CFabcoinSecret(const CKey &vchSecret)
Definition: base58.h:135
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:79
bool DecodeBase58(const char *psz, std::vector< unsigned char > &vchRet)
Decode a base58-encoded string (psz) into a byte vector (vchRet).
Definition: base58.cpp:21
base58-encoded Fabcoin addresses.
Definition: base58.h:104
vector_uchar vchData
Definition: base58.h:79
CChainParams defines various tweakable parameters of a given instance of the Fabcoin system...
Definition: chainparams.h:47
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:130
void SetData(const std::vector< unsigned char > &vchVersionIn, const void *pdata, size_t nSize)
Definition: base58.cpp:158
const std::vector< unsigned char > & Base58Prefix(Base58Type type) const
Definition: chainparams.h:80
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
CFabcoinExtKeyBase(const K &key)
Definition: base58.h:157
bool operator==(const CBase58Data &b58) const
Definition: base58.h:91
std::string ToString() const
Definition: base58.cpp:193
CFabcoinAddress(const std::string &strAddress)
Definition: base58.h:114
bool operator>(const CBase58Data &b58) const
Definition: base58.h:95
void SetKey(const K &key)
Definition: base58.h:142
bool SetString(const char *psz, unsigned int nVersionBytes=1)
Definition: base58.cpp:171
std::string EncodeBase58Check(const std::vector< unsigned char > &vchIn)
Encode a byte vector into a base58-encoded string, including checksum.
Definition: base58.cpp:121
CFabcoinExtKeyBase< CExtPubKey, BIP32_EXTKEY_SIZE, CChainParams::EXT_PUBLIC_KEY > CFabcoinExtPubKey
Definition: base58.h:169
const CChainParams & Params()
Return the currently selected parameters.
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:29
std::string EncodeBase58(const unsigned char *pbegin, const unsigned char *pend)
Why base-58 instead of standard base-64 encoding?
Definition: base58.cpp:71
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:28
bool operator<(const CBase58Data &b58) const
Definition: base58.h:94
CFabcoinAddress(const char *pszAddress)
Definition: base58.h:115
An encapsulated private key.
Definition: key.h:35
CFabcoinAddress(const CTxDestination &dest)
Definition: base58.h:113
CFabcoinExtKeyBase(const std::string &strBase58c)
Definition: base58.h:161
std::vector< unsigned char > vchVersion
the version byte(s)
Definition: base58.h:75
bool operator<=(const CBase58Data &b58) const
Definition: base58.h:92
CFabcoinExtKeyBase< CExtKey, BIP32_EXTKEY_SIZE, CChainParams::EXT_SECRET_KEY > CFabcoinExtKey
Definition: base58.h:168
CFabcoinSecret()
Definition: base58.h:136
int CompareTo(const CBase58Data &b58) const
Definition: base58.cpp:200
bool operator>=(const CBase58Data &b58) const
Definition: base58.h:93