Bitcoin ABC 0.32.12
P2P Digital Currency
signmessage.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
7
8#include <hash.h> // For HashWriter
9#include <key.h> // For CKey
10#include <key_io.h> // For DecodeDestination()
11#include <pubkey.h> // For CPubKey
12#include <script/standard.h> // For CTxDestination, IsValidDestination(), PKHash
13#include <serialize.h> // For SER_GETHASH
14#include <util/strencodings.h> // For DecodeBase64()
15
16#include <string>
17#include <vector>
18
23const std::string MESSAGE_MAGIC = "eCash Signed Message:\n";
24
26 const std::string &address,
27 const std::string &signature,
28 const std::string &message) {
29 CTxDestination destination = DecodeDestination(address, params);
30 if (!IsValidDestination(destination)) {
32 }
33
34 if (std::get_if<PKHash>(&destination) == nullptr) {
36 }
37
38 auto signature_bytes = DecodeBase64(signature);
39 if (!signature_bytes) {
41 }
42
43 CPubKey pubkey;
44 if (!pubkey.RecoverCompact(MessageHash(message), *signature_bytes)) {
46 }
47
48 if (!(CTxDestination(PKHash(pubkey)) == destination)) {
50 }
51
53}
54
55bool MessageSign(const CKey &privkey, const std::string &message,
56 std::string &signature) {
57 std::vector<uint8_t> signature_bytes;
58
59 if (!privkey.SignCompact(MessageHash(message), signature_bytes)) {
60 return false;
61 }
62
63 signature = EncodeBase64(signature_bytes);
64
65 return true;
66}
67
68uint256 MessageHash(const std::string &message) {
69 HashWriter hasher{};
70 hasher << MESSAGE_MAGIC << message;
71
72 return hasher.GetHash();
73}
74
75std::string SigningResultString(const SigningResult res) {
76 switch (res) {
78 return "No error";
80 return "Private key not available";
82 return "Sign failed";
83 // no default case, so the compiler can warn about missing cases
84 }
85 assert(false);
86}
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:86
An encapsulated secp256k1 private key.
Definition: key.h:28
bool SignCompact(const uint256 &hash, std::vector< uint8_t > &vchSig) const
Create a compact ECDSA signature (65 bytes), which allows reconstructing the used public key.
Definition: key.cpp:315
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:234
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:99
256-bit opaque blob.
Definition: uint256.h:129
uint256 MessageHash(const std::string &message)
Hashes a message for signing and verification in a manner that prevents inadvertently signing a trans...
Definition: signmessage.cpp:68
MessageVerificationResult MessageVerify(const CChainParams &params, const std::string &address, const std::string &signature, const std::string &message)
Verify a signed message.
Definition: signmessage.cpp:25
bool MessageSign(const CKey &privkey, const std::string &message, std::string &signature)
Sign a message.
Definition: signmessage.cpp:55
const std::string MESSAGE_MAGIC
Text used to signify that a signed message follows and to prevent inadvertently signing a transaction...
Definition: signmessage.cpp:23
std::string SigningResultString(const SigningResult res)
Definition: signmessage.cpp:75
CTxDestination DecodeDestination(const std::string &addr, const CChainParams &params)
Definition: key_io.cpp:174
SigningResult
Definition: signmessage.h:47
@ PRIVATE_KEY_NOT_AVAILABLE
@ OK
No error.
MessageVerificationResult
The result of a signed message verification.
Definition: signmessage.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.
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())