electrum-bitcoinprivate/gui/qt/paytoedit.py

294 lines
8.7 KiB
Python
Raw Normal View History

2014-06-03 12:53:25 -07:00
#!/usr/bin/env python
#
# Electrum - lightweight Bitcoin client
# Copyright (C) 2012 thomasv@gitorious
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qrtextedit import ScanQRTextEdit
2014-06-03 12:53:25 -07:00
2014-06-04 05:49:55 -07:00
import re
from decimal import Decimal
from electrum import bitcoin
2015-07-11 09:14:00 -07:00
import util
2014-06-04 05:49:55 -07:00
RE_ADDRESS = '[1-9A-HJ-NP-Za-km-z]{26,}'
RE_ALIAS = '(.*?)\s*\<([1-9A-HJ-NP-Za-km-z]{26,})\>'
2014-06-03 12:53:25 -07:00
2014-06-05 05:49:32 -07:00
frozen_style = "QWidget { background-color:none; border:none;}"
normal_style = "QPlainTextEdit { }"
2014-06-05 05:49:32 -07:00
class PayToEdit(ScanQRTextEdit):
2014-06-14 03:17:44 -07:00
def __init__(self, win):
ScanQRTextEdit.__init__(self)
2015-07-02 03:44:53 -07:00
self.win = win
2014-06-14 03:17:44 -07:00
self.amount_edit = win.amount_e
2014-06-03 12:53:25 -07:00
self.document().contentsChanged.connect(self.update_size)
self.heightMin = 0
self.heightMax = 150
self.c = None
2014-06-05 22:17:47 -07:00
self.textChanged.connect(self.check_text)
2014-06-05 22:48:08 -07:00
self.outputs = []
2014-09-06 03:34:42 -07:00
self.errors = []
2014-06-06 07:16:14 -07:00
self.is_pr = False
2015-07-02 03:44:53 -07:00
self.is_alias = False
self.scan_f = win.pay_to_URI
2014-06-24 13:28:54 -07:00
self.update_size()
2014-06-26 01:40:33 -07:00
self.payto_address = None
2014-06-14 03:17:44 -07:00
2015-07-02 03:44:53 -07:00
self.previous_payto = ''
2014-06-04 05:49:55 -07:00
def lock_amount(self):
2014-06-05 05:49:32 -07:00
self.amount_edit.setFrozen(True)
2014-06-04 05:49:55 -07:00
def unlock_amount(self):
2014-06-05 05:49:32 -07:00
self.amount_edit.setFrozen(False)
2014-06-04 05:49:55 -07:00
2014-06-05 05:49:32 -07:00
def setFrozen(self, b):
self.setReadOnly(b)
self.setStyleSheet(frozen_style if b else normal_style)
2015-03-14 04:56:00 -07:00
for button in self.buttons:
button.setHidden(b)
2014-06-05 05:15:58 -07:00
def setGreen(self):
2015-07-11 09:14:00 -07:00
self.setStyleSheet(util.GREEN_BG)
2014-06-05 05:15:58 -07:00
def setExpired(self):
2015-07-11 09:14:00 -07:00
self.setStyleSheet(util.RED_BG)
2014-06-06 07:16:14 -07:00
2014-06-05 03:20:15 -07:00
def parse_address_and_amount(self, line):
x, y = line.split(',')
n = re.match('^SCRIPT\s+([0-9a-fA-F]+)$', x.strip())
if n:
2015-08-19 00:12:46 -07:00
script = str(n.group(1)).decode('hex')
amount = self.parse_amount(y)
2015-08-19 00:12:46 -07:00
return 'script', script, amount
2014-06-27 08:08:20 -07:00
else:
address = self.parse_address(x)
amount = self.parse_amount(y)
2015-08-19 00:12:46 -07:00
return 'address', address, amount
2014-06-05 03:20:15 -07:00
def parse_amount(self, x):
p = pow(10, self.amount_edit.decimal_point())
2015-08-19 00:12:46 -07:00
return int(p * Decimal(x.strip()))
2014-06-05 03:20:15 -07:00
def parse_address(self, line):
r = line.strip()
m = re.match('^'+RE_ALIAS+'$', r)
2015-08-19 00:12:46 -07:00
address = str(m.group(2)) if m else r
2014-06-05 03:20:15 -07:00
assert bitcoin.is_address(address)
return address
2014-06-04 05:49:55 -07:00
def check_text(self):
2014-09-06 03:34:42 -07:00
self.errors = []
2014-06-06 07:16:14 -07:00
if self.is_pr:
return
2014-06-04 05:49:55 -07:00
# filter out empty lines
2015-08-19 00:12:46 -07:00
lines = filter(lambda x: x, self.lines())
2014-06-04 05:49:55 -07:00
outputs = []
total = 0
2014-06-05 22:17:47 -07:00
self.payto_address = None
2014-06-05 03:20:15 -07:00
if len(lines) == 1:
2015-01-31 11:41:28 -08:00
data = lines[0]
if data.startswith("bitcoin:"):
self.scan_f(data)
return
2014-06-05 03:20:15 -07:00
try:
2015-01-31 11:41:28 -08:00
self.payto_address = self.parse_address(data)
2014-06-05 03:20:15 -07:00
except:
2014-06-05 22:17:47 -07:00
pass
2014-06-05 03:20:15 -07:00
if self.payto_address:
self.unlock_amount()
return
2014-09-06 03:34:42 -07:00
for i, line in enumerate(lines):
2014-06-04 05:49:55 -07:00
try:
2015-08-19 00:12:46 -07:00
_type, to_address, amount = self.parse_address_and_amount(line)
2014-06-04 05:49:55 -07:00
except:
2014-09-06 03:34:42 -07:00
self.errors.append((i, line.strip()))
2014-06-04 05:49:55 -07:00
continue
2015-08-19 00:12:46 -07:00
outputs.append((_type, to_address, amount))
2014-06-04 05:49:55 -07:00
total += amount
self.outputs = outputs
2014-06-05 03:20:15 -07:00
self.payto_address = None
2014-06-27 08:08:20 -07:00
if outputs:
2014-06-05 03:20:15 -07:00
self.amount_edit.setAmount(total)
else:
self.amount_edit.setText("")
2014-06-04 05:49:55 -07:00
if total or len(lines)>1:
self.lock_amount()
else:
self.unlock_amount()
2014-06-05 03:20:15 -07:00
def get_errors(self):
return self.errors
def get_outputs(self):
2014-06-05 22:58:46 -07:00
if self.payto_address:
try:
amount = self.amount_edit.get_amount()
except:
amount = None
2014-07-08 10:38:16 -07:00
self.outputs = [('address', self.payto_address, amount)]
2014-06-05 22:58:46 -07:00
2014-06-12 02:27:18 -07:00
return self.outputs[:]
2014-06-05 03:20:15 -07:00
2014-06-04 05:49:55 -07:00
def lines(self):
2015-08-15 03:17:43 -07:00
return unicode(self.toPlainText()).split('\n')
2014-06-04 05:49:55 -07:00
def is_multiline(self):
return len(self.lines()) > 1
2015-04-26 04:16:09 -07:00
def paytomany(self):
from electrum.i18n import _
self.setText("\n\n\n")
self.update_size()
msg = '\n'.join([
2015-04-26 04:31:39 -07:00
_('Enter a list of outputs in the \'Pay to\' field.'),
2015-04-26 04:25:36 -07:00
_('One output per line.'),
2015-04-26 04:31:39 -07:00
_('Format: address, amount.'),
2015-04-26 04:25:36 -07:00
_('You may load a CSV file using the file icon.')
2015-04-26 04:16:09 -07:00
])
QMessageBox.warning(self, _('Pay to many'), msg, _('OK'))
2014-06-04 05:49:55 -07:00
2014-06-03 12:53:25 -07:00
def update_size(self):
docHeight = self.document().size().height()
h = docHeight*17 + 11
if self.heightMin <= h <= self.heightMax:
self.setMinimumHeight(h)
self.setMaximumHeight(h)
self.verticalScrollBar().hide()
2014-06-03 12:53:25 -07:00
def setCompleter(self, completer):
self.c = completer
self.c.setWidget(self)
self.c.setCompletionMode(QCompleter.PopupCompletion)
self.c.activated.connect(self.insertCompletion)
def insertCompletion(self, completion):
if self.c.widget() != self:
return
tc = self.textCursor()
extra = completion.length() - self.c.completionPrefix().length()
tc.movePosition(QTextCursor.Left)
tc.movePosition(QTextCursor.EndOfWord)
tc.insertText(completion.right(extra))
self.setTextCursor(tc)
2014-06-03 12:53:25 -07:00
def textUnderCursor(self):
tc = self.textCursor()
tc.select(QTextCursor.WordUnderCursor)
return tc.selectedText()
def keyPressEvent(self, e):
if self.isReadOnly():
return
2014-06-03 12:53:25 -07:00
if self.c.popup().isVisible():
if e.key() in [Qt.Key_Enter, Qt.Key_Return]:
e.ignore()
return
2014-06-04 05:49:55 -07:00
if e.key() in [Qt.Key_Tab]:
e.ignore()
return
if e.key() in [Qt.Key_Down, Qt.Key_Up] and not self.is_multiline():
e.ignore()
return
QPlainTextEdit.keyPressEvent(self, e)
2014-06-03 12:53:25 -07:00
ctrlOrShift = e.modifiers() and (Qt.ControlModifier or Qt.ShiftModifier)
if self.c is None or (ctrlOrShift and e.text().isEmpty()):
return
eow = QString("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-=")
hasModifier = (e.modifiers() != Qt.NoModifier) and not ctrlOrShift;
completionPrefix = self.textUnderCursor()
2014-06-27 08:08:20 -07:00
if hasModifier or e.text().isEmpty() or completionPrefix.length() < 1 or eow.contains(e.text().right(1)):
2014-06-03 12:53:25 -07:00
self.c.popup().hide()
return
if completionPrefix != self.c.completionPrefix():
self.c.setCompletionPrefix(completionPrefix);
self.c.popup().setCurrentIndex(self.c.completionModel().index(0, 0))
cr = self.cursorRect()
cr.setWidth(self.c.popup().sizeHintForColumn(0) + self.c.popup().verticalScrollBar().sizeHint().width())
self.c.complete(cr)
def qr_input(self):
data = super(PayToEdit,self).qr_input()
if data.startswith("bitcoin:"):
self.scan_f(data)
# TODO: update fee
2015-07-02 03:44:53 -07:00
def resolve(self):
self.is_alias = False
if self.hasFocus():
return
if self.is_multiline(): # only supports single line entries atm
return
if self.is_pr:
return
key = str(self.toPlainText())
if key == self.previous_payto:
return
self.previous_payto = key
if not (('.' in key) and (not '<' in key) and (not ' ' in key)):
return
try:
data = self.win.contacts.resolve(key)
except:
return
if not data:
return
self.is_alias = True
address = data.get('address')
name = data.get('name')
new_url = key + ' <' + address + '>'
self.setText(new_url)
self.previous_payto = new_url
#if self.win.config.get('openalias_autoadd') == 'checked':
self.win.contacts[key] = ('openalias', name)
self.win.update_contacts_tab()
self.setFrozen(True)
if data.get('type') == 'openalias':
self.validated = data.get('validated')
if self.validated:
self.setGreen()
else:
self.setExpired()
else:
self.validated = None