Update CMakeLists.txt - play with openssl
[novacoin.git] / src / qt / bitcoinaddressvalidator.cpp
1 #include "bitcoinaddressvalidator.h"
2
3 /* Base58 characters are:
4      "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
5
6   This is:
7   - All numbers except for '0'
8   - All upper-case letters except for 'I' and 'O'
9   - All lower-case letters except for 'l'
10
11   User friendly Base58 input can map
12   - 'l' and 'I' to '1'
13   - '0' and 'O' to 'o'
14 */
15
16 BitcoinAddressValidator::BitcoinAddressValidator(QObject *parent) :
17     QValidator(parent)
18 {
19 }
20
21 QValidator::State BitcoinAddressValidator::validate(QString &input, int &pos) const
22 {
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
51     // Validation
52     QValidator::State state = QValidator::Acceptable;
53     for(int idx=0; idx<input.size(); ++idx)
54     {
55         int ch = input.at(idx).unicode();
56
57         if(((ch >= '0' && ch<='9') ||
58            (ch >= 'a' && ch<='z') ||
59            (ch >= 'A' && ch<='Z')) &&
60            ch != 'l' && ch != 'I' && ch != '0' && ch != 'O')
61         {
62             // Alphanumeric and not a 'forbidden' character
63         }
64         else
65         {
66             state = QValidator::Invalid;
67         }
68     }
69
70     // Empty address is "intermediate" input
71     if(input.isEmpty())
72     {
73         state = QValidator::Intermediate;
74     }
75
76     return state;
77 }