fix for notifications
[electrum-nvc.git] / lib / wallet.py
index c1769b0..0047452 100644 (file)
 # You should have received a copy of the GNU General Public License
 # along with this program. If not, see <http://www.gnu.org/licenses/>.
 
+import sys
+import base64
+import os
+import re
+import hashlib
+import copy
+import operator
+import ast
+import threading
+import random
+import aes
+import ecdsa
+import Queue
+import time
 
-import sys, base64, os, re, hashlib, copy, operator, ast, threading, random
-import aes, ecdsa
 from ecdsa.util import string_to_number, number_to_string
-
-############ functions from pywallet ##################### 
-
-addrtype = 0
-
-def hash_160(public_key):
-    try:
-        md = hashlib.new('ripemd160')
-        md.update(hashlib.sha256(public_key).digest())
-        return md.digest()
-    except:
-        import ripemd
-        md = ripemd.new(hashlib.sha256(public_key).digest())
-        return md.digest()
-
-
-def public_key_to_bc_address(public_key):
-    h160 = hash_160(public_key)
-    return hash_160_to_bc_address(h160)
-
-def hash_160_to_bc_address(h160):
-    vh160 = chr(addrtype) + h160
-    h = Hash(vh160)
-    addr = vh160 + h[0:4]
-    return b58encode(addr)
-
-def bc_address_to_hash_160(addr):
-    bytes = b58decode(addr, 25)
-    return bytes[1:21]
-
-def encode_point(pubkey, compressed=False):
-    order = generator_secp256k1.order()
-    p = pubkey.pubkey.point
-    x_str = ecdsa.util.number_to_string(p.x(), order)
-    y_str = ecdsa.util.number_to_string(p.y(), order)
-    if compressed:
-        return chr(2 + (p.y() & 1)) + x_str
-    else:
-        return chr(4) + pubkey.to_string() #x_str + y_str
-
-__b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
-__b58base = len(__b58chars)
-
-def b58encode(v):
-    """ encode v, which is a string of bytes, to base58.               
-    """
-
-    long_value = 0L
-    for (i, c) in enumerate(v[::-1]):
-        long_value += (256**i) * ord(c)
-
-    result = ''
-    while long_value >= __b58base:
-        div, mod = divmod(long_value, __b58base)
-        result = __b58chars[mod] + result
-        long_value = div
-    result = __b58chars[long_value] + result
-
-    # Bitcoin does a little leading-zero-compression:
-    # leading 0-bytes in the input become leading-1s
-    nPad = 0
-    for c in v:
-        if c == '\0': nPad += 1
-        else: break
-
-    return (__b58chars[0]*nPad) + result
-
-def b58decode(v, length):
-    """ decode v into a string of len bytes
-    """
-    long_value = 0L
-    for (i, c) in enumerate(v[::-1]):
-        long_value += __b58chars.find(c) * (__b58base**i)
-
-    result = ''
-    while long_value >= 256:
-        div, mod = divmod(long_value, 256)
-        result = chr(mod) + result
-        long_value = div
-    result = chr(long_value) + result
-
-    nPad = 0
-    for c in v:
-        if c == __b58chars[0]: nPad += 1
-        else: break
-
-    result = chr(0)*nPad + result
-    if length is not None and len(result) != length:
-        return None
-
-    return result
-
-
-def Hash(data):
-    return hashlib.sha256(hashlib.sha256(data).digest()).digest()
-
-def EncodeBase58Check(vchIn):
-    hash = Hash(vchIn)
-    return b58encode(vchIn + hash[0:4])
-
-def DecodeBase58Check(psz):
-    vchRet = b58decode(psz, None)
-    key = vchRet[0:-4]
-    csum = vchRet[-4:]
-    hash = Hash(key)
-    cs32 = hash[0:4]
-    if cs32 != csum:
-        return None
-    else:
-        return key
-
-def PrivKeyToSecret(privkey):
-    return privkey[9:9+32]
-
-def SecretToASecret(secret):
-    vchIn = chr(addrtype+128) + secret
-    return EncodeBase58Check(vchIn)
-
-def ASecretToSecret(key):
-    vch = DecodeBase58Check(key)
-    if vch and vch[0] == chr(addrtype+128):
-        return vch[1:]
-    else:
-        return False
-
-########### end pywallet functions #######################
+from util import print_error, user_dir, format_satoshis
+from bitcoin import *
 
 # URL decode
 _ud = re.compile('%([0-9a-hA-H]{2})', re.MULTILINE)
 urldecode = lambda x: _ud.sub(lambda m: chr(int(m.group(1), 16)), x)
 
-
-def int_to_hex(i, length=1):
-    s = hex(i)[2:].rstrip('L')
-    s = "0"*(2*length - len(s)) + s
-    return s.decode('hex')[::-1].encode('hex')
-
-
-# AES
+# AES encryption
 EncodeAES = lambda secret, s: base64.b64encode(aes.encryptData(secret,s))
 DecodeAES = lambda secret, e: aes.decryptData(secret, base64.b64decode(e))
 
 
-
-# secp256k1, http://www.oid-info.com/get/1.3.132.0.10
-_p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2FL
-_r = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141L
-_b = 0x0000000000000000000000000000000000000000000000000000000000000007L
-_a = 0x0000000000000000000000000000000000000000000000000000000000000000L
-_Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798L
-_Gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8L
-curve_secp256k1 = ecdsa.ellipticcurve.CurveFp( _p, _a, _b )
-generator_secp256k1 = ecdsa.ellipticcurve.Point( curve_secp256k1, _Gx, _Gy, _r )
-oid_secp256k1 = (1,3,132,0,10)
-SECP256k1 = ecdsa.curves.Curve("SECP256k1", curve_secp256k1, generator_secp256k1, oid_secp256k1 ) 
-
-
-def filter(s): 
-    out = re.sub('( [^\n]*|)\n','',s)
-    out = out.replace(' ','')
-    out = out.replace('\n','')
-    return out
-
-def raw_tx( inputs, outputs, for_sig = None ):
-    s  = int_to_hex(1,4)                                     +   '     version\n' 
-    s += int_to_hex( len(inputs) )                           +   '     number of inputs\n'
-    for i in range(len(inputs)):
-        _, _, p_hash, p_index, p_script, pubkey, sig = inputs[i]
-        s += p_hash.decode('hex')[::-1].encode('hex')        +  '     prev hash\n'
-        s += int_to_hex(p_index,4)                           +  '     prev index\n'
-        if for_sig is None:
-            sig = sig + chr(1)                               # hashtype
-            script  = int_to_hex( len(sig))                  +  '     push %d bytes\n'%len(sig)
-            script += sig.encode('hex')                      +  '     sig\n'
-            pubkey = chr(4) + pubkey
-            script += int_to_hex( len(pubkey))               +  '     push %d bytes\n'%len(pubkey)
-            script += pubkey.encode('hex')                   +  '     pubkey\n'
-        elif for_sig==i:
-            script = p_script                                +  '     scriptsig \n'
-        else:
-            script=''
-        s += int_to_hex( len(filter(script))/2 )             +  '     script length \n'
-        s += script
-        s += "ffffffff"                                      +  '     sequence\n'
-    s += int_to_hex( len(outputs) )                          +  '     number of outputs\n'
-    for output in outputs:
-        addr, amount = output
-        s += int_to_hex( amount, 8)                          +  '     amount: %d\n'%amount 
-        script = '76a9'                                      # op_dup, op_hash_160
-        script += '14'                                       # push 0x14 bytes
-        script += bc_address_to_hash_160(addr).encode('hex')
-        script += '88ac'                                     # op_equalverify, op_checksig
-        s += int_to_hex( len(filter(script))/2 )             +  '     script length \n'
-        s += script                                          +  '     script \n'
-    s += int_to_hex(0,4)                                     # lock time
-    if for_sig is not None: s += int_to_hex(1, 4)            # hash type
-    return s
-
-
-
-
-def format_satoshis(x, is_diff=False, num_zeros = 0):
-    from decimal import Decimal
-    s = Decimal(x)
-    sign, digits, exp = s.as_tuple()
-    digits = map(str, digits)
-    while len(digits) < 9:
-        digits.insert(0,'0')
-    digits.insert(-8,'.')
-    s = ''.join(digits).rstrip('0')
-    if sign: 
-        s = '-' + s
-    elif is_diff:
-        s = "+" + s
-
-    p = s.find('.')
-    s += "0"*( 1 + num_zeros - ( len(s) - p ))
-    s += " "*( 9 - ( len(s) - p ))
-    s = " "*( 5 - ( p )) + s
-    return s
-
-
 from version import ELECTRUM_VERSION, SEED_VERSION
-from interface import DEFAULT_SERVERS
-
-
 
 
 class Wallet:
-    def __init__(self, gui_callback = lambda: None):
+    def __init__(self, config={}):
 
+        self.config = config
         self.electrum_version = ELECTRUM_VERSION
-        self.seed_version = SEED_VERSION
-        self.gui_callback = gui_callback
-
-        self.gap_limit = 5           # configuration
-        self.use_change = True
-        self.fee = 100000
-        self.num_zeros = 0
-        self.master_public_key = ''
 
         # saved fields
-        self.use_encryption = False
-        self.addresses = []          # receiving addresses visible for user
-        self.change_addresses = []   # addresses used as change
-        self.seed = ''               # encrypted
-        self.history = {}
-        self.labels = {}             # labels for addresses and transactions
-        self.aliases = {}            # aliases for addresses
-        self.authorities = {}        # trusted addresses
-        self.frozen_addresses = []
-        self.prioritized_addresses = []
-        self.expert_mode = False
-        
-        self.receipts = {}           # signed URIs
-        self.receipt = None          # next receipt
-        self.addressbook = []        # outgoing addresses, for payments
+        self.seed_version          = config.get('seed_version', SEED_VERSION)
+        self.gap_limit             = config.get('gap_limit', 5)
+        self.use_change            = config.get('use_change',True)
+        self.fee                   = int(config.get('fee',100000))
+        self.num_zeros             = int(config.get('num_zeros',0))
+        self.master_public_key     = config.get('master_public_key','')
+        self.use_encryption        = config.get('use_encryption', False)
+        self.addresses             = config.get('addresses', [])          # receiving addresses visible for user
+        self.change_addresses      = config.get('change_addresses', [])   # addresses used as change
+        self.seed                  = config.get('seed', '')               # encrypted
+        self.labels                = config.get('labels',{})              # labels for addresses and transactions
+        self.aliases               = config.get('aliases', {})            # aliases for addresses
+        self.authorities           = config.get('authorities', {})        # trusted addresses
+        self.frozen_addresses      = config.get('frozen_addresses',[])
+        self.prioritized_addresses = config.get('prioritized_addresses',[])
+        self.receipts              = config.get('receipts',{})            # signed URIs
+        self.addressbook           = config.get('contacts', [])           # outgoing addresses, for payments
+        self.imported_keys         = config.get('imported_keys',{})
+        self.history               = config.get('addr_history',{})        # address -> list(txid, height)
+        self.transactions          = config.get('transactions',{})        # txid -> deserialised
 
         # not saved
-        self.tx_history = {}
-
-        self.imported_keys = {}
-        self.remote_url = None
-
-        self.was_updated = True
-        self.blocks = -1
+        self.prevout_values = {}
+        self.spent_outputs = []
+        self.receipt = None          # next receipt
         self.banner = ''
 
-        # there is a difference between self.up_to_date and self.is_up_to_date()
-        # self.is_up_to_date() returns true when all requests have been answered and processed
-        # self.up_to_date is true when the wallet is synchronized (stronger requirement)
+        # spv
+        self.verifier = None
+
+        # there is a difference between wallet.up_to_date and interface.is_up_to_date()
+        # interface.is_up_to_date() returns true when all requests have been answered and processed
+        # wallet.up_to_date is true when the wallet is synchronized (stronger requirement)
         self.up_to_date_event = threading.Event()
         self.up_to_date_event.clear()
         self.up_to_date = False
         self.lock = threading.Lock()
         self.tx_event = threading.Event()
 
-        self.pick_random_server()
-
-
-
-    def pick_random_server(self):
-        self.server = random.choice( DEFAULT_SERVERS )         # random choice when the wallet is created
-
-    def is_up_to_date(self):
-        return self.interface.responses.empty() and not self.interface.unanswered_requests
+        if self.seed_version != SEED_VERSION:
+            raise ValueError("This wallet seed is deprecated. Please run upgrade.py for a diagnostic.")
 
-    def set_server(self, server):
-        # raise an error if the format isnt correct
-        a,b,c = server.split(':')
-        b = int(b)
-        assert c in ['t','h','n']
-        # set the server
-        if server != self.server:
-            self.server = server
-            self.save()
-            self.interface.is_connected = False  # this exits the polling loop
-            self.interface.poke()
+        for tx_hash in self.transactions.keys():
+            self.update_tx_outputs(tx_hash)
 
-    def set_path(self, wallet_path):
 
-        if wallet_path is not None:
-            self.path = wallet_path
-        else:
-            # backward compatibility: look for wallet file in the default data directory
-            if "HOME" in os.environ:
-                wallet_dir = os.path.join( os.environ["HOME"], '.electrum')
-            elif "LOCALAPPDATA" in os.environ:
-                wallet_dir = os.path.join( os.environ["LOCALAPPDATA"], 'Electrum' )
-            elif "APPDATA" in os.environ:
-                wallet_dir = os.path.join( os.environ["APPDATA"], 'Electrum' )
-            else:
-                raise BaseException("No home directory found in environment variables.")
+    def init_up_to_date(self):
+        self.up_to_date_event.clear()
+        self.up_to_date = False
 
-            if not os.path.exists( wallet_dir ): os.mkdir( wallet_dir )
-            self.path = os.path.join( wallet_dir, 'electrum.dat' )
 
     def import_key(self, keypair, password):
         address, key = keypair.split(':')
@@ -354,6 +122,7 @@ class Wallet:
             raise BaseException('Address does not match private key')
         self.imported_keys[address] = self.pw_encode( key, password )
 
+
     def new_seed(self, password):
         seed = "%032x"%ecdsa.util.randrange( pow(2,128) )
         #self.init_mpk(seed)
@@ -366,7 +135,7 @@ class Wallet:
         curve = SECP256k1
         secexp = self.stretch_key(seed)
         master_private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
-        self.master_public_key = master_private_key.get_verifying_key().to_string()
+        self.master_public_key = master_private_key.get_verifying_key().to_string().encode('hex')
 
     def all_addresses(self):
         return self.addresses + self.change_addresses + self.imported_keys.keys()
@@ -393,7 +162,7 @@ class Wallet:
         return string_to_number( seed )
 
     def get_sequence(self,n,for_change):
-        return string_to_number( Hash( "%d:%d:"%(n,for_change) + self.master_public_key ) )
+        return string_to_number( Hash( "%d:%d:"%(n,for_change) + self.master_public_key.decode('hex') ) )
 
     def get_private_key_base58(self, address, password):
         pk = self.get_private_key(address, password)
@@ -494,29 +263,32 @@ class Wallet:
     
 
     def create_new_address(self, for_change):
-        """   Publickey(type,n) = Master_public_key + H(n|S|type)*point  """
-        curve = SECP256k1
         n = len(self.change_addresses) if for_change else len(self.addresses)
-        z = self.get_sequence(n,for_change)
-        master_public_key = ecdsa.VerifyingKey.from_string( self.master_public_key, curve = SECP256k1 )
-        pubkey_point = master_public_key.pubkey.point + z*curve.generator
-        public_key2 = ecdsa.VerifyingKey.from_public_point( pubkey_point, curve = SECP256k1 )
-        address = public_key_to_bc_address( '04'.decode('hex') + public_key2.to_string() )
+        address = self.get_new_address(n, for_change)
         if for_change:
             self.change_addresses.append(address)
         else:
             self.addresses.append(address)
-
         self.history[address] = []
+        return address
+        
+    def get_new_address(self, n, for_change):
+        """   Publickey(type,n) = Master_public_key + H(n|S|type)*point  """
+        curve = SECP256k1
+        z = self.get_sequence(n, for_change)
+        master_public_key = ecdsa.VerifyingKey.from_string( self.master_public_key.decode('hex'), curve = SECP256k1 )
+        pubkey_point = master_public_key.pubkey.point + z*curve.generator
+        public_key2 = ecdsa.VerifyingKey.from_public_point( pubkey_point, curve = SECP256k1 )
+        address = public_key_to_bc_address( '04'.decode('hex') + public_key2.to_string() )
         print address
         return address
-
+                                                                      
 
     def change_gap_limit(self, value):
         if value >= self.gap_limit:
             self.gap_limit = value
             self.save()
-            self.interface.poke()
+            self.interface.poke('synchronizer')
             return True
 
         elif value >= self.min_acceptable_gap():
@@ -570,121 +342,26 @@ class Wallet:
             if len(self.addresses) < n:
                 new_addresses.append( self.create_new_address(False) )
                 continue
-            if map( lambda a: self.history.get(a), self.addresses[-n:] ) == n*[[]]:
+            if map( lambda a: self.history.get(a, []), self.addresses[-n:] ) == n*[[]]:
                 break
             else:
                 new_addresses.append( self.create_new_address(False) )
 
-        if self.remote_url:
-            num = self.get_remote_number()
-            while len(self.addresses)<num:
-                new_addresses.append( self.create_new_address(False) )
-
         return new_addresses
 
 
-    def get_remote_number(self):
-        import jsonrpclib
-        server = jsonrpclib.Server(self.remote_url)
-        out = server.getnum()
-        return out
-
-    def get_remote_mpk(self):
-        import jsonrpclib
-        server = jsonrpclib.Server(self.remote_url)
-        out = server.getkey()
-        return out
-
     def is_found(self):
         return (len(self.change_addresses) > 1 ) or ( len(self.addresses) > self.gap_limit )
 
     def fill_addressbook(self):
-        for tx in self.tx_history.values():
-            if tx['value']<0:
-                for i in tx['outputs']:
-                    if not self.is_mine(i) and i not in self.addressbook:
-                        self.addressbook.append(i)
+        for tx_hash, tx in self.transactions.items():
+            if self.get_tx_value(tx_hash)<0:
+                for o in tx['outputs']:
+                    addr = o.get('address')
+                    if not self.is_mine(addr) and addr not in self.addressbook:
+                        self.addressbook.append(addr)
         # redo labels
-        self.update_tx_labels()
-
-
-    def save(self):
-        s = {
-            'seed_version':self.seed_version,
-            'use_encryption':self.use_encryption,
-            'use_change':self.use_change,
-            'master_public_key': self.master_public_key.encode('hex'),
-            'fee':self.fee,
-            'server':self.server,
-            'seed':self.seed,
-            'addresses':self.addresses,
-            'change_addresses':self.change_addresses,
-            'history':self.history, 
-            'labels':self.labels,
-            'contacts':self.addressbook,
-            'imported_keys':self.imported_keys,
-            'aliases':self.aliases,
-            'authorities':self.authorities,
-            'receipts':self.receipts,
-            'num_zeros':self.num_zeros,
-            'frozen_addresses':self.frozen_addresses,
-            'prioritized_addresses':self.prioritized_addresses,
-            'expert_mode':self.expert_mode,
-            'gap_limit':self.gap_limit,
-            }
-        f = open(self.path,"w")
-        f.write( repr(s) )
-        f.close()
-        import stat
-        os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)
-
-    def read(self):
-        import interface
-
-        upgrade_msg = """This wallet seed is deprecated. Please run upgrade.py for a diagnostic."""
-        self.file_exists = False
-        try:
-            f = open(self.path,"r")
-            data = f.read()
-            f.close()
-        except:
-            return
-        try:
-            d = ast.literal_eval( data )
-            interface.old_to_new(d)
-            self.seed_version = d.get('seed_version')
-            self.master_public_key = d.get('master_public_key').decode('hex')
-            self.use_encryption = d.get('use_encryption')
-            self.use_change = bool(d.get('use_change',True))
-            self.fee = int( d.get('fee') )
-            self.seed = d.get('seed')
-            self.server = d.get('server')
-            #blocks = d.get('blocks')
-            self.addresses = d.get('addresses')
-            self.change_addresses = d.get('change_addresses')
-            self.history = d.get('history')
-            self.labels = d.get('labels')
-            self.addressbook = d.get('contacts')
-            self.imported_keys = d.get('imported_keys',{})
-            self.aliases = d.get('aliases',{})
-            self.authorities = d.get('authorities',{})
-            self.receipts = d.get('receipts',{})
-            self.num_zeros = d.get('num_zeros',0)
-            self.frozen_addresses = d.get('frozen_addresses',[])
-            self.prioritized_addresses = d.get('prioritized_addresses',[])
-            self.expert_mode = d.get('expert_mode',False)
-            self.gap_limit = d.get('gap_limit',5)
-        except:
-            raise BaseException("cannot read wallet file")
-
-        self.update_tx_history()
-
-        if self.seed_version != SEED_VERSION:
-            raise BaseException(upgrade_msg)
-
-        if self.remote_url: assert self.master_public_key.encode('hex') == self.get_remote_mpk()
-
-        self.file_exists = True
+        # self.update_tx_labels()
 
 
     def get_address_flags(self, addr):
@@ -693,13 +370,51 @@ class Wallet:
         return flags
         
 
+    def get_tx_value(self, tx_hash, addresses = None):
+        # return the balance for that tx
+        if addresses is None: addresses = self.all_addresses()
+        with self.lock:
+            v = 0
+            d = self.transactions.get(tx_hash)
+            if not d: return 0
+            for item in d.get('inputs'):
+                addr = item.get('address')
+                if addr in addresses:
+                    key = item['prevout_hash']  + ':%d'%item['prevout_n']
+                    value = self.prevout_values.get( key )
+                    if value is None: continue
+                    v -= value
+            for item in d.get('outputs'):
+                addr = item.get('address')
+                if addr in addresses: 
+                    value = item.get('value')
+                    v += value 
+            return v
+
+
+    
+    def update_tx_outputs(self, tx_hash):
+        tx = self.transactions.get(tx_hash)
+        for item in tx.get('outputs'):
+            value = item.get('value')
+            key = tx_hash+ ':%d'%item.get('index')
+            with self.lock:
+                self.prevout_values[key] = value 
+
+        for item in tx.get('inputs'):
+            if self.is_mine(item.get('address')):
+                key = item['prevout_hash'] + ':%d'%item['prevout_n']
+                self.spent_outputs.append(key)
+
+
     def get_addr_balance(self, addr):
         assert self.is_mine(addr)
         h = self.history.get(addr,[])
+        if h == ['*']: return 0,0
         c = u = 0
-        for item in h:
-            v = item['value']
-            if item['height']:
+        for tx_hash, tx_height in h:
+            v = self.get_tx_value(tx_hash, [addr])
+            if tx_height:
                 c += v
             else:
                 u += v
@@ -729,28 +444,37 @@ class Wallet:
             if i in domain: domain.remove(i)
 
         for addr in domain:
-            h = self.history.get(addr)
-            if h is None: continue
-            for item in h:
-                if item.get('raw_output_script'):
-                    coins.append( (addr,item))
-
-        coins = sorted( coins, key = lambda x: x[1]['timestamp'] )
+            h = self.history.get(addr, [])
+            if h == ['*']: continue
+            for tx_hash, tx_height in h:
+                tx = self.transactions.get(tx_hash)
+                for output in tx.get('outputs'):
+                    if output.get('address') != addr: continue
+                    key = tx_hash + ":%d" % output.get('index')
+                    if key in self.spent_outputs: continue
+                    output['tx_hash'] = tx_hash
+                    coins.append(output)
+
+        #coins = sorted( coins, key = lambda x: x[1]['timestamp'] )
 
         for addr in self.prioritized_addresses:
-            h = self.history.get(addr)
-            if h is None: continue
-            for item in h:
-                if item.get('raw_output_script'):
-                    prioritized_coins.append( (addr,item))
-
-        prioritized_coins = sorted( prioritized_coins, key = lambda x: x[1]['timestamp'] )
+            h = self.history.get(addr, [])
+            if h == ['*']: continue
+            for tx_hash, tx_height in h:
+                for output in tx.get('outputs'):
+                    if output.get('address') != addr: continue
+                    key = tx_hash + ":%d" % output.get('index')
+                    if key in self.spent_outputs: continue
+                    output['tx_hash'] = tx_hash
+                    prioritized_coins.append(output)
+
+        #prioritized_coins = sorted( prioritized_coins, key = lambda x: x[1]['timestamp'] )
 
         inputs = []
         coins = prioritized_coins + coins
 
-        for c in coins: 
-            addr, item = c
+        for item in coins: 
+            addr = item.get('address')
             v = item.get('value')
             total += v
             inputs.append((addr, v, item['tx_hash'], item['index'], item['raw_output_script'], None, None) )
@@ -799,74 +523,109 @@ class Wallet:
                 try:
                     d.decode('hex')
                 except:
-                    raise BaseException("Invalid password")
+                    raise ValueError("Invalid password")
             return d
         else:
             return s
 
-    def get_status(self, address):
-        h = self.history.get(address)
-        if not h:
-            status = None
-        else:
-            lastpoint = h[-1]
-            status = lastpoint['block_hash']
-            if status == 'mempool': 
-                status = status + ':%d'% len(h)
-        return status
 
-    def receive_status_callback(self, addr, status):
+    def get_history(self, address):
         with self.lock:
-            if self.get_status(addr) != status:
-                #print "updating status for", addr, status
-                self.interface.get_history(addr)
+            return self.history.get(address)
+
+    def get_status(self, h):
+        if not h: return None
+        if h == ['*']: return '*'
+        status = ''
+        for tx_hash, height in h:
+            status += tx_hash + ':%d:' % height
+        return hashlib.sha256( status ).digest().encode('hex')
+
+
+
+    def receive_tx_callback(self, tx_hash, tx):
 
-    def receive_history_callback(self, addr, data): 
-        #print "updating history for", addr
+        if not self.check_new_tx(tx_hash, tx):
+            raise BaseException("error: received transaction is not consistent with history"%tx_hash)
+
+        with self.lock:
+            self.transactions[tx_hash] = tx
+
+        tx_height = tx.get('height')
+        if tx_height>0: self.verifier.add(tx_hash, tx_height)
+
+        self.update_tx_outputs(tx_hash)
+
+        self.save()
+
+
+    def receive_history_callback(self, addr, hist):
+
+        if hist != ['*']:
+            if not self.check_new_history(addr, hist):
+                raise BaseException("error: received history for %s is not consistent with known transactions"%addr)
+            
         with self.lock:
-            self.history[addr] = data
-            self.update_tx_history()
+            self.history[addr] = hist
             self.save()
 
+        if hist != ['*']:
+            for tx_hash, tx_height in hist:
+                if tx_height>0:
+                    # add it in case it was previously unconfirmed
+                    self.verifier.add(tx_hash, tx_height)
+                    # set the height in case it changed
+                    tx = self.transactions.get(tx_hash)
+                    if tx:
+                        if tx.get('height') != tx_height:
+                            print_error( "changing height for tx", tx_hash )
+                            tx['height'] = tx_height
+
+
     def get_tx_history(self):
-        lines = self.tx_history.values()
+        with self.lock:
+            lines = self.transactions.values()
+
         lines = sorted(lines, key=operator.itemgetter("timestamp"))
         return lines
 
-    def update_tx_history(self):
-        self.tx_history= {}
-        for addr in self.all_addresses():
-            h = self.history.get(addr)
-            if h is None: continue
-            for tx in h:
-                tx_hash = tx['tx_hash']
-                line = self.tx_history.get(tx_hash)
-                if not line:
-                    self.tx_history[tx_hash] = copy.copy(tx)
-                    line = self.tx_history.get(tx_hash)
-                else:
-                    line['value'] += tx['value']
-                if line['height'] == 0:
-                    line['timestamp'] = 1e12
-        self.update_tx_labels()
+    def get_transactions_at_height(self, height):
+        with self.lock:
+            values = self.transactions.values()[:]
 
-    def update_tx_labels(self):
-        for tx in self.tx_history.values():
+        out = []
+        for tx in values:
+            if tx['height'] == height:
+                out.append(tx['tx_hash'])
+        return out
+
+
+    def get_label(self, tx_hash):
+        label = self.labels.get(tx_hash)
+        is_default = (label == '') or (label is None)
+        if is_default: label = self.get_default_label(tx_hash)
+        return label, is_default
+
+    def get_default_label(self, tx_hash):
+        tx = self.transactions.get(tx_hash)
+        if tx:
             default_label = ''
-            if tx['value']<0:
-                for o_addr in tx['outputs']:
+            if self.get_tx_value(tx_hash)<0:
+                for o in tx['outputs']:
+                    o_addr = o.get('address')
                     if not self.is_mine(o_addr):
-                        dest_label = self.labels.get(o_addr)
-                        if dest_label:
-                            default_label = 'to: ' + dest_label
-                        else:
-                            default_label = 'to: ' + o_addr
+                        try:
+                            default_label = self.labels[o_addr]
+                        except KeyError:
+                            default_label = o_addr
             else:
-                for o_addr in tx['outputs']:
+                for o in tx['outputs']:
+                    o_addr = o.get('address')
                     if self.is_mine(o_addr) and not self.is_change(o_addr):
                         break
                 else:
-                    for o_addr in tx['outputs']:
+                    for o in tx['outputs']:
+                        o_addr = o.get('address')
                         if self.is_mine(o_addr):
                             break
                     else:
@@ -874,25 +633,24 @@ class Wallet:
 
                 if o_addr:
                     dest_label = self.labels.get(o_addr)
-                    if dest_label:
-                        default_label = 'at: ' + dest_label
-                    else:
-                        default_label = 'at: ' + o_addr
+                    try:
+                        default_label = self.labels[o_addr]
+                    except KeyError:
+                        default_label = o_addr
+
+        return default_label
 
-            tx['default_label'] = default_label
 
-    def mktx(self, to_address, amount, label, password, fee=None, from_addr= None):
+    def mktx(self, to_address, amount, label, password, fee=None, change_addr=None, from_addr= None):
         if not self.is_valid(to_address):
-            raise BaseException("Invalid address")
+            raise ValueError("Invalid address")
         inputs, total, fee = self.choose_tx_inputs( amount, fee, from_addr )
         if not inputs:
-            raise BaseException("Not enough funds")
+            raise ValueError("Not enough funds")
 
-        if self.use_change:
+        if not self.use_change and not change_addr:
             change_addr = inputs[0][0]
-            print "sending change to", change_addr
-        else:
-            change_addr = None
+            print "Sending change to", change_addr
 
         outputs = self.choose_tx_outputs( to_address, amount, fee, total, change_addr )
         s_inputs = self.sign_inputs( inputs, outputs, password )
@@ -907,10 +665,19 @@ class Wallet:
         return tx
 
     def sendtx(self, tx):
-        tx_hash = Hash(tx.decode('hex') )[::-1].encode('hex')
-        self.tx_event.clear()
-        self.interface.send([('blockchain.transaction.broadcast', [tx])])
+        # synchronous
+        h = self.send_tx(tx)
         self.tx_event.wait()
+        self.receive_tx(h)
+
+    def send_tx(self, tx):
+        # asynchronous
+        self.tx_event.clear()
+        tx_hash = Hash(tx.decode('hex') )[::-1].encode('hex')
+        self.interface.send([('blockchain.transaction.broadcast', [tx])], 'synchronizer')
+        return tx_hash
+
+    def receive_tx(self,tx_hash):
         out = self.tx_result 
         if out != tx_hash:
             return False, "error: " + out
@@ -927,9 +694,9 @@ class Wallet:
         m1 = re.match('([\w\-\.]+)@((\w[\w\-]+\.)+[\w\-]+)', alias)
         m2 = re.match('((\w[\w\-]+\.)+[\w\-]+)', alias)
         if m1:
-            url = 'http://' + m1.group(2) + '/bitcoin.id/' + m1.group(1) 
+            url = 'https://' + m1.group(2) + '/bitcoin.id/' + m1.group(1) 
         elif m2:
-            url = 'http://' + alias + '/bitcoin.id'
+            url = 'https://' + alias + '/bitcoin.id'
         else:
             return ''
         try:
@@ -959,7 +726,7 @@ class Wallet:
             self.verify_message(previous, signature, "alias:%s:%s"%(alias,target))
 
         if not self.is_valid(target):
-            raise BaseException("Invalid bitcoin address")
+            raise ValueError("Invalid bitcoin address")
 
         return target, signing_addr, auth_name
 
@@ -1066,21 +833,15 @@ class Wallet:
 
 
     def update(self):
-        self.interface.poke()
+        self.interface.poke('synchronizer')
         self.up_to_date_event.wait(10000000000)
 
 
-    def start_session(self, interface):
-        self.interface = interface
-        self.interface.send([('server.banner',[]), ('blockchain.numblocks.subscribe',[]), ('server.peers.subscribe',[])])
-        self.interface.subscribe(self.all_addresses())
-
-
     def freeze(self,addr):
         if addr in self.all_addresses() and addr not in self.frozen_addresses:
             self.unprioritize(addr)
             self.frozen_addresses.append(addr)
-            self.save()
+            self.config.set_key('frozen_addresses', self.frozen_addresses, True)
             return True
         else:
             return False
@@ -1088,7 +849,7 @@ class Wallet:
     def unfreeze(self,addr):
         if addr in self.all_addresses() and addr in self.frozen_addresses:
             self.frozen_addresses.remove(addr)
-            self.save()
+            self.config.set_key('frozen_addresses', self.frozen_addresses, True)
             return True
         else:
             return False
@@ -1097,7 +858,7 @@ class Wallet:
         if addr in self.all_addresses() and addr not in self.prioritized_addresses:
             self.unfreeze(addr)
             self.prioritized_addresses.append(addr)
-            self.save()
+            self.config.set_key('prioritized_addresses', self.prioritized_addresses, True)
             return True
         else:
             return False
@@ -1105,7 +866,295 @@ class Wallet:
     def unprioritize(self,addr):
         if addr in self.all_addresses() and addr in self.prioritized_addresses:
             self.prioritized_addresses.remove(addr)
-            self.save()
+            self.config.set_key('prioritized_addresses', self.prioritized_addresses, True)
             return True
         else:
             return False
+
+    def save(self):
+        s = {
+            'seed_version': self.seed_version,
+            'use_encryption': self.use_encryption,
+            'use_change': self.use_change,
+            'master_public_key': self.master_public_key,
+            'fee': self.fee,
+            'seed': self.seed,
+            'addresses': self.addresses,
+            'change_addresses': self.change_addresses,
+            'addr_history': self.history, 
+            'labels': self.labels,
+            'contacts': self.addressbook,
+            'imported_keys': self.imported_keys,
+            'aliases': self.aliases,
+            'authorities': self.authorities,
+            'receipts': self.receipts,
+            'num_zeros': self.num_zeros,
+            'frozen_addresses': self.frozen_addresses,
+            'prioritized_addresses': self.prioritized_addresses,
+            'gap_limit': self.gap_limit,
+            'transactions': self.transactions,
+        }
+        for k, v in s.items():
+            self.config.set_key(k,v)
+        self.config.save()
+
+    def set_verifier(self, verifier):
+        self.verifier = verifier
+
+        # review transactions (they might not all be in history)
+        for tx_hash, tx in self.transactions.items():
+            tx_height = tx.get('height')
+            if tx_height <1:
+                print_error( "skipping", tx_hash, tx_height )
+                continue
+            
+            if tx_height>0:
+                self.verifier.add(tx_hash, tx_height)
+
+            # set the timestamp for transactions that need it
+            if tx and not tx.get('timestamp'):
+                timestamp = self.verifier.get_timestamp(tx_height)
+                if timestamp:
+                    self.set_tx_timestamp(tx_hash, timestamp)
+
+
+        # review existing history
+        for addr, hist in self.history.items():
+            if hist == ['*']: continue
+            for tx_hash, tx_height in hist:
+                if tx_height>0:
+                    # add it in case it was previously unconfirmed
+                    self.verifier.add(tx_hash, tx_height)
+                    # set the height in case it changed
+                    tx = self.transactions.get(tx_hash)
+                    if tx:
+                        if tx.get('height') != tx_height:
+                            print_error( "changing height for tx", tx_hash )
+                            tx['height'] = tx_height
+
+
+
+    def set_tx_timestamp(self, tx_hash, timestamp):
+        with self.lock:
+            self.transactions[tx_hash]['timestamp'] = timestamp
+
+
+
+    def is_addr_in_tx(self, addr, tx):
+        found = False
+        for txin in tx.get('inputs'):
+            if addr == txin.get('address'): 
+                found = True
+                break
+        for txout in tx.get('outputs'):
+            if addr == txout.get('address'): 
+                found = True
+                break
+        return found
+
+
+    def check_new_history(self, addr, hist):
+        # - check that all tx in hist are relevant
+        for tx_hash, height in hist:
+            tx = self.transactions.get(tx_hash)
+            if not tx: continue
+            if not self.is_addr_in_tx(addr,tx):
+                return False
+
+        # todo: check that we are not "orphaning" a transaction
+        # if we are, reject tx if unconfirmed, else reject the server
+
+        return True
+
+
+
+    def check_new_tx(self, tx_hash, tx):
+        # 1 check that tx is referenced in addr_history. 
+        addresses = []
+        for addr, hist in self.history.items():
+            if hist == ['*']:continue
+            for txh, height in hist:
+                if txh == tx_hash: 
+                    addresses.append(addr)
+
+        if not addresses:
+            return False
+
+        # 2 check that referencing addresses are in the tx
+        for addr in addresses:
+            if not self.is_addr_in_tx(addr, tx):
+                return False
+
+        return True
+
+
+
+
+class WalletSynchronizer(threading.Thread):
+
+
+    def __init__(self, wallet, config):
+        threading.Thread.__init__(self)
+        self.daemon = True
+        self.wallet = wallet
+        self.interface = self.wallet.interface
+        self.interface.register_channel('synchronizer')
+        self.wallet.interface.register_callback('connected', self.wallet.init_up_to_date)
+        self.wallet.interface.register_callback('connected', lambda: self.interface.send([('server.banner',[])],'synchronizer') )
+        self.was_updated = True
+
+    def synchronize_wallet(self):
+        new_addresses = self.wallet.synchronize()
+        if new_addresses:
+            self.subscribe_to_addresses(new_addresses)
+            self.wallet.up_to_date = False
+            return
+            
+        if not self.interface.is_up_to_date('synchronizer'):
+            if self.wallet.up_to_date:
+                self.wallet.up_to_date = False
+                self.was_updated = True
+            return
+
+        self.wallet.up_to_date = True
+        self.was_updated = True
+        self.wallet.up_to_date_event.set()
+
+    
+    def subscribe_to_addresses(self, addresses):
+        messages = []
+        for addr in addresses:
+            messages.append(('blockchain.address.subscribe', [addr]))
+        self.interface.send( messages, 'synchronizer')
+
+
+    def run(self):
+        requested_tx = []
+        missing_tx = []
+        requested_histories = {}
+
+        # request any missing transactions
+        for history in self.wallet.history.values():
+            if history == ['*']: continue
+            for tx_hash, tx_height in history:
+                if self.wallet.transactions.get(tx_hash) is None and (tx_hash, tx_height) not in missing_tx:
+                    missing_tx.append( (tx_hash, tx_height) )
+        print_error("missing tx", missing_tx)
+
+        # wait until we are connected, in case the user is not connected
+        while not self.interface.is_connected:
+            time.sleep(1)
+        
+        # request banner, because 'connected' event happens before this thread is started
+        self.interface.send([('server.banner',[])],'synchronizer')
+
+        # subscriptions
+        self.subscribe_to_addresses(self.wallet.all_addresses())
+
+        while True:
+            # 1. send new requests
+            self.synchronize_wallet()
+
+            for tx_hash, tx_height in missing_tx:
+                if (tx_hash, tx_height) not in requested_tx:
+                    self.interface.send([ ('blockchain.transaction.get',[tx_hash, tx_height]) ], 'synchronizer')
+                    requested_tx.append( (tx_hash, tx_height) )
+            missing_tx = []
+
+            if self.was_updated:
+                self.interface.trigger_callback('updated')
+                self.was_updated = False
+
+            # 2. get a response
+            r = self.interface.get_response('synchronizer')
+            if not r: 
+                continue
+
+            # 3. handle response
+            method = r['method']
+            params = r['params']
+            result = r.get('result')
+            error = r.get('error')
+            if error:
+                print "error", r
+                continue
+
+            if method == 'blockchain.address.subscribe':
+                addr = params[0]
+                if self.wallet.get_status(self.wallet.get_history(addr)) != result:
+                    self.interface.send([('blockchain.address.get_history', [addr])], 'synchronizer')
+                    requested_histories[addr] = result
+
+            elif method == 'blockchain.address.get_history':
+                addr = params[0]
+                print_error("receiving history", addr, result)
+                if result == ['*']:
+                    assert requested_histories.pop(addr) == '*'
+                    self.wallet.receive_history_callback(addr, result)
+                else:
+                    hist = []
+                    # check that txids are unique
+                    txids = []
+                    for item in result:
+                        tx_hash = item['tx_hash']
+                        if tx_hash not in txids:
+                            txids.append(tx_hash)
+                            hist.append( (tx_hash, item['height']) )
+
+                    if len(hist) != len(result):
+                        raise BaseException("error: server sent history with non-unique txid", result)
+
+                    # check that the status corresponds to what was announced
+                    rs = requested_histories.pop(addr)
+                    if self.wallet.get_status(hist) != rs:
+                        raise BaseException("error: status mismatch: %s"%addr)
+                
+                    # store received history
+                    self.wallet.receive_history_callback(addr, hist)
+
+                    # request transactions that we don't have 
+                    for tx_hash, tx_height in hist:
+                        if self.wallet.transactions.get(tx_hash) is None:
+                            if (tx_hash, tx_height) not in requested_tx and (tx_hash, tx_height) not in missing_tx:
+                                missing_tx.append( (tx_hash, tx_height) )
+                        else:
+                            timestamp = self.wallet.verifier.get_timestamp(tx_height)
+                            self.wallet.set_tx_timestamp(tx_hash, timestamp)
+
+            elif method == 'blockchain.transaction.get':
+                tx_hash = params[0]
+                tx_height = params[1]
+                d = self.deserialize_tx(tx_hash, tx_height, result)
+                self.wallet.receive_tx_callback(tx_hash, d)
+                self.was_updated = True
+                requested_tx.remove( (tx_hash, tx_height) )
+                print_error("received tx:", d)
+
+            elif method == 'blockchain.transaction.broadcast':
+                self.wallet.tx_result = result
+                self.wallet.tx_event.set()
+
+            elif method == 'server.banner':
+                self.wallet.banner = result
+                self.was_updated = True
+
+            else:
+                print_error("Error: Unknown message:" + method + ", " + repr(params) + ", " + repr(result) )
+
+            if self.was_updated and not requested_tx:
+                self.interface.trigger_callback('updated')
+                self.was_updated = False
+
+
+    def deserialize_tx(self, tx_hash, tx_height, raw_tx):
+
+        assert tx_hash == hash_encode(Hash(raw_tx.decode('hex')))
+        import deserialize
+        vds = deserialize.BCDataStream()
+        vds.write(raw_tx.decode('hex'))
+        d = deserialize.parse_Transaction(vds)
+        d['height'] = tx_height
+        d['tx_hash'] = tx_hash
+        d['timestamp'] = self.wallet.verifier.get_timestamp(tx_height)
+        return d
+