X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fkey.h;h=e329cf04effb72986fc6a2a87ee15f13490d2455;hb=42c55f2e8a14a2f1c504cfe62e3d1717e8ac5454;hp=bd58c843754382c8339f158415c2c998e6dc9b01;hpb=23e7583a8c9a0dcee9cbbf3be8bfc453298773f0;p=novacoin.git diff --git a/src/key.h b/src/key.h index bd58c84..e329cf0 100644 --- a/src/key.h +++ b/src/key.h @@ -9,7 +9,10 @@ #include #include "allocators.h" +#include "serialize.h" #include "uint256.h" +#include "hash.h" +#include "bignum.h" #include // for EC_KEY definition @@ -42,8 +45,62 @@ public: explicit key_error(const std::string& str) : std::runtime_error(str) {} }; +/** A reference to a CKey: the Hash160 of its serialized public key */ +class CKeyID : public uint160 +{ +public: + CKeyID() : uint160(0) { } + CKeyID(const uint160 &in) : uint160(in) { } +}; + +/** A reference to a CScript: the Hash160 of its serialization (see script.h) */ +class CScriptID : public uint160 +{ +public: + CScriptID() : uint160(0) { } + CScriptID(const uint160 &in) : uint160(in) { } +}; -// secure_allocator is defined in serialize.h +/** An encapsulated public key. */ +class CPubKey { +private: + std::vector vchPubKey; + friend class CKey; + +public: + CPubKey() { } + CPubKey(const std::vector &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); + ) + + CKeyID GetID() const { + return CKeyID(Hash160(vchPubKey)); + } + + uint256 GetHash() const { + return Hash(vchPubKey.begin(), vchPubKey.end()); + } + + bool IsValid() const { + return vchPubKey.size() == 33 || vchPubKey.size() == 65; + } + + bool IsCompressed() const { + return vchPubKey.size() == 33; + } + + std::vector Raw() const { + return vchPubKey; + } +}; + + +// secure_allocator is defined in allocators.h // CPrivKey is a serialized private key, with all parameters included (279 bytes) typedef std::vector > CPrivKey; // CSecret is a serialization of just the secret parameter (32 bytes) @@ -78,8 +135,8 @@ public: bool SetSecret(const CSecret& vchSecret, bool fCompressed = false); CSecret GetSecret(bool &fCompressed) const; CPrivKey GetPrivKey() const; - bool SetPubKey(const std::vector& vchPubKey); - std::vector GetPubKey() const; + bool SetPubKey(const CPubKey& vchPubKey); + CPubKey GetPubKey() const; bool Sign(uint256 hash, std::vector& vchSig); @@ -101,6 +158,12 @@ public: bool VerifyCompact(uint256 hash, const std::vector& vchSig); bool IsValid(); + + // Check whether an element of a signature (r or s) is valid. + static bool CheckSignatureElement(const unsigned char *vch, int len, bool half); + + // Reserialize to DER + static bool ReserealizeSignature(std::vector& vchSig); }; #endif