|
| 1 | +// Copyright (c) 2026 The Bitcoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include <qml/models/signverifymessageadapter.h> |
| 6 | + |
| 7 | +#include <bitcoin-build-config.h> // IWYU pragma: keep |
| 8 | +#include <key_io.h> |
| 9 | + |
| 10 | +#include <QObject> |
| 11 | + |
| 12 | +namespace { |
| 13 | +QString InvalidAddressMessage() |
| 14 | +{ |
| 15 | + return QObject::tr("The entered address is invalid.") + QStringLiteral(" ") |
| 16 | + + QObject::tr("Please check the address and try again."); |
| 17 | +} |
| 18 | +} // namespace |
| 19 | + |
| 20 | +SignAddressValidationResult SignVerifyMessageAdapter::ValidateSigningAddress(const QString& address) |
| 21 | +{ |
| 22 | + const QString trimmed_address = address.trimmed(); |
| 23 | + if (trimmed_address.isEmpty()) { |
| 24 | + return { |
| 25 | + false, |
| 26 | + QObject::tr("Enter a Bitcoin address to sign with."), |
| 27 | + std::nullopt, |
| 28 | + }; |
| 29 | + } |
| 30 | + |
| 31 | + const CTxDestination destination = DecodeDestination(trimmed_address.toStdString()); |
| 32 | + if (!IsValidDestination(destination)) { |
| 33 | + return { |
| 34 | + false, |
| 35 | + InvalidAddressMessage(), |
| 36 | + std::nullopt, |
| 37 | + }; |
| 38 | + } |
| 39 | + |
| 40 | + const PKHash* pkhash = std::get_if<PKHash>(&destination); |
| 41 | + if (!pkhash) { |
| 42 | + return { |
| 43 | + false, |
| 44 | + UnsupportedAddressTypeMessage(), |
| 45 | + std::nullopt, |
| 46 | + }; |
| 47 | + } |
| 48 | + |
| 49 | + return { |
| 50 | + true, |
| 51 | + QString{}, |
| 52 | + *pkhash, |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +SignVerifyMessageResult SignVerifyMessageAdapter::BuildSigningResult(const SigningResult result, |
| 57 | + const QString& signature) |
| 58 | +{ |
| 59 | + switch (result) { |
| 60 | + case SigningResult::OK: |
| 61 | + return { |
| 62 | + true, |
| 63 | + QObject::tr("Message signed."), |
| 64 | + signature, |
| 65 | + }; |
| 66 | + case SigningResult::PRIVATE_KEY_NOT_AVAILABLE: |
| 67 | + return { |
| 68 | + false, |
| 69 | + QObject::tr("Private key for the entered address is not available."), |
| 70 | + QString{}, |
| 71 | + }; |
| 72 | + case SigningResult::SIGNING_FAILED: |
| 73 | + return { |
| 74 | + false, |
| 75 | + QObject::tr("Message signing failed."), |
| 76 | + QString{}, |
| 77 | + }; |
| 78 | + } |
| 79 | + |
| 80 | + return { |
| 81 | + false, |
| 82 | + QObject::tr("Message signing failed."), |
| 83 | + QString{}, |
| 84 | + }; |
| 85 | +} |
| 86 | + |
| 87 | +SignVerifyMessageResult SignVerifyMessageAdapter::VerifyMessage(const QString& address, |
| 88 | + const QString& signature, |
| 89 | + const QString& message) |
| 90 | +{ |
| 91 | + const auto result = MessageVerify(address.toStdString(), |
| 92 | + signature.toStdString(), |
| 93 | + message.toStdString()); |
| 94 | + |
| 95 | + switch (result) { |
| 96 | + case MessageVerificationResult::OK: |
| 97 | + return { |
| 98 | + true, |
| 99 | + QObject::tr("Message verified."), |
| 100 | + QString{}, |
| 101 | + }; |
| 102 | + case MessageVerificationResult::ERR_INVALID_ADDRESS: |
| 103 | + return { |
| 104 | + false, |
| 105 | + InvalidAddressMessage(), |
| 106 | + QString{}, |
| 107 | + }; |
| 108 | + case MessageVerificationResult::ERR_ADDRESS_NO_KEY: |
| 109 | + return { |
| 110 | + false, |
| 111 | + UnsupportedAddressTypeMessage(), |
| 112 | + QString{}, |
| 113 | + }; |
| 114 | + case MessageVerificationResult::ERR_MALFORMED_SIGNATURE: |
| 115 | + return { |
| 116 | + false, |
| 117 | + QObject::tr("The signature could not be decoded.") + QStringLiteral(" ") |
| 118 | + + QObject::tr("Please check the signature and try again."), |
| 119 | + QString{}, |
| 120 | + }; |
| 121 | + case MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED: |
| 122 | + return { |
| 123 | + false, |
| 124 | + QObject::tr("The signature did not match the message digest.") + QStringLiteral(" ") |
| 125 | + + QObject::tr("Please check the signature and try again."), |
| 126 | + QString{}, |
| 127 | + }; |
| 128 | + case MessageVerificationResult::ERR_NOT_SIGNED: |
| 129 | + return { |
| 130 | + false, |
| 131 | + QObject::tr("Message verification failed."), |
| 132 | + QString{}, |
| 133 | + }; |
| 134 | + } |
| 135 | + |
| 136 | + return { |
| 137 | + false, |
| 138 | + QObject::tr("Message verification failed."), |
| 139 | + QString{}, |
| 140 | + }; |
| 141 | +} |
| 142 | + |
| 143 | +QString SignVerifyMessageAdapter::UnsupportedAddressTypeMessage() |
| 144 | +{ |
| 145 | + return QObject::tr("The entered address does not refer to a legacy (P2PKH) key. " |
| 146 | + "Message signing for SegWit and other non-P2PKH address types is " |
| 147 | + "not supported in this version of %1. Please check the address and try again.") |
| 148 | + .arg(QStringLiteral(CLIENT_NAME)); |
| 149 | +} |
0 commit comments