Line data Source code
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 : 15 : enum MessageVersion { 16 : MESS_VER_STRMESS = 0, // old format 17 : MESS_VER_HASH = 1, 18 : }; 19 : 20 : /** Helper class for signing messages and checking their signatures 21 : */ 22 : class CMessageSigner 23 : { 24 : public: 25 : /// Set the private/public key values, returns true if successful 26 : static bool GetKeysFromSecret(const std::string& strSecret, CKey& keyRet, CPubKey& pubkeyRet); 27 : static bool GetKeysFromSecret(const std::string& strSecret, CKey& keyRet, CKeyID& keyIDRet); 28 : /// Get the hash based on the input message 29 : static uint256 GetMessageHash(const std::string& strMessage); 30 : /// Sign the message, returns true if successful 31 : static bool SignMessage(const std::string& strMessage, std::vector<unsigned char>& vchSigRet, const CKey& key); 32 : /// Sign the message with BLS key, returns true if successful 33 : static bool SignMessage(const std::string& strMessage, std::vector<unsigned char>& vchSigRet, const CBLSSecretKey& key); 34 : /// Verify the message signature, returns true if successful 35 : static bool VerifyMessage(const CPubKey& pubkey, const std::vector<unsigned char>& vchSig, const std::string& strMessage, std::string& strErrorRet); 36 : /// Verify the message signature, returns true if successful 37 : static bool VerifyMessage(const CKeyID& keyID, const std::vector<unsigned char>& vchSig, const std::string& strMessage, std::string& strErrorRet); 38 : /// Verify the message BLS signature, returns true if successful 39 : static bool VerifyMessage(const CBLSPublicKey& pk, const std::vector<unsigned char>& vchSig, const std::string& strMessage); 40 : }; 41 : 42 : /** Helper class for signing hashes and checking their signatures 43 : */ 44 : class CHashSigner 45 : { 46 : public: 47 : /// Sign the hash, returns true if successful 48 : static bool SignHash(const uint256& hash, const CKey& key, std::vector<unsigned char>& vchSigRet); 49 : /// Sign the hash with BLS key, returns true if successful 50 : static bool SignHash(const uint256& hash, const CBLSSecretKey& key, std::vector<unsigned char>& vchSigRet); 51 : /// Verify the hash signature, returns true if successful 52 : static bool VerifyHash(const uint256& hash, const CPubKey& pubkey, const std::vector<unsigned char>& vchSig, std::string& strErrorRet); 53 : /// Verify the hash signature, returns true if successful 54 : static bool VerifyHash(const uint256& hash, const CKeyID& keyID, const std::vector<unsigned char>& vchSig, std::string& strErrorRet); 55 : /// Verify the hash BLS signature, returns true if successful 56 : static bool VerifyHash(const uint256& hash, const CBLSPublicKey& pk, const std::vector<unsigned char>& vchSig); 57 : }; 58 : 59 : /** Base Class for all signed messages on the network 60 : */ 61 : 62 24933 : class CSignedMessage 63 : { 64 : protected: 65 : std::vector<unsigned char> vchSig; 66 : 67 : public: 68 : int nMessVersion; 69 : 70 84305 : CSignedMessage() : 71 : vchSig(), 72 83841 : nMessVersion(MessageVersion::MESS_VER_HASH) 73 : {} 74 9135 : 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 2 : void SetVchSig(const std::vector<unsigned char>& vchSigIn) { vchSig = vchSigIn; } 88 1 : 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