PRCYCoin  2.0.0.7rc1
P2P Digital Currency
messagesigner.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2018 The Dash Core developers
2 // Copyright (c) 2018-2019 The PIVX 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 #include "base58.h"
7 #include "hash.h"
8 #include "main.h" // For strMessageMagic
9 #include "messagesigner.h"
10 #include "tinyformat.h"
11 #include "utilstrencodings.h"
12 
13 bool CMessageSigner::GetKeysFromSecret(const std::string& strSecret, CKey& keyRet, CPubKey& pubkeyRet)
14 {
15  CBitcoinSecret vchSecret;
16 
17  if(!vchSecret.SetString(strSecret)) return false;
18 
19  keyRet = vchSecret.GetKey();
20  pubkeyRet = keyRet.GetPubKey();
21 
22  return true;
23 }
24 
25 bool CMessageSigner::SignMessage(const std::string& strMessage, std::vector<unsigned char>& vchSigRet, const CKey& key)
26 {
27  CHashWriter ss(SER_GETHASH, 0);
28  ss << strMessageMagic;
29  ss << strMessage;
30 
31  return CHashSigner::SignHash(ss.GetHash(), key, vchSigRet);
32 }
33 
34 bool CMessageSigner::VerifyMessage(const CPubKey& pubkey, const std::vector<unsigned char>& vchSig, const std::string& strMessage, std::string& strErrorRet)
35 {
36  return VerifyMessage(pubkey.GetID(), vchSig, strMessage, strErrorRet);
37 }
38 
39 bool CMessageSigner::VerifyMessage(const CKeyID& keyID, const std::vector<unsigned char>& vchSig, const std::string& strMessage, std::string& strErrorRet)
40 {
41  CHashWriter ss(SER_GETHASH, 0);
42  ss << strMessageMagic;
43  ss << strMessage;
44 
45  return CHashSigner::VerifyHash(ss.GetHash(), keyID, vchSig, strErrorRet);
46 }
47 
48 bool CHashSigner::SignHash(const uint256& hash, const CKey& key, std::vector<unsigned char>& vchSigRet)
49 {
50  return key.SignCompact(hash, vchSigRet);
51 }
52 
53 bool CHashSigner::VerifyHash(const uint256& hash, const CPubKey& pubkey, const std::vector<unsigned char>& vchSig, std::string& strErrorRet)
54 {
55  return VerifyHash(hash, pubkey.GetID(), vchSig, strErrorRet);
56 }
57 
58 bool CHashSigner::VerifyHash(const uint256& hash, const CKeyID& keyID, const std::vector<unsigned char>& vchSig, std::string& strErrorRet)
59 {
60  CPubKey pubkeyFromSig;
61  if(!pubkeyFromSig.RecoverCompact(hash, vchSig)) {
62  strErrorRet = "Error recovering public key.";
63  return false;
64  }
65 
66  if(pubkeyFromSig.GetID() != keyID) {
67  strErrorRet = strprintf("Keys don't match: pubkey=%s, pubkeyFromSig=%s, hash=%s, vchSig=%s",
68  keyID.ToString(), pubkeyFromSig.GetID().ToString(), hash.ToString(),
69  EncodeBase64(&vchSig[0], vchSig.size()));
70  return false;
71  }
72 
73  return true;
74 }
CBitcoinSecret::GetKey
CKey GetKey()
Definition: base58.cpp:304
SER_GETHASH
@ SER_GETHASH
Definition: serialize.h:161
CKeyID
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:29
CMessageSigner::VerifyMessage
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.
Definition: messagesigner.cpp:34
CBitcoinSecret
A base58-encoded secret key.
Definition: base58.h:131
CMessageSigner::SignMessage
static bool SignMessage(const std::string &strMessage, std::vector< unsigned char > &vchSigRet, const CKey &key)
Sign the message, returns true if successful.
Definition: messagesigner.cpp:25
tinyformat.h
CHashSigner::VerifyHash
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.
Definition: messagesigner.cpp:53
CKey::SignCompact
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:126
CBitcoinSecret::SetString
bool SetString(const char *pszSecret)
Definition: base58.cpp:319
uint256
256-bit unsigned big integer.
Definition: uint256.h:38
CKey::GetPubKey
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:79
CPubKey::RecoverCompact
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Recover a public key from a compact signature.
Definition: pubkey.cpp:32
EncodeBase64
std::string EncodeBase64(const unsigned char *pch, size_t len)
Definition: utilstrencodings.cpp:102
CMessageSigner::GetKeysFromSecret
static bool GetKeysFromSecret(const std::string &strSecret, CKey &keyRet, CPubKey &pubkeyRet)
Set the private/public key values, returns true if successful.
Definition: messagesigner.cpp:13
messagesigner.h
strprintf
#define strprintf
Definition: tinyformat.h:1056
CPubKey
An encapsulated public key.
Definition: pubkey.h:37
CKey
An encapsulated private key.
Definition: key.h:39
main.h
key
CKey key
Definition: bip38tooldialog.cpp:173
CHashWriter
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:259
strMessageMagic
const std::string strMessageMagic
Definition: main.cpp:119
hash.h
base58.h
utilstrencodings.h
CHashWriter::GetHash
uint256 GetHash()
Definition: hash.h:277
CHashSigner::SignHash
static bool SignHash(const uint256 &hash, const CKey &key, std::vector< unsigned char > &vchSigRet)
Sign the hash, returns true if successful.
Definition: messagesigner.cpp:48
CPubKey::GetID
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:143
base_uint::ToString
std::string ToString() const
Definition: arith_uint256.cpp:199