123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- // ECOin - Copyright (c) - 2014/2021 - GPLv3 - epsylon@riseup.net (https://03c8.net)
- #ifndef ECOIN_KEY_H
- #define ECOIN_KEY_H
- #include <stdexcept>
- #include <vector>
- #include "allocators.h"
- #include "serialize.h"
- #include "uint256.h"
- #include "util.h"
- #include <openssl/ec.h> // 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<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);
- )
- 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<unsigned char> Raw() const {
- return vchPubKey;
- }
- };
- typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
- typedef std::vector<unsigned char, secure_allocator<unsigned char> > 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<unsigned char>& vchSig);
- bool SignCompact(uint256 hash, std::vector<unsigned char>& vchSig);
- bool SetCompactSignature(uint256 hash, const std::vector<unsigned char>& vchSig);
- bool Verify(uint256 hash, const std::vector<unsigned char>& vchSig);
- bool VerifyCompact(uint256 hash, const std::vector<unsigned char>& vchSig);
- bool IsValid();
- };
- #endif
|