forked from etotheipi/BitcoinArmory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toolsDialogs.py
361 lines (309 loc) · 15.7 KB
/
toolsDialogs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
################################################################################
# #
# Copyright (C) 2011-2015, Armory Technologies, Inc. #
# Distributed under the GNU Affero General Public License (AGPL v3) #
# See LICENSE or http://www.gnu.org/licenses/agpl.html #
# #
################################################################################
from PyQt4 import Qt, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from armorycolors import htmlColor
from jasvet import ASv0, ASv1B64, ASv1CS, verifySignature, readSigBlock
from qtdefines import *
from qtdialogs import MIN_PASSWD_WIDTH, DlgPasswd3, createAddrBookButton,\
DlgUnlockWallet
from armoryengine.ArmoryUtils import isASCII
from announcefetch import ANNOUNCE_SIGN_PUBKEY
class MessageSigningVerificationDialog(ArmoryDialog):
def __init__(self, parent=None, main=None):
super(MessageSigningVerificationDialog, self).__init__(parent, main)
layout = QVBoxLayout()
self.setWindowTitle("Message Signing/Verification")
self.setWindowIcon(QIcon( self.main.iconfile))
self.setMinimumWidth(600)
tabbedPanel = QTabWidget()
messageSigningTab = MessageSigningWidget(parent, main)
bareSignatureVerificationTab = BareSignatureVerificationWidget(parent, main)
signedMsgBlockVerificationTab = SignedMessageBlockVerificationWidget(parent, main)
tabbedPanel.addTab(messageSigningTab, "Sign Message")
tabbedPanel.addTab(bareSignatureVerificationTab, "Verify Bare Signature")
tabbedPanel.addTab(signedMsgBlockVerificationTab, "Verify Signed Message Block")
layout.addWidget(tabbedPanel)
self.goBackButton = QPushButton("Done")
actionButtonBox = QDialogButtonBox()
actionButtonBox.addButton(self.goBackButton, QDialogButtonBox.RejectRole)
layout.addWidget(actionButtonBox)
self.setLayout(layout)
self.connect(self.goBackButton, SIGNAL('clicked()'), \
self, SLOT('reject()'))
def clearFields(self):
self.addressLineEdit.setText('')
self.messageTextEdit.setPlainText('')
self.signatureDisplay.setPlainText('')
class MessageSigningWidget(QWidget):
def __init__(self, parent=None, main=None):
super(MessageSigningWidget, self).__init__(parent)
self.main = main
signMessageLayout = QGridLayout()
self.setMinimumWidth(800)
# Pick an Address in Row 0 of the grid layout
addressLabel = QLabel('Sign with Address:')
self.addressLineEdit = QLineEdit()
self.addressBookButton = createAddrBookButton(self, self.addressLineEdit, None,
selectMineOnly=True, showLockboxes=False)
signMessageLayout.addWidget(addressLabel, 0, 0)
signMessageLayout.addWidget(self.addressLineEdit, 0, 1)
signMessageLayout.addWidget(self.addressBookButton, 0, 2)
# Create a message in Row 1
messageLabel = QLabel("Message to sign:")
self.messageTextEdit = QTextEdit()
self.messageTextEdit.setAcceptRichText(False)
self.messageTextEdit.setStyleSheet("font: 9pt \"Courier\";")
signMessageLayout.addWidget(messageLabel, 1, 0)
signMessageLayout.addWidget(self.messageTextEdit, 1, 1, 1, 2)
# Create a row with just a sign message button
self.bareSigButton = QPushButton('Bare Signature (Bitcoin-Qt Compatible)')
self.base64SigButton = QPushButton('Base64 Signature')
self.clearSigButton = QPushButton('Clearsign Signature')
sigButtonFrame = makeHorizFrame([self.bareSigButton,\
self.base64SigButton,\
self.clearSigButton,\
'Stretch'])
signMessageLayout.addWidget(sigButtonFrame, 2, 1, 1, 3)
# Create a Signature display
signatureLabel = QLabel('Message Signature:')
self.signatureDisplay = QTextEdit()
self.signatureDisplay.setReadOnly(True)
self.signatureDisplay.setStyleSheet("font: 9pt \"Courier\"; background-color: #bbbbbb;")
signMessageLayout.addWidget(signatureLabel, 3, 0)
signMessageLayout.addWidget(self.signatureDisplay, 3, 1, 1, 2)
self.copySignatureButton = QPushButton("Copy Signature")
self.clearFieldsButton = QPushButton("Clear All")
buttonFrame = makeHorizFrame([self.copySignatureButton, self.clearFieldsButton,'Stretch'])
signMessageLayout.addWidget(buttonFrame, 4, 1, 1, 3)
self.setLayout(signMessageLayout)
self.connect(self.bareSigButton, SIGNAL('clicked()'), \
self.bareSignMessage)
self.connect(self.base64SigButton, SIGNAL('clicked()'), \
self.base64SignMessage)
self.connect(self.clearSigButton, SIGNAL('clicked()'), \
self.clearSignMessage)
self.connect(self.copySignatureButton, SIGNAL('clicked()'), \
self.copySignature)
self.connect(self.clearFieldsButton, SIGNAL('clicked()'), \
self.clearFields)
def getPrivateKeyFromAddrInput(self):
atype, addr160 = addrStr_to_hash160(str(self.addressLineEdit.text()))
if atype==P2SHBYTE:
LOGWARN('P2SH address requested')
walletId = self.main.getWalletForAddr160(addr160)
wallet = self.main.walletMap[walletId]
if wallet.useEncryption and wallet.isLocked:
# Target wallet is encrypted...
unlockdlg = DlgUnlockWallet(wallet, self, self.main, 'Unlock Wallet to Import')
if not unlockdlg.exec_():
QMessageBox.critical(self, 'Wallet is Locked', \
'Cannot import private keys without unlocking wallet!', \
QMessageBox.Ok)
return
return wallet.addrMap[addr160].binPrivKey32_Plain.toBinStr()
def bareSignMessage(self):
messageText = str(self.messageTextEdit.toPlainText())
if not isASCII(messageText):
QMessageBox.warning(self, 'Non ASCII Text', 'Message to sign must be ASCII', QMessageBox.Ok)
else:
try:
privateKey = self.getPrivateKeyFromAddrInput()
if privateKey:
signature = ASv0(privateKey, messageText)
self.signatureDisplay.setPlainText(signature['b64-signature'])
else:
QMessageBox.warning(self, 'Private Key Not Known', 'The private key is not known for this address.', QMessageBox.Ok)
except:
QMessageBox.warning(self, 'Invalid Address', 'The signing address is invalid.', QMessageBox.Ok)
raise
def base64SignMessage(self):
messageText = str(self.messageTextEdit.toPlainText())
if not isASCII(messageText):
QMessageBox.warning(self, 'Non ASCII Text', 'Message to sign must be ASCII', QMessageBox.Ok)
else:
try:
privateKey = self.getPrivateKeyFromAddrInput()
if privateKey:
signature = ASv1B64(self.getPrivateKeyFromAddrInput(), messageText)
self.signatureDisplay.setPlainText(signature)
else:
QMessageBox.warning(self, 'Private Key Not Known', 'The private key is not known for this address.', QMessageBox.Ok)
except:
QMessageBox.warning(self, 'Invalid Address', 'The signing address is invalid.', QMessageBox.Ok)
raise
def clearSignMessage(self):
messageText = str(self.messageTextEdit.toPlainText())
if not isASCII(messageText):
QMessageBox.warning(self, 'Non ASCII Text', 'Message to sign must be ASCII', QMessageBox.Ok)
else:
try:
privateKey = self.getPrivateKeyFromAddrInput()
except:
QMessageBox.warning(self, 'Invalid Address', 'The signing address is invalid.', QMessageBox.Ok)
raise
if privateKey:
signature = ASv1CS(privateKey, messageText)
self.signatureDisplay.setPlainText(signature)
else:
QMessageBox.warning(self, 'Private Key Not Known', 'The private key is not known for this address.', QMessageBox.Ok)
def copySignature(self):
clipb = QApplication.clipboard()
clipb.clear()
clipb.setText(str(self.signatureDisplay.toPlainText()))
def clearFields(self):
self.addressLineEdit.setText('')
self.messageTextEdit.setPlainText('')
self.signatureDisplay.setPlainText('')
# Intended to be a base class
class SignatureVerificationWidget(QWidget):
def __init__(self, parent=None, main=None):
super(SignatureVerificationWidget, self).__init__(parent)
self.main = main
self.signMessageLayout = QGridLayout()
self.setMinimumWidth(800)
self.verifySignatureButton = QPushButton("Verify Signature")
self.clearFieldsButton = QPushButton("Clear All")
self.lblSigResult = QRichLabel('', doWrap=False)
buttonFrame = makeHorizFrame([self.verifySignatureButton, self.clearFieldsButton,\
'Stretch', self.lblSigResult])
self.signMessageLayout.addWidget(buttonFrame, 3, 1, 1, 2)
self.setLayout(self.signMessageLayout)
self.connect(self.verifySignatureButton, SIGNAL('clicked()'), \
self.verifySignature)
self.connect(self.clearFieldsButton, SIGNAL('clicked()'), \
self.clearFields)
# To be implemented by child classes
def verifySignature(self):
pass
def clearFields(self):
self.lblSigResult.setText('')
def displayVerifiedBox(self, addrB58, messageString):
atihash160 = hash160(hex_to_binary(ANNOUNCE_SIGN_PUBKEY))
addrDisp = addrB58
if addrB58==hash160_to_addrStr(atihash160):
addrDisp = '<b>Armory Technologies, Inc.</b>'
if CLI_OPTIONS.testAnnounceCode:
ownerStr = tr("""
<font color="%s"><b>Armory Technologies, Inc.
(testing key)</b></font> has signed the following
block of text:<br>""") % htmlColor('TextGreen')
else:
ownerStr = tr("""
<font color="%s"><b>Armory Technologies, Inc.</b></font>
has signed the following block of text:<br>""") % \
htmlColor('TextGreen')
else:
ownerStr = tr("""
The owner of the following Bitcoin address...
<br>
<blockquote>
<font face="Courier" size=4 color="#000060"><b>%s</b></font>
</blockquote>
<br>
... has produced a <b><u>valid</u></b> signature for
the following message:<br>
""") % addrB58
if addrB58:
msg = messageString.replace('\r\n','\n')
msg = ' ' + '<br> '.join(msg.split('\n'))
# The user will be able to see the entire message
# in the Message Signing/Verification dialog
msg = '<br>'.join([line[:60]+ '...'*(len(line)>60) for line in msg.split('<br>')][:12])
MsgBoxCustom(MSGBOX.Good, tr('Verified!'), tr("""
%s
<hr>
<blockquote>
<font face="Courier" color="#000060"><b>%s</b></font>
</blockquote>
<hr><br>
<b>Please</b> make sure that the address above (%s...) matches the
exact address you were expecting. A valid signature is meaningless
unless it is made
from a recognized address!""") % (ownerStr, msg, addrB58[:10]))
self.lblSigResult.setText(\
'<font color="green">Valid Signature by %s</font>' % addrDisp)
else:
self.displayInvalidSignatureMessage()
def displayInvalidSignatureMessage(self):
MsgBoxCustom(MSGBOX.Error, 'Invalid Signature!', \
'The supplied signature <b>is not valid</b>!')
self.lblSigResult.setText('<font color="red">Invalid Signature!</font>')
class BareSignatureVerificationWidget(SignatureVerificationWidget):
def __init__(self, parent=None, main=None):
super(BareSignatureVerificationWidget, self).__init__(parent, main)
# Pick an Address in Row 0 of the grid layout
addressLabel = QLabel('Signing Address:')
self.addressLineEdit = QLineEdit()
self.addressBookButton = createAddrBookButton(self, self.addressLineEdit, None,
selectMineOnly=True, showLockboxes=False)
self.signMessageLayout.addWidget(addressLabel, 0, 0)
self.signMessageLayout.addWidget(self.addressLineEdit, 0, 1)
self.signMessageLayout.addWidget(self.addressBookButton, 0, 2)
# Create a message text box
messageLabel = QLabel("Signed Message:")
self.messageTextEdit = QTextEdit()
self.messageTextEdit.setAcceptRichText(False)
self.messageTextEdit.setStyleSheet("font: 9pt \"Courier\";")
self.signMessageLayout.addWidget(messageLabel, 1, 0)
self.signMessageLayout.addWidget(self.messageTextEdit, 1, 1)
# Create a Signature display
signatureLabel = QLabel('Signature:')
self.signatureTextEdit = QTextEdit()
self.signatureTextEdit.setStyleSheet("font: 9pt \"Courier\";")
self.signMessageLayout.addWidget(signatureLabel, 2, 0)
self.signMessageLayout.addWidget(self.signatureTextEdit, 2, 1)
def verifySignature(self):
messageString = str(self.messageTextEdit.toPlainText())
try:
addrB58 = verifySignature(str(self.signatureTextEdit.toPlainText()), \
messageString, 'v0', ord(ADDRBYTE))
if addrB58 == str(self.addressLineEdit.text()):
self.displayVerifiedBox(addrB58, messageString)
else:
self.displayInvalidSignatureMessage()
except:
self.displayInvalidSignatureMessage()
raise
def clearFields(self):
super(BareSignatureVerificationWidget, self).clearFields()
self.addressLineEdit.setText('')
self.messageTextEdit.setPlainText('')
self.signatureTextEdit.setPlainText('')
class SignedMessageBlockVerificationWidget(SignatureVerificationWidget):
def __init__(self, parent=None, main=None):
super(SignedMessageBlockVerificationWidget, self).__init__(parent, main)
# Create a Signature display
signatureLabel = QLabel('Signed Message Block:')
self.signedMessageBlockTextEdit = QTextEdit()
self.signedMessageBlockTextEdit.setStyleSheet("font: 9pt \"Courier\";")
self.signedMessageBlockTextEdit.setAcceptRichText(False)
self.signMessageLayout.addWidget(signatureLabel, 0, 0)
self.signMessageLayout.addWidget(self.signedMessageBlockTextEdit, 0, 1)
# Create a message in Row 1
messageLabel = QLabel("Message:")
self.messageTextEdit = QTextEdit()
self.messageTextEdit.setAcceptRichText(False)
self.messageTextEdit.setReadOnly(True)
self.messageTextEdit.setStyleSheet("font: 9pt \"Courier\"; background-color: #bbbbbb;")
self.signMessageLayout.addWidget(messageLabel, 1, 0)
self.signMessageLayout.addWidget(self.messageTextEdit, 1, 1, 1, 2)
def verifySignature(self):
try:
sig, msg = readSigBlock(str(self.signedMessageBlockTextEdit.toPlainText()))
addrB58 = verifySignature(sig, msg, 'v1', ord(ADDRBYTE) )
self.displayVerifiedBox(addrB58, msg)
self.messageTextEdit.setPlainText(msg)
except:
self.displayInvalidSignatureMessage()
raise
def clearFields(self):
super(SignedMessageBlockVerificationWidget, self).clearFields()
self.signedMessageBlockTextEdit.setPlainText('')
self.messageTextEdit.setPlainText('')