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
39 COINBASE_MATURITY = 100
43 EncodeAES = lambda secret, s: base64.b64encode(aes.encryptData(secret,s))
44 DecodeAES = lambda secret, e: aes.decryptData(secret, base64.b64decode(e))
46 def pw_encode(s, password):
48 secret = Hash(password)
49 return EncodeAES(secret, s)
53 def pw_decode(s, password):
54 if password is not None:
55 secret = Hash(password)
57 d = DecodeAES(secret, s)
59 raise Exception('Invalid password')
73 def __init__(self, config):
74 self.lock = threading.Lock()
76 self.file_exists = False
77 self.path = self.init_path(config)
78 print_error( "wallet path", self.path )
83 def init_path(self, config):
84 """Set the path of the wallet."""
86 # command line -w option
87 path = config.get('wallet_path')
92 path = config.get('default_wallet_path')
97 dirpath = os.path.join(config.path, "wallets")
98 if not os.path.exists(dirpath):
101 new_path = os.path.join(config.path, "wallets", "default_wallet")
103 # default path in pre 1.9 versions
104 old_path = os.path.join(config.path, "electrum.dat")
105 if os.path.exists(old_path) and not os.path.exists(new_path):
106 os.rename(old_path, new_path)
111 def read(self, path):
112 """Read the contents of the wallet file."""
114 with open(self.path, "r") as f:
119 d = ast.literal_eval( data ) #parse raw data from reading wallet file
121 raise IOError("Cannot read wallet file.")
124 self.file_exists = True
127 def get(self, key, default=None):
128 v = self.data.get(key)
133 def put(self, key, value, save = True):
136 if value is not None:
137 self.data[key] = value
145 f = open(self.path,"w")
148 if 'ANDROID_DATA' not in os.environ:
150 os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)
155 def __init__(self, storage):
157 self.storage = storage
158 self.electrum_version = ELECTRUM_VERSION
159 self.gap_limit_for_change = 3 # constant
162 self.seed_version = storage.get('seed_version', SEED_VERSION)
164 self.gap_limit = storage.get('gap_limit', 5)
165 self.use_change = storage.get('use_change',True)
166 self.use_encryption = storage.get('use_encryption', False)
167 self.seed = storage.get('seed', '') # encrypted
168 self.labels = storage.get('labels', {})
169 self.frozen_addresses = storage.get('frozen_addresses',[])
170 self.addressbook = storage.get('contacts', [])
172 self.imported_keys = storage.get('imported_keys',{})
173 self.history = storage.get('addr_history',{}) # address -> list(txid, height)
175 self.fee = int(storage.get('fee_per_kb',20000))
177 self.master_public_keys = storage.get('master_public_keys',{})
178 self.master_private_keys = storage.get('master_private_keys', {})
180 self.next_addresses = storage.get('next_addresses',{})
182 if self.seed_version not in [4, 6]:
183 msg = "This wallet seed is not supported."
184 if self.seed_version in [5]:
185 msg += "\nTo open this wallet, try 'git checkout seed_v%d'"%self.seed_version
189 # This attribute is set when wallet.start_threads is called.
190 self.synchronizer = None
194 self.transactions = {}
195 tx_list = self.storage.get('transactions',{})
196 for k,v in tx_list.items():
200 print_msg("Warning: Cannot deserialize transactions. skipping")
203 self.add_extra_addresses(tx)
204 self.transactions[k] = tx
206 for h,tx in self.transactions.items():
207 if not self.check_new_tx(h, tx):
208 print_error("removing unreferenced tx", h)
209 self.transactions.pop(h)
213 self.prevout_values = {} # my own transaction outputs
214 self.spent_outputs = []
219 # there is a difference between wallet.up_to_date and interface.is_up_to_date()
220 # interface.is_up_to_date() returns true when all requests have been answered and processed
221 # wallet.up_to_date is true when the wallet is synchronized (stronger requirement)
223 self.up_to_date = False
224 self.lock = threading.Lock()
225 self.transaction_lock = threading.Lock()
226 self.tx_event = threading.Event()
228 for tx_hash, tx in self.transactions.items():
229 self.update_tx_outputs(tx_hash)
232 def add_extra_addresses(self, tx):
234 # find the address corresponding to pay-to-pubkey inputs
235 tx.add_extra_addresses(self.transactions)
236 for o in tx.d.get('outputs'):
237 if o.get('is_pubkey'):
238 for tx2 in self.transactions.values():
239 tx2.add_extra_addresses({h:tx})
244 def set_up_to_date(self,b):
245 with self.lock: self.up_to_date = b
248 def is_up_to_date(self):
249 with self.lock: return self.up_to_date
253 self.up_to_date = False
254 while not self.is_up_to_date():
258 def import_key(self, sec, password):
260 seed = self.get_seed(password)
262 address = address_from_private_key(sec)
264 raise Exception('Invalid private key')
266 if self.is_mine(address):
267 raise Exception('Address already in wallet')
269 # store the originally requested keypair into the imported keys table
270 self.imported_keys[address] = pw_encode(sec, password )
271 self.storage.put('imported_keys', self.imported_keys, True)
272 if self.synchronizer:
273 self.synchronizer.subscribe_to_addresses([address])
276 def delete_imported_key(self, addr):
277 if addr in self.imported_keys:
278 self.imported_keys.pop(addr)
279 self.storage.put('imported_keys', self.imported_keys, True)
283 import mnemonic, ecdsa
284 entropy = ecdsa.util.randrange( pow(2,160) )
287 ss = "%040x"%(entropy+nonce)
288 s = hashlib.sha256(ss.decode('hex')).digest().encode('hex')
289 # we keep only 13 words, that's approximately 139 bits of entropy
290 words = mnemonic.mn_encode(s)[0:13]
291 seed = ' '.join(words)
293 break # this will remove 8 bits of entropy
299 def init_seed(self, seed):
303 raise Exception("a seed exists")
306 self.seed = self.make_seed()
307 self.seed_version = SEED_VERSION
310 # find out what kind of wallet we are
312 seed.strip().decode('hex')
313 self.seed_version = 4
314 self.seed = str(seed)
321 mnemonic.mn_decode(words)
322 uses_electrum_words = True
324 uses_electrum_words = False
326 if uses_electrum_words and len(words) != 13:
327 self.seed_version = 4
328 self.seed = mnemonic.mn_decode(words)
330 #assert is_seed(seed)
331 self.seed_version = SEED_VERSION
335 def save_seed(self, password):
337 self.seed = pw_encode( self.seed, password)
338 self.use_encryption = True
339 self.storage.put('seed', self.seed, True)
340 self.storage.put('seed_version', self.seed_version, True)
341 self.storage.put('use_encryption', self.use_encryption,True)
342 self.create_accounts(password)
345 def create_watching_only_wallet(self, params):
351 self.seed_version = 4
352 self.storage.put('seed_version', self.seed_version, True)
353 self.create_old_account(K0)
357 self.master_public_keys = {
358 "m/0'/": (c0, K0, cK0),
360 self.storage.put('master_public_keys', self.master_public_keys, True)
361 self.storage.put('seed_version', self.seed_version, True)
362 self.create_account('1','Main account')
365 def create_accounts(self, password):
366 seed = pw_decode(self.seed, password)
368 if self.seed_version == 4:
369 mpk = OldAccount.mpk_from_seed(seed)
370 self.create_old_account(mpk)
372 # create default account
373 self.create_master_keys('1', password)
374 self.create_account('1','Main account')
377 def create_master_keys(self, account_type, password):
378 master_k, master_c, master_K, master_cK = bip32_init(self.get_seed(password))
379 if account_type == '1':
380 k0, c0, K0, cK0 = bip32_private_derivation(master_k, master_c, "m/", "m/0'/")
381 self.master_public_keys["m/0'/"] = (c0, K0, cK0)
382 self.master_private_keys["m/0'/"] = pw_encode(k0, password)
383 elif account_type == '2of2':
384 k1, c1, K1, cK1 = bip32_private_derivation(master_k, master_c, "m/", "m/1'/")
385 k2, c2, K2, cK2 = bip32_private_derivation(master_k, master_c, "m/", "m/2'/")
386 self.master_public_keys["m/1'/"] = (c1, K1, cK1)
387 self.master_public_keys["m/2'/"] = (c2, K2, cK2)
388 self.master_private_keys["m/1'/"] = pw_encode(k1, password)
389 self.master_private_keys["m/2'/"] = pw_encode(k2, password)
390 elif account_type == '2of3':
391 k3, c3, K3, cK3 = bip32_private_derivation(master_k, master_c, "m/", "m/3'/")
392 k4, c4, K4, cK4 = bip32_private_derivation(master_k, master_c, "m/", "m/4'/")
393 k5, c5, K5, cK5 = bip32_private_derivation(master_k, master_c, "m/", "m/5'/")
394 self.master_public_keys["m/3'/"] = (c3, K3, cK3)
395 self.master_public_keys["m/4'/"] = (c4, K4, cK4)
396 self.master_public_keys["m/5'/"] = (c5, K5, cK5)
397 self.master_private_keys["m/3'/"] = pw_encode(k3, password)
398 self.master_private_keys["m/4'/"] = pw_encode(k4, password)
399 self.master_private_keys["m/5'/"] = pw_encode(k5, password)
401 self.storage.put('master_public_keys', self.master_public_keys, True)
402 self.storage.put('master_private_keys', self.master_private_keys, True)
404 def has_master_public_keys(self, account_type):
405 if account_type == '1':
406 return "m/0'/" in self.master_public_keys
407 elif account_type == '2of2':
408 return set(["m/1'/", "m/2'/"]) <= set(self.master_public_keys.keys())
409 elif account_type == '2of3':
410 return set(["m/3'/", "m/4'/", "m/5'/"]) <= set(self.master_public_keys.keys())
412 def find_root_by_master_key(self, c, K):
413 for key, v in self.master_public_keys.items():
414 if key == "m/":continue
416 if (c == cc) and (K == KK):
419 def deseed_root(self, seed, password):
420 # for safety, we ask the user to enter their seed
421 assert seed == self.get_seed(password)
423 self.storage.put('seed', '', True)
426 def deseed_branch(self, k):
427 # check that parent has no seed
428 # assert self.seed == ''
429 self.master_private_keys.pop(k)
430 self.storage.put('master_private_keys', self.master_private_keys, True)
433 def is_watching_only(self):
434 return (self.seed == '') and (self.master_private_keys == {})
438 def account_id(self, account_type, i):
439 if account_type == '1':
441 elif account_type == '2of2':
442 return "m/1'/%d & m/2'/%d"%(i,i)
443 elif account_type == '2of3':
444 return "m/3'/%d & m/4'/%d & m/5'/%d"%(i,i,i)
446 raise Exception('unknown account type')
449 def num_accounts(self, account_type):
450 keys = self.accounts.keys()
453 account_id = self.account_id(account_type, i)
454 if account_id not in keys: break
459 def new_account_address(self, account_type = '1'):
460 i = self.num_accounts(account_type)
461 k = self.account_id(account_type,i)
463 addr = self.next_addresses.get(k)
465 account_id, account = self.next_account(account_type)
466 addr = account.first_address()
467 self.next_addresses[k] = addr
468 self.storage.put('next_addresses',self.next_addresses)
473 def next_account(self, account_type = '1'):
475 i = self.num_accounts(account_type)
476 account_id = self.account_id(account_type,i)
478 if account_type is '1':
479 master_c0, master_K0, _ = self.master_public_keys["m/0'/"]
480 c0, K0, cK0 = bip32_public_derivation(master_c0.decode('hex'), master_K0.decode('hex'), "m/0'/", "m/0'/%d"%i)
481 account = BIP32_Account({ 'c':c0, 'K':K0, 'cK':cK0 })
483 elif account_type == '2of2':
484 master_c1, master_K1, _ = self.master_public_keys["m/1'/"]
485 c1, K1, cK1 = bip32_public_derivation(master_c1.decode('hex'), master_K1.decode('hex'), "m/1'/", "m/1'/%d"%i)
486 master_c2, master_K2, _ = self.master_public_keys["m/2'/"]
487 c2, K2, cK2 = bip32_public_derivation(master_c2.decode('hex'), master_K2.decode('hex'), "m/2'/", "m/2'/%d"%i)
488 account = BIP32_Account_2of2({ 'c':c1, 'K':K1, 'cK':cK1, 'c2':c2, 'K2':K2, 'cK2':cK2 })
490 elif account_type == '2of3':
491 master_c3, master_K3, _ = self.master_public_keys["m/3'/"]
492 c3, K3, cK3 = bip32_public_derivation(master_c3.decode('hex'), master_K3.decode('hex'), "m/3'/", "m/3'/%d"%i)
493 master_c4, master_K4, _ = self.master_public_keys["m/4'/"]
494 c4, K4, cK4 = bip32_public_derivation(master_c4.decode('hex'), master_K4.decode('hex'), "m/4'/", "m/4'/%d"%i)
495 master_c5, master_K5, _ = self.master_public_keys["m/5'/"]
496 c5, K5, cK5 = bip32_public_derivation(master_c5.decode('hex'), master_K5.decode('hex'), "m/5'/", "m/5'/%d"%i)
497 account = BIP32_Account_2of3({ 'c':c3, 'K':K3, 'cK':cK3, 'c2':c4, 'K2':K4, 'cK2':cK4, 'c3':c5, 'K3':K5, 'cK3':cK5 })
499 return account_id, account
502 def set_label(self, name, text = None):
504 old_text = self.labels.get(name)
507 self.labels[name] = text
511 self.labels.pop(name)
515 self.storage.put('labels', self.labels, True)
517 run_hook('set_label', name, text, changed)
522 def create_account(self, account_type = '1', name = None):
523 k, account = self.next_account(account_type)
524 if k in self.pending_accounts:
525 self.pending_accounts.pop(k)
526 self.storage.put('pending_accounts', self.pending_accounts)
528 self.accounts[k] = account
531 self.set_label(k, name)
534 def create_old_account(self, mpk):
535 self.storage.put('master_public_key', mpk, True)
536 self.accounts[0] = OldAccount({'mpk':mpk, 0:[], 1:[]})
540 def save_accounts(self):
542 for k, v in self.accounts.items():
544 self.storage.put('accounts', d, True)
548 def load_accounts(self):
549 d = self.storage.get('accounts', {})
551 for k, v in d.items():
553 v['mpk'] = self.storage.get('master_public_key')
554 self.accounts[k] = OldAccount(v)
556 self.accounts[k] = BIP32_Account_2of2(v)
558 self.accounts[k] = BIP32_Account(v)
560 self.pending_accounts = self.storage.get('pending_accounts',{})
563 def delete_pending_account(self, k):
564 self.pending_accounts.pop(k)
565 self.storage.put('pending_accounts', self.pending_accounts)
567 def account_is_pending(self, k):
568 return k in self.pending_accounts
570 def create_pending_account(self, acct_type, name):
571 k, addr = self.new_account_address(acct_type)
572 self.set_label(k, name)
573 self.pending_accounts[k] = addr
574 self.storage.put('pending_accounts', self.pending_accounts)
576 def get_pending_accounts(self):
577 return self.pending_accounts.items()
580 def addresses(self, include_change = True, _next=True):
581 o = self.get_account_addresses(-1, include_change)
582 for a in self.accounts.keys():
583 o += self.get_account_addresses(a, include_change)
586 for addr in self.next_addresses.values():
592 def is_mine(self, address):
593 return address in self.addresses(True)
596 def is_change(self, address):
597 if not self.is_mine(address): return False
598 if address in self.imported_keys.keys(): return False
599 acct, s = self.get_address_index(address)
600 if s is None: return False
603 def get_master_public_key(self):
604 if self.seed_version == 4:
605 return self.storage.get("master_public_key")
607 c, K, cK = self.storage.get("master_public_keys")["m/0'/"]
610 def get_master_private_key(self, account, password):
611 k = self.master_private_keys.get(account)
613 master_k = pw_decode( k, password)
614 master_c, master_K, master_Kc = self.master_public_keys[account]
616 K, Kc = get_pubkeys_from_secret(master_k.decode('hex'))
617 assert K.encode('hex') == master_K
619 raise Exception("Invalid password")
623 def get_address_index(self, address):
624 if address in self.imported_keys.keys():
627 for account in self.accounts.keys():
628 for for_change in [0,1]:
629 addresses = self.accounts[account].get_addresses(for_change)
630 for addr in addresses:
632 return account, (for_change, addresses.index(addr))
634 for k,v in self.next_addresses.items():
638 raise Exception("Address not found", address)
641 def get_roots(self, account):
643 for a in account.split('&'):
645 m = re.match("(m/\d+'/)(\d+)", s)
646 roots.append( m.group(1) )
649 def is_seeded(self, account):
650 if type(account) is int:
651 return self.seed is not None
653 for root in self.get_roots(account):
654 if root not in self.master_private_keys.keys():
658 def rebase_sequence(self, account, sequence):
661 for a in account.split('&'):
663 m = re.match("(m/\d+'/)(\d+)", s)
665 num = int(m.group(2))
666 dd.append( (root, [num,c,i] ) )
670 def get_keyID(self, account, sequence):
673 mpk = self.storage.get('master_public_key')
674 return 'old(%s,%d,%d)'%(mpk,a,b)
676 rs = self.rebase_sequence(account, sequence)
678 for root, public_sequence in rs:
679 c, K, _ = self.master_public_keys[root]
680 s = '/' + '/'.join( map(lambda x:str(x), public_sequence) )
681 dd.append( 'bip32(%s,%s,%s)'%(c,K, s) )
686 def get_seed(self, password):
687 s = pw_decode(self.seed, password)
688 if self.seed_version == 4:
690 self.accounts[0].check_seed(seed)
692 seed = mnemonic_hash(s)
696 def get_mnemonic(self, password):
698 s = pw_decode(self.seed, password)
699 if self.seed_version == 4:
700 return ' '.join(mnemonic.mn_encode(s))
706 def get_private_key(self, address, password):
707 if self.is_watching_only():
710 # first check the provided password
711 seed = self.get_seed(password)
714 if address in self.imported_keys.keys():
715 out.append( pw_decode( self.imported_keys[address], password ) )
717 account, sequence = self.get_address_index(address)
719 pk = self.accounts[account].get_private_key(seed, sequence)
723 # assert address == self.accounts[account].get_address(*sequence)
724 rs = self.rebase_sequence( account, sequence)
725 for root, public_sequence in rs:
727 if root not in self.master_private_keys.keys(): continue
728 master_k = self.get_master_private_key(root, password)
729 master_c, _, _ = self.master_public_keys[root]
730 pk = bip32_private_key( public_sequence, master_k.decode('hex'), master_c.decode('hex'))
736 def add_keypairs_from_wallet(self, tx, keypairs, password):
737 for txin in tx.inputs:
738 address = txin['address']
739 if not self.is_mine(address):
741 private_keys = self.get_private_key(address, password)
742 for sec in private_keys:
743 pubkey = public_key_from_private_key(sec)
744 keypairs[ pubkey ] = sec
745 if address in self.imported_keys.keys():
746 txin['redeemPubkey'] = pubkey
749 def add_keypairs_from_KeyID(self, tx, keypairs, password):
750 # first check the provided password
751 seed = self.get_seed(password)
753 for txin in tx.inputs:
754 keyid = txin.get('KeyID')
757 if self.seed_version == 4:
758 m = re.match("old\(([0-9a-f]+),(\d+),(\d+)", keyid)
761 if mpk != self.storage.get('master_public_key'): continue
762 for_change = int(m.group(2))
763 num = int(m.group(3))
764 account = self.accounts[0]
765 addr = account.get_address(for_change, num)
766 txin['address'] = addr # fixme: side effect
767 pk = account.get_private_key(seed, (for_change, num))
768 pubkey = public_key_from_private_key(pk)
769 keypairs[pubkey] = pk
774 for s in keyid.split('&'):
775 m = re.match("bip32\(([0-9a-f]+),([0-9a-f]+),(/\d+/\d+/\d+)", s)
779 sequence = m.group(3)
780 root = self.find_root_by_master_key(c,K)
781 if not root: continue
782 sequence = map(lambda x:int(x), sequence.strip('/').split('/'))
783 root = root + '%d'%sequence[0]
784 sequence = sequence[1:]
785 roots.append((root,sequence))
787 account_id = " & ".join( map(lambda x:x[0], roots) )
788 account = self.accounts.get(account_id)
789 if not account: continue
790 addr = account.get_address(*sequence)
791 txin['address'] = addr # fixme: side effect
792 pk = self.get_private_key(addr, password)
794 pubkey = public_key_from_private_key(sec)
795 keypairs[pubkey] = sec
799 def signrawtransaction(self, tx, input_info, private_keys, password):
801 # check that the password is correct
802 seed = self.get_seed(password)
805 tx.add_input_info(input_info)
807 # add redeem script for coins that are in the wallet
808 # FIXME: add redeemPubkey too!
809 unspent_coins = self.get_unspent_coins()
810 for txin in tx.inputs:
811 for item in unspent_coins:
812 if txin['prevout_hash'] == item['prevout_hash'] and txin['prevout_n'] == item['prevout_n']:
813 print_error( "tx input is in unspent coins" )
814 txin['scriptPubKey'] = item['scriptPubKey']
815 account, sequence = self.get_address_index(item['address'])
817 txin['redeemScript'] = self.accounts[account].redeem_script(sequence)
818 print_error("added redeemScript", txin['redeemScript'])
822 # build a list of public/private keys
825 # add private keys from parameter
826 for sec in private_keys:
827 pubkey = public_key_from_private_key(sec)
828 keypairs[ pubkey ] = sec
830 # add private_keys from KeyID
831 self.add_keypairs_from_KeyID(tx, keypairs, password)
833 # add private keys from wallet
834 self.add_keypairs_from_wallet(tx, keypairs, password)
835 self.sign_transaction(tx, keypairs)
838 def sign_message(self, address, message, password):
839 keys = self.get_private_key(address, password)
840 assert len(keys) == 1
842 key = regenerate_key(sec)
843 compressed = is_compressed(sec)
844 return key.sign_message(message, compressed, address)
847 def change_gap_limit(self, value):
848 if value >= self.gap_limit:
849 self.gap_limit = value
850 self.storage.put('gap_limit', self.gap_limit, True)
851 #self.interface.poke('synchronizer')
854 elif value >= self.min_acceptable_gap():
855 for key, account in self.accounts.items():
856 addresses = account[0]
857 k = self.num_unused_trailing_addresses(addresses)
858 n = len(addresses) - k + value
859 addresses = addresses[0:n]
860 self.accounts[key][0] = addresses
862 self.gap_limit = value
863 self.storage.put('gap_limit', self.gap_limit, True)
869 def num_unused_trailing_addresses(self, addresses):
871 for a in addresses[::-1]:
872 if self.history.get(a):break
876 def min_acceptable_gap(self):
877 # fixme: this assumes wallet is synchronized
881 for account in self.accounts.values():
882 addresses = account.get_addresses(0)
883 k = self.num_unused_trailing_addresses(addresses)
884 for a in addresses[0:-k]:
885 if self.history.get(a):
889 if n > nmax: nmax = n
893 def address_is_old(self, address):
895 h = self.history.get(address, [])
898 for tx_hash, tx_height in h:
902 tx_age = self.verifier.blockchain.height() - tx_height + 1
908 def synchronize_sequence(self, account, for_change):
909 limit = self.gap_limit_for_change if for_change else self.gap_limit
912 addresses = account.get_addresses(for_change)
913 if len(addresses) < limit:
914 address = account.create_new_address(for_change)
915 self.history[address] = []
916 new_addresses.append( address )
919 if map( lambda a: self.address_is_old(a), addresses[-limit:] ) == limit*[False]:
922 address = account.create_new_address(for_change)
923 self.history[address] = []
924 new_addresses.append( address )
930 def create_pending_accounts(self):
931 for account_type in ['1','2of2','2of3']:
932 if not self.has_master_public_keys(account_type):
934 k, a = self.new_account_address(account_type)
935 if self.address_is_old(a):
936 print_error( "creating account", a )
937 self.create_account(account_type)
938 self.next_addresses.pop(k)
941 def synchronize_account(self, account):
943 new += self.synchronize_sequence(account, 0)
944 new += self.synchronize_sequence(account, 1)
948 def synchronize(self):
949 if self.master_public_keys:
950 self.create_pending_accounts()
952 for account in self.accounts.values():
953 new += self.synchronize_account(account)
956 self.storage.put('addr_history', self.history, True)
961 return self.history.values() != [[]] * len(self.history)
964 def add_contact(self, address, label=None):
965 self.addressbook.append(address)
966 self.storage.put('contacts', self.addressbook, True)
968 self.set_label(address, label)
971 def delete_contact(self, addr):
972 if addr in self.addressbook:
973 self.addressbook.remove(addr)
974 self.storage.put('addressbook', self.addressbook, True)
977 def fill_addressbook(self):
978 for tx_hash, tx in self.transactions.items():
979 is_relevant, is_send, _, _ = self.get_tx_value(tx)
981 for addr, v in tx.outputs:
982 if not self.is_mine(addr) and addr not in self.addressbook:
983 self.addressbook.append(addr)
985 # self.update_tx_labels()
987 def get_num_tx(self, address):
989 for tx in self.transactions.values():
990 if address in map(lambda x:x[0], tx.outputs): n += 1
994 def get_address_flags(self, addr):
995 flags = "C" if self.is_change(addr) else "I" if addr in self.imported_keys.keys() else "-"
996 flags += "F" if addr in self.frozen_addresses else "-"
1000 def get_tx_value(self, tx, account=None):
1001 domain = self.get_account_addresses(account)
1002 return tx.get_value(domain, self.prevout_values)
1005 def update_tx_outputs(self, tx_hash):
1006 tx = self.transactions.get(tx_hash)
1008 for i, (addr, value) in enumerate(tx.outputs):
1009 key = tx_hash+ ':%d'%i
1010 self.prevout_values[key] = value
1012 for item in tx.inputs:
1013 if self.is_mine(item.get('address')):
1014 key = item['prevout_hash'] + ':%d'%item['prevout_n']
1015 self.spent_outputs.append(key)
1018 def get_addr_balance(self, address):
1019 assert self.is_mine(address)
1020 h = self.history.get(address,[])
1021 if h == ['*']: return 0,0
1023 received_coins = [] # list of coins received at address
1025 for tx_hash, tx_height in h:
1026 tx = self.transactions.get(tx_hash)
1029 for i, (addr, value) in enumerate(tx.outputs):
1031 key = tx_hash + ':%d'%i
1032 received_coins.append(key)
1034 for tx_hash, tx_height in h:
1035 tx = self.transactions.get(tx_hash)
1039 for item in tx.inputs:
1040 addr = item.get('address')
1042 key = item['prevout_hash'] + ':%d'%item['prevout_n']
1043 value = self.prevout_values.get( key )
1044 if key in received_coins:
1047 for i, (addr, value) in enumerate(tx.outputs):
1048 key = tx_hash + ':%d'%i
1059 def get_account_name(self, k):
1061 if self.seed_version == 4:
1062 name = 'Main account'
1064 name = 'Old account'
1066 default = "Unnamed account"
1067 m = re.match("m/0'/(\d+)", k)
1071 default = "Main account"
1073 default = "Account %s"%num
1075 m = re.match("m/1'/(\d+) & m/2'/(\d+)", k)
1078 default = "2of2 account %s"%num
1079 name = self.labels.get(k, default)
1083 def get_account_names(self):
1085 for k, account in self.accounts.items():
1086 accounts[k] = self.get_account_name(k)
1087 if self.imported_keys:
1088 accounts[-1] = 'Imported keys'
1091 def get_account_addresses(self, a, include_change=True):
1093 o = self.addresses(True)
1095 o = self.imported_keys.keys()
1097 ac = self.accounts[a]
1098 o = ac.get_addresses(0)
1099 if include_change: o += ac.get_addresses(1)
1102 def get_imported_balance(self):
1103 return self.get_balance(self.imported_keys.keys())
1105 def get_account_balance(self, account):
1106 return self.get_balance(self.get_account_addresses(account))
1108 def get_frozen_balance(self):
1109 return self.get_balance(self.frozen_addresses)
1111 def get_balance(self, domain=None):
1112 if domain is None: domain = self.addresses(True)
1115 c, u = self.get_addr_balance(addr)
1121 def get_unspent_coins(self, domain=None):
1123 if domain is None: domain = self.addresses(True)
1125 h = self.history.get(addr, [])
1126 if h == ['*']: continue
1127 for tx_hash, tx_height in h:
1128 tx = self.transactions.get(tx_hash)
1129 if tx is None: raise Exception("Wallet not synchronized")
1130 is_coinbase = tx.inputs[0].get('prevout_hash') == '0'*64
1131 for o in tx.d.get('outputs'):
1133 if output.get('address') != addr: continue
1134 key = tx_hash + ":%d" % output.get('prevout_n')
1135 if key in self.spent_outputs: continue
1136 output['prevout_hash'] = tx_hash
1137 output['height'] = tx_height
1138 output['coinbase'] = is_coinbase
1139 coins.append((tx_height, output))
1143 coins = sorted(coins)
1144 if coins[-1][0] != 0:
1145 while coins[0][0] == 0:
1146 coins = coins[1:] + [ coins[0] ]
1147 return [x[1] for x in coins]
1150 def choose_tx_inputs( self, amount, fixed_fee, domain = None ):
1151 """ todo: minimize tx size """
1153 fee = self.fee if fixed_fee is None else fixed_fee
1155 domain = self.addresses(True)
1157 for i in self.frozen_addresses:
1158 if i in domain: domain.remove(i)
1160 coins = self.get_unspent_coins(domain)
1164 if item.get('coinbase') and item.get('height') + COINBASE_MATURITY > self.network.blockchain.height:
1166 addr = item.get('address')
1167 v = item.get('value')
1170 fee = self.estimated_fee(inputs) if fixed_fee is None else fixed_fee
1171 if total >= amount + fee: break
1175 return inputs, total, fee
1178 def set_fee(self, fee):
1181 self.storage.put('fee_per_kb', self.fee, True)
1183 def estimated_fee(self, inputs):
1184 estimated_size = len(inputs) * 180 + 80 # this assumes non-compressed keys
1185 fee = self.fee * int(round(estimated_size/1024.))
1186 if fee == 0: fee = self.fee
1190 def add_tx_change( self, inputs, outputs, amount, fee, total, change_addr=None):
1191 "add change to a transaction"
1192 change_amount = total - ( amount + fee )
1193 if change_amount > DUST_THRESHOLD:
1196 # send change to one of the accounts involved in the tx
1197 address = inputs[0].get('address')
1198 account, _ = self.get_address_index(address)
1200 if not self.use_change or account == -1:
1201 change_addr = inputs[-1]['address']
1203 change_addr = self.accounts[account].get_addresses(1)[-self.gap_limit_for_change]
1205 # Insert the change output at a random position in the outputs
1206 posn = random.randint(0, len(outputs))
1207 outputs[posn:posn] = [( change_addr, change_amount)]
1211 def get_history(self, address):
1213 return self.history.get(address)
1216 def get_status(self, h):
1217 if not h: return None
1218 if h == ['*']: return '*'
1220 for tx_hash, height in h:
1221 status += tx_hash + ':%d:' % height
1222 return hashlib.sha256( status ).digest().encode('hex')
1225 def receive_tx_callback(self, tx_hash, tx, tx_height):
1227 with self.transaction_lock:
1228 self.add_extra_addresses(tx)
1229 if not self.check_new_tx(tx_hash, tx):
1230 # may happen due to pruning
1231 print_error("received transaction that is no longer referenced in history", tx_hash)
1233 self.transactions[tx_hash] = tx
1234 self.network.interface.pending_transactions_for_notifications.append(tx)
1235 self.save_transactions()
1236 if self.verifier and tx_height>0:
1237 self.verifier.add(tx_hash, tx_height)
1238 self.update_tx_outputs(tx_hash)
1241 def save_transactions(self):
1243 for k,v in self.transactions.items():
1245 self.storage.put('transactions', tx, True)
1247 def receive_history_callback(self, addr, hist):
1249 if not self.check_new_history(addr, hist):
1250 raise Exception("error: received history for %s is not consistent with known transactions"%addr)
1253 self.history[addr] = hist
1254 self.storage.put('addr_history', self.history, True)
1257 for tx_hash, tx_height in hist:
1259 # add it in case it was previously unconfirmed
1260 if self.verifier: self.verifier.add(tx_hash, tx_height)
1263 def get_tx_history(self, account=None):
1264 if not self.verifier:
1267 with self.transaction_lock:
1268 history = self.transactions.items()
1269 history.sort(key = lambda x: self.verifier.get_txpos(x[0]))
1273 for tx_hash, tx in history:
1274 is_relevant, is_mine, v, fee = self.get_tx_value(tx, account)
1275 if v is not None: balance += v
1277 c, u = self.get_account_balance(account)
1280 result.append( ('', 1000, 0, c+u-balance, None, c+u-balance, None ) )
1282 balance = c + u - balance
1283 for tx_hash, tx in history:
1284 is_relevant, is_mine, value, fee = self.get_tx_value(tx, account)
1287 if value is not None:
1290 conf, timestamp = self.verifier.get_confirmations(tx_hash) if self.verifier else (None, None)
1291 result.append( (tx_hash, conf, is_mine, value, fee, balance, timestamp) )
1296 def get_label(self, tx_hash):
1297 label = self.labels.get(tx_hash)
1298 is_default = (label == '') or (label is None)
1299 if is_default: label = self.get_default_label(tx_hash)
1300 return label, is_default
1303 def get_default_label(self, tx_hash):
1304 tx = self.transactions.get(tx_hash)
1307 is_relevant, is_mine, _, _ = self.get_tx_value(tx)
1309 for o in tx.outputs:
1311 if not self.is_mine(o_addr):
1313 default_label = self.labels[o_addr]
1315 default_label = o_addr
1318 default_label = '(internal)'
1320 for o in tx.outputs:
1322 if self.is_mine(o_addr) and not self.is_change(o_addr):
1325 for o in tx.outputs:
1327 if self.is_mine(o_addr):
1333 dest_label = self.labels.get(o_addr)
1335 default_label = self.labels[o_addr]
1337 default_label = o_addr
1339 return default_label
1342 def make_unsigned_transaction(self, outputs, fee=None, change_addr=None, domain=None ):
1343 for address, x in outputs:
1344 assert is_valid(address)
1345 amount = sum( map(lambda x:x[1], outputs) )
1346 inputs, total, fee = self.choose_tx_inputs( amount, fee, domain )
1348 raise ValueError("Not enough funds")
1349 self.add_input_info(inputs)
1350 outputs = self.add_tx_change(inputs, outputs, amount, fee, total, change_addr)
1351 return Transaction.from_io(inputs, outputs)
1354 def mktx(self, outputs, password, fee=None, change_addr=None, domain= None ):
1355 tx = self.make_unsigned_transaction(outputs, fee, change_addr, domain)
1357 self.add_keypairs_from_wallet(tx, keypairs, password)
1359 self.sign_transaction(tx, keypairs)
1363 def add_input_info(self, inputs):
1365 address = txin['address']
1366 if address in self.imported_keys.keys():
1368 account, sequence = self.get_address_index(address)
1369 txin['KeyID'] = self.get_keyID(account, sequence)
1370 redeemScript = self.accounts[account].redeem_script(sequence)
1372 txin['redeemScript'] = redeemScript
1374 txin['redeemPubkey'] = self.accounts[account].get_pubkey(*sequence)
1377 def sign_transaction(self, tx, keypairs):
1379 run_hook('sign_transaction', tx)
1382 def sendtx(self, tx):
1384 h = self.send_tx(tx)
1385 self.tx_event.wait()
1386 return self.receive_tx(h, tx)
1388 def send_tx(self, tx):
1390 self.tx_event.clear()
1391 self.network.interface.send([('blockchain.transaction.broadcast', [str(tx)])], self.on_broadcast)
1394 def on_broadcast(self, i, r):
1395 self.tx_result = r.get('result')
1398 def receive_tx(self, tx_hash, tx):
1399 out = self.tx_result
1401 return False, "error: " + out
1402 run_hook('receive_tx', tx, self)
1407 def update_password(self, old_password, new_password):
1408 if new_password == '': new_password = None
1409 decoded = self.get_seed(old_password)
1410 self.seed = pw_encode( decoded, new_password)
1411 self.storage.put('seed', self.seed, True)
1412 self.use_encryption = (new_password != None)
1413 self.storage.put('use_encryption', self.use_encryption,True)
1414 for k in self.imported_keys.keys():
1415 a = self.imported_keys[k]
1416 b = pw_decode(a, old_password)
1417 c = pw_encode(b, new_password)
1418 self.imported_keys[k] = c
1419 self.storage.put('imported_keys', self.imported_keys, True)
1421 for k, v in self.master_private_keys.items():
1422 b = pw_decode(v, old_password)
1423 c = pw_encode(b, new_password)
1424 self.master_private_keys[k] = c
1425 self.storage.put('master_private_keys', self.master_private_keys, True)
1428 def freeze(self,addr):
1429 if self.is_mine(addr) and addr not in self.frozen_addresses:
1430 self.frozen_addresses.append(addr)
1431 self.storage.put('frozen_addresses', self.frozen_addresses, True)
1436 def unfreeze(self,addr):
1437 if self.is_mine(addr) and addr in self.frozen_addresses:
1438 self.frozen_addresses.remove(addr)
1439 self.storage.put('frozen_addresses', self.frozen_addresses, True)
1445 def set_verifier(self, verifier):
1446 self.verifier = verifier
1448 # review transactions that are in the history
1449 for addr, hist in self.history.items():
1450 if hist == ['*']: continue
1451 for tx_hash, tx_height in hist:
1453 # add it in case it was previously unconfirmed
1454 self.verifier.add(tx_hash, tx_height)
1457 # if we are on a pruning server, remove unverified transactions
1458 vr = self.verifier.transactions.keys() + self.verifier.verified_tx.keys()
1459 for tx_hash in self.transactions.keys():
1460 if tx_hash not in vr:
1461 self.transactions.pop(tx_hash)
1465 def check_new_history(self, addr, hist):
1467 # check that all tx in hist are relevant
1469 for tx_hash, height in hist:
1470 tx = self.transactions.get(tx_hash)
1472 if not tx.has_address(addr):
1475 # check that we are not "orphaning" a transaction
1476 old_hist = self.history.get(addr,[])
1477 if old_hist == ['*']: return True
1479 for tx_hash, height in old_hist:
1480 if tx_hash in map(lambda x:x[0], hist): continue
1482 for _addr, _hist in self.history.items():
1483 if _addr == addr: continue
1484 if _hist == ['*']: continue
1485 _tx_hist = map(lambda x:x[0], _hist)
1486 if tx_hash in _tx_hist:
1491 tx = self.transactions.get(tx_hash)
1492 # tx might not be there
1496 if self.verifier.get_height(tx_hash):
1499 print_error("new history is orphaning transaction:", tx_hash)
1500 # check that all outputs are not mine, request histories
1502 for _addr, _v in tx.outputs:
1503 # assert not self.is_mine(_addr)
1504 ext_requests.append( ('blockchain.address.get_history', [_addr]) )
1506 ext_h = self.network.synchronous_get(ext_requests)
1507 print_error("sync:", ext_requests, ext_h)
1510 if h == ['*']: continue
1512 if item.get('tx_hash') == tx_hash:
1513 height = item.get('height')
1515 print_error("found height for", tx_hash, height)
1516 self.verifier.add(tx_hash, height)
1518 print_error("removing orphaned tx from history", tx_hash)
1519 self.transactions.pop(tx_hash)
1525 def check_new_tx(self, tx_hash, tx):
1526 # 1 check that tx is referenced in addr_history.
1528 for addr, hist in self.history.items():
1529 if hist == ['*']:continue
1530 for txh, height in hist:
1532 addresses.append(addr)
1537 # 2 check that referencing addresses are in the tx
1538 for addr in addresses:
1539 if not tx.has_address(addr):
1545 def start_threads(self, network):
1546 from verifier import TxVerifier
1547 self.network = network
1548 if self.network is not None:
1549 self.verifier = TxVerifier(self.network, self.storage)
1550 self.verifier.start()
1551 self.set_verifier(self.verifier)
1552 self.synchronizer = WalletSynchronizer(self, network)
1553 self.synchronizer.start()
1555 self.verifier = None
1556 self.synchronizer =None
1558 def stop_threads(self):
1560 self.verifier.stop()
1561 self.synchronizer.stop()
1564 def restore(self, callback):
1566 def wait_for_wallet():
1567 self.set_up_to_date(False)
1568 while not self.is_up_to_date():
1569 msg = "%s\n%s %d\n%s %.1f"%(
1570 _("Please wait..."),
1571 _("Addresses generated:"),
1572 len(self.addresses(True)),
1573 _("Kilobytes received:"),
1574 self.network.interface.bytes_received/1024.)
1576 apply(callback, (msg,))
1579 def wait_for_network():
1580 while not self.network.is_connected():
1581 msg = "%s \n" % (_("Connecting..."))
1582 apply(callback, (msg,))
1585 # wait until we are connected, because the user might have selected another server
1592 self.fill_addressbook()
1597 class WalletSynchronizer(threading.Thread):
1600 def __init__(self, wallet, network):
1601 threading.Thread.__init__(self)
1603 self.wallet = wallet
1604 self.network = network
1605 self.was_updated = True
1606 self.running = False
1607 self.lock = threading.Lock()
1608 self.queue = Queue.Queue()
1611 with self.lock: self.running = False
1613 def is_running(self):
1614 with self.lock: return self.running
1617 def subscribe_to_addresses(self, addresses):
1619 for addr in addresses:
1620 messages.append(('blockchain.address.subscribe', [addr]))
1621 self.network.subscribe( messages, lambda i,r: self.queue.put(r))
1628 while self.is_running():
1630 if not self.network.is_connected():
1631 self.network.wait_until_connected()
1633 self.run_interface()
1636 def run_interface(self):
1638 print_error("synchronizer: connected to", self.network.main_server())
1642 requested_histories = {}
1644 # request any missing transactions
1645 for history in self.wallet.history.values():
1646 if history == ['*']: continue
1647 for tx_hash, tx_height in history:
1648 if self.wallet.transactions.get(tx_hash) is None and (tx_hash, tx_height) not in missing_tx:
1649 missing_tx.append( (tx_hash, tx_height) )
1652 print_error("missing tx", missing_tx)
1655 self.subscribe_to_addresses(self.wallet.addresses(True))
1657 while self.is_running():
1658 # 1. create new addresses
1659 new_addresses = self.wallet.synchronize()
1661 # request missing addresses
1663 self.subscribe_to_addresses(new_addresses)
1665 # request missing transactions
1666 for tx_hash, tx_height in missing_tx:
1667 if (tx_hash, tx_height) not in requested_tx:
1668 self.network.send([ ('blockchain.transaction.get',[tx_hash, tx_height]) ], lambda i,r: self.queue.put(r))
1669 requested_tx.append( (tx_hash, tx_height) )
1672 # detect if situation has changed
1673 if self.network.is_up_to_date() and self.queue.empty():
1674 if not self.wallet.is_up_to_date():
1675 self.wallet.set_up_to_date(True)
1676 self.was_updated = True
1678 if self.wallet.is_up_to_date():
1679 self.wallet.set_up_to_date(False)
1680 self.was_updated = True
1682 if self.was_updated:
1683 self.network.trigger_callback('updated')
1684 self.was_updated = False
1688 r = self.queue.get(block=True, timeout=1)
1693 #if interface != self.network.interface:
1699 # 3. handle response
1700 method = r['method']
1701 params = r['params']
1702 result = r.get('result')
1703 error = r.get('error')
1708 if method == 'blockchain.address.subscribe':
1710 if self.wallet.get_status(self.wallet.get_history(addr)) != result:
1711 if requested_histories.get(addr) is None:
1712 self.network.send([('blockchain.address.get_history', [addr])], lambda i,r:self.queue.put(r))
1713 requested_histories[addr] = result
1715 elif method == 'blockchain.address.get_history':
1717 print_error("receiving history", addr, result)
1719 assert requested_histories.pop(addr) == '*'
1720 self.wallet.receive_history_callback(addr, result)
1723 # check that txids are unique
1726 tx_hash = item['tx_hash']
1727 if tx_hash not in txids:
1728 txids.append(tx_hash)
1729 hist.append( (tx_hash, item['height']) )
1731 if len(hist) != len(result):
1732 raise Exception("error: server sent history with non-unique txid", result)
1734 # check that the status corresponds to what was announced
1735 rs = requested_histories.pop(addr)
1736 if self.wallet.get_status(hist) != rs:
1737 raise Exception("error: status mismatch: %s"%addr)
1739 # store received history
1740 self.wallet.receive_history_callback(addr, hist)
1742 # request transactions that we don't have
1743 for tx_hash, tx_height in hist:
1744 if self.wallet.transactions.get(tx_hash) is None:
1745 if (tx_hash, tx_height) not in requested_tx and (tx_hash, tx_height) not in missing_tx:
1746 missing_tx.append( (tx_hash, tx_height) )
1748 elif method == 'blockchain.transaction.get':
1750 tx_height = params[1]
1751 assert tx_hash == hash_encode(Hash(result.decode('hex')))
1752 tx = Transaction(result)
1753 self.wallet.receive_tx_callback(tx_hash, tx, tx_height)
1754 self.was_updated = True
1755 requested_tx.remove( (tx_hash, tx_height) )
1756 print_error("received tx:", tx_hash, len(tx.raw))
1759 print_error("Error: Unknown message:" + method + ", " + repr(params) + ", " + repr(result) )
1761 if self.was_updated and not requested_tx:
1762 self.network.trigger_callback('updated')
1763 # Updated gets called too many times from other places as well; if we use that signal we get the notification three times
1764 self.network.trigger_callback("new_transaction")
1765 self.was_updated = False