key.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #ifndef ECOIN_KEY_H
  3. #define ECOIN_KEY_H
  4. #include <stdexcept>
  5. #include <vector>
  6. #include "allocators.h"
  7. #include "serialize.h"
  8. #include "uint256.h"
  9. #include "util.h"
  10. #include <openssl/ec.h> // for EC_KEY definition
  11. class key_error : public std::runtime_error
  12. {
  13. public:
  14. explicit key_error(const std::string& str) : std::runtime_error(str) {}
  15. };
  16. class CKeyID : public uint160
  17. {
  18. public:
  19. CKeyID() : uint160(0) { }
  20. CKeyID(const uint160 &in) : uint160(in) { }
  21. };
  22. class CScriptID : public uint160
  23. {
  24. public:
  25. CScriptID() : uint160(0) { }
  26. CScriptID(const uint160 &in) : uint160(in) { }
  27. };
  28. class CPubKey {
  29. private:
  30. std::vector<unsigned char> vchPubKey;
  31. friend class CKey;
  32. public:
  33. CPubKey() { }
  34. CPubKey(const std::vector<unsigned char> &vchPubKeyIn) : vchPubKey(vchPubKeyIn) { }
  35. friend bool operator==(const CPubKey &a, const CPubKey &b) { return a.vchPubKey == b.vchPubKey; }
  36. friend bool operator!=(const CPubKey &a, const CPubKey &b) { return a.vchPubKey != b.vchPubKey; }
  37. friend bool operator<(const CPubKey &a, const CPubKey &b) { return a.vchPubKey < b.vchPubKey; }
  38. IMPLEMENT_SERIALIZE(
  39. READWRITE(vchPubKey);
  40. )
  41. CKeyID GetID() const {
  42. return CKeyID(Hash160(vchPubKey));
  43. }
  44. uint256 GetHash() const {
  45. return Hash(vchPubKey.begin(), vchPubKey.end());
  46. }
  47. bool IsValid() const {
  48. return vchPubKey.size() == 33 || vchPubKey.size() == 65;
  49. }
  50. bool IsCompressed() const {
  51. return vchPubKey.size() == 33;
  52. }
  53. std::vector<unsigned char> Raw() const {
  54. return vchPubKey;
  55. }
  56. };
  57. typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
  58. typedef std::vector<unsigned char, secure_allocator<unsigned char> > CSecret;
  59. class CKey
  60. {
  61. protected:
  62. EC_KEY* pkey;
  63. bool fSet;
  64. bool fCompressedPubKey;
  65. void SetCompressedPubKey();
  66. public:
  67. void Reset();
  68. CKey();
  69. CKey(const CKey& b);
  70. CKey& operator=(const CKey& b);
  71. ~CKey();
  72. bool IsNull() const;
  73. bool IsCompressed() const;
  74. void MakeNewKey(bool fCompressed);
  75. bool SetPrivKey(const CPrivKey& vchPrivKey);
  76. bool SetSecret(const CSecret& vchSecret, bool fCompressed = false);
  77. CSecret GetSecret(bool &fCompressed) const;
  78. CPrivKey GetPrivKey() const;
  79. bool SetPubKey(const CPubKey& vchPubKey);
  80. CPubKey GetPubKey() const;
  81. bool Sign(uint256 hash, std::vector<unsigned char>& vchSig);
  82. bool SignCompact(uint256 hash, std::vector<unsigned char>& vchSig);
  83. bool SetCompactSignature(uint256 hash, const std::vector<unsigned char>& vchSig);
  84. bool Verify(uint256 hash, const std::vector<unsigned char>& vchSig);
  85. bool VerifyCompact(uint256 hash, const std::vector<unsigned char>& vchSig);
  86. bool IsValid();
  87. };
  88. #endif