Somewhat confident now, tested on GNOME+KDE, with all types of transactions. Next...
[novacoin.git] / src / qt / bitcoinaddressvalidator.cpp
1 #include "bitcoinaddressvalidator.h"
2
3 #include <QDebug>
4
5 /* Base58 characters are:
6      "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
7
8   This is:
9   - All numbers except for '0'
10   - All uppercase letters except for 'I' and 'O'
11   - All lowercase letters except for 'l'
12
13   User friendly Base58 input can map
14   - 'l' and 'I' to '1'
15   - '0' and 'O' to 'o'
16 */
17
18 BitcoinAddressValidator::BitcoinAddressValidator(QObject *parent) :
19     QValidator(parent)
20 {
21 }
22
23 QValidator::State BitcoinAddressValidator::validate(QString &input, int &pos) const
24 {
25     /* Correction */
26     for(int idx=0; idx<input.size(); ++idx)
27     {
28         switch(input.at(idx).unicode())
29         {
30         case 'l':
31         case 'I':
32             input[idx] = QChar('1');
33             break;
34         case '0':
35         case 'O':
36             input[idx] = QChar('o');
37             break;
38         default:
39             break;
40         }
41     }
42
43     /* Validation */
44     QValidator::State state = QValidator::Acceptable;
45     for(int idx=0; idx<input.size(); ++idx)
46     {
47         int ch = input.at(idx).unicode();
48
49         if(((ch >= '0' && ch<='9') ||
50            (ch >= 'a' && ch<='z') ||
51            (ch >= 'A' && ch<='Z')) &&
52            ch != 'l' && ch != 'I' && ch != '0' && ch != 'O')
53         {
54             /* Alphanumeric and not a 'forbidden' character */
55         }
56         else
57         {
58             state = QValidator::Invalid;
59         }
60     }
61
62     return state;
63 }