better errors
[electrum-nvc.git] / client / wallet.py
1 #!/usr/bin/env python
2 #
3 # Electrum - lightweight Bitcoin client
4 # Copyright (C) 2011 thomasv@gitorious
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19
20 import sys, base64, os, re, hashlib, copy, operator, ast, threading
21
22 try:
23     import ecdsa  
24     from ecdsa.util import string_to_number, number_to_string
25 except:
26     print "python-ecdsa does not seem to be installed. Try 'sudo easy_install ecdsa'"
27     sys.exit(1)
28
29 try:
30     import aes
31 except:
32     print "AES does not seem to be installed. Try 'sudo easy_install slowaes'"
33     sys.exit(1)
34
35
36 ############ functions from pywallet ##################### 
37
38 addrtype = 0
39
40 def hash_160(public_key):
41     md = hashlib.new('ripemd160')
42     md.update(hashlib.sha256(public_key).digest())
43     return md.digest()
44
45 def public_key_to_bc_address(public_key):
46     h160 = hash_160(public_key)
47     return hash_160_to_bc_address(h160)
48
49 def hash_160_to_bc_address(h160):
50     vh160 = chr(addrtype) + h160
51     h = Hash(vh160)
52     addr = vh160 + h[0:4]
53     return b58encode(addr)
54
55 def bc_address_to_hash_160(addr):
56     bytes = b58decode(addr, 25)
57     return bytes[1:21]
58
59 __b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
60 __b58base = len(__b58chars)
61
62 def b58encode(v):
63     """ encode v, which is a string of bytes, to base58.                
64     """
65
66     long_value = 0L
67     for (i, c) in enumerate(v[::-1]):
68         long_value += (256**i) * ord(c)
69
70     result = ''
71     while long_value >= __b58base:
72         div, mod = divmod(long_value, __b58base)
73         result = __b58chars[mod] + result
74         long_value = div
75     result = __b58chars[long_value] + result
76
77     # Bitcoin does a little leading-zero-compression:
78     # leading 0-bytes in the input become leading-1s
79     nPad = 0
80     for c in v:
81         if c == '\0': nPad += 1
82         else: break
83
84     return (__b58chars[0]*nPad) + result
85
86 def b58decode(v, length):
87     """ decode v into a string of len bytes
88     """
89     long_value = 0L
90     for (i, c) in enumerate(v[::-1]):
91         long_value += __b58chars.find(c) * (__b58base**i)
92
93     result = ''
94     while long_value >= 256:
95         div, mod = divmod(long_value, 256)
96         result = chr(mod) + result
97         long_value = div
98     result = chr(long_value) + result
99
100     nPad = 0
101     for c in v:
102         if c == __b58chars[0]: nPad += 1
103         else: break
104
105     result = chr(0)*nPad + result
106     if length is not None and len(result) != length:
107         return None
108
109     return result
110
111
112 def Hash(data):
113     return hashlib.sha256(hashlib.sha256(data).digest()).digest()
114
115 def EncodeBase58Check(vchIn):
116     hash = Hash(vchIn)
117     return b58encode(vchIn + hash[0:4])
118
119 def DecodeBase58Check(psz):
120     vchRet = b58decode(psz, None)
121     key = vchRet[0:-4]
122     csum = vchRet[-4:]
123     hash = Hash(key)
124     cs32 = hash[0:4]
125     if cs32 != csum:
126         return None
127     else:
128         return key
129
130 def PrivKeyToSecret(privkey):
131     return privkey[9:9+32]
132
133 def SecretToASecret(secret):
134     vchIn = chr(addrtype+128) + secret
135     return EncodeBase58Check(vchIn)
136
137 def ASecretToSecret(key):
138     vch = DecodeBase58Check(key)
139     if vch and vch[0] == chr(addrtype+128):
140         return vch[1:]
141     else:
142         return False
143
144 ########### end pywallet functions #######################
145
146 # URL decode
147 _ud = re.compile('%([0-9a-hA-H]{2})', re.MULTILINE)
148 urldecode = lambda x: _ud.sub(lambda m: chr(int(m.group(1), 16)), x)
149
150
151 def int_to_hex(i, length=1):
152     s = hex(i)[2:].rstrip('L')
153     s = "0"*(2*length - len(s)) + s
154     return s.decode('hex')[::-1].encode('hex')
155
156
157 # AES
158 EncodeAES = lambda secret, s: base64.b64encode(aes.encryptData(secret,s))
159 DecodeAES = lambda secret, e: aes.decryptData(secret, base64.b64decode(e))
160
161
162
163 # secp256k1, http://www.oid-info.com/get/1.3.132.0.10
164 _p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2FL
165 _r = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141L
166 _b = 0x0000000000000000000000000000000000000000000000000000000000000007L
167 _a = 0x0000000000000000000000000000000000000000000000000000000000000000L
168 _Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798L
169 _Gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8L
170 curve_secp256k1 = ecdsa.ellipticcurve.CurveFp( _p, _a, _b )
171 generator_secp256k1 = ecdsa.ellipticcurve.Point( curve_secp256k1, _Gx, _Gy, _r )
172 oid_secp256k1 = (1,3,132,0,10)
173 SECP256k1 = ecdsa.curves.Curve("SECP256k1", curve_secp256k1, generator_secp256k1, oid_secp256k1 ) 
174
175
176 def filter(s): 
177     out = re.sub('( [^\n]*|)\n','',s)
178     out = out.replace(' ','')
179     out = out.replace('\n','')
180     return out
181
182 def raw_tx( inputs, outputs, for_sig = None ):
183     s  = int_to_hex(1,4)                                     +   '     version\n' 
184     s += int_to_hex( len(inputs) )                           +   '     number of inputs\n'
185     for i in range(len(inputs)):
186         _, _, p_hash, p_index, p_script, pubkey, sig = inputs[i]
187         s += p_hash.decode('hex')[::-1].encode('hex')        +  '     prev hash\n'
188         s += int_to_hex(p_index,4)                           +  '     prev index\n'
189         if for_sig is None:
190             sig = sig + chr(1)                               # hashtype
191             script  = int_to_hex( len(sig))                  +  '     push %d bytes\n'%len(sig)
192             script += sig.encode('hex')                      +  '     sig\n'
193             pubkey = chr(4) + pubkey
194             script += int_to_hex( len(pubkey))               +  '     push %d bytes\n'%len(pubkey)
195             script += pubkey.encode('hex')                   +  '     pubkey\n'
196         elif for_sig==i:
197             script = p_script                                +  '     scriptsig \n'
198         else:
199             script=''
200         s += int_to_hex( len(filter(script))/2 )             +  '     script length \n'
201         s += script
202         s += "ffffffff"                                      +  '     sequence\n'
203     s += int_to_hex( len(outputs) )                          +  '     number of outputs\n'
204     for output in outputs:
205         addr, amount = output
206         s += int_to_hex( amount, 8)                          +  '     amount: %d\n'%amount 
207         script = '76a9'                                      # op_dup, op_hash_160
208         script += '14'                                       # push 0x14 bytes
209         script += bc_address_to_hash_160(addr).encode('hex')
210         script += '88ac'                                     # op_equalverify, op_checksig
211         s += int_to_hex( len(filter(script))/2 )             +  '     script length \n'
212         s += script                                          +  '     script \n'
213     s += int_to_hex(0,4)                                     # lock time
214     if for_sig is not None: s += int_to_hex(1, 4)            # hash type
215     return s
216
217
218
219
220 def format_satoshis(x, is_diff=False):
221     from decimal import Decimal
222     s = str( Decimal(x) /100000000 )
223     if is_diff and x>0:
224         s = "+" + s
225     if not '.' in s: s += '.'
226     p = s.find('.')
227     s += " "*( 9 - ( len(s) - p ))
228     s = " "*( 5 - ( p )) + s
229     return s
230
231
232 from version import ELECTRUM_VERSION, SEED_VERSION
233
234 DEFAULT_PORT = 50000
235
236
237 class Wallet:
238     def __init__(self):
239
240         self.electrum_version = ELECTRUM_VERSION
241         self.seed_version = SEED_VERSION
242
243         self.gap_limit = 5           # configuration
244         self.fee = 100000
245         self.master_public_key = ''
246
247         # saved fields
248         self.use_encryption = False
249         self.addresses = []          # receiving addresses visible for user
250         self.change_addresses = []   # addresses used as change
251         self.seed = ''               # encrypted
252         self.status = {}             # current status of addresses
253         self.history = {}
254         self.labels = {}             # labels for addresses and transactions
255         self.aliases = {}            # aliases for addresses
256         self.authorities = {}        # trusted addresses
257         
258         self.receipts = {}           # signed URIs
259         self.receipt = None          # next receipt
260         self.addressbook = []        # outgoing addresses, for payments
261
262         # not saved
263         self.tx_history = {}
264
265         self.imported_keys = {}
266         self.remote_url = None
267
268         self.was_updated = False 
269         self.blocks = 0 
270         self.banner = ''
271         self.up_to_date_event = threading.Event()
272         self.up_to_date_event.clear()
273
274
275     def set_server(self, host, port):
276         if host!= self.host or port!=self.port:
277             self.host = host
278             self.port = port
279             self.interface.is_connected = False  # this exits the polling loop
280
281     def set_path(self, wallet_path):
282
283         if wallet_path is not None:
284             self.path = wallet_path
285         else:
286             # backward compatibility: look for wallet file in the default data directory
287             if "HOME" in os.environ:
288                 wallet_dir = os.path.join( os.environ["HOME"], '.electrum')
289             elif "LOCALAPPDATA" in os.environ:
290                 wallet_dir = os.path.join( os.environ["LOCALAPPDATA"], 'Electrum' )
291             elif "APPDATA" in os.environ:
292                 wallet_dir = os.path.join( os.environ["APPDATA"], 'Electrum' )
293             else:
294                 raise BaseException("No home directory found in environment variables.")
295
296             if not os.path.exists( wallet_dir ): os.mkdir( wallet_dir )
297             self.path = os.path.join( wallet_dir, 'electrum.dat' )
298
299     def import_key(self, keypair, password):
300         address, key = keypair.split(':')
301         if not self.is_valid(address): return False
302         if address in self.all_addresses(): return False
303         b = ASecretToSecret( key )
304         if not b: return False
305         secexp = int( b.encode('hex'), 16)
306         private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve=SECP256k1 )
307         # sanity check
308         public_key = private_key.get_verifying_key()
309         if not address == public_key_to_bc_address( '04'.decode('hex') + public_key.to_string() ): return False
310         self.imported_keys[address] = self.pw_encode( key, password )
311         return True
312
313     def new_seed(self, password):
314         seed = "%032x"%ecdsa.util.randrange( pow(2,128) )
315         self.init_mpk(seed)
316         # encrypt
317         self.seed = self.pw_encode( seed, password )
318
319         # create addresses
320         self.create_new_address_without_history(True)
321         for i in range(self.gap_limit):
322             self.create_new_address_without_history(False)
323
324
325     def init_mpk(self,seed):
326         # public key
327         curve = SECP256k1
328         secexp = self.stretch_key(seed)
329         master_private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
330         self.master_public_key = master_private_key.get_verifying_key().to_string()
331
332     def all_addresses(self):
333         return self.addresses + self.change_addresses + self.imported_keys.keys()
334
335     def is_mine(self, address):
336         return address in self.all_addresses()
337
338     def is_change(self, address):
339         return address in self.change_addresses
340
341     def is_valid(self,addr):
342         ADDRESS_RE = re.compile('[1-9A-HJ-NP-Za-km-z]{26,}\\Z')
343         if not ADDRESS_RE.match(addr): return False
344         try:
345             h = bc_address_to_hash_160(addr)
346         except:
347             return False
348         return addr == hash_160_to_bc_address(h)
349
350     def stretch_key(self,seed):
351         oldseed = seed
352         for i in range(100000):
353             seed = hashlib.sha256(seed + oldseed).digest()
354         return string_to_number( seed )
355
356     def get_sequence(self,n,for_change):
357         return string_to_number( Hash( "%d:%d:"%(n,for_change) + self.master_public_key ) )
358
359     def get_private_key(self, address, password):
360         """  Privatekey(type,n) = Master_private_key + H(n|S|type)  """
361         order = generator_secp256k1.order()
362         
363         if address in self.imported_keys.keys():
364             b = self.pw_decode( self.imported_keys[address], password )
365             b = ASecretToSecret( b )
366             secexp = int( b.encode('hex'), 16)
367         else:
368             if address in self.addresses:
369                 n = self.addresses.index(address)
370                 for_change = False
371             elif address in self.change_addresses:
372                 n = self.change_addresses.index(address)
373                 for_change = True
374             else:
375                 raise BaseException("unknown address")
376             try:
377                 seed = self.pw_decode( self.seed, password)
378             except:
379                 raise BaseException("Invalid password")
380             secexp = self.stretch_key(seed)
381             secexp = ( secexp + self.get_sequence(n,for_change) ) % order
382
383         pk = number_to_string(secexp,order)
384         return pk
385
386     def msg_magic(self, message):
387         return "\x18Bitcoin Signed Message:\n" + chr( len(message) ) + message
388
389     def sign_message(self, address, message, password):
390         private_key = ecdsa.SigningKey.from_string( self.get_private_key(address, password), curve = SECP256k1 )
391         public_key = private_key.get_verifying_key()
392         signature = private_key.sign_digest( Hash( self.msg_magic( message ) ), sigencode = ecdsa.util.sigencode_string )
393         assert public_key.verify_digest( signature, Hash( self.msg_magic( message ) ), sigdecode = ecdsa.util.sigdecode_string)
394         for i in range(4):
395             sig = base64.b64encode( chr(27+i) + signature )
396             try:
397                 self.verify_message( address, sig, message)
398                 return sig
399             except:
400                 continue
401         else:
402             raise BaseException("error: cannot sign message")
403         
404             
405     def verify_message(self, address, signature, message):
406         """ See http://www.secg.org/download/aid-780/sec1-v2.pdf for the math """
407         from ecdsa import numbertheory, ellipticcurve, util
408         import msqr
409         curve = curve_secp256k1
410         G = generator_secp256k1
411         order = G.order()
412         # extract r,s from signature
413         sig = base64.b64decode(signature)
414         if len(sig) != 65: raise BaseException("Wrong encoding")
415         r,s = util.sigdecode_string(sig[1:], order)
416         recid = ord(sig[0]) - 27
417         # 1.1
418         x = r + (recid/2) * order
419         # 1.3
420         alpha = ( x * x * x  + curve.a() * x + curve.b() ) % curve.p()
421         beta = msqr.modular_sqrt(alpha, curve.p())
422         y = beta if (beta - recid) % 2 == 0 else curve.p() - beta
423         # 1.4 the constructor checks that nR is at infinity
424         R = ellipticcurve.Point(curve, x, y, order)
425         # 1.5 compute e from message:
426         h = Hash( self.msg_magic( message ) )
427         e = string_to_number(h)
428         minus_e = -e % order
429         # 1.6 compute Q = r^-1 (sR - eG)
430         inv_r = numbertheory.inverse_mod(r,order)
431         Q = inv_r * ( s * R + minus_e * G )
432         public_key = ecdsa.VerifyingKey.from_public_point( Q, curve = SECP256k1 )
433         # check that Q is the public key
434         public_key.verify_digest( sig[1:], h, sigdecode = ecdsa.util.sigdecode_string)
435         # check that we get the original signing address
436         addr = public_key_to_bc_address( '04'.decode('hex') + public_key.to_string() )
437         # print addr
438         if address != addr:
439             print "bad signature"
440             raise BaseException("Bad signature")
441     
442
443     def create_new_address_without_history(self, for_change):
444         """   Publickey(type,n) = Master_public_key + H(n|S|type)*point  """
445         curve = SECP256k1
446         n = len(self.change_addresses) if for_change else len(self.addresses)
447         z = self.get_sequence(n,for_change)
448         master_public_key = ecdsa.VerifyingKey.from_string( self.master_public_key, curve = SECP256k1 )
449         pubkey_point = master_public_key.pubkey.point + z*curve.generator
450         public_key2 = ecdsa.VerifyingKey.from_public_point( pubkey_point, curve = SECP256k1 )
451         address = public_key_to_bc_address( '04'.decode('hex') + public_key2.to_string() )
452         if for_change:
453             self.change_addresses.append(address)
454         else:
455             self.addresses.append(address)
456
457         self.history[address] = []
458         self.status[address] = None
459         print address
460         return address
461
462
463     def create_new_address(self, bool):
464         address = self.create_new_address_without_history(bool)
465         self.interface.subscribe([address])
466         return address
467
468
469     def synchronize(self):
470         if not self.master_public_key:
471             return False
472
473         is_new = False
474         while True:
475             if self.change_addresses == []:
476                 self.create_new_address(True)
477                 is_new = True
478                 continue
479             a = self.change_addresses[-1]
480             if self.history.get(a):
481                 self.create_new_address(True)
482                 is_new = True
483             else:
484                 break
485
486         n = self.gap_limit
487         while True:
488             if len(self.addresses) < n:
489                 self.create_new_address(False)
490                 is_new = True
491                 continue
492             if map( lambda a: self.history.get(a), self.addresses[-n:] ) == n*[[]]:
493                 break
494             else:
495                 self.create_new_address(False)
496                 is_new = True
497
498         if self.remote_url:
499             num = self.get_remote_number()
500             while len(self.addresses)<num:
501                 self.create_new_address(False)
502                 is_new = True
503
504         return is_new
505
506
507     def get_remote_number(self):
508         import jsonrpclib
509         server = jsonrpclib.Server(self.remote_url)
510         out = server.getnum()
511         return out
512
513     def get_remote_mpk(self):
514         import jsonrpclib
515         server = jsonrpclib.Server(self.remote_url)
516         out = server.getkey()
517         return out
518
519     def is_found(self):
520         return (len(self.change_addresses) > 1 ) or ( len(self.addresses) > self.gap_limit )
521
522     def fill_addressbook(self):
523         for tx in self.tx_history.values():
524             if tx['value']<0:
525                 for i in tx['outputs']:
526                     if not self.is_mine(i) and i not in self.addressbook:
527                         self.addressbook.append(i)
528         # redo labels
529         self.update_tx_labels()
530
531
532     def save(self):
533         s = {
534             'seed_version':self.seed_version,
535             'use_encryption':self.use_encryption,
536             'master_public_key': self.master_public_key.encode('hex'),
537             'fee':self.fee,
538             'host':self.host,
539             'port':self.port,
540             #'blocks':self.interface.blocks,
541             'seed':self.seed,
542             'addresses':self.addresses,
543             'change_addresses':self.change_addresses,
544             'status':self.status,
545             'history':self.history, 
546             'labels':self.labels,
547             'contacts':self.addressbook,
548             'imported_keys':self.imported_keys,
549             'aliases':self.aliases,
550             'authorities':self.authorities,
551             'receipts':self.receipts,
552             }
553         f = open(self.path,"w")
554         f.write( repr(s) )
555         f.close()
556
557     def read(self):
558         import interface
559
560         upgrade_msg = """This wallet seed is deprecated. Please run upgrade.py for a diagnostic."""
561         self.file_exists = False
562         try:
563             f = open(self.path,"r")
564             data = f.read()
565             f.close()
566         except:
567             self.host = ''
568             self.port = DEFAULT_PORT
569             return
570         try:
571             d = ast.literal_eval( data )
572             self.seed_version = d.get('seed_version')
573             self.master_public_key = d.get('master_public_key').decode('hex')
574             self.use_encryption = d.get('use_encryption')
575             self.fee = int( d.get('fee') )
576             self.seed = d.get('seed')
577             self.host = d.get('host')
578             self.port = d.get('port')
579             blocks = d.get('blocks')
580             self.addresses = d.get('addresses')
581             self.change_addresses = d.get('change_addresses')
582             self.status = d.get('status')
583             self.history = d.get('history')
584             self.labels = d.get('labels')
585             self.addressbook = d.get('contacts')
586             self.imported_keys = d.get('imported_keys',{})
587             self.aliases = d.get('aliases',{})
588             self.authorities = d.get('authorities',{})
589             self.receipts = d.get('receipts',{})
590         except:
591             raise BaseException("cannot read wallet file")
592
593         self.update_tx_history()
594
595         if self.seed_version != SEED_VERSION:
596             raise BaseException(upgrade_msg)
597
598         if self.remote_url: assert self.master_public_key.encode('hex') == self.get_remote_mpk()
599
600         self.file_exists = True
601         #self.interface = interface.start_interface(self)
602
603         
604     def get_new_address(self):
605         n = 0 
606         for addr in self.addresses[-self.gap_limit:]:
607             if not self.history.get(addr): 
608                 n = n + 1
609         if n < self.gap_limit:
610             new_address = self.create_new_address(False)
611             self.history[new_address] = [] #get from server
612             return True, new_address
613         else:
614             return False, "The last %d addresses in your list have never been used. You should use them first, or increase the allowed gap size in your preferences. "%self.gap_limit
615
616     def get_addr_balance(self, addr):
617         if self.is_mine(addr):
618             h = self.history.get(addr)
619         else:
620             h = self.interface.retrieve_history(addr)
621         if not h: return 0,0
622         c = u = 0
623         for item in h:
624             v = item['value']
625             if item['height']:
626                 c += v
627             else:
628                 u += v
629         return c, u
630
631     def get_balance(self):
632         conf = unconf = 0
633         for addr in self.all_addresses(): 
634             c, u = self.get_addr_balance(addr)
635             conf += c
636             unconf += u
637         return conf, unconf
638
639
640     def choose_tx_inputs( self, amount, fixed_fee, from_addr = None ):
641         """ todo: minimize tx size """
642         total = 0
643         fee = self.fee if fixed_fee is None else fixed_fee
644
645         coins = []
646         domain = [from_addr] if from_addr else self.all_addresses()
647         for addr in domain:
648             h = self.history.get(addr)
649             if h is None: continue
650             for item in h:
651                 if item.get('raw_scriptPubKey'):
652                     coins.append( (addr,item))
653
654         coins = sorted( coins, key = lambda x: x[1]['nTime'] )
655         inputs = []
656         for c in coins: 
657             addr, item = c
658             v = item.get('value')
659             total += v
660             inputs.append((addr, v, item['tx_hash'], item['pos'], item['raw_scriptPubKey'], None, None) )
661             fee = self.fee*len(inputs) if fixed_fee is None else fixed_fee
662             if total >= amount + fee: break
663         else:
664             #print "not enough funds: %d %d"%(total, fee)
665             inputs = []
666         return inputs, total, fee
667
668     def choose_tx_outputs( self, to_addr, amount, fee, total, change_addr=None ):
669         outputs = [ (to_addr, amount) ]
670         change_amount = total - ( amount + fee )
671         if change_amount != 0:
672             # normally, the update thread should ensure that the last change address is unused
673             if not change_addr:
674                 change_addr = self.change_addresses[-1]
675             outputs.append( ( change_addr,  change_amount) )
676         return outputs
677
678     def sign_inputs( self, inputs, outputs, password ):
679         s_inputs = []
680         for i in range(len(inputs)):
681             addr, v, p_hash, p_pos, p_scriptPubKey, _, _ = inputs[i]
682             private_key = ecdsa.SigningKey.from_string( self.get_private_key(addr, password), curve = SECP256k1 )
683             public_key = private_key.get_verifying_key()
684             pubkey = public_key.to_string()
685             tx = filter( raw_tx( inputs, outputs, for_sig = i ) )
686             sig = private_key.sign_digest( Hash( tx.decode('hex') ), sigencode = ecdsa.util.sigencode_der )
687             assert public_key.verify_digest( sig, Hash( tx.decode('hex') ), sigdecode = ecdsa.util.sigdecode_der)
688             s_inputs.append( (addr, v, p_hash, p_pos, p_scriptPubKey, pubkey, sig) )
689         return s_inputs
690
691     def pw_encode(self, s, password):
692         if password:
693             secret = Hash(password)
694             return EncodeAES(secret, s)
695         else:
696             return s
697
698     def pw_decode(self, s, password):
699         if password is not None:
700             secret = Hash(password)
701             d = DecodeAES(secret, s)
702             if s == self.seed:
703                 try:
704                     d.decode('hex')
705                 except:
706                     raise BaseException("Invalid password")
707             return d
708         else:
709             return s
710
711     def receive_status_callback(self, addr, status):
712         if self.status.get(addr) != status:
713             #print "updating status for", addr, repr(self.status.get(addr)), repr(status)
714             self.status[addr] = status
715             self.interface.get_history(addr)
716
717     def receive_history_callback(self, addr, data):
718         #print "updating history for", addr
719         self.history[addr] = data
720         self.update_tx_history()
721         self.save()
722
723     def get_tx_history(self):
724         lines = self.tx_history.values()
725         lines = sorted(lines, key=operator.itemgetter("nTime"))
726         return lines
727
728     def update_tx_history(self):
729         self.tx_history= {}
730         for addr in self.all_addresses():
731             h = self.history.get(addr)
732             if h is None: continue
733             for tx in h:
734                 tx_hash = tx['tx_hash']
735                 line = self.tx_history.get(tx_hash)
736                 if not line:
737                     self.tx_history[tx_hash] = copy.copy(tx)
738                     line = self.tx_history.get(tx_hash)
739                 else:
740                     line['value'] += tx['value']
741                 if line['height'] == 0:
742                     line['nTime'] = 1e12
743         self.update_tx_labels()
744
745     def update_tx_labels(self):
746         for tx in self.tx_history.values():
747             default_label = ''
748             if tx['value']<0:
749                 for o_addr in tx['outputs']:
750                     if not self.is_change(o_addr):
751                         dest_label = self.labels.get(o_addr)
752                         if dest_label:
753                             default_label = 'to: ' + dest_label
754                         else:
755                             default_label = 'to: ' + o_addr
756             else:
757                 for o_addr in tx['outputs']:
758                     if self.is_mine(o_addr) and not self.is_change(o_addr):
759                         dest_label = self.labels.get(o_addr)
760                         if dest_label:
761                             default_label = 'at: ' + dest_label
762                         else:
763                             default_label = 'at: ' + o_addr
764             tx['default_label'] = default_label
765
766     def mktx(self, to_address, amount, label, password, fee=None, change_addr=None, from_addr= None):
767         if not self.is_valid(to_address):
768             raise BaseException("Invalid address")
769         inputs, total, fee = self.choose_tx_inputs( amount, fee, from_addr )
770         if not inputs:
771             raise BaseException("Not enough funds")
772         outputs = self.choose_tx_outputs( to_address, amount, fee, total, change_addr )
773         s_inputs = self.sign_inputs( inputs, outputs, password )
774
775         tx = filter( raw_tx( s_inputs, outputs ) )
776         if to_address not in self.addressbook:
777             self.addressbook.append(to_address)
778         if label: 
779             tx_hash = Hash(tx.decode('hex') )[::-1].encode('hex')
780             self.labels[tx_hash] = label
781
782         return tx
783
784     def sendtx(self, tx):
785         tx_hash = Hash(tx.decode('hex') )[::-1].encode('hex')
786         out = self.interface.send_tx(tx)
787         if out != tx_hash:
788             return False, "error: " + out
789         if self.receipt:
790             self.receipts[tx_hash] = self.receipt
791             self.receipt = None
792         return True, out
793
794
795     def read_alias(self, alias):
796         # this might not be the right place for this function.
797         import urllib
798
799         m1 = re.match('([\w\-\.]+)@((\w[\w\-]+\.)+[\w\-]+)', alias)
800         m2 = re.match('((\w[\w\-]+\.)+[\w\-]+)', alias)
801         if m1:
802             url = 'http://' + m1.group(2) + '/bitcoin.id/' + m1.group(1) 
803         elif m2:
804             url = 'http://' + alias + '/bitcoin.id'
805         else:
806             return ''
807         try:
808             lines = urllib.urlopen(url).readlines()
809         except:
810             return ''
811
812         # line 0
813         line = lines[0].strip().split(':')
814         if len(line) == 1:
815             auth_name = None
816             target = signing_addr = line[0]
817         else:
818             target, auth_name, signing_addr, signature = line
819             msg = "alias:%s:%s:%s"%(alias,target,auth_name)
820             print msg, signature
821             self.verify_message(signing_addr, signature, msg)
822         
823         # other lines are signed updates
824         for line in lines[1:]:
825             line = line.strip()
826             if not line: continue
827             line = line.split(':')
828             previous = target
829             print repr(line)
830             target, signature = line
831             self.verify_message(previous, signature, "alias:%s:%s"%(alias,target))
832
833         if not self.is_valid(target):
834             raise BaseException("Invalid bitcoin address")
835
836         return target, signing_addr, auth_name
837
838     def update_password(self, seed, new_password):
839         self.use_encryption = (new_password != '')
840         self.seed = self.pw_encode( seed, new_password)
841         for k in self.imported_keys.keys():
842             a = self.imported_keys[k]
843             b = self.pw_decode(a, password)
844             c = self.pw_encode(b, new_password)
845             self.imported_keys[k] = c
846         self.save()
847
848     def get_alias(self, alias, interactive = False, show_message=None, question = None):
849         try:
850             target, signing_address, auth_name = self.read_alias(alias)
851         except BaseException, e:
852             # raise exception if verify fails (verify the chain)
853             if interactive:
854                 show_message("Alias error: " + e.message)
855             return
856
857         print target, signing_address, auth_name
858
859         if auth_name is None:
860             a = self.aliases.get(alias)
861             if not a:
862                 msg = "Warning: the alias '%s' is self-signed.\nThe signing address is %s.\n\nDo you want to add this alias to your list of contacts?"%(alias,signing_address)
863                 if interactive and question( msg ):
864                     self.aliases[alias] = (signing_address, target)
865                 else:
866                     target = None
867             else:
868                 if signing_address != a[0]:
869                     msg = "Warning: the key of alias '%s' has changed since your last visit! It is possible that someone is trying to do something nasty!!!\nDo you accept to change your trusted key?"%alias
870                     if interactive and question( msg ):
871                         self.aliases[alias] = (signing_address, target)
872                     else:
873                         target = None
874         else:
875             if signing_address not in self.authorities.keys():
876                 msg = "The alias: '%s' links to %s\n\nWarning: this alias was signed by an unknown key.\nSigning authority: %s\nSigning address: %s\n\nDo you want to add this key to your list of trusted keys?"%(alias,target,auth_name,signing_address)
877                 if interactive and question( msg ):
878                     self.authorities[signing_address] = auth_name
879                 else:
880                     target = None
881
882         if target:
883             self.aliases[alias] = (signing_address, target)
884             
885         return target
886
887
888     def parse_url(self, url, show_message, question):
889         o = url[8:].split('?')
890         address = o[0]
891         if len(o)>1:
892             params = o[1].split('&')
893         else:
894             params = []
895
896         amount = label = message = signature = identity = ''
897         for p in params:
898             k,v = p.split('=')
899             uv = urldecode(v)
900             if k == 'amount': amount = uv
901             elif k == 'message': message = uv
902             elif k == 'label': label = uv
903             elif k == 'signature':
904                 identity, signature = uv.split(':')
905                 url = url.replace('&%s=%s'%(k,v),'')
906             else: 
907                 print k,v
908
909         if signature:
910             if re.match('^(|([\w\-\.]+)@)((\w[\w\-]+\.)+[\w\-]+)$', identity):
911                 signing_address = self.get_alias(identity, True, show_message, question)
912             elif self.is_valid(identity):
913                 signing_address = identity
914             else:
915                 signing_address = None
916             if not signing_address:
917                 return
918             try:
919                 self.verify_message(signing_address, signature, url )
920                 self.receipt = (signing_address, signature, url)
921             except:
922                 show_message('Warning: the URI contains a bad signature.\nThe identity of the recipient cannot be verified.')
923                 address = amount = label = identity = message = ''
924
925         if re.match('^(|([\w\-\.]+)@)((\w[\w\-]+\.)+[\w\-]+)$', address):
926             payto_address = self.get_alias(address, True, show_message, question)
927             if payto_address:
928                 address = address + ' <' + payto_address + '>'
929
930         return address, amount, label, message, signature, identity, url
931
932
933
934     def handle_response(self, r):
935         if r is None:
936             return
937
938         method = r['method']
939         params = r['params']
940         result = r['result']
941
942         if method == 'server.banner':
943             self.banner = result
944             self.was_updated = True
945
946         elif method == 'session.poll':
947             # native poll
948             blocks, changed_addresses = result 
949             if blocks == -1: raise BaseException("session not found")
950             self.blocks = int(blocks)
951             if changed_addresses:
952                 self.was_updated = True
953                 for addr, status in changed_addresses.items():
954                     self.receive_status_callback(addr, status)
955
956         elif method == 'server.peers':
957             self.interface.servers = map( lambda x:x[1], result )
958
959         elif method == 'address.subscribe':
960             addr = params[-1]
961             self.receive_status_callback(addr, result)
962                             
963         elif method == 'address.get_history':
964             addr = params[0]
965             self.receive_history_callback(addr, result)
966             self.was_updated = True
967
968         elif method == 'transaction.broadcast':
969             self.tx_result = result
970             self.tx_event.set()
971
972         elif method == 'numblocks.subscribe':
973             self.blocks = result
974
975         elif method == 'client.version':
976             pass
977
978         else:
979             print "unknown message:", method, params, result
980
981
982     def update(self):
983         self.up_to_date_event.wait()
984
985
986     def run(self):
987         while self.interface.is_connected:
988             # the interface should use an input queue for requests so that we don't care about synchronous
989             is_new = self.synchronize() # in synchronous mode, this puts new responses in the queue
990             if self.interface.is_up_to_date() and not is_new:
991                 self.up_to_date = True
992                 self.up_to_date_event.set()
993             else:
994                 self.up_to_date = False
995
996             response = self.interface.responses.get()
997             self.handle_response(response)
998
999     def start_interface(self):
1000         from interface import NativeInterface, AsynchronousInterface, HttpInterface, DEFAULT_SERVERS
1001         import random
1002
1003         if not self.host:
1004             self.host = random.choice( DEFAULT_SERVERS )         # random choice when the wallet is created
1005         
1006         if self.port == 50000:
1007             InterfaceClass = NativeInterface
1008         elif self.port == 50001:
1009             InterfaceClass = AsynchronousInterface
1010         elif self.port in [80, 81, 8080, 8081]:
1011             InterfaceClass = HttpInterface
1012         else:
1013             print "unknown port number: %d. using native protocol."%self.port
1014             InterfaceClass = NativeInterface
1015
1016         self.interface = InterfaceClass(self.host, self.port)
1017         addresses = self.all_addresses()
1018         version = self.electrum_version
1019         self.interface.start_session(addresses, version)
1020         print "Starting new session: %s:%d"%(self.host,self.port)