forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 717
/
messagesigner.h
85 lines (72 loc) · 3.25 KB
/
messagesigner.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) 2014-2018 The Dash Core developers
// Copyright (c) 2018-2020 The PIVX developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef MESSAGESIGNER_H
#define MESSAGESIGNER_H
#include "key.h"
#include "primitives/transaction.h" // for CTxIn
enum MessageVersion {
MESS_VER_STRMESS = 0,
MESS_VER_HASH = 1,
};
/** Helper class for signing messages and checking their signatures
*/
class CMessageSigner
{
public:
/// Set the private/public key values, returns true if successful
static bool GetKeysFromSecret(const std::string& strSecret, CKey& keyRet, CPubKey& pubkeyRet);
/// Get the hash based on the input message
static uint256 GetMessageHash(const std::string& strMessage);
/// Sign the message, returns true if successful
static bool SignMessage(const std::string& strMessage, std::vector<unsigned char>& vchSigRet, const CKey& key);
/// Verify the message signature, 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 VerifyMessage(const CKeyID& keyID, const std::vector<unsigned char>& vchSig, const std::string& strMessage, std::string& strErrorRet);
};
/** Helper class for signing hashes and checking their signatures
*/
class CHashSigner
{
public:
/// Sign the hash, returns true if successful
static bool SignHash(const uint256& hash, const CKey& key, std::vector<unsigned char>& vchSigRet);
/// Verify the hash signature, 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
static bool VerifyHash(const uint256& hash, const CKeyID& keyID, const std::vector<unsigned char>& vchSig, std::string& strErrorRet);
};
/** Base Class for all signed messages on the network
*/
class CSignedMessage
{
protected:
std::vector<unsigned char> vchSig;
public:
int nMessVersion;
CSignedMessage() :
vchSig(),
nMessVersion(MessageVersion::MESS_VER_HASH)
{}
virtual ~CSignedMessage() {};
// Sign-Verify message
bool Sign(const CKey& key, const CPubKey& pubKey);
bool Sign(const std::string strSignKey);
bool CheckSignature(const CPubKey& pubKey) const;
bool CheckSignature() const;
// Pure virtual functions (used in Sign-Verify functions)
// Must be implemented in child classes
virtual uint256 GetSignatureHash() const = 0;
virtual std::string GetStrMessage() const = 0;
virtual const CTxIn GetVin() const = 0;
// GetPublicKey defaults to public key of masternode with vin from GetVin.
// Child classes can override if public key is directly accessible.
virtual const CPubKey GetPublicKey(std::string& strErrorRet) const;
// Setters and getters
void SetVchSig(const std::vector<unsigned char>& vchSigIn) { vchSig = vchSigIn; }
std::vector<unsigned char> GetVchSig() const { return vchSig; }
std::string GetSignatureBase64() const;
};
#endif