Browse Source

misc ui and lib fixes for py3

seed_v14
Dmitry Sorokin 8 years ago
committed by ThomasV
parent
commit
f02d18226b
  1. 25
      gui/qt/main_window.py
  2. 4
      gui/qt/transaction_dialog.py
  3. 0
      lib/account.py
  4. 8
      lib/base_wizard.py
  5. 19
      lib/bitcoin.py
  6. 21
      lib/blockchain.py
  7. 2
      lib/util.py
  8. 10
      lib/verifier.py
  9. 5
      lib/wallet.py

25
gui/qt/main_window.py

@ -1888,7 +1888,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
@protected @protected
def do_sign(self, address, message, signature, password): def do_sign(self, address, message, signature, password):
address = str(address.text()).strip() address = address.text().strip()
message = message.toPlainText().strip() message = message.toPlainText().strip()
if not bitcoin.is_address(address): if not bitcoin.is_address(address):
self.show_message('Invalid Bitcoin address.') self.show_message('Invalid Bitcoin address.')
@ -1901,12 +1901,12 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
return return
task = partial(self.wallet.sign_message, address, message, password) task = partial(self.wallet.sign_message, address, message, password)
def show_signed_message(sig): def show_signed_message(sig):
signature.setText(base64.b64encode(sig)) signature.setText(base64.b64encode(sig).decode('ascii'))
self.wallet.thread.add(task, on_success=show_signed_message) self.wallet.thread.add(task, on_success=show_signed_message)
def do_verify(self, address, message, signature): def do_verify(self, address, message, signature):
address = str(address.text()).strip() address = address.text().strip()
message = message.toPlainText().strip() message = message.toPlainText().strip().encode('utf8')
if not bitcoin.is_address(address): if not bitcoin.is_address(address):
self.show_message('Invalid Bitcoin address.') self.show_message('Invalid Bitcoin address.')
return return
@ -1917,14 +1917,13 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
# This can throw on invalid base64 # This can throw on invalid base64
sig = base64.b64decode(str(signature.toPlainText())) sig = base64.b64decode(str(signature.toPlainText()))
verified = bitcoin.verify_message(address, sig, message) verified = bitcoin.verify_message(address, sig, message)
except: except Exception as e:
verified = False verified = False
if verified: if verified:
self.show_message(_("Signature verified")) self.show_message(_("Signature verified"))
else: else:
self.show_error(_("Wrong signature")) self.show_error(_("Wrong signature"))
def sign_verify_message(self, address=''): def sign_verify_message(self, address=''):
d = WindowModalDialog(self, _('Sign/verify Message')) d = WindowModalDialog(self, _('Sign/verify Message'))
d.setMinimumSize(410, 290) d.setMinimumSize(410, 290)
@ -1964,22 +1963,20 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
@protected @protected
def do_decrypt(self, message_e, pubkey_e, encrypted_e, password): def do_decrypt(self, message_e, pubkey_e, encrypted_e, password):
cyphertext = str(encrypted_e.toPlainText()) cyphertext = encrypted_e.toPlainText()
task = partial(self.wallet.decrypt_message, str(pubkey_e.text()), task = partial(self.wallet.decrypt_message, pubkey_e.text(), cyphertext, password)
cyphertext, password) self.wallet.thread.add(task, on_success=lambda text: message_e.setText(text.decode('utf8')))
self.wallet.thread.add(task, on_success=message_e.setText)
def do_encrypt(self, message_e, pubkey_e, encrypted_e): def do_encrypt(self, message_e, pubkey_e, encrypted_e):
message = message_e.toPlainText() message = message_e.toPlainText()
message = message.encode('utf-8') message = message.encode('utf8')
try: try:
encrypted = bitcoin.encrypt_message(message, pubkey_e.text()) encrypted = bitcoin.encrypt_message(message, pubkey_e.text())
encrypted_e.setText(encrypted) encrypted_e.setText(encrypted.decode('ascii'))
except BaseException as e: except BaseException as e:
traceback.print_exc(file=sys.stdout) traceback.print_exc(file=sys.stdout)
self.show_warning(str(e)) self.show_warning(str(e))
def encrypt_message(self, address=''): def encrypt_message(self, address=''):
d = WindowModalDialog(self, _('Encrypt/decrypt Message')) d = WindowModalDialog(self, _('Encrypt/decrypt Message'))
d.setMinimumSize(610, 490) d.setMinimumSize(610, 490)
@ -2025,7 +2022,6 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
d = PasswordDialog(parent, msg) d = PasswordDialog(parent, msg)
return d.run() return d.run()
def tx_from_text(self, txt): def tx_from_text(self, txt):
from electrum.transaction import tx_from_str, Transaction from electrum.transaction import tx_from_str, Transaction
try: try:
@ -2059,7 +2055,6 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
return return
self.show_transaction(tx) self.show_transaction(tx)
def read_tx_from_file(self): def read_tx_from_file(self):
fileName = self.getOpenFileName(_("Select your transaction file"), "*.txn") fileName = self.getOpenFileName(_("Select your transaction file"), "*.txn")
if not fileName: if not fileName:

4
gui/qt/transaction_dialog.py

@ -42,6 +42,7 @@ from electrum.bitcoin import base_encode
from electrum.i18n import _ from electrum.i18n import _
from electrum.plugins import run_hook from electrum.plugins import run_hook
from lib.util import bfh
from .util import * from .util import *
dialogs = [] # Otherwise python randomly garbage collects the dialogs... dialogs = [] # Otherwise python randomly garbage collects the dialogs...
@ -150,14 +151,13 @@ class TxDialog(QDialog, MessageBoxMixin):
dialogs.remove(self) dialogs.remove(self)
def show_qr(self): def show_qr(self):
text = str(self.tx).decode('hex') text = bfh(str(self.tx))
text = base_encode(text, base=43) text = base_encode(text, base=43)
try: try:
self.main_window.show_qrcode(text, 'Transaction', parent=self) self.main_window.show_qrcode(text, 'Transaction', parent=self)
except Exception as e: except Exception as e:
self.show_message(str(e)) self.show_message(str(e))
def sign(self): def sign(self):
def sign_done(success): def sign_done(success):
if success: if success:

0
lib/account.py

8
lib/base_wizard.py

@ -1,4 +1,4 @@
#!/usr/bin/env python # -*- coding: utf-8 -*-
# #
# Electrum - lightweight Bitcoin client # Electrum - lightweight Bitcoin client
# Copyright (C) 2016 Thomas Voegtlin # Copyright (C) 2016 Thomas Voegtlin
@ -22,12 +22,7 @@
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE. # SOFTWARE.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import six
import os import os
from . import bitcoin from . import bitcoin
from . import keystore from . import keystore
@ -36,6 +31,7 @@ from .wallet import Wallet, Imported_Wallet, Standard_Wallet, Multisig_Wallet, w
from .i18n import _ from .i18n import _
from .plugins import run_hook from .plugins import run_hook
class BaseWizard(object): class BaseWizard(object):
def __init__(self, config, storage): def __init__(self, config, storage):

19
lib/bitcoin.py

@ -1,5 +1,4 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
#!/usr/bin/env python
# #
# Electrum - lightweight Bitcoin client # Electrum - lightweight Bitcoin client
# Copyright (C) 2011 thomasv@gitorious # Copyright (C) 2011 thomasv@gitorious
@ -100,8 +99,6 @@ except:
def aes_encrypt_with_iv(key, iv, data): def aes_encrypt_with_iv(key, iv, data):
assert_bytes(key, iv, data) assert_bytes(key, iv, data)
if six.PY2:
key, iv, data = map(str, (key, iv, data))
if AES: if AES:
padlen = 16 - (len(data) % 16) padlen = 16 - (len(data) % 16)
if padlen == 0: if padlen == 0:
@ -117,8 +114,6 @@ def aes_encrypt_with_iv(key, iv, data):
def aes_decrypt_with_iv(key, iv, data): def aes_decrypt_with_iv(key, iv, data):
assert_bytes(key, iv, data) assert_bytes(key, iv, data)
if six.PY2:
key, iv, data = map(str, (key, iv, data))
if AES: if AES:
cipher = AES.new(key, AES.MODE_CBC, iv) cipher = AES.new(key, AES.MODE_CBC, iv)
data = cipher.decrypt(data) data = cipher.decrypt(data)
@ -135,7 +130,7 @@ def aes_decrypt_with_iv(key, iv, data):
def EncodeAES(secret, s): def EncodeAES(secret, s):
assert_bytes(s) assert_bytes(s)
iv = _bytes(os.urandom(16)) iv = bytes(os.urandom(16))
# aes_cbc = pyaes.AESModeOfOperationCBC(secret, iv=iv) # aes_cbc = pyaes.AESModeOfOperationCBC(secret, iv=iv)
# aes = pyaes.Encrypter(aes_cbc) # aes = pyaes.Encrypter(aes_cbc)
# e = iv + aes.feed(s) + aes.feed() # e = iv + aes.feed(s) + aes.feed()
@ -144,7 +139,7 @@ def EncodeAES(secret, s):
return base64.b64encode(e) return base64.b64encode(e)
def DecodeAES(secret, e): def DecodeAES(secret, e):
e = _bytes(base64.b64decode(e)) e = bytes(base64.b64decode(e))
iv, e = e[:16], e[16:] iv, e = e[:16], e[16:]
# aes_cbc = pyaes.AESModeOfOperationCBC(secret, iv=iv) # aes_cbc = pyaes.AESModeOfOperationCBC(secret, iv=iv)
# aes = pyaes.Decrypter(aes_cbc) # aes = pyaes.Decrypter(aes_cbc)
@ -289,6 +284,7 @@ def hash_160(public_key):
md.update(sha256(public_key)) md.update(sha256(public_key))
return md.digest() return md.digest()
def hash_160_to_bc_address(h160, addrtype, witness_program_version=1): def hash_160_to_bc_address(h160, addrtype, witness_program_version=1):
s = bytes([addrtype]) s = bytes([addrtype])
if addrtype == ADDRTYPE_P2WPKH: if addrtype == ADDRTYPE_P2WPKH:
@ -302,6 +298,7 @@ def bc_address_to_hash_160(addr):
_bytes = base_decode(addr, 25, base=58) _bytes = base_decode(addr, 25, base=58)
return _bytes[0], _bytes[1:21] return _bytes[0], _bytes[1:21]
def hash160_to_p2pkh(h160): def hash160_to_p2pkh(h160):
return hash_160_to_bc_address(h160, ADDRTYPE_P2PKH) return hash_160_to_bc_address(h160, ADDRTYPE_P2PKH)
@ -309,11 +306,13 @@ def hash160_to_p2pkh(h160):
def hash160_to_p2sh(h160): def hash160_to_p2sh(h160):
return hash_160_to_bc_address(h160, ADDRTYPE_P2SH) return hash_160_to_bc_address(h160, ADDRTYPE_P2SH)
def public_key_to_p2pkh(public_key): def public_key_to_p2pkh(public_key):
return hash160_to_p2pkh(hash_160(public_key)) return hash160_to_p2pkh(hash_160(public_key))
def public_key_to_p2wpkh(public_key): def public_key_to_p2wpkh(public_key):
return hash160_to_bc_address(hash_160(public_key), ADDRTYPE_P2WPKH) return hash_160_to_bc_address(hash_160(public_key), ADDRTYPE_P2WPKH)
@ -512,14 +511,12 @@ from ecdsa.util import string_to_number, number_to_string
def msg_magic(message): def msg_magic(message):
varint = var_int(len(message)) varint = var_int(len(message))
if six.PY3:
encoded_varint = varint.encode('ascii') encoded_varint = varint.encode('ascii')
else:
encoded_varint = b"".join([chr(int(varint[i:i+2], 16)) for i in range(0, len(varint), 2)])
return b"\x18Bitcoin Signed Message:\n" + encoded_varint + message return b"\x18Bitcoin Signed Message:\n" + encoded_varint + message
def verify_message(address, sig, message): def verify_message(address, sig, message):
assert_bytes(sig, message)
try: try:
h = Hash(msg_magic(message)) h = Hash(msg_magic(message))
public_key, compressed = pubkey_from_signature(sig, h) public_key, compressed = pubkey_from_signature(sig, h)

21
lib/blockchain.py

@ -1,5 +1,3 @@
#!/usr/bin/env python
#
# Electrum - lightweight Bitcoin client # Electrum - lightweight Bitcoin client
# Copyright (C) 2012 thomasv@ecdsa.org # Copyright (C) 2012 thomasv@ecdsa.org
# #
@ -22,14 +20,6 @@
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE. # SOFTWARE.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import six
import os import os
import threading import threading
@ -100,8 +90,9 @@ def can_connect(header):
class Blockchain(util.PrintError): class Blockchain(util.PrintError):
"""
'''Manages blockchain headers and their verification''' Manages blockchain headers and their verification
"""
def __init__(self, config, checkpoint, parent_id): def __init__(self, config, checkpoint, parent_id):
self.config = config self.config = config
@ -165,7 +156,7 @@ class Blockchain(util.PrintError):
raise BaseException("insufficient proof of work: %s vs target %s" % (int('0x' + _hash, 16), target)) raise BaseException("insufficient proof of work: %s vs target %s" % (int('0x' + _hash, 16), target))
def verify_chunk(self, index, data): def verify_chunk(self, index, data):
num = len(data) / 80 num = len(data) // 80
prev_header = None prev_header = None
if index != 0: if index != 0:
prev_header = self.read_header(index * 2016 - 1) prev_header = self.read_header(index * 2016 - 1)
@ -299,7 +290,7 @@ class Blockchain(util.PrintError):
c = ("%064x" % new_target)[2:] c = ("%064x" % new_target)[2:]
while c[:2] == '00' and len(c) > 6: while c[:2] == '00' and len(c) > 6:
c = c[2:] c = c[2:]
bitsN, bitsBase = len(c) / 2, int('0x' + c[:6], 16) bitsN, bitsBase = len(c) // 2, int('0x' + c[:6], 16)
if bitsBase >= 0x800000: if bitsBase >= 0x800000:
bitsN += 1 bitsN += 1
bitsBase >>= 8 bitsBase >>= 8
@ -327,7 +318,7 @@ class Blockchain(util.PrintError):
def connect_chunk(self, idx, hexdata): def connect_chunk(self, idx, hexdata):
try: try:
data = hexdata.decode('hex') data = bfh(hexdata)
self.verify_chunk(idx, data) self.verify_chunk(idx, data)
#self.print_error("validated chunk %d" % idx) #self.print_error("validated chunk %d" % idx)
self.save_chunk(idx, data) self.save_chunk(idx, data)

2
lib/util.py

@ -332,7 +332,7 @@ def bfh(x):
elif isinstance(x, (list, tuple, map)): elif isinstance(x, (list, tuple, map)):
return [bfh(sub) for sub in x] return [bfh(sub) for sub in x]
else: else:
raise TypeError('Unexpected type: ' + type(x)) raise TypeError('Unexpected type: ' + str(type(x)))
def bh2u(x): def bh2u(x):

10
lib/verifier.py

@ -1,5 +1,3 @@
#!/usr/bin/env python
#
# Electrum - Lightweight Bitcoin Client # Electrum - Lightweight Bitcoin Client
# Copyright (c) 2012 Thomas Voegtlin # Copyright (c) 2012 Thomas Voegtlin
# #
@ -22,12 +20,6 @@
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE. # SOFTWARE.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import six
from .util import ThreadJob from .util import ThreadJob
from .bitcoin import * from .bitcoin import *
@ -48,7 +40,7 @@ class SPV(ThreadJob):
unverified = self.wallet.get_unverified_txs() unverified = self.wallet.get_unverified_txs()
for tx_hash, tx_height in unverified.items(): for tx_hash, tx_height in unverified.items():
# do not request merkle branch before headers are available # do not request merkle branch before headers are available
if tx_height>0 and tx_hash not in self.merkle_roots and tx_height <= lh: if (tx_height > 0) and (tx_hash not in self.merkle_roots) and (tx_height <= lh):
request = ('blockchain.transaction.get_merkle', request = ('blockchain.transaction.get_merkle',
[tx_hash, tx_height]) [tx_hash, tx_height])
self.network.send([request], self.verify_merkle) self.network.send([request], self.verify_merkle)

5
lib/wallet.py

@ -1,5 +1,3 @@
#!/usr/bin/env python
#
# Electrum - lightweight Bitcoin client # Electrum - lightweight Bitcoin client
# Copyright (C) 2015 Thomas Voegtlin # Copyright (C) 2015 Thomas Voegtlin
# #
@ -73,7 +71,6 @@ TX_STATUS = [
] ]
class Abstract_Wallet(PrintError): class Abstract_Wallet(PrintError):
""" """
Wallet classes are created to handle various address generation methods. Wallet classes are created to handle various address generation methods.
@ -297,7 +294,6 @@ class Abstract_Wallet(PrintError):
self.verifier.merkle_roots.pop(tx_hash, None) self.verifier.merkle_roots.pop(tx_hash, None)
# tx will be verified only if height > 0 # tx will be verified only if height > 0
print('unverif', tx_hash, tx_height)
if tx_hash not in self.verified_tx: if tx_hash not in self.verified_tx:
self.unverified_tx[tx_hash] = tx_height self.unverified_tx[tx_hash] = tx_height
@ -477,7 +473,6 @@ class Abstract_Wallet(PrintError):
return tx_hash, status, label, can_broadcast, can_bump, amount, fee, height, conf, timestamp, exp_n return tx_hash, status, label, can_broadcast, can_bump, amount, fee, height, conf, timestamp, exp_n
def get_addr_io(self, address): def get_addr_io(self, address):
h = self.history.get(address, []) h = self.history.get(address, [])
received = {} received = {}

Loading…
Cancel
Save