@ -4,35 +4,36 @@
# include <string>
# include <string>
# include <vector>
# include <vector>
# include <QDialogButtonBox>
# include <QDialog>
# include <QAbstractButton>
# include <QLabel>
# include <QClipboard>
# include <QLineEdit>
# include <QMessageBox>
# include <QPlainTextEdit>
# include <QPushButton>
# include "main.h"
# include "main.h"
# include "wallet.h"
# include "wallet.h"
# include "walletmodel.h"
# include "walletmodel.h"
# include "addresstablemodel.h"
# include "guiutil.h"
# include "guiutil.h"
# include "base58.h"
# include "base58.h"
VerifyMessageDialog : : VerifyMessageDialog ( AddressTableModel * addressModel , QWidget * parent ) :
VerifyMessageDialog : : VerifyMessageDialog ( QWidget * parent ) :
QDialog ( parent ) ,
QDialog ( parent ) ,
ui ( new Ui : : VerifyMessageDialog ) ,
ui ( new Ui : : VerifyMessageDialog )
model ( addressModel )
{
{
ui - > setupUi ( this ) ;
ui - > setupUi ( this ) ;
# if (QT_VERSION >= 0x040700)
# if (QT_VERSION >= 0x040700)
/* Do not move this to the XML file, Qt before 4.7 will choke on it */
/* Do not move this to the XML file, Qt before 4.7 will choke on it */
ui - > lnAddress - > setPlaceholderText ( tr ( " Enter a Bitcoin address (e.g. 1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L) " ) ) ;
ui - > lnSig - > setPlaceholderText ( tr ( " Enter Bitcoin signature " ) ) ;
ui - > lnSig - > setPlaceholderText ( tr ( " Enter Bitcoin signature " ) ) ;
ui - > lnAddress - > setPlaceholderText ( tr ( " Click \" Verify Message \" to obtain address " ) ) ;
# endif
# endif
GUIUtil : : setupAddressWidget ( ui - > lnAddress , this ) ;
GUIUtil : : setupAddressWidget ( ui - > lnAddress , this ) ;
ui - > lnAddress - > installEventFilter ( this ) ;
ui - > lnAddress - > installEventFilter ( this ) ;
ui - > edMessage - > setFocus ( ) ;
ui - > lnSig - > setFont ( GUIUtil : : bitcoinAddressFont ( ) ) ;
ui - > lnAddress - > setFocus ( ) ;
}
}
VerifyMessageDialog : : ~ VerifyMessageDialog ( )
VerifyMessageDialog : : ~ VerifyMessageDialog ( )
@ -40,54 +41,65 @@ VerifyMessageDialog::~VerifyMessageDialog()
delete ui ;
delete ui ;
}
}
bool VerifyMessageDialog : : checkAddress ( )
void VerifyMessageDialog : : on_verifyMessage_clicked ( )
{
{
CDataStream ss ( SER_GETHASH , 0 ) ;
CBitcoinAddress addr ( ui - > lnAddress - > text ( ) . toStdString ( ) ) ;
ss < < strMessageMagic ;
if ( ! addr . IsValid ( ) )
ss < < ui - > edMessage - > document ( ) - > toPlainText ( ) . toStdString ( ) ;
uint256 hash = Hash ( ss . begin ( ) , ss . end ( ) ) ;
bool invalid = true ;
std : : vector < unsigned char > vchSig = DecodeBase64 ( ui - > lnSig - > text ( ) . toStdString ( ) . c_str ( ) , & invalid ) ;
if ( invalid )
{
{
QMessageBox : : warning ( this , tr ( " Invalid Signature " ) , tr ( " The signature could not be decoded. Please check the signature and try again. " ) ) ;
ui - > lnAddress - > setValid ( false ) ;
return false ;
ui - > lblStatus - > setStyleSheet ( " QLabel { color: red; } " ) ;
ui - > lblStatus - > setText ( tr ( " \" %1 \" is not a valid address. " ) . arg ( ui - > lnAddress - > text ( ) ) + QString ( " " ) + tr ( " Please check the address and try again. " ) ) ;
return ;
}
}
CKeyID keyID ;
CKey key ;
if ( ! addr . GetKeyID ( keyID ) )
if ( ! key . SetCompactSignature ( hash , vchSig ) )
{
{
QMessageBox : : warning ( this , tr ( " Invalid Signature " ) , tr ( " The signature did not match the message digest. Please check the signature and try again. " ) ) ;
ui - > lnAddress - > setValid ( false ) ;
return false ;
ui - > lblStatus - > setStyleSheet ( " QLabel { color: red; } " ) ;
ui - > lblStatus - > setText ( tr ( " \" %1 \" does not refer to a key. " ) . arg ( ui - > lnAddress - > text ( ) ) + QString ( " " ) + tr ( " Please check the address and try again. " ) ) ;
return ;
}
}
CBitcoinAddress address ( key . GetPubKey ( ) . GetID ( ) ) ;
bool fInvalid = false ;
QString qStringAddress = QString : : fromStdString ( address . ToString ( ) ) ;
std : : vector < unsigned char > vchSig = DecodeBase64 ( ui - > lnSig - > text ( ) . toStdString ( ) . c_str ( ) , & fInvalid ) ;
ui - > lnAddress - > setText ( qStringAddress ) ;
ui - > copyToClipboard - > setEnabled ( true ) ;
QString label = model - > labelForAddress ( qStringAddress ) ;
if ( fInvalid )
ui - > lblStatus - > setText ( label . isEmpty ( ) ? tr ( " Address not found in address book. " ) : tr ( " Address found in address book: %1 " ) . arg ( label ) ) ;
{
return true ;
ui - > lnSig - > setValid ( false ) ;
ui - > lblStatus - > setStyleSheet ( " QLabel { color: red; } " ) ;
ui - > lblStatus - > setText ( tr ( " The signature could not be decoded. " ) + QString ( " " ) + tr ( " Please check the signature and try again. " ) ) ;
return ;
}
}
void VerifyMessageDialog : : on_verifyMessage_clicked ( )
CDataStream ss ( SER_GETHASH , 0 ) ;
ss < < strMessageMagic ;
ss < < ui - > edMessage - > document ( ) - > toPlainText ( ) . toStdString ( ) ;
CKey key ;
if ( ! key . SetCompactSignature ( Hash ( ss . begin ( ) , ss . end ( ) ) , vchSig ) )
{
{
checkAddress ( ) ;
ui - > lnSig - > setValid ( false ) ;
ui - > lblStatus - > setStyleSheet ( " QLabel { color: red; } " ) ;
ui - > lblStatus - > setText ( tr ( " The signature did not match the message digest. " ) + QString ( " " ) + tr ( " Please check the signature and try again. " ) ) ;
return ;
}
}
void VerifyMessageDialog : : on_copyToClipboard_clicked ( )
if ( ! ( CBitcoinAddress ( key . GetPubKey ( ) . GetID ( ) ) = = addr ) )
{
{
QApplication : : clipboard ( ) - > setText ( ui - > lnAddress - > text ( ) ) ;
ui - > lblStatus - > setStyleSheet ( " QLabel { color: red; } " ) ;
ui - > lblStatus - > setText ( QString ( " <nobr> " ) + tr ( " Message verification failed. " ) + QString ( " </nobr> " ) ) ;
return ;
}
ui - > lblStatus - > setStyleSheet ( " QLabel { color: green; } " ) ;
ui - > lblStatus - > setText ( QString ( " <nobr> " ) + tr ( " Message verified. " ) + QString ( " </nobr> " ) ) ;
}
}
void VerifyMessageDialog : : on_clearButton_clicked ( )
void VerifyMessageDialog : : on_clearButton_clicked ( )
{
{
ui - > edMessage - > clear ( ) ;
ui - > lnSig - > clear ( ) ;
ui - > lnAddress - > clear ( ) ;
ui - > lnAddress - > clear ( ) ;
ui - > lnSig - > clear ( ) ;
ui - > edMessage - > clear ( ) ;
ui - > lblStatus - > clear ( ) ;
ui - > lblStatus - > clear ( ) ;
ui - > edMessage - > setFocus ( ) ;
ui - > edMessage - > setFocus ( ) ;
@ -98,6 +110,8 @@ bool VerifyMessageDialog::eventFilter(QObject *object, QEvent *event)
if ( object = = ui - > lnAddress & & ( event - > type ( ) = = QEvent : : MouseButtonPress | |
if ( object = = ui - > lnAddress & & ( event - > type ( ) = = QEvent : : MouseButtonPress | |
event - > type ( ) = = QEvent : : FocusIn ) )
event - > type ( ) = = QEvent : : FocusIn ) )
{
{
// set lnAddress to valid, as QEvent::FocusIn would not reach QValidatedLineEdit::focusInEvent
ui - > lnAddress - > setValid ( true ) ;
ui - > lnAddress - > selectAll ( ) ;
ui - > lnAddress - > selectAll ( ) ;
return true ;
return true ;
}
}