| 22 | const std::string MESSAGE_MAGIC = "Bitcoin Signed Message:\n"; |
| 23 | |
| 24 | MessageVerificationResult MessageVerify( |
| 25 | const std::string& address, |
| 26 | const std::string& signature, |
| 27 | const std::string& message) |
| 28 | { |
| 29 | CTxDestination destination = DecodeDestination(address); |
| 30 | if (!IsValidDestination(destination)) { |
| 31 | return MessageVerificationResult::ERR_INVALID_ADDRESS; |
| 32 | } |
| 33 | |
| 34 | if (std::get_if<PKHash>(&destination) == nullptr) { |
| 35 | return MessageVerificationResult::ERR_ADDRESS_NO_KEY; |
| 36 | } |
| 37 | |
| 38 | bool invalid = false; |
| 39 | std::vector<unsigned char> signature_bytes = DecodeBase64(signature.c_str(), &invalid); |
| 40 | if (invalid) { |
| 41 | return MessageVerificationResult::ERR_MALFORMED_SIGNATURE; |
| 42 | } |
| 43 | |
| 44 | CPubKey pubkey; |
| 45 | if (!pubkey.RecoverCompact(MessageHash(message), signature_bytes)) { |
| 46 | return MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED; |
| 47 | } |
| 48 | |
| 49 | if (!(CTxDestination(PKHash(pubkey)) == destination)) { |
| 50 | return MessageVerificationResult::ERR_NOT_SIGNED; |
| 51 | } |
| 52 | |
| 53 | return MessageVerificationResult::OK; |
| 54 | } |
| 55 | |
| 56 | bool MessageSign( |
| 57 | const CKey& privkey, |