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
38 EncodeAES = lambda secret, s: base64.b64encode(aes.encryptData(secret,s))
39 DecodeAES = lambda secret, e: aes.decryptData(secret, base64.b64decode(e))
41 def pw_encode(s, password):
43 secret = Hash(password)
44 return EncodeAES(secret, s)
48 def pw_decode(s, password):
49 if password is not None:
50 secret = Hash(password)
52 d = DecodeAES(secret, s)
54 raise BaseException('Invalid password')
63 from version import ELECTRUM_VERSION, SEED_VERSION
67 def __init__(self, config={}):
70 self.electrum_version = ELECTRUM_VERSION
71 self.gap_limit_for_change = 3 # constant
74 self.seed_version = config.get('seed_version', SEED_VERSION)
75 self.gap_limit = config.get('gap_limit', 5)
76 self.use_change = config.get('use_change',True)
77 self.fee = int(config.get('fee_per_kb',20000))
78 self.num_zeros = int(config.get('num_zeros',0))
79 self.use_encryption = config.get('use_encryption', False)
80 self.seed = config.get('seed', '') # encrypted
81 self.labels = config.get('labels', {})
82 self.frozen_addresses = config.get('frozen_addresses',[])
83 self.prioritized_addresses = config.get('prioritized_addresses',[])
84 self.addressbook = config.get('contacts', [])
85 self.imported_keys = config.get('imported_keys',{})
86 self.history = config.get('addr_history',{}) # address -> list(txid, height)
87 self.accounts = config.get('accounts', {}) # this should not include public keys
89 self.SequenceClass = ElectrumSequence
91 self.sequences[0] = self.SequenceClass(self.config.get('master_public_key'))
93 if self.accounts.get(0) is None:
94 self.accounts[0] = { 0:[], 1:[], 'name':'Main account' }
96 self.transactions = {}
97 tx = config.get('transactions',{})
99 for k,v in tx.items(): self.transactions[k] = Transaction(v)
101 print_msg("Warning: Cannot deserialize transactions. skipping")
104 self.prevout_values = {} # my own transaction outputs
105 self.spent_outputs = []
110 # there is a difference between wallet.up_to_date and interface.is_up_to_date()
111 # interface.is_up_to_date() returns true when all requests have been answered and processed
112 # wallet.up_to_date is true when the wallet is synchronized (stronger requirement)
114 self.up_to_date = False
115 self.lock = threading.Lock()
116 self.transaction_lock = threading.Lock()
117 self.tx_event = threading.Event()
119 if self.seed_version != SEED_VERSION:
120 raise ValueError("This wallet seed is deprecated. Please run upgrade.py for a diagnostic.")
122 for tx_hash in self.transactions.keys():
123 self.update_tx_outputs(tx_hash)
126 def set_up_to_date(self,b):
127 with self.lock: self.up_to_date = b
129 def is_up_to_date(self):
130 with self.lock: return self.up_to_date
133 self.up_to_date = False
134 self.interface.poke('synchronizer')
135 while not self.is_up_to_date(): time.sleep(0.1)
137 def import_key(self, sec, password):
139 seed = self.decode_seed(password)
140 address = address_from_private_key(sec)
142 if self.is_mine(address):
143 raise BaseException('Address already in wallet')
145 # store the originally requested keypair into the imported keys table
146 self.imported_keys[address] = pw_encode(sec, password )
150 def init_seed(self, seed):
151 if self.seed: raise BaseException("a seed exists")
153 seed = random_seed(128)
155 self.config.set_key('seed', self.seed, True)
156 self.config.set_key('seed_version', self.seed_version, True)
157 mpk = self.SequenceClass.mpk_from_seed(self.seed)
158 self.init_sequence(mpk)
161 def init_sequence(self, mpk):
162 self.config.set_key('master_public_key', mpk, True)
163 self.sequences[0] = self.SequenceClass(mpk)
164 self.accounts[0] = { 0:[], 1:[], 'name':'Main account' }
165 self.config.set_key('accounts', self.accounts, True)
168 def addresses(self, include_change = True):
169 o = self.get_account_addresses(-1, include_change)
170 for a in self.accounts.keys():
171 o += self.get_account_addresses(a, include_change)
175 def is_mine(self, address):
176 return address in self.addresses(True)
178 def is_change(self, address):
179 if not self.is_mine(address): return False
180 if address in self.imported_keys.keys(): return False
181 acct, s = self.get_address_index(address)
184 def get_master_public_key(self):
185 return self.config.get("master_public_key")
187 def get_address_index(self, address):
188 if address in self.imported_keys.keys():
189 raise BaseException("imported key")
190 for account in self.accounts.keys():
191 for for_change in [0,1]:
192 addresses = self.accounts[account][for_change]
193 for addr in addresses:
195 return account, (for_change, addresses.index(addr))
196 raise BaseException("not found")
199 def get_public_key(self, address):
200 account, sequence = self.get_address_index(address)
201 return self.sequences[account].get_pubkey( sequence )
204 def decode_seed(self, password):
205 seed = pw_decode(self.seed, password)
206 self.sequences[0].check_seed(seed)
209 def get_private_key(self, address, password):
210 return self.get_private_keys([address], password).get(address)
212 def get_private_keys(self, addresses, password):
213 if not self.seed: return {}
214 # decode seed in any case, in order to test the password
215 seed = self.decode_seed(password)
219 for address in addresses:
220 if address in self.imported_keys.keys():
221 out[address] = pw_decode( self.imported_keys[address], password )
223 account, sequence = self.get_address_index(address)
225 l_sequences.append(sequence)
226 l_addresses.append(address)
228 pk = self.sequences[0].get_private_keys(l_sequences, seed)
229 for i, address in enumerate(l_addresses): out[address] = pk[i]
233 def signrawtransaction(self, tx, input_info, private_keys, password):
234 unspent_coins = self.get_unspent_coins()
235 seed = self.decode_seed(password)
237 # convert private_keys to dict
239 for sec in private_keys:
240 address = address_from_private_key(sec)
244 for txin in tx.inputs:
245 # convert to own format
246 txin['tx_hash'] = txin['prevout_hash']
247 txin['index'] = txin['prevout_n']
249 for item in input_info:
250 if item.get('txid') == txin['tx_hash'] and item.get('vout') == txin['index']:
251 txin['raw_output_script'] = item['scriptPubKey']
252 txin['redeemScript'] = item.get('redeemScript')
253 txin['KeyID'] = item.get('KeyID')
256 for item in unspent_coins:
257 if txin['tx_hash'] == item['tx_hash'] and txin['index'] == item['index']:
258 txin['raw_output_script'] = item['raw_output_script']
261 # if neither, we might want to get it from the server..
265 if txin.get('KeyID'):
266 account, name, sequence = txin.get('KeyID')
267 if name != 'Electrum': continue
268 sec = self.sequences[account].get_private_key(sequence, seed)
269 addr = self.sequences[account].get_address(sequence)
270 txin['address'] = addr
271 private_keys[addr] = sec
273 elif txin.get("redeemScript"):
274 txin['address'] = hash_160_to_bc_address(hash_160(txin.get("redeemScript").decode('hex')), 5)
276 elif txin.get("raw_output_script"):
278 addr = deserialize.get_address_from_output_script(txin.get("raw_output_script").decode('hex'))
279 sec = self.get_private_key(addr, password)
281 private_keys[addr] = sec
282 txin['address'] = addr
284 tx.sign( private_keys )
286 def sign_message(self, address, message, password):
287 sec = self.get_private_key(address, password)
288 key = regenerate_key(sec)
289 compressed = is_compressed(sec)
290 return key.sign_message(message, compressed, address)
293 def create_new_address(self, account, for_change):
294 addresses = self.accounts[account][for_change]
296 address = self.get_new_address( account, for_change, n)
297 self.accounts[account][for_change].append(address)
298 self.history[address] = []
303 def get_new_address(self, account, for_change, n):
304 return self.sequences[account].get_address((for_change, n))
308 def change_gap_limit(self, value):
309 if value >= self.gap_limit:
310 self.gap_limit = value
312 self.interface.poke('synchronizer')
315 elif value >= self.min_acceptable_gap():
316 for key, account in self.accounts.items():
317 addresses = account[0]
318 k = self.num_unused_trailing_addresses(addresses)
319 n = len(addresses) - k + value
320 addresses = addresses[0:n]
321 self.accounts[key][0] = addresses
323 self.gap_limit = value
329 def num_unused_trailing_addresses(self, addresses):
331 for a in addresses[::-1]:
332 if self.history.get(a):break
336 def min_acceptable_gap(self):
337 # fixme: this assumes wallet is synchronized
341 for account in self.accounts.values():
342 addresses = account[0]
343 k = self.num_unused_trailing_addresses(addresses)
344 for a in addresses[0:-k]:
345 if self.history.get(a):
349 if n > nmax: nmax = n
353 def address_is_old(self, address):
355 h = self.history.get(address, [])
358 for tx_hash, tx_height in h:
362 tx_age = self.verifier.height - tx_height + 1
368 def synchronize_sequence(self, account, for_change):
369 limit = self.gap_limit_for_change if for_change else self.gap_limit
370 addresses = self.accounts[account][for_change]
373 if len(addresses) < limit:
374 new_addresses.append( self.create_new_address(account, for_change) )
376 if map( lambda a: self.address_is_old(a), addresses[-limit:] ) == limit*[False]:
379 new_addresses.append( self.create_new_address(account, for_change) )
383 def synchronize_account(self, account):
385 new += self.synchronize_sequence(account, 0)
386 new += self.synchronize_sequence(account, 1)
389 def synchronize(self):
391 for account in self.accounts.keys():
392 new += self.synchronize_account(account)
397 return self.history.values() != [[]] * len(self.history)
400 def fill_addressbook(self):
401 for tx_hash, tx in self.transactions.items():
402 is_relevant, is_send, _, _ = self.get_tx_value(tx)
404 for addr, v in tx.outputs:
405 if not self.is_mine(addr) and addr not in self.addressbook:
406 self.addressbook.append(addr)
408 # self.update_tx_labels()
410 def get_num_tx(self, address):
412 for tx in self.transactions.values():
413 if address in map(lambda x:x[0], tx.outputs): n += 1
417 def get_address_flags(self, addr):
418 flags = "C" if self.is_change(addr) else "I" if addr in self.imported_keys.keys() else "-"
419 flags += "F" if addr in self.frozen_addresses else "P" if addr in self.prioritized_addresses else "-"
423 def get_tx_value(self, tx, account=None):
424 domain = self.get_account_addresses(account)
425 return tx.get_value(domain, self.prevout_values)
428 def update_tx_outputs(self, tx_hash):
429 tx = self.transactions.get(tx_hash)
431 for i, (addr, value) in enumerate(tx.outputs):
432 key = tx_hash+ ':%d'%i
433 self.prevout_values[key] = value
435 for item in tx.inputs:
436 if self.is_mine(item.get('address')):
437 key = item['prevout_hash'] + ':%d'%item['prevout_n']
438 self.spent_outputs.append(key)
441 def get_addr_balance(self, address):
442 assert self.is_mine(address)
443 h = self.history.get(address,[])
444 if h == ['*']: return 0,0
446 received_coins = [] # list of coins received at address
448 for tx_hash, tx_height in h:
449 tx = self.transactions.get(tx_hash)
452 for i, (addr, value) in enumerate(tx.outputs):
454 key = tx_hash + ':%d'%i
455 received_coins.append(key)
457 for tx_hash, tx_height in h:
458 tx = self.transactions.get(tx_hash)
462 for item in tx.inputs:
463 addr = item.get('address')
465 key = item['prevout_hash'] + ':%d'%item['prevout_n']
466 value = self.prevout_values.get( key )
467 if key in received_coins:
470 for i, (addr, value) in enumerate(tx.outputs):
471 key = tx_hash + ':%d'%i
482 def get_accounts(self):
484 for k, account in self.accounts.items():
485 accounts[k] = account.get('name')
486 if self.imported_keys:
487 accounts[-1] = 'Imported keys'
490 def get_account_addresses(self, a, include_change=True):
492 o = self.addresses(True)
494 o = self.imported_keys.keys()
496 ac = self.accounts[a]
498 if include_change: o += ac[1]
501 def get_imported_balance(self):
503 for addr in self.imported_keys.keys():
504 c, u = self.get_addr_balance(addr)
509 def get_account_balance(self, account):
511 return self.get_balance()
513 return self.get_imported_balance()
516 for addr in self.get_account_addresses(account):
517 c, u = self.get_addr_balance(addr)
522 def get_balance(self):
524 for a in self.accounts.keys():
525 c, u = self.get_account_balance(a)
528 c, u = self.get_imported_balance()
534 def get_unspent_coins(self, domain=None):
536 if domain is None: domain = self.addresses(True)
538 h = self.history.get(addr, [])
539 if h == ['*']: continue
540 for tx_hash, tx_height in h:
541 tx = self.transactions.get(tx_hash)
542 if tx is None: raise BaseException("Wallet not synchronized")
543 for output in tx.d.get('outputs'):
544 if output.get('address') != addr: continue
545 key = tx_hash + ":%d" % output.get('index')
546 if key in self.spent_outputs: continue
547 output['tx_hash'] = tx_hash
553 def choose_tx_inputs( self, amount, fixed_fee, account = None ):
554 """ todo: minimize tx size """
556 fee = self.fee if fixed_fee is None else fixed_fee
557 domain = self.get_account_addresses(account)
559 prioritized_coins = []
560 for i in self.frozen_addresses:
561 if i in domain: domain.remove(i)
563 for i in self.prioritized_addresses:
564 if i in domain: domain.remove(i)
566 coins = self.get_unspent_coins(domain)
567 prioritized_coins = self.get_unspent_coins(self.prioritized_addresses)
570 coins = prioritized_coins + coins
573 addr = item.get('address')
574 v = item.get('value')
576 inputs.append( item )
577 fee = self.estimated_fee(inputs) if fixed_fee is None else fixed_fee
578 if total >= amount + fee: break
582 return inputs, total, fee
585 def estimated_fee(self, inputs):
586 estimated_size = len(inputs) * 180 + 80 # this assumes non-compressed keys
587 fee = self.fee * int(round(estimated_size/1024.))
588 if fee == 0: fee = self.fee
592 def add_tx_change( self, inputs, outputs, amount, fee, total, change_addr=None, account=0 ):
593 "add change to a transaction"
594 change_amount = total - ( amount + fee )
595 if change_amount != 0:
597 if not self.use_change or account == -1:
598 change_addr = inputs[-1]['address']
600 if account is None: account = 0
601 change_addr = self.accounts[account][1][-self.gap_limit_for_change]
603 # Insert the change output at a random position in the outputs
604 posn = random.randint(0, len(outputs))
605 outputs[posn:posn] = [( change_addr, change_amount)]
609 def get_history(self, address):
611 return self.history.get(address)
614 def get_status(self, h):
615 if not h: return None
616 if h == ['*']: return '*'
618 for tx_hash, height in h:
619 status += tx_hash + ':%d:' % height
620 return hashlib.sha256( status ).digest().encode('hex')
623 def receive_tx_callback(self, tx_hash, tx, tx_height):
625 if not self.check_new_tx(tx_hash, tx):
626 # may happen due to pruning
627 print_error("received transaction that is no longer referenced in history", tx_hash)
630 with self.transaction_lock:
631 self.transactions[tx_hash] = tx
632 if self.verifier and tx_height>0:
633 self.verifier.add(tx_hash, tx_height)
634 self.update_tx_outputs(tx_hash)
639 def receive_history_callback(self, addr, hist):
641 if not self.check_new_history(addr, hist):
642 raise BaseException("error: received history for %s is not consistent with known transactions"%addr)
645 self.history[addr] = hist
649 for tx_hash, tx_height in hist:
651 # add it in case it was previously unconfirmed
652 if self.verifier: self.verifier.add(tx_hash, tx_height)
655 def get_tx_history(self, account=None):
656 with self.transaction_lock:
657 history = self.transactions.items()
658 history.sort(key = lambda x: self.verifier.verified_tx.get(x[0]) if self.verifier.verified_tx.get(x[0]) else (1e12,0,0))
662 for tx_hash, tx in history:
663 is_relevant, is_mine, v, fee = self.get_tx_value(tx, account)
664 if v is not None: balance += v
666 c, u = self.get_account_balance(account)
669 result.append( ('', 1000, 0, c+u-balance, None, c+u-balance, None ) )
671 balance = c + u - balance
672 for tx_hash, tx in history:
673 is_relevant, is_mine, value, fee = self.get_tx_value(tx, account)
676 if value is not None:
679 conf, timestamp = self.verifier.get_confirmations(tx_hash) if self.verifier else (None, None)
680 result.append( (tx_hash, conf, is_mine, value, fee, balance, timestamp) )
685 def get_label(self, tx_hash):
686 label = self.labels.get(tx_hash)
687 is_default = (label == '') or (label is None)
688 if is_default: label = self.get_default_label(tx_hash)
689 return label, is_default
692 def get_default_label(self, tx_hash):
693 tx = self.transactions.get(tx_hash)
696 is_relevant, is_mine, _, _ = self.get_tx_value(tx)
700 if not self.is_mine(o_addr):
702 default_label = self.labels[o_addr]
704 default_label = o_addr
707 default_label = '(internal)'
711 if self.is_mine(o_addr) and not self.is_change(o_addr):
716 if self.is_mine(o_addr):
722 dest_label = self.labels.get(o_addr)
724 default_label = self.labels[o_addr]
726 default_label = o_addr
731 def mktx(self, outputs, password, fee=None, change_addr=None, account=None ):
735 None means use all accounts
736 -1 means imported keys
737 0, 1, etc are seed accounts
740 for address, x in outputs:
741 assert is_valid(address)
743 amount = sum( map(lambda x:x[1], outputs) )
745 domain = self.get_account_addresses(account)
747 inputs, total, fee = self.choose_tx_inputs( amount, fee, domain )
749 raise ValueError("Not enough funds")
751 outputs = self.add_tx_change(inputs, outputs, amount, fee, total, change_addr, account)
753 tx = Transaction.from_io(inputs, outputs)
756 for i in range(len(tx.inputs)):
758 address = txin['address']
759 if address in self.imported_keys.keys():
760 pk_addresses.append(address)
762 account, sequence = self.get_address_index(address)
763 txin['KeyID'] = (account, 'Electrum', sequence) # used by the server to find the key
764 pk_addr, redeemScript = self.sequences[account].get_input_info(sequence)
765 if redeemScript: txin['redeemScript'] = redeemScript
766 pk_addresses.append(pk_addr)
768 # get all private keys at once.
770 private_keys = self.get_private_keys(pk_addresses, password)
771 tx.sign(private_keys)
773 for address, x in outputs:
774 if address not in self.addressbook and not self.is_mine(address):
775 self.addressbook.append(address)
781 def sendtx(self, tx):
785 return self.receive_tx(h)
787 def send_tx(self, tx):
789 self.tx_event.clear()
790 self.interface.send([('blockchain.transaction.broadcast', [str(tx)])], 'synchronizer')
793 def receive_tx(self,tx_hash):
796 return False, "error: " + out
801 def update_password(self, seed, old_password, new_password):
802 if new_password == '': new_password = None
803 self.use_encryption = (new_password != None)
804 self.seed = pw_encode( seed, new_password)
805 self.config.set_key('seed', self.seed, True)
806 for k in self.imported_keys.keys():
807 a = self.imported_keys[k]
808 b = pw_decode(a, old_password)
809 c = pw_encode(b, new_password)
810 self.imported_keys[k] = c
815 def freeze(self,addr):
816 if self.is_mine(addr) and addr not in self.frozen_addresses:
817 self.unprioritize(addr)
818 self.frozen_addresses.append(addr)
819 self.config.set_key('frozen_addresses', self.frozen_addresses, True)
824 def unfreeze(self,addr):
825 if self.is_mine(addr) and addr in self.frozen_addresses:
826 self.frozen_addresses.remove(addr)
827 self.config.set_key('frozen_addresses', self.frozen_addresses, True)
832 def prioritize(self,addr):
833 if self.is_mine(addr) and addr not in self.prioritized_addresses:
835 self.prioritized_addresses.append(addr)
836 self.config.set_key('prioritized_addresses', self.prioritized_addresses, True)
841 def unprioritize(self,addr):
842 if self.is_mine(addr) and addr in self.prioritized_addresses:
843 self.prioritized_addresses.remove(addr)
844 self.config.set_key('prioritized_addresses', self.prioritized_addresses, True)
851 for k,v in self.transactions.items():
855 'use_encryption': self.use_encryption,
856 'use_change': self.use_change,
857 'fee_per_kb': self.fee,
858 'accounts': self.accounts,
859 'addr_history': self.history,
860 'labels': self.labels,
861 'contacts': self.addressbook,
862 'imported_keys': self.imported_keys,
863 'num_zeros': self.num_zeros,
864 'frozen_addresses': self.frozen_addresses,
865 'prioritized_addresses': self.prioritized_addresses,
866 'gap_limit': self.gap_limit,
869 for k, v in s.items():
870 self.config.set_key(k,v)
873 def set_verifier(self, verifier):
874 self.verifier = verifier
876 # review transactions that are in the history
877 for addr, hist in self.history.items():
878 if hist == ['*']: continue
879 for tx_hash, tx_height in hist:
881 # add it in case it was previously unconfirmed
882 self.verifier.add(tx_hash, tx_height)
885 # if we are on a pruning server, remove unverified transactions
886 vr = self.verifier.transactions.keys() + self.verifier.verified_tx.keys()
887 for tx_hash in self.transactions.keys():
888 if tx_hash not in vr:
889 self.transactions.pop(tx_hash)
893 def check_new_history(self, addr, hist):
895 # check that all tx in hist are relevant
897 for tx_hash, height in hist:
898 tx = self.transactions.get(tx_hash)
900 if not tx.has_address(addr):
903 # check that we are not "orphaning" a transaction
904 old_hist = self.history.get(addr,[])
905 if old_hist == ['*']: return True
907 for tx_hash, height in old_hist:
908 if tx_hash in map(lambda x:x[0], hist): continue
910 for _addr, _hist in self.history.items():
911 if _addr == addr: continue
912 if _hist == ['*']: continue
913 _tx_hist = map(lambda x:x[0], _hist)
914 if tx_hash in _tx_hist:
919 tx = self.transactions.get(tx_hash)
920 # tx might not be there
924 if self.verifier.get_height(tx_hash):
927 print_error("new history is orphaning transaction:", tx_hash)
928 # check that all outputs are not mine, request histories
930 for _addr, _v in tx.outputs:
931 # assert not self.is_mine(_addr)
932 ext_requests.append( ('blockchain.address.get_history', [_addr]) )
934 ext_h = self.interface.synchronous_get(ext_requests)
935 print_error("sync:", ext_requests, ext_h)
938 if h == ['*']: continue
940 if item.get('tx_hash') == tx_hash:
941 height = item.get('height')
943 print_error("found height for", tx_hash, height)
944 self.verifier.add(tx_hash, height)
946 print_error("removing orphaned tx from history", tx_hash)
947 self.transactions.pop(tx_hash)
953 def check_new_tx(self, tx_hash, tx):
954 # 1 check that tx is referenced in addr_history.
956 for addr, hist in self.history.items():
957 if hist == ['*']:continue
958 for txh, height in hist:
960 addresses.append(addr)
965 # 2 check that referencing addresses are in the tx
966 for addr in addresses:
967 if not tx.has_address(addr):
975 class WalletSynchronizer(threading.Thread):
978 def __init__(self, wallet, config):
979 threading.Thread.__init__(self)
982 self.interface = self.wallet.interface
983 self.interface.register_channel('synchronizer')
984 self.wallet.interface.register_callback('connected', lambda: self.wallet.set_up_to_date(False))
985 self.was_updated = True
987 self.lock = threading.Lock()
990 with self.lock: self.running = False
991 self.interface.poke('synchronizer')
993 def is_running(self):
994 with self.lock: return self.running
997 def subscribe_to_addresses(self, addresses):
999 for addr in addresses:
1000 messages.append(('blockchain.address.subscribe', [addr]))
1001 self.interface.send( messages, 'synchronizer')
1005 with self.lock: self.running = True
1009 requested_histories = {}
1011 # request any missing transactions
1012 for history in self.wallet.history.values():
1013 if history == ['*']: continue
1014 for tx_hash, tx_height in history:
1015 if self.wallet.transactions.get(tx_hash) is None and (tx_hash, tx_height) not in missing_tx:
1016 missing_tx.append( (tx_hash, tx_height) )
1017 print_error("missing tx", missing_tx)
1019 # wait until we are connected, in case the user is not connected
1020 while not self.interface.is_connected:
1024 self.subscribe_to_addresses(self.wallet.addresses(True))
1026 while self.is_running():
1027 # 1. create new addresses
1028 new_addresses = self.wallet.synchronize()
1030 # request missing addresses
1032 self.subscribe_to_addresses(new_addresses)
1034 # request missing transactions
1035 for tx_hash, tx_height in missing_tx:
1036 if (tx_hash, tx_height) not in requested_tx:
1037 self.interface.send([ ('blockchain.transaction.get',[tx_hash, tx_height]) ], 'synchronizer')
1038 requested_tx.append( (tx_hash, tx_height) )
1041 # detect if situation has changed
1042 if not self.interface.is_up_to_date('synchronizer'):
1043 if self.wallet.is_up_to_date():
1044 self.wallet.set_up_to_date(False)
1045 self.was_updated = True
1047 if not self.wallet.is_up_to_date():
1048 self.wallet.set_up_to_date(True)
1049 self.was_updated = True
1051 if self.was_updated:
1052 self.interface.trigger_callback('updated')
1053 self.was_updated = False
1056 r = self.interface.get_response('synchronizer')
1058 # poke sends None. (needed during stop)
1061 # 3. handle response
1062 method = r['method']
1063 params = r['params']
1064 result = r.get('result')
1065 error = r.get('error')
1070 if method == 'blockchain.address.subscribe':
1072 if self.wallet.get_status(self.wallet.get_history(addr)) != result:
1073 if requested_histories.get(addr) is None:
1074 self.interface.send([('blockchain.address.get_history', [addr])], 'synchronizer')
1075 requested_histories[addr] = result
1077 elif method == 'blockchain.address.get_history':
1079 print_error("receiving history", addr, result)
1081 assert requested_histories.pop(addr) == '*'
1082 self.wallet.receive_history_callback(addr, result)
1085 # check that txids are unique
1088 tx_hash = item['tx_hash']
1089 if tx_hash not in txids:
1090 txids.append(tx_hash)
1091 hist.append( (tx_hash, item['height']) )
1093 if len(hist) != len(result):
1094 raise BaseException("error: server sent history with non-unique txid", result)
1096 # check that the status corresponds to what was announced
1097 rs = requested_histories.pop(addr)
1098 if self.wallet.get_status(hist) != rs:
1099 raise BaseException("error: status mismatch: %s"%addr)
1101 # store received history
1102 self.wallet.receive_history_callback(addr, hist)
1104 # request transactions that we don't have
1105 for tx_hash, tx_height in hist:
1106 if self.wallet.transactions.get(tx_hash) is None:
1107 if (tx_hash, tx_height) not in requested_tx and (tx_hash, tx_height) not in missing_tx:
1108 missing_tx.append( (tx_hash, tx_height) )
1110 elif method == 'blockchain.transaction.get':
1112 tx_height = params[1]
1113 assert tx_hash == hash_encode(Hash(result.decode('hex')))
1114 tx = Transaction(result)
1115 self.wallet.receive_tx_callback(tx_hash, tx, tx_height)
1116 self.was_updated = True
1117 requested_tx.remove( (tx_hash, tx_height) )
1118 print_error("received tx:", tx_hash, len(tx.raw))
1120 elif method == 'blockchain.transaction.broadcast':
1121 self.wallet.tx_result = result
1122 self.wallet.tx_event.set()
1125 print_error("Error: Unknown message:" + method + ", " + repr(params) + ", " + repr(result) )
1127 if self.was_updated and not requested_tx:
1128 self.interface.trigger_callback('updated')
1129 self.was_updated = False