Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

messagesigner.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2020 The Dash Core developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <base58.h>
6 #include <hash.h>
7 #include <validation.h> // For strMessageMagic
8 #include <messagesigner.h>
9 #include <tinyformat.h>
10 #include <utilstrencodings.h>
11 
12 bool CMessageSigner::GetKeysFromSecret(const std::string& strSecret, CKey& keyRet, CPubKey& pubkeyRet)
13 {
14  CBitcoinSecret vchSecret;
15 
16  if(!vchSecret.SetString(strSecret)) return false;
17 
18  keyRet = vchSecret.GetKey();
19  pubkeyRet = keyRet.GetPubKey();
20 
21  return true;
22 }
23 
24 bool CMessageSigner::SignMessage(const std::string& strMessage, std::vector<unsigned char>& vchSigRet, const CKey& key)
25 {
26  CHashWriter ss(SER_GETHASH, 0);
27  ss << strMessageMagic;
28  ss << strMessage;
29 
30  return CHashSigner::SignHash(ss.GetHash(), key, vchSigRet);
31 }
32 
33 bool CMessageSigner::VerifyMessage(const CPubKey& pubkey, const std::vector<unsigned char>& vchSig, const std::string& strMessage, std::string& strErrorRet)
34 {
35  return VerifyMessage(pubkey.GetID(), vchSig, strMessage, strErrorRet);
36 }
37 
38 bool CMessageSigner::VerifyMessage(const CKeyID& keyID, const std::vector<unsigned char>& vchSig, const std::string& strMessage, std::string& strErrorRet)
39 {
40  CHashWriter ss(SER_GETHASH, 0);
41  ss << strMessageMagic;
42  ss << strMessage;
43 
44  return CHashSigner::VerifyHash(ss.GetHash(), keyID, vchSig, strErrorRet);
45 }
46 
47 bool CHashSigner::SignHash(const uint256& hash, const CKey& key, std::vector<unsigned char>& vchSigRet)
48 {
49  return key.SignCompact(hash, vchSigRet);
50 }
51 
52 bool CHashSigner::VerifyHash(const uint256& hash, const CPubKey& pubkey, const std::vector<unsigned char>& vchSig, std::string& strErrorRet)
53 {
54  return VerifyHash(hash, pubkey.GetID(), vchSig, strErrorRet);
55 }
56 
57 bool CHashSigner::VerifyHash(const uint256& hash, const CKeyID& keyID, const std::vector<unsigned char>& vchSig, std::string& strErrorRet)
58 {
59  CPubKey pubkeyFromSig;
60  if(!pubkeyFromSig.RecoverCompact(hash, vchSig)) {
61  strErrorRet = "Error recovering public key.";
62  return false;
63  }
64 
65  if(pubkeyFromSig.GetID() != keyID) {
66  strErrorRet = strprintf("Keys don't match: pubkey=%s, pubkeyFromSig=%s, hash=%s, vchSig=%s",
67  keyID.ToString(), pubkeyFromSig.GetID().ToString(), hash.ToString(),
68  EncodeBase64(vchSig.data(), vchSig.size()));
69  return false;
70  }
71 
72  return true;
73 }
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 succcessful.
CKey GetKey()
Definition: base58.cpp:304
#define strprintf
Definition: tinyformat.h:1066
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:179
static bool SignHash(const uint256 &hash, const CKey &key, std::vector< unsigned char > &vchSigRet)
Sign the hash, returns true if successful.
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:149
const std::string strMessageMagic
Definition: validation.cpp:257
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 succcessful.
bool SignCompact(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Create a compact signature (65 bytes), which allows reconstructing the used public key...
Definition: key.cpp:221
bool SetString(const char *pszSecret)
Definition: base58.cpp:319
static bool SignMessage(const std::string &strMessage, std::vector< unsigned char > &vchSigRet, const CKey &key)
Sign the message, returns true if successful.
A base58-encoded secret key.
Definition: base58.h:101
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Recover a public key from a compact signature.
Definition: pubkey.cpp:186
An encapsulated public key.
Definition: pubkey.h:30
std::string ToString() const
Definition: uint256.cpp:62
uint256 GetHash()
Definition: hash.h:203
256-bit opaque blob.
Definition: uint256.h:123
static bool GetKeysFromSecret(const std::string &strSecret, CKey &keyRet, CPubKey &pubkeyRet)
Set the private/public key values, returns true if successful.
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:20
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:184
An encapsulated private key.
Definition: key.h:27
std::string EncodeBase64(const unsigned char *pch, size_t len)
Released under the MIT license