Dash Core  0.12.2.1
P2P Digital Currency
messagesigner.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2017 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  CHashWriter ss(SER_GETHASH, 0);
36  ss << strMessageMagic;
37  ss << strMessage;
38 
39  return CHashSigner::VerifyHash(ss.GetHash(), pubkey, vchSig, strErrorRet);
40 }
41 
42 bool CHashSigner::SignHash(const uint256& hash, const CKey key, std::vector<unsigned char>& vchSigRet)
43 {
44  return key.SignCompact(hash, vchSigRet);
45 }
46 
47 bool CHashSigner::VerifyHash(const uint256& hash, const CPubKey pubkey, const std::vector<unsigned char>& vchSig, std::string& strErrorRet)
48 {
49  CPubKey pubkeyFromSig;
50  if(!pubkeyFromSig.RecoverCompact(hash, vchSig)) {
51  strErrorRet = "Error recovering public key.";
52  return false;
53  }
54 
55  if(pubkeyFromSig.GetID() != pubkey.GetID()) {
56  strErrorRet = strprintf("Keys don't match: pubkey=%s, pubkeyFromSig=%s, hash=%s, vchSig=%s",
57  pubkey.GetID().ToString(), pubkeyFromSig.GetID().ToString(), hash.ToString(),
58  EncodeBase64(&vchSig[0], vchSig.size()));
59  return false;
60  }
61 
62  return true;
63 }
bool SetString(const char *pszSecret)
Definition: base58.cpp:329
#define strprintf
Definition: tinyformat.h:1011
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:144
string EncodeBase64(const unsigned char *pch, size_t len)
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Recover a public key from a compact signature.
Definition: pubkey.cpp:187
uint256 GetHash()
Definition: hash.h:254
static bool GetKeysFromSecret(const std::string strSecret, CKey &keyRet, CPubKey &pubkeyRet)
Set the private/public key values, 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 succcessful.
std::string ToString() const
Definition: uint256.cpp:65
static bool SignHash(const uint256 &hash, const CKey key, std::vector< unsigned char > &vchSigRet)
Sign the hash, returns true if successful.
CKey GetKey()
Definition: base58.cpp:314
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.
CPubKey GetPubKey() const
Definition: key.cpp:156
Definition: pubkey.h:37
static bool SignMessage(const std::string strMessage, std::vector< unsigned char > &vchSigRet, const CKey key)
Sign the message, returns true if successful.
const string strMessageMagic
Definition: validation.cpp:109
Definition: key.h:35