PIVX Core  5.6.99
P2P Digital Currency
bitcoinaddressvalidator.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2014 The Bitcoin developers
2 // Copyright (c) 2014-2015 The Dash developers
3 // Copyright (c) 2015-2021 The PIVX Core developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
8 
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 */
19 
21 {
22 }
23 
24 QValidator::State BitcoinAddressEntryValidator::validate(QString& input, int& pos) const
25 {
26  Q_UNUSED(pos);
27 
28  // Empty address is "intermediate" input
29  if (input.isEmpty())
30  return QValidator::Intermediate;
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  // To next character
54  if (removeChar)
55  input.remove(idx, 1);
56  else
57  ++idx;
58  }
59 
60  // Validation
61  QValidator::State state = QValidator::Acceptable;
62  for (int idx = 0; idx < input.size(); ++idx) {
63  int ch = input.at(idx).unicode();
64 
65  if (((ch >= '0' && ch <= '9') ||
66  (ch >= 'a' && ch <= 'z') ||
67  (ch >= 'A' && ch <= 'Z')) &&
68  ch != 'l' && ch != 'I' && ch != '0' && ch != 'O') {
69  // Alphanumeric and not a 'forbidden' character
70  } else {
71  state = QValidator::Invalid;
72  }
73  }
74 
75  return state;
76 }
77 
79 {
80 }
81 
82 QValidator::State BitcoinAddressCheckValidator::validate(QString& input, int& pos) const
83 {
84  Q_UNUSED(pos);
85  // Validate the passed PIVX address
86  CTxDestination addr = DecodeDestination(input.toStdString());
87  if (IsValidDestination(addr))
88  return QValidator::Acceptable;
89 
90  return QValidator::Invalid;
91 }
State validate(QString &input, int &pos) const
BitcoinAddressEntryValidator(QObject *parent)
State validate(QString &input, int &pos) const
bool IsValidDestination(const CWDestination &address)
CWDestination DecodeDestination(const std::string &strAddress)
boost::variant< CNoDestination, CKeyID, CScriptID, CExchangeKeyID > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:72