PIVX Core  5.6.99
P2P Digital Currency
messagesigner.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2018 The Dash Core developers
2 // Copyright (c) 2018-2021 The PIVX Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef PIVX_MESSAGESIGNER_H
7 #define PIVX_MESSAGESIGNER_H
8 
9 #include "key.h"
10 #include "primitives/transaction.h" // for CTxIn
11 
12 class CBLSPublicKey;
13 class CBLSSecretKey;
14 
16  MESS_VER_STRMESS = 0, // old format
18 };
19 
23 {
24 public:
26  static bool GetKeysFromSecret(const std::string& strSecret, CKey& keyRet, CPubKey& pubkeyRet);
27  static bool GetKeysFromSecret(const std::string& strSecret, CKey& keyRet, CKeyID& keyIDRet);
29  static uint256 GetMessageHash(const std::string& strMessage);
31  static bool SignMessage(const std::string& strMessage, std::vector<unsigned char>& vchSigRet, const CKey& key);
33  static bool SignMessage(const std::string& strMessage, std::vector<unsigned char>& vchSigRet, const CBLSSecretKey& key);
35  static bool VerifyMessage(const CPubKey& pubkey, const std::vector<unsigned char>& vchSig, const std::string& strMessage, std::string& strErrorRet);
37  static bool VerifyMessage(const CKeyID& keyID, const std::vector<unsigned char>& vchSig, const std::string& strMessage, std::string& strErrorRet);
39  static bool VerifyMessage(const CBLSPublicKey& pk, const std::vector<unsigned char>& vchSig, const std::string& strMessage);
40 };
41 
45 {
46 public:
48  static bool SignHash(const uint256& hash, const CKey& key, std::vector<unsigned char>& vchSigRet);
50  static bool SignHash(const uint256& hash, const CBLSSecretKey& key, std::vector<unsigned char>& vchSigRet);
52  static bool VerifyHash(const uint256& hash, const CPubKey& pubkey, const std::vector<unsigned char>& vchSig, std::string& strErrorRet);
54  static bool VerifyHash(const uint256& hash, const CKeyID& keyID, const std::vector<unsigned char>& vchSig, std::string& strErrorRet);
56  static bool VerifyHash(const uint256& hash, const CBLSPublicKey& pk, const std::vector<unsigned char>& vchSig);
57 };
58 
63 {
64 protected:
65  std::vector<unsigned char> vchSig;
66 
67 public:
69 
71  vchSig(),
73  {}
74  virtual ~CSignedMessage() {};
75 
76  // Sign-Verify message
77  bool Sign(const CKey& key, const CKeyID& keyID);
78  bool Sign(const std::string strSignKey);
79  bool CheckSignature(const CKeyID& keyID) const;
80 
81  // Pure virtual functions (used in Sign-Verify functions)
82  // Must be implemented in child classes
83  virtual uint256 GetSignatureHash() const = 0;
84  virtual std::string GetStrMessage() const = 0;
85 
86  // Setters and getters
87  void SetVchSig(const std::vector<unsigned char>& vchSigIn) { vchSig = vchSigIn; }
88  std::vector<unsigned char> GetVchSig() const { return vchSig; }
89  std::string GetSignatureBase64() const;
90 
91  // Sign-Verify with BLS
92  bool Sign(const CBLSSecretKey& sk);
93  bool CheckSignature(const CBLSPublicKey& pk) const;
94 };
95 
96 #endif // PIVX_MESSAGESIGNER_H
Helper class for signing hashes and checking their signatures.
Definition: messagesigner.h:45
static bool SignHash(const uint256 &hash, const CKey &key, std::vector< unsigned char > &vchSigRet)
Sign the hash, returns true if successful.
static bool VerifyHash(const uint256 &hash, const CPubKey &pubkey, const std::vector< unsigned char > &vchSig, std::string &strErrorRet)
Verify the hash signature, returns true if successful.
An encapsulated private key.
Definition: key.h:30
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:21
Helper class for signing messages and checking their signatures.
Definition: messagesigner.h:23
static bool SignMessage(const std::string &strMessage, std::vector< unsigned char > &vchSigRet, const CKey &key)
Sign the message, returns true if successful.
static bool VerifyMessage(const CPubKey &pubkey, const std::vector< unsigned char > &vchSig, const std::string &strMessage, std::string &strErrorRet)
Verify the message signature, returns true if successful.
static bool GetKeysFromSecret(const std::string &strSecret, CKey &keyRet, CPubKey &pubkeyRet)
Set the private/public key values, returns true if successful.
static uint256 GetMessageHash(const std::string &strMessage)
Get the hash based on the input message.
An encapsulated public key.
Definition: pubkey.h:44
Base Class for all signed messages on the network.
Definition: messagesigner.h:63
bool Sign(const CKey &key, const CKeyID &keyID)
CSignedMessage Class Functions inherited by network signed-messages.
bool CheckSignature(const CKeyID &keyID) const
virtual std::string GetStrMessage() const =0
std::vector< unsigned char > GetVchSig() const
Definition: messagesigner.h:88
virtual ~CSignedMessage()
Definition: messagesigner.h:74
virtual uint256 GetSignatureHash() const =0
std::string GetSignatureBase64() const
std::vector< unsigned char > vchSig
Definition: messagesigner.h:65
void SetVchSig(const std::vector< unsigned char > &vchSigIn)
Definition: messagesigner.h:87
256-bit opaque blob.
Definition: uint256.h:138
MessageVersion
Definition: messagesigner.h:15
@ MESS_VER_STRMESS
Definition: messagesigner.h:16
@ MESS_VER_HASH
Definition: messagesigner.h:17