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 return self.data.get(key, default)
130 def put(self, key, value, save = True):
133 if value is not None:
134 self.data[key] = value
142 f = open(self.path,"w")
145 if 'ANDROID_DATA' not in os.environ:
147 os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)
152 def __init__(self, storage):
154 self.storage = storage
155 self.electrum_version = ELECTRUM_VERSION
156 self.gap_limit_for_change = 3 # constant
159 self.seed_version = storage.get('seed_version', SEED_VERSION)
161 self.gap_limit = storage.get('gap_limit', 5)
162 self.use_change = storage.get('use_change',True)
163 self.use_encryption = storage.get('use_encryption', False)
164 self.seed = storage.get('seed', '') # encrypted
165 self.labels = storage.get('labels', {})
166 self.frozen_addresses = storage.get('frozen_addresses',[])
167 self.prioritized_addresses = storage.get('prioritized_addresses',[])
168 self.addressbook = storage.get('contacts', [])
170 self.imported_keys = storage.get('imported_keys',{})
171 self.history = storage.get('addr_history',{}) # address -> list(txid, height)
173 self.fee = int(storage.get('fee_per_kb',20000))
175 self.master_public_keys = storage.get('master_public_keys',{})
176 self.master_private_keys = storage.get('master_private_keys', {})
178 self.next_addresses = storage.get('next_addresses',{})
180 if self.seed_version not in [4, 6]:
181 msg = "This wallet seed is not supported."
182 if self.seed_version in [5]:
183 msg += "\nTo open this wallet, try 'git checkout seed_v%d'"%self.seed_version
190 self.transactions = {}
191 tx_list = self.storage.get('transactions',{})
192 for k,v in tx_list.items():
196 print_msg("Warning: Cannot deserialize transactions. skipping")
199 self.add_extra_addresses(tx)
200 self.transactions[k] = tx
202 for h,tx in self.transactions.items():
203 if not self.check_new_tx(h, tx):
204 print_error("removing unreferenced tx", h)
205 self.transactions.pop(h)
209 self.prevout_values = {} # my own transaction outputs
210 self.spent_outputs = []
215 # there is a difference between wallet.up_to_date and interface.is_up_to_date()
216 # interface.is_up_to_date() returns true when all requests have been answered and processed
217 # wallet.up_to_date is true when the wallet is synchronized (stronger requirement)
219 self.up_to_date = False
220 self.lock = threading.Lock()
221 self.transaction_lock = threading.Lock()
222 self.tx_event = threading.Event()
224 for tx_hash, tx in self.transactions.items():
225 self.update_tx_outputs(tx_hash)
228 def add_extra_addresses(self, tx):
230 # find the address corresponding to pay-to-pubkey inputs
231 tx.add_extra_addresses(self.transactions)
232 for o in tx.d.get('outputs'):
233 if o.get('is_pubkey'):
234 for tx2 in self.transactions.values():
235 tx2.add_extra_addresses({h:tx})
240 def set_up_to_date(self,b):
241 with self.lock: self.up_to_date = b
244 def is_up_to_date(self):
245 with self.lock: return self.up_to_date
249 self.up_to_date = False
250 while not self.is_up_to_date():
254 def import_key(self, sec, password):
256 seed = self.get_seed(password)
258 address = address_from_private_key(sec)
260 raise Exception('Invalid private key')
262 if self.is_mine(address):
263 raise Exception('Address already in wallet')
265 # store the originally requested keypair into the imported keys table
266 self.imported_keys[address] = pw_encode(sec, password )
267 self.storage.put('imported_keys', self.imported_keys, True)
268 if self.synchronizer:
269 self.synchronizer.subscribe_to_addresses([address])
272 def delete_imported_key(self, addr):
273 if addr in self.imported_keys:
274 self.imported_keys.pop(addr)
275 self.storage.put('imported_keys', self.imported_keys, True)
279 import mnemonic, ecdsa
280 entropy = ecdsa.util.randrange( pow(2,160) )
283 ss = "%040x"%(entropy+nonce)
284 s = hashlib.sha256(ss.decode('hex')).digest().encode('hex')
285 # we keep only 13 words, that's approximately 139 bits of entropy
286 words = mnemonic.mn_encode(s)[0:13]
287 seed = ' '.join(words)
288 if mnemonic_hash(seed).startswith(SEED_PREFIX):
289 break # this removes 12 bits of entropy
295 def init_seed(self, seed):
299 raise Exception("a seed exists")
302 self.seed = random_seed(128)
303 self.seed_version = 4
307 # self.seed = self.make_seed()
308 # self.seed_version = SEED_VERSION
311 # find out what kind of wallet we are
313 seed.strip().decode('hex')
314 self.seed_version = 4
315 self.seed = str(seed)
321 self.seed_version = 4
322 self.seed = mnemonic.mn_decode(words)
325 # mnemonic.mn_decode(words)
326 # uses_electrum_words = True
328 # uses_electrum_words = False
330 #if uses_electrum_words and len(words) != 13:
331 # self.seed_version = 4
332 # self.seed = mnemonic.mn_decode(words)
334 # assert mnemonic_hash(seed).startswith(SEED_PREFIX)
335 # self.seed_version = SEED_VERSION
340 self.storage.put('seed', self.seed, True)
341 self.storage.put('seed_version', self.seed_version, True)
342 self.create_accounts()
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):
366 if self.seed_version == 4:
367 mpk = OldAccount.mpk_from_seed(self.seed)
368 self.create_old_account(mpk)
370 # create default account
371 self.create_master_keys('1')
372 self.create_account('1','Main account')
375 def create_master_keys(self, account_type):
376 master_k, master_c, master_K, master_cK = bip32_init(self.get_seed(None))
377 if account_type == '1':
378 k0, c0, K0, cK0 = bip32_private_derivation(master_k, master_c, "m/", "m/0'/")
379 self.master_public_keys["m/0'/"] = (c0, K0, cK0)
380 self.master_private_keys["m/0'/"] = k0
381 elif account_type == '2of2':
382 k1, c1, K1, cK1 = bip32_private_derivation(master_k, master_c, "m/", "m/1'/")
383 k2, c2, K2, cK2 = bip32_private_derivation(master_k, master_c, "m/", "m/2'/")
384 self.master_public_keys["m/1'/"] = (c1, K1, cK1)
385 self.master_public_keys["m/2'/"] = (c2, K2, cK2)
386 self.master_private_keys["m/1'/"] = k1
387 self.master_private_keys["m/2'/"] = k2
388 elif account_type == '2of3':
389 k3, c3, K3, cK3 = bip32_private_derivation(master_k, master_c, "m/", "m/3'/")
390 k4, c4, K4, cK4 = bip32_private_derivation(master_k, master_c, "m/", "m/4'/")
391 k5, c5, K5, cK5 = bip32_private_derivation(master_k, master_c, "m/", "m/5'/")
392 self.master_public_keys["m/3'/"] = (c3, K3, cK3)
393 self.master_public_keys["m/4'/"] = (c4, K4, cK4)
394 self.master_public_keys["m/5'/"] = (c5, K5, cK5)
395 self.master_private_keys["m/3'/"] = k3
396 self.master_private_keys["m/4'/"] = k4
397 self.master_private_keys["m/5'/"] = k5
399 self.storage.put('master_public_keys', self.master_public_keys, True)
400 self.storage.put('master_private_keys', self.master_private_keys, True)
402 def has_master_public_keys(self, account_type):
403 if account_type == '1':
404 return "m/0'/" in self.master_public_keys
405 elif account_type == '2of2':
406 return set(["m/1'/", "m/2'/"]) <= set(self.master_public_keys.keys())
407 elif account_type == '2of3':
408 return set(["m/3'/", "m/4'/", "m/5'/"]) <= set(self.master_public_keys.keys())
410 def find_root_by_master_key(self, c, K):
411 for key, v in self.master_public_keys.items():
412 if key == "m/":continue
414 if (c == cc) and (K == KK):
417 def deseed_root(self, seed, password):
418 # for safety, we ask the user to enter their seed
419 assert seed == self.get_seed(password)
421 self.storage.put('seed', '', True)
424 def deseed_branch(self, k):
425 # check that parent has no seed
426 assert self.seed == ''
427 self.master_private_keys.pop(k)
428 self.storage.put('master_private_keys', self.master_private_keys, True)
430 def is_watching_only(self):
431 return (self.seed == '') and (self.master_private_keys == {})
435 def account_id(self, account_type, i):
436 if account_type == '1':
438 elif account_type == '2of2':
439 return "m/1'/%d & m/2'/%d"%(i,i)
440 elif account_type == '2of3':
441 return "m/3'/%d & m/4'/%d & m/5'/%d"%(i,i,i)
443 raise Exception('unknown account type')
446 def num_accounts(self, account_type):
447 keys = self.accounts.keys()
450 account_id = self.account_id(account_type, i)
451 if account_id not in keys: break
456 def new_account_address(self, account_type = '1'):
457 i = self.num_accounts(account_type)
458 k = self.account_id(account_type,i)
460 addr = self.next_addresses.get(k)
462 account_id, account = self.next_account(account_type)
463 addr = account.first_address()
464 self.next_addresses[k] = addr
465 self.storage.put('next_addresses',self.next_addresses)
470 def next_account(self, account_type = '1'):
472 i = self.num_accounts(account_type)
473 account_id = self.account_id(account_type,i)
475 if account_type is '1':
476 master_c0, master_K0, _ = self.master_public_keys["m/0'/"]
477 c0, K0, cK0 = bip32_public_derivation(master_c0.decode('hex'), master_K0.decode('hex'), "m/0'/", "m/0'/%d"%i)
478 account = BIP32_Account({ 'c':c0, 'K':K0, 'cK':cK0 })
480 elif account_type == '2of2':
481 master_c1, master_K1, _ = self.master_public_keys["m/1'/"]
482 c1, K1, cK1 = bip32_public_derivation(master_c1.decode('hex'), master_K1.decode('hex'), "m/1'/", "m/1'/%d"%i)
483 master_c2, master_K2, _ = self.master_public_keys["m/2'/"]
484 c2, K2, cK2 = bip32_public_derivation(master_c2.decode('hex'), master_K2.decode('hex'), "m/2'/", "m/2'/%d"%i)
485 account = BIP32_Account_2of2({ 'c':c1, 'K':K1, 'cK':cK1, 'c2':c2, 'K2':K2, 'cK2':cK2 })
487 elif account_type == '2of3':
488 master_c3, master_K3, _ = self.master_public_keys["m/3'/"]
489 c3, K3, cK3 = bip32_public_derivation(master_c3.decode('hex'), master_K3.decode('hex'), "m/3'/", "m/3'/%d"%i)
490 master_c4, master_K4, _ = self.master_public_keys["m/4'/"]
491 c4, K4, cK4 = bip32_public_derivation(master_c4.decode('hex'), master_K4.decode('hex'), "m/4'/", "m/4'/%d"%i)
492 master_c5, master_K5, _ = self.master_public_keys["m/5'/"]
493 c5, K5, cK5 = bip32_public_derivation(master_c5.decode('hex'), master_K5.decode('hex'), "m/5'/", "m/5'/%d"%i)
494 account = BIP32_Account_2of3({ 'c':c3, 'K':K3, 'cK':cK3, 'c2':c4, 'K2':K4, 'cK2':cK4, 'c3':c5, 'K3':K5, 'cK3':cK5 })
496 return account_id, account
499 def set_label(self, name, text = None):
501 old_text = self.labels.get(name)
504 self.labels[name] = text
508 self.labels.pop(name)
512 self.storage.put('labels', self.labels, True)
514 run_hook('set_label', name, text, changed)
519 def create_account(self, account_type = '1', name = None):
520 k, account = self.next_account(account_type)
521 if k in self.pending_accounts:
522 self.pending_accounts.pop(k)
523 self.storage.put('pending_accounts', self.pending_accounts)
525 self.accounts[k] = account
528 self.set_label(k, name)
531 def create_old_account(self, mpk):
532 self.storage.put('master_public_key', mpk, True)
533 self.accounts[0] = OldAccount({'mpk':mpk, 0:[], 1:[]})
537 def save_accounts(self):
539 for k, v in self.accounts.items():
541 self.storage.put('accounts', d, True)
545 def load_accounts(self):
546 d = self.storage.get('accounts', {})
548 for k, v in d.items():
550 v['mpk'] = self.storage.get('master_public_key')
551 self.accounts[k] = OldAccount(v)
553 self.accounts[k] = BIP32_Account_2of2(v)
555 self.accounts[k] = BIP32_Account(v)
557 self.pending_accounts = self.storage.get('pending_accounts',{})
560 def delete_pending_account(self, k):
561 self.pending_accounts.pop(k)
562 self.storage.put('pending_accounts', self.pending_accounts)
564 def account_is_pending(self, k):
565 return k in self.pending_accounts
567 def create_pending_account(self, acct_type, name):
568 k, addr = self.new_account_address(acct_type)
569 self.set_label(k, name)
570 self.pending_accounts[k] = addr
571 self.storage.put('pending_accounts', self.pending_accounts)
573 def get_pending_accounts(self):
574 return self.pending_accounts.items()
577 def addresses(self, include_change = True, _next=True):
578 o = self.get_account_addresses(-1, include_change)
579 for a in self.accounts.keys():
580 o += self.get_account_addresses(a, include_change)
583 for addr in self.next_addresses.values():
589 def is_mine(self, address):
590 return address in self.addresses(True)
593 def is_change(self, address):
594 if not self.is_mine(address): return False
595 if address in self.imported_keys.keys(): return False
596 acct, s = self.get_address_index(address)
597 if s is None: return False
600 def get_master_public_key(self):
601 if self.seed_version == 4:
602 return self.storage.get("master_public_key")
604 c, K, cK = self.storage.get("master_public_keys")["m/0'/"]
607 def get_master_private_key(self, account, password):
608 master_k = pw_decode( self.master_private_keys[account], password)
609 master_c, master_K, master_Kc = self.master_public_keys[account]
611 K, Kc = get_pubkeys_from_secret(master_k.decode('hex'))
612 assert K.encode('hex') == master_K
614 raise Exception("Invalid password")
618 def get_address_index(self, address):
619 if address in self.imported_keys.keys():
622 for account in self.accounts.keys():
623 for for_change in [0,1]:
624 addresses = self.accounts[account].get_addresses(for_change)
625 for addr in addresses:
627 return account, (for_change, addresses.index(addr))
629 for k,v in self.next_addresses.items():
633 raise Exception("Address not found", address)
636 def get_roots(self, account):
638 for a in account.split('&'):
640 m = re.match("(m/\d+'/)(\d+)", s)
641 roots.append( m.group(1) )
644 def is_seeded(self, account):
645 if type(account) is int:
646 return self.seed is not None
648 for root in self.get_roots(account):
649 if root not in self.master_private_keys.keys():
653 def rebase_sequence(self, account, sequence):
656 for a in account.split('&'):
658 m = re.match("(m/\d+'/)(\d+)", s)
660 num = int(m.group(2))
661 dd.append( (root, [num,c,i] ) )
665 def get_keyID(self, account, sequence):
668 mpk = self.storage.get('master_public_key')
669 return 'old(%s,%d,%d)'%(mpk,a,b)
671 rs = self.rebase_sequence(account, sequence)
673 for root, public_sequence in rs:
674 c, K, _ = self.master_public_keys[root]
675 s = '/' + '/'.join( map(lambda x:str(x), public_sequence) )
676 dd.append( 'bip32(%s,%s,%s)'%(c,K, s) )
681 def get_seed(self, password):
682 s = pw_decode(self.seed, password)
683 if self.seed_version == 4:
685 self.accounts[0].check_seed(seed)
687 seed = mnemonic_hash(s)
691 def get_mnemonic(self, password):
693 s = pw_decode(self.seed, password)
694 if self.seed_version == 4:
695 return ' '.join(mnemonic.mn_encode(s))
701 def get_private_key(self, address, password):
702 # first check the provided password
703 seed = self.get_seed(password)
706 if address in self.imported_keys.keys():
707 out.append( pw_decode( self.imported_keys[address], password ) )
709 account, sequence = self.get_address_index(address)
711 pk = self.accounts[account].get_private_key(seed, sequence)
715 # assert address == self.accounts[account].get_address(*sequence)
716 rs = self.rebase_sequence( account, sequence)
717 for root, public_sequence in rs:
719 if root not in self.master_private_keys.keys(): continue
720 master_k = self.get_master_private_key(root, password)
721 master_c, _, _ = self.master_public_keys[root]
722 pk = bip32_private_key( public_sequence, master_k.decode('hex'), master_c.decode('hex'))
728 def add_keypairs_from_wallet(self, tx, keypairs, password):
729 for txin in tx.inputs:
730 address = txin['address']
731 private_keys = self.get_private_key(address, password)
732 for sec in private_keys:
733 pubkey = public_key_from_private_key(sec)
734 keypairs[ pubkey ] = sec
735 if address in self.imported_keys.keys():
736 txin['redeemPubkey'] = pubkey
739 def add_keypairs_from_KeyID(self, tx, keypairs, password):
740 for txin in tx.inputs:
741 keyid = txin.get('KeyID')
744 if self.seed_version==4:
745 m = re.match("old\(([0-9a-f]+),(\d+),(\d+)", keyid)
748 if mpk != self.storage.get('master_public_key'): continue
749 index = int(m.group(2))
750 num = int(m.group(3))
751 account = self.accounts[0]
752 addr = account.get_address(index, num)
753 txin['address'] = addr # fixme: side effect
754 pk = self.get_private_key(addr, password)
756 pubkey = public_key_from_private_key(sec)
757 keypairs[pubkey] = sec
762 for s in keyid.split('&'):
763 m = re.match("bip32\(([0-9a-f]+),([0-9a-f]+),(/\d+/\d+/\d+)", s)
767 sequence = m.group(3)
768 root = self.find_root_by_master_key(c,K)
769 if not root: continue
770 sequence = map(lambda x:int(x), sequence.strip('/').split('/'))
771 root = root + '%d'%sequence[0]
772 sequence = sequence[1:]
773 roots.append((root,sequence))
775 account_id = " & ".join( map(lambda x:x[0], roots) )
776 account = self.accounts.get(account_id)
777 if not account: continue
778 addr = account.get_address(*sequence)
779 txin['address'] = addr # fixme: side effect
780 pk = self.get_private_key(addr, password)
782 pubkey = public_key_from_private_key(sec)
783 keypairs[pubkey] = sec
787 def signrawtransaction(self, tx, input_info, private_keys, password):
789 # check that the password is correct
790 seed = self.get_seed(password)
793 tx.add_input_info(input_info)
795 # add redeem script for coins that are in the wallet
796 # FIXME: add redeemPubkey too!
797 unspent_coins = self.get_unspent_coins()
798 for txin in tx.inputs:
799 for item in unspent_coins:
800 if txin['prevout_hash'] == item['prevout_hash'] and txin['prevout_n'] == item['prevout_n']:
801 print_error( "tx input is in unspent coins" )
802 txin['scriptPubKey'] = item['scriptPubKey']
803 account, sequence = self.get_address_index(item['address'])
805 txin['redeemScript'] = self.accounts[account].redeem_script(sequence)
806 print_error("added redeemScript", txin['redeemScript'])
810 # build a list of public/private keys
813 # add private keys from parameter
814 for sec in private_keys:
815 pubkey = public_key_from_private_key(sec)
816 keypairs[ pubkey ] = sec
818 # add private_keys from KeyID
819 self.add_keypairs_from_KeyID(tx, keypairs, password)
821 # add private keys from wallet
822 self.add_keypairs_from_wallet(tx, keypairs, password)
823 self.sign_transaction(tx, keypairs)
826 def sign_message(self, address, message, password):
827 keys = self.get_private_key(address, password)
828 assert len(keys) == 1
830 key = regenerate_key(sec)
831 compressed = is_compressed(sec)
832 return key.sign_message(message, compressed, address)
835 def change_gap_limit(self, value):
836 if value >= self.gap_limit:
837 self.gap_limit = value
838 self.storage.put('gap_limit', self.gap_limit, True)
839 #self.interface.poke('synchronizer')
842 elif value >= self.min_acceptable_gap():
843 for key, account in self.accounts.items():
844 addresses = account[0]
845 k = self.num_unused_trailing_addresses(addresses)
846 n = len(addresses) - k + value
847 addresses = addresses[0:n]
848 self.accounts[key][0] = addresses
850 self.gap_limit = value
851 self.storage.put('gap_limit', self.gap_limit, True)
857 def num_unused_trailing_addresses(self, addresses):
859 for a in addresses[::-1]:
860 if self.history.get(a):break
864 def min_acceptable_gap(self):
865 # fixme: this assumes wallet is synchronized
869 for account in self.accounts.values():
870 addresses = account.get_addresses(0)
871 k = self.num_unused_trailing_addresses(addresses)
872 for a in addresses[0:-k]:
873 if self.history.get(a):
877 if n > nmax: nmax = n
881 def address_is_old(self, address):
883 h = self.history.get(address, [])
886 for tx_hash, tx_height in h:
890 tx_age = self.verifier.blockchain.height() - tx_height + 1
896 def synchronize_sequence(self, account, for_change):
897 limit = self.gap_limit_for_change if for_change else self.gap_limit
900 addresses = account.get_addresses(for_change)
901 if len(addresses) < limit:
902 address = account.create_new_address(for_change)
903 self.history[address] = []
904 new_addresses.append( address )
907 if map( lambda a: self.address_is_old(a), addresses[-limit:] ) == limit*[False]:
910 address = account.create_new_address(for_change)
911 self.history[address] = []
912 new_addresses.append( address )
918 def create_pending_accounts(self):
919 for account_type in ['1','2of2','2of3']:
920 if not self.has_master_public_keys(account_type):
922 k, a = self.new_account_address(account_type)
923 if self.address_is_old(a):
924 print_error( "creating account", a )
925 self.create_account(account_type)
926 self.next_addresses.pop(k)
929 def synchronize_account(self, account):
931 new += self.synchronize_sequence(account, 0)
932 new += self.synchronize_sequence(account, 1)
936 def synchronize(self):
937 if self.master_public_keys:
938 self.create_pending_accounts()
940 for account in self.accounts.values():
941 new += self.synchronize_account(account)
944 self.storage.put('addr_history', self.history, True)
949 return self.history.values() != [[]] * len(self.history)
952 def add_contact(self, address, label=None):
953 self.addressbook.append(address)
954 self.storage.put('contacts', self.addressbook, True)
956 self.set_label(address, label)
959 def delete_contact(self, addr):
960 if addr in self.addressbook:
961 self.addressbook.remove(addr)
962 self.storage.put('addressbook', self.addressbook, True)
965 def fill_addressbook(self):
966 for tx_hash, tx in self.transactions.items():
967 is_relevant, is_send, _, _ = self.get_tx_value(tx)
969 for addr, v in tx.outputs:
970 if not self.is_mine(addr) and addr not in self.addressbook:
971 self.addressbook.append(addr)
973 # self.update_tx_labels()
975 def get_num_tx(self, address):
977 for tx in self.transactions.values():
978 if address in map(lambda x:x[0], tx.outputs): n += 1
982 def get_address_flags(self, addr):
983 flags = "C" if self.is_change(addr) else "I" if addr in self.imported_keys.keys() else "-"
984 flags += "F" if addr in self.frozen_addresses else "P" if addr in self.prioritized_addresses else "-"
988 def get_tx_value(self, tx, account=None):
989 domain = self.get_account_addresses(account)
990 return tx.get_value(domain, self.prevout_values)
993 def update_tx_outputs(self, tx_hash):
994 tx = self.transactions.get(tx_hash)
996 for i, (addr, value) in enumerate(tx.outputs):
997 key = tx_hash+ ':%d'%i
998 self.prevout_values[key] = value
1000 for item in tx.inputs:
1001 if self.is_mine(item.get('address')):
1002 key = item['prevout_hash'] + ':%d'%item['prevout_n']
1003 self.spent_outputs.append(key)
1006 def get_addr_balance(self, address):
1007 assert self.is_mine(address)
1008 h = self.history.get(address,[])
1009 if h == ['*']: return 0,0
1011 received_coins = [] # list of coins received at address
1013 for tx_hash, tx_height in h:
1014 tx = self.transactions.get(tx_hash)
1017 for i, (addr, value) in enumerate(tx.outputs):
1019 key = tx_hash + ':%d'%i
1020 received_coins.append(key)
1022 for tx_hash, tx_height in h:
1023 tx = self.transactions.get(tx_hash)
1027 for item in tx.inputs:
1028 addr = item.get('address')
1030 key = item['prevout_hash'] + ':%d'%item['prevout_n']
1031 value = self.prevout_values.get( key )
1032 if key in received_coins:
1035 for i, (addr, value) in enumerate(tx.outputs):
1036 key = tx_hash + ':%d'%i
1047 def get_account_name(self, k):
1049 if self.seed_version == 4:
1050 name = 'Main account'
1052 name = 'Old account'
1054 default = "Unnamed account"
1055 m = re.match("m/0'/(\d+)", k)
1059 default = "Main account"
1061 default = "Account %s"%num
1063 m = re.match("m/1'/(\d+) & m/2'/(\d+)", k)
1066 default = "2of2 account %s"%num
1067 name = self.labels.get(k, default)
1071 def get_account_names(self):
1073 for k, account in self.accounts.items():
1074 accounts[k] = self.get_account_name(k)
1075 if self.imported_keys:
1076 accounts[-1] = 'Imported keys'
1079 def get_account_addresses(self, a, include_change=True):
1081 o = self.addresses(True)
1083 o = self.imported_keys.keys()
1085 ac = self.accounts[a]
1086 o = ac.get_addresses(0)
1087 if include_change: o += ac.get_addresses(1)
1090 def get_imported_balance(self):
1091 return self.get_balance(self.imported_keys.keys())
1093 def get_account_balance(self, account):
1094 return self.get_balance(self.get_account_addresses(account))
1096 def get_frozen_balance(self):
1097 return self.get_balance(self.frozen_addresses)
1099 def get_balance(self, domain=None):
1100 if domain is None: domain = self.addresses(True)
1103 c, u = self.get_addr_balance(addr)
1109 def get_unspent_coins(self, domain=None):
1111 if domain is None: domain = self.addresses(True)
1113 h = self.history.get(addr, [])
1114 if h == ['*']: continue
1115 for tx_hash, tx_height in h:
1116 tx = self.transactions.get(tx_hash)
1117 if tx is None: raise Exception("Wallet not synchronized")
1118 is_coinbase = tx.inputs[0].get('prevout_hash') == '0'*64
1119 for o in tx.d.get('outputs'):
1121 if output.get('address') != addr: continue
1122 key = tx_hash + ":%d" % output.get('prevout_n')
1123 if key in self.spent_outputs: continue
1124 output['prevout_hash'] = tx_hash
1125 output['height'] = tx_height
1126 output['coinbase'] = is_coinbase
1127 coins.append((tx_height, output))
1131 coins = sorted(coins)
1132 if coins[-1][0] != 0:
1133 while coins[0][0] == 0:
1134 coins = coins[1:] + [ coins[0] ]
1135 return [x[1] for x in coins]
1139 def choose_tx_inputs_from_account( self, amount, fixed_fee, account ):
1140 domain = self.get_account_addresses(account) if account else None
1141 return self.choose_tx_inputs( amount, fixed_fee, domain )
1144 def choose_tx_inputs( self, amount, fixed_fee, domain = None ):
1145 """ todo: minimize tx size """
1147 fee = self.fee if fixed_fee is None else fixed_fee
1149 domain = self.addresses(True)
1151 for i in self.frozen_addresses:
1152 if i in domain: domain.remove(i)
1155 for i in self.prioritized_addresses:
1158 prioritized.append(i)
1160 coins = self.get_unspent_coins(domain)
1161 prioritized_coins = self.get_unspent_coins(prioritized)
1164 coins = prioritized_coins + coins
1167 if item.get('coinbase') and item.get('height') + COINBASE_MATURITY > self.network.blockchain.height:
1169 addr = item.get('address')
1170 v = item.get('value')
1173 fee = self.estimated_fee(inputs) if fixed_fee is None else fixed_fee
1174 if total >= amount + fee: break
1178 return inputs, total, fee
1181 def set_fee(self, fee):
1184 self.storage.put('fee_per_kb', self.fee, True)
1186 def estimated_fee(self, inputs):
1187 estimated_size = len(inputs) * 180 + 80 # this assumes non-compressed keys
1188 fee = self.fee * int(round(estimated_size/1024.))
1189 if fee == 0: fee = self.fee
1193 def add_tx_change( self, inputs, outputs, amount, fee, total, change_addr=None):
1194 "add change to a transaction"
1195 change_amount = total - ( amount + fee )
1196 if change_amount > DUST_THRESHOLD:
1199 # send change to one of the accounts involved in the tx
1200 address = inputs[0].get('address')
1201 account, _ = self.get_address_index(address)
1203 if not self.use_change or account == -1:
1204 change_addr = inputs[-1]['address']
1206 change_addr = self.accounts[account].get_addresses(1)[-self.gap_limit_for_change]
1208 # Insert the change output at a random position in the outputs
1209 posn = random.randint(0, len(outputs))
1210 outputs[posn:posn] = [( change_addr, change_amount)]
1214 def get_history(self, address):
1216 return self.history.get(address)
1219 def get_status(self, h):
1220 if not h: return None
1221 if h == ['*']: return '*'
1223 for tx_hash, height in h:
1224 status += tx_hash + ':%d:' % height
1225 return hashlib.sha256( status ).digest().encode('hex')
1228 def receive_tx_callback(self, tx_hash, tx, tx_height):
1230 with self.transaction_lock:
1231 self.add_extra_addresses(tx)
1232 if not self.check_new_tx(tx_hash, tx):
1233 # may happen due to pruning
1234 print_error("received transaction that is no longer referenced in history", tx_hash)
1236 self.transactions[tx_hash] = tx
1237 self.network.interface.pending_transactions_for_notifications.append(tx)
1238 self.save_transactions()
1239 if self.verifier and tx_height>0:
1240 self.verifier.add(tx_hash, tx_height)
1241 self.update_tx_outputs(tx_hash)
1244 def save_transactions(self):
1246 for k,v in self.transactions.items():
1248 self.storage.put('transactions', tx, True)
1250 def receive_history_callback(self, addr, hist):
1252 if not self.check_new_history(addr, hist):
1253 raise Exception("error: received history for %s is not consistent with known transactions"%addr)
1256 self.history[addr] = hist
1257 self.storage.put('addr_history', self.history, True)
1260 for tx_hash, tx_height in hist:
1262 # add it in case it was previously unconfirmed
1263 if self.verifier: self.verifier.add(tx_hash, tx_height)
1266 def get_tx_history(self, account=None):
1267 if not self.verifier:
1270 with self.transaction_lock:
1271 history = self.transactions.items()
1272 history.sort(key = lambda x: self.verifier.get_txpos(x[0]))
1276 for tx_hash, tx in history:
1277 is_relevant, is_mine, v, fee = self.get_tx_value(tx, account)
1278 if v is not None: balance += v
1280 c, u = self.get_account_balance(account)
1283 result.append( ('', 1000, 0, c+u-balance, None, c+u-balance, None ) )
1285 balance = c + u - balance
1286 for tx_hash, tx in history:
1287 is_relevant, is_mine, value, fee = self.get_tx_value(tx, account)
1290 if value is not None:
1293 conf, timestamp = self.verifier.get_confirmations(tx_hash) if self.verifier else (None, None)
1294 result.append( (tx_hash, conf, is_mine, value, fee, balance, timestamp) )
1299 def get_label(self, tx_hash):
1300 label = self.labels.get(tx_hash)
1301 is_default = (label == '') or (label is None)
1302 if is_default: label = self.get_default_label(tx_hash)
1303 return label, is_default
1306 def get_default_label(self, tx_hash):
1307 tx = self.transactions.get(tx_hash)
1310 is_relevant, is_mine, _, _ = self.get_tx_value(tx)
1312 for o in tx.outputs:
1314 if not self.is_mine(o_addr):
1316 default_label = self.labels[o_addr]
1318 default_label = o_addr
1321 default_label = '(internal)'
1323 for o in tx.outputs:
1325 if self.is_mine(o_addr) and not self.is_change(o_addr):
1328 for o in tx.outputs:
1330 if self.is_mine(o_addr):
1336 dest_label = self.labels.get(o_addr)
1338 default_label = self.labels[o_addr]
1340 default_label = o_addr
1342 return default_label
1345 def make_unsigned_transaction(self, outputs, fee=None, change_addr=None, domain=None ):
1346 for address, x in outputs:
1347 assert is_valid(address)
1348 amount = sum( map(lambda x:x[1], outputs) )
1349 inputs, total, fee = self.choose_tx_inputs( amount, fee, domain )
1351 raise ValueError("Not enough funds")
1352 self.add_input_info(inputs)
1353 outputs = self.add_tx_change(inputs, outputs, amount, fee, total, change_addr)
1354 return Transaction.from_io(inputs, outputs)
1357 def mktx_from_account(self, outputs, password, fee=None, account=None):
1358 domain = self.get_account_addresses(account) if account else None
1359 return self.mktx(outputs, password, fee, change_addr=None, domain=domain)
1362 def mktx(self, outputs, password, fee=None, change_addr=None, domain= None ):
1363 tx = self.make_unsigned_transaction(outputs, fee, change_addr, domain)
1365 self.add_keypairs_from_wallet(tx, keypairs, password)
1367 self.sign_transaction(tx, keypairs)
1371 def add_input_info(self, inputs):
1373 address = txin['address']
1374 if address in self.imported_keys.keys():
1376 account, sequence = self.get_address_index(address)
1377 txin['KeyID'] = self.get_keyID(account, sequence)
1378 redeemScript = self.accounts[account].redeem_script(sequence)
1380 txin['redeemScript'] = redeemScript
1382 txin['redeemPubkey'] = self.accounts[account].get_pubkey(*sequence)
1385 def sign_transaction(self, tx, keypairs):
1387 run_hook('sign_transaction', tx)
1390 def sendtx(self, tx):
1392 h = self.send_tx(tx)
1393 self.tx_event.wait()
1394 return self.receive_tx(h)
1396 def send_tx(self, tx):
1398 self.tx_event.clear()
1399 self.network.interface.send([('blockchain.transaction.broadcast', [str(tx)])], self.on_broadcast)
1402 def on_broadcast(self, i, r):
1403 self.tx_result = r.get('result')
1406 def receive_tx(self,tx_hash):
1407 out = self.tx_result
1409 return False, "error: " + out
1414 def update_password(self, old_password, new_password):
1415 if new_password == '': new_password = None
1416 decoded = self.get_seed(old_password)
1417 self.seed = pw_encode( decoded, new_password)
1418 self.storage.put('seed', self.seed, True)
1419 self.use_encryption = (new_password != None)
1420 self.storage.put('use_encryption', self.use_encryption,True)
1421 for k in self.imported_keys.keys():
1422 a = self.imported_keys[k]
1423 b = pw_decode(a, old_password)
1424 c = pw_encode(b, new_password)
1425 self.imported_keys[k] = c
1426 self.storage.put('imported_keys', self.imported_keys, True)
1428 for k, v in self.master_private_keys.items():
1429 b = pw_decode(v, old_password)
1430 c = pw_encode(b, new_password)
1431 self.master_private_keys[k] = c
1432 self.storage.put('master_private_keys', self.master_private_keys, True)
1435 def freeze(self,addr):
1436 if self.is_mine(addr) and addr not in self.frozen_addresses:
1437 self.unprioritize(addr)
1438 self.frozen_addresses.append(addr)
1439 self.storage.put('frozen_addresses', self.frozen_addresses, True)
1444 def unfreeze(self,addr):
1445 if self.is_mine(addr) and addr in self.frozen_addresses:
1446 self.frozen_addresses.remove(addr)
1447 self.storage.put('frozen_addresses', self.frozen_addresses, True)
1452 def prioritize(self,addr):
1453 if self.is_mine(addr) and addr not in self.prioritized_addresses:
1455 self.prioritized_addresses.append(addr)
1456 self.storage.put('prioritized_addresses', self.prioritized_addresses, True)
1461 def unprioritize(self,addr):
1462 if self.is_mine(addr) and addr in self.prioritized_addresses:
1463 self.prioritized_addresses.remove(addr)
1464 self.storage.put('prioritized_addresses', self.prioritized_addresses, True)
1470 def set_verifier(self, verifier):
1471 self.verifier = verifier
1473 # review transactions that are in the history
1474 for addr, hist in self.history.items():
1475 if hist == ['*']: continue
1476 for tx_hash, tx_height in hist:
1478 # add it in case it was previously unconfirmed
1479 self.verifier.add(tx_hash, tx_height)
1482 # if we are on a pruning server, remove unverified transactions
1483 vr = self.verifier.transactions.keys() + self.verifier.verified_tx.keys()
1484 for tx_hash in self.transactions.keys():
1485 if tx_hash not in vr:
1486 self.transactions.pop(tx_hash)
1490 def check_new_history(self, addr, hist):
1492 # check that all tx in hist are relevant
1494 for tx_hash, height in hist:
1495 tx = self.transactions.get(tx_hash)
1497 if not tx.has_address(addr):
1500 # check that we are not "orphaning" a transaction
1501 old_hist = self.history.get(addr,[])
1502 if old_hist == ['*']: return True
1504 for tx_hash, height in old_hist:
1505 if tx_hash in map(lambda x:x[0], hist): continue
1507 for _addr, _hist in self.history.items():
1508 if _addr == addr: continue
1509 if _hist == ['*']: continue
1510 _tx_hist = map(lambda x:x[0], _hist)
1511 if tx_hash in _tx_hist:
1516 tx = self.transactions.get(tx_hash)
1517 # tx might not be there
1521 if self.verifier.get_height(tx_hash):
1524 print_error("new history is orphaning transaction:", tx_hash)
1525 # check that all outputs are not mine, request histories
1527 for _addr, _v in tx.outputs:
1528 # assert not self.is_mine(_addr)
1529 ext_requests.append( ('blockchain.address.get_history', [_addr]) )
1531 ext_h = self.network.synchronous_get(ext_requests)
1532 print_error("sync:", ext_requests, ext_h)
1535 if h == ['*']: continue
1537 if item.get('tx_hash') == tx_hash:
1538 height = item.get('height')
1540 print_error("found height for", tx_hash, height)
1541 self.verifier.add(tx_hash, height)
1543 print_error("removing orphaned tx from history", tx_hash)
1544 self.transactions.pop(tx_hash)
1550 def check_new_tx(self, tx_hash, tx):
1551 # 1 check that tx is referenced in addr_history.
1553 for addr, hist in self.history.items():
1554 if hist == ['*']:continue
1555 for txh, height in hist:
1557 addresses.append(addr)
1562 # 2 check that referencing addresses are in the tx
1563 for addr in addresses:
1564 if not tx.has_address(addr):
1570 def start_threads(self, network):
1571 from verifier import TxVerifier
1572 self.network = network
1574 self.verifier = TxVerifier(self.network, self.storage)
1575 self.verifier.start()
1576 self.set_verifier(self.verifier)
1577 self.synchronizer = WalletSynchronizer(self, network)
1578 self.synchronizer.start()
1580 self.verifier = None
1581 self.synchronizer =None
1583 def stop_threads(self):
1585 self.verifier.stop()
1586 self.synchronizer.stop()
1589 def restore(self, callback):
1591 def wait_for_wallet():
1592 self.set_up_to_date(False)
1593 while not self.is_up_to_date():
1594 msg = "%s\n%s %d\n%s %.1f"%(
1595 _("Please wait..."),
1596 _("Addresses generated:"),
1597 len(self.addresses(True)),
1598 _("Kilobytes received:"),
1599 self.network.interface.bytes_received/1024.)
1601 apply(callback, (msg,))
1604 def wait_for_network():
1605 while not self.network.is_connected():
1606 msg = "%s \n" % (_("Connecting..."))
1607 apply(callback, (msg,))
1610 # wait until we are connected, because the user might have selected another server
1617 self.fill_addressbook()
1622 class WalletSynchronizer(threading.Thread):
1625 def __init__(self, wallet, network):
1626 threading.Thread.__init__(self)
1628 self.wallet = wallet
1629 self.network = network
1630 self.was_updated = True
1631 self.running = False
1632 self.lock = threading.Lock()
1633 self.queue = Queue.Queue()
1636 with self.lock: self.running = False
1638 def is_running(self):
1639 with self.lock: return self.running
1642 def subscribe_to_addresses(self, addresses):
1644 for addr in addresses:
1645 messages.append(('blockchain.address.subscribe', [addr]))
1646 self.network.subscribe( messages, lambda i,r: self.queue.put(r))
1653 while self.is_running():
1655 if not self.network.is_connected():
1656 print_error("synchronizer: waiting for interface")
1657 self.network.wait_until_connected()
1659 self.run_interface(self.network.interface)
1662 def run_interface(self, interface):
1664 print_error("synchronizer: connected to", interface.server)
1668 requested_histories = {}
1670 # request any missing transactions
1671 for history in self.wallet.history.values():
1672 if history == ['*']: continue
1673 for tx_hash, tx_height in history:
1674 if self.wallet.transactions.get(tx_hash) is None and (tx_hash, tx_height) not in missing_tx:
1675 missing_tx.append( (tx_hash, tx_height) )
1678 print_error("missing tx", missing_tx)
1681 self.subscribe_to_addresses(self.wallet.addresses(True))
1683 while self.is_running():
1684 # 1. create new addresses
1685 new_addresses = self.wallet.synchronize()
1687 # request missing addresses
1689 self.subscribe_to_addresses(new_addresses)
1691 # request missing transactions
1692 for tx_hash, tx_height in missing_tx:
1693 if (tx_hash, tx_height) not in requested_tx:
1694 interface.send([ ('blockchain.transaction.get',[tx_hash, tx_height]) ], lambda i,r: self.queue.put(r))
1695 requested_tx.append( (tx_hash, tx_height) )
1698 # detect if situation has changed
1699 if interface.is_up_to_date() and self.queue.empty():
1700 if not self.wallet.is_up_to_date():
1701 self.wallet.set_up_to_date(True)
1702 self.was_updated = True
1704 if self.wallet.is_up_to_date():
1705 self.wallet.set_up_to_date(False)
1706 self.was_updated = True
1708 if self.was_updated:
1709 self.wallet.network.trigger_callback('updated')
1710 self.was_updated = False
1714 r = self.queue.get(block=True, timeout=1)
1718 if interface != self.network.interface:
1724 # 3. handle response
1725 method = r['method']
1726 params = r['params']
1727 result = r.get('result')
1728 error = r.get('error')
1733 if method == 'blockchain.address.subscribe':
1735 if self.wallet.get_status(self.wallet.get_history(addr)) != result:
1736 if requested_histories.get(addr) is None:
1737 interface.send([('blockchain.address.get_history', [addr])], lambda i,r:self.queue.put(r))
1738 requested_histories[addr] = result
1740 elif method == 'blockchain.address.get_history':
1742 print_error("receiving history", addr, result)
1744 assert requested_histories.pop(addr) == '*'
1745 self.wallet.receive_history_callback(addr, result)
1748 # check that txids are unique
1751 tx_hash = item['tx_hash']
1752 if tx_hash not in txids:
1753 txids.append(tx_hash)
1754 hist.append( (tx_hash, item['height']) )
1756 if len(hist) != len(result):
1757 raise Exception("error: server sent history with non-unique txid", result)
1759 # check that the status corresponds to what was announced
1760 rs = requested_histories.pop(addr)
1761 if self.wallet.get_status(hist) != rs:
1762 raise Exception("error: status mismatch: %s"%addr)
1764 # store received history
1765 self.wallet.receive_history_callback(addr, hist)
1767 # request transactions that we don't have
1768 for tx_hash, tx_height in hist:
1769 if self.wallet.transactions.get(tx_hash) is None:
1770 if (tx_hash, tx_height) not in requested_tx and (tx_hash, tx_height) not in missing_tx:
1771 missing_tx.append( (tx_hash, tx_height) )
1773 elif method == 'blockchain.transaction.get':
1775 tx_height = params[1]
1776 assert tx_hash == hash_encode(Hash(result.decode('hex')))
1777 tx = Transaction(result)
1778 self.wallet.receive_tx_callback(tx_hash, tx, tx_height)
1779 self.was_updated = True
1780 requested_tx.remove( (tx_hash, tx_height) )
1781 print_error("received tx:", tx_hash, len(tx.raw))
1784 print_error("Error: Unknown message:" + method + ", " + repr(params) + ", " + repr(result) )
1786 if self.was_updated and not requested_tx:
1787 self.wallet.network.trigger_callback('updated')
1788 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
1790 self.was_updated = False