tmove is_valid from wallet class to bitcoin.py - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit f72c8ee5d22fdde117fc1d479931343543a11d95 DIR parent f3cc234c87a951496e926b340f732ac2c13946ce HTML Author: thomasv <thomasv@gitorious> Date: Fri, 1 Mar 2013 14:27:56 +0100 move is_valid from wallet class to bitcoin.py Diffstat: M electrum | 7 ++++++- M lib/bitcoin.py | 10 ++++++++++ M lib/commands.py | 21 ++++++++++++++++----- M lib/gui.py | 5 +++-- M lib/gui_android.py | 5 +++-- M lib/gui_lite.py | 9 +++------ M lib/gui_qt.py | 6 +++--- M lib/gui_text.py | 3 ++- M lib/wallet.py | 15 +++------------ 9 files changed, 49 insertions(+), 32 deletions(-) --- DIR diff --git a/electrum b/electrum t@@ -444,7 +444,12 @@ if __name__ == '__main__': cmd_runner = Commands(wallet, interface) func = eval('cmd_runner.' + cmd) cmd_runner.password = password - result = func(*args[1:]) + try: + result = func(*args[1:]) + except BaseException, e: + print_msg("Error: " + str(e)) + sys.exit(1) + if type(result) == str: util.print_msg(result) else: DIR diff --git a/lib/bitcoin.py b/lib/bitcoin.py t@@ -259,6 +259,16 @@ def address_from_private_key(sec): return address +def is_valid(addr): + ADDRESS_RE = re.compile('[1-9A-HJ-NP-Za-km-z]{26,}\\Z') + if not ADDRESS_RE.match(addr): return False + try: + addrtype, h = bc_address_to_hash_160(addr) + except: + return False + return addr == hash_160_to_bc_address(h, addrtype) + + ########### end pywallet functions ####################### # secp256k1, http://www.oid-info.com/get/1.3.132.0.10 DIR diff --git a/lib/commands.py b/lib/commands.py t@@ -152,9 +152,9 @@ class Commands: return self.wallet.get_private_keys(addresses, self.password) def validateaddress(self,addr): - is_valid = self.wallet.is_valid(addr) - out = { 'isvalid':is_valid } - if is_valid: + isvalid = is_valid(addr) + out = { 'isvalid':isvalid } + if isvalid: is_mine = self.wallet.is_mine(addr) out['address'] = addr out['ismine'] = is_mine t@@ -209,8 +209,19 @@ class Commands: def _mktx(self, to_address, amount, fee = None, change_addr = None, from_addr = None): - if from_addr and from_addr not in wallet.all_addresses(): - raise BaseException("address not in wallet") + if not is_valid(to_address): + raise BaseException("Invalid Bitcoin address", to_address) + + if change_addr: + if not is_valid(change_addr): + raise BaseException("Invalid Bitcoin address", change_addr) + + if from_addr: + if not is_valid(from_addr): + raise BaseException("invalid Bitcoin address", from_addr) + + if from_addr not in self.wallet.all_addresses(): + raise BaseException("address not in wallet") for k, v in self.wallet.labels.items(): if v == to_address: DIR diff --git a/lib/gui.py b/lib/gui.py t@@ -24,6 +24,7 @@ pygtk.require('2.0') import gtk, gobject from decimal import Decimal from util import print_error +from bitcoin import is_valid import pyqrnative, mnemonic t@@ -815,7 +816,7 @@ class ElectrumWindow: else: to_address = r - if not self.wallet.is_valid(to_address): + if not is_valid(to_address): self.show_message( "invalid bitcoin address:\n"+to_address) return t@@ -1220,7 +1221,7 @@ class ElectrumWindow: dialog.destroy() if result == 1: - if self.wallet.is_valid(address): + if is_valid(address): self.wallet.addressbook.append(address) if label: self.wallet.labels[address] = label self.wallet.save() DIR diff --git a/lib/gui_android.py b/lib/gui_android.py t@@ -24,6 +24,7 @@ import android from electrum import SimpleConfig, Interface, WalletSynchronizer, Wallet, format_satoshis, mnemonic_encode, mnemonic_decode from decimal import Decimal import datetime, re +from bitcoin import is_valid t@@ -483,7 +484,7 @@ def make_new_contact(): if data: if re.match('^bitcoin:', data): address, _, _, _, _, _, _ = wallet.parse_url(data, None, lambda x: modal_question('Question',x)) - elif wallet.is_valid(data): + elif is_valid(data): address = data else: address = None t@@ -589,7 +590,7 @@ def payto_loop(): label = droid.fullQueryDetail("label").result.get('text') amount = droid.fullQueryDetail('amount').result.get('text') - if not wallet.is_valid(recipient): + if not is_valid(recipient): modal_dialog('Error','Invalid Bitcoin address') continue DIR diff --git a/lib/gui_lite.py b/lib/gui_lite.py t@@ -13,6 +13,7 @@ except ImportError: from decimal import Decimal as D from util import get_resource_path as rsrc +from bitcoin import is_valid from i18n import _ import decimal import exchange_rate t@@ -554,7 +555,7 @@ class MiniWindow(QDialog): address = match2.group(2) self.address_input.setText(address) - if self.actuator.is_valid(address): + if is_valid(address): self.check_button_status() self.address_input.setProperty("isValid", True) self.recompute_style(self.address_input) t@@ -827,7 +828,7 @@ class MiniActuator: """Send bitcoins to the target address.""" dest_address = self.fetch_destination(address) - if dest_address is None or not self.wallet.is_valid(dest_address): + if dest_address is None or not is_valid(dest_address): QMessageBox.warning(parent_window, _('Error'), _('Invalid Bitcoin Address') + ':\n' + address, _('OK')) return False t@@ -897,10 +898,6 @@ class MiniActuator: else: return recipient - def is_valid(self, address): - """Check if bitcoin address is valid.""" - - return self.wallet.is_valid(address) def copy_master_public_key(self): master_pubkey = self.wallet.master_public_key DIR diff --git a/lib/gui_qt.py b/lib/gui_qt.py t@@ -38,7 +38,7 @@ except: sys.exit("Error: Could not import icons_rc.py, please generate it with: 'pyrcc4 icons.qrc -o lib/icons_rc.py'") from wallet import format_satoshis -from bitcoin import Transaction +from bitcoin import Transaction, is_valid import bmp, mnemonic, pyqrnative, qrscanner import exchange_rate t@@ -914,7 +914,7 @@ class ElectrumWindow(QMainWindow): else: to_address = r - if not self.wallet.is_valid(to_address): + if not is_valid(to_address): QMessageBox.warning(self, _('Error'), _('Invalid Bitcoin Address') + ':\n' + to_address, _('OK')) return t@@ -1350,7 +1350,7 @@ class ElectrumWindow(QMainWindow): text, ok = QInputDialog.getText(self, _('New Contact'), _('Address') + ':') address = unicode(text) if ok: - if self.wallet.is_valid(address): + if is_valid(address): self.wallet.addressbook.append(address) self.wallet.save() self.update_contacts_tab() DIR diff --git a/lib/gui_text.py b/lib/gui_text.py t@@ -3,6 +3,7 @@ from decimal import Decimal _ = lambda x:x #from i18n import _ from util import format_satoshis, set_verbosity +from bitcoin import is_valid import tty, sys t@@ -238,7 +239,7 @@ class ElectrumGui: self.str_description = '' def do_send(self): - if not self.wallet.is_valid(self.str_recipient): + if not is_valid(self.str_recipient): self.show_message(_('Invalid Bitcoin address')) return try: DIR diff --git a/lib/wallet.py b/lib/wallet.py t@@ -178,15 +178,6 @@ class Wallet: def is_change(self, address): return address in self.change_addresses - def is_valid(self,addr): - ADDRESS_RE = re.compile('[1-9A-HJ-NP-Za-km-z]{26,}\\Z') - if not ADDRESS_RE.match(addr): return False - try: - addrtype, h = bc_address_to_hash_160(addr) - except: - return False - return addr == hash_160_to_bc_address(h, addrtype) - def get_master_public_key(self): return self.sequence.master_public_key t@@ -714,7 +705,7 @@ class Wallet: def mktx(self, outputs, password, fee=None, change_addr=None, from_addr= None): for address, x in outputs: - assert self.is_valid(address) + assert is_valid(address) amount = sum( map(lambda x:x[1], outputs) ) inputs, total, fee = self.choose_tx_inputs( amount, fee, from_addr ) t@@ -811,7 +802,7 @@ class Wallet: target, signature = line EC_KEY.verify_message(previous, signature, "alias:%s:%s"%(alias,target)) - if not self.is_valid(target): + if not is_valid(target): raise ValueError("Invalid bitcoin address") return target, signing_addr, auth_name t@@ -898,7 +889,7 @@ class Wallet: if signature: if re.match('^(|([\w\-\.]+)@)((\w[\w\-]+\.)+[\w\-]+)$', identity): signing_address = self.get_alias(identity, True, show_message, question) - elif self.is_valid(identity): + elif is_valid(identity): signing_address = identity else: signing_address = None