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
42 EncodeAES = lambda secret, s: base64.b64encode(aes.encryptData(secret,s))
43 DecodeAES = lambda secret, e: aes.decryptData(secret, base64.b64decode(e))
45 def pw_encode(s, password):
47 secret = Hash(password)
48 return EncodeAES(secret, s)
52 def pw_decode(s, password):
53 if password is not None:
54 secret = Hash(password)
56 d = DecodeAES(secret, s)
58 raise BaseException('Invalid password')
72 def __init__(self, config):
73 self.lock = threading.Lock()
75 self.file_exists = False
76 self.path = self.init_path(config)
77 print_error( "wallet path", self.path )
82 def init_path(self, config):
83 """Set the path of the wallet."""
85 # command line -w option
86 path = config.get('wallet_path')
91 path = config.get('default_wallet_path')
96 dirpath = os.path.join(config.path, "wallets")
97 if not os.path.exists(dirpath):
100 new_path = os.path.join(config.path, "wallets", "default_wallet")
102 # default path in pre 1.9 versions
103 old_path = os.path.join(config.path, "electrum.dat")
104 if os.path.exists(old_path) and not os.path.exists(new_path):
105 os.rename(old_path, new_path)
110 def read(self, path):
111 """Read the contents of the wallet file."""
113 with open(self.path, "r") as f:
118 d = ast.literal_eval( data ) #parse raw data from reading wallet file
120 raise IOError("Cannot read wallet file.")
123 self.file_exists = True
126 def get(self, key, default=None):
127 return self.data.get(key, default)
129 def put(self, key, value, save = True):
132 if value is not None:
133 self.data[key] = value
141 f = open(self.path,"w")
144 if 'ANDROID_DATA' not in os.environ:
146 os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)
151 def __init__(self, storage):
153 self.storage = storage
154 self.electrum_version = ELECTRUM_VERSION
155 self.gap_limit_for_change = 3 # constant
158 self.seed_version = storage.get('seed_version', SEED_VERSION)
160 self.gap_limit = storage.get('gap_limit', 5)
161 self.use_change = storage.get('use_change',True)
162 self.use_encryption = storage.get('use_encryption', False)
163 self.seed = storage.get('seed', '') # encrypted
164 self.labels = storage.get('labels', {})
165 self.frozen_addresses = storage.get('frozen_addresses',[])
166 self.prioritized_addresses = storage.get('prioritized_addresses',[])
167 self.addressbook = storage.get('contacts', [])
169 self.imported_keys = storage.get('imported_keys',{})
170 self.history = storage.get('addr_history',{}) # address -> list(txid, height)
172 self.fee = int(storage.get('fee_per_kb',20000))
174 self.master_public_keys = storage.get('master_public_keys',{})
175 self.master_private_keys = storage.get('master_private_keys', {})
177 self.next_addresses = storage.get('next_addresses',{})
179 if self.seed_version not in [4, 6]:
180 msg = "This wallet seed is not supported."
181 if self.seed_version in [5]: msg += "\nTo open this wallet, try 'git checkout seed_v%d'"%self.seed_version
182 raise ValueError(msg)
186 self.transactions = {}
187 tx_list = self.storage.get('transactions',{})
188 for k,v in tx_list.items():
192 print_msg("Warning: Cannot deserialize transactions. skipping")
195 self.add_extra_addresses(tx)
196 self.transactions[k] = tx
198 for h,tx in self.transactions.items():
199 if not self.check_new_tx(h, tx):
200 print_error("removing unreferenced tx", h)
201 self.transactions.pop(h)
205 self.prevout_values = {} # my own transaction outputs
206 self.spent_outputs = []
211 # there is a difference between wallet.up_to_date and interface.is_up_to_date()
212 # interface.is_up_to_date() returns true when all requests have been answered and processed
213 # wallet.up_to_date is true when the wallet is synchronized (stronger requirement)
215 self.up_to_date = False
216 self.lock = threading.Lock()
217 self.transaction_lock = threading.Lock()
218 self.tx_event = threading.Event()
220 for tx_hash, tx in self.transactions.items():
221 self.update_tx_outputs(tx_hash)
224 def add_extra_addresses(self, tx):
226 # find the address corresponding to pay-to-pubkey inputs
227 tx.add_extra_addresses(self.transactions)
228 for o in tx.d.get('outputs'):
229 if o.get('is_pubkey'):
230 for tx2 in self.transactions.values():
231 tx2.add_extra_addresses({h:tx})
236 def set_up_to_date(self,b):
237 with self.lock: self.up_to_date = b
240 def is_up_to_date(self):
241 with self.lock: return self.up_to_date
245 self.up_to_date = False
246 while not self.is_up_to_date():
250 def import_key(self, sec, password):
252 seed = self.get_seed(password)
254 address = address_from_private_key(sec)
256 raise BaseException('Invalid private key')
258 if self.is_mine(address):
259 raise BaseException('Address already in wallet')
261 # store the originally requested keypair into the imported keys table
262 self.imported_keys[address] = pw_encode(sec, password )
263 self.storage.put('imported_keys', self.imported_keys, True)
264 if self.synchronizer:
265 self.synchronizer.subscribe_to_addresses([address])
268 def delete_imported_key(self, addr):
269 if addr in self.imported_keys:
270 self.imported_keys.pop(addr)
271 self.storage.put('imported_keys', self.imported_keys, True)
275 import mnemonic, ecdsa
276 entropy = ecdsa.util.randrange( pow(2,160) )
279 ss = "%040x"%(entropy+nonce)
280 s = hashlib.sha256(ss.decode('hex')).digest().encode('hex')
281 # we keep only 13 words, that's approximately 139 bits of entropy
282 words = mnemonic.mn_encode(s)[0:13]
283 seed = ' '.join(words)
284 if mnemonic_hash(seed)[0:3] == SEED_PREFIX:
285 break # this removes 12 bits of entropy
291 def init_seed(self, seed):
293 raise BaseException("a seed exists")
296 self.seed = self.make_seed()
297 self.seed_version = SEED_VERSION
300 # find out what kind of wallet we are
303 self.seed_version = 4
310 mnemonic.mn_decode(words)
311 uses_electrum_words = True
313 uses_electrum_words = False
315 if uses_electrum_words and len(words) != 13:
316 self.seed_version = 4
317 self.seed = mnemonic.mn_encode(seed)
319 assert mnemonic_hash(seed)[0:3] == SEED_PREFIX
320 self.seed_version = SEED_VERSION
325 self.storage.put('seed', self.seed, True)
326 self.storage.put('seed_version', self.seed_version, True)
328 def create_watching_only_wallet(self, c0, K0):
330 self.master_public_keys = {
331 "m/0'/": (c0, K0, cK0),
333 self.storage.put('master_public_keys', self.master_public_keys, True)
334 self.create_account('1','Main account')
337 def create_accounts(self):
338 # create default account
339 self.create_master_keys('1')
340 self.create_account('1','Main account')
343 def create_master_keys(self, account_type):
344 master_k, master_c, master_K, master_cK = bip32_init(self.get_seed(None))
345 if account_type == '1':
346 k0, c0, K0, cK0 = bip32_private_derivation(master_k, master_c, "m/", "m/0'/")
347 self.master_public_keys["m/0'/"] = (c0, K0, cK0)
348 self.master_private_keys["m/0'/"] = k0
349 elif account_type == '2of2':
350 k1, c1, K1, cK1 = bip32_private_derivation(master_k, master_c, "m/", "m/1'/")
351 k2, c2, K2, cK2 = bip32_private_derivation(master_k, master_c, "m/", "m/2'/")
352 self.master_public_keys["m/1'/"] = (c1, K1, cK1)
353 self.master_public_keys["m/2'/"] = (c2, K2, cK2)
354 self.master_private_keys["m/1'/"] = k1
355 self.master_private_keys["m/2'/"] = k2
356 elif account_type == '2of3':
357 k3, c3, K3, cK3 = bip32_private_derivation(master_k, master_c, "m/", "m/3'/")
358 k4, c4, K4, cK4 = bip32_private_derivation(master_k, master_c, "m/", "m/4'/")
359 k5, c5, K5, cK5 = bip32_private_derivation(master_k, master_c, "m/", "m/5'/")
360 self.master_public_keys["m/3'/"] = (c3, K3, cK3)
361 self.master_public_keys["m/4'/"] = (c4, K4, cK4)
362 self.master_public_keys["m/5'/"] = (c5, K5, cK5)
363 self.master_private_keys["m/3'/"] = k3
364 self.master_private_keys["m/4'/"] = k4
365 self.master_private_keys["m/5'/"] = k5
367 self.storage.put('master_public_keys', self.master_public_keys, True)
368 self.storage.put('master_private_keys', self.master_private_keys, True)
370 def has_master_public_keys(self, account_type):
371 if account_type == '1':
372 return "m/0'/" in self.master_public_keys
373 elif account_type == '2of2':
374 return set(["m/1'/", "m/2'/"]) <= set(self.master_public_keys.keys())
375 elif account_type == '2of3':
376 return set(["m/3'/", "m/4'/", "m/5'/"]) <= set(self.master_public_keys.keys())
378 def find_root_by_master_key(self, c, K):
379 for key, v in self.master_public_keys.items():
380 if key == "m/":continue
382 if (c == cc) and (K == KK):
385 def deseed_root(self, seed, password):
386 # for safety, we ask the user to enter their seed
387 assert seed == self.get_seed(password)
389 self.storage.put('seed', '', True)
392 def deseed_branch(self, k):
393 # check that parent has no seed
394 assert self.seed == ''
395 self.master_private_keys.pop(k)
396 self.storage.put('master_private_keys', self.master_private_keys, True)
398 def is_watching_only(self):
399 return (self.seed == '') and (self.master_private_keys == {})
403 def account_id(self, account_type, i):
404 if account_type == '1':
406 elif account_type == '2of2':
407 return "m/1'/%d & m/2'/%d"%(i,i)
408 elif account_type == '2of3':
409 return "m/3'/%d & m/4'/%d & m/5'/%d"%(i,i,i)
411 raise BaseException('unknown account type')
414 def num_accounts(self, account_type):
415 keys = self.accounts.keys()
418 account_id = self.account_id(account_type, i)
419 if account_id not in keys: break
424 def new_account_address(self, account_type = '1'):
425 i = self.num_accounts(account_type)
426 k = self.account_id(account_type,i)
428 addr = self.next_addresses.get(k)
430 account_id, account = self.next_account(account_type)
431 addr = account.first_address()
432 self.next_addresses[k] = addr
433 self.storage.put('next_addresses',self.next_addresses)
438 def next_account(self, account_type = '1'):
440 i = self.num_accounts(account_type)
441 account_id = self.account_id(account_type,i)
443 if account_type is '1':
444 master_c0, master_K0, _ = self.master_public_keys["m/0'/"]
445 c0, K0, cK0 = bip32_public_derivation(master_c0.decode('hex'), master_K0.decode('hex'), "m/0'/", "m/0'/%d"%i)
446 account = BIP32_Account({ 'c':c0, 'K':K0, 'cK':cK0 })
448 elif account_type == '2of2':
449 master_c1, master_K1, _ = self.master_public_keys["m/1'/"]
450 c1, K1, cK1 = bip32_public_derivation(master_c1.decode('hex'), master_K1.decode('hex'), "m/1'/", "m/1'/%d"%i)
451 master_c2, master_K2, _ = self.master_public_keys["m/2'/"]
452 c2, K2, cK2 = bip32_public_derivation(master_c2.decode('hex'), master_K2.decode('hex'), "m/2'/", "m/2'/%d"%i)
453 account = BIP32_Account_2of2({ 'c':c1, 'K':K1, 'cK':cK1, 'c2':c2, 'K2':K2, 'cK2':cK2 })
455 elif account_type == '2of3':
456 master_c3, master_K3, _ = self.master_public_keys["m/3'/"]
457 c3, K3, cK3 = bip32_public_derivation(master_c3.decode('hex'), master_K3.decode('hex'), "m/3'/", "m/3'/%d"%i)
458 master_c4, master_K4, _ = self.master_public_keys["m/4'/"]
459 c4, K4, cK4 = bip32_public_derivation(master_c4.decode('hex'), master_K4.decode('hex'), "m/4'/", "m/4'/%d"%i)
460 master_c5, master_K5, _ = self.master_public_keys["m/5'/"]
461 c5, K5, cK5 = bip32_public_derivation(master_c5.decode('hex'), master_K5.decode('hex'), "m/5'/", "m/5'/%d"%i)
462 account = BIP32_Account_2of3({ 'c':c3, 'K':K3, 'cK':cK3, 'c2':c4, 'K2':K4, 'cK2':cK4, 'c3':c5, 'K3':K5, 'cK3':cK5 })
464 return account_id, account
467 def set_label(self, name, text = None):
469 old_text = self.labels.get(name)
472 self.labels[name] = text
476 self.labels.pop(name)
480 self.storage.put('labels', self.labels, True)
482 run_hook('set_label', name, text, changed)
487 def create_account(self, account_type = '1', name = None):
488 k, account = self.next_account(account_type)
489 if k in self.pending_accounts:
490 self.pending_accounts.pop(k)
491 self.storage.put('pending_accounts', self.pending_accounts)
493 self.accounts[k] = account
496 self.set_label(k, name)
499 def create_old_account(self):
500 mpk = OldAccount.mpk_from_seed(self.seed)
501 self.storage.put('master_public_key', mpk, True)
502 self.accounts[0] = OldAccount({'mpk':mpk, 0:[], 1:[]})
506 def save_accounts(self):
508 for k, v in self.accounts.items():
510 self.storage.put('accounts', d, True)
514 def load_accounts(self):
515 d = self.storage.get('accounts', {})
517 for k, v in d.items():
519 v['mpk'] = self.storage.get('master_public_key')
520 self.accounts[k] = OldAccount(v)
522 self.accounts[k] = BIP32_Account_2of2(v)
524 self.accounts[k] = BIP32_Account(v)
526 self.pending_accounts = self.storage.get('pending_accounts',{})
529 def delete_pending_account(self, k):
530 self.pending_accounts.pop(k)
531 self.storage.put('pending_accounts', self.pending_accounts)
533 def account_is_pending(self, k):
534 return k in self.pending_accounts
536 def create_pending_account(self, acct_type, name):
537 k, addr = self.new_account_address(acct_type)
538 self.set_label(k, name)
539 self.pending_accounts[k] = addr
540 self.storage.put('pending_accounts', self.pending_accounts)
542 def get_pending_accounts(self):
543 return self.pending_accounts.items()
546 def addresses(self, include_change = True, _next=True):
547 o = self.get_account_addresses(-1, include_change)
548 for a in self.accounts.keys():
549 o += self.get_account_addresses(a, include_change)
552 for addr in self.next_addresses.values():
558 def is_mine(self, address):
559 return address in self.addresses(True)
562 def is_change(self, address):
563 if not self.is_mine(address): return False
564 if address in self.imported_keys.keys(): return False
565 acct, s = self.get_address_index(address)
566 if s is None: return False
569 def get_master_public_key(self):
570 if self.seed_version == 4:
571 return self.storage.get("master_public_key")
573 c, K, cK = self.storage.get("master_public_keys")["m/0'/"]
576 def get_master_private_key(self, account, password):
577 master_k = pw_decode( self.master_private_keys[account], password)
578 master_c, master_K, master_Kc = self.master_public_keys[account]
580 K, Kc = get_pubkeys_from_secret(master_k.decode('hex'))
581 assert K.encode('hex') == master_K
583 raise BaseException("Invalid password")
587 def get_address_index(self, address):
588 if address in self.imported_keys.keys():
591 for account in self.accounts.keys():
592 for for_change in [0,1]:
593 addresses = self.accounts[account].get_addresses(for_change)
594 for addr in addresses:
596 return account, (for_change, addresses.index(addr))
598 for k,v in self.next_addresses.items():
602 raise BaseException("Address not found", address)
605 def get_roots(self, account):
607 for a in account.split('&'):
609 m = re.match("(m/\d+'/)(\d+)", s)
610 roots.append( m.group(1) )
613 def is_seeded(self, account):
614 if type(account) is int:
615 return self.seed is not None
617 for root in self.get_roots(account):
618 if root not in self.master_private_keys.keys():
622 def rebase_sequence(self, account, sequence):
625 for a in account.split('&'):
627 m = re.match("(m/\d+'/)(\d+)", s)
629 num = int(m.group(2))
630 dd.append( (root, [num,c,i] ) )
634 def get_keyID(self, account, sequence):
638 rs = self.rebase_sequence(account, sequence)
640 for root, public_sequence in rs:
641 c, K, _ = self.master_public_keys[root]
642 s = '/' + '/'.join( map(lambda x:str(x), public_sequence) )
643 dd.append( 'bip32(%s,%s,%s)'%(c,K, s) )
648 def get_seed(self, password):
649 s = pw_decode(self.seed, password)
650 if self.seed_version == 4:
653 seed = mnemonic_hash(s)
654 #todo: #self.sequences[0].check_seed(seed)
658 def get_mnemonic(self, password):
660 s = pw_decode(self.seed, password)
661 if self.seed_version == 4:
662 return ' '.join(mnemonic.mn_encode(s))
668 def get_private_key(self, address, password):
670 if address in self.imported_keys.keys():
671 out.append( pw_decode( self.imported_keys[address], password ) )
673 account, sequence = self.get_address_index(address)
675 seed = self.get_seed(password)
676 pk = self.accounts[account].get_private_key(seed, sequence)
680 # assert address == self.accounts[account].get_address(*sequence)
681 rs = self.rebase_sequence( account, sequence)
682 for root, public_sequence in rs:
684 if root not in self.master_private_keys.keys(): continue
685 master_k = self.get_master_private_key(root, password)
686 master_c, _, _ = self.master_public_keys[root]
687 pk = bip32_private_key( public_sequence, master_k.decode('hex'), master_c.decode('hex'))
693 def add_keypairs_from_wallet(self, tx, keypairs, password):
694 for txin in tx.inputs:
695 address = txin['address']
696 private_keys = self.get_private_key(address, password)
697 for sec in private_keys:
698 pubkey = public_key_from_private_key(sec)
699 keypairs[ pubkey ] = sec
702 def add_keypairs_from_KeyID(self, tx, keypairs, password):
703 for txin in tx.inputs:
704 keyid = txin.get('KeyID')
707 for s in keyid.split('&'):
708 m = re.match("bip32\(([0-9a-f]+),([0-9a-f]+),(/\d+/\d+/\d+)", s)
712 sequence = m.group(3)
713 root = self.find_root_by_master_key(c,K)
714 if not root: continue
715 sequence = map(lambda x:int(x), sequence.strip('/').split('/'))
716 root = root + '%d'%sequence[0]
717 sequence = sequence[1:]
718 roots.append((root,sequence))
720 account_id = " & ".join( map(lambda x:x[0], roots) )
721 account = self.accounts.get(account_id)
722 if not account: continue
723 addr = account.get_address(*sequence)
724 txin['address'] = addr # fixme: side effect
725 pk = self.get_private_key(addr, password)
727 pubkey = public_key_from_private_key(sec)
728 keypairs[pubkey] = sec
732 def signrawtransaction(self, tx, input_info, private_keys, password):
734 # check that the password is correct
735 seed = self.get_seed(password)
738 tx.add_input_info(input_info)
740 # add redeem script for coins that are in the wallet
741 # FIXME: add redeemPubkey too!
742 unspent_coins = self.get_unspent_coins()
743 for txin in tx.inputs:
744 for item in unspent_coins:
745 if txin['prevout_hash'] == item['prevout_hash'] and txin['prevout_n'] == item['prevout_n']:
746 print_error( "tx input is in unspent coins" )
747 txin['scriptPubKey'] = item['scriptPubKey']
748 account, sequence = self.get_address_index(item['address'])
750 txin['redeemScript'] = self.accounts[account].redeem_script(sequence)
751 print_error("added redeemScript", txin['redeemScript'])
755 # build a list of public/private keys
758 # add private keys from parameter
759 for sec in private_keys:
760 pubkey = public_key_from_private_key(sec)
761 keypairs[ pubkey ] = sec
763 # add private_keys from KeyID
764 self.add_keypairs_from_KeyID(tx, keypairs, password)
766 # add private keys from wallet
767 self.add_keypairs_from_wallet(tx, keypairs, password)
768 self.sign_transaction(tx, keypairs)
771 def sign_message(self, address, message, password):
772 keys = self.get_private_key(address, password)
773 assert len(keys) == 1
775 key = regenerate_key(sec)
776 compressed = is_compressed(sec)
777 return key.sign_message(message, compressed, address)
780 def change_gap_limit(self, value):
781 if value >= self.gap_limit:
782 self.gap_limit = value
783 self.storage.put('gap_limit', self.gap_limit, True)
784 #self.interface.poke('synchronizer')
787 elif value >= self.min_acceptable_gap():
788 for key, account in self.accounts.items():
789 addresses = account[0]
790 k = self.num_unused_trailing_addresses(addresses)
791 n = len(addresses) - k + value
792 addresses = addresses[0:n]
793 self.accounts[key][0] = addresses
795 self.gap_limit = value
796 self.storage.put('gap_limit', self.gap_limit, True)
802 def num_unused_trailing_addresses(self, addresses):
804 for a in addresses[::-1]:
805 if self.history.get(a):break
809 def min_acceptable_gap(self):
810 # fixme: this assumes wallet is synchronized
814 for account in self.accounts.values():
815 addresses = account.get_addresses(0)
816 k = self.num_unused_trailing_addresses(addresses)
817 for a in addresses[0:-k]:
818 if self.history.get(a):
822 if n > nmax: nmax = n
826 def address_is_old(self, address):
828 h = self.history.get(address, [])
831 for tx_hash, tx_height in h:
835 tx_age = self.verifier.blockchain.height() - tx_height + 1
841 def synchronize_sequence(self, account, for_change):
842 limit = self.gap_limit_for_change if for_change else self.gap_limit
845 addresses = account.get_addresses(for_change)
846 if len(addresses) < limit:
847 address = account.create_new_address(for_change)
848 self.history[address] = []
849 new_addresses.append( address )
852 if map( lambda a: self.address_is_old(a), addresses[-limit:] ) == limit*[False]:
855 address = account.create_new_address(for_change)
856 self.history[address] = []
857 new_addresses.append( address )
863 def create_pending_accounts(self):
864 for account_type in ['1','2of2','2of3']:
865 if not self.has_master_public_keys(account_type):
867 k, a = self.new_account_address(account_type)
868 if self.address_is_old(a):
869 print_error( "creating account", a )
870 self.create_account(account_type)
871 self.next_addresses.pop(k)
874 def synchronize_account(self, account):
876 new += self.synchronize_sequence(account, 0)
877 new += self.synchronize_sequence(account, 1)
881 def synchronize(self):
882 if self.master_public_keys:
883 self.create_pending_accounts()
885 for account in self.accounts.values():
886 new += self.synchronize_account(account)
889 self.storage.put('addr_history', self.history, True)
894 return self.history.values() != [[]] * len(self.history)
897 def add_contact(self, address, label=None):
898 self.addressbook.append(address)
899 self.storage.put('contacts', self.addressbook, True)
901 self.set_label(address, label)
904 def delete_contact(self, addr):
905 if addr in self.addressbook:
906 self.addressbook.remove(addr)
907 self.storage.put('addressbook', self.addressbook, True)
910 def fill_addressbook(self):
911 for tx_hash, tx in self.transactions.items():
912 is_relevant, is_send, _, _ = self.get_tx_value(tx)
914 for addr, v in tx.outputs:
915 if not self.is_mine(addr) and addr not in self.addressbook:
916 self.addressbook.append(addr)
918 # self.update_tx_labels()
920 def get_num_tx(self, address):
922 for tx in self.transactions.values():
923 if address in map(lambda x:x[0], tx.outputs): n += 1
927 def get_address_flags(self, addr):
928 flags = "C" if self.is_change(addr) else "I" if addr in self.imported_keys.keys() else "-"
929 flags += "F" if addr in self.frozen_addresses else "P" if addr in self.prioritized_addresses else "-"
933 def get_tx_value(self, tx, account=None):
934 domain = self.get_account_addresses(account)
935 return tx.get_value(domain, self.prevout_values)
938 def update_tx_outputs(self, tx_hash):
939 tx = self.transactions.get(tx_hash)
941 for i, (addr, value) in enumerate(tx.outputs):
942 key = tx_hash+ ':%d'%i
943 self.prevout_values[key] = value
945 for item in tx.inputs:
946 if self.is_mine(item.get('address')):
947 key = item['prevout_hash'] + ':%d'%item['prevout_n']
948 self.spent_outputs.append(key)
951 def get_addr_balance(self, address):
952 assert self.is_mine(address)
953 h = self.history.get(address,[])
954 if h == ['*']: return 0,0
956 received_coins = [] # list of coins received at address
958 for tx_hash, tx_height in h:
959 tx = self.transactions.get(tx_hash)
962 for i, (addr, value) in enumerate(tx.outputs):
964 key = tx_hash + ':%d'%i
965 received_coins.append(key)
967 for tx_hash, tx_height in h:
968 tx = self.transactions.get(tx_hash)
972 for item in tx.inputs:
973 addr = item.get('address')
975 key = item['prevout_hash'] + ':%d'%item['prevout_n']
976 value = self.prevout_values.get( key )
977 if key in received_coins:
980 for i, (addr, value) in enumerate(tx.outputs):
981 key = tx_hash + ':%d'%i
992 def get_account_name(self, k):
994 if self.seed_version == 4:
995 name = 'Main account'
999 default = "Unnamed account"
1000 m = re.match("m/0'/(\d+)", k)
1004 default = "Main account"
1006 default = "Account %s"%num
1008 m = re.match("m/1'/(\d+) & m/2'/(\d+)", k)
1011 default = "2of2 account %s"%num
1012 name = self.labels.get(k, default)
1016 def get_account_names(self):
1018 for k, account in self.accounts.items():
1019 accounts[k] = self.get_account_name(k)
1020 if self.imported_keys:
1021 accounts[-1] = 'Imported keys'
1024 def get_account_addresses(self, a, include_change=True):
1026 o = self.addresses(True)
1028 o = self.imported_keys.keys()
1030 ac = self.accounts[a]
1031 o = ac.get_addresses(0)
1032 if include_change: o += ac.get_addresses(1)
1035 def get_imported_balance(self):
1036 return self.get_balance(self.imported_keys.keys())
1038 def get_account_balance(self, account):
1039 return self.get_balance(self.get_account_addresses(account))
1041 def get_frozen_balance(self):
1042 return self.get_balance(self.frozen_addresses)
1044 def get_balance(self, domain=None):
1045 if domain is None: domain = self.addresses(True)
1048 c, u = self.get_addr_balance(addr)
1054 def get_unspent_coins(self, domain=None):
1056 if domain is None: domain = self.addresses(True)
1058 h = self.history.get(addr, [])
1059 if h == ['*']: continue
1060 for tx_hash, tx_height in h:
1061 tx = self.transactions.get(tx_hash)
1062 if tx is None: raise BaseException("Wallet not synchronized")
1063 is_coinbase = tx.inputs[0].get('prevout_hash') == '0'*64
1064 for output in tx.d.get('outputs'):
1065 if output.get('address') != addr: continue
1066 key = tx_hash + ":%d" % output.get('prevout_n')
1067 if key in self.spent_outputs: continue
1068 output['prevout_hash'] = tx_hash
1069 output['height'] = tx_height
1070 output['coinbase'] = is_coinbase
1071 coins.append((tx_height, output))
1075 coins = sorted(coins)
1076 if coins[-1][0] != 0:
1077 while coins[0][0] == 0:
1078 coins = coins[1:] + [ coins[0] ]
1079 return [x[1] for x in coins]
1083 def choose_tx_inputs_from_account( self, amount, fixed_fee, account ):
1084 domain = self.get_account_addresses(account) if account else None
1085 return self.choose_tx_inputs( amount, fixed_fee, domain )
1088 def choose_tx_inputs( self, amount, fixed_fee, domain = None ):
1089 """ todo: minimize tx size """
1091 fee = self.fee if fixed_fee is None else fixed_fee
1093 domain = self.addresses(True)
1095 for i in self.frozen_addresses:
1096 if i in domain: domain.remove(i)
1099 for i in self.prioritized_addresses:
1102 prioritized.append(i)
1104 coins = self.get_unspent_coins(domain)
1105 prioritized_coins = self.get_unspent_coins(prioritized)
1108 coins = prioritized_coins + coins
1111 if item.get('coinbase') and item.get('height') + COINBASE_MATURITY > self.network.blockchain.height:
1113 addr = item.get('address')
1114 v = item.get('value')
1117 fee = self.estimated_fee(inputs) if fixed_fee is None else fixed_fee
1118 if total >= amount + fee: break
1122 return inputs, total, fee
1125 def set_fee(self, fee):
1128 self.storage.put('fee_per_kb', self.fee, True)
1130 def estimated_fee(self, inputs):
1131 estimated_size = len(inputs) * 180 + 80 # this assumes non-compressed keys
1132 fee = self.fee * int(round(estimated_size/1024.))
1133 if fee == 0: fee = self.fee
1137 def add_tx_change( self, inputs, outputs, amount, fee, total, change_addr=None):
1138 "add change to a transaction"
1139 change_amount = total - ( amount + fee )
1140 if change_amount != 0:
1143 # send change to one of the accounts involved in the tx
1144 address = inputs[0].get('address')
1145 account, _ = self.get_address_index(address)
1147 if not self.use_change or account == -1:
1148 change_addr = inputs[-1]['address']
1150 change_addr = self.accounts[account].get_addresses(1)[-self.gap_limit_for_change]
1152 # Insert the change output at a random position in the outputs
1153 posn = random.randint(0, len(outputs))
1154 outputs[posn:posn] = [( change_addr, change_amount)]
1158 def get_history(self, address):
1160 return self.history.get(address)
1163 def get_status(self, h):
1164 if not h: return None
1165 if h == ['*']: return '*'
1167 for tx_hash, height in h:
1168 status += tx_hash + ':%d:' % height
1169 return hashlib.sha256( status ).digest().encode('hex')
1172 def receive_tx_callback(self, tx_hash, tx, tx_height):
1174 with self.transaction_lock:
1175 self.add_extra_addresses(tx)
1176 if not self.check_new_tx(tx_hash, tx):
1177 # may happen due to pruning
1178 print_error("received transaction that is no longer referenced in history", tx_hash)
1180 self.transactions[tx_hash] = tx
1181 self.network.interface.pending_transactions_for_notifications.append(tx)
1182 self.save_transactions()
1183 if self.verifier and tx_height>0:
1184 self.verifier.add(tx_hash, tx_height)
1185 self.update_tx_outputs(tx_hash)
1188 def save_transactions(self):
1190 for k,v in self.transactions.items():
1192 self.storage.put('transactions', tx, True)
1194 def receive_history_callback(self, addr, hist):
1196 if not self.check_new_history(addr, hist):
1197 raise BaseException("error: received history for %s is not consistent with known transactions"%addr)
1200 self.history[addr] = hist
1201 self.storage.put('addr_history', self.history, True)
1204 for tx_hash, tx_height in hist:
1206 # add it in case it was previously unconfirmed
1207 if self.verifier: self.verifier.add(tx_hash, tx_height)
1210 def get_tx_history(self, account=None):
1211 with self.transaction_lock:
1212 history = self.transactions.items()
1213 history.sort(key = lambda x: self.verifier.get_txpos(x[0]))
1217 for tx_hash, tx in history:
1218 is_relevant, is_mine, v, fee = self.get_tx_value(tx, account)
1219 if v is not None: balance += v
1221 c, u = self.get_account_balance(account)
1224 result.append( ('', 1000, 0, c+u-balance, None, c+u-balance, None ) )
1226 balance = c + u - balance
1227 for tx_hash, tx in history:
1228 is_relevant, is_mine, value, fee = self.get_tx_value(tx, account)
1231 if value is not None:
1234 conf, timestamp = self.verifier.get_confirmations(tx_hash) if self.verifier else (None, None)
1235 result.append( (tx_hash, conf, is_mine, value, fee, balance, timestamp) )
1240 def get_label(self, tx_hash):
1241 label = self.labels.get(tx_hash)
1242 is_default = (label == '') or (label is None)
1243 if is_default: label = self.get_default_label(tx_hash)
1244 return label, is_default
1247 def get_default_label(self, tx_hash):
1248 tx = self.transactions.get(tx_hash)
1251 is_relevant, is_mine, _, _ = self.get_tx_value(tx)
1253 for o in tx.outputs:
1255 if not self.is_mine(o_addr):
1257 default_label = self.labels[o_addr]
1259 default_label = o_addr
1262 default_label = '(internal)'
1264 for o in tx.outputs:
1266 if self.is_mine(o_addr) and not self.is_change(o_addr):
1269 for o in tx.outputs:
1271 if self.is_mine(o_addr):
1277 dest_label = self.labels.get(o_addr)
1279 default_label = self.labels[o_addr]
1281 default_label = o_addr
1283 return default_label
1286 def make_unsigned_transaction(self, outputs, fee=None, change_addr=None, domain=None ):
1287 for address, x in outputs:
1288 assert is_valid(address)
1289 amount = sum( map(lambda x:x[1], outputs) )
1290 inputs, total, fee = self.choose_tx_inputs( amount, fee, domain )
1292 raise ValueError("Not enough funds")
1293 self.add_input_info(inputs)
1294 outputs = self.add_tx_change(inputs, outputs, amount, fee, total, change_addr)
1295 return Transaction.from_io(inputs, outputs)
1298 def mktx_from_account(self, outputs, password, fee=None, account=None):
1299 domain = self.get_account_addresses(account) if account else None
1300 return self.mktx(outputs, password, fee, change_addr=None, domain=domain)
1303 def mktx(self, outputs, password, fee=None, change_addr=None, domain= None ):
1304 tx = self.make_unsigned_transaction(outputs, fee, change_addr, domain)
1306 self.add_keypairs_from_wallet(tx, keypairs, password)
1308 self.sign_transaction(tx, keypairs)
1312 def add_input_info(self, inputs):
1314 address = txin['address']
1315 account, sequence = self.get_address_index(address)
1316 txin['KeyID'] = self.get_keyID(account, sequence)
1317 redeemScript = self.accounts[account].redeem_script(sequence)
1319 txin['redeemScript'] = redeemScript
1321 txin['redeemPubkey'] = self.accounts[account].get_pubkey(*sequence)
1324 def sign_transaction(self, tx, keypairs):
1326 run_hook('sign_transaction', tx)
1329 def sendtx(self, tx):
1331 h = self.send_tx(tx)
1332 self.tx_event.wait()
1333 return self.receive_tx(h)
1335 def send_tx(self, tx):
1337 self.tx_event.clear()
1338 self.network.interface.send([('blockchain.transaction.broadcast', [str(tx)])], self.on_broadcast)
1341 def on_broadcast(self, i, r):
1342 self.tx_result = r.get('result')
1345 def receive_tx(self,tx_hash):
1346 out = self.tx_result
1348 return False, "error: " + out
1353 def update_password(self, old_password, new_password):
1354 if new_password == '': new_password = None
1355 # this will throw an exception if unicode cannot be converted
1356 decoded = pw_decode(self.seed, old_password)
1357 self.seed = pw_encode( decoded, new_password)
1358 self.storage.put('seed', self.seed, True)
1359 self.use_encryption = (new_password != None)
1360 self.storage.put('use_encryption', self.use_encryption,True)
1361 for k in self.imported_keys.keys():
1362 a = self.imported_keys[k]
1363 b = pw_decode(a, old_password)
1364 c = pw_encode(b, new_password)
1365 self.imported_keys[k] = c
1366 self.storage.put('imported_keys', self.imported_keys, True)
1368 for k, v in self.master_private_keys.items():
1369 b = pw_decode(v, old_password)
1370 c = pw_encode(b, new_password)
1371 self.master_private_keys[k] = c
1372 self.storage.put('master_private_keys', self.master_private_keys, True)
1375 def freeze(self,addr):
1376 if self.is_mine(addr) and addr not in self.frozen_addresses:
1377 self.unprioritize(addr)
1378 self.frozen_addresses.append(addr)
1379 self.storage.put('frozen_addresses', self.frozen_addresses, True)
1384 def unfreeze(self,addr):
1385 if self.is_mine(addr) and addr in self.frozen_addresses:
1386 self.frozen_addresses.remove(addr)
1387 self.storage.put('frozen_addresses', self.frozen_addresses, True)
1392 def prioritize(self,addr):
1393 if self.is_mine(addr) and addr not in self.prioritized_addresses:
1395 self.prioritized_addresses.append(addr)
1396 self.storage.put('prioritized_addresses', self.prioritized_addresses, True)
1401 def unprioritize(self,addr):
1402 if self.is_mine(addr) and addr in self.prioritized_addresses:
1403 self.prioritized_addresses.remove(addr)
1404 self.storage.put('prioritized_addresses', self.prioritized_addresses, True)
1410 def set_verifier(self, verifier):
1411 self.verifier = verifier
1413 # review transactions that are in the history
1414 for addr, hist in self.history.items():
1415 if hist == ['*']: continue
1416 for tx_hash, tx_height in hist:
1418 # add it in case it was previously unconfirmed
1419 self.verifier.add(tx_hash, tx_height)
1422 # if we are on a pruning server, remove unverified transactions
1423 vr = self.verifier.transactions.keys() + self.verifier.verified_tx.keys()
1424 for tx_hash in self.transactions.keys():
1425 if tx_hash not in vr:
1426 self.transactions.pop(tx_hash)
1430 def check_new_history(self, addr, hist):
1432 # check that all tx in hist are relevant
1434 for tx_hash, height in hist:
1435 tx = self.transactions.get(tx_hash)
1437 if not tx.has_address(addr):
1440 # check that we are not "orphaning" a transaction
1441 old_hist = self.history.get(addr,[])
1442 if old_hist == ['*']: return True
1444 for tx_hash, height in old_hist:
1445 if tx_hash in map(lambda x:x[0], hist): continue
1447 for _addr, _hist in self.history.items():
1448 if _addr == addr: continue
1449 if _hist == ['*']: continue
1450 _tx_hist = map(lambda x:x[0], _hist)
1451 if tx_hash in _tx_hist:
1456 tx = self.transactions.get(tx_hash)
1457 # tx might not be there
1461 if self.verifier.get_height(tx_hash):
1464 print_error("new history is orphaning transaction:", tx_hash)
1465 # check that all outputs are not mine, request histories
1467 for _addr, _v in tx.outputs:
1468 # assert not self.is_mine(_addr)
1469 ext_requests.append( ('blockchain.address.get_history', [_addr]) )
1471 ext_h = self.network.synchronous_get(ext_requests)
1472 print_error("sync:", ext_requests, ext_h)
1475 if h == ['*']: continue
1477 if item.get('tx_hash') == tx_hash:
1478 height = item.get('height')
1480 print_error("found height for", tx_hash, height)
1481 self.verifier.add(tx_hash, height)
1483 print_error("removing orphaned tx from history", tx_hash)
1484 self.transactions.pop(tx_hash)
1490 def check_new_tx(self, tx_hash, tx):
1491 # 1 check that tx is referenced in addr_history.
1493 for addr, hist in self.history.items():
1494 if hist == ['*']:continue
1495 for txh, height in hist:
1497 addresses.append(addr)
1502 # 2 check that referencing addresses are in the tx
1503 for addr in addresses:
1504 if not tx.has_address(addr):
1510 def start_threads(self, network):
1511 from verifier import TxVerifier
1512 self.network = network
1513 self.verifier = TxVerifier(self.network, self.storage)
1514 self.verifier.start()
1515 self.set_verifier(self.verifier)
1516 self.synchronizer = WalletSynchronizer(self, network)
1517 self.synchronizer.start()
1519 def stop_threads(self):
1520 self.verifier.stop()
1521 self.synchronizer.stop()
1525 def restore(self, callback):
1527 def wait_for_wallet():
1528 self.set_up_to_date(False)
1529 while not self.is_up_to_date():
1530 msg = "%s\n%s %d\n%s %.1f"%(
1531 _("Please wait..."),
1532 _("Addresses generated:"),
1533 len(self.addresses(True)),_("Kilobytes received:"),
1534 self.network.interface.bytes_received/1024.)
1536 apply(callback, (msg,))
1539 def wait_for_network():
1540 while not self.network.interface.is_connected:
1541 msg = "%s \n" % (_("Connecting..."))
1542 apply(callback, (msg,))
1545 # wait until we are connected, because the user might have selected another server
1549 if self.seed_version == 4:
1550 self.create_old_account()
1552 self.create_accounts()
1558 class WalletSynchronizer(threading.Thread):
1561 def __init__(self, wallet, network):
1562 threading.Thread.__init__(self)
1564 self.wallet = wallet
1565 self.network = network
1566 self.was_updated = True
1567 self.running = False
1568 self.lock = threading.Lock()
1569 self.queue = Queue.Queue()
1572 with self.lock: self.running = False
1574 def is_running(self):
1575 with self.lock: return self.running
1578 def subscribe_to_addresses(self, addresses):
1580 for addr in addresses:
1581 messages.append(('blockchain.address.subscribe', [addr]))
1582 self.network.subscribe( messages, lambda i,r: self.queue.put(r))
1589 while self.is_running():
1591 if not self.network.is_connected():
1592 print_error("synchronizer: waiting for interface")
1593 self.network.wait_until_connected()
1595 self.run_interface(self.network.interface)
1598 def run_interface(self, interface):
1600 print_error("synchronizer: connected to", interface.server)
1604 requested_histories = {}
1606 # request any missing transactions
1607 for history in self.wallet.history.values():
1608 if history == ['*']: continue
1609 for tx_hash, tx_height in history:
1610 if self.wallet.transactions.get(tx_hash) is None and (tx_hash, tx_height) not in missing_tx:
1611 missing_tx.append( (tx_hash, tx_height) )
1614 print_error("missing tx", missing_tx)
1617 self.subscribe_to_addresses(self.wallet.addresses(True))
1619 while self.is_running():
1620 # 1. create new addresses
1621 new_addresses = self.wallet.synchronize()
1623 # request missing addresses
1625 self.subscribe_to_addresses(new_addresses)
1627 # request missing transactions
1628 for tx_hash, tx_height in missing_tx:
1629 if (tx_hash, tx_height) not in requested_tx:
1630 interface.send([ ('blockchain.transaction.get',[tx_hash, tx_height]) ], lambda i,r: self.queue.put(r))
1631 requested_tx.append( (tx_hash, tx_height) )
1634 # detect if situation has changed
1635 if interface.is_up_to_date() and self.queue.empty():
1636 if not self.wallet.is_up_to_date():
1637 self.wallet.set_up_to_date(True)
1638 self.was_updated = True
1640 if self.wallet.is_up_to_date():
1641 self.wallet.set_up_to_date(False)
1642 self.was_updated = True
1644 if self.was_updated:
1645 self.wallet.network.trigger_callback('updated')
1646 self.was_updated = False
1650 r = self.queue.get(block=True, timeout=1)
1654 if interface != self.network.interface:
1660 # 3. handle response
1661 method = r['method']
1662 params = r['params']
1663 result = r.get('result')
1664 error = r.get('error')
1669 if method == 'blockchain.address.subscribe':
1671 if self.wallet.get_status(self.wallet.get_history(addr)) != result:
1672 if requested_histories.get(addr) is None:
1673 interface.send([('blockchain.address.get_history', [addr])], lambda i,r:self.queue.put(r))
1674 requested_histories[addr] = result
1676 elif method == 'blockchain.address.get_history':
1678 print_error("receiving history", addr, result)
1680 assert requested_histories.pop(addr) == '*'
1681 self.wallet.receive_history_callback(addr, result)
1684 # check that txids are unique
1687 tx_hash = item['tx_hash']
1688 if tx_hash not in txids:
1689 txids.append(tx_hash)
1690 hist.append( (tx_hash, item['height']) )
1692 if len(hist) != len(result):
1693 raise BaseException("error: server sent history with non-unique txid", result)
1695 # check that the status corresponds to what was announced
1696 rs = requested_histories.pop(addr)
1697 if self.wallet.get_status(hist) != rs:
1698 raise BaseException("error: status mismatch: %s"%addr)
1700 # store received history
1701 self.wallet.receive_history_callback(addr, hist)
1703 # request transactions that we don't have
1704 for tx_hash, tx_height in hist:
1705 if self.wallet.transactions.get(tx_hash) is None:
1706 if (tx_hash, tx_height) not in requested_tx and (tx_hash, tx_height) not in missing_tx:
1707 missing_tx.append( (tx_hash, tx_height) )
1709 elif method == 'blockchain.transaction.get':
1711 tx_height = params[1]
1712 assert tx_hash == hash_encode(Hash(result.decode('hex')))
1713 tx = Transaction(result)
1714 self.wallet.receive_tx_callback(tx_hash, tx, tx_height)
1715 self.was_updated = True
1716 requested_tx.remove( (tx_hash, tx_height) )
1717 print_error("received tx:", tx_hash, len(tx.raw))
1720 print_error("Error: Unknown message:" + method + ", " + repr(params) + ", " + repr(result) )
1722 if self.was_updated and not requested_tx:
1723 self.wallet.network.trigger_callback('updated')
1724 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
1726 self.was_updated = False