Skip to content

Commit

Permalink
99% done with calc + ecdsa sig/verify!
Browse files Browse the repository at this point in the history
  • Loading branch information
etotheipi committed Feb 25, 2012
1 parent 341ea65 commit bc95958
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 21 deletions.
Binary file added img/MsgBox_good48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/checkmark_okay.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 74 additions & 2 deletions qtdefines.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
WLTTYPES = enum('Plain', 'Crypt', 'WatchOnly', 'Offline')
WLTFIELDS = enum('Name', 'Descr', 'WltID', 'NumAddr', 'Secure', \
'BelongsTo', 'Crypto', 'Time', 'Mem')
MSGBOX = enum('Info', 'Question', 'Warning', 'Critical', 'Error')
MSGBOX = enum('Good','Info', 'Question', 'Warning', 'Critical', 'Error')

STYLE_SUNKEN = QFrame.Box | QFrame.Sunken
STYLE_RAISED = QFrame.Box | QFrame.Raised
Expand Down Expand Up @@ -246,14 +246,86 @@ def createToolTipObject(tiptext, iconSz=2):
return lbl


################################################################################
def MsgBoxCustom(wtype, title, msg, wCancel=False):
"""
Creates a warning/question/critical dialog, but with a "Do not ask again"
checkbox. Will return a pair (response, DNAA-is-checked)
"""

class dlgWarn(QDialog):
def __init__(self, dtype, dtitle, wmsg, withCancel=False):
super(dlgWarn, self).__init__(None)

msgIcon = QLabel()
fpix = ''
if dtype==MSGBOX.Good:
fpix = 'img/MsgBox_good48.png'
if dtype==MSGBOX.Info:
fpix = 'img/MsgBox_info48.png'
if dtype==MSGBOX.Question:
fpix = 'img/MsgBox_question64.png'
if dtype==MSGBOX.Warning:
fpix = 'img/MsgBox_warning48.png'
if dtype==MSGBOX.Critical:
fpix = 'img/MsgBox_critical64.png'
if dtype==MSGBOX.Error:
fpix = 'img/MsgBox_error64.png'


if len(fpix)>0:
msgIcon.setPixmap(QPixmap(fpix))
msgIcon.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)

lblMsg = QLabel(msg)
lblMsg.setTextFormat(Qt.RichText)
lblMsg.setWordWrap(True)
lblMsg.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
w,h = tightSizeNChar(lblMsg, 50)
lblMsg.setMinimumSize( w, 3.2*h )

buttonbox = QDialogButtonBox()

if dtype==MSGBOX.Question:
btnYes = QPushButton('Yes')
btnNo = QPushButton('No')
self.connect(btnYes, SIGNAL('clicked()'), self.accept)
self.connect(btnNo, SIGNAL('clicked()'), self.reject)
buttonbox.addButton(btnYes,QDialogButtonBox.AcceptRole)
buttonbox.addButton(btnNo, QDialogButtonBox.RejectRole)
else:
btnOk = QPushButton('Ok')
self.connect(btnOk, SIGNAL('clicked()'), self.accept)
buttonbox.addButton(btnOk, QDialogButtonBox.AcceptRole)
if withCancel:
btnOk = QPushButton('Cancel')
self.connect(btnOk, SIGNAL('clicked()'), self.reject)
buttonbox.addButton(btnOk, QDialogButtonBox.RejectRole)


spacer = QSpacerItem(20, 10, QSizePolicy.Fixed, QSizePolicy.Expanding)


layout = QGridLayout()
layout.addItem( spacer, 0,0, 1,2)
layout.addWidget(msgIcon, 1,0, 1,1)
layout.addWidget(lblMsg, 1,1, 1,1)
layout.addWidget(buttonbox, 3,0, 1,2)
layout.setSpacing(20)
self.setLayout(layout)
self.setWindowTitle(dtitle)

dlg = dlgWarn(wtype, title, msg, wCancel)
result = dlg.exec_()

return result

################################################################################
def MsgBoxWithDNAA(wtype, title, msg, dnaaMsg, wCancel=False):
"""
Creates a warning/question/critical dialog, but with a "Do not ask again"
checkbox. Will return a pair (response, DNAA-is-checked)
"""


class dlgWarn(QDialog):
def __init__(self, dtype, dtitle, wmsg, dmsg=None, withCancel=False):
Expand Down
81 changes: 62 additions & 19 deletions qtdialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6218,6 +6218,9 @@ def __init__(self, parent=None, main=None):
txt.setMinimumHeight(2.2*h)
txt.sizeHint = lambda: QSize(400, 2.2*h)

self.connect(self.txtMsg, SIGNAL('textChanged()'), self.msgTextChanged)
self.connect(self.txtSig, SIGNAL('textChanged()'), self.msgTextChanged)


btnSwitchEnd = QPushButton('Switch Endian')
self.connect(btnSwitchEnd, SIGNAL('clicked()'), self.privSwitch)
Expand Down Expand Up @@ -6295,6 +6298,8 @@ def __init__(self, parent=None, main=None):
self.btnWltData = QPushButton('Get Keys From Wallet')
self.btnClearFrm = QPushButton('Clear')
self.btnCalcKeys = QPushButton('Calculate')
self.btnClearFrm.setEnabled(False)
self.btnCalcKeys.setEnabled(False)
self.btnWltData.setEnabled(False)
self.connect(self.btnWltData, SIGNAL('clicked()'), self.getOtherData)
self.connect(self.btnClearFrm, SIGNAL('clicked()'), self.clearFormData)
Expand All @@ -6310,24 +6315,29 @@ def __init__(self, parent=None, main=None):
tabKeysTopFrm = makeVertFrame( [topHeaderRow, keyDataFrame])


self.btnSignMsg = QPushButton('Sign Message')
self.btnVerify = QPushButton('Verify Signature')
self.btnSignMsg = QPushButton('Sign Message')
self.btnChallenge = QPushButton('Make Challenge')
self.btnVerify = QPushButton('Verify Signature')
self.btnSignMsg.setEnabled(False)
self.btnVerify.setEnabled(False)
self.lblSigResult = QRichLabel('')

self.connect(self.btnSignMsg, SIGNAL('clicked()'), self.signMsg)
self.connect(self.btnVerify, SIGNAL('clicked()'), self.verifyMsg)
self.connect(self.btnSignMsg, SIGNAL('clicked()'), self.signMsg)
self.connect(self.btnVerify, SIGNAL('clicked()'), self.verifyMsg)
self.connect(self.btnChallenge, SIGNAL('clicked()'), self.makeChallenge)

ttipMsg = createToolTipObject( \
'Copy a message to be signed or verified by the key data above. '
'A message to be signed or verified by the key data above. '
'Or create a random "nonce" to give to someone to sign.')
ttipSig = createToolTipObject( \
'The output of signing the message above will be put here, or you '
'can copy in a signature of the above message, and check that it '
'is valid against the public key on the left (if present).')

msgBoxHead = makeHorizFrame([QRichLabel('Message'), ttipMsg, 'Stretch', self.btnSignMsg])
sigBoxHead = makeHorizFrame([QRichLabel('Signature'), ttipSig, 'Stretch', self.btnVerify])
msgBoxHead = makeHorizFrame([QRichLabel('Message'), ttipMsg, 'Stretch', \
self.btnChallenge, self.btnSignMsg])
sigBoxHead = makeHorizFrame([QRichLabel('Signature'), ttipSig, 'Stretch', \
self.lblSigResult, self.btnVerify])
tabKeysBtmFrmLayout = QGridLayout()
tabKeysBtmFrmLayout.addWidget( msgBoxHead, 0,0)
tabKeysBtmFrmLayout.addWidget( sigBoxHead, 0,1)
Expand Down Expand Up @@ -6440,15 +6450,39 @@ def __init__(self, parent=None, main=None):



#############################################################################
def keyTextEdited(self, txtIndex):
notEmpty = not self.formIsEmpty()
self.btnClearFrm.setEnabled(notEmpty)
self.btnCalcKeys.setEnabled(notEmpty)
self.btnSignMsg.setEnabled(notEmpty)
self.btnVerify.setEnabled(notEmpty)

if not isinstance(txtIndex, (list,tuple)):
txtIndex = [txtIndex]

for i,txt in enumerate(self.keyTxtList):
if not i in txtIndex:
txt.setText('')

#############################################################################
def msgTextChanged(self):
"""
Yes, I intended to use text 'changed', instead of 'edited' here.
Because I don't care how the text was modified, it's going to break
the signature.
"""
self.lblSigResult.setText('')


#############################################################################
def formIsEmpty(self):
totalEmpty = [0 if len(str(a.text()))>0 else 1 for a in self.keyTxtList]
allEmpty = not sum(totalEmpty)!=0
self.btnSignMsg.setEnabled(not allEmpty)
return allEmpty

#############################################################################
def keyWaterfall(self):
self.returnPressedFirst = True
try:
Expand Down Expand Up @@ -6576,7 +6610,7 @@ def getOtherData(self):
# This address is ours, get the priv key and fill in everything else
wlt = self.main.walletMap[wltID]
if wlt.useEncryption and wlt.isLocked:
dlg = DlgUnlockWallet(self.wlt, self.main, 'Encrypt New Address')
dlg = DlgUnlockWallet(wlt, self.main, 'Encrypt New Address')
if not dlg.exec_():
reply = QMessageBox.critical(self, 'Wallet is locked',
'Could not unlock wallet, so private key data could not '
Expand All @@ -6595,9 +6629,6 @@ def getOtherData(self):
self.txtPubF.setText(hexPub)
self.keyWaterfall()





#############################################################################
def clearFormData(self):
Expand All @@ -6606,13 +6637,22 @@ def clearFormData(self):
self.lblPrivType.setText('')
self.lblTopMid.setText('')
self.btnWltData.setEnabled(False)
self.btnSignMsg.setEnabled(False)
self.btnVerify.setEnabled(False)

#############################################################################
def privSwitch(self):
privHex = str(self.txtPriv.text()).strip().replace(' ','')
if len(privHex)>0:
self.txtPriv.setText(hex_switchEndian(privHex))

#############################################################################
def makeChallenge(self):
rnd = SecureBinaryData().GenerateRandom(16)
msgToBeSigned = 'SignThisMessage_%s' % rnd.toHexStr()
self.txtMsg.setText(msgToBeSigned)


#############################################################################
def signMsg(self):
self.keyWaterfall()
Expand Down Expand Up @@ -6650,19 +6690,23 @@ def verifyMsg(self):
return

try:
binSig = hex_to_binary(str(self.txtSig.toPlainText().strip()).replace(' ',''))
binSig = hex_to_binary(str(self.txtSig.toPlainText()).strip().replace(' ',''))
except:
QMessageBox.critical(self, 'Input Error', \
'The signature data is not recognized.', QMessageBox.Ok)
return

strMsg = str(self.txtMsg.toPlainText().strip())
strMsg = str(self.txtMsg.toPlainText()).strip()


if len(binPub)!=65:
QMessageBox.critical(self, 'Invalid Public Key!', \
'Cannot verify a message without a valid public key.', QMessageBox.Ok)
return
if len(binSig)==0:
QMessageBox.critical(self, 'No Signature!', \
'There is no signature to verify', QMessageBox.Ok)
return
if len(strMsg)==0:
QMessageBox.critical(self, 'Nothing to Verify!', \
'Need the original message in order to verify the signature.', QMessageBox.Ok)
Expand All @@ -6673,13 +6717,12 @@ def verifyMsg(self):
SecureBinaryData(binPub))

if isValid:
QMessageBox.information(self, 'Valid!', \
'The supplied signature is valid for this message and public key!', \
QMessageBox.Ok)
MsgBoxCustom(MSGBOX.Good, 'Verified!', 'The supplied signature <b>is valid</b>!')
self.lblSigResult.setText('<font color="green">Valid Signature!</font>')
else:
QMessageBox.critical(self, 'Invalid!', \
'The supplied signature is <b>not</b> valid for this message and public key!', \
QMessageBox.Ok)
MsgBoxCustom(MSGBOX.Critical, 'Invalid Signature!', \
'The supplied signature <b>is not valid</b>!')
self.lblSigResult.setText('<font color="red">Invalid Signature!</font>')



Expand Down

0 comments on commit bc95958

Please sign in to comment.