Bitcoin ABC  0.22.12
P2P Digital Currency
bitcoinaddressvalidator.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2014 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Bitcoin 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 <config.h>
9 #include <key_io.h>
10 
11 /* Base58 characters are:
12  "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
13 
14  This is:
15  - All numbers except for '0'
16  - All upper-case letters except for 'I' and 'O'
17  - All lower-case letters except for 'l'
18 */
20  const std::string &cashaddrprefixIn, QObject *parent)
21  : QValidator(parent), cashaddrprefix(cashaddrprefixIn) {}
22 
23 QValidator::State BitcoinAddressEntryValidator::validate(QString &input,
24  int &pos) const {
25  Q_UNUSED(pos);
26 
27  // Empty address is "intermediate" input
28  if (input.isEmpty()) {
29  return QValidator::Intermediate;
30  }
31 
32  // Correction
33  for (int idx = 0; idx < input.size();) {
34  bool removeChar = false;
35  QChar ch = input.at(idx);
36  // Corrections made are very conservative on purpose, to avoid
37  // users unexpectedly getting away with typos that would normally
38  // be detected, and thus sending to the wrong address.
39  switch (ch.unicode()) {
40  // Qt categorizes these as "Other_Format" not "Separator_Space"
41  case 0x200B: // ZERO WIDTH SPACE
42  case 0xFEFF: // ZERO WIDTH NO-BREAK SPACE
43  removeChar = true;
44  break;
45  default:
46  break;
47  }
48 
49  // Remove whitespace
50  if (ch.isSpace()) {
51  removeChar = true;
52  }
53 
54  // To next character
55  if (removeChar) {
56  input.remove(idx, 1);
57  } else {
58  ++idx;
59  }
60  }
61 
62  // Validation
63  QValidator::State state = QValidator::Acceptable;
64  for (int idx = 0; idx < input.size(); ++idx) {
65  int ch = input.at(idx).unicode();
66 
67  if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') ||
68  (ch >= 'A' && ch <= 'Z') || (ch == ':')) {
69  // Alphanumeric and not a 'forbidden' character
70  // We also include ':' for cashaddr.
71  } else {
72  return QValidator::Invalid;
73  }
74  }
75 
76  return state;
77 }
78 
80  : QValidator(parent) {}
81 
82 QValidator::State BitcoinAddressCheckValidator::validate(QString &input,
83  int &pos) const {
84  Q_UNUSED(pos);
85 
86  // Validate the passed Bitcoin address
87  if (IsValidDestinationString(input.toStdString(),
89  return QValidator::Acceptable;
90  }
91 
92  return QValidator::Invalid;
93 }
bool IsValidDestinationString(const std::string &str, const CChainParams &params)
Definition: key_io.cpp:186
State validate(QString &input, int &pos) const override
virtual const CChainParams & GetChainParams() const =0
State validate(QString &input, int &pos) const override
const Config & GetConfig()
Definition: config.cpp:34
BitcoinAddressEntryValidator(const std::string &cashaddrprefixIn, QObject *parent)