// ECOin - Copyright (c) - 2014/2021 - GPLv3 - epsylon@riseup.net (https://03c8.net) #ifndef ECOIN_KEY_H #define ECOIN_KEY_H #include #include #include "allocators.h" #include "serialize.h" #include "uint256.h" #include "util.h" #include // for EC_KEY definition class key_error : public std::runtime_error { public: explicit key_error(const std::string& str) : std::runtime_error(str) {} }; class CKeyID : public uint160 { public: CKeyID() : uint160(0) { } CKeyID(const uint160 &in) : uint160(in) { } }; class CScriptID : public uint160 { public: CScriptID() : uint160(0) { } CScriptID(const uint160 &in) : uint160(in) { } }; 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; } }; typedef std::vector > CPrivKey; typedef std::vector > CSecret; class CKey { protected: EC_KEY* pkey; bool fSet; bool fCompressedPubKey; void SetCompressedPubKey(); public: void Reset(); CKey(); CKey(const CKey& b); CKey& operator=(const CKey& b); ~CKey(); bool IsNull() const; bool IsCompressed() const; void MakeNewKey(bool fCompressed); bool SetPrivKey(const CPrivKey& vchPrivKey); bool SetSecret(const CSecret& vchSecret, bool fCompressed = false); CSecret GetSecret(bool &fCompressed) const; CPrivKey GetPrivKey() const; bool SetPubKey(const CPubKey& vchPubKey); CPubKey GetPubKey() const; bool Sign(uint256 hash, std::vector& vchSig); bool SignCompact(uint256 hash, std::vector& vchSig); bool SetCompactSignature(uint256 hash, const std::vector& vchSig); bool Verify(uint256 hash, const std::vector& vchSig); bool VerifyCompact(uint256 hash, const std::vector& vchSig); bool IsValid(); }; #endif