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/>.
33 from util import print_msg, print_error, user_dir, format_satoshis
37 _ud = re.compile('%([0-9a-hA-H]{2})', re.MULTILINE)
38 urldecode = lambda x: _ud.sub(lambda m: chr(int(m.group(1), 16)), x)
41 EncodeAES = lambda secret, s: base64.b64encode(aes.encryptData(secret,s))
42 DecodeAES = lambda secret, e: aes.decryptData(secret, base64.b64decode(e))
44 def pw_encode(s, password):
46 secret = Hash(password)
47 return EncodeAES(secret, s)
51 def pw_decode(s, password):
52 if password is not None:
53 secret = Hash(password)
55 d = DecodeAES(secret, s)
57 raise BaseException('Invalid password')
66 from version import ELECTRUM_VERSION, SEED_VERSION
70 def __init__(self, config={}):
73 self.electrum_version = ELECTRUM_VERSION
74 self.gap_limit_for_change = 3 # constant
77 self.seed_version = config.get('seed_version', SEED_VERSION)
78 self.gap_limit = config.get('gap_limit', 5)
79 self.use_change = config.get('use_change',True)
80 self.fee = int(config.get('fee',100000))
81 self.num_zeros = int(config.get('num_zeros',0))
82 self.use_encryption = config.get('use_encryption', False)
83 self.seed = config.get('seed', '') # encrypted
84 self.labels = config.get('labels', {})
85 self.aliases = config.get('aliases', {}) # aliases for addresses
86 self.authorities = config.get('authorities', {}) # trusted addresses
87 self.frozen_addresses = config.get('frozen_addresses',[])
88 self.prioritized_addresses = config.get('prioritized_addresses',[])
89 self.receipts = config.get('receipts',{}) # signed URIs
90 self.addressbook = config.get('contacts', [])
91 self.imported_keys = config.get('imported_keys',{})
92 self.history = config.get('addr_history',{}) # address -> list(txid, height)
93 self.tx_height = config.get('tx_height',{})
94 self.accounts = config.get('accounts', {}) # this should not include public keys
97 self.sequences[0] = DeterministicSequence(self.config.get('master_public_key'))
99 if self.accounts.get(0) is None:
100 self.accounts[0] = { 0:[], 1:[], 'name':'Main account' }
102 self.transactions = {}
103 tx = config.get('transactions',{})
105 for k,v in tx.items(): self.transactions[k] = Transaction(v)
107 print_msg("Warning: Cannot deserialize transactions. skipping")
111 self.plugin_hooks = {}
114 self.prevout_values = {} # my own transaction outputs
115 self.spent_outputs = []
116 self.receipt = None # next receipt
122 # there is a difference between wallet.up_to_date and interface.is_up_to_date()
123 # interface.is_up_to_date() returns true when all requests have been answered and processed
124 # wallet.up_to_date is true when the wallet is synchronized (stronger requirement)
126 self.up_to_date = False
127 self.lock = threading.Lock()
128 self.tx_event = threading.Event()
130 if self.seed_version != SEED_VERSION:
131 raise ValueError("This wallet seed is deprecated. Please run upgrade.py for a diagnostic.")
133 for tx_hash in self.transactions.keys():
134 self.update_tx_outputs(tx_hash)
138 def set_hook(self, name, callback):
139 h = self.plugin_hooks.get(name, [])
141 self.plugin_hooks[name] = h
143 def unset_hook(self, name, callback):
144 h = self.plugin_hooks.get(name,[])
145 if callback in h: h.remove(callback)
146 self.plugin_hooks[name] = h
148 def run_hook(self, name, args):
149 for cb in self.plugin_hooks.get(name,[]):
152 def init_plugins(self, plugins):
153 self.plugins = plugins
159 print_msg("Error:cannot initialize plugin",p)
160 traceback.print_exc(file=sys.stdout)
163 def set_up_to_date(self,b):
164 with self.lock: self.up_to_date = b
166 def is_up_to_date(self):
167 with self.lock: return self.up_to_date
170 self.up_to_date = False
171 self.interface.poke('synchronizer')
172 while not self.is_up_to_date(): time.sleep(0.1)
174 def import_key(self, sec, password):
176 seed = self.decode_seed(password)
177 address = address_from_private_key(sec)
179 if self.is_mine(address):
180 raise BaseException('Address already in wallet')
182 # store the originally requested keypair into the imported keys table
183 self.imported_keys[address] = pw_encode(sec, password )
187 def init_seed(self, seed):
188 if self.seed: raise BaseException("a seed exists")
190 seed = random_seed(128)
192 self.config.set_key('seed', self.seed, True)
193 self.config.set_key('seed_version', self.seed_version, True)
195 mpk = DeterministicSequence.mpk_from_seed(self.seed)
196 self.config.set_key('master_public_key', mpk, True)
197 self.sequences[0] = DeterministicSequence(mpk)
199 self.accounts[0] = { 0:[], 1:[], 'name':'Main account' }
200 self.config.set_key('accounts', self.accounts, True)
204 def addresses(self, include_change = False):
205 o = self.imported_keys.keys()
206 for a in self.accounts.values():
208 if include_change: o += a[1]
212 def is_mine(self, address):
213 return address in self.addresses(True)
215 def is_change(self, address):
216 #return address in self.change_addresses
219 def get_master_public_key(self):
220 return self.sequence.master_public_key
222 def get_address_index(self, address):
223 if address in self.imported_keys.keys():
224 raise BaseException("imported key")
225 for account in self.accounts.keys():
226 for for_change in [0,1]:
227 addresses = self.accounts[account][for_change]
228 for addr in addresses:
230 return account, (for_change, addresses.index(addr))
231 raise BaseException("not found")
234 def get_public_key(self, address):
235 account, sequence = self.get_address_index(address)
236 return self.sequences[account].get_pubkey( sequence )
239 def decode_seed(self, password):
240 seed = pw_decode(self.seed, password)
241 self.sequences[0].check_seed(seed)
244 def get_private_key(self, address, password):
245 return self.get_private_keys([address], password).get(address)
247 def get_private_keys(self, addresses, password):
248 # decode seed in any case, in order to test the password
249 seed = self.decode_seed(password)
253 for address in addresses:
254 if address in self.imported_keys.keys():
255 out[address] = pw_decode( self.imported_keys[address], password )
257 account, sequence = self.get_address_index(address)
259 l_sequences.append(sequence)
260 l_addresses.append(address)
262 pk = self.sequences[0].get_private_keys(l_sequences, seed)
263 for i, address in enumerate(l_addresses): out[address] = pk[i]
267 def signrawtransaction(self, tx, input_info, private_keys, password):
268 unspent_coins = self.get_unspent_coins()
269 seed = self.decode_seed(password)
271 # convert private_keys to dict
273 for sec in private_keys:
274 address = address_from_private_key(sec)
278 for txin in tx.inputs:
279 # convert to own format
280 txin['tx_hash'] = txin['prevout_hash']
281 txin['index'] = txin['prevout_n']
283 for item in input_info:
284 if item.get('txid') == txin['tx_hash'] and item.get('vout') == txin['index']:
285 txin['raw_output_script'] = item['scriptPubKey']
286 txin['redeemScript'] = item.get('redeemScript')
287 txin['electrumKeyID'] = item.get('electrumKeyID')
290 for item in unspent_coins:
291 if txin['tx_hash'] == item['tx_hash'] and txin['index'] == item['index']:
292 txin['raw_output_script'] = item['raw_output_script']
295 # if neither, we might want to get it from the server..
299 if txin.get('electrumKeyID'):
300 account, sequence = txin.get('electrumKeyID')
301 sec = self.sequences[account].get_private_key(sequence, seed)
302 addr = self.sequences[account].get_address(sequence)
303 txin['address'] = addr
304 private_keys[addr] = sec
306 elif txin.get("redeemScript"):
307 txin['address'] = hash_160_to_bc_address(hash_160(txin.get("redeemScript").decode('hex')), 5)
309 elif txin.get("raw_output_script"):
311 addr = deserialize.get_address_from_output_script(txin.get("raw_output_script").decode('hex'))
312 sec = self.get_private_key(addr, password)
314 private_keys[addr] = sec
315 txin['address'] = addr
317 tx.sign( private_keys )
319 def sign_message(self, address, message, password):
320 sec = self.get_private_key(address, password)
321 key = regenerate_key(sec)
322 compressed = is_compressed(sec)
323 return key.sign_message(message, compressed, address)
326 def create_new_address(self, account, for_change):
327 addresses = self.accounts[account][for_change]
329 address = self.get_new_address( account, for_change, n)
330 self.accounts[account][for_change].append(address)
331 self.history[address] = []
335 def get_new_address(self, account, for_change, n):
336 return self.sequences[account].get_address((for_change, n))
340 def change_gap_limit(self, value):
341 if value >= self.gap_limit:
342 self.gap_limit = value
344 self.interface.poke('synchronizer')
347 elif value >= self.min_acceptable_gap():
348 for key, account in self.accounts.items():
349 addresses = account[0]
350 k = self.num_unused_trailing_addresses(addresses)
351 n = len(addresses) - k + value
352 addresses = addresses[0:n]
353 self.accounts[key][0] = addresses
355 self.gap_limit = value
361 def num_unused_trailing_addresses(self, addresses):
363 for a in addresses[::-1]:
364 if self.history.get(a):break
368 def min_acceptable_gap(self):
369 # fixme: this assumes wallet is synchronized
373 for account in self.accounts.values():
374 addresses = account[0]
375 k = self.num_unused_trailing_addresses(addresses)
376 for a in addresses[0:-k]:
377 if self.history.get(a):
381 if n > nmax: nmax = n
385 def address_is_old(self, address):
387 h = self.history.get(address, [])
390 for tx_hash, tx_height in h:
394 tx_age = self.verifier.height - tx_height + 1
400 def synchronize_sequence(self, account, for_change):
401 limit = self.gap_limit_for_change if for_change else self.gap_limit
402 addresses = self.accounts[account][for_change]
405 if len(addresses) < limit:
406 new_addresses.append( self.create_new_address(account, for_change) )
408 if map( lambda a: self.address_is_old(a), addresses[-limit:] ) == limit*[False]:
411 new_addresses.append( self.create_new_address(account, for_change) )
415 def synchronize_account(self, account):
417 new += self.synchronize_sequence(account, 0)
418 new += self.synchronize_sequence(account, 1)
421 def synchronize(self):
423 for account in self.accounts.keys():
424 new += self.synchronize_account(account)
429 return self.history.values() != [[]] * len(self.history)
432 def fill_addressbook(self):
433 for tx_hash, tx in self.transactions.items():
434 is_send, _, _ = self.get_tx_value(tx)
436 for addr, v in tx.outputs:
437 if not self.is_mine(addr) and addr not in self.addressbook:
438 self.addressbook.append(addr)
440 # self.update_tx_labels()
443 def get_address_flags(self, addr):
444 flags = "C" if self.is_change(addr) else "I" if addr in self.imported_keys.keys() else "-"
445 flags += "F" if addr in self.frozen_addresses else "P" if addr in self.prioritized_addresses else "-"
449 def get_tx_value(self, tx, addresses=None):
450 if addresses is None: addresses = self.addresses(True)
451 return tx.get_value(addresses, self.prevout_values)
454 def get_tx_details(self, tx_hash):
456 if not tx_hash: return ''
457 tx = self.transactions.get(tx_hash)
458 is_mine, v, fee = self.get_tx_value(tx)
459 conf, timestamp = self.verifier.get_confirmations(tx_hash)
462 time_str = datetime.datetime.fromtimestamp(timestamp).isoformat(' ')[:-3]
466 inputs = map(lambda x: x.get('address'), tx.inputs)
467 outputs = map(lambda x: x.get('address'), tx.d['outputs'])
468 tx_details = "Transaction Details" +"\n\n" \
469 + "Transaction ID:\n" + tx_hash + "\n\n" \
470 + "Status: %d confirmations\n"%conf
473 tx_details += "Amount sent: %s\n"% format_satoshis(v-fee, False) \
474 + "Transaction fee: %s\n"% format_satoshis(fee, False)
476 tx_details += "Amount sent: %s\n"% format_satoshis(v, False) \
477 + "Transaction fee: unknown\n"
479 tx_details += "Amount received: %s\n"% format_satoshis(v, False) \
481 tx_details += "Date: %s\n\n"%time_str \
482 + "Inputs:\n-"+ '\n-'.join(inputs) + "\n\n" \
483 + "Outputs:\n-"+ '\n-'.join(outputs)
485 r = self.receipts.get(tx_hash)
487 tx_details += "\n_______________________________________" \
488 + '\n\nSigned URI: ' + r[2] \
489 + "\n\nSigned by: " + r[0] \
490 + '\n\nSignature: ' + r[1]
495 def update_tx_outputs(self, tx_hash):
496 tx = self.transactions.get(tx_hash)
498 for item in tx.outputs:
500 key = tx_hash+ ':%d'%i
502 self.prevout_values[key] = value
505 for item in tx.inputs:
506 if self.is_mine(item.get('address')):
507 key = item['prevout_hash'] + ':%d'%item['prevout_n']
508 self.spent_outputs.append(key)
511 def get_addr_balance(self, address):
512 assert self.is_mine(address)
513 h = self.history.get(address,[])
514 if h == ['*']: return 0,0
516 received_coins = [] # list of coins received at address
518 for tx_hash, tx_height in h:
519 tx = self.transactions.get(tx_hash)
522 for item in tx.outputs:
525 key = tx_hash + ':%d'%i
526 received_coins.append(key)
529 for tx_hash, tx_height in h:
530 tx = self.transactions.get(tx_hash)
534 for item in tx.inputs:
535 addr = item.get('address')
537 key = item['prevout_hash'] + ':%d'%item['prevout_n']
538 value = self.prevout_values.get( key )
539 if key in received_coins:
543 for item in tx.outputs:
545 key = tx_hash + ':%d'%i
556 def get_account_addresses(self, a):
557 ac = self.accounts[a]
560 def get_imported_balance(self):
562 for addr in self.imported_keys.keys():
563 c, u = self.get_addr_balance(addr)
568 def get_account_balance(self, account):
570 for addr in self.get_account_addresses(account):
571 c, u = self.get_addr_balance(addr)
576 def get_balance(self):
578 for a in self.accounts.keys():
579 c, u = self.get_account_balance(a)
582 c, u = self.get_imported_balance()
588 def get_unspent_coins(self, domain=None):
590 if domain is None: domain = self.addresses(True)
592 h = self.history.get(addr, [])
593 if h == ['*']: continue
594 for tx_hash, tx_height in h:
595 tx = self.transactions.get(tx_hash)
596 for output in tx.d.get('outputs'):
597 if output.get('address') != addr: continue
598 key = tx_hash + ":%d" % output.get('index')
599 if key in self.spent_outputs: continue
600 output['tx_hash'] = tx_hash
606 def choose_tx_inputs( self, amount, fixed_fee, from_addr = None ):
607 """ todo: minimize tx size """
609 fee = self.fee if fixed_fee is None else fixed_fee
612 prioritized_coins = []
613 domain = [from_addr] if from_addr else self.addresses(True)
614 for i in self.frozen_addresses:
615 if i in domain: domain.remove(i)
617 for i in self.prioritized_addresses:
618 if i in domain: domain.remove(i)
620 coins = self.get_unspent_coins(domain)
621 prioritized_coins = self.get_unspent_coins(self.prioritized_addresses)
624 coins = prioritized_coins + coins
627 addr = item.get('address')
628 v = item.get('value')
631 inputs.append( item )
632 fee = self.fee*len(inputs) if fixed_fee is None else fixed_fee
633 if total >= amount + fee: break
635 #print "not enough funds: %s %s"%(format_satoshis(total), format_satoshis(fee))
638 return inputs, total, fee
640 def add_tx_change( self, outputs, amount, fee, total, change_addr=None ):
641 change_amount = total - ( amount + fee )
642 if change_amount != 0:
643 # normally, the update thread should ensure that the last change address is unused
645 change_addresses = self.accounts[0][1]
646 change_addr = change_addresses[-self.gap_limit_for_change]
647 # Insert the change output at a random position in the outputs
648 posn = random.randint(0, len(outputs))
649 outputs[posn:posn] = [( change_addr, change_amount)]
653 def get_history(self, address):
655 return self.history.get(address)
657 def get_status(self, h):
658 if not h: return None
659 if h == ['*']: return '*'
661 for tx_hash, height in h:
662 status += tx_hash + ':%d:' % height
663 return hashlib.sha256( status ).digest().encode('hex')
667 def receive_tx_callback(self, tx_hash, tx, tx_height):
669 if not self.check_new_tx(tx_hash, tx):
670 raise BaseException("error: received transaction is not consistent with history"%tx_hash)
673 self.transactions[tx_hash] = tx
674 self.tx_height[tx_hash] = tx_height
676 #tx_height = tx.get('height')
677 if self.verifier and tx_height>0:
678 self.verifier.add(tx_hash, tx_height)
680 self.update_tx_outputs(tx_hash)
685 def receive_history_callback(self, addr, hist):
687 if not self.check_new_history(addr, hist):
688 raise BaseException("error: received history for %s is not consistent with known transactions"%addr)
691 self.history[addr] = hist
695 for tx_hash, tx_height in hist:
697 # add it in case it was previously unconfirmed
698 if self.verifier: self.verifier.add(tx_hash, tx_height)
699 # set the height in case it changed
700 txh = self.tx_height.get(tx_hash)
701 if txh is not None and txh != tx_height:
702 print_error( "changing height for tx", tx_hash )
703 self.tx_height[tx_hash] = tx_height
706 def get_tx_history(self):
708 history = self.transactions.items()
709 history.sort(key = lambda x: self.tx_height.get(x[0]) if self.tx_height.get(x[0]) else 1e12)
713 for tx_hash, tx in history:
714 is_mine, v, fee = self.get_tx_value(tx)
715 if v is not None: balance += v
716 c, u = self.get_balance()
719 v_str = format_satoshis( c+u - balance, True, self.num_zeros)
720 result.append( ('', 1000, 0, c+u-balance, None, c+u-balance, None ) )
722 balance = c + u - balance
723 for tx_hash, tx in history:
724 conf, timestamp = self.verifier.get_confirmations(tx_hash) if self.verifier else (None, None)
725 is_mine, value, fee = self.get_tx_value(tx)
726 if value is not None:
729 result.append( (tx_hash, conf, is_mine, value, fee, balance, timestamp) )
734 def get_label(self, tx_hash):
735 label = self.labels.get(tx_hash)
736 is_default = (label == '') or (label is None)
737 if is_default: label = self.get_default_label(tx_hash)
738 return label, is_default
741 def get_default_label(self, tx_hash):
742 tx = self.transactions.get(tx_hash)
745 is_mine, _, _ = self.get_tx_value(tx)
749 if not self.is_mine(o_addr):
751 default_label = self.labels[o_addr]
753 default_label = o_addr
757 if self.is_mine(o_addr) and not self.is_change(o_addr):
762 if self.is_mine(o_addr):
768 dest_label = self.labels.get(o_addr)
770 default_label = self.labels[o_addr]
772 default_label = o_addr
777 def mktx(self, outputs, password, fee=None, change_addr=None, from_addr= None):
779 for address, x in outputs:
780 assert is_valid(address)
782 amount = sum( map(lambda x:x[1], outputs) )
783 inputs, total, fee = self.choose_tx_inputs( amount, fee, from_addr )
785 raise ValueError("Not enough funds")
787 if not self.use_change and not change_addr:
788 change_addr = inputs[-1]['address']
789 print_error( "Sending change to", change_addr )
790 outputs = self.add_tx_change(outputs, amount, fee, total, change_addr)
792 tx = Transaction.from_io(inputs, outputs)
795 for i in range(len(tx.inputs)):
797 address = txin['address']
798 if address in self.imported_keys.keys():
799 pk_addresses.append(address)
801 account, sequence = self.get_address_index(address)
802 txin['electrumKeyID'] = (account, sequence) # used by the server to find the key
803 pk_addr, redeemScript = self.sequences[account].get_input_info(sequence)
804 if redeemScript: txin['redeemScript'] = redeemScript
805 pk_addresses.append(pk_addr)
807 # get all private keys at once.
808 private_keys = self.get_private_keys(pk_addresses, password)
809 tx.sign(private_keys)
811 for address, x in outputs:
812 if address not in self.addressbook and not self.is_mine(address):
813 self.addressbook.append(address)
819 def sendtx(self, tx):
823 return self.receive_tx(h)
825 def send_tx(self, tx):
827 self.tx_event.clear()
828 self.interface.send([('blockchain.transaction.broadcast', [str(tx)])], 'synchronizer')
831 def receive_tx(self,tx_hash):
834 return False, "error: " + out
836 self.receipts[tx_hash] = self.receipt
841 def read_alias(self, alias):
842 # this might not be the right place for this function.
845 m1 = re.match('([\w\-\.]+)@((\w[\w\-]+\.)+[\w\-]+)', alias)
846 m2 = re.match('((\w[\w\-]+\.)+[\w\-]+)', alias)
848 url = 'https://' + m1.group(2) + '/bitcoin.id/' + m1.group(1)
850 url = 'https://' + alias + '/bitcoin.id'
854 lines = urllib.urlopen(url).readlines()
859 line = lines[0].strip().split(':')
862 target = signing_addr = line[0]
864 target, auth_name, signing_addr, signature = line
865 msg = "alias:%s:%s:%s"%(alias,target,auth_name)
867 EC_KEY.verify_message(signing_addr, signature, msg)
869 # other lines are signed updates
870 for line in lines[1:]:
872 if not line: continue
873 line = line.split(':')
876 target, signature = line
877 EC_KEY.verify_message(previous, signature, "alias:%s:%s"%(alias,target))
879 if not is_valid(target):
880 raise ValueError("Invalid bitcoin address")
882 return target, signing_addr, auth_name
884 def update_password(self, seed, old_password, new_password):
885 if new_password == '': new_password = None
886 self.use_encryption = (new_password != None)
887 self.seed = pw_encode( seed, new_password)
888 self.config.set_key('seed', self.seed, True)
889 for k in self.imported_keys.keys():
890 a = self.imported_keys[k]
891 b = pw_decode(a, old_password)
892 c = pw_encode(b, new_password)
893 self.imported_keys[k] = c
896 def get_alias(self, alias, interactive = False, show_message=None, question = None):
898 target, signing_address, auth_name = self.read_alias(alias)
899 except BaseException, e:
900 # raise exception if verify fails (verify the chain)
902 show_message("Alias error: " + str(e))
905 print target, signing_address, auth_name
907 if auth_name is None:
908 a = self.aliases.get(alias)
910 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)
911 if interactive and question( msg ):
912 self.aliases[alias] = (signing_address, target)
916 if signing_address != a[0]:
917 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
918 if interactive and question( msg ):
919 self.aliases[alias] = (signing_address, target)
923 if signing_address not in self.authorities.keys():
924 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)
925 if interactive and question( msg ):
926 self.authorities[signing_address] = auth_name
931 self.aliases[alias] = (signing_address, target)
936 def parse_url(self, url, show_message, question):
937 o = url[8:].split('?')
940 params = o[1].split('&')
944 amount = label = message = signature = identity = ''
948 if k == 'amount': amount = uv
949 elif k == 'message': message = uv
950 elif k == 'label': label = uv
951 elif k == 'signature':
952 identity, signature = uv.split(':')
953 url = url.replace('&%s=%s'%(k,v),'')
957 if label and self.labels.get(address) != label:
958 if question('Give label "%s" to address %s ?'%(label,address)):
959 if address not in self.addressbook and not self.is_mine(address):
960 self.addressbook.append(address)
961 self.labels[address] = label
964 if re.match('^(|([\w\-\.]+)@)((\w[\w\-]+\.)+[\w\-]+)$', identity):
965 signing_address = self.get_alias(identity, True, show_message, question)
966 elif is_valid(identity):
967 signing_address = identity
969 signing_address = None
970 if not signing_address:
973 EC_KEY.verify_message(signing_address, signature, url )
974 self.receipt = (signing_address, signature, url)
976 show_message('Warning: the URI contains a bad signature.\nThe identity of the recipient cannot be verified.')
977 address = amount = label = identity = message = ''
979 if re.match('^(|([\w\-\.]+)@)((\w[\w\-]+\.)+[\w\-]+)$', address):
980 payto_address = self.get_alias(address, True, show_message, question)
982 address = address + ' <' + payto_address + '>'
984 return address, amount, label, message, signature, identity, url
988 def freeze(self,addr):
989 if self.is_mine(addr) and addr not in self.frozen_addresses:
990 self.unprioritize(addr)
991 self.frozen_addresses.append(addr)
992 self.config.set_key('frozen_addresses', self.frozen_addresses, True)
997 def unfreeze(self,addr):
998 if self.is_mine(addr) and addr in self.frozen_addresses:
999 self.frozen_addresses.remove(addr)
1000 self.config.set_key('frozen_addresses', self.frozen_addresses, True)
1005 def prioritize(self,addr):
1006 if is_mine(addr) and addr not in self.prioritized_addresses:
1008 self.prioritized_addresses.append(addr)
1009 self.config.set_key('prioritized_addresses', self.prioritized_addresses, True)
1014 def unprioritize(self,addr):
1015 if is_mine(addr) and addr in self.prioritized_addresses:
1016 self.prioritized_addresses.remove(addr)
1017 self.config.set_key('prioritized_addresses', self.prioritized_addresses, True)
1024 for k,v in self.transactions.items():
1028 'use_encryption': self.use_encryption,
1029 'use_change': self.use_change,
1031 'accounts': self.accounts,
1032 'addr_history': self.history,
1033 'labels': self.labels,
1034 'contacts': self.addressbook,
1035 'imported_keys': self.imported_keys,
1036 'aliases': self.aliases,
1037 'authorities': self.authorities,
1038 'receipts': self.receipts,
1039 'num_zeros': self.num_zeros,
1040 'frozen_addresses': self.frozen_addresses,
1041 'prioritized_addresses': self.prioritized_addresses,
1042 'gap_limit': self.gap_limit,
1044 'tx_height': self.tx_height,
1046 for k, v in s.items():
1047 self.config.set_key(k,v)
1050 def set_verifier(self, verifier):
1051 self.verifier = verifier
1053 # review stored transactions and send them to the verifier
1054 # (they are not necessarily in the history, because history items might have have been pruned)
1055 for tx_hash, tx in self.transactions.items():
1056 tx_height = self.tx_height[tx_hash]
1058 print_error( "skipping", tx_hash, tx_height )
1062 self.verifier.add(tx_hash, tx_height)
1064 # review transactions that are in the history
1065 for addr, hist in self.history.items():
1066 if hist == ['*']: continue
1067 for tx_hash, tx_height in hist:
1069 # add it in case it was previously unconfirmed
1070 self.verifier.add(tx_hash, tx_height)
1071 # set the height in case it changed
1072 txh = self.tx_height.get(tx_hash)
1073 if txh is not None and txh != tx_height:
1074 print_error( "changing height for tx", tx_hash )
1075 self.tx_height[tx_hash] = tx_height
1080 def check_new_history(self, addr, hist):
1082 # check that all tx in hist are relevant
1084 for tx_hash, height in hist:
1085 tx = self.transactions.get(tx_hash)
1087 if not tx.has_address(addr):
1090 # check that we are not "orphaning" a transaction
1091 old_hist = self.history.get(addr,[])
1092 if old_hist == ['*']: return True
1094 for tx_hash, height in old_hist:
1095 if tx_hash in map(lambda x:x[0], hist): continue
1097 for _addr, _hist in self.history.items():
1098 if _addr == addr: continue
1099 if _hist == ['*']: continue
1100 _tx_hist = map(lambda x:x[0], _hist)
1101 if tx_hash in _tx_hist:
1106 tx = self.transactions.get(tx_hash)
1107 # tx might not be there
1111 if self.tx_height.get(tx_hash):
1114 print_error("new history is orphaning transaction:", tx_hash)
1115 # check that all outputs are not mine, request histories
1117 for o in tx.get('outputs'):
1118 _addr = o.get('address')
1119 # assert not self.is_mine(_addr)
1120 ext_requests.append( ('blockchain.address.get_history', [_addr]) )
1122 ext_h = self.interface.synchronous_get(ext_requests)
1125 if h == ['*']: continue
1127 if item.get('tx_hash') == tx_hash:
1128 height = item.get('height')
1129 self.tx_height[tx_hash] = height
1131 print_error("found height for", tx_hash, height)
1132 self.verifier.add(tx_hash, height)
1134 print_error("removing orphaned tx from history", tx_hash)
1135 self.transactions.pop(tx_hash)
1141 def check_new_tx(self, tx_hash, tx):
1142 # 1 check that tx is referenced in addr_history.
1144 for addr, hist in self.history.items():
1145 if hist == ['*']:continue
1146 for txh, height in hist:
1148 addresses.append(addr)
1153 # 2 check that referencing addresses are in the tx
1154 for addr in addresses:
1155 if not tx.has_address(addr):
1163 class WalletSynchronizer(threading.Thread):
1166 def __init__(self, wallet, config):
1167 threading.Thread.__init__(self)
1169 self.wallet = wallet
1170 self.interface = self.wallet.interface
1171 self.interface.register_channel('synchronizer')
1172 self.wallet.interface.register_callback('connected', lambda: self.wallet.set_up_to_date(False))
1173 self.wallet.interface.register_callback('connected', lambda: self.interface.send([('server.banner',[])],'synchronizer') )
1174 self.was_updated = True
1175 self.running = False
1176 self.lock = threading.Lock()
1179 with self.lock: self.running = False
1180 self.interface.poke('synchronizer')
1182 def is_running(self):
1183 with self.lock: return self.running
1185 def synchronize_wallet(self):
1186 new_addresses = self.wallet.synchronize()
1188 self.subscribe_to_addresses(new_addresses)
1189 self.wallet.up_to_date = False
1192 if not self.interface.is_up_to_date('synchronizer'):
1193 if self.wallet.is_up_to_date():
1194 self.wallet.set_up_to_date(False)
1195 self.was_updated = True
1198 self.wallet.set_up_to_date(True)
1199 self.was_updated = True
1202 def subscribe_to_addresses(self, addresses):
1204 for addr in addresses:
1205 messages.append(('blockchain.address.subscribe', [addr]))
1206 self.interface.send( messages, 'synchronizer')
1210 with self.lock: self.running = True
1214 requested_histories = {}
1216 # request any missing transactions
1217 for history in self.wallet.history.values():
1218 if history == ['*']: continue
1219 for tx_hash, tx_height in history:
1220 if self.wallet.transactions.get(tx_hash) is None and (tx_hash, tx_height) not in missing_tx:
1221 missing_tx.append( (tx_hash, tx_height) )
1222 print_error("missing tx", missing_tx)
1224 # wait until we are connected, in case the user is not connected
1225 while not self.interface.is_connected:
1228 # request banner, because 'connected' event happens before this thread is started
1229 self.interface.send([('server.banner',[])],'synchronizer')
1232 self.subscribe_to_addresses(self.wallet.addresses(True))
1234 while self.is_running():
1235 # 1. send new requests
1236 self.synchronize_wallet()
1238 for tx_hash, tx_height in missing_tx:
1239 if (tx_hash, tx_height) not in requested_tx:
1240 self.interface.send([ ('blockchain.transaction.get',[tx_hash, tx_height]) ], 'synchronizer')
1241 requested_tx.append( (tx_hash, tx_height) )
1244 if self.was_updated:
1245 self.interface.trigger_callback('updated')
1246 self.was_updated = False
1249 r = self.interface.get_response('synchronizer')
1251 # poke sends None. (needed during stop)
1254 # 3. handle response
1255 method = r['method']
1256 params = r['params']
1257 result = r.get('result')
1258 error = r.get('error')
1263 if method == 'blockchain.address.subscribe':
1265 if self.wallet.get_status(self.wallet.get_history(addr)) != result:
1266 if requested_histories.get(addr) is None:
1267 self.interface.send([('blockchain.address.get_history', [addr])], 'synchronizer')
1268 requested_histories[addr] = result
1270 elif method == 'blockchain.address.get_history':
1272 print_error("receiving history", addr, result)
1274 assert requested_histories.pop(addr) == '*'
1275 self.wallet.receive_history_callback(addr, result)
1278 # check that txids are unique
1281 tx_hash = item['tx_hash']
1282 if tx_hash not in txids:
1283 txids.append(tx_hash)
1284 hist.append( (tx_hash, item['height']) )
1286 if len(hist) != len(result):
1287 raise BaseException("error: server sent history with non-unique txid", result)
1289 # check that the status corresponds to what was announced
1290 rs = requested_histories.pop(addr)
1291 if self.wallet.get_status(hist) != rs:
1292 raise BaseException("error: status mismatch: %s"%addr)
1294 # store received history
1295 self.wallet.receive_history_callback(addr, hist)
1297 # request transactions that we don't have
1298 for tx_hash, tx_height in hist:
1299 if self.wallet.transactions.get(tx_hash) is None:
1300 if (tx_hash, tx_height) not in requested_tx and (tx_hash, tx_height) not in missing_tx:
1301 missing_tx.append( (tx_hash, tx_height) )
1303 elif method == 'blockchain.transaction.get':
1305 tx_height = params[1]
1306 assert tx_hash == hash_encode(Hash(result.decode('hex')))
1307 tx = Transaction(result)
1308 self.wallet.receive_tx_callback(tx_hash, tx, tx_height)
1309 self.was_updated = True
1310 requested_tx.remove( (tx_hash, tx_height) )
1311 print_error("received tx:", tx)
1313 elif method == 'blockchain.transaction.broadcast':
1314 self.wallet.tx_result = result
1315 self.wallet.tx_event.set()
1317 elif method == 'server.banner':
1318 self.wallet.banner = result
1319 self.interface.trigger_callback('banner')
1321 print_error("Error: Unknown message:" + method + ", " + repr(params) + ", " + repr(result) )
1323 if self.was_updated and not requested_tx:
1324 self.interface.trigger_callback('updated')
1325 self.was_updated = False