373877808f729387ac77f3971a619225b09567fb
[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 uppercase letters except for 'I' and 'O'
9   - All lowercase 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(); ++idx)
25     {
26         switch(input.at(idx).unicode())
27         {
28         case 'l':
29         case 'I':
30             input[idx] = QChar('1');
31             break;
32         case '0':
33         case 'O':
34             input[idx] = QChar('o');
35             break;
36         default:
37             break;
38         }
39     }
40
41     // Validation
42     QValidator::State state = QValidator::Acceptable;
43     for(int idx=0; idx<input.size(); ++idx)
44     {
45         int ch = input.at(idx).unicode();
46
47         if(((ch >= '0' && ch<='9') ||
48            (ch >= 'a' && ch<='z') ||
49            (ch >= 'A' && ch<='Z')) &&
50            ch != 'l' && ch != 'I' && ch != '0' && ch != 'O')
51         {
52             // Alphanumeric and not a 'forbidden' character
53         }
54         else
55         {
56             state = QValidator::Invalid;
57         }
58     }
59
60     // Empty address is "intermediate" input
61     if(input.isEmpty())
62     {
63         state = QValidator::Intermediate;
64     }
65
66     return state;
67 }