electrum-bitcoinprivate/gui/qt/paytoedit.py

253 lines
7.9 KiB
Python
Raw Normal View History

2014-06-03 12:53:25 -07:00
#!/usr/bin/env python
#
2019-12-24 03:21:35 -08:00
# Electrum - lightweight bitcoinprivate client
2014-06-03 12:53:25 -07:00
# Copyright (C) 2012 thomasv@gitorious
#
2016-02-23 02:36:42 -08:00
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
2014-06-03 12:53:25 -07:00
#
2016-02-23 02:36:42 -08:00
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
2014-06-03 12:53:25 -07:00
#
2016-02-23 02:36:42 -08:00
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
2014-06-03 12:53:25 -07:00
2017-09-22 20:54:38 -07:00
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QLineEdit
2014-06-04 05:49:55 -07:00
import re
from decimal import Decimal
2019-12-24 03:21:35 -08:00
from electrum_bitcoinprivate import bitcoin
from electrum_bitcoinprivate.util import bfh
2014-06-04 05:49:55 -07:00
from .qrtextedit import ScanQRTextEdit
from .completion_text_edit import CompletionTextEdit
2017-01-22 10:25:24 -08:00
from . import util
2015-07-11 09:14:00 -07:00
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(CompletionTextEdit, ScanQRTextEdit):
2014-06-14 03:17:44 -07:00
def __init__(self, win):
CompletionTextEdit.__init__(self)
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-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):
self.setStyleSheet(util.ColorScheme.GREEN.as_stylesheet(True))
2014-06-05 05:15:58 -07:00
def setExpired(self):
self.setStyleSheet(util.ColorScheme.RED.as_stylesheet(True))
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(',')
out_type, out = self.parse_output(x)
amount = self.parse_amount(y)
return out_type, out, amount
def parse_output(self, x):
try:
address = self.parse_address(x)
return bitcoin.TYPE_ADDRESS, address
except:
script = self.parse_script(x)
return bitcoin.TYPE_SCRIPT, script
def parse_script(self, x):
2019-12-24 03:21:35 -08:00
from electrum_bitcoinprivate.transaction import opcodes, push_script
script = ''
for word in x.split():
if word[0:3] == 'OP_':
assert word in opcodes.lookup
opcode_int = opcodes.lookup[word]
assert opcode_int < 256 # opcode is single-byte
script += bitcoin.int_to_hex(opcode_int)
else:
bfh(word) # to test it is hex data
script += push_script(word)
return script
2014-06-05 03:20:15 -07:00
def parse_amount(self, x):
if x.strip() == '!':
return '!'
2014-06-05 03:20:15 -07:00
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 04:33:00 -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
2017-01-30 01:36:56 -08:00
lines = [i for i in self.lines() if i]
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]
2019-12-24 03:21:35 -08:00
if data.startswith("bitcoinprivate:"):
2015-01-31 11:41:28 -08:00
self.scan_f(data)
return
2014-06-05 03:20:15 -07:00
try:
self.payto_address = self.parse_output(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.win.lock_amount(False)
2014-06-05 03:20:15 -07:00
return
2016-12-31 08:10:45 -08:00
is_max = False
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))
if amount == '!':
2016-12-31 08:10:45 -08:00
is_max = True
else:
total += amount
2014-06-04 05:49:55 -07:00
2016-12-31 08:10:45 -08:00
self.win.is_max = is_max
2014-06-04 05:49:55 -07:00
self.outputs = outputs
2014-06-05 03:20:15 -07:00
self.payto_address = None
if self.win.is_max:
self.win.do_update_fee()
else:
self.amount_edit.setAmount(total if outputs else None)
self.win.lock_amount(total or len(lines)>1)
2014-06-05 03:20:15 -07:00
def get_errors(self):
return self.errors
def get_recipient(self):
return self.payto_address
def get_outputs(self, is_max):
2014-06-05 22:58:46 -07:00
if self.payto_address:
if is_max:
amount = '!'
else:
2014-06-05 22:58:46 -07:00
amount = self.amount_edit.get_amount()
_type, addr = self.payto_address
self.outputs = [(_type, addr, 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):
2017-01-30 01:36:56 -08:00
return 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):
self.setText("\n\n\n")
self.update_size()
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()
lineEditHeight = QLineEdit().sizeHint().height()
lineHeight = self.fontMetrics().height()
h = lineEditHeight + lineHeight * (docHeight - 1)
if self.heightMin <= h <= self.heightMax:
self.setMinimumHeight(h)
self.setMaximumHeight(h)
self.verticalScrollBar().hide()
2014-06-03 12:53:25 -07:00
def qr_input(self):
data = super(PayToEdit,self).qr_input()
2019-12-24 03:21:35 -08:00
if data.startswith("bitcoinprivate:"):
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
parts = key.split(sep=',') # assuming single line
if parts and len(parts) > 0 and bitcoin.is_address(parts[0]):
return
2015-07-02 03:44:53 -07:00
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)
2016-05-28 07:34:08 -07:00
self.win.contact_list.on_update()
2015-07-02 03:44:53 -07:00
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