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
36 from transaction import Transaction
37 from plugins import run_hook
40 EncodeAES = lambda secret, s: base64.b64encode(aes.encryptData(secret,s))
41 DecodeAES = lambda secret, e: aes.decryptData(secret, base64.b64decode(e))
43 def pw_encode(s, password):
45 secret = Hash(password)
46 return EncodeAES(secret, s)
50 def pw_decode(s, password):
51 if password is not None:
52 secret = Hash(password)
54 d = DecodeAES(secret, s)
56 raise BaseException('Invalid password')
65 from version import ELECTRUM_VERSION, SEED_VERSION
70 def __init__(self, config):
72 self.file_exists = False
73 self.init_path(config)
74 print_error( "wallet path", self.path )
79 def init_path(self, config):
80 """Set the path of the wallet."""
82 path = config.get('wallet_path')
84 path = config.get('default_wallet_path')
89 self.path = os.path.join(config.path, "electrum.dat")
93 """Read the contents of the wallet file."""
95 with open(self.path, "r") as f:
100 d = ast.literal_eval( data ) #parse raw data from reading wallet file
102 raise IOError("Cannot read wallet file.")
105 self.file_exists = True
108 def get(self, key, default=None):
109 return self.data.get(key, default)
111 def put(self, key, value, save = True):
113 if self.data.get(key) is not None:
114 self.data[key] = value
116 # add key to wallet config
117 self.data[key] = value
125 f = open(self.path,"w")
128 if self.get('gui') != 'android':
130 os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)
135 def __init__(self, storage):
137 self.storage = storage
138 self.electrum_version = ELECTRUM_VERSION
139 self.gap_limit_for_change = 3 # constant
142 self.seed_version = storage.get('seed_version', SEED_VERSION)
144 self.gap_limit = storage.get('gap_limit', 5)
145 self.use_change = storage.get('use_change',True)
146 self.use_encryption = storage.get('use_encryption', False)
147 self.seed = storage.get('seed', '') # encrypted
148 self.labels = storage.get('labels', {})
149 self.frozen_addresses = storage.get('frozen_addresses',[])
150 self.prioritized_addresses = storage.get('prioritized_addresses',[])
151 self.addressbook = storage.get('contacts', [])
153 self.imported_keys = storage.get('imported_keys',{})
154 self.history = storage.get('addr_history',{}) # address -> list(txid, height)
156 self.fee = int(storage.get('fee_per_kb',20000))
158 self.master_public_keys = storage.get('master_public_keys',{})
159 self.master_private_keys = storage.get('master_private_keys', {})
161 self.next_addresses = storage.get('next_addresses',{})
163 if self.seed_version < 4:
164 raise ValueError("This wallet seed is deprecated.")
168 self.transactions = {}
169 tx_list = self.storage.get('transactions',{})
170 for k,v in tx_list.items():
174 print_msg("Warning: Cannot deserialize transactions. skipping")
177 self.add_extra_addresses(tx)
178 self.transactions[k] = tx
180 for h,tx in self.transactions.items():
181 if not self.check_new_tx(h, tx):
182 print_error("removing unreferenced tx", h)
183 self.transactions.pop(h)
187 self.prevout_values = {} # my own transaction outputs
188 self.spent_outputs = []
193 # there is a difference between wallet.up_to_date and interface.is_up_to_date()
194 # interface.is_up_to_date() returns true when all requests have been answered and processed
195 # wallet.up_to_date is true when the wallet is synchronized (stronger requirement)
197 self.up_to_date = False
198 self.lock = threading.Lock()
199 self.transaction_lock = threading.Lock()
200 self.tx_event = threading.Event()
202 for tx_hash, tx in self.transactions.items():
203 self.update_tx_outputs(tx_hash)
206 def add_extra_addresses(self, tx):
208 # find the address corresponding to pay-to-pubkey inputs
209 tx.add_extra_addresses(self.transactions)
210 for o in tx.d.get('outputs'):
211 if o.get('is_pubkey'):
212 for tx2 in self.transactions.values():
213 tx2.add_extra_addresses({h:tx})
218 def set_up_to_date(self,b):
219 with self.lock: self.up_to_date = b
222 def is_up_to_date(self):
223 with self.lock: return self.up_to_date
227 self.up_to_date = False
228 while not self.is_up_to_date():
232 def import_key(self, sec, password):
234 seed = self.decode_seed(password)
236 address = address_from_private_key(sec)
238 raise BaseException('Invalid private key')
240 if self.is_mine(address):
241 raise BaseException('Address already in wallet')
243 # store the originally requested keypair into the imported keys table
244 self.imported_keys[address] = pw_encode(sec, password )
245 self.storage.put('imported_keys', self.imported_keys, True)
246 if self.synchronizer:
247 self.synchronizer.subscribe_to_addresses([address])
250 def delete_imported_key(self, addr):
251 if addr in self.imported_keys:
252 self.imported_keys.pop(addr)
253 self.storage.put('imported_keys', self.imported_keys, True)
256 def init_seed(self, seed):
257 if self.seed: raise BaseException("a seed exists")
259 seed = random_seed(128)
264 self.storage.put('seed', self.seed, True)
265 self.storage.put('seed_version', self.seed_version, True)
267 def create_watching_only_wallet(self, c0, K0):
269 self.master_public_keys = {
270 "m/0'/": (c0, K0, cK0),
272 self.storage.put('master_public_keys', self.master_public_keys, True)
273 self.create_account('1','Main account')
275 def create_accounts(self):
277 master_k, master_c, master_K, master_cK = bip32_init(self.seed)
280 k0, c0, K0, cK0 = bip32_private_derivation(master_k, master_c, "m/", "m/0'/")
282 k1, c1, K1, cK1 = bip32_private_derivation(master_k, master_c, "m/", "m/1'/")
283 k2, c2, K2, cK2 = bip32_private_derivation(master_k, master_c, "m/", "m/2'/")
285 k3, c3, K3, cK3 = bip32_private_derivation(master_k, master_c, "m/", "m/3'/")
286 k4, c4, K4, cK4 = bip32_private_derivation(master_k, master_c, "m/", "m/4'/")
287 k5, c5, K5, cK5 = bip32_private_derivation(master_k, master_c, "m/", "m/5'/")
289 self.master_public_keys = {
290 "m/0'/": (c0, K0, cK0),
291 "m/1'/": (c1, K1, cK1),
292 "m/2'/": (c2, K2, cK2),
293 "m/3'/": (c3, K3, cK3),
294 "m/4'/": (c4, K4, cK4),
295 "m/5'/": (c5, K5, cK5)
298 self.master_private_keys = {
307 self.storage.put('master_public_keys', self.master_public_keys, True)
308 self.storage.put('master_private_keys', self.master_private_keys, True)
310 # create default account
311 self.create_account('1','Main account')
314 def find_root_by_master_key(self, c, K):
315 for key, v in self.master_public_keys.items():
316 if key == "m/":continue
318 if (c == cc) and (K == KK):
321 def deseed_root(self, seed, password):
322 # for safety, we ask the user to enter their seed
323 assert seed == self.decode_seed(password)
325 self.storage.put('seed', '', True)
328 def deseed_branch(self, k):
329 # check that parent has no seed
330 assert self.seed == ''
331 self.master_private_keys.pop(k)
332 self.storage.put('master_private_keys', self.master_private_keys, True)
335 def account_id(self, account_type, i):
336 if account_type == '1':
338 elif account_type == '2of2':
339 return "m/1'/%d & m/2'/%d"%(i,i)
340 elif account_type == '2of3':
341 return "m/3'/%d & m/4'/%d & m/5'/%d"%(i,i,i)
343 raise BaseException('unknown account type')
346 def num_accounts(self, account_type):
347 keys = self.accounts.keys()
350 account_id = self.account_id(account_type, i)
351 if account_id not in keys: break
356 def new_account_address(self, account_type = '1'):
357 i = self.num_accounts(account_type)
358 k = self.account_id(account_type,i)
360 addr = self.next_addresses.get(k)
362 account_id, account = self.next_account(account_type)
363 addr = account.first_address()
364 self.next_addresses[k] = addr
365 self.storage.put('next_addresses',self.next_addresses)
370 def next_account(self, account_type = '1'):
372 i = self.num_accounts(account_type)
373 account_id = self.account_id(account_type,i)
375 if account_type is '1':
376 master_c0, master_K0, _ = self.master_public_keys["m/0'/"]
377 c0, K0, cK0 = bip32_public_derivation(master_c0.decode('hex'), master_K0.decode('hex'), "m/0'/", "m/0'/%d"%i)
378 account = BIP32_Account({ 'c':c0, 'K':K0, 'cK':cK0 })
380 elif account_type == '2of2':
381 master_c1, master_K1, _ = self.master_public_keys["m/1'/"]
382 c1, K1, cK1 = bip32_public_derivation(master_c1.decode('hex'), master_K1.decode('hex'), "m/1'/", "m/1'/%d"%i)
383 master_c2, master_K2, _ = self.master_public_keys["m/2'/"]
384 c2, K2, cK2 = bip32_public_derivation(master_c2.decode('hex'), master_K2.decode('hex'), "m/2'/", "m/2'/%d"%i)
385 account = BIP32_Account_2of2({ 'c':c1, 'K':K1, 'cK':cK1, 'c2':c2, 'K2':K2, 'cK2':cK2 })
387 elif account_type == '2of3':
388 master_c3, master_K3, _ = self.master_public_keys["m/3'/"]
389 c3, K3, cK3 = bip32_public_derivation(master_c3.decode('hex'), master_K3.decode('hex'), "m/3'/", "m/3'/%d"%i)
390 master_c4, master_K4, _ = self.master_public_keys["m/4'/"]
391 c4, K4, cK4 = bip32_public_derivation(master_c4.decode('hex'), master_K4.decode('hex'), "m/4'/", "m/4'/%d"%i)
392 master_c5, master_K5, _ = self.master_public_keys["m/5'/"]
393 c5, K5, cK5 = bip32_public_derivation(master_c5.decode('hex'), master_K5.decode('hex'), "m/5'/", "m/5'/%d"%i)
394 account = BIP32_Account_2of3({ 'c':c3, 'K':K3, 'cK':cK3, 'c2':c4, 'K2':K4, 'cK2':cK4, 'c3':c5, 'K3':K5, 'cK3':cK5 })
396 return account_id, account
399 def set_label(self, name, text = None):
401 old_text = self.labels.get(name)
404 self.labels[name] = text
408 self.labels.pop(name)
412 self.storage.put('labels', self.labels, True)
414 run_hook('set_label', name, text, changed)
419 def create_account(self, account_type = '1', name = None):
420 account_id, account = self.next_account(account_type)
421 self.accounts[account_id] = account
424 self.set_label(account_id, name)
427 def create_old_account(self):
428 mpk = OldAccount.mpk_from_seed(self.seed)
429 self.storage.put('master_public_key', mpk, True)
430 self.accounts[0] = OldAccount({'mpk':mpk, 0:[], 1:[]})
434 def save_accounts(self):
436 for k, v in self.accounts.items():
438 self.storage.put('accounts', d, True)
442 def load_accounts(self):
443 d = self.storage.get('accounts', {})
445 for k, v in d.items():
447 v['mpk'] = self.storage.get('master_public_key')
448 self.accounts[k] = OldAccount(v)
450 self.accounts[k] = BIP32_Account_2of2(v)
452 self.accounts[k] = BIP32_Account(v)
455 def addresses(self, include_change = True, next=False):
456 o = self.get_account_addresses(-1, include_change)
457 for a in self.accounts.keys():
458 o += self.get_account_addresses(a, include_change)
461 for addr in self.next_addresses.values():
467 def is_mine(self, address):
468 return address in self.addresses(True)
471 def is_change(self, address):
472 if not self.is_mine(address): return False
473 if address in self.imported_keys.keys(): return False
474 acct, s = self.get_address_index(address)
475 if s is None: return False
478 def get_master_public_key(self):
479 if self.seed_version == 4:
480 return self.storage.get("master_public_key")
482 c, K, cK = self.storage.get("master_public_keys")["m/0'/"]
485 def get_master_private_key(self, account, password):
486 master_k = pw_decode( self.master_private_keys[account], password)
487 master_c, master_K, master_Kc = self.master_public_keys[account]
489 K, Kc = get_pubkeys_from_secret(master_k.decode('hex'))
490 assert K.encode('hex') == master_K
492 raise BaseException("Invalid password")
496 def get_address_index(self, address):
497 if address in self.imported_keys.keys():
500 for account in self.accounts.keys():
501 for for_change in [0,1]:
502 addresses = self.accounts[account].get_addresses(for_change)
503 for addr in addresses:
505 return account, (for_change, addresses.index(addr))
507 raise BaseException("Address not found", address)
510 def get_roots(self, account):
512 for a in account.split('&'):
514 m = re.match("(m/\d+'/)(\d+)", s)
515 roots.append( m.group(1) )
518 def is_seeded(self, account):
519 if type(account) is int:
520 return self.seed is not None
522 for root in self.get_roots(account):
523 if root not in self.master_private_keys.keys():
527 def rebase_sequence(self, account, sequence):
530 for a in account.split('&'):
532 m = re.match("(m/\d+'/)(\d+)", s)
534 num = int(m.group(2))
535 dd.append( (root, [num,c,i] ) )
539 def get_keyID(self, account, sequence):
543 rs = self.rebase_sequence(account, sequence)
545 for root, public_sequence in rs:
546 c, K, _ = self.master_public_keys[root]
547 s = '/' + '/'.join( map(lambda x:str(x), public_sequence) )
548 dd.append( 'bip32(%s,%s,%s)'%(c,K, s) )
552 def get_public_key(self, address):
553 account, sequence = self.get_address_index(address)
554 return self.accounts[account].get_pubkey( *sequence )
557 def decode_seed(self, password):
558 seed = pw_decode(self.seed, password)
559 #todo: #self.sequences[0].check_seed(seed)
563 def get_private_key(self, address, password):
565 if address in self.imported_keys.keys():
566 out.append( pw_decode( self.imported_keys[address], password ) )
568 account, sequence = self.get_address_index(address)
570 seed = self.decode_seed(password)
571 pk = self.accounts[account].get_private_key(seed, sequence)
575 # assert address == self.accounts[account].get_address(*sequence)
576 rs = self.rebase_sequence( account, sequence)
577 for root, public_sequence in rs:
579 if root not in self.master_private_keys.keys(): continue
580 master_k = self.get_master_private_key(root, password)
581 master_c, _, _ = self.master_public_keys[root]
582 pk = bip32_private_key( public_sequence, master_k.decode('hex'), master_c.decode('hex'))
590 def signrawtransaction(self, tx, input_info, private_keys, password):
592 unspent_coins = self.get_unspent_coins()
593 seed = self.decode_seed(password)
595 # build a list of public/private keys
597 for sec in private_keys:
598 pubkey = public_key_from_private_key(sec)
599 keypairs[ pubkey ] = sec
602 for txin in tx.inputs:
603 # convert to own format
604 txin['tx_hash'] = txin['prevout_hash']
605 txin['index'] = txin['prevout_n']
607 for item in input_info:
608 if item.get('txid') == txin['tx_hash'] and item.get('vout') == txin['index']:
609 txin['raw_output_script'] = item['scriptPubKey']
610 txin['redeemScript'] = item.get('redeemScript')
611 txin['KeyID'] = item.get('KeyID')
614 for item in unspent_coins:
615 if txin['tx_hash'] == item['tx_hash'] and txin['index'] == item['index']:
616 print_error( "tx input is in unspent coins" )
617 txin['raw_output_script'] = item['raw_output_script']
618 account, sequence = self.get_address_index(item['address'])
620 txin['redeemScript'] = self.accounts[account].redeem_script(sequence)
623 raise BaseException("Unknown transaction input. Please provide the 'input_info' parameter, or synchronize this wallet")
625 # if available, derive private_keys from KeyID
626 keyid = txin.get('KeyID')
629 for s in keyid.split('&'):
630 m = re.match("bip32\(([0-9a-f]+),([0-9a-f]+),(/\d+/\d+/\d+)", s)
634 sequence = m.group(3)
635 root = self.find_root_by_master_key(c,K)
636 if not root: continue
637 sequence = map(lambda x:int(x), sequence.strip('/').split('/'))
638 root = root + '%d'%sequence[0]
639 sequence = sequence[1:]
640 roots.append((root,sequence))
642 account_id = " & ".join( map(lambda x:x[0], roots) )
643 account = self.accounts.get(account_id)
644 if not account: continue
645 addr = account.get_address(*sequence)
646 txin['address'] = addr
647 pk = self.get_private_key(addr, password)
649 pubkey = public_key_from_private_key(sec)
650 keypairs[pubkey] = sec
652 redeem_script = txin.get("redeemScript")
653 print_error( "p2sh:", "yes" if redeem_script else "no")
655 addr = hash_160_to_bc_address(hash_160(redeem_script.decode('hex')), 5)
658 _, addr = transaction.get_address_from_output_script(txin["raw_output_script"].decode('hex'))
659 txin['address'] = addr
661 # add private keys that are in the wallet
662 pk = self.get_private_key(addr, password)
664 pubkey = public_key_from_private_key(sec)
665 keypairs[pubkey] = sec
666 if not redeem_script:
667 txin['redeemPubkey'] = pubkey
671 self.sign_tx(tx, keypairs)
674 def sign_message(self, address, message, password):
675 keys = self.get_private_key(address, password)
676 assert len(keys) == 1
678 key = regenerate_key(sec)
679 compressed = is_compressed(sec)
680 return key.sign_message(message, compressed, address)
682 def verify_message(self, address, signature, message):
684 EC_KEY.verify_message(address, signature, message)
686 except BaseException as e:
687 print_error("Verification error: {0}".format(e))
691 def change_gap_limit(self, value):
692 if value >= self.gap_limit:
693 self.gap_limit = value
694 self.storage.put('gap_limit', self.gap_limit, True)
695 #self.interface.poke('synchronizer')
698 elif value >= self.min_acceptable_gap():
699 for key, account in self.accounts.items():
700 addresses = account[0]
701 k = self.num_unused_trailing_addresses(addresses)
702 n = len(addresses) - k + value
703 addresses = addresses[0:n]
704 self.accounts[key][0] = addresses
706 self.gap_limit = value
707 self.storage.put('gap_limit', self.gap_limit, True)
713 def num_unused_trailing_addresses(self, addresses):
715 for a in addresses[::-1]:
716 if self.history.get(a):break
720 def min_acceptable_gap(self):
721 # fixme: this assumes wallet is synchronized
725 for account in self.accounts.values():
726 addresses = account.get_addresses(0)
727 k = self.num_unused_trailing_addresses(addresses)
728 for a in addresses[0:-k]:
729 if self.history.get(a):
733 if n > nmax: nmax = n
737 def address_is_old(self, address):
739 h = self.history.get(address, [])
742 for tx_hash, tx_height in h:
746 tx_age = self.verifier.blockchain.height - tx_height + 1
752 def synchronize_sequence(self, account, for_change):
753 limit = self.gap_limit_for_change if for_change else self.gap_limit
756 addresses = account.get_addresses(for_change)
757 if len(addresses) < limit:
758 address = account.create_new_address(for_change)
759 self.history[address] = []
760 new_addresses.append( address )
763 if map( lambda a: self.address_is_old(a), addresses[-limit:] ) == limit*[False]:
766 address = account.create_new_address(for_change)
767 self.history[address] = []
768 new_addresses.append( address )
774 def create_pending_accounts(self):
775 for account_type in ['1','2of2','2of3']:
776 a = self.new_account_address(account_type)
777 if self.address_is_old(a):
778 print_error( "creating account", a )
779 self.create_account(account_type)
782 def synchronize_account(self, account):
784 new += self.synchronize_sequence(account, 0)
785 new += self.synchronize_sequence(account, 1)
789 def synchronize(self):
790 if self.master_public_keys:
791 self.create_pending_accounts()
793 for account in self.accounts.values():
794 new += self.synchronize_account(account)
797 self.storage.put('addr_history', self.history, True)
802 return self.history.values() != [[]] * len(self.history)
805 def add_contact(self, address, label=None):
806 self.addressbook.append(address)
807 self.storage.put('contacts', self.addressbook, True)
809 self.set_label(address, label)
812 def delete_contact(self, addr):
813 if addr in self.addressbook:
814 self.addressbook.remove(addr)
815 self.storage.put('addressbook', self.addressbook, True)
818 def fill_addressbook(self):
819 for tx_hash, tx in self.transactions.items():
820 is_relevant, is_send, _, _ = self.get_tx_value(tx)
822 for addr, v in tx.outputs:
823 if not self.is_mine(addr) and addr not in self.addressbook:
824 self.addressbook.append(addr)
826 # self.update_tx_labels()
828 def get_num_tx(self, address):
830 for tx in self.transactions.values():
831 if address in map(lambda x:x[0], tx.outputs): n += 1
835 def get_address_flags(self, addr):
836 flags = "C" if self.is_change(addr) else "I" if addr in self.imported_keys.keys() else "-"
837 flags += "F" if addr in self.frozen_addresses else "P" if addr in self.prioritized_addresses else "-"
841 def get_tx_value(self, tx, account=None):
842 domain = self.get_account_addresses(account)
843 return tx.get_value(domain, self.prevout_values)
846 def update_tx_outputs(self, tx_hash):
847 tx = self.transactions.get(tx_hash)
849 for i, (addr, value) in enumerate(tx.outputs):
850 key = tx_hash+ ':%d'%i
851 self.prevout_values[key] = value
853 for item in tx.inputs:
854 if self.is_mine(item.get('address')):
855 key = item['prevout_hash'] + ':%d'%item['prevout_n']
856 self.spent_outputs.append(key)
859 def get_addr_balance(self, address):
860 assert self.is_mine(address)
861 h = self.history.get(address,[])
862 if h == ['*']: return 0,0
864 received_coins = [] # list of coins received at address
866 for tx_hash, tx_height in h:
867 tx = self.transactions.get(tx_hash)
870 for i, (addr, value) in enumerate(tx.outputs):
872 key = tx_hash + ':%d'%i
873 received_coins.append(key)
875 for tx_hash, tx_height in h:
876 tx = self.transactions.get(tx_hash)
880 for item in tx.inputs:
881 addr = item.get('address')
883 key = item['prevout_hash'] + ':%d'%item['prevout_n']
884 value = self.prevout_values.get( key )
885 if key in received_coins:
888 for i, (addr, value) in enumerate(tx.outputs):
889 key = tx_hash + ':%d'%i
900 def get_account_name(self, k):
902 if self.seed_version == 4:
903 name = 'Main account'
907 name = self.labels.get(k, 'Unnamed account')
910 def get_account_names(self):
912 for k, account in self.accounts.items():
913 accounts[k] = self.get_account_name(k)
914 if self.imported_keys:
915 accounts[-1] = 'Imported keys'
918 def get_account_addresses(self, a, include_change=True):
920 o = self.addresses(True)
922 o = self.imported_keys.keys()
924 ac = self.accounts[a]
925 o = ac.get_addresses(0)
926 if include_change: o += ac.get_addresses(1)
929 def get_imported_balance(self):
931 for addr in self.imported_keys.keys():
932 c, u = self.get_addr_balance(addr)
937 def get_account_balance(self, account):
939 return self.get_balance()
941 return self.get_imported_balance()
944 for addr in self.get_account_addresses(account):
945 c, u = self.get_addr_balance(addr)
950 def get_frozen_balance(self):
952 for addr in self.frozen_addresses:
953 c, u = self.get_addr_balance(addr)
959 def get_balance(self):
961 for a in self.accounts.keys():
962 c, u = self.get_account_balance(a)
965 c, u = self.get_imported_balance()
971 def get_unspent_coins(self, domain=None):
973 if domain is None: domain = self.addresses(True)
975 h = self.history.get(addr, [])
976 if h == ['*']: continue
977 for tx_hash, tx_height in h:
978 tx = self.transactions.get(tx_hash)
979 if tx is None: raise BaseException("Wallet not synchronized")
980 for output in tx.d.get('outputs'):
981 if output.get('address') != addr: continue
982 key = tx_hash + ":%d" % output.get('index')
983 if key in self.spent_outputs: continue
984 output['tx_hash'] = tx_hash
985 output['height'] = tx_height
986 coins.append((tx_height, output))
990 coins = sorted(coins)
991 if coins[-1][0] != 0:
992 while coins[0][0] == 0:
993 coins = coins[1:] + [ coins[0] ]
994 return [x[1] for x in coins]
998 def choose_tx_inputs( self, amount, fixed_fee, domain = None ):
999 """ todo: minimize tx size """
1001 fee = self.fee if fixed_fee is None else fixed_fee
1003 domain = self.addresses()
1005 for i in self.frozen_addresses:
1006 if i in domain: domain.remove(i)
1009 for i in self.prioritized_addresses:
1012 prioritized.append(i)
1014 coins = self.get_unspent_coins(domain)
1015 prioritized_coins = self.get_unspent_coins(prioritized)
1018 coins = prioritized_coins + coins
1021 addr = item.get('address')
1022 v = item.get('value')
1024 inputs.append( item )
1025 fee = self.estimated_fee(inputs) if fixed_fee is None else fixed_fee
1026 if total >= amount + fee: break
1030 return inputs, total, fee
1033 def set_fee(self, fee):
1036 self.storage.put('fee_per_kb', self.fee, True)
1038 def estimated_fee(self, inputs):
1039 estimated_size = len(inputs) * 180 + 80 # this assumes non-compressed keys
1040 fee = self.fee * int(round(estimated_size/1024.))
1041 if fee == 0: fee = self.fee
1045 def add_tx_change( self, inputs, outputs, amount, fee, total, change_addr=None):
1046 "add change to a transaction"
1047 change_amount = total - ( amount + fee )
1048 if change_amount != 0:
1051 # send change to one of the accounts involved in the tx
1052 address = inputs[0].get('address')
1053 account, _ = self.get_address_index(address)
1055 if not self.use_change or account == -1:
1056 change_addr = inputs[-1]['address']
1058 change_addr = self.accounts[account].get_addresses(1)[-self.gap_limit_for_change]
1060 # Insert the change output at a random position in the outputs
1061 posn = random.randint(0, len(outputs))
1062 outputs[posn:posn] = [( change_addr, change_amount)]
1066 def get_history(self, address):
1068 return self.history.get(address)
1071 def get_status(self, h):
1072 if not h: return None
1073 if h == ['*']: return '*'
1075 for tx_hash, height in h:
1076 status += tx_hash + ':%d:' % height
1077 return hashlib.sha256( status ).digest().encode('hex')
1080 def receive_tx_callback(self, tx_hash, tx, tx_height):
1082 with self.transaction_lock:
1083 self.add_extra_addresses(tx)
1084 if not self.check_new_tx(tx_hash, tx):
1085 # may happen due to pruning
1086 print_error("received transaction that is no longer referenced in history", tx_hash)
1088 self.transactions[tx_hash] = tx
1089 self.network.interface.pending_transactions_for_notifications.append(tx)
1090 self.save_transactions()
1091 if self.verifier and tx_height>0:
1092 self.verifier.add(tx_hash, tx_height)
1093 self.update_tx_outputs(tx_hash)
1096 def save_transactions(self):
1098 for k,v in self.transactions.items():
1100 self.storage.put('transactions', tx, True)
1102 def receive_history_callback(self, addr, hist):
1104 if not self.check_new_history(addr, hist):
1105 raise BaseException("error: received history for %s is not consistent with known transactions"%addr)
1108 self.history[addr] = hist
1109 self.storage.put('addr_history', self.history, True)
1112 for tx_hash, tx_height in hist:
1114 # add it in case it was previously unconfirmed
1115 if self.verifier: self.verifier.add(tx_hash, tx_height)
1118 def get_tx_history(self, account=None):
1119 with self.transaction_lock:
1120 history = self.transactions.items()
1121 history.sort(key = lambda x: self.verifier.get_txpos(x[0]))
1125 for tx_hash, tx in history:
1126 is_relevant, is_mine, v, fee = self.get_tx_value(tx, account)
1127 if v is not None: balance += v
1129 c, u = self.get_account_balance(account)
1132 result.append( ('', 1000, 0, c+u-balance, None, c+u-balance, None ) )
1134 balance = c + u - balance
1135 for tx_hash, tx in history:
1136 is_relevant, is_mine, value, fee = self.get_tx_value(tx, account)
1139 if value is not None:
1142 conf, timestamp = self.verifier.get_confirmations(tx_hash) if self.verifier else (None, None)
1143 result.append( (tx_hash, conf, is_mine, value, fee, balance, timestamp) )
1148 def get_label(self, tx_hash):
1149 label = self.labels.get(tx_hash)
1150 is_default = (label == '') or (label is None)
1151 if is_default: label = self.get_default_label(tx_hash)
1152 return label, is_default
1155 def get_default_label(self, tx_hash):
1156 tx = self.transactions.get(tx_hash)
1159 is_relevant, is_mine, _, _ = self.get_tx_value(tx)
1161 for o in tx.outputs:
1163 if not self.is_mine(o_addr):
1165 default_label = self.labels[o_addr]
1167 default_label = o_addr
1170 default_label = '(internal)'
1172 for o in tx.outputs:
1174 if self.is_mine(o_addr) and not self.is_change(o_addr):
1177 for o in tx.outputs:
1179 if self.is_mine(o_addr):
1185 dest_label = self.labels.get(o_addr)
1187 default_label = self.labels[o_addr]
1189 default_label = o_addr
1191 return default_label
1194 def make_unsigned_transaction(self, outputs, fee=None, change_addr=None, domain=None ):
1195 for address, x in outputs:
1196 assert is_valid(address)
1197 amount = sum( map(lambda x:x[1], outputs) )
1198 inputs, total, fee = self.choose_tx_inputs( amount, fee, domain )
1200 raise ValueError("Not enough funds")
1201 self.add_input_info(inputs)
1202 outputs = self.add_tx_change(inputs, outputs, amount, fee, total, change_addr)
1203 return Transaction.from_io(inputs, outputs)
1206 def mktx_from_account(self, outputs, password, fee=None, change_addr=None, account=None):
1207 domain = self.get_account_addresses(account) if account else None
1208 return self.mktx(outputs, password, fee, change_addr, domain)
1211 def mktx(self, outputs, password, fee=None, change_addr=None, domain= None ):
1212 tx = self.make_unsigned_transaction(outputs, fee, change_addr, domain)
1213 self.sign_transaction(tx, password)
1217 def add_input_info(self, inputs):
1219 address = txin['address']
1220 account, sequence = self.get_address_index(address)
1221 txin['KeyID'] = self.get_keyID(account, sequence)
1222 redeemScript = self.accounts[account].redeem_script(sequence)
1224 txin['redeemScript'] = redeemScript
1226 txin['redeemPubkey'] = self.accounts[account].get_pubkey(*sequence)
1229 def sign_transaction(self, tx, password):
1231 for i, txin in enumerate(tx.inputs):
1232 address = txin['address']
1233 private_keys = self.get_private_key(address, password)
1234 for sec in private_keys:
1235 pubkey = public_key_from_private_key(sec)
1236 keypairs[ pubkey ] = sec
1238 self.sign_tx(tx, keypairs)
1241 def sign_tx(self, tx, keypairs):
1243 run_hook('sign_tx', tx)
1246 def sendtx(self, tx):
1248 h = self.send_tx(tx)
1249 self.tx_event.wait()
1250 return self.receive_tx(h)
1252 def send_tx(self, tx):
1254 self.tx_event.clear()
1255 self.network.interface.send([('blockchain.transaction.broadcast', [str(tx)])], self.on_broadcast)
1258 def on_broadcast(self, i, r):
1259 self.tx_result = r.get('result')
1262 def receive_tx(self,tx_hash):
1263 out = self.tx_result
1265 return False, "error: " + out
1270 def update_password(self, seed, old_password, new_password):
1271 if new_password == '': new_password = None
1272 # this will throw an exception if unicode cannot be converted
1273 self.seed = pw_encode( seed, new_password)
1274 self.storage.put('seed', self.seed, True)
1275 self.use_encryption = (new_password != None)
1276 self.storage.put('use_encryption', self.use_encryption,True)
1277 for k in self.imported_keys.keys():
1278 a = self.imported_keys[k]
1279 b = pw_decode(a, old_password)
1280 c = pw_encode(b, new_password)
1281 self.imported_keys[k] = c
1282 self.storage.put('imported_keys', self.imported_keys, True)
1284 for k, v in self.master_private_keys.items():
1285 b = pw_decode(v, old_password)
1286 c = pw_encode(b, new_password)
1287 self.master_private_keys[k] = c
1288 self.storage.put('master_private_keys', self.master_private_keys, True)
1291 def freeze(self,addr):
1292 if self.is_mine(addr) and addr not in self.frozen_addresses:
1293 self.unprioritize(addr)
1294 self.frozen_addresses.append(addr)
1295 self.storage.put('frozen_addresses', self.frozen_addresses, True)
1300 def unfreeze(self,addr):
1301 if self.is_mine(addr) and addr in self.frozen_addresses:
1302 self.frozen_addresses.remove(addr)
1303 self.storage.put('frozen_addresses', self.frozen_addresses, True)
1308 def prioritize(self,addr):
1309 if self.is_mine(addr) and addr not in self.prioritized_addresses:
1311 self.prioritized_addresses.append(addr)
1312 self.storage.put('prioritized_addresses', self.prioritized_addresses, True)
1317 def unprioritize(self,addr):
1318 if self.is_mine(addr) and addr in self.prioritized_addresses:
1319 self.prioritized_addresses.remove(addr)
1320 self.storage.put('prioritized_addresses', self.prioritized_addresses, True)
1326 def set_verifier(self, verifier):
1327 self.verifier = verifier
1329 # review transactions that are in the history
1330 for addr, hist in self.history.items():
1331 if hist == ['*']: continue
1332 for tx_hash, tx_height in hist:
1334 # add it in case it was previously unconfirmed
1335 self.verifier.add(tx_hash, tx_height)
1338 # if we are on a pruning server, remove unverified transactions
1339 vr = self.verifier.transactions.keys() + self.verifier.verified_tx.keys()
1340 for tx_hash in self.transactions.keys():
1341 if tx_hash not in vr:
1342 self.transactions.pop(tx_hash)
1346 def check_new_history(self, addr, hist):
1348 # check that all tx in hist are relevant
1350 for tx_hash, height in hist:
1351 tx = self.transactions.get(tx_hash)
1353 if not tx.has_address(addr):
1356 # check that we are not "orphaning" a transaction
1357 old_hist = self.history.get(addr,[])
1358 if old_hist == ['*']: return True
1360 for tx_hash, height in old_hist:
1361 if tx_hash in map(lambda x:x[0], hist): continue
1363 for _addr, _hist in self.history.items():
1364 if _addr == addr: continue
1365 if _hist == ['*']: continue
1366 _tx_hist = map(lambda x:x[0], _hist)
1367 if tx_hash in _tx_hist:
1372 tx = self.transactions.get(tx_hash)
1373 # tx might not be there
1377 if self.verifier.get_height(tx_hash):
1380 print_error("new history is orphaning transaction:", tx_hash)
1381 # check that all outputs are not mine, request histories
1383 for _addr, _v in tx.outputs:
1384 # assert not self.is_mine(_addr)
1385 ext_requests.append( ('blockchain.address.get_history', [_addr]) )
1387 ext_h = self.network.interface.synchronous_get(ext_requests)
1388 print_error("sync:", ext_requests, ext_h)
1391 if h == ['*']: continue
1393 if item.get('tx_hash') == tx_hash:
1394 height = item.get('height')
1396 print_error("found height for", tx_hash, height)
1397 self.verifier.add(tx_hash, height)
1399 print_error("removing orphaned tx from history", tx_hash)
1400 self.transactions.pop(tx_hash)
1406 def check_new_tx(self, tx_hash, tx):
1407 # 1 check that tx is referenced in addr_history.
1409 for addr, hist in self.history.items():
1410 if hist == ['*']:continue
1411 for txh, height in hist:
1413 addresses.append(addr)
1418 # 2 check that referencing addresses are in the tx
1419 for addr in addresses:
1420 if not tx.has_address(addr):
1426 def start_threads(self, network):
1427 from verifier import TxVerifier
1428 self.network = network
1429 self.verifier = TxVerifier(self.network, self.storage)
1430 self.verifier.start()
1431 self.set_verifier(self.verifier)
1432 self.synchronizer = WalletSynchronizer(self)
1433 self.synchronizer.start()
1435 def stop_threads(self):
1436 self.verifier.stop()
1437 self.synchronizer.stop()
1441 def restore(self, callback):
1443 def wait_for_wallet():
1444 self.set_up_to_date(False)
1445 while not self.is_up_to_date():
1446 msg = "%s\n%s %d\n%s %.1f"%(
1447 _("Please wait..."),
1448 _("Addresses generated:"),
1449 len(self.addresses(True)),_("Kilobytes received:"),
1450 self.network.interface.bytes_received/1024.)
1452 apply(callback, (msg,))
1455 def wait_for_network():
1456 while not self.network.interface.is_connected:
1457 msg = "%s \n" % (_("Connecting..."))
1458 apply(callback, (msg,))
1461 # wait until we are connected, because the user might have selected another server
1464 # try to restore old account
1465 self.create_old_account()
1469 self.seed_version = 4
1470 self.storage.put('seed_version', wallet.seed_version, True)
1472 self.accounts.pop(0)
1473 self.create_accounts()
1479 class WalletSynchronizer(threading.Thread):
1482 def __init__(self, wallet):
1483 threading.Thread.__init__(self)
1485 self.wallet = wallet
1486 wallet.synchronizer = self
1487 self.network = self.wallet.network
1488 #self.wallet.network.register_callback('connected', lambda: self.wallet.set_up_to_date(False))
1489 self.was_updated = True
1490 self.running = False
1491 self.lock = threading.Lock()
1492 self.queue = Queue.Queue()
1495 with self.lock: self.running = False
1497 def is_running(self):
1498 with self.lock: return self.running
1501 def subscribe_to_addresses(self, addresses):
1503 for addr in addresses:
1504 messages.append(('blockchain.address.subscribe', [addr]))
1505 self.network.interface.send( messages, lambda i,r: self.queue.put(r))
1512 while self.is_running():
1513 interface = self.network.interface
1514 if not interface.is_connected:
1515 print_error("synchronizer: waiting for interface")
1516 interface.connect_event.wait()
1518 self.run_interface(interface)
1521 def run_interface(self, interface):
1523 print_error("synchronizer: connected to", interface.server)
1527 requested_histories = {}
1529 # request any missing transactions
1530 for history in self.wallet.history.values():
1531 if history == ['*']: continue
1532 for tx_hash, tx_height in history:
1533 if self.wallet.transactions.get(tx_hash) is None and (tx_hash, tx_height) not in missing_tx:
1534 missing_tx.append( (tx_hash, tx_height) )
1537 print_error("missing tx", missing_tx)
1540 self.subscribe_to_addresses(self.wallet.addresses(True, next=True))
1542 while self.is_running():
1543 # 1. create new addresses
1544 new_addresses = self.wallet.synchronize()
1546 # request missing addresses
1548 self.subscribe_to_addresses(new_addresses)
1550 # request missing transactions
1551 for tx_hash, tx_height in missing_tx:
1552 if (tx_hash, tx_height) not in requested_tx:
1553 interface.send([ ('blockchain.transaction.get',[tx_hash, tx_height]) ], lambda i,r: self.queue.put(r))
1554 requested_tx.append( (tx_hash, tx_height) )
1557 # detect if situation has changed
1558 if interface.is_up_to_date() and self.queue.empty():
1559 if not self.wallet.is_up_to_date():
1560 self.wallet.set_up_to_date(True)
1561 self.was_updated = True
1563 if self.wallet.is_up_to_date():
1564 self.wallet.set_up_to_date(False)
1565 self.was_updated = True
1567 if self.was_updated:
1568 self.wallet.network.trigger_callback('updated')
1569 self.was_updated = False
1573 r = self.queue.get(block=True, timeout=1)
1577 if interface != self.network.interface:
1583 # 3. handle response
1584 method = r['method']
1585 params = r['params']
1586 result = r.get('result')
1587 error = r.get('error')
1592 if method == 'blockchain.address.subscribe':
1594 if self.wallet.get_status(self.wallet.get_history(addr)) != result:
1595 if requested_histories.get(addr) is None:
1596 interface.send([('blockchain.address.get_history', [addr])], lambda i,r:self.queue.put(r))
1597 requested_histories[addr] = result
1599 elif method == 'blockchain.address.get_history':
1601 print_error("receiving history", addr, result)
1603 assert requested_histories.pop(addr) == '*'
1604 self.wallet.receive_history_callback(addr, result)
1607 # check that txids are unique
1610 tx_hash = item['tx_hash']
1611 if tx_hash not in txids:
1612 txids.append(tx_hash)
1613 hist.append( (tx_hash, item['height']) )
1615 if len(hist) != len(result):
1616 raise BaseException("error: server sent history with non-unique txid", result)
1618 # check that the status corresponds to what was announced
1619 rs = requested_histories.pop(addr)
1620 if self.wallet.get_status(hist) != rs:
1621 raise BaseException("error: status mismatch: %s"%addr)
1623 # store received history
1624 self.wallet.receive_history_callback(addr, hist)
1626 # request transactions that we don't have
1627 for tx_hash, tx_height in hist:
1628 if self.wallet.transactions.get(tx_hash) is None:
1629 if (tx_hash, tx_height) not in requested_tx and (tx_hash, tx_height) not in missing_tx:
1630 missing_tx.append( (tx_hash, tx_height) )
1632 elif method == 'blockchain.transaction.get':
1634 tx_height = params[1]
1635 assert tx_hash == hash_encode(Hash(result.decode('hex')))
1636 tx = Transaction(result)
1637 self.wallet.receive_tx_callback(tx_hash, tx, tx_height)
1638 self.was_updated = True
1639 requested_tx.remove( (tx_hash, tx_height) )
1640 print_error("received tx:", tx_hash, len(tx.raw))
1643 print_error("Error: Unknown message:" + method + ", " + repr(params) + ", " + repr(result) )
1645 if self.was_updated and not requested_tx:
1646 self.wallet.network.trigger_callback('updated')
1647 self.wallet.network.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
1649 self.was_updated = False