Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
message.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2020 The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#include <hash.h> // For HashWriter
7#include <key.h> // For CKey
8#include <key_io.h> // For DecodeDestination()
9#include <pubkey.h> // For CPubKey
10#include <script/standard.h> // For CTxDestination, IsValidDestination(), PKHash
11#include <serialize.h> // For SER_GETHASH
12#include <util/message.h>
13#include <util/strencodings.h> // For DecodeBase64()
14
15#include <string>
16#include <vector>
17
22const std::string MESSAGE_MAGIC = "eCash Signed Message:\n";
23
25 const std::string &address,
26 const std::string &signature,
27 const std::string &message) {
28 CTxDestination destination = DecodeDestination(address, params);
29 if (!IsValidDestination(destination)) {
31 }
32
33 if (std::get_if<PKHash>(&destination) == nullptr) {
35 }
36
37 auto signature_bytes = DecodeBase64(signature);
38 if (!signature_bytes) {
40 }
41
42 CPubKey pubkey;
43 if (!pubkey.RecoverCompact(MessageHash(message), *signature_bytes)) {
45 }
46
47 if (!(CTxDestination(PKHash(pubkey)) == destination)) {
49 }
50
52}
53
54bool MessageSign(const CKey &privkey, const std::string &message,
55 std::string &signature) {
56 std::vector<uint8_t> signature_bytes;
57
58 if (!privkey.SignCompact(MessageHash(message), signature_bytes)) {
59 return false;
60 }
61
62 signature = EncodeBase64(signature_bytes);
63
64 return true;
65}
66
67uint256 MessageHash(const std::string &message) {
68 HashWriter hasher{};
69 hasher << MESSAGE_MAGIC << message;
70
71 return hasher.GetHash();
72}
73
75 switch (res) {
77 return "No error";
79 return "Private key not available";
81 return "Sign failed";
82 // no default case, so the compiler can warn about missing cases
83 }
84 assert(false);
85}
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition chainparams.h:80
An encapsulated secp256k1 private key.
Definition key.h:28
An encapsulated public key.
Definition pubkey.h:31
bool RecoverCompact(const uint256 &hash, const std::vector< uint8_t > &vchSig)
Recover a public key from a compact ECDSA signature.
Definition pubkey.cpp:227
A writer stream (for serialization) that computes a 256-bit hash.
Definition hash.h:100
uint256 GetHash()
Compute the double-SHA256 hash of all data written to this object.
Definition hash.h:114
256-bit opaque blob.
Definition uint256.h:129
CTxDestination DecodeDestination(const std::string &addr, const CChainParams &params)
Definition key_io.cpp:174
uint256 MessageHash(const std::string &message)
Hashes a message for signing and verification in a manner that prevents inadvertently signing a trans...
Definition message.cpp:67
MessageVerificationResult MessageVerify(const CChainParams &params, const std::string &address, const std::string &signature, const std::string &message)
Verify a signed message.
Definition message.cpp:24
bool MessageSign(const CKey &privkey, const std::string &message, std::string &signature)
Sign a message.
Definition message.cpp:54
const std::string MESSAGE_MAGIC
Text used to signify that a signed message follows and to prevent inadvertently signing a transaction...
Definition message.cpp:22
std::string SigningResultString(const SigningResult res)
Definition message.cpp:74
SigningResult
Definition message.h:47
@ PRIVATE_KEY_NOT_AVAILABLE
@ OK
No error.
MessageVerificationResult
The result of a signed message verification.
Definition message.h:26
@ ERR_MALFORMED_SIGNATURE
The provided signature couldn't be parsed (maybe invalid base64).
@ ERR_INVALID_ADDRESS
The provided address is invalid.
@ ERR_ADDRESS_NO_KEY
The provided address is valid but does not refer to a public key.
@ ERR_NOT_SIGNED
The message was not signed with the private key of the provided address.
@ OK
The message verification was successful.
@ ERR_PUBKEY_NOT_RECOVERED
A public key could not be recovered from the provided signature and message.
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition standard.cpp:260
std::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition standard.h:85
std::string EncodeBase64(Span< const uint8_t > input)
std::optional< std::vector< uint8_t > > DecodeBase64(std::string_view str)
assert(!tx.IsCoinBase())