Browse Source

move is_valid from wallet class to bitcoin.py

master
thomasv 13 years ago
parent
commit
f72c8ee5d2
  1. 7
      electrum
  2. 10
      lib/bitcoin.py
  3. 21
      lib/commands.py
  4. 5
      lib/gui.py
  5. 5
      lib/gui_android.py
  6. 9
      lib/gui_lite.py
  7. 6
      lib/gui_qt.py
  8. 3
      lib/gui_text.py
  9. 15
      lib/wallet.py

7
electrum

@ -444,7 +444,12 @@ if __name__ == '__main__':
cmd_runner = Commands(wallet, interface) cmd_runner = Commands(wallet, interface)
func = eval('cmd_runner.' + cmd) func = eval('cmd_runner.' + cmd)
cmd_runner.password = password 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: if type(result) == str:
util.print_msg(result) util.print_msg(result)
else: else:

10
lib/bitcoin.py

@ -259,6 +259,16 @@ def address_from_private_key(sec):
return address 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 ####################### ########### end pywallet functions #######################
# secp256k1, http://www.oid-info.com/get/1.3.132.0.10 # secp256k1, http://www.oid-info.com/get/1.3.132.0.10

21
lib/commands.py

@ -152,9 +152,9 @@ class Commands:
return self.wallet.get_private_keys(addresses, self.password) return self.wallet.get_private_keys(addresses, self.password)
def validateaddress(self,addr): def validateaddress(self,addr):
is_valid = self.wallet.is_valid(addr) isvalid = is_valid(addr)
out = { 'isvalid':is_valid } out = { 'isvalid':isvalid }
if is_valid: if isvalid:
is_mine = self.wallet.is_mine(addr) is_mine = self.wallet.is_mine(addr)
out['address'] = addr out['address'] = addr
out['ismine'] = is_mine out['ismine'] = is_mine
@ -209,8 +209,19 @@ class Commands:
def _mktx(self, to_address, amount, fee = None, change_addr = None, from_addr = None): 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(): if not is_valid(to_address):
raise BaseException("address not in wallet") 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(): for k, v in self.wallet.labels.items():
if v == to_address: if v == to_address:

5
lib/gui.py

@ -24,6 +24,7 @@ pygtk.require('2.0')
import gtk, gobject import gtk, gobject
from decimal import Decimal from decimal import Decimal
from util import print_error from util import print_error
from bitcoin import is_valid
import pyqrnative, mnemonic import pyqrnative, mnemonic
@ -815,7 +816,7 @@ class ElectrumWindow:
else: else:
to_address = r 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) self.show_message( "invalid bitcoin address:\n"+to_address)
return return
@ -1220,7 +1221,7 @@ class ElectrumWindow:
dialog.destroy() dialog.destroy()
if result == 1: if result == 1:
if self.wallet.is_valid(address): if is_valid(address):
self.wallet.addressbook.append(address) self.wallet.addressbook.append(address)
if label: self.wallet.labels[address] = label if label: self.wallet.labels[address] = label
self.wallet.save() self.wallet.save()

5
lib/gui_android.py

@ -24,6 +24,7 @@ import android
from electrum import SimpleConfig, Interface, WalletSynchronizer, Wallet, format_satoshis, mnemonic_encode, mnemonic_decode from electrum import SimpleConfig, Interface, WalletSynchronizer, Wallet, format_satoshis, mnemonic_encode, mnemonic_decode
from decimal import Decimal from decimal import Decimal
import datetime, re import datetime, re
from bitcoin import is_valid
@ -483,7 +484,7 @@ def make_new_contact():
if data: if data:
if re.match('^bitcoin:', data): if re.match('^bitcoin:', data):
address, _, _, _, _, _, _ = wallet.parse_url(data, None, lambda x: modal_question('Question',x)) address, _, _, _, _, _, _ = wallet.parse_url(data, None, lambda x: modal_question('Question',x))
elif wallet.is_valid(data): elif is_valid(data):
address = data address = data
else: else:
address = None address = None
@ -589,7 +590,7 @@ def payto_loop():
label = droid.fullQueryDetail("label").result.get('text') label = droid.fullQueryDetail("label").result.get('text')
amount = droid.fullQueryDetail('amount').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') modal_dialog('Error','Invalid Bitcoin address')
continue continue

9
lib/gui_lite.py

@ -13,6 +13,7 @@ except ImportError:
from decimal import Decimal as D from decimal import Decimal as D
from util import get_resource_path as rsrc from util import get_resource_path as rsrc
from bitcoin import is_valid
from i18n import _ from i18n import _
import decimal import decimal
import exchange_rate import exchange_rate
@ -554,7 +555,7 @@ class MiniWindow(QDialog):
address = match2.group(2) address = match2.group(2)
self.address_input.setText(address) self.address_input.setText(address)
if self.actuator.is_valid(address): if is_valid(address):
self.check_button_status() self.check_button_status()
self.address_input.setProperty("isValid", True) self.address_input.setProperty("isValid", True)
self.recompute_style(self.address_input) self.recompute_style(self.address_input)
@ -827,7 +828,7 @@ class MiniActuator:
"""Send bitcoins to the target address.""" """Send bitcoins to the target address."""
dest_address = self.fetch_destination(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'), QMessageBox.warning(parent_window, _('Error'),
_('Invalid Bitcoin Address') + ':\n' + address, _('OK')) _('Invalid Bitcoin Address') + ':\n' + address, _('OK'))
return False return False
@ -897,10 +898,6 @@ class MiniActuator:
else: else:
return recipient 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): def copy_master_public_key(self):
master_pubkey = self.wallet.master_public_key master_pubkey = self.wallet.master_public_key

6
lib/gui_qt.py

@ -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'") 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 wallet import format_satoshis
from bitcoin import Transaction from bitcoin import Transaction, is_valid
import bmp, mnemonic, pyqrnative, qrscanner import bmp, mnemonic, pyqrnative, qrscanner
import exchange_rate import exchange_rate
@ -914,7 +914,7 @@ class ElectrumWindow(QMainWindow):
else: else:
to_address = r 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')) QMessageBox.warning(self, _('Error'), _('Invalid Bitcoin Address') + ':\n' + to_address, _('OK'))
return return
@ -1350,7 +1350,7 @@ class ElectrumWindow(QMainWindow):
text, ok = QInputDialog.getText(self, _('New Contact'), _('Address') + ':') text, ok = QInputDialog.getText(self, _('New Contact'), _('Address') + ':')
address = unicode(text) address = unicode(text)
if ok: if ok:
if self.wallet.is_valid(address): if is_valid(address):
self.wallet.addressbook.append(address) self.wallet.addressbook.append(address)
self.wallet.save() self.wallet.save()
self.update_contacts_tab() self.update_contacts_tab()

3
lib/gui_text.py

@ -3,6 +3,7 @@ from decimal import Decimal
_ = lambda x:x _ = lambda x:x
#from i18n import _ #from i18n import _
from util import format_satoshis, set_verbosity from util import format_satoshis, set_verbosity
from bitcoin import is_valid
import tty, sys import tty, sys
@ -238,7 +239,7 @@ class ElectrumGui:
self.str_description = '' self.str_description = ''
def do_send(self): 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')) self.show_message(_('Invalid Bitcoin address'))
return return
try: try:

15
lib/wallet.py

@ -178,15 +178,6 @@ class Wallet:
def is_change(self, address): def is_change(self, address):
return address in self.change_addresses 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): def get_master_public_key(self):
return self.sequence.master_public_key return self.sequence.master_public_key
@ -714,7 +705,7 @@ class Wallet:
def mktx(self, outputs, password, fee=None, change_addr=None, from_addr= None): def mktx(self, outputs, password, fee=None, change_addr=None, from_addr= None):
for address, x in outputs: for address, x in outputs:
assert self.is_valid(address) assert is_valid(address)
amount = sum( map(lambda x:x[1], outputs) ) amount = sum( map(lambda x:x[1], outputs) )
inputs, total, fee = self.choose_tx_inputs( amount, fee, from_addr ) inputs, total, fee = self.choose_tx_inputs( amount, fee, from_addr )
@ -811,7 +802,7 @@ class Wallet:
target, signature = line target, signature = line
EC_KEY.verify_message(previous, signature, "alias:%s:%s"%(alias,target)) 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") raise ValueError("Invalid bitcoin address")
return target, signing_addr, auth_name return target, signing_addr, auth_name
@ -898,7 +889,7 @@ class Wallet:
if signature: if signature:
if re.match('^(|([\w\-\.]+)@)((\w[\w\-]+\.)+[\w\-]+)$', identity): if re.match('^(|([\w\-\.]+)@)((\w[\w\-]+\.)+[\w\-]+)$', identity):
signing_address = self.get_alias(identity, True, show_message, question) signing_address = self.get_alias(identity, True, show_message, question)
elif self.is_valid(identity): elif is_valid(identity):
signing_address = identity signing_address = identity
else: else:
signing_address = None signing_address = None

Loading…
Cancel
Save