3 # Electrum - lightweight Bitcoin client
4 # Copyright (C) 2011 thomasv@gitorious
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.
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.
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/>.
34 from ecdsa.util import string_to_number, number_to_string
35 from util import print_error, user_dir, format_satoshis
39 _ud = re.compile('%([0-9a-hA-H]{2})', re.MULTILINE)
40 urldecode = lambda x: _ud.sub(lambda m: chr(int(m.group(1), 16)), x)
43 EncodeAES = lambda secret, s: base64.b64encode(aes.encryptData(secret,s))
44 DecodeAES = lambda secret, e: aes.decryptData(secret, base64.b64decode(e))
47 from version import ELECTRUM_VERSION, SEED_VERSION
51 def __init__(self, config={}):
54 self.electrum_version = ELECTRUM_VERSION
57 self.seed_version = config.get('seed_version', SEED_VERSION)
58 self.gap_limit = config.get('gap_limit', 5)
59 self.use_change = config.get('use_change',True)
60 self.fee = int(config.get('fee',100000))
61 self.num_zeros = int(config.get('num_zeros',0))
62 self.master_public_key = config.get('master_public_key','')
63 self.use_encryption = config.get('use_encryption', False)
64 self.addresses = config.get('addresses', []) # receiving addresses visible for user
65 self.change_addresses = config.get('change_addresses', []) # addresses used as change
66 self.seed = config.get('seed', '') # encrypted
67 self.labels = config.get('labels',{'1NmduGNyC5XejoysbuioodCN3jR3yf64xM':'Electrum donation address'})
68 self.aliases = config.get('aliases', {}) # aliases for addresses
69 self.authorities = config.get('authorities', {}) # trusted addresses
70 self.frozen_addresses = config.get('frozen_addresses',[])
71 self.prioritized_addresses = config.get('prioritized_addresses',[])
72 self.receipts = config.get('receipts',{}) # signed URIs
73 self.addressbook = config.get('contacts', ['1NmduGNyC5XejoysbuioodCN3jR3yf64xM'])
74 self.imported_keys = config.get('imported_keys',{})
75 self.history = config.get('addr_history',{}) # address -> list(txid, height)
76 self.transactions = config.get('transactions',{}) # txid -> deserialised
78 self.requested_amounts = config.get('requested_amounts',{}) # txid -> deserialised
81 self.prevout_values = {} # my own transaction outputs
82 self.spent_outputs = []
83 self.receipt = None # next receipt
89 # there is a difference between wallet.up_to_date and interface.is_up_to_date()
90 # interface.is_up_to_date() returns true when all requests have been answered and processed
91 # wallet.up_to_date is true when the wallet is synchronized (stronger requirement)
93 self.up_to_date = False
94 self.lock = threading.Lock()
95 self.tx_event = threading.Event()
97 if self.seed_version != SEED_VERSION:
98 raise ValueError("This wallet seed is deprecated. Please run upgrade.py for a diagnostic.")
100 for tx_hash in self.transactions.keys():
101 self.update_tx_outputs(tx_hash)
104 def set_up_to_date(self,b):
105 with self.lock: self.up_to_date = b
107 def is_up_to_date(self):
108 with self.lock: return self.up_to_date
111 self.up_to_date = False
112 self.interface.poke('synchronizer')
113 while not self.is_up_to_date(): time.sleep(0.1)
115 def import_key(self, sec, password):
117 # rebuild public key from private key, compressed or uncompressed
118 pkey = regenerate_key(sec)
122 # figure out if private key is compressed
123 compressed = is_compressed(sec)
125 # rebuild private and public key from regenerated secret
126 private_key = GetPrivKey(pkey, compressed)
127 public_key = GetPubKey(pkey, compressed)
128 address = public_key_to_bc_address(public_key)
130 if address in self.all_addresses():
131 raise BaseException('Address already in wallet')
133 # store the originally requested keypair into the imported keys table
134 self.imported_keys[address] = self.pw_encode(sec, password )
138 def new_seed(self, password):
139 seed = "%032x"%ecdsa.util.randrange( pow(2,128) )
142 self.seed = self.pw_encode( seed, password )
145 def init_mpk(self,seed):
148 secexp = self.stretch_key(seed)
149 master_private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
150 self.master_public_key = master_private_key.get_verifying_key().to_string().encode('hex')
152 def all_addresses(self):
153 return self.addresses + self.change_addresses + self.imported_keys.keys()
155 def is_mine(self, address):
156 return address in self.all_addresses()
158 def is_change(self, address):
159 return address in self.change_addresses
161 def is_valid(self,addr):
162 ADDRESS_RE = re.compile('[1-9A-HJ-NP-Za-km-z]{26,}\\Z')
163 if not ADDRESS_RE.match(addr): return False
165 h = bc_address_to_hash_160(addr)
168 return addr == hash_160_to_bc_address(h)
170 def stretch_key(self,seed):
172 for i in range(100000):
173 seed = hashlib.sha256(seed + oldseed).digest()
174 return string_to_number( seed )
176 def get_sequence(self,n,for_change):
177 return string_to_number( Hash( "%d:%d:"%(n,for_change) + self.master_public_key.decode('hex') ) )
179 def get_private_key_base58(self, address, password):
180 secexp, compressed = self.get_private_key(address, password)
181 if secexp is None: return None
182 pk = number_to_string( secexp, generator_secp256k1.order() )
183 return SecretToASecret( pk, compressed )
185 def get_private_key(self, address, password):
186 """ Privatekey(type,n) = Master_private_key + H(n|S|type) """
187 order = generator_secp256k1.order()
189 if address in self.imported_keys.keys():
190 sec = self.pw_decode( self.imported_keys[address], password )
191 if not sec: return None, None
193 pkey = regenerate_key(sec)
194 compressed = is_compressed(sec)
198 if address in self.addresses:
199 n = self.addresses.index(address)
201 elif address in self.change_addresses:
202 n = self.change_addresses.index(address)
205 raise BaseException("unknown address")
207 seed = self.pw_decode( self.seed, password)
209 raise BaseException("Invalid password")
210 if not seed: return None
211 secexp = self.stretch_key(seed)
212 secexp = ( secexp + self.get_sequence(n,for_change) ) % order
215 return secexp, compressed
217 def msg_magic(self, message):
218 return "\x18Bitcoin Signed Message:\n" + chr( len(message) ) + message
220 def sign_message(self, address, message, password):
221 secexp, compressed = self.get_private_key(address, password)
222 private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
223 public_key = private_key.get_verifying_key()
224 signature = private_key.sign_digest( Hash( self.msg_magic( message ) ), sigencode = ecdsa.util.sigencode_string )
225 assert public_key.verify_digest( signature, Hash( self.msg_magic( message ) ), sigdecode = ecdsa.util.sigdecode_string)
227 sig = base64.b64encode( chr(27 + i + (4 if compressed else 0)) + signature )
229 self.verify_message( address, sig, message)
234 raise BaseException("error: cannot sign message")
237 def verify_message(self, address, signature, message):
238 """ See http://www.secg.org/download/aid-780/sec1-v2.pdf for the math """
239 from ecdsa import numbertheory, ellipticcurve, util
241 curve = curve_secp256k1
242 G = generator_secp256k1
244 # extract r,s from signature
245 sig = base64.b64decode(signature)
246 if len(sig) != 65: raise BaseException("Wrong encoding")
247 r,s = util.sigdecode_string(sig[1:], order)
249 if nV < 27 or nV >= 35:
250 raise BaseException("Bad encoding")
259 x = r + (recid/2) * order
261 alpha = ( x * x * x + curve.a() * x + curve.b() ) % curve.p()
262 beta = msqr.modular_sqrt(alpha, curve.p())
263 y = beta if (beta - recid) % 2 == 0 else curve.p() - beta
264 # 1.4 the constructor checks that nR is at infinity
265 R = ellipticcurve.Point(curve, x, y, order)
266 # 1.5 compute e from message:
267 h = Hash( self.msg_magic( message ) )
268 e = string_to_number(h)
270 # 1.6 compute Q = r^-1 (sR - eG)
271 inv_r = numbertheory.inverse_mod(r,order)
272 Q = inv_r * ( s * R + minus_e * G )
273 public_key = ecdsa.VerifyingKey.from_public_point( Q, curve = SECP256k1 )
274 # check that Q is the public key
275 public_key.verify_digest( sig[1:], h, sigdecode = ecdsa.util.sigdecode_string)
276 # check that we get the original signing address
277 addr = public_key_to_bc_address( encode_point(public_key, compressed) )
279 raise BaseException("Bad signature")
282 def create_new_address(self, for_change):
283 n = len(self.change_addresses) if for_change else len(self.addresses)
284 address = self.get_new_address(n, for_change)
286 self.change_addresses.append(address)
288 self.addresses.append(address)
289 self.history[address] = []
292 def get_new_address(self, n, for_change):
293 """ Publickey(type,n) = Master_public_key + H(n|S|type)*point """
295 z = self.get_sequence(n, for_change)
296 master_public_key = ecdsa.VerifyingKey.from_string( self.master_public_key.decode('hex'), curve = SECP256k1 )
297 pubkey_point = master_public_key.pubkey.point + z*curve.generator
298 public_key2 = ecdsa.VerifyingKey.from_public_point( pubkey_point, curve = SECP256k1 )
299 address = public_key_to_bc_address( '04'.decode('hex') + public_key2.to_string() )
304 def change_gap_limit(self, value):
305 if value >= self.gap_limit:
306 self.gap_limit = value
308 self.interface.poke('synchronizer')
311 elif value >= self.min_acceptable_gap():
312 k = self.num_unused_trailing_addresses()
313 n = len(self.addresses) - k + value
314 self.addresses = self.addresses[0:n]
315 self.gap_limit = value
321 def num_unused_trailing_addresses(self):
323 for a in self.addresses[::-1]:
324 if self.history.get(a):break
328 def min_acceptable_gap(self):
329 # fixme: this assumes wallet is synchronized
332 k = self.num_unused_trailing_addresses()
333 for a in self.addresses[0:-k]:
334 if self.history.get(a):
338 if n > nmax: nmax = n
342 def synchronize(self):
343 if not self.master_public_key:
348 if self.change_addresses == []:
349 new_addresses.append( self.create_new_address(True) )
351 a = self.change_addresses[-1]
352 if self.history.get(a):
353 new_addresses.append( self.create_new_address(True) )
359 if len(self.addresses) < n:
360 new_addresses.append( self.create_new_address(False) )
362 if map( lambda a: self.history.get(a, []), self.addresses[-n:] ) == n*[[]]:
365 new_addresses.append( self.create_new_address(False) )
371 return (len(self.change_addresses) > 1 ) or ( len(self.addresses) > self.gap_limit )
373 def fill_addressbook(self):
374 for tx_hash, tx in self.transactions.items():
375 is_send, _, _ = self.get_tx_value(tx_hash)
377 for o in tx['outputs']:
378 addr = o.get('address')
379 if not self.is_mine(addr) and addr not in self.addressbook:
380 self.addressbook.append(addr)
382 # self.update_tx_labels()
385 def get_address_flags(self, addr):
386 flags = "C" if self.is_change(addr) else "I" if addr in self.imported_keys.keys() else "-"
387 flags += "F" if addr in self.frozen_addresses else "P" if addr in self.prioritized_addresses else "-"
391 def get_tx_value(self, tx_hash, addresses = None):
392 # return the balance for that tx
393 if addresses is None: addresses = self.all_addresses()
397 v_in = v_out = v_out_mine = 0
398 d = self.transactions.get(tx_hash)
402 for item in d.get('inputs'):
403 addr = item.get('address')
404 if addr in addresses:
406 key = item['prevout_hash'] + ':%d'%item['prevout_n']
407 value = self.prevout_values.get( key )
415 for item in d.get('outputs'):
416 addr = item.get('address')
417 value = item.get('value')
419 if addr in addresses:
423 # all inputs are mine:
425 v = v_out_mine - v_in
427 # some inputs are mine:
430 v = v_out_mine - v_out
435 return is_send, v, fee
438 def get_tx_details(self, tx_hash):
440 if not tx_hash: return ''
441 tx = self.transactions.get(tx_hash)
442 is_mine, v, fee = self.get_tx_value(tx_hash)
443 conf, timestamp = self.verifier.get_confirmations(tx_hash)
446 time_str = datetime.datetime.fromtimestamp(timestamp).isoformat(' ')[:-3]
450 inputs = map(lambda x: x.get('address'), tx['inputs'])
451 outputs = map(lambda x: x.get('address'), tx['outputs'])
452 tx_details = "Transaction Details" +"\n\n" \
453 + "Transaction ID:\n" + tx_hash + "\n\n" \
454 + "Status: %d confirmations\n"%conf
457 tx_details += "Amount sent: %s\n"% format_satoshis(v-fee, False) \
458 + "Transaction fee: %s\n"% format_satoshis(fee, False)
460 tx_details += "Amount sent: %s\n"% format_satoshis(v, False) \
461 + "Transaction fee: unknown\n"
463 tx_details += "Amount received: %s\n"% format_satoshis(v, False) \
465 tx_details += "Date: %s\n\n"%time_str \
466 + "Inputs:\n-"+ '\n-'.join(inputs) + "\n\n" \
467 + "Outputs:\n-"+ '\n-'.join(outputs)
469 r = self.receipts.get(tx_hash)
471 tx_details += "\n_______________________________________" \
472 + '\n\nSigned URI: ' + r[2] \
473 + "\n\nSigned by: " + r[0] \
474 + '\n\nSignature: ' + r[1]
479 def update_tx_outputs(self, tx_hash):
480 tx = self.transactions.get(tx_hash)
481 for item in tx.get('outputs'):
482 value = item.get('value')
483 key = tx_hash+ ':%d'%item.get('index')
485 self.prevout_values[key] = value
487 for item in tx.get('inputs'):
488 if self.is_mine(item.get('address')):
489 key = item['prevout_hash'] + ':%d'%item['prevout_n']
490 self.spent_outputs.append(key)
493 def get_addr_balance(self, address):
494 assert self.is_mine(address)
495 h = self.history.get(address,[])
496 if h == ['*']: return 0,0
498 received_coins = [] # list of coins received at address
500 for tx_hash, tx_height in h:
501 d = self.transactions.get(tx_hash)
503 for item in d.get('outputs'):
504 addr = item.get('address')
506 key = tx_hash + ':%d'%item['index']
507 received_coins.append(key)
509 for tx_hash, tx_height in h:
510 d = self.transactions.get(tx_hash)
514 for item in d.get('inputs'):
515 addr = item.get('address')
517 key = item['prevout_hash'] + ':%d'%item['prevout_n']
518 value = self.prevout_values.get( key )
519 if key in received_coins:
522 for item in d.get('outputs'):
523 addr = item.get('address')
524 key = tx_hash + ':%d'%item['index']
526 v += item.get('value')
534 def get_balance(self):
536 for addr in self.all_addresses():
537 c, u = self.get_addr_balance(addr)
543 def choose_tx_inputs( self, amount, fixed_fee, from_addr = None ):
544 """ todo: minimize tx size """
546 fee = self.fee if fixed_fee is None else fixed_fee
549 prioritized_coins = []
550 domain = [from_addr] if from_addr else self.all_addresses()
551 for i in self.frozen_addresses:
552 if i in domain: domain.remove(i)
554 for i in self.prioritized_addresses:
555 if i in domain: domain.remove(i)
558 h = self.history.get(addr, [])
559 if h == ['*']: continue
560 for tx_hash, tx_height in h:
561 tx = self.transactions.get(tx_hash)
562 for output in tx.get('outputs'):
563 if output.get('address') != addr: continue
564 key = tx_hash + ":%d" % output.get('index')
565 if key in self.spent_outputs: continue
566 output['tx_hash'] = tx_hash
570 for addr in self.prioritized_addresses:
571 h = self.history.get(addr, [])
572 if h == ['*']: continue
573 for tx_hash, tx_height in h:
574 tx = self.transactions.get(tx_hash)
575 for output in tx.get('outputs'):
576 if output.get('address') != addr: continue
577 key = tx_hash + ":%d" % output.get('index')
578 if key in self.spent_outputs: continue
579 output['tx_hash'] = tx_hash
580 prioritized_coins.append(output)
584 coins = prioritized_coins + coins
587 addr = item.get('address')
588 v = item.get('value')
590 inputs.append((addr, v, item['tx_hash'], item['index'], item['raw_output_script'], None, None) )
591 fee = self.fee*len(inputs) if fixed_fee is None else fixed_fee
592 if total >= amount + fee: break
594 #print "not enough funds: %s %s"%(format_satoshis(total), format_satoshis(fee))
596 return inputs, total, fee
598 def add_tx_change( self, outputs, amount, fee, total, change_addr=None ):
599 change_amount = total - ( amount + fee )
600 if change_amount != 0:
601 # normally, the update thread should ensure that the last change address is unused
603 change_addr = self.change_addresses[-1]
604 outputs.append( ( change_addr, change_amount) )
607 def sign_inputs( self, inputs, outputs, password ):
609 for i in range(len(inputs)):
610 addr, v, p_hash, p_pos, p_scriptPubKey, _, _ = inputs[i]
611 secexp, compressed = self.get_private_key(addr, password)
612 private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
613 public_key = private_key.get_verifying_key()
615 pkey = EC_KEY(secexp)
616 pubkey = GetPubKey(pkey, compressed)
618 tx = filter( raw_tx( inputs, outputs, for_sig = i ) )
619 sig = private_key.sign_digest( Hash( tx.decode('hex') ), sigencode = ecdsa.util.sigencode_der )
620 assert public_key.verify_digest( sig, Hash( tx.decode('hex') ), sigdecode = ecdsa.util.sigdecode_der)
621 s_inputs.append( (addr, v, p_hash, p_pos, p_scriptPubKey, pubkey, sig) )
624 def pw_encode(self, s, password):
626 secret = Hash(password)
627 return EncodeAES(secret, s)
631 def pw_decode(self, s, password):
632 if password is not None:
633 secret = Hash(password)
634 d = DecodeAES(secret, s)
639 raise ValueError("Invalid password")
645 def get_history(self, address):
647 return self.history.get(address)
649 def get_status(self, h):
650 if not h: return None
651 if h == ['*']: return '*'
653 for tx_hash, height in h:
654 status += tx_hash + ':%d:' % height
655 return hashlib.sha256( status ).digest().encode('hex')
659 def receive_tx_callback(self, tx_hash, tx):
661 if not self.check_new_tx(tx_hash, tx):
662 raise BaseException("error: received transaction is not consistent with history"%tx_hash)
665 self.transactions[tx_hash] = tx
667 tx_height = tx.get('height')
668 if self.verifier and tx_height>0:
669 self.verifier.add(tx_hash, tx_height)
671 self.update_tx_outputs(tx_hash)
676 def receive_history_callback(self, addr, hist):
678 if not self.check_new_history(addr, hist):
679 raise BaseException("error: received history for %s is not consistent with known transactions"%addr)
682 self.history[addr] = hist
686 for tx_hash, tx_height in hist:
688 # add it in case it was previously unconfirmed
689 if self.verifier: self.verifier.add(tx_hash, tx_height)
690 # set the height in case it changed
691 tx = self.transactions.get(tx_hash)
693 if tx.get('height') != tx_height:
694 print_error( "changing height for tx", tx_hash )
695 tx['height'] = tx_height
698 def get_tx_history(self):
700 history = self.transactions.values()
701 history.sort(key = lambda x: x.get('height') if x.get('height') else 1e12)
706 is_mine, v, fee = self.get_tx_value(tx['tx_hash'])
707 if v is not None: balance += v
708 c, u = self.get_balance()
711 v_str = format_satoshis( c+u - balance, True, self.num_zeros)
712 result.append( ('', 1000, 0, c+u-balance, None, c+u-balance, None ) )
714 balance = c + u - balance
716 tx_hash = tx['tx_hash']
717 conf, timestamp = self.verifier.get_confirmations(tx_hash) if self.verifier else (None, None)
718 is_mine, value, fee = self.get_tx_value(tx_hash)
719 if value is not None:
722 result.append( (tx_hash, conf, is_mine, value, fee, balance, timestamp) )
726 def get_transactions_at_height(self, height):
728 values = self.transactions.values()[:]
732 if tx['height'] == height:
733 out.append(tx['tx_hash'])
737 def get_label(self, tx_hash):
738 label = self.labels.get(tx_hash)
739 is_default = (label == '') or (label is None)
740 if is_default: label = self.get_default_label(tx_hash)
741 return label, is_default
743 def get_default_label(self, tx_hash):
744 tx = self.transactions.get(tx_hash)
747 is_mine, _, _ = self.get_tx_value(tx_hash)
749 for o in tx['outputs']:
750 o_addr = o.get('address')
751 if not self.is_mine(o_addr):
753 default_label = self.labels[o_addr]
755 default_label = o_addr
757 for o in tx['outputs']:
758 o_addr = o.get('address')
759 if self.is_mine(o_addr) and not self.is_change(o_addr):
762 for o in tx['outputs']:
763 o_addr = o.get('address')
764 if self.is_mine(o_addr):
770 dest_label = self.labels.get(o_addr)
772 default_label = self.labels[o_addr]
774 default_label = o_addr
779 def mktx(self, outputs, label, password, fee=None, change_addr=None, from_addr= None):
781 for address, x in outputs:
782 assert self.is_valid(address)
784 amount = sum( map(lambda x:x[1], outputs) )
785 inputs, total, fee = self.choose_tx_inputs( amount, fee, from_addr )
787 raise ValueError("Not enough funds")
789 if not self.use_change and not change_addr:
790 change_addr = inputs[-1][0]
791 print_error( "Sending change to", change_addr )
792 outputs = self.add_tx_change(outputs, amount, fee, total, change_addr)
795 return repr({'inputs':inputs, 'outputs':outputs})
797 tx = self.signed_tx(inputs, outputs, password)
799 for address, x in outputs:
800 if address not in self.addressbook and not self.is_mine(address):
801 self.addressbook.append(address)
804 tx_hash = Hash(tx.decode('hex') )[::-1].encode('hex')
805 self.labels[tx_hash] = label
809 def signed_tx(self, inputs, outputs, password):
810 s_inputs = self.sign_inputs( inputs, outputs, password )
811 tx = filter( raw_tx( s_inputs, outputs ) )
814 def sendtx(self, tx):
818 return self.receive_tx(h)
820 def send_tx(self, tx):
822 self.tx_event.clear()
823 tx_hash = Hash(tx.decode('hex') )[::-1].encode('hex')
824 self.interface.send([('blockchain.transaction.broadcast', [tx])], 'synchronizer')
827 def receive_tx(self,tx_hash):
830 return False, "error: " + out
832 self.receipts[tx_hash] = self.receipt
837 def read_alias(self, alias):
838 # this might not be the right place for this function.
841 m1 = re.match('([\w\-\.]+)@((\w[\w\-]+\.)+[\w\-]+)', alias)
842 m2 = re.match('((\w[\w\-]+\.)+[\w\-]+)', alias)
844 url = 'https://' + m1.group(2) + '/bitcoin.id/' + m1.group(1)
846 url = 'https://' + alias + '/bitcoin.id'
850 lines = urllib.urlopen(url).readlines()
855 line = lines[0].strip().split(':')
858 target = signing_addr = line[0]
860 target, auth_name, signing_addr, signature = line
861 msg = "alias:%s:%s:%s"%(alias,target,auth_name)
863 self.verify_message(signing_addr, signature, msg)
865 # other lines are signed updates
866 for line in lines[1:]:
868 if not line: continue
869 line = line.split(':')
872 target, signature = line
873 self.verify_message(previous, signature, "alias:%s:%s"%(alias,target))
875 if not self.is_valid(target):
876 raise ValueError("Invalid bitcoin address")
878 return target, signing_addr, auth_name
880 def update_password(self, seed, old_password, new_password):
881 if new_password == '': new_password = None
882 self.use_encryption = (new_password != None)
883 self.seed = self.pw_encode( seed, new_password)
884 for k in self.imported_keys.keys():
885 a = self.imported_keys[k]
886 b = self.pw_decode(a, old_password)
887 c = self.pw_encode(b, new_password)
888 self.imported_keys[k] = c
891 def get_alias(self, alias, interactive = False, show_message=None, question = None):
893 target, signing_address, auth_name = self.read_alias(alias)
894 except BaseException, e:
895 # raise exception if verify fails (verify the chain)
897 show_message("Alias error: " + str(e))
900 print target, signing_address, auth_name
902 if auth_name is None:
903 a = self.aliases.get(alias)
905 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)
906 if interactive and question( msg ):
907 self.aliases[alias] = (signing_address, target)
911 if signing_address != a[0]:
912 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
913 if interactive and question( msg ):
914 self.aliases[alias] = (signing_address, target)
918 if signing_address not in self.authorities.keys():
919 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)
920 if interactive and question( msg ):
921 self.authorities[signing_address] = auth_name
926 self.aliases[alias] = (signing_address, target)
931 def parse_url(self, url, show_message, question):
932 o = url[8:].split('?')
935 params = o[1].split('&')
939 amount = label = message = signature = identity = ''
943 if k == 'amount': amount = uv
944 elif k == 'message': message = uv
945 elif k == 'label': label = uv
946 elif k == 'signature':
947 identity, signature = uv.split(':')
948 url = url.replace('&%s=%s'%(k,v),'')
952 if label and self.labels.get(address) != label:
953 if question('Give label "%s" to address %s ?'%(label,address)):
954 if address not in self.addressbook and address not in self.all_addresses():
955 self.addressbook.append(address)
956 self.labels[address] = label
959 if re.match('^(|([\w\-\.]+)@)((\w[\w\-]+\.)+[\w\-]+)$', identity):
960 signing_address = self.get_alias(identity, True, show_message, question)
961 elif self.is_valid(identity):
962 signing_address = identity
964 signing_address = None
965 if not signing_address:
968 self.verify_message(signing_address, signature, url )
969 self.receipt = (signing_address, signature, url)
971 show_message('Warning: the URI contains a bad signature.\nThe identity of the recipient cannot be verified.')
972 address = amount = label = identity = message = ''
974 if re.match('^(|([\w\-\.]+)@)((\w[\w\-]+\.)+[\w\-]+)$', address):
975 payto_address = self.get_alias(address, True, show_message, question)
977 address = address + ' <' + payto_address + '>'
979 return address, amount, label, message, signature, identity, url
983 def freeze(self,addr):
984 if addr in self.all_addresses() and addr not in self.frozen_addresses:
985 self.unprioritize(addr)
986 self.frozen_addresses.append(addr)
987 self.config.set_key('frozen_addresses', self.frozen_addresses, True)
992 def unfreeze(self,addr):
993 if addr in self.all_addresses() and addr in self.frozen_addresses:
994 self.frozen_addresses.remove(addr)
995 self.config.set_key('frozen_addresses', self.frozen_addresses, True)
1000 def prioritize(self,addr):
1001 if addr in self.all_addresses() and addr not in self.prioritized_addresses:
1003 self.prioritized_addresses.append(addr)
1004 self.config.set_key('prioritized_addresses', self.prioritized_addresses, True)
1009 def unprioritize(self,addr):
1010 if addr in self.all_addresses() and addr in self.prioritized_addresses:
1011 self.prioritized_addresses.remove(addr)
1012 self.config.set_key('prioritized_addresses', self.prioritized_addresses, True)
1019 'seed_version': self.seed_version,
1020 'use_encryption': self.use_encryption,
1021 'use_change': self.use_change,
1022 'master_public_key': self.master_public_key,
1025 'addresses': self.addresses,
1026 'change_addresses': self.change_addresses,
1027 'addr_history': self.history,
1028 'labels': self.labels,
1029 'contacts': self.addressbook,
1030 'imported_keys': self.imported_keys,
1031 'aliases': self.aliases,
1032 'authorities': self.authorities,
1033 'receipts': self.receipts,
1034 'num_zeros': self.num_zeros,
1035 'frozen_addresses': self.frozen_addresses,
1036 'prioritized_addresses': self.prioritized_addresses,
1037 'gap_limit': self.gap_limit,
1038 'transactions': self.transactions,
1039 'requested_amounts': self.requested_amounts,
1041 for k, v in s.items():
1042 self.config.set_key(k,v)
1045 def set_verifier(self, verifier):
1046 self.verifier = verifier
1048 # review stored transactions and send them to the verifier
1049 # (they are not necessarily in the history, because history items might have have been pruned)
1050 for tx_hash, tx in self.transactions.items():
1051 tx_height = tx.get('height')
1053 print_error( "skipping", tx_hash, tx_height )
1057 self.verifier.add(tx_hash, tx_height)
1059 # review transactions that are in the history
1060 for addr, hist in self.history.items():
1061 if hist == ['*']: continue
1062 for tx_hash, tx_height in hist:
1064 # add it in case it was previously unconfirmed
1065 self.verifier.add(tx_hash, tx_height)
1066 # set the height in case it changed
1067 tx = self.transactions.get(tx_hash)
1069 if tx.get('height') != tx_height:
1070 print_error( "changing height for tx", tx_hash )
1071 tx['height'] = tx_height
1074 def is_addr_in_tx(self, addr, tx):
1076 for txin in tx.get('inputs'):
1077 if addr == txin.get('address'):
1080 for txout in tx.get('outputs'):
1081 if addr == txout.get('address'):
1087 def check_new_history(self, addr, hist):
1089 # check that all tx in hist are relevant
1091 for tx_hash, height in hist:
1092 tx = self.transactions.get(tx_hash)
1094 if not self.is_addr_in_tx(addr,tx):
1097 # check that we are not "orphaning" a transaction
1098 old_hist = self.history.get(addr,[])
1099 if old_hist == ['*']: return True
1101 for tx_hash, height in old_hist:
1102 if tx_hash in map(lambda x:x[0], hist): continue
1104 for _addr, _hist in self.history.items():
1105 if _addr == addr: continue
1106 if _hist == ['*']: continue
1107 _tx_hist = map(lambda x:x[0], _hist)
1108 if tx_hash in _tx_hist:
1113 tx = self.transactions.get(tx_hash)
1115 if tx.get('height'):
1118 print_error("new history is orphaning transaction:", tx_hash)
1119 # check that all outputs are not mine, request histories
1121 for o in tx.get('outputs'):
1122 _addr = o.get('address')
1123 # assert not self.is_mine(_addr)
1124 ext_requests.append( ('blockchain.address.get_history', [_addr]) )
1126 ext_h = self.interface.synchronous_get(ext_requests)
1129 if h == ['*']: continue
1131 if item.get('tx_hash') == tx_hash:
1132 height = item.get('height')
1133 tx['height'] = height
1135 print_error("found height for", tx_hash, height)
1136 self.verifier.add(tx_hash, height)
1138 print_error("removing orphaned tx from history", tx_hash)
1139 self.transactions.pop(tx_hash)
1145 def check_new_tx(self, tx_hash, tx):
1146 # 1 check that tx is referenced in addr_history.
1148 for addr, hist in self.history.items():
1149 if hist == ['*']:continue
1150 for txh, height in hist:
1152 addresses.append(addr)
1157 # 2 check that referencing addresses are in the tx
1158 for addr in addresses:
1159 if not self.is_addr_in_tx(addr, tx):
1167 class WalletSynchronizer(threading.Thread):
1170 def __init__(self, wallet, config):
1171 threading.Thread.__init__(self)
1173 self.wallet = wallet
1174 self.interface = self.wallet.interface
1175 self.interface.register_channel('synchronizer')
1176 self.wallet.interface.register_callback('connected', lambda: self.wallet.set_up_to_date(False))
1177 self.wallet.interface.register_callback('connected', lambda: self.interface.send([('server.banner',[])],'synchronizer') )
1178 self.was_updated = True
1179 self.running = False
1180 self.lock = threading.Lock()
1183 with self.lock: self.running = False
1184 self.interface.poke('synchronizer')
1186 def is_running(self):
1187 with self.lock: return self.running
1189 def synchronize_wallet(self):
1190 new_addresses = self.wallet.synchronize()
1192 self.subscribe_to_addresses(new_addresses)
1193 self.wallet.up_to_date = False
1196 if not self.interface.is_up_to_date('synchronizer'):
1197 if self.wallet.is_up_to_date():
1198 self.wallet.set_up_to_date(False)
1199 self.was_updated = True
1202 self.wallet.set_up_to_date(True)
1203 self.was_updated = True
1206 def subscribe_to_addresses(self, addresses):
1208 for addr in addresses:
1209 messages.append(('blockchain.address.subscribe', [addr]))
1210 self.interface.send( messages, 'synchronizer')
1214 with self.lock: self.running = True
1218 requested_histories = {}
1220 # request any missing transactions
1221 for history in self.wallet.history.values():
1222 if history == ['*']: continue
1223 for tx_hash, tx_height in history:
1224 if self.wallet.transactions.get(tx_hash) is None and (tx_hash, tx_height) not in missing_tx:
1225 missing_tx.append( (tx_hash, tx_height) )
1226 print_error("missing tx", missing_tx)
1228 # wait until we are connected, in case the user is not connected
1229 while not self.interface.is_connected:
1232 # request banner, because 'connected' event happens before this thread is started
1233 self.interface.send([('server.banner',[])],'synchronizer')
1236 self.subscribe_to_addresses(self.wallet.all_addresses())
1238 while self.is_running():
1239 # 1. send new requests
1240 self.synchronize_wallet()
1242 for tx_hash, tx_height in missing_tx:
1243 if (tx_hash, tx_height) not in requested_tx:
1244 self.interface.send([ ('blockchain.transaction.get',[tx_hash, tx_height]) ], 'synchronizer')
1245 requested_tx.append( (tx_hash, tx_height) )
1248 if self.was_updated:
1249 self.interface.trigger_callback('updated')
1250 self.was_updated = False
1253 r = self.interface.get_response('synchronizer')
1255 # poke sends None. (needed during stop)
1258 # 3. handle response
1259 method = r['method']
1260 params = r['params']
1261 result = r.get('result')
1262 error = r.get('error')
1267 if method == 'blockchain.address.subscribe':
1269 if self.wallet.get_status(self.wallet.get_history(addr)) != result:
1270 if requested_histories.get(addr) is None:
1271 self.interface.send([('blockchain.address.get_history', [addr])], 'synchronizer')
1272 requested_histories[addr] = result
1274 elif method == 'blockchain.address.get_history':
1276 print_error("receiving history", addr, result)
1278 assert requested_histories.pop(addr) == '*'
1279 self.wallet.receive_history_callback(addr, result)
1282 # check that txids are unique
1285 tx_hash = item['tx_hash']
1286 if tx_hash not in txids:
1287 txids.append(tx_hash)
1288 hist.append( (tx_hash, item['height']) )
1290 if len(hist) != len(result):
1291 raise BaseException("error: server sent history with non-unique txid", result)
1293 # check that the status corresponds to what was announced
1294 rs = requested_histories.pop(addr)
1295 if self.wallet.get_status(hist) != rs:
1296 raise BaseException("error: status mismatch: %s"%addr)
1298 # store received history
1299 self.wallet.receive_history_callback(addr, hist)
1301 # request transactions that we don't have
1302 for tx_hash, tx_height in hist:
1303 if self.wallet.transactions.get(tx_hash) is None:
1304 if (tx_hash, tx_height) not in requested_tx and (tx_hash, tx_height) not in missing_tx:
1305 missing_tx.append( (tx_hash, tx_height) )
1307 elif method == 'blockchain.transaction.get':
1309 tx_height = params[1]
1310 d = self.deserialize_tx(tx_hash, tx_height, result)
1311 self.wallet.receive_tx_callback(tx_hash, d)
1312 self.was_updated = True
1313 requested_tx.remove( (tx_hash, tx_height) )
1314 print_error("received tx:", d)
1316 elif method == 'blockchain.transaction.broadcast':
1317 self.wallet.tx_result = result
1318 self.wallet.tx_event.set()
1320 elif method == 'server.banner':
1321 self.wallet.banner = result
1322 self.was_updated = True
1325 print_error("Error: Unknown message:" + method + ", " + repr(params) + ", " + repr(result) )
1327 if self.was_updated and not requested_tx:
1328 self.interface.trigger_callback('updated')
1329 self.was_updated = False
1332 def deserialize_tx(self, tx_hash, tx_height, raw_tx):
1334 assert tx_hash == hash_encode(Hash(raw_tx.decode('hex')))
1336 vds = deserialize.BCDataStream()
1337 vds.write(raw_tx.decode('hex'))
1338 d = deserialize.parse_Transaction(vds)
1339 d['height'] = tx_height
1340 d['tx_hash'] = tx_hash