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] = ElectrumSequence(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")
110 self.prevout_values = {} # my own transaction outputs
111 self.spent_outputs = []
112 self.receipt = None # next receipt
118 # there is a difference between wallet.up_to_date and interface.is_up_to_date()
119 # interface.is_up_to_date() returns true when all requests have been answered and processed
120 # wallet.up_to_date is true when the wallet is synchronized (stronger requirement)
122 self.up_to_date = False
123 self.lock = threading.Lock()
124 self.tx_event = threading.Event()
126 if self.seed_version != SEED_VERSION:
127 raise ValueError("This wallet seed is deprecated. Please run upgrade.py for a diagnostic.")
129 for tx_hash in self.transactions.keys():
130 self.update_tx_outputs(tx_hash)
133 def set_up_to_date(self,b):
134 with self.lock: self.up_to_date = b
136 def is_up_to_date(self):
137 with self.lock: return self.up_to_date
140 self.up_to_date = False
141 self.interface.poke('synchronizer')
142 while not self.is_up_to_date(): time.sleep(0.1)
144 def import_key(self, sec, password):
146 seed = self.decode_seed(password)
147 address = address_from_private_key(sec)
149 if self.is_mine(address):
150 raise BaseException('Address already in wallet')
152 # store the originally requested keypair into the imported keys table
153 self.imported_keys[address] = pw_encode(sec, password )
157 def init_seed(self, seed):
158 if self.seed: raise BaseException("a seed exists")
160 seed = random_seed(128)
162 self.config.set_key('seed', self.seed, True)
163 self.config.set_key('seed_version', self.seed_version, True)
165 mpk = ElectrumSequence.mpk_from_seed(self.seed)
166 self.config.set_key('master_public_key', mpk, True)
167 self.sequences[0] = ElectrumSequence(mpk)
169 self.accounts[0] = { 0:[], 1:[], 'name':'Main account' }
170 self.config.set_key('accounts', self.accounts, True)
174 def addresses(self, include_change = False):
175 o = self.imported_keys.keys()
176 for a in self.accounts.values():
178 if include_change: o += a[1]
182 def is_mine(self, address):
183 return address in self.addresses(True)
185 def is_change(self, address):
186 #return address in self.change_addresses
189 def get_master_public_key(self):
190 return self.sequence.master_public_key
192 def get_address_index(self, address):
193 if address in self.imported_keys.keys():
194 raise BaseException("imported key")
195 for account in self.accounts.keys():
196 for for_change in [0,1]:
197 addresses = self.accounts[account][for_change]
198 for addr in addresses:
200 return account, (for_change, addresses.index(addr))
201 raise BaseException("not found")
204 def get_public_key(self, address):
205 account, sequence = self.get_address_index(address)
206 return self.sequences[account].get_pubkey( sequence )
209 def decode_seed(self, password):
210 seed = pw_decode(self.seed, password)
211 self.sequences[0].check_seed(seed)
214 def get_private_key(self, address, password):
215 return self.get_private_keys([address], password).get(address)
217 def get_private_keys(self, addresses, password):
218 # decode seed in any case, in order to test the password
219 seed = self.decode_seed(password)
223 for address in addresses:
224 if address in self.imported_keys.keys():
225 out[address] = pw_decode( self.imported_keys[address], password )
227 account, sequence = self.get_address_index(address)
229 l_sequences.append(sequence)
230 l_addresses.append(address)
232 pk = self.sequences[0].get_private_keys(l_sequences, seed)
233 for i, address in enumerate(l_addresses): out[address] = pk[i]
237 def signrawtransaction(self, tx, input_info, private_keys, password):
238 unspent_coins = self.get_unspent_coins()
239 seed = self.decode_seed(password)
241 # convert private_keys to dict
243 for sec in private_keys:
244 address = address_from_private_key(sec)
248 for txin in tx.inputs:
249 # convert to own format
250 txin['tx_hash'] = txin['prevout_hash']
251 txin['index'] = txin['prevout_n']
253 for item in input_info:
254 if item.get('txid') == txin['tx_hash'] and item.get('vout') == txin['index']:
255 txin['raw_output_script'] = item['scriptPubKey']
256 txin['redeemScript'] = item.get('redeemScript')
257 txin['electrumKeyID'] = item.get('electrumKeyID')
260 for item in unspent_coins:
261 if txin['tx_hash'] == item['tx_hash'] and txin['index'] == item['index']:
262 txin['raw_output_script'] = item['raw_output_script']
265 # if neither, we might want to get it from the server..
269 if txin.get('electrumKeyID'):
270 account, sequence = txin.get('electrumKeyID')
271 sec = self.sequences[account].get_private_key(sequence, seed)
272 addr = self.sequences[account].get_address(sequence)
273 txin['address'] = addr
274 private_keys[addr] = sec
276 elif txin.get("redeemScript"):
277 txin['address'] = hash_160_to_bc_address(hash_160(txin.get("redeemScript").decode('hex')), 5)
279 elif txin.get("raw_output_script"):
281 addr = deserialize.get_address_from_output_script(txin.get("raw_output_script").decode('hex'))
282 sec = self.get_private_key(addr, password)
284 private_keys[addr] = sec
285 txin['address'] = addr
287 tx.sign( private_keys )
289 def sign_message(self, address, message, password):
290 sec = self.get_private_key(address, password)
291 key = regenerate_key(sec)
292 compressed = is_compressed(sec)
293 return key.sign_message(message, compressed, address)
296 def create_new_address(self, account, for_change):
297 addresses = self.accounts[account][for_change]
299 address = self.get_new_address( account, for_change, n)
300 self.accounts[account][for_change].append(address)
301 self.history[address] = []
305 def get_new_address(self, account, for_change, n):
306 return self.sequences[account].get_address((for_change, n))
310 def change_gap_limit(self, value):
311 if value >= self.gap_limit:
312 self.gap_limit = value
314 self.interface.poke('synchronizer')
317 elif value >= self.min_acceptable_gap():
318 for key, account in self.accounts.items():
319 addresses = account[0]
320 k = self.num_unused_trailing_addresses(addresses)
321 n = len(addresses) - k + value
322 addresses = addresses[0:n]
323 self.accounts[key][0] = addresses
325 self.gap_limit = value
331 def num_unused_trailing_addresses(self, addresses):
333 for a in addresses[::-1]:
334 if self.history.get(a):break
338 def min_acceptable_gap(self):
339 # fixme: this assumes wallet is synchronized
343 for account in self.accounts.values():
344 addresses = account[0]
345 k = self.num_unused_trailing_addresses(addresses)
346 for a in addresses[0:-k]:
347 if self.history.get(a):
351 if n > nmax: nmax = n
355 def address_is_old(self, address):
357 h = self.history.get(address, [])
360 for tx_hash, tx_height in h:
364 tx_age = self.verifier.height - tx_height + 1
370 def synchronize_sequence(self, account, for_change):
371 limit = self.gap_limit_for_change if for_change else self.gap_limit
372 addresses = self.accounts[account][for_change]
375 if len(addresses) < limit:
376 new_addresses.append( self.create_new_address(account, for_change) )
378 if map( lambda a: self.address_is_old(a), addresses[-limit:] ) == limit*[False]:
381 new_addresses.append( self.create_new_address(account, for_change) )
385 def synchronize_account(self, account):
387 new += self.synchronize_sequence(account, 0)
388 new += self.synchronize_sequence(account, 1)
391 def synchronize(self):
393 for account in self.accounts.keys():
394 new += self.synchronize_account(account)
399 return self.history.values() != [[]] * len(self.history)
402 def fill_addressbook(self):
403 for tx_hash, tx in self.transactions.items():
404 is_send, _, _ = self.get_tx_value(tx)
406 for addr, v in tx.outputs:
407 if not self.is_mine(addr) and addr not in self.addressbook:
408 self.addressbook.append(addr)
410 # self.update_tx_labels()
413 def get_address_flags(self, addr):
414 flags = "C" if self.is_change(addr) else "I" if addr in self.imported_keys.keys() else "-"
415 flags += "F" if addr in self.frozen_addresses else "P" if addr in self.prioritized_addresses else "-"
419 def get_tx_value(self, tx, addresses=None):
420 if addresses is None: addresses = self.addresses(True)
421 return tx.get_value(addresses, self.prevout_values)
424 def get_tx_details(self, tx_hash):
426 if not tx_hash: return ''
427 tx = self.transactions.get(tx_hash)
428 is_mine, v, fee = self.get_tx_value(tx)
429 conf, timestamp = self.verifier.get_confirmations(tx_hash)
432 time_str = datetime.datetime.fromtimestamp(timestamp).isoformat(' ')[:-3]
436 inputs = map(lambda x: x.get('address'), tx.inputs)
437 outputs = map(lambda x: x.get('address'), tx.d['outputs'])
438 tx_details = "Transaction Details" +"\n\n" \
439 + "Transaction ID:\n" + tx_hash + "\n\n" \
440 + "Status: %d confirmations\n"%conf
443 tx_details += "Amount sent: %s\n"% format_satoshis(v-fee, False) \
444 + "Transaction fee: %s\n"% format_satoshis(fee, False)
446 tx_details += "Amount sent: %s\n"% format_satoshis(v, False) \
447 + "Transaction fee: unknown\n"
449 tx_details += "Amount received: %s\n"% format_satoshis(v, False) \
451 tx_details += "Date: %s\n\n"%time_str \
452 + "Inputs:\n-"+ '\n-'.join(inputs) + "\n\n" \
453 + "Outputs:\n-"+ '\n-'.join(outputs)
455 r = self.receipts.get(tx_hash)
457 tx_details += "\n_______________________________________" \
458 + '\n\nSigned URI: ' + r[2] \
459 + "\n\nSigned by: " + r[0] \
460 + '\n\nSignature: ' + r[1]
465 def update_tx_outputs(self, tx_hash):
466 tx = self.transactions.get(tx_hash)
468 for item in tx.outputs:
470 key = tx_hash+ ':%d'%i
472 self.prevout_values[key] = value
475 for item in tx.inputs:
476 if self.is_mine(item.get('address')):
477 key = item['prevout_hash'] + ':%d'%item['prevout_n']
478 self.spent_outputs.append(key)
481 def get_addr_balance(self, address):
482 assert self.is_mine(address)
483 h = self.history.get(address,[])
484 if h == ['*']: return 0,0
486 received_coins = [] # list of coins received at address
488 for tx_hash, tx_height in h:
489 tx = self.transactions.get(tx_hash)
492 for item in tx.outputs:
495 key = tx_hash + ':%d'%i
496 received_coins.append(key)
499 for tx_hash, tx_height in h:
500 tx = self.transactions.get(tx_hash)
504 for item in tx.inputs:
505 addr = item.get('address')
507 key = item['prevout_hash'] + ':%d'%item['prevout_n']
508 value = self.prevout_values.get( key )
509 if key in received_coins:
513 for item in tx.outputs:
515 key = tx_hash + ':%d'%i
526 def get_account_addresses(self, a):
527 ac = self.accounts[a]
530 def get_imported_balance(self):
532 for addr in self.imported_keys.keys():
533 c, u = self.get_addr_balance(addr)
538 def get_account_balance(self, account):
540 for addr in self.get_account_addresses(account):
541 c, u = self.get_addr_balance(addr)
546 def get_balance(self):
548 for a in self.accounts.keys():
549 c, u = self.get_account_balance(a)
552 c, u = self.get_imported_balance()
558 def get_unspent_coins(self, domain=None):
560 if domain is None: domain = self.addresses(True)
562 h = self.history.get(addr, [])
563 if h == ['*']: continue
564 for tx_hash, tx_height in h:
565 tx = self.transactions.get(tx_hash)
566 for output in tx.d.get('outputs'):
567 if output.get('address') != addr: continue
568 key = tx_hash + ":%d" % output.get('index')
569 if key in self.spent_outputs: continue
570 output['tx_hash'] = tx_hash
576 def choose_tx_inputs( self, amount, fixed_fee, from_addr = None ):
577 """ todo: minimize tx size """
579 fee = self.fee if fixed_fee is None else fixed_fee
582 prioritized_coins = []
583 domain = [from_addr] if from_addr else self.addresses(True)
584 for i in self.frozen_addresses:
585 if i in domain: domain.remove(i)
587 for i in self.prioritized_addresses:
588 if i in domain: domain.remove(i)
590 coins = self.get_unspent_coins(domain)
591 prioritized_coins = self.get_unspent_coins(self.prioritized_addresses)
594 coins = prioritized_coins + coins
597 addr = item.get('address')
598 v = item.get('value')
601 inputs.append( item )
602 fee = self.fee*len(inputs) if fixed_fee is None else fixed_fee
603 if total >= amount + fee: break
605 #print "not enough funds: %s %s"%(format_satoshis(total), format_satoshis(fee))
608 return inputs, total, fee
610 def add_tx_change( self, outputs, amount, fee, total, change_addr=None ):
611 change_amount = total - ( amount + fee )
612 if change_amount != 0:
613 # normally, the update thread should ensure that the last change address is unused
615 change_addresses = self.accounts[0][1]
616 change_addr = change_addresses[-self.gap_limit_for_change]
617 # Insert the change output at a random position in the outputs
618 posn = random.randint(0, len(outputs))
619 outputs[posn:posn] = [( change_addr, change_amount)]
623 def get_history(self, address):
625 return self.history.get(address)
627 def get_status(self, h):
628 if not h: return None
629 if h == ['*']: return '*'
631 for tx_hash, height in h:
632 status += tx_hash + ':%d:' % height
633 return hashlib.sha256( status ).digest().encode('hex')
637 def receive_tx_callback(self, tx_hash, tx, tx_height):
639 if not self.check_new_tx(tx_hash, tx):
640 raise BaseException("error: received transaction is not consistent with history"%tx_hash)
643 self.transactions[tx_hash] = tx
644 self.tx_height[tx_hash] = tx_height
646 #tx_height = tx.get('height')
647 if self.verifier and tx_height>0:
648 self.verifier.add(tx_hash, tx_height)
650 self.update_tx_outputs(tx_hash)
655 def receive_history_callback(self, addr, hist):
657 if not self.check_new_history(addr, hist):
658 raise BaseException("error: received history for %s is not consistent with known transactions"%addr)
661 self.history[addr] = hist
665 for tx_hash, tx_height in hist:
667 # add it in case it was previously unconfirmed
668 if self.verifier: self.verifier.add(tx_hash, tx_height)
669 # set the height in case it changed
670 txh = self.tx_height.get(tx_hash)
671 if txh is not None and txh != tx_height:
672 print_error( "changing height for tx", tx_hash )
673 self.tx_height[tx_hash] = tx_height
676 def get_tx_history(self):
678 history = self.transactions.items()
679 history.sort(key = lambda x: self.tx_height.get(x[0]) if self.tx_height.get(x[0]) else 1e12)
683 for tx_hash, tx in history:
684 is_mine, v, fee = self.get_tx_value(tx)
685 if v is not None: balance += v
686 c, u = self.get_balance()
689 v_str = format_satoshis( c+u - balance, True, self.num_zeros)
690 result.append( ('', 1000, 0, c+u-balance, None, c+u-balance, None ) )
692 balance = c + u - balance
693 for tx_hash, tx in history:
694 conf, timestamp = self.verifier.get_confirmations(tx_hash) if self.verifier else (None, None)
695 is_mine, value, fee = self.get_tx_value(tx)
696 if value is not None:
699 result.append( (tx_hash, conf, is_mine, value, fee, balance, timestamp) )
704 def get_label(self, tx_hash):
705 label = self.labels.get(tx_hash)
706 is_default = (label == '') or (label is None)
707 if is_default: label = self.get_default_label(tx_hash)
708 return label, is_default
711 def get_default_label(self, tx_hash):
712 tx = self.transactions.get(tx_hash)
715 is_mine, _, _ = self.get_tx_value(tx)
719 if not self.is_mine(o_addr):
721 default_label = self.labels[o_addr]
723 default_label = o_addr
727 if self.is_mine(o_addr) and not self.is_change(o_addr):
732 if self.is_mine(o_addr):
738 dest_label = self.labels.get(o_addr)
740 default_label = self.labels[o_addr]
742 default_label = o_addr
747 def mktx(self, outputs, password, fee=None, change_addr=None, from_addr= None):
749 for address, x in outputs:
750 assert is_valid(address)
752 amount = sum( map(lambda x:x[1], outputs) )
753 inputs, total, fee = self.choose_tx_inputs( amount, fee, from_addr )
755 raise ValueError("Not enough funds")
757 if not self.use_change and not change_addr:
758 change_addr = inputs[-1]['address']
759 print_error( "Sending change to", change_addr )
760 outputs = self.add_tx_change(outputs, amount, fee, total, change_addr)
762 tx = Transaction.from_io(inputs, outputs)
765 for i in range(len(tx.inputs)):
767 address = txin['address']
768 if address in self.imported_keys.keys():
769 pk_addresses.append(address)
771 account, sequence = self.get_address_index(address)
772 txin['electrumKeyID'] = (account, sequence) # used by the server to find the key
773 pk_addr, redeemScript = self.sequences[account].get_input_info(sequence)
774 if redeemScript: txin['redeemScript'] = redeemScript
775 pk_addresses.append(pk_addr)
777 # get all private keys at once.
778 private_keys = self.get_private_keys(pk_addresses, password)
779 tx.sign(private_keys)
781 for address, x in outputs:
782 if address not in self.addressbook and not self.is_mine(address):
783 self.addressbook.append(address)
789 def sendtx(self, tx):
793 return self.receive_tx(h)
795 def send_tx(self, tx):
797 self.tx_event.clear()
798 self.interface.send([('blockchain.transaction.broadcast', [str(tx)])], 'synchronizer')
801 def receive_tx(self,tx_hash):
804 return False, "error: " + out
806 self.receipts[tx_hash] = self.receipt
811 def read_alias(self, alias):
812 # this might not be the right place for this function.
815 m1 = re.match('([\w\-\.]+)@((\w[\w\-]+\.)+[\w\-]+)', alias)
816 m2 = re.match('((\w[\w\-]+\.)+[\w\-]+)', alias)
818 url = 'https://' + m1.group(2) + '/bitcoin.id/' + m1.group(1)
820 url = 'https://' + alias + '/bitcoin.id'
824 lines = urllib.urlopen(url).readlines()
829 line = lines[0].strip().split(':')
832 target = signing_addr = line[0]
834 target, auth_name, signing_addr, signature = line
835 msg = "alias:%s:%s:%s"%(alias,target,auth_name)
837 EC_KEY.verify_message(signing_addr, signature, msg)
839 # other lines are signed updates
840 for line in lines[1:]:
842 if not line: continue
843 line = line.split(':')
846 target, signature = line
847 EC_KEY.verify_message(previous, signature, "alias:%s:%s"%(alias,target))
849 if not is_valid(target):
850 raise ValueError("Invalid bitcoin address")
852 return target, signing_addr, auth_name
854 def update_password(self, seed, old_password, new_password):
855 if new_password == '': new_password = None
856 self.use_encryption = (new_password != None)
857 self.seed = pw_encode( seed, new_password)
858 self.config.set_key('seed', self.seed, True)
859 for k in self.imported_keys.keys():
860 a = self.imported_keys[k]
861 b = pw_decode(a, old_password)
862 c = pw_encode(b, new_password)
863 self.imported_keys[k] = c
866 def get_alias(self, alias, interactive = False, show_message=None, question = None):
868 target, signing_address, auth_name = self.read_alias(alias)
869 except BaseException, e:
870 # raise exception if verify fails (verify the chain)
872 show_message("Alias error: " + str(e))
875 print target, signing_address, auth_name
877 if auth_name is None:
878 a = self.aliases.get(alias)
880 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)
881 if interactive and question( msg ):
882 self.aliases[alias] = (signing_address, target)
886 if signing_address != a[0]:
887 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
888 if interactive and question( msg ):
889 self.aliases[alias] = (signing_address, target)
893 if signing_address not in self.authorities.keys():
894 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)
895 if interactive and question( msg ):
896 self.authorities[signing_address] = auth_name
901 self.aliases[alias] = (signing_address, target)
906 def parse_url(self, url, show_message, question):
907 o = url[8:].split('?')
910 params = o[1].split('&')
914 amount = label = message = signature = identity = ''
918 if k == 'amount': amount = uv
919 elif k == 'message': message = uv
920 elif k == 'label': label = uv
921 elif k == 'signature':
922 identity, signature = uv.split(':')
923 url = url.replace('&%s=%s'%(k,v),'')
927 if label and self.labels.get(address) != label:
928 if question('Give label "%s" to address %s ?'%(label,address)):
929 if address not in self.addressbook and not self.is_mine(address):
930 self.addressbook.append(address)
931 self.labels[address] = label
934 if re.match('^(|([\w\-\.]+)@)((\w[\w\-]+\.)+[\w\-]+)$', identity):
935 signing_address = self.get_alias(identity, True, show_message, question)
936 elif is_valid(identity):
937 signing_address = identity
939 signing_address = None
940 if not signing_address:
943 EC_KEY.verify_message(signing_address, signature, url )
944 self.receipt = (signing_address, signature, url)
946 show_message('Warning: the URI contains a bad signature.\nThe identity of the recipient cannot be verified.')
947 address = amount = label = identity = message = ''
949 if re.match('^(|([\w\-\.]+)@)((\w[\w\-]+\.)+[\w\-]+)$', address):
950 payto_address = self.get_alias(address, True, show_message, question)
952 address = address + ' <' + payto_address + '>'
954 return address, amount, label, message, signature, identity, url
958 def freeze(self,addr):
959 if self.is_mine(addr) and addr not in self.frozen_addresses:
960 self.unprioritize(addr)
961 self.frozen_addresses.append(addr)
962 self.config.set_key('frozen_addresses', self.frozen_addresses, True)
967 def unfreeze(self,addr):
968 if self.is_mine(addr) and addr in self.frozen_addresses:
969 self.frozen_addresses.remove(addr)
970 self.config.set_key('frozen_addresses', self.frozen_addresses, True)
975 def prioritize(self,addr):
976 if self.is_mine(addr) and addr not in self.prioritized_addresses:
978 self.prioritized_addresses.append(addr)
979 self.config.set_key('prioritized_addresses', self.prioritized_addresses, True)
984 def unprioritize(self,addr):
985 if self.is_mine(addr) and addr in self.prioritized_addresses:
986 self.prioritized_addresses.remove(addr)
987 self.config.set_key('prioritized_addresses', self.prioritized_addresses, True)
994 for k,v in self.transactions.items():
998 'use_encryption': self.use_encryption,
999 'use_change': self.use_change,
1001 'accounts': self.accounts,
1002 'addr_history': self.history,
1003 'labels': self.labels,
1004 'contacts': self.addressbook,
1005 'imported_keys': self.imported_keys,
1006 'aliases': self.aliases,
1007 'authorities': self.authorities,
1008 'receipts': self.receipts,
1009 'num_zeros': self.num_zeros,
1010 'frozen_addresses': self.frozen_addresses,
1011 'prioritized_addresses': self.prioritized_addresses,
1012 'gap_limit': self.gap_limit,
1014 'tx_height': self.tx_height,
1016 for k, v in s.items():
1017 self.config.set_key(k,v)
1020 def set_verifier(self, verifier):
1021 self.verifier = verifier
1023 # review stored transactions and send them to the verifier
1024 # (they are not necessarily in the history, because history items might have have been pruned)
1025 for tx_hash, tx in self.transactions.items():
1026 tx_height = self.tx_height[tx_hash]
1028 print_error( "skipping", tx_hash, tx_height )
1032 self.verifier.add(tx_hash, tx_height)
1034 # review transactions that are in the history
1035 for addr, hist in self.history.items():
1036 if hist == ['*']: continue
1037 for tx_hash, tx_height in hist:
1039 # add it in case it was previously unconfirmed
1040 self.verifier.add(tx_hash, tx_height)
1041 # set the height in case it changed
1042 txh = self.tx_height.get(tx_hash)
1043 if txh is not None and txh != tx_height:
1044 print_error( "changing height for tx", tx_hash )
1045 self.tx_height[tx_hash] = tx_height
1050 def check_new_history(self, addr, hist):
1052 # check that all tx in hist are relevant
1054 for tx_hash, height in hist:
1055 tx = self.transactions.get(tx_hash)
1057 if not tx.has_address(addr):
1060 # check that we are not "orphaning" a transaction
1061 old_hist = self.history.get(addr,[])
1062 if old_hist == ['*']: return True
1064 for tx_hash, height in old_hist:
1065 if tx_hash in map(lambda x:x[0], hist): continue
1067 for _addr, _hist in self.history.items():
1068 if _addr == addr: continue
1069 if _hist == ['*']: continue
1070 _tx_hist = map(lambda x:x[0], _hist)
1071 if tx_hash in _tx_hist:
1076 tx = self.transactions.get(tx_hash)
1077 # tx might not be there
1081 if self.tx_height.get(tx_hash):
1084 print_error("new history is orphaning transaction:", tx_hash)
1085 # check that all outputs are not mine, request histories
1087 for _addr, _v in tx.outputs:
1088 # assert not self.is_mine(_addr)
1089 ext_requests.append( ('blockchain.address.get_history', [_addr]) )
1091 ext_h = self.interface.synchronous_get(ext_requests)
1094 if h == ['*']: continue
1096 if item.get('tx_hash') == tx_hash:
1097 height = item.get('height')
1098 self.tx_height[tx_hash] = height
1100 print_error("found height for", tx_hash, height)
1101 self.verifier.add(tx_hash, height)
1103 print_error("removing orphaned tx from history", tx_hash)
1104 self.transactions.pop(tx_hash)
1110 def check_new_tx(self, tx_hash, tx):
1111 # 1 check that tx is referenced in addr_history.
1113 for addr, hist in self.history.items():
1114 if hist == ['*']:continue
1115 for txh, height in hist:
1117 addresses.append(addr)
1122 # 2 check that referencing addresses are in the tx
1123 for addr in addresses:
1124 if not tx.has_address(addr):
1132 class WalletSynchronizer(threading.Thread):
1135 def __init__(self, wallet, config):
1136 threading.Thread.__init__(self)
1138 self.wallet = wallet
1139 self.interface = self.wallet.interface
1140 self.interface.register_channel('synchronizer')
1141 self.wallet.interface.register_callback('connected', lambda: self.wallet.set_up_to_date(False))
1142 self.wallet.interface.register_callback('connected', lambda: self.interface.send([('server.banner',[])],'synchronizer') )
1143 self.was_updated = True
1144 self.running = False
1145 self.lock = threading.Lock()
1148 with self.lock: self.running = False
1149 self.interface.poke('synchronizer')
1151 def is_running(self):
1152 with self.lock: return self.running
1154 def synchronize_wallet(self):
1155 new_addresses = self.wallet.synchronize()
1157 self.subscribe_to_addresses(new_addresses)
1158 self.wallet.up_to_date = False
1161 if not self.interface.is_up_to_date('synchronizer'):
1162 if self.wallet.is_up_to_date():
1163 self.wallet.set_up_to_date(False)
1164 self.was_updated = True
1167 self.wallet.set_up_to_date(True)
1168 self.was_updated = True
1171 def subscribe_to_addresses(self, addresses):
1173 for addr in addresses:
1174 messages.append(('blockchain.address.subscribe', [addr]))
1175 self.interface.send( messages, 'synchronizer')
1179 with self.lock: self.running = True
1183 requested_histories = {}
1185 # request any missing transactions
1186 for history in self.wallet.history.values():
1187 if history == ['*']: continue
1188 for tx_hash, tx_height in history:
1189 if self.wallet.transactions.get(tx_hash) is None and (tx_hash, tx_height) not in missing_tx:
1190 missing_tx.append( (tx_hash, tx_height) )
1191 print_error("missing tx", missing_tx)
1193 # wait until we are connected, in case the user is not connected
1194 while not self.interface.is_connected:
1197 # request banner, because 'connected' event happens before this thread is started
1198 self.interface.send([('server.banner',[])],'synchronizer')
1201 self.subscribe_to_addresses(self.wallet.addresses(True))
1203 while self.is_running():
1204 # 1. send new requests
1205 self.synchronize_wallet()
1207 for tx_hash, tx_height in missing_tx:
1208 if (tx_hash, tx_height) not in requested_tx:
1209 self.interface.send([ ('blockchain.transaction.get',[tx_hash, tx_height]) ], 'synchronizer')
1210 requested_tx.append( (tx_hash, tx_height) )
1213 if self.was_updated:
1214 self.interface.trigger_callback('updated')
1215 self.was_updated = False
1218 r = self.interface.get_response('synchronizer')
1220 # poke sends None. (needed during stop)
1223 # 3. handle response
1224 method = r['method']
1225 params = r['params']
1226 result = r.get('result')
1227 error = r.get('error')
1232 if method == 'blockchain.address.subscribe':
1234 if self.wallet.get_status(self.wallet.get_history(addr)) != result:
1235 if requested_histories.get(addr) is None:
1236 self.interface.send([('blockchain.address.get_history', [addr])], 'synchronizer')
1237 requested_histories[addr] = result
1239 elif method == 'blockchain.address.get_history':
1241 print_error("receiving history", addr, result)
1243 assert requested_histories.pop(addr) == '*'
1244 self.wallet.receive_history_callback(addr, result)
1247 # check that txids are unique
1250 tx_hash = item['tx_hash']
1251 if tx_hash not in txids:
1252 txids.append(tx_hash)
1253 hist.append( (tx_hash, item['height']) )
1255 if len(hist) != len(result):
1256 raise BaseException("error: server sent history with non-unique txid", result)
1258 # check that the status corresponds to what was announced
1259 rs = requested_histories.pop(addr)
1260 if self.wallet.get_status(hist) != rs:
1261 raise BaseException("error: status mismatch: %s"%addr)
1263 # store received history
1264 self.wallet.receive_history_callback(addr, hist)
1266 # request transactions that we don't have
1267 for tx_hash, tx_height in hist:
1268 if self.wallet.transactions.get(tx_hash) is None:
1269 if (tx_hash, tx_height) not in requested_tx and (tx_hash, tx_height) not in missing_tx:
1270 missing_tx.append( (tx_hash, tx_height) )
1272 elif method == 'blockchain.transaction.get':
1274 tx_height = params[1]
1275 assert tx_hash == hash_encode(Hash(result.decode('hex')))
1276 tx = Transaction(result)
1277 self.wallet.receive_tx_callback(tx_hash, tx, tx_height)
1278 self.was_updated = True
1279 requested_tx.remove( (tx_hash, tx_height) )
1280 print_error("received tx:", tx)
1282 elif method == 'blockchain.transaction.broadcast':
1283 self.wallet.tx_result = result
1284 self.wallet.tx_event.set()
1286 elif method == 'server.banner':
1287 self.wallet.banner = result
1288 self.interface.trigger_callback('banner')
1290 print_error("Error: Unknown message:" + method + ", " + repr(params) + ", " + repr(result) )
1292 if self.was_updated and not requested_tx:
1293 self.interface.trigger_callback('updated')
1294 self.was_updated = False