aboutsummaryrefslogtreecommitdiff
path: root/src/key.h
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2012-05-14 19:07:52 +0200
committerPieter Wuille <pieter.wuille@gmail.com>2012-05-24 19:58:12 +0200
commitfd61d6f5068cf92d34569862b4225f177049a4f0 (patch)
tree5556094bda3a7162535b9352b67016e620bb9a77 /src/key.h
parentf04017f702e36563b9ba05b3fede216767de580a (diff)
downloadbitcoin-fd61d6f5068cf92d34569862b4225f177049a4f0.tar.xz
Encapsulate public keys in CPubKey
Diffstat (limited to 'src/key.h')
-rw-r--r--src/key.h39
1 files changed, 37 insertions, 2 deletions
diff --git a/src/key.h b/src/key.h
index bd58c84375..7c3e9d4421 100644
--- a/src/key.h
+++ b/src/key.h
@@ -9,7 +9,9 @@
#include <vector>
#include "allocators.h"
+#include "serialize.h"
#include "uint256.h"
+#include "util.h"
#include <openssl/ec.h> // for EC_KEY definition
@@ -42,6 +44,39 @@ public:
explicit key_error(const std::string& str) : std::runtime_error(str) {}
};
+class CPubKey {
+private:
+ std::vector<unsigned char> vchPubKey;
+ friend class CKey;
+
+public:
+ CPubKey() { }
+ CPubKey(const std::vector<unsigned char> &vchPubKeyIn) : vchPubKey(vchPubKeyIn) { }
+ friend bool operator==(const CPubKey &a, const CPubKey &b) { return a.vchPubKey == b.vchPubKey; }
+ friend bool operator!=(const CPubKey &a, const CPubKey &b) { return a.vchPubKey != b.vchPubKey; }
+ friend bool operator<(const CPubKey &a, const CPubKey &b) { return a.vchPubKey < b.vchPubKey; }
+
+ IMPLEMENT_SERIALIZE(
+ READWRITE(vchPubKey);
+ )
+
+ uint160 GetID() const {
+ return Hash160(vchPubKey);
+ }
+
+ uint256 GetHash() const {
+ return Hash(vchPubKey.begin(), vchPubKey.end());
+ }
+
+ bool IsValid() const {
+ return vchPubKey.size() == 33 || vchPubKey.size() == 65;
+ }
+
+ std::vector<unsigned char> Raw() const {
+ return vchPubKey;
+ }
+};
+
// secure_allocator is defined in serialize.h
// CPrivKey is a serialized private key, with all parameters included (279 bytes)
@@ -78,8 +113,8 @@ public:
bool SetSecret(const CSecret& vchSecret, bool fCompressed = false);
CSecret GetSecret(bool &fCompressed) const;
CPrivKey GetPrivKey() const;
- bool SetPubKey(const std::vector<unsigned char>& vchPubKey);
- std::vector<unsigned char> GetPubKey() const;
+ bool SetPubKey(const CPubKey& vchPubKey);
+ CPubKey GetPubKey() const;
bool Sign(uint256 hash, std::vector<unsigned char>& vchSig);