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, 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
68 def __init__(self, config):
70 self.file_exists = False
71 self.init_path(config)
72 print_error( "wallet path", self.path )
77 def init_path(self, config):
78 """Set the path of the wallet."""
80 path = config.get('wallet_path')
82 path = config.get('default_wallet_path')
87 self.path = os.path.join(config.path, "electrum.dat")
91 """Read the contents of the wallet file."""
93 with open(self.path, "r") as f:
98 d = ast.literal_eval( data ) #parse raw data from reading wallet file
100 raise IOError("Cannot read wallet file.")
103 self.file_exists = True
106 def get(self, key, default=None):
107 return self.data.get(key, default)
109 def put(self, key, value, save = True):
111 if self.data.get(key) is not None:
112 self.data[key] = value
114 # add key to wallet config
115 self.data[key] = value
123 f = open(self.path,"w")
126 if self.get('gui') != 'android':
128 os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)
133 def __init__(self, storage):
135 self.storage = storage
136 self.electrum_version = ELECTRUM_VERSION
137 self.gap_limit_for_change = 3 # constant
140 self.seed_version = storage.get('seed_version', SEED_VERSION)
142 self.gap_limit = storage.get('gap_limit', 5)
143 self.use_change = storage.get('use_change',True)
144 self.use_encryption = storage.get('use_encryption', False)
145 self.seed = storage.get('seed', '') # encrypted
146 self.labels = storage.get('labels', {})
147 self.frozen_addresses = storage.get('frozen_addresses',[])
148 self.prioritized_addresses = storage.get('prioritized_addresses',[])
149 self.addressbook = storage.get('contacts', [])
151 self.imported_keys = storage.get('imported_keys',{})
152 self.history = storage.get('addr_history',{}) # address -> list(txid, height)
154 self.fee = int(storage.get('fee_per_kb',20000))
156 self.master_public_keys = storage.get('master_public_keys',{})
157 self.master_private_keys = storage.get('master_private_keys', {})
159 self.first_addresses = storage.get('first_addresses',{})
161 #if self.seed_version != SEED_VERSION:
162 # raise ValueError("This wallet seed is deprecated. Please restore from seed.")
166 self.transactions = {}
167 tx = storage.get('transactions',{})
169 for k,v in tx.items(): self.transactions[k] = Transaction(v)
171 print_msg("Warning: Cannot deserialize transactions. skipping")
174 self.prevout_values = {} # my own transaction outputs
175 self.spent_outputs = []
180 # there is a difference between wallet.up_to_date and interface.is_up_to_date()
181 # interface.is_up_to_date() returns true when all requests have been answered and processed
182 # wallet.up_to_date is true when the wallet is synchronized (stronger requirement)
184 self.up_to_date = False
185 self.lock = threading.Lock()
186 self.transaction_lock = threading.Lock()
187 self.tx_event = threading.Event()
189 for tx_hash, tx in self.transactions.items():
190 if self.check_new_tx(tx_hash, tx):
191 self.update_tx_outputs(tx_hash)
193 print_error("unreferenced tx", tx_hash)
194 self.transactions.pop(tx_hash)
197 def set_up_to_date(self,b):
198 with self.lock: self.up_to_date = b
200 def is_up_to_date(self):
201 with self.lock: return self.up_to_date
204 self.up_to_date = False
205 self.interface.poke('synchronizer')
206 while not self.is_up_to_date(): time.sleep(0.1)
208 def import_key(self, sec, password):
210 seed = self.decode_seed(password)
212 address = address_from_private_key(sec)
214 raise BaseException('Invalid private key')
216 if self.is_mine(address):
217 raise BaseException('Address already in wallet')
219 # store the originally requested keypair into the imported keys table
220 self.imported_keys[address] = pw_encode(sec, password )
221 self.storage.put('imported_keys', self.imported_keys, True)
224 def delete_imported_key(self, addr):
225 if addr in self.imported_keys:
226 self.imported_keys.pop(addr)
227 self.storage.put('imported_keys', self.imported_keys, True)
230 def init_seed(self, seed):
231 if self.seed: raise BaseException("a seed exists")
233 seed = random_seed(128)
238 self.storage.put('seed', self.seed, True)
239 self.storage.put('seed_version', self.seed_version, True)
241 master_k, master_c, master_K, master_cK = bip32_init(self.seed)
244 k0, c0, K0, cK0 = bip32_private_derivation(master_k, master_c, "m/", "m/0'/")
246 k1, c1, K1, cK1 = bip32_private_derivation(master_k, master_c, "m/", "m/1'/")
247 k2, c2, K2, cK2 = bip32_private_derivation(master_k, master_c, "m/", "m/2'/")
249 k3, c3, K3, cK3 = bip32_private_derivation(master_k, master_c, "m/", "m/3'/")
250 k4, c4, K4, cK4 = bip32_private_derivation(master_k, master_c, "m/", "m/4'/")
251 k5, c5, K5, cK5 = bip32_private_derivation(master_k, master_c, "m/", "m/5'/")
253 self.master_public_keys = {
254 "m/0'/": (c0, K0, cK0),
255 "m/1'/": (c1, K1, cK1),
256 "m/2'/": (c2, K2, cK2),
257 "m/3'/": (c3, K3, cK3),
258 "m/4'/": (c4, K4, cK4),
259 "m/5'/": (c5, K5, cK5)
262 self.master_private_keys = {
271 self.storage.put('master_public_keys', self.master_public_keys, True)
272 self.storage.put('master_private_keys', self.master_private_keys, True)
274 # create default account
275 self.create_account('1','Main account')
278 def find_root_by_master_key(self, c, K):
279 for key, v in self.master_public_keys.items():
280 if key == "m/":continue
282 if (c == cc) and (K == KK):
285 def deseed_root(self, seed, password):
286 # for safety, we ask the user to enter their seed
287 assert seed == self.decode_seed(password)
289 self.storage.put('seed', '', True)
292 def deseed_branch(self, k):
293 # check that parent has no seed
294 assert self.seed == ''
295 self.master_private_keys.pop(k)
296 self.storage.put('master_private_keys', self.master_private_keys, True)
299 def account_id(self, account_type, i):
300 if account_type == '1':
302 elif account_type == '2of2':
303 return "m/1'/%d & m/2'/%d"%(i,i)
304 elif account_type == '2of3':
305 return "m/3'/%d & m/4'/%d & m/5'/%d"%(i,i,i)
307 raise BaseException('unknown account type')
310 def num_accounts(self, account_type):
311 keys = self.accounts.keys()
314 account_id = self.account_id(account_type, i)
315 if account_id not in keys: break
320 def new_account_address(self, account_type = '1'):
321 i = self.num_accounts(account_type)
322 k = self.account_id(account_type,i)
324 addr = self.first_addresses.get(k)
326 account_id, account = self.next_account(account_type)
327 addr = account.first_address()
328 self.first_addresses[k] = addr
329 self.storage.put('first_addresses',self.first_addresses)
334 def next_account(self, account_type = '1'):
336 i = self.num_accounts(account_type)
337 account_id = self.account_id(account_type,i)
339 if account_type is '1':
340 master_c0, master_K0, _ = self.master_public_keys["m/0'/"]
341 c0, K0, cK0 = bip32_public_derivation(master_c0.decode('hex'), master_K0.decode('hex'), "m/0'/", "m/0'/%d"%i)
342 account = BIP32_Account({ 'c':c0, 'K':K0, 'cK':cK0 })
344 elif account_type == '2of2':
345 master_c1, master_K1, _ = self.master_public_keys["m/1'/"]
346 c1, K1, cK1 = bip32_public_derivation(master_c1.decode('hex'), master_K1.decode('hex'), "m/1'/", "m/1'/%d"%i)
347 master_c2, master_K2, _ = self.master_public_keys["m/2'/"]
348 c2, K2, cK2 = bip32_public_derivation(master_c2.decode('hex'), master_K2.decode('hex'), "m/2'/", "m/2'/%d"%i)
349 account = BIP32_Account_2of2({ 'c':c1, 'K':K1, 'cK':cK1, 'c2':c2, 'K2':K2, 'cK2':cK2 })
351 elif account_type == '2of3':
352 master_c3, master_K3, _ = self.master_public_keys["m/3'/"]
353 c3, K3, cK3 = bip32_public_derivation(master_c3.decode('hex'), master_K3.decode('hex'), "m/3'/", "m/3'/%d"%i)
354 master_c4, master_K4, _ = self.master_public_keys["m/4'/"]
355 c4, K4, cK4 = bip32_public_derivation(master_c4.decode('hex'), master_K4.decode('hex'), "m/4'/", "m/4'/%d"%i)
356 master_c5, master_K5, _ = self.master_public_keys["m/5'/"]
357 c5, K5, cK5 = bip32_public_derivation(master_c5.decode('hex'), master_K5.decode('hex'), "m/5'/", "m/5'/%d"%i)
358 account = BIP32_Account_2of3({ 'c':c3, 'K':K3, 'cK':cK3, 'c2':c4, 'K2':K4, 'cK2':cK4, 'c3':c5, 'K3':K5, 'cK3':cK5 })
360 return account_id, account
363 def create_account(self, account_type = '1', name = None):
364 account_id, account = self.next_account(account_type)
365 self.accounts[account_id] = account
368 self.labels[account_id] = name
369 self.storage.put('labels', self.labels, True)
372 def create_old_account(self):
374 mpk = OldAccount.mpk_from_seed(self.seed)
375 self.storage.put('master_public_key', mpk, True)
376 self.accounts[0] = OldAccount({'mpk':mpk, 0:[], 1:[]})
380 def save_accounts(self):
382 for k, v in self.accounts.items():
384 self.storage.put('accounts', d, True)
388 def load_accounts(self):
389 d = self.storage.get('accounts', {})
391 for k, v in d.items():
393 v['mpk'] = self.storage.get('master_public_key')
394 self.accounts[k] = OldAccount(v)
396 self.accounts[k] = BIP32_Account_2of2(v)
398 self.accounts[k] = BIP32_Account(v)
401 def addresses(self, include_change = True):
402 o = self.get_account_addresses(-1, include_change)
403 for a in self.accounts.keys():
404 o += self.get_account_addresses(a, include_change)
405 o += self.first_addresses.values()
409 def is_mine(self, address):
410 return address in self.addresses(True)
413 def is_change(self, address):
414 if not self.is_mine(address): return False
415 if address in self.imported_keys.keys(): return False
416 acct, s = self.get_address_index(address)
419 def get_master_public_key(self):
421 return self.storage.get("master_public_key")
423 def get_master_private_key(self, account, password):
424 master_k = pw_decode( self.master_private_keys[account], password)
425 master_c, master_K, master_Kc = self.master_public_keys[account]
427 K, Kc = get_pubkeys_from_secret(master_k.decode('hex'))
428 assert K.encode('hex') == master_K
430 raise BaseException("Invalid password")
434 def get_address_index(self, address):
435 if address in self.imported_keys.keys():
438 for account in self.accounts.keys():
439 for for_change in [0,1]:
440 addresses = self.accounts[account].get_addresses(for_change)
441 for addr in addresses:
443 return account, (for_change, addresses.index(addr))
445 raise BaseException("not found")
448 def rebase_sequence(self, account, sequence):
451 for a in account.split('&'):
453 m = re.match("(m/\d+'/)(\d+)", s)
455 num = int(m.group(2))
456 dd.append( (root, [num,c,i] ) )
460 def get_keyID(self, account, sequence):
464 rs = self.rebase_sequence(account, sequence)
466 for root, public_sequence in rs:
467 c, K, _ = self.master_public_keys[root]
468 s = '/' + '/'.join( map(lambda x:str(x), public_sequence) )
469 dd.append( 'bip32(%s,%s,%s)'%(c,K, s) )
473 def get_public_key(self, address):
474 account, sequence = self.get_address_index(address)
475 return self.accounts[account].get_pubkey( *sequence )
478 def decode_seed(self, password):
479 seed = pw_decode(self.seed, password)
480 #todo: #self.sequences[0].check_seed(seed)
484 def get_private_key(self, address, password):
486 if address in self.imported_keys.keys():
487 out.append( pw_decode( self.imported_keys[address], password ) )
489 account, sequence = self.get_address_index(address)
491 seed = self.decode_seed(password)
492 pk = self.accounts[account].get_private_key(seed, sequence)
496 # assert address == self.accounts[account].get_address(*sequence)
497 rs = self.rebase_sequence( account, sequence)
498 for root, public_sequence in rs:
500 if root not in self.master_private_keys.keys(): continue
501 master_k = self.get_master_private_key(root, password)
502 master_c, _, _ = self.master_public_keys[root]
503 pk = bip32_private_key( public_sequence, master_k.decode('hex'), master_c.decode('hex'))
511 def signrawtransaction(self, tx, input_info, private_keys, password):
513 unspent_coins = self.get_unspent_coins()
514 seed = self.decode_seed(password)
516 # build a list of public/private keys
518 for sec in private_keys:
519 pubkey = public_key_from_private_key(sec)
520 keypairs[ pubkey ] = sec
523 for txin in tx.inputs:
524 # convert to own format
525 txin['tx_hash'] = txin['prevout_hash']
526 txin['index'] = txin['prevout_n']
528 for item in input_info:
529 if item.get('txid') == txin['tx_hash'] and item.get('vout') == txin['index']:
530 txin['raw_output_script'] = item['scriptPubKey']
531 txin['redeemScript'] = item.get('redeemScript')
532 txin['KeyID'] = item.get('KeyID')
535 for item in unspent_coins:
536 if txin['tx_hash'] == item['tx_hash'] and txin['index'] == item['index']:
537 print_error( "tx input is in unspent coins" )
538 txin['raw_output_script'] = item['raw_output_script']
539 account, sequence = self.get_address_index(item['address'])
541 txin['redeemScript'] = self.accounts[account].redeem_script(sequence)
544 raise BaseException("Unknown transaction input. Please provide the 'input_info' parameter, or synchronize this wallet")
546 # if available, derive private_keys from KeyID
547 keyid = txin.get('KeyID')
550 for s in keyid.split('&'):
551 m = re.match("bip32\(([0-9a-f]+),([0-9a-f]+),(/\d+/\d+/\d+)", s)
555 sequence = m.group(3)
556 root = self.find_root_by_master_key(c,K)
557 if not root: continue
558 sequence = map(lambda x:int(x), sequence.strip('/').split('/'))
559 root = root + '%d'%sequence[0]
560 sequence = sequence[1:]
561 roots.append((root,sequence))
563 account_id = " & ".join( map(lambda x:x[0], roots) )
564 account = self.accounts.get(account_id)
565 if not account: continue
566 addr = account.get_address(*sequence)
567 txin['address'] = addr
568 pk = self.get_private_key(addr, password)
570 pubkey = public_key_from_private_key(sec)
571 keypairs[pubkey] = sec
573 redeem_script = txin.get("redeemScript")
574 print_error( "p2sh:", "yes" if redeem_script else "no")
576 addr = hash_160_to_bc_address(hash_160(redeem_script.decode('hex')), 5)
578 addr = deserialize.get_address_from_output_script(txin["raw_output_script"].decode('hex'))
579 txin['address'] = addr
581 # add private keys that are in the wallet
582 pk = self.get_private_key(addr, password)
584 pubkey = public_key_from_private_key(sec)
585 keypairs[pubkey] = sec
586 if not redeem_script:
587 txin['redeemPubkey'] = pubkey
593 def sign_message(self, address, message, password):
594 sec = self.get_private_key(address, password)
595 key = regenerate_key(sec)
596 compressed = is_compressed(sec)
597 return key.sign_message(message, compressed, address)
599 def verify_message(self, address, signature, message):
601 EC_KEY.verify_message(address, signature, message)
603 except BaseException as e:
604 print_error("Verification error: {0}".format(e))
608 def change_gap_limit(self, value):
609 if value >= self.gap_limit:
610 self.gap_limit = value
611 self.storage.put('gap_limit', self.gap_limit, True)
612 self.interface.poke('synchronizer')
615 elif value >= self.min_acceptable_gap():
616 for key, account in self.accounts.items():
617 addresses = account[0]
618 k = self.num_unused_trailing_addresses(addresses)
619 n = len(addresses) - k + value
620 addresses = addresses[0:n]
621 self.accounts[key][0] = addresses
623 self.gap_limit = value
624 self.storage.put('gap_limit', self.gap_limit, True)
630 def num_unused_trailing_addresses(self, addresses):
632 for a in addresses[::-1]:
633 if self.history.get(a):break
637 def min_acceptable_gap(self):
638 # fixme: this assumes wallet is synchronized
642 for account in self.accounts.values():
643 addresses = account.get_addresses(0)
644 k = self.num_unused_trailing_addresses(addresses)
645 for a in addresses[0:-k]:
646 if self.history.get(a):
650 if n > nmax: nmax = n
654 def address_is_old(self, address):
656 h = self.history.get(address, [])
659 for tx_hash, tx_height in h:
663 tx_age = self.verifier.blockchain.height - tx_height + 1
669 def synchronize_sequence(self, account, for_change):
670 limit = self.gap_limit_for_change if for_change else self.gap_limit
673 addresses = account.get_addresses(for_change)
674 if len(addresses) < limit:
675 address = account.create_new_address(for_change)
676 self.history[address] = []
677 new_addresses.append( address )
680 if map( lambda a: self.address_is_old(a), addresses[-limit:] ) == limit*[False]:
683 address = account.create_new_address(for_change)
684 self.history[address] = []
685 new_addresses.append( address )
691 def create_pending_accounts(self):
692 for account_type in ['1','2of2','2of3']:
693 a = self.new_account_address(account_type)
694 if self.address_is_old(a):
695 print "creating account", a
696 self.create_account(account_type)
699 def synchronize_account(self, account):
701 new += self.synchronize_sequence(account, 0)
702 new += self.synchronize_sequence(account, 1)
706 def synchronize(self):
707 if self.master_public_keys:
708 self.create_pending_accounts()
710 for account in self.accounts.values():
711 new += self.synchronize_account(account)
714 self.storage.put('addr_history', self.history, True)
719 return self.history.values() != [[]] * len(self.history)
722 def add_contact(self, address, label=None):
723 self.addressbook.append(address)
724 self.storage.put('contacts', self.addressbook, True)
726 self.labels[address] = label
727 self.storage.put('labels', self.labels, True)
729 def delete_contact(self, addr):
730 if addr in self.addressbook:
731 self.addressbook.remove(addr)
732 self.storage.put('addressbook', self.addressbook, True)
735 def fill_addressbook(self):
736 for tx_hash, tx in self.transactions.items():
737 is_relevant, is_send, _, _ = self.get_tx_value(tx)
739 for addr, v in tx.outputs:
740 if not self.is_mine(addr) and addr not in self.addressbook:
741 self.addressbook.append(addr)
743 # self.update_tx_labels()
745 def get_num_tx(self, address):
747 for tx in self.transactions.values():
748 if address in map(lambda x:x[0], tx.outputs): n += 1
752 def get_address_flags(self, addr):
753 flags = "C" if self.is_change(addr) else "I" if addr in self.imported_keys.keys() else "-"
754 flags += "F" if addr in self.frozen_addresses else "P" if addr in self.prioritized_addresses else "-"
758 def get_tx_value(self, tx, account=None):
759 domain = self.get_account_addresses(account)
760 return tx.get_value(domain, self.prevout_values)
763 def update_tx_outputs(self, tx_hash):
764 tx = self.transactions.get(tx_hash)
766 for i, (addr, value) in enumerate(tx.outputs):
767 key = tx_hash+ ':%d'%i
768 self.prevout_values[key] = value
770 for item in tx.inputs:
771 if self.is_mine(item.get('address')):
772 key = item['prevout_hash'] + ':%d'%item['prevout_n']
773 self.spent_outputs.append(key)
776 def get_addr_balance(self, address):
777 assert self.is_mine(address)
778 h = self.history.get(address,[])
779 if h == ['*']: return 0,0
781 received_coins = [] # list of coins received at address
783 for tx_hash, tx_height in h:
784 tx = self.transactions.get(tx_hash)
787 for i, (addr, value) in enumerate(tx.outputs):
789 key = tx_hash + ':%d'%i
790 received_coins.append(key)
792 for tx_hash, tx_height in h:
793 tx = self.transactions.get(tx_hash)
797 for item in tx.inputs:
798 addr = item.get('address')
800 key = item['prevout_hash'] + ':%d'%item['prevout_n']
801 value = self.prevout_values.get( key )
802 if key in received_coins:
805 for i, (addr, value) in enumerate(tx.outputs):
806 key = tx_hash + ':%d'%i
817 def get_accounts(self):
819 for k, account in self.accounts.items():
820 accounts[k] = self.labels.get(k, 'unnamed')
821 if self.imported_keys:
822 accounts[-1] = 'Imported keys'
825 def get_account_addresses(self, a, include_change=True):
827 o = self.addresses(True)
829 o = self.imported_keys.keys()
831 ac = self.accounts[a]
832 o = ac.get_addresses(0)
833 if include_change: o += ac.get_addresses(1)
836 def get_imported_balance(self):
838 for addr in self.imported_keys.keys():
839 c, u = self.get_addr_balance(addr)
844 def get_account_balance(self, account):
846 return self.get_balance()
848 return self.get_imported_balance()
851 for addr in self.get_account_addresses(account):
852 c, u = self.get_addr_balance(addr)
857 def get_frozen_balance(self):
859 for addr in self.frozen_addresses:
860 c, u = self.get_addr_balance(addr)
866 def get_balance(self):
868 for a in self.accounts.keys():
869 c, u = self.get_account_balance(a)
872 c, u = self.get_imported_balance()
878 def get_unspent_coins(self, domain=None):
880 if domain is None: domain = self.addresses(True)
882 h = self.history.get(addr, [])
883 if h == ['*']: continue
884 for tx_hash, tx_height in h:
885 tx = self.transactions.get(tx_hash)
886 if tx is None: raise BaseException("Wallet not synchronized")
887 for output in tx.d.get('outputs'):
888 if output.get('address') != addr: continue
889 key = tx_hash + ":%d" % output.get('index')
890 if key in self.spent_outputs: continue
891 output['tx_hash'] = tx_hash
897 def choose_tx_inputs( self, amount, fixed_fee, account = None ):
898 """ todo: minimize tx size """
900 fee = self.fee if fixed_fee is None else fixed_fee
901 domain = self.get_account_addresses(account)
903 prioritized_coins = []
904 for i in self.frozen_addresses:
905 if i in domain: domain.remove(i)
907 for i in self.prioritized_addresses:
908 if i in domain: domain.remove(i)
910 coins = self.get_unspent_coins(domain)
911 prioritized_coins = self.get_unspent_coins(self.prioritized_addresses)
914 coins = prioritized_coins + coins
917 addr = item.get('address')
918 v = item.get('value')
920 inputs.append( item )
921 fee = self.estimated_fee(inputs) if fixed_fee is None else fixed_fee
922 if total >= amount + fee: break
926 return inputs, total, fee
929 def set_fee(self, fee):
932 self.storage.put('fee_per_kb', self.fee, True)
934 def estimated_fee(self, inputs):
935 estimated_size = len(inputs) * 180 + 80 # this assumes non-compressed keys
936 fee = self.fee * int(round(estimated_size/1024.))
937 if fee == 0: fee = self.fee
941 def add_tx_change( self, inputs, outputs, amount, fee, total, change_addr=None, account=0 ):
942 "add change to a transaction"
943 change_amount = total - ( amount + fee )
944 if change_amount != 0:
947 # send change to one of the accounts involved in the tx
948 address = inputs[0].get('address')
949 account, _ = self.get_address_index(address)
951 if not self.use_change or account == -1:
952 change_addr = inputs[-1]['address']
954 change_addr = self.accounts[account].get_addresses(1)[-self.gap_limit_for_change]
956 # Insert the change output at a random position in the outputs
957 posn = random.randint(0, len(outputs))
958 outputs[posn:posn] = [( change_addr, change_amount)]
962 def get_history(self, address):
964 return self.history.get(address)
967 def get_status(self, h):
968 if not h: return None
969 if h == ['*']: return '*'
971 for tx_hash, height in h:
972 status += tx_hash + ':%d:' % height
973 return hashlib.sha256( status ).digest().encode('hex')
976 def receive_tx_callback(self, tx_hash, tx, tx_height):
977 if not self.check_new_tx(tx_hash, tx):
978 # may happen due to pruning
979 print_error("received transaction that is no longer referenced in history", tx_hash)
982 with self.transaction_lock:
983 self.transactions[tx_hash] = tx
985 self.interface.pending_transactions_for_notifications.append(tx)
987 self.save_transactions()
988 if self.verifier and tx_height>0:
989 self.verifier.add(tx_hash, tx_height)
990 self.update_tx_outputs(tx_hash)
993 def save_transactions(self):
995 for k,v in self.transactions.items():
997 self.storage.put('transactions', tx, True)
999 def receive_history_callback(self, addr, hist):
1001 if not self.check_new_history(addr, hist):
1002 raise BaseException("error: received history for %s is not consistent with known transactions"%addr)
1005 self.history[addr] = hist
1006 self.storage.put('addr_history', self.history, True)
1009 for tx_hash, tx_height in hist:
1011 # add it in case it was previously unconfirmed
1012 if self.verifier: self.verifier.add(tx_hash, tx_height)
1015 def get_tx_history(self, account=None):
1016 with self.transaction_lock:
1017 history = self.transactions.items()
1018 history.sort(key = lambda x: self.verifier.get_txpos(x[0]))
1022 for tx_hash, tx in history:
1023 is_relevant, is_mine, v, fee = self.get_tx_value(tx, account)
1024 if v is not None: balance += v
1026 c, u = self.get_account_balance(account)
1029 result.append( ('', 1000, 0, c+u-balance, None, c+u-balance, None ) )
1031 balance = c + u - balance
1032 for tx_hash, tx in history:
1033 is_relevant, is_mine, value, fee = self.get_tx_value(tx, account)
1036 if value is not None:
1039 conf, timestamp = self.verifier.get_confirmations(tx_hash) if self.verifier else (None, None)
1040 result.append( (tx_hash, conf, is_mine, value, fee, balance, timestamp) )
1045 def get_label(self, tx_hash):
1046 label = self.labels.get(tx_hash)
1047 is_default = (label == '') or (label is None)
1048 if is_default: label = self.get_default_label(tx_hash)
1049 return label, is_default
1052 def get_default_label(self, tx_hash):
1053 tx = self.transactions.get(tx_hash)
1056 is_relevant, is_mine, _, _ = self.get_tx_value(tx)
1058 for o in tx.outputs:
1060 if not self.is_mine(o_addr):
1062 default_label = self.labels[o_addr]
1064 default_label = o_addr
1067 default_label = '(internal)'
1069 for o in tx.outputs:
1071 if self.is_mine(o_addr) and not self.is_change(o_addr):
1074 for o in tx.outputs:
1076 if self.is_mine(o_addr):
1082 dest_label = self.labels.get(o_addr)
1084 default_label = self.labels[o_addr]
1086 default_label = o_addr
1088 return default_label
1091 def mktx(self, outputs, password, fee=None, change_addr=None, account=None ):
1093 for address, x in outputs:
1094 assert is_valid(address)
1096 amount = sum( map(lambda x:x[1], outputs) )
1098 inputs, total, fee = self.choose_tx_inputs( amount, fee, account )
1100 raise ValueError("Not enough funds")
1102 outputs = self.add_tx_change(inputs, outputs, amount, fee, total, change_addr, account)
1103 tx = Transaction.from_io(inputs, outputs)
1106 for i, txin in enumerate(tx.inputs):
1107 address = txin['address']
1109 account, sequence = self.get_address_index(address)
1110 txin['KeyID'] = self.get_keyID(account, sequence)
1112 redeemScript = self.accounts[account].redeem_script(sequence)
1114 txin['redeemScript'] = redeemScript
1116 txin['redeemPubkey'] = self.accounts[account].get_pubkey(*sequence)
1118 private_keys = self.get_private_key(address, password)
1120 for sec in private_keys:
1121 pubkey = public_key_from_private_key(sec)
1122 keypairs[ pubkey ] = sec
1125 for address, x in outputs:
1126 if address not in self.addressbook and not self.is_mine(address):
1127 self.addressbook.append(address)
1133 def sendtx(self, tx):
1135 h = self.send_tx(tx)
1136 self.tx_event.wait()
1137 return self.receive_tx(h)
1139 def send_tx(self, tx):
1141 self.tx_event.clear()
1142 self.interface.send([('blockchain.transaction.broadcast', [str(tx)])], 'synchronizer')
1145 def receive_tx(self,tx_hash):
1146 out = self.tx_result
1148 return False, "error: " + out
1153 def update_password(self, seed, old_password, new_password):
1154 if new_password == '': new_password = None
1155 # this will throw an exception if unicode cannot be converted
1156 self.seed = pw_encode( seed, new_password)
1157 self.storage.put('seed', self.seed, True)
1158 self.use_encryption = (new_password != None)
1159 self.storage.put('use_encryption', self.use_encryption,True)
1160 for k in self.imported_keys.keys():
1161 a = self.imported_keys[k]
1162 b = pw_decode(a, old_password)
1163 c = pw_encode(b, new_password)
1164 self.imported_keys[k] = c
1165 self.storage.put('imported_keys', self.imported_keys, True)
1167 for k, v in self.master_private_keys.items():
1168 b = pw_decode(v, old_password)
1169 c = pw_encode(b, new_password)
1170 self.master_private_keys[k] = c
1171 self.storage.put('master_private_keys', self.master_private_keys, True)
1174 def freeze(self,addr):
1175 if self.is_mine(addr) and addr not in self.frozen_addresses:
1176 self.unprioritize(addr)
1177 self.frozen_addresses.append(addr)
1178 self.storage.put('frozen_addresses', self.frozen_addresses, True)
1183 def unfreeze(self,addr):
1184 if self.is_mine(addr) and addr in self.frozen_addresses:
1185 self.frozen_addresses.remove(addr)
1186 self.storage.put('frozen_addresses', self.frozen_addresses, True)
1191 def prioritize(self,addr):
1192 if self.is_mine(addr) and addr not in self.prioritized_addresses:
1194 self.prioritized_addresses.append(addr)
1195 self.storage.put('prioritized_addresses', self.prioritized_addresses, True)
1200 def unprioritize(self,addr):
1201 if self.is_mine(addr) and addr in self.prioritized_addresses:
1202 self.prioritized_addresses.remove(addr)
1203 self.storage.put('prioritized_addresses', self.prioritized_addresses, True)
1209 def set_verifier(self, verifier):
1210 self.verifier = verifier
1212 # review transactions that are in the history
1213 for addr, hist in self.history.items():
1214 if hist == ['*']: continue
1215 for tx_hash, tx_height in hist:
1217 # add it in case it was previously unconfirmed
1218 self.verifier.add(tx_hash, tx_height)
1221 # if we are on a pruning server, remove unverified transactions
1222 vr = self.verifier.transactions.keys() + self.verifier.verified_tx.keys()
1223 for tx_hash in self.transactions.keys():
1224 if tx_hash not in vr:
1225 self.transactions.pop(tx_hash)
1229 def check_new_history(self, addr, hist):
1231 # check that all tx in hist are relevant
1233 for tx_hash, height in hist:
1234 tx = self.transactions.get(tx_hash)
1236 if not tx.has_address(addr):
1239 # check that we are not "orphaning" a transaction
1240 old_hist = self.history.get(addr,[])
1241 if old_hist == ['*']: return True
1243 for tx_hash, height in old_hist:
1244 if tx_hash in map(lambda x:x[0], hist): continue
1246 for _addr, _hist in self.history.items():
1247 if _addr == addr: continue
1248 if _hist == ['*']: continue
1249 _tx_hist = map(lambda x:x[0], _hist)
1250 if tx_hash in _tx_hist:
1255 tx = self.transactions.get(tx_hash)
1256 # tx might not be there
1260 if self.verifier.get_height(tx_hash):
1263 print_error("new history is orphaning transaction:", tx_hash)
1264 # check that all outputs are not mine, request histories
1266 for _addr, _v in tx.outputs:
1267 # assert not self.is_mine(_addr)
1268 ext_requests.append( ('blockchain.address.get_history', [_addr]) )
1270 ext_h = self.interface.synchronous_get(ext_requests)
1271 print_error("sync:", ext_requests, ext_h)
1274 if h == ['*']: continue
1276 if item.get('tx_hash') == tx_hash:
1277 height = item.get('height')
1279 print_error("found height for", tx_hash, height)
1280 self.verifier.add(tx_hash, height)
1282 print_error("removing orphaned tx from history", tx_hash)
1283 self.transactions.pop(tx_hash)
1289 def check_new_tx(self, tx_hash, tx):
1290 # 1 check that tx is referenced in addr_history.
1292 for addr, hist in self.history.items():
1293 if hist == ['*']:continue
1294 for txh, height in hist:
1296 addresses.append(addr)
1301 # 2 check that referencing addresses are in the tx
1302 for addr in addresses:
1303 if not tx.has_address(addr):
1309 def start_threads(self, interface, blockchain):
1310 from verifier import TxVerifier
1311 self.interface = interface
1312 self.verifier = TxVerifier(interface, blockchain, self.storage)
1313 self.verifier.start()
1314 self.set_verifier(self.verifier)
1315 self.synchronizer = WalletSynchronizer(self)
1316 self.synchronizer.start()
1318 def stop_threads(self):
1319 self.verifier.stop()
1320 self.synchronizer.stop()
1325 class WalletSynchronizer(threading.Thread):
1328 def __init__(self, wallet):
1329 threading.Thread.__init__(self)
1331 self.wallet = wallet
1332 wallet.synchronizer = self
1333 self.interface = self.wallet.interface
1334 self.interface.register_channel('synchronizer')
1335 self.wallet.interface.register_callback('connected', lambda: self.wallet.set_up_to_date(False))
1336 self.was_updated = True
1337 self.running = False
1338 self.lock = threading.Lock()
1341 with self.lock: self.running = False
1342 self.interface.poke('synchronizer')
1344 def is_running(self):
1345 with self.lock: return self.running
1348 def subscribe_to_addresses(self, addresses):
1350 for addr in addresses:
1351 messages.append(('blockchain.address.subscribe', [addr]))
1352 self.interface.send( messages, 'synchronizer')
1356 with self.lock: self.running = True
1360 requested_histories = {}
1362 # request any missing transactions
1363 for history in self.wallet.history.values():
1364 if history == ['*']: continue
1365 for tx_hash, tx_height in history:
1366 if self.wallet.transactions.get(tx_hash) is None and (tx_hash, tx_height) not in missing_tx:
1367 missing_tx.append( (tx_hash, tx_height) )
1368 print_error("missing tx", missing_tx)
1370 # wait until we are connected, in case the user is not connected
1371 while not self.interface.is_connected:
1375 self.subscribe_to_addresses(self.wallet.addresses(True))
1377 while self.is_running():
1378 # 1. create new addresses
1379 new_addresses = self.wallet.synchronize()
1381 # request missing addresses
1383 self.subscribe_to_addresses(new_addresses)
1385 # request missing transactions
1386 for tx_hash, tx_height in missing_tx:
1387 if (tx_hash, tx_height) not in requested_tx:
1388 self.interface.send([ ('blockchain.transaction.get',[tx_hash, tx_height]) ], 'synchronizer')
1389 requested_tx.append( (tx_hash, tx_height) )
1392 # detect if situation has changed
1393 if not self.interface.is_up_to_date('synchronizer'):
1394 if self.wallet.is_up_to_date():
1395 self.wallet.set_up_to_date(False)
1396 self.was_updated = True
1398 if not self.wallet.is_up_to_date():
1399 self.wallet.set_up_to_date(True)
1400 self.was_updated = True
1402 if self.was_updated:
1403 self.interface.trigger_callback('updated')
1404 self.was_updated = False
1407 r = self.interface.get_response('synchronizer')
1409 # poke sends None. (needed during stop)
1412 # 3. handle response
1413 method = r['method']
1414 params = r['params']
1415 result = r.get('result')
1416 error = r.get('error')
1421 if method == 'blockchain.address.subscribe':
1423 if self.wallet.get_status(self.wallet.get_history(addr)) != result:
1424 if requested_histories.get(addr) is None:
1425 self.interface.send([('blockchain.address.get_history', [addr])], 'synchronizer')
1426 requested_histories[addr] = result
1428 elif method == 'blockchain.address.get_history':
1430 print_error("receiving history", addr, result)
1432 assert requested_histories.pop(addr) == '*'
1433 self.wallet.receive_history_callback(addr, result)
1436 # check that txids are unique
1439 tx_hash = item['tx_hash']
1440 if tx_hash not in txids:
1441 txids.append(tx_hash)
1442 hist.append( (tx_hash, item['height']) )
1444 if len(hist) != len(result):
1445 raise BaseException("error: server sent history with non-unique txid", result)
1447 # check that the status corresponds to what was announced
1448 rs = requested_histories.pop(addr)
1449 if self.wallet.get_status(hist) != rs:
1450 raise BaseException("error: status mismatch: %s"%addr)
1452 # store received history
1453 self.wallet.receive_history_callback(addr, hist)
1455 # request transactions that we don't have
1456 for tx_hash, tx_height in hist:
1457 if self.wallet.transactions.get(tx_hash) is None:
1458 if (tx_hash, tx_height) not in requested_tx and (tx_hash, tx_height) not in missing_tx:
1459 missing_tx.append( (tx_hash, tx_height) )
1461 elif method == 'blockchain.transaction.get':
1463 tx_height = params[1]
1464 assert tx_hash == hash_encode(Hash(result.decode('hex')))
1465 tx = Transaction(result)
1466 self.wallet.receive_tx_callback(tx_hash, tx, tx_height)
1467 self.was_updated = True
1468 requested_tx.remove( (tx_hash, tx_height) )
1469 print_error("received tx:", tx_hash, len(tx.raw))
1471 elif method == 'blockchain.transaction.broadcast':
1472 self.wallet.tx_result = result
1473 self.wallet.tx_event.set()
1476 print_error("Error: Unknown message:" + method + ", " + repr(params) + ", " + repr(result) )
1478 if self.was_updated and not requested_tx:
1479 self.interface.trigger_callback('updated')
1480 self.interface.trigger_callback("new_transaction") # Updated gets called too many times from other places as well; if we use that signal we get the notification three times
1483 self.was_updated = False