You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

starwelsaddressvalidator.cpp 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright (c) 2011-2014 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "starwelsaddressvalidator.h"
  5. #include "base58.h"
  6. /* Base58 characters are:
  7. "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
  8. This is:
  9. - All numbers except for '0'
  10. - All upper-case letters except for 'I' and 'O'
  11. - All lower-case letters except for 'l'
  12. */
  13. StarwelsAddressEntryValidator::StarwelsAddressEntryValidator(QObject *parent) :
  14. QValidator(parent)
  15. {
  16. }
  17. QValidator::State StarwelsAddressEntryValidator::validate(QString &input, int &pos) const
  18. {
  19. Q_UNUSED(pos);
  20. // Empty address is "intermediate" input
  21. if (input.isEmpty())
  22. return QValidator::Intermediate;
  23. // Correction
  24. for (int idx = 0; idx < input.size();)
  25. {
  26. bool removeChar = false;
  27. QChar ch = input.at(idx);
  28. // Corrections made are very conservative on purpose, to avoid
  29. // users unexpectedly getting away with typos that would normally
  30. // be detected, and thus sending to the wrong address.
  31. switch(ch.unicode())
  32. {
  33. // Qt categorizes these as "Other_Format" not "Separator_Space"
  34. case 0x200B: // ZERO WIDTH SPACE
  35. case 0xFEFF: // ZERO WIDTH NO-BREAK SPACE
  36. removeChar = true;
  37. break;
  38. default:
  39. break;
  40. }
  41. // Remove whitespace
  42. if (ch.isSpace())
  43. removeChar = true;
  44. // To next character
  45. if (removeChar)
  46. input.remove(idx, 1);
  47. else
  48. ++idx;
  49. }
  50. // Validation
  51. QValidator::State state = QValidator::Acceptable;
  52. for (int idx = 0; idx < input.size(); ++idx)
  53. {
  54. int ch = input.at(idx).unicode();
  55. if (((ch >= '0' && ch<='9') ||
  56. (ch >= 'a' && ch<='z') ||
  57. (ch >= 'A' && ch<='Z')) &&
  58. ch != 'l' && ch != 'I' && ch != '0' && ch != 'O')
  59. {
  60. // Alphanumeric and not a 'forbidden' character
  61. }
  62. else
  63. {
  64. state = QValidator::Invalid;
  65. }
  66. }
  67. return state;
  68. }
  69. StarwelsAddressCheckValidator::StarwelsAddressCheckValidator(QObject *parent) :
  70. QValidator(parent)
  71. {
  72. }
  73. QValidator::State StarwelsAddressCheckValidator::validate(QString &input, int &pos) const
  74. {
  75. Q_UNUSED(pos);
  76. // Validate the passed Starwels address
  77. CStarwelsAddress addr(input.toStdString());
  78. if (addr.IsValid())
  79. return QValidator::Acceptable;
  80. return QValidator::Invalid;
  81. }