2020-01-17 15:42:55 +01:00
|
|
|
// Copyright (c) 2014-2020 The Dash Core developers
|
2017-04-12 09:04:06 +02:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
Merge #11372: Address encoding cleanup
92f1f8b31 Split off key_io_tests from base58_tests (Pieter Wuille)
119b0f85e Split key_io (address/key encodings) off from base58 (Pieter Wuille)
ebfe217b1 Stop using CBase58Data for ext keys (Pieter Wuille)
32e69fa0d Replace CBitcoinSecret with {Encode,Decode}Secret (Pieter Wuille)
Pull request description:
This PR contains some of the changes left as TODO in #11167 (and built on top of that PR). They are not intended for backporting.
This removes the `CBase58`, `CBitcoinSecret`, `CBitcoinExtKey`, and `CBitcoinExtPubKey` classes, in favor of simple `Encode`/`Decode` functions. Furthermore, all Bitcoin-specific logic (addresses, WIF, BIP32) is moved to `key_io.{h,cpp}`, leaving `base58.{h,cpp}` as a pure utility that implements the base58 encoding/decoding logic.
Tree-SHA512: a5962c0ed27ad53cbe00f22af432cf11aa530e3efc9798e25c004bc9ed1b5673db5df3956e398ee2c085e3a136ac8da69fe7a7d97a05fb2eb3be0b60d0479655
Make linter happy
Dashify
2018-03-07 00:04:56 +01:00
|
|
|
#include <key_io.h>
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <hash.h>
|
2021-11-16 16:19:47 +01:00
|
|
|
#include <util/validation.h> // For strMessageMagic
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <messagesigner.h>
|
|
|
|
#include <tinyformat.h>
|
2021-06-27 08:33:13 +02:00
|
|
|
#include <util/strencodings.h>
|
2017-04-12 09:04:06 +02:00
|
|
|
|
2018-02-12 13:49:00 +01:00
|
|
|
bool CMessageSigner::GetKeysFromSecret(const std::string& strSecret, CKey& keyRet, CPubKey& pubkeyRet)
|
2017-04-12 09:04:06 +02:00
|
|
|
{
|
Merge #11372: Address encoding cleanup
92f1f8b31 Split off key_io_tests from base58_tests (Pieter Wuille)
119b0f85e Split key_io (address/key encodings) off from base58 (Pieter Wuille)
ebfe217b1 Stop using CBase58Data for ext keys (Pieter Wuille)
32e69fa0d Replace CBitcoinSecret with {Encode,Decode}Secret (Pieter Wuille)
Pull request description:
This PR contains some of the changes left as TODO in #11167 (and built on top of that PR). They are not intended for backporting.
This removes the `CBase58`, `CBitcoinSecret`, `CBitcoinExtKey`, and `CBitcoinExtPubKey` classes, in favor of simple `Encode`/`Decode` functions. Furthermore, all Bitcoin-specific logic (addresses, WIF, BIP32) is moved to `key_io.{h,cpp}`, leaving `base58.{h,cpp}` as a pure utility that implements the base58 encoding/decoding logic.
Tree-SHA512: a5962c0ed27ad53cbe00f22af432cf11aa530e3efc9798e25c004bc9ed1b5673db5df3956e398ee2c085e3a136ac8da69fe7a7d97a05fb2eb3be0b60d0479655
Make linter happy
Dashify
2018-03-07 00:04:56 +01:00
|
|
|
keyRet = DecodeSecret(strSecret);
|
|
|
|
if (!keyRet.IsValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-12 09:04:06 +02:00
|
|
|
pubkeyRet = keyRet.GetPubKey();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-12 13:49:00 +01:00
|
|
|
bool CMessageSigner::SignMessage(const std::string& strMessage, std::vector<unsigned char>& vchSigRet, const CKey& key)
|
2017-04-12 09:04:06 +02:00
|
|
|
{
|
|
|
|
CHashWriter ss(SER_GETHASH, 0);
|
|
|
|
ss << strMessageMagic;
|
|
|
|
ss << strMessage;
|
|
|
|
|
|
|
|
return CHashSigner::SignHash(ss.GetHash(), key, vchSigRet);
|
|
|
|
}
|
|
|
|
|
2018-02-12 13:49:00 +01:00
|
|
|
bool CMessageSigner::VerifyMessage(const CPubKey& pubkey, const std::vector<unsigned char>& vchSig, const std::string& strMessage, std::string& strErrorRet)
|
2018-03-02 14:15:04 +01:00
|
|
|
{
|
|
|
|
return VerifyMessage(pubkey.GetID(), vchSig, strMessage, strErrorRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CMessageSigner::VerifyMessage(const CKeyID& keyID, const std::vector<unsigned char>& vchSig, const std::string& strMessage, std::string& strErrorRet)
|
2017-04-12 09:04:06 +02:00
|
|
|
{
|
|
|
|
CHashWriter ss(SER_GETHASH, 0);
|
|
|
|
ss << strMessageMagic;
|
|
|
|
ss << strMessage;
|
|
|
|
|
2018-03-02 14:15:04 +01:00
|
|
|
return CHashSigner::VerifyHash(ss.GetHash(), keyID, vchSig, strErrorRet);
|
2017-04-12 09:04:06 +02:00
|
|
|
}
|
|
|
|
|
2018-02-12 13:49:00 +01:00
|
|
|
bool CHashSigner::SignHash(const uint256& hash, const CKey& key, std::vector<unsigned char>& vchSigRet)
|
2017-04-12 09:04:06 +02:00
|
|
|
{
|
|
|
|
return key.SignCompact(hash, vchSigRet);
|
|
|
|
}
|
|
|
|
|
2018-02-12 13:49:00 +01:00
|
|
|
bool CHashSigner::VerifyHash(const uint256& hash, const CPubKey& pubkey, const std::vector<unsigned char>& vchSig, std::string& strErrorRet)
|
2018-03-02 14:15:04 +01:00
|
|
|
{
|
|
|
|
return VerifyHash(hash, pubkey.GetID(), vchSig, strErrorRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CHashSigner::VerifyHash(const uint256& hash, const CKeyID& keyID, const std::vector<unsigned char>& vchSig, std::string& strErrorRet)
|
2017-04-12 09:04:06 +02:00
|
|
|
{
|
|
|
|
CPubKey pubkeyFromSig;
|
|
|
|
if(!pubkeyFromSig.RecoverCompact(hash, vchSig)) {
|
|
|
|
strErrorRet = "Error recovering public key.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-02 14:15:04 +01:00
|
|
|
if(pubkeyFromSig.GetID() != keyID) {
|
2017-04-12 09:04:06 +02:00
|
|
|
strErrorRet = strprintf("Keys don't match: pubkey=%s, pubkeyFromSig=%s, hash=%s, vchSig=%s",
|
2018-03-02 14:15:04 +01:00
|
|
|
keyID.ToString(), pubkeyFromSig.GetID().ToString(), hash.ToString(),
|
2020-08-07 19:55:51 +02:00
|
|
|
EncodeBase64(vchSig));
|
2017-04-12 09:04:06 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|