move is_valid from wallet class to bitcoin.py

This commit is contained in:
thomasv 2013-03-01 14:27:56 +01:00
parent f3cc234c87
commit f72c8ee5d2
9 changed files with 49 additions and 32 deletions

View File

@ -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:

View File

@ -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

View File

@ -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
@ -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:

View File

@ -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
@ -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
@ -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()

View File

@ -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
@ -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
@ -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

View File

@ -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
@ -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)
@ -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
@ -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

View File

@ -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
@ -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
@ -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()

View File

@ -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
@ -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:

View File

@ -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
@ -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 )
@ -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
@ -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