Browse Source

Make base58 validator explicit

miguelfreitas
Wladimir J. van der Laan 14 years ago
parent
commit
dab03e34f5
  1. 5
      gui/include/bitcoinaddressvalidator.h
  2. 26
      gui/src/bitcoinaddressvalidator.cpp

5
gui/include/bitcoinaddressvalidator.h

@ -3,7 +3,10 @@
#include <QRegExpValidator> #include <QRegExpValidator>
class BitcoinAddressValidator : public QRegExpValidator /* Base48 entry widget validator.
Corrects near-miss characters and refuses characters that are no part of base48.
*/
class BitcoinAddressValidator : public QValidator
{ {
Q_OBJECT Q_OBJECT
public: public:

26
gui/src/bitcoinaddressvalidator.cpp

@ -1,7 +1,5 @@
#include "bitcoinaddressvalidator.h" #include "bitcoinaddressvalidator.h"
#include "base58.h"
#include <QDebug> #include <QDebug>
/* Base58 characters are: /* Base58 characters are:
@ -18,12 +16,13 @@
*/ */
BitcoinAddressValidator::BitcoinAddressValidator(QObject *parent) : BitcoinAddressValidator::BitcoinAddressValidator(QObject *parent) :
QRegExpValidator(QRegExp(QString("^[")+QString(pszBase58)+QString("]+")), parent) QValidator(parent)
{ {
} }
QValidator::State BitcoinAddressValidator::validate(QString &input, int &pos) const QValidator::State BitcoinAddressValidator::validate(QString &input, int &pos) const
{ {
/* Correction */
for(int idx=0; idx<input.size(); ++idx) for(int idx=0; idx<input.size(); ++idx)
{ {
switch(input.at(idx).unicode()) switch(input.at(idx).unicode())
@ -39,7 +38,26 @@ QValidator::State BitcoinAddressValidator::validate(QString &input, int &pos) co
default: default:
break; break;
} }
}
/* Validation */
QValidator::State state = QValidator::Acceptable;
for(int idx=0; idx<input.size(); ++idx)
{
int ch = input.at(idx).unicode();
if(((ch >= '0' && ch<='9') ||
(ch >= 'a' && ch<='z') ||
(ch >= 'A' && ch<='Z')) &&
ch != 'l' && ch != 'I' && ch != '0' && ch != 'O')
{
/* Alphanumeric and not a 'forbidden' character */
}
else
{
state = QValidator::Invalid;
} }
return QRegExpValidator::validate(input, pos); }
return state;
} }

Loading…
Cancel
Save