URI: 
       tuse contacts in command line - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit ee5f499fc1d146368c0e7c44195a4d68d81e865f
   DIR parent 5cd3bfedb6c55477e386e3f59608cd91cc7d1d5b
  HTML Author: ThomasV <thomasv@gitorious>
       Date:   Sun, 31 May 2015 14:10:52 +0200
       
       use contacts in command line
       
       Diffstat:
         M gui/qt/main_window.py               |       2 +-
         M lib/commands.py                     |      60 ++++++++++----------------------
         M lib/util.py                         |      16 ++++++++++++++++
       
       3 files changed, 35 insertions(+), 43 deletions(-)
       ---
   DIR diff --git a/gui/qt/main_window.py b/gui/qt/main_window.py
       t@@ -118,7 +118,7 @@ class ElectrumWindow(QMainWindow):
                self.app = gui_object.app
        
                self.invoices = InvoiceStore(self.config)
       -        self.contacts = StoreDict(self.config, 'contacts')
       +        self.contacts = util.Contacts(self.config)
        
                self.create_status_bar()
                self.need_update = threading.Event()
   DIR diff --git a/lib/commands.py b/lib/commands.py
       t@@ -23,9 +23,9 @@ import copy
        import argparse
        import json
        
       +import util
        from util import print_msg, format_satoshis, print_stderr
       -from util import StoreDict
       -from bitcoin import is_valid, hash_160_to_bc_address, hash_160
       +from bitcoin import is_address, hash_160_to_bc_address, hash_160
        from decimal import Decimal
        import bitcoin
        from transaction import Transaction
       t@@ -153,10 +153,11 @@ arg_types = {
            'num':int,
            'nbits':int,
            'entropy':long,
       -    'pubkeys':json.loads,
       +    'pubkeys': json.loads,
            'inputs': json.loads,
       -    'outputs':json.loads,
       -    'tx_fee':lambda x: (Decimal(x) if x is not None else None)
       +    'outputs': json.loads,
       +    'tx_fee': lambda x: Decimal(x) if x is not None else None,
       +    'amount': lambda x: Decimal(x) if x!='!' else '!',
        }
        
        
       t@@ -252,6 +253,7 @@ class Commands:
                self.network = network
                self._callback = callback
                self.password = None
       +        self.contacts = util.Contacts(self.config)
        
            def _run(self, method, args, password_getter):
                cmd = known_commands[method]
       t@@ -351,7 +353,7 @@ class Commands:
                return [self.wallet.get_private_key(address, self.password) for address in addresses]
        
            def validateaddress(self, addr):
       -        isvalid = is_valid(addr)
       +        isvalid = is_address(addr)
                out = { 'isvalid':isvalid }
                if isvalid:
                    out['address'] = addr
       t@@ -425,37 +427,13 @@ class Commands:
                return bitcoin.verify_message(address, signature, message)
        
            def _mktx(self, outputs, fee=None, change_addr=None, domain=None):
       -        for to_address, amount in outputs:
       -            if not is_valid(to_address):
       -                raise Exception("Invalid Bitcoin address", to_address)
       -
       -        if change_addr:
       -            if not is_valid(change_addr):
       -                raise Exception("Invalid Bitcoin address", change_addr)
       -
       -        if domain is not None:
       -            for addr in domain:
       -                if not is_valid(addr):
       -                    raise Exception("invalid Bitcoin address", addr)
       -
       -                if not self.wallet.is_mine(addr):
       -                    raise Exception("address not in wallet", addr)
       -
       -        for k, v in self.wallet.labels.items():
       -            if change_addr and v == change_addr:
       -                change_addr = k
       -
       -        if fee is not None:
       -            fee = int(100000000*fee)
       -
       +        change_addr = None if change_addr is None else self.contacts.resolve(change_addr)
       +        domain = None if domain is None else map(self.contacts.resolve, domain)
       +        fee = None if fee is None else int(100000000*Decimal(fee))
                final_outputs = []
       -        for to_address, amount in outputs:
       -            for k, v in self.wallet.labels.items():
       -                if v == to_address:
       -                    to_address = k
       -                    print_msg("alias", to_address)
       -                    break
       -
       +        for address, amount in outputs:
       +            address = self.contacts.resolve(address)
       +            #assert self.wallet.is_mine(address)
                    if amount == '!':
                        assert len(outputs) == 1
                        inputs = self.wallet.get_spendable_coins(domain)
       t@@ -468,8 +446,8 @@ class Commands:
                            fee = self.wallet.estimated_fee(dummy_tx)
                        amount -= fee
                    else:
       -                amount = int(100000000*amount)
       -            final_outputs.append(('address', to_address, amount))
       +                amount = int(100000000*Decimal(amount))
       +            final_outputs.append(('address', address, amount))
        
                return self.wallet.mktx(final_outputs, self.password, fee, change_addr, domain)
        
       t@@ -528,13 +506,11 @@ class Commands:
                self.wallet.set_label(key, label)
        
            def listcontacts(self):
       -        contacts = StoreDict(self.config, 'contacts')
       -        return contacts
       +        return self.contacts
        
            def searchcontacts(self, query):
       -        contacts = StoreDict(self.config, 'contacts')
                results = {}
       -        for key, value in contacts.items():
       +        for key, value in self.contacts.items():
                    if query.lower() in key.lower():
                        results[key] = value
                return results
   DIR diff --git a/lib/util.py b/lib/util.py
       t@@ -461,3 +461,19 @@ class StoreDict(dict):
                if key in self.keys():
                    dict.pop(self, key)
                    self.save()
       +
       +
       +import bitcoin
       +
       +class Contacts(StoreDict):
       +    def __init__(self, config):
       +        StoreDict.__init__(self, config, 'contacts')
       +
       +    def resolve(self, k):
       +        if bitcoin.is_address(k):
       +            return k
       +        if k in self.keys():
       +            _type, addr = self[k]
       +            return addr
       +        raise Exception("invalid Bitcoin address", k)
       +