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/>.
34 from util import print_msg, print_error, format_satoshis
37 from transaction import Transaction
38 from plugins import run_hook
40 COINBASE_MATURITY = 100
44 EncodeAES = lambda secret, s: base64.b64encode(aes.encryptData(secret,s))
45 DecodeAES = lambda secret, e: aes.decryptData(secret, base64.b64decode(e))
47 def pw_encode(s, password):
49 secret = Hash(password)
50 return EncodeAES(secret, s)
54 def pw_decode(s, password):
55 if password is not None:
56 secret = Hash(password)
58 d = DecodeAES(secret, s)
60 raise Exception('Invalid password')
74 def __init__(self, config):
75 self.lock = threading.Lock()
78 self.file_exists = False
79 self.path = self.init_path(config)
80 print_error( "wallet path", self.path )
85 def init_path(self, config):
86 """Set the path of the wallet."""
88 # command line -w option
89 path = config.get('wallet_path')
94 path = config.get('default_wallet_path')
99 dirpath = os.path.join(config.path, "wallets")
100 if not os.path.exists(dirpath):
103 new_path = os.path.join(config.path, "wallets", "default_wallet")
105 # default path in pre 1.9 versions
106 old_path = os.path.join(config.path, "electrum.dat")
107 if os.path.exists(old_path) and not os.path.exists(new_path):
108 os.rename(old_path, new_path)
113 def read(self, path):
114 """Read the contents of the wallet file."""
116 with open(self.path, "r") as f:
121 d = ast.literal_eval( data ) #parse raw data from reading wallet file
123 raise IOError("Cannot read wallet file.")
126 self.file_exists = True
129 def get(self, key, default=None):
130 v = self.data.get(key)
135 def put(self, key, value, save = True):
138 if value is not None:
139 self.data[key] = value
147 f = open(self.path,"w")
150 if 'ANDROID_DATA' not in os.environ:
152 os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)
160 def __init__(self, storage):
162 self.storage = storage
163 self.electrum_version = ELECTRUM_VERSION
164 self.gap_limit_for_change = 3 # constant
167 self.seed_version = storage.get('seed_version', NEW_SEED_VERSION)
169 self.gap_limit = storage.get('gap_limit', 5)
170 self.use_change = storage.get('use_change',True)
171 self.use_encryption = storage.get('use_encryption', False)
172 self.seed = storage.get('seed', '') # encrypted
173 self.labels = storage.get('labels', {})
174 self.frozen_addresses = storage.get('frozen_addresses',[])
175 self.addressbook = storage.get('contacts', [])
177 self.imported_keys = storage.get('imported_keys',{})
178 self.history = storage.get('addr_history',{}) # address -> list(txid, height)
180 self.fee = int(storage.get('fee_per_kb', 10000))
182 self.master_public_keys = storage.get('master_public_keys',{})
183 self.master_private_keys = storage.get('master_private_keys', {})
185 self.next_addresses = storage.get('next_addresses',{})
188 # This attribute is set when wallet.start_threads is called.
189 self.synchronizer = None
193 self.transactions = {}
194 tx_list = self.storage.get('transactions',{})
195 for k,v in tx_list.items():
199 print_msg("Warning: Cannot deserialize transactions. skipping")
202 self.add_extra_addresses(tx)
203 self.transactions[k] = tx
205 for h,tx in self.transactions.items():
206 if not self.check_new_tx(h, tx):
207 print_error("removing unreferenced tx", h)
208 self.transactions.pop(h)
212 self.prevout_values = {} # my own transaction outputs
213 self.spent_outputs = []
218 # there is a difference between wallet.up_to_date and interface.is_up_to_date()
219 # interface.is_up_to_date() returns true when all requests have been answered and processed
220 # wallet.up_to_date is true when the wallet is synchronized (stronger requirement)
222 self.up_to_date = False
223 self.lock = threading.Lock()
224 self.transaction_lock = threading.Lock()
225 self.tx_event = threading.Event()
227 for tx_hash, tx in self.transactions.items():
228 self.update_tx_outputs(tx_hash)
231 def add_extra_addresses(self, tx):
233 # find the address corresponding to pay-to-pubkey inputs
234 tx.add_extra_addresses(self.transactions)
235 for o in tx.d.get('outputs'):
236 if o.get('is_pubkey'):
237 for tx2 in self.transactions.values():
238 tx2.add_extra_addresses({h:tx})
241 def get_action(self):
245 def can_create_accounts(self):
246 return not self.is_watching_only()
249 def set_up_to_date(self,b):
250 with self.lock: self.up_to_date = b
253 def is_up_to_date(self):
254 with self.lock: return self.up_to_date
258 self.up_to_date = False
259 while not self.is_up_to_date():
263 def import_key(self, sec, password):
265 seed = self.get_seed(password)
267 address = address_from_private_key(sec)
269 raise Exception('Invalid private key')
271 if self.is_mine(address):
272 raise Exception('Address already in wallet')
274 # store the originally requested keypair into the imported keys table
275 self.imported_keys[address] = pw_encode(sec, password )
276 self.storage.put('imported_keys', self.imported_keys, True)
277 if self.synchronizer:
278 self.synchronizer.subscribe_to_addresses([address])
281 def delete_imported_key(self, addr):
282 if addr in self.imported_keys:
283 self.imported_keys.pop(addr)
284 self.storage.put('imported_keys', self.imported_keys, True)
288 import mnemonic, ecdsa
289 entropy = ecdsa.util.randrange( pow(2,160) )
292 ss = "%040x"%(entropy+nonce)
293 s = hashlib.sha256(ss.decode('hex')).digest().encode('hex')
294 # we keep only 13 words, that's approximately 139 bits of entropy
295 words = mnemonic.mn_encode(s)[0:13]
296 seed = ' '.join(words)
297 if is_new_seed(seed):
298 break # this will remove 8 bits of entropy
304 def prepare_seed(self, seed):
306 return NEW_SEED_VERSION, unicodedata.normalize('NFC', unicode(seed.strip()))
310 def add_seed(self, seed, password):
312 raise Exception("a seed exists")
314 self.seed_version, self.seed = self.prepare_seed(seed)
316 self.seed = pw_encode( self.seed, password)
317 self.use_encryption = True
319 self.use_encryption = False
321 self.storage.put('seed', self.seed, True)
322 self.storage.put('seed_version', self.seed_version, True)
323 self.storage.put('use_encryption', self.use_encryption,True)
324 self.create_master_keys(password)
327 def create_watching_only_wallet(self, xpub):
328 self.storage.put('seed_version', self.seed_version, True)
329 self.add_master_public_key("m/", xpub)
330 account = BIP32_Account({'xpub':xpub})
331 self.add_account("m/", account)
334 def create_accounts(self, password):
335 seed = pw_decode(self.seed, password)
336 self.create_account('Main account', password)
339 def add_master_public_key(self, name, mpk):
340 self.master_public_keys[name] = mpk
341 self.storage.put('master_public_keys', self.master_public_keys, True)
344 def add_master_private_key(self, name, xpriv, password):
345 self.master_private_keys[name] = pw_encode(xpriv, password)
346 self.storage.put('master_private_keys', self.master_private_keys, True)
349 def add_master_keys(self, root, account_id, password):
350 x = self.master_private_keys.get(root)
352 master_xpriv = pw_decode(x, password )
353 xpriv, xpub = bip32_private_derivation(master_xpriv, root, account_id)
354 self.add_master_public_key(account_id, xpub)
355 self.add_master_private_key(account_id, xpriv, password)
357 master_xpub = self.master_public_keys[root]
358 xpub = bip32_public_derivation(master_xpub, root, account_id)
359 self.add_master_public_key(account_id, xpub)
363 def create_master_keys(self, password):
364 xpriv, xpub = bip32_root(self.get_seed(password))
365 self.add_master_public_key("m/", xpub)
366 self.add_master_private_key("m/", xpriv, password)
369 def find_root_by_master_key(self, xpub):
370 for key, xpub2 in self.master_public_keys.items():
371 if key == "m/":continue
375 def is_watching_only(self):
376 return (self.seed == '') and (self.master_private_keys == {})
379 def num_accounts(self):
380 keys = self.accounts.keys()
383 account_id = self.account_id(i)
384 if account_id not in keys: break
389 def next_account_address(self, password):
390 i = self.num_accounts()
391 account_id = self.account_id(i)
393 addr = self.next_addresses.get(account_id)
395 account = self.make_account(account_id, password)
396 addr = account.first_address()
397 self.next_addresses[account_id] = addr
398 self.storage.put('next_addresses', self.next_addresses)
400 return account_id, addr
402 def account_id(self, i):
405 def make_account(self, account_id, password):
406 """Creates and saves the master keys, but does not save the account"""
407 xpub = self.add_master_keys("m/", account_id, password)
408 account = BIP32_Account({'xpub':xpub})
412 def set_label(self, name, text = None):
414 old_text = self.labels.get(name)
417 self.labels[name] = text
421 self.labels.pop(name)
425 self.storage.put('labels', self.labels, True)
427 run_hook('set_label', name, text, changed)
431 def create_account(self, name, password):
432 i = self.num_accounts()
433 account_id = self.account_id(i)
434 account = self.make_account(account_id, password)
435 self.add_account(account_id, account)
437 self.set_label(account_id, name)
439 # add address of the next account
440 _, _ = self.next_account_address(password)
443 def add_account(self, account_id, account):
444 self.accounts[account_id] = account
445 if account_id in self.pending_accounts:
446 self.pending_accounts.pop(account_id)
447 self.storage.put('pending_accounts', self.pending_accounts)
451 def save_accounts(self):
453 for k, v in self.accounts.items():
455 self.storage.put('accounts', d, True)
459 def load_accounts(self):
460 d = self.storage.get('accounts', {})
462 for k, v in d.items():
464 v['mpk'] = self.storage.get('master_public_key')
465 self.accounts[k] = OldAccount(v)
467 self.accounts[k] = BIP32_Account_2of3(v)
469 self.accounts[k] = BIP32_Account_2of2(v)
471 self.accounts[k] = BIP32_Account(v)
475 self.pending_accounts = self.storage.get('pending_accounts',{})
478 def delete_pending_account(self, k):
479 self.pending_accounts.pop(k)
480 self.storage.put('pending_accounts', self.pending_accounts)
482 def account_is_pending(self, k):
483 return k in self.pending_accounts
485 def create_pending_account(self, name, password):
486 account_id, addr = self.next_account_address(password)
487 self.set_label(account_id, name)
488 self.pending_accounts[account_id] = addr
489 self.storage.put('pending_accounts', self.pending_accounts)
491 def get_pending_accounts(self):
492 return self.pending_accounts.items()
495 def addresses(self, include_change = True, _next=True):
496 o = self.get_account_addresses(-1, include_change)
497 for a in self.accounts.keys():
498 o += self.get_account_addresses(a, include_change)
501 for addr in self.next_addresses.values():
507 def is_mine(self, address):
508 return address in self.addresses(True)
511 def is_change(self, address):
512 if not self.is_mine(address): return False
513 if address in self.imported_keys.keys(): return False
514 acct, s = self.get_address_index(address)
515 if s is None: return False
518 def get_master_public_keys(self):
520 for k, account in self.accounts.items():
521 name = self.get_account_name(k)
522 mpk_text = '\n\n'.join( account.get_master_pubkeys() )
526 def get_master_private_key(self, account, password):
527 k = self.master_private_keys.get(account)
529 xpriv = pw_decode( k, password)
533 def get_address_index(self, address):
534 if address in self.imported_keys.keys():
537 for account in self.accounts.keys():
538 for for_change in [0,1]:
539 addresses = self.accounts[account].get_addresses(for_change)
540 for addr in addresses:
542 return account, (for_change, addresses.index(addr))
544 for k,v in self.next_addresses.items():
548 raise Exception("Address not found", address)
551 def getpubkeys(self, addr):
552 assert is_valid(addr) and self.is_mine(addr)
553 account, sequence = self.get_address_index(addr)
555 a = self.accounts[account]
556 return a.get_pubkeys( sequence )
559 def get_roots(self, account):
561 for a in account.split('&'):
563 m = re.match("m/(\d+')", s)
564 roots.append( m.group(1) )
568 def is_seeded(self, account):
572 for root in self.get_roots(account):
573 if root not in self.master_private_keys.keys():
577 def rebase_sequence(self, account, sequence):
578 # account has one or more xpub
579 # sequence is a sequence of public derivations
582 for a in account.split('&'):
584 m = re.match("m/(\d+)'", s)
586 num = int(m.group(1))
587 dd.append( (root, [num,c,i] ) )
594 def get_seed(self, password):
595 s = pw_decode(self.seed, password)
596 seed = mnemonic_to_seed(s,'').encode('hex')
600 def get_mnemonic(self, password):
601 return pw_decode(self.seed, password)
604 def get_private_key(self, address, password):
605 if self.is_watching_only():
608 # first check the provided password
609 seed = self.get_seed(password)
612 if address in self.imported_keys.keys():
613 out.append( pw_decode( self.imported_keys[address], password ) )
615 account_id, sequence = self.get_address_index(address)
616 account = self.accounts[account_id]
617 xpubs = account.get_master_pubkeys()
618 roots = [k for k, v in self.master_public_keys.iteritems() if v in xpubs]
620 xpriv = self.get_master_private_key(root, password)
623 _, _, _, c, k = deserialize_xkey(xpriv)
624 pk = bip32_private_key( sequence, k, c )
630 def get_public_keys(self, address):
631 account_id, sequence = self.get_address_index(address)
632 return self.accounts[account_id].get_pubkeys(sequence)
635 def add_keypairs_from_wallet(self, tx, keypairs, password):
636 for txin in tx.inputs:
637 address = txin['address']
638 if not self.is_mine(address):
640 private_keys = self.get_private_key(address, password)
641 for sec in private_keys:
642 pubkey = public_key_from_private_key(sec)
643 keypairs[ pubkey ] = sec
644 if address in self.imported_keys.keys():
645 txin['redeemPubkey'] = pubkey
648 def add_keypairs_from_KeyID(self, tx, keypairs, password):
649 # first check the provided password
650 seed = self.get_seed(password)
652 for txin in tx.inputs:
653 keyid = txin.get('KeyID')
656 for s in keyid.split('&'):
657 m = re.match("bip32\((.*),(/\d+/\d+)\)", s)
660 sequence = m.group(2)
661 root = self.find_root_by_master_key(xpub)
662 if not root: continue
663 sequence = map(lambda x:int(x), sequence.strip('/').split('/'))
664 root = root + '%d'%sequence[0]
665 sequence = sequence[1:]
666 roots.append((root,sequence))
668 account_id = " & ".join( map(lambda x:x[0], roots) )
669 account = self.accounts.get(account_id)
670 if not account: continue
671 addr = account.get_address(*sequence)
672 txin['address'] = addr # fixme: side effect
673 pk = self.get_private_key(addr, password)
675 pubkey = public_key_from_private_key(sec)
676 keypairs[pubkey] = sec
680 def signrawtransaction(self, tx, input_info, private_keys, password):
682 # check that the password is correct
683 seed = self.get_seed(password)
685 # if input_info is not known, build it using wallet UTXOs
688 unspent_coins = self.get_unspent_coins()
689 for txin in tx.inputs:
690 for item in unspent_coins:
691 if txin['prevout_hash'] == item['prevout_hash'] and txin['prevout_n'] == item['prevout_n']:
692 info = { 'address':item['address'], 'scriptPubKey':item['scriptPubKey'] }
693 self.add_input_info(info)
694 input_info.append(info)
697 print_error( "input not in UTXOs" )
698 input_info.append(None)
700 # add input_info to the transaction
701 print_error("input_info", input_info)
702 tx.add_input_info(input_info)
704 # build a list of public/private keys
707 # add private keys from parameter
708 for sec in private_keys:
709 pubkey = public_key_from_private_key(sec)
710 keypairs[ pubkey ] = sec
712 # add private_keys from KeyID
713 self.add_keypairs_from_KeyID(tx, keypairs, password)
714 # add private keys from wallet
715 self.add_keypairs_from_wallet(tx, keypairs, password)
716 # sign the transaction
717 self.sign_transaction(tx, keypairs, password)
720 def sign_message(self, address, message, password):
721 keys = self.get_private_key(address, password)
722 assert len(keys) == 1
724 key = regenerate_key(sec)
725 compressed = is_compressed(sec)
726 return key.sign_message(message, compressed, address)
730 def decrypt_message(self, pubkey, message, password):
731 address = public_key_to_bc_address(pubkey.decode('hex'))
732 keys = self.get_private_key(address, password)
734 ec = regenerate_key(secret)
735 decrypted = ec.decrypt_message(message)
739 def change_gap_limit(self, value):
740 if value >= self.gap_limit:
741 self.gap_limit = value
742 self.storage.put('gap_limit', self.gap_limit, True)
743 #self.interface.poke('synchronizer')
746 elif value >= self.min_acceptable_gap():
747 for key, account in self.accounts.items():
748 addresses = account[0]
749 k = self.num_unused_trailing_addresses(addresses)
750 n = len(addresses) - k + value
751 addresses = addresses[0:n]
752 self.accounts[key][0] = addresses
754 self.gap_limit = value
755 self.storage.put('gap_limit', self.gap_limit, True)
761 def num_unused_trailing_addresses(self, addresses):
763 for a in addresses[::-1]:
764 if self.history.get(a):break
768 def min_acceptable_gap(self):
769 # fixme: this assumes wallet is synchronized
773 for account in self.accounts.values():
774 addresses = account.get_addresses(0)
775 k = self.num_unused_trailing_addresses(addresses)
776 for a in addresses[0:-k]:
777 if self.history.get(a):
781 if n > nmax: nmax = n
785 def address_is_old(self, address):
787 h = self.history.get(address, [])
790 for tx_hash, tx_height in h:
794 tx_age = self.network.get_local_height() - tx_height + 1
800 def synchronize_sequence(self, account, for_change):
801 limit = self.gap_limit_for_change if for_change else self.gap_limit
804 addresses = account.get_addresses(for_change)
805 if len(addresses) < limit:
806 address = account.create_new_address(for_change)
807 self.history[address] = []
808 new_addresses.append( address )
811 if map( lambda a: self.address_is_old(a), addresses[-limit:] ) == limit*[False]:
814 address = account.create_new_address(for_change)
815 self.history[address] = []
816 new_addresses.append( address )
821 def check_pending_accounts(self):
822 for account_id, addr in self.next_addresses.items():
823 if self.address_is_old(addr):
824 print_error( "creating account", account_id )
825 xpub = self.master_public_keys[account_id]
826 account = BIP32_Account({'xpub':xpub})
827 self.add_account(account_id, account)
828 self.next_addresses.pop(account_id)
831 def synchronize_account(self, account):
833 new += self.synchronize_sequence(account, 0)
834 new += self.synchronize_sequence(account, 1)
838 def synchronize(self):
839 self.check_pending_accounts()
841 for account in self.accounts.values():
842 new += self.synchronize_account(account)
845 self.storage.put('addr_history', self.history, True)
850 return self.history.values() != [[]] * len(self.history)
853 def add_contact(self, address, label=None):
854 self.addressbook.append(address)
855 self.storage.put('contacts', self.addressbook, True)
857 self.set_label(address, label)
860 def delete_contact(self, addr):
861 if addr in self.addressbook:
862 self.addressbook.remove(addr)
863 self.storage.put('addressbook', self.addressbook, True)
866 def fill_addressbook(self):
867 for tx_hash, tx in self.transactions.items():
868 is_relevant, is_send, _, _ = self.get_tx_value(tx)
870 for addr, v in tx.outputs:
871 if not self.is_mine(addr) and addr not in self.addressbook:
872 self.addressbook.append(addr)
874 # self.update_tx_labels()
876 def get_num_tx(self, address):
878 for tx in self.transactions.values():
879 if address in map(lambda x:x[0], tx.outputs): n += 1
883 def get_address_flags(self, addr):
884 flags = "C" if self.is_change(addr) else "I" if addr in self.imported_keys.keys() else "-"
885 flags += "F" if addr in self.frozen_addresses else "-"
889 def get_tx_value(self, tx, account=None):
890 domain = self.get_account_addresses(account)
891 return tx.get_value(domain, self.prevout_values)
894 def update_tx_outputs(self, tx_hash):
895 tx = self.transactions.get(tx_hash)
897 for i, (addr, value) in enumerate(tx.outputs):
898 key = tx_hash+ ':%d'%i
899 self.prevout_values[key] = value
901 for item in tx.inputs:
902 if self.is_mine(item.get('address')):
903 key = item['prevout_hash'] + ':%d'%item['prevout_n']
904 self.spent_outputs.append(key)
907 def get_addr_balance(self, address):
908 assert self.is_mine(address)
909 h = self.history.get(address,[])
910 if h == ['*']: return 0,0
912 received_coins = [] # list of coins received at address
914 for tx_hash, tx_height in h:
915 tx = self.transactions.get(tx_hash)
918 for i, (addr, value) in enumerate(tx.outputs):
920 key = tx_hash + ':%d'%i
921 received_coins.append(key)
923 for tx_hash, tx_height in h:
924 tx = self.transactions.get(tx_hash)
928 for item in tx.inputs:
929 addr = item.get('address')
931 key = item['prevout_hash'] + ':%d'%item['prevout_n']
932 value = self.prevout_values.get( key )
933 if key in received_coins:
936 for i, (addr, value) in enumerate(tx.outputs):
937 key = tx_hash + ':%d'%i
948 def get_account_name(self, k):
949 default = "Unnamed account"
950 m = re.match("m/0'/(\d+)", k)
954 default = "Main account"
956 default = "Account %s"%num
958 m = re.match("m/1'/(\d+) & m/2'/(\d+)", k)
961 default = "2of2 account %s"%num
962 name = self.labels.get(k, default)
966 def get_account_names(self):
968 for k, account in self.accounts.items():
969 accounts[k] = self.get_account_name(k)
970 if self.imported_keys:
971 accounts[-1] = 'Imported keys'
975 def get_account_addresses(self, a, include_change=True):
977 o = self.addresses(True)
979 o = self.imported_keys.keys()
981 ac = self.accounts[a]
982 o = ac.get_addresses(0)
983 if include_change: o += ac.get_addresses(1)
986 def get_imported_balance(self):
987 return self.get_balance(self.imported_keys.keys())
989 def get_account_balance(self, account):
990 return self.get_balance(self.get_account_addresses(account))
992 def get_frozen_balance(self):
993 return self.get_balance(self.frozen_addresses)
995 def get_balance(self, domain=None):
996 if domain is None: domain = self.addresses(True)
999 c, u = self.get_addr_balance(addr)
1005 def get_unspent_coins(self, domain=None):
1007 if domain is None: domain = self.addresses(True)
1009 h = self.history.get(addr, [])
1010 if h == ['*']: continue
1011 for tx_hash, tx_height in h:
1012 tx = self.transactions.get(tx_hash)
1013 if tx is None: raise Exception("Wallet not synchronized")
1014 is_coinbase = tx.inputs[0].get('prevout_hash') == '0'*64
1015 for o in tx.d.get('outputs'):
1017 if output.get('address') != addr: continue
1018 key = tx_hash + ":%d" % output.get('prevout_n')
1019 if key in self.spent_outputs: continue
1020 output['prevout_hash'] = tx_hash
1021 output['height'] = tx_height
1022 output['coinbase'] = is_coinbase
1023 coins.append((tx_height, output))
1027 coins = sorted(coins)
1028 if coins[-1][0] != 0:
1029 while coins[0][0] == 0:
1030 coins = coins[1:] + [ coins[0] ]
1031 return [x[1] for x in coins]
1034 def choose_tx_inputs( self, amount, fixed_fee, num_outputs, domain = None ):
1035 """ todo: minimize tx size """
1037 fee = self.fee if fixed_fee is None else fixed_fee
1039 domain = self.addresses(True)
1041 for i in self.frozen_addresses:
1042 if i in domain: domain.remove(i)
1044 coins = self.get_unspent_coins(domain)
1048 if item.get('coinbase') and item.get('height') + COINBASE_MATURITY > self.network.get_local_height():
1050 addr = item.get('address')
1051 v = item.get('value')
1054 fee = self.estimated_fee(inputs, num_outputs) if fixed_fee is None else fixed_fee
1055 if total >= amount + fee: break
1059 return inputs, total, fee
1062 def set_fee(self, fee):
1065 self.storage.put('fee_per_kb', self.fee, True)
1067 def estimated_fee(self, inputs, num_outputs):
1068 estimated_size = len(inputs) * 180 + num_outputs * 34 # this assumes non-compressed keys
1069 fee = self.fee * int(math.ceil(estimated_size/1000.))
1073 def add_tx_change( self, inputs, outputs, amount, fee, total, change_addr=None):
1074 "add change to a transaction"
1075 change_amount = total - ( amount + fee )
1076 if change_amount > DUST_THRESHOLD:
1079 # send change to one of the accounts involved in the tx
1080 address = inputs[0].get('address')
1081 account, _ = self.get_address_index(address)
1083 if not self.use_change or account == -1:
1084 change_addr = inputs[-1]['address']
1086 change_addr = self.accounts[account].get_addresses(1)[-self.gap_limit_for_change]
1088 # Insert the change output at a random position in the outputs
1089 posn = random.randint(0, len(outputs))
1090 outputs[posn:posn] = [( change_addr, change_amount)]
1094 def get_history(self, address):
1096 return self.history.get(address)
1099 def get_status(self, h):
1100 if not h: return None
1101 if h == ['*']: return '*'
1103 for tx_hash, height in h:
1104 status += tx_hash + ':%d:' % height
1105 return hashlib.sha256( status ).digest().encode('hex')
1108 def receive_tx_callback(self, tx_hash, tx, tx_height):
1110 with self.transaction_lock:
1111 self.add_extra_addresses(tx)
1112 if not self.check_new_tx(tx_hash, tx):
1113 # may happen due to pruning
1114 print_error("received transaction that is no longer referenced in history", tx_hash)
1116 self.transactions[tx_hash] = tx
1117 self.network.pending_transactions_for_notifications.append(tx)
1118 self.save_transactions()
1119 if self.verifier and tx_height>0:
1120 self.verifier.add(tx_hash, tx_height)
1121 self.update_tx_outputs(tx_hash)
1124 def save_transactions(self):
1126 for k,v in self.transactions.items():
1128 self.storage.put('transactions', tx, True)
1130 def receive_history_callback(self, addr, hist):
1132 if not self.check_new_history(addr, hist):
1133 raise Exception("error: received history for %s is not consistent with known transactions"%addr)
1136 self.history[addr] = hist
1137 self.storage.put('addr_history', self.history, True)
1140 for tx_hash, tx_height in hist:
1142 # add it in case it was previously unconfirmed
1143 if self.verifier: self.verifier.add(tx_hash, tx_height)
1146 def get_tx_history(self, account=None):
1147 if not self.verifier:
1150 with self.transaction_lock:
1151 history = self.transactions.items()
1152 history.sort(key = lambda x: self.verifier.get_txpos(x[0]))
1156 for tx_hash, tx in history:
1157 is_relevant, is_mine, v, fee = self.get_tx_value(tx, account)
1158 if v is not None: balance += v
1160 c, u = self.get_account_balance(account)
1163 result.append( ('', 1000, 0, c+u-balance, None, c+u-balance, None ) )
1165 balance = c + u - balance
1166 for tx_hash, tx in history:
1167 is_relevant, is_mine, value, fee = self.get_tx_value(tx, account)
1170 if value is not None:
1173 conf, timestamp = self.verifier.get_confirmations(tx_hash) if self.verifier else (None, None)
1174 result.append( (tx_hash, conf, is_mine, value, fee, balance, timestamp) )
1179 def get_label(self, tx_hash):
1180 label = self.labels.get(tx_hash)
1181 is_default = (label == '') or (label is None)
1182 if is_default: label = self.get_default_label(tx_hash)
1183 return label, is_default
1186 def get_default_label(self, tx_hash):
1187 tx = self.transactions.get(tx_hash)
1190 is_relevant, is_mine, _, _ = self.get_tx_value(tx)
1192 for o in tx.outputs:
1194 if not self.is_mine(o_addr):
1196 default_label = self.labels[o_addr]
1198 default_label = '>' + o_addr
1201 default_label = '(internal)'
1203 for o in tx.outputs:
1205 if self.is_mine(o_addr) and not self.is_change(o_addr):
1208 for o in tx.outputs:
1210 if self.is_mine(o_addr):
1216 dest_label = self.labels.get(o_addr)
1218 default_label = self.labels[o_addr]
1220 default_label = '<' + o_addr
1222 return default_label
1225 def make_unsigned_transaction(self, outputs, fee=None, change_addr=None, domain=None ):
1226 for address, x in outputs:
1227 assert is_valid(address), "Address " + address + " is invalid!"
1228 amount = sum( map(lambda x:x[1], outputs) )
1229 inputs, total, fee = self.choose_tx_inputs( amount, fee, len(outputs), domain )
1231 raise ValueError("Not enough funds")
1233 self.add_input_info(txin)
1234 outputs = self.add_tx_change(inputs, outputs, amount, fee, total, change_addr)
1235 return Transaction.from_io(inputs, outputs)
1238 def mktx(self, outputs, password, fee=None, change_addr=None, domain= None ):
1239 tx = self.make_unsigned_transaction(outputs, fee, change_addr, domain)
1241 self.add_keypairs_from_wallet(tx, keypairs, password)
1243 self.sign_transaction(tx, keypairs, password)
1247 def add_input_info(self, txin):
1248 address = txin['address']
1249 if address in self.imported_keys.keys():
1251 account_id, sequence = self.get_address_index(address)
1252 account = self.accounts[account_id]
1253 txin['KeyID'] = account.get_keyID(sequence)
1254 redeemScript = account.redeem_script(sequence)
1256 txin['redeemScript'] = redeemScript
1258 txin['redeemPubkey'] = account.get_pubkey(*sequence)
1261 def sign_transaction(self, tx, keypairs, password):
1263 run_hook('sign_transaction', tx, password)
1266 def sendtx(self, tx):
1268 h = self.send_tx(tx)
1269 self.tx_event.wait()
1270 return self.receive_tx(h, tx)
1272 def send_tx(self, tx):
1274 self.tx_event.clear()
1275 self.network.send([('blockchain.transaction.broadcast', [str(tx)])], self.on_broadcast)
1278 def on_broadcast(self, i, r):
1279 self.tx_result = r.get('result')
1282 def receive_tx(self, tx_hash, tx):
1283 out = self.tx_result
1285 return False, "error: " + out
1286 run_hook('receive_tx', tx, self)
1291 def update_password(self, old_password, new_password):
1292 if new_password == '': new_password = None
1293 decoded = self.get_seed(old_password)
1294 self.seed = pw_encode( decoded, new_password)
1295 self.storage.put('seed', self.seed, True)
1296 self.use_encryption = (new_password != None)
1297 self.storage.put('use_encryption', self.use_encryption,True)
1298 for k in self.imported_keys.keys():
1299 a = self.imported_keys[k]
1300 b = pw_decode(a, old_password)
1301 c = pw_encode(b, new_password)
1302 self.imported_keys[k] = c
1303 self.storage.put('imported_keys', self.imported_keys, True)
1305 for k, v in self.master_private_keys.items():
1306 b = pw_decode(v, old_password)
1307 c = pw_encode(b, new_password)
1308 self.master_private_keys[k] = c
1309 self.storage.put('master_private_keys', self.master_private_keys, True)
1312 def freeze(self,addr):
1313 if self.is_mine(addr) and addr not in self.frozen_addresses:
1314 self.frozen_addresses.append(addr)
1315 self.storage.put('frozen_addresses', self.frozen_addresses, True)
1320 def unfreeze(self,addr):
1321 if self.is_mine(addr) and addr in self.frozen_addresses:
1322 self.frozen_addresses.remove(addr)
1323 self.storage.put('frozen_addresses', self.frozen_addresses, True)
1329 def set_verifier(self, verifier):
1330 self.verifier = verifier
1332 # review transactions that are in the history
1333 for addr, hist in self.history.items():
1334 if hist == ['*']: continue
1335 for tx_hash, tx_height in hist:
1337 # add it in case it was previously unconfirmed
1338 self.verifier.add(tx_hash, tx_height)
1341 # if we are on a pruning server, remove unverified transactions
1342 vr = self.verifier.transactions.keys() + self.verifier.verified_tx.keys()
1343 for tx_hash in self.transactions.keys():
1344 if tx_hash not in vr:
1345 self.transactions.pop(tx_hash)
1349 def check_new_history(self, addr, hist):
1351 # check that all tx in hist are relevant
1353 for tx_hash, height in hist:
1354 tx = self.transactions.get(tx_hash)
1356 if not tx.has_address(addr):
1359 # check that we are not "orphaning" a transaction
1360 old_hist = self.history.get(addr,[])
1361 if old_hist == ['*']: return True
1363 for tx_hash, height in old_hist:
1364 if tx_hash in map(lambda x:x[0], hist): continue
1366 for _addr, _hist in self.history.items():
1367 if _addr == addr: continue
1368 if _hist == ['*']: continue
1369 _tx_hist = map(lambda x:x[0], _hist)
1370 if tx_hash in _tx_hist:
1375 tx = self.transactions.get(tx_hash)
1376 # tx might not be there
1380 if self.verifier.get_height(tx_hash):
1383 print_error("new history is orphaning transaction:", tx_hash)
1384 # check that all outputs are not mine, request histories
1386 for _addr, _v in tx.outputs:
1387 # assert not self.is_mine(_addr)
1388 ext_requests.append( ('blockchain.address.get_history', [_addr]) )
1390 ext_h = self.network.synchronous_get(ext_requests)
1391 print_error("sync:", ext_requests, ext_h)
1394 if h == ['*']: continue
1396 if item.get('tx_hash') == tx_hash:
1397 height = item.get('height')
1399 print_error("found height for", tx_hash, height)
1400 self.verifier.add(tx_hash, height)
1402 print_error("removing orphaned tx from history", tx_hash)
1403 self.transactions.pop(tx_hash)
1409 def check_new_tx(self, tx_hash, tx):
1410 # 1 check that tx is referenced in addr_history.
1412 for addr, hist in self.history.items():
1413 if hist == ['*']:continue
1414 for txh, height in hist:
1416 addresses.append(addr)
1421 # 2 check that referencing addresses are in the tx
1422 for addr in addresses:
1423 if not tx.has_address(addr):
1429 def start_threads(self, network):
1430 from verifier import TxVerifier
1431 self.network = network
1432 if self.network is not None:
1433 self.verifier = TxVerifier(self.network, self.storage)
1434 self.verifier.start()
1435 self.set_verifier(self.verifier)
1436 self.synchronizer = WalletSynchronizer(self, network)
1437 self.synchronizer.start()
1439 self.verifier = None
1440 self.synchronizer =None
1442 def stop_threads(self):
1444 self.verifier.stop()
1445 self.synchronizer.stop()
1448 def restore(self, callback):
1450 def wait_for_wallet():
1451 self.set_up_to_date(False)
1452 while not self.is_up_to_date():
1453 msg = "%s\n%s %d\n%s %.1f"%(
1454 _("Please wait..."),
1455 _("Addresses generated:"),
1456 len(self.addresses(True)),
1457 _("Kilobytes received:"),
1458 self.network.interface.bytes_received/1024.)
1460 apply(callback, (msg,))
1463 def wait_for_network():
1464 while not self.network.is_connected():
1465 msg = "%s \n" % (_("Connecting..."))
1466 apply(callback, (msg,))
1469 # wait until we are connected, because the user might have selected another server
1476 self.fill_addressbook()
1480 class Wallet_2of2(NewWallet):
1482 def __init__(self, storage):
1483 NewWallet.__init__(self, storage)
1484 self.storage.put('wallet_type', '2of2', True)
1486 def can_create_accounts(self):
1489 def create_account(self):
1490 xpub1 = self.master_public_keys.get("m/")
1491 xpub2 = self.master_public_keys.get("cold/")
1492 account = BIP32_Account_2of2({'xpub':xpub1, 'xpub2':xpub2})
1493 self.add_account('m/', account)
1495 def get_master_public_keys(self):
1496 xpub1 = self.master_public_keys.get("m/")
1497 xpub2 = self.master_public_keys.get("cold/")
1498 return {'hot':xpub1, 'cold':xpub2}
1500 def get_action(self):
1501 xpub1 = self.master_public_keys.get("m/")
1502 xpub2 = self.master_public_keys.get("cold/")
1504 return 'create_2of2_1'
1506 return 'create_2of2_2'
1510 class Wallet_2of3(Wallet_2of2):
1512 def __init__(self, storage):
1513 NewWallet.__init__(self, storage)
1514 self.storage.put('wallet_type', '2of3', True)
1516 def create_account(self):
1517 xpub1 = self.master_public_keys.get("m/")
1518 xpub2 = self.master_public_keys.get("cold/")
1519 xpub3 = self.master_public_keys.get("remote/")
1520 account = BIP32_Account_2of3({'xpub':xpub1, 'xpub2':xpub2, 'xpub3':xpub3})
1521 self.add_account('m/', account)
1523 def get_master_public_keys(self):
1524 xpub1 = self.master_public_keys.get("m/")
1525 xpub2 = self.master_public_keys.get("cold/")
1526 xpub3 = self.master_public_keys.get("remote/")
1527 return {'hot':xpub1, 'cold':xpub2, 'remote':xpub3}
1529 def get_action(self):
1530 xpub1 = self.master_public_keys.get("m/")
1531 xpub2 = self.master_public_keys.get("cold/")
1532 xpub3 = self.master_public_keys.get("remote/")
1534 return 'create_2of3_1'
1536 return 'create_2of3_2'
1538 return 'create_2of3_3'
1541 class WalletSynchronizer(threading.Thread):
1544 def __init__(self, wallet, network):
1545 threading.Thread.__init__(self)
1547 self.wallet = wallet
1548 self.network = network
1549 self.was_updated = True
1550 self.running = False
1551 self.lock = threading.Lock()
1552 self.queue = Queue.Queue()
1555 with self.lock: self.running = False
1557 def is_running(self):
1558 with self.lock: return self.running
1561 def subscribe_to_addresses(self, addresses):
1563 for addr in addresses:
1564 messages.append(('blockchain.address.subscribe', [addr]))
1565 self.network.subscribe( messages, lambda i,r: self.queue.put(r))
1572 while self.is_running():
1574 if not self.network.is_connected():
1575 self.network.wait_until_connected()
1577 self.run_interface()
1580 def run_interface(self):
1582 print_error("synchronizer: connected to", self.network.main_server())
1586 requested_histories = {}
1588 # request any missing transactions
1589 for history in self.wallet.history.values():
1590 if history == ['*']: continue
1591 for tx_hash, tx_height in history:
1592 if self.wallet.transactions.get(tx_hash) is None and (tx_hash, tx_height) not in missing_tx:
1593 missing_tx.append( (tx_hash, tx_height) )
1596 print_error("missing tx", missing_tx)
1599 self.subscribe_to_addresses(self.wallet.addresses(True))
1601 while self.is_running():
1602 # 1. create new addresses
1603 new_addresses = self.wallet.synchronize()
1605 # request missing addresses
1607 self.subscribe_to_addresses(new_addresses)
1609 # request missing transactions
1610 for tx_hash, tx_height in missing_tx:
1611 if (tx_hash, tx_height) not in requested_tx:
1612 self.network.send([ ('blockchain.transaction.get',[tx_hash, tx_height]) ], lambda i,r: self.queue.put(r))
1613 requested_tx.append( (tx_hash, tx_height) )
1616 # detect if situation has changed
1617 if self.network.is_up_to_date() and self.queue.empty():
1618 if not self.wallet.is_up_to_date():
1619 self.wallet.set_up_to_date(True)
1620 self.was_updated = True
1622 if self.wallet.is_up_to_date():
1623 self.wallet.set_up_to_date(False)
1624 self.was_updated = True
1626 if self.was_updated:
1627 self.network.trigger_callback('updated')
1628 self.was_updated = False
1632 r = self.queue.get(block=True, timeout=1)
1637 #if interface != self.network.interface:
1643 # 3. handle response
1644 method = r['method']
1645 params = r['params']
1646 result = r.get('result')
1647 error = r.get('error')
1652 if method == 'blockchain.address.subscribe':
1654 if self.wallet.get_status(self.wallet.get_history(addr)) != result:
1655 if requested_histories.get(addr) is None:
1656 self.network.send([('blockchain.address.get_history', [addr])], lambda i,r:self.queue.put(r))
1657 requested_histories[addr] = result
1659 elif method == 'blockchain.address.get_history':
1661 print_error("receiving history", addr, result)
1663 assert requested_histories.pop(addr) == '*'
1664 self.wallet.receive_history_callback(addr, result)
1667 # check that txids are unique
1670 tx_hash = item['tx_hash']
1671 if tx_hash not in txids:
1672 txids.append(tx_hash)
1673 hist.append( (tx_hash, item['height']) )
1675 if len(hist) != len(result):
1676 raise Exception("error: server sent history with non-unique txid", result)
1678 # check that the status corresponds to what was announced
1679 rs = requested_histories.pop(addr)
1680 if self.wallet.get_status(hist) != rs:
1681 raise Exception("error: status mismatch: %s"%addr)
1683 # store received history
1684 self.wallet.receive_history_callback(addr, hist)
1686 # request transactions that we don't have
1687 for tx_hash, tx_height in hist:
1688 if self.wallet.transactions.get(tx_hash) is None:
1689 if (tx_hash, tx_height) not in requested_tx and (tx_hash, tx_height) not in missing_tx:
1690 missing_tx.append( (tx_hash, tx_height) )
1692 elif method == 'blockchain.transaction.get':
1694 tx_height = params[1]
1695 assert tx_hash == hash_encode(Hash(result.decode('hex')))
1696 tx = Transaction(result)
1697 self.wallet.receive_tx_callback(tx_hash, tx, tx_height)
1698 self.was_updated = True
1699 requested_tx.remove( (tx_hash, tx_height) )
1700 print_error("received tx:", tx_hash, len(tx.raw))
1703 print_error("Error: Unknown message:" + method + ", " + repr(params) + ", " + repr(result) )
1705 if self.was_updated and not requested_tx:
1706 self.network.trigger_callback('updated')
1707 # Updated gets called too many times from other places as well; if we use that signal we get the notification three times
1708 self.network.trigger_callback("new_transaction")
1709 self.was_updated = False
1714 class OldWallet(NewWallet):
1716 def can_create_accounts(self):
1719 def make_seed(self):
1721 seed = random_seed(128)
1722 return ' '.join(mnemonic.mn_encode(seed))
1724 def prepare_seed(self, seed):
1726 # see if seed was entered as hex
1731 return OLD_SEED_VERSION, str(seed)
1735 words = seed.split()
1736 seed = mnemonic.mn_decode(words)
1738 raise Exception("Invalid seed")
1740 return OLD_SEED_VERSION, seed
1743 def create_master_keys(self, password):
1744 seed = pw_decode(self.seed, password)
1745 mpk = OldAccount.mpk_from_seed(seed)
1746 self.storage.put('master_public_key', mpk, True)
1748 def get_master_public_keys(self):
1749 mpk = self.storage.get("master_public_key")
1750 return {'Main Account':mpk}
1752 def create_accounts(self, password):
1753 mpk = self.storage.get("master_public_key")
1754 self.create_account(mpk)
1756 def create_account(self, mpk):
1757 self.accounts[0] = OldAccount({'mpk':mpk, 0:[], 1:[]})
1758 self.save_accounts()
1760 def create_watching_only_wallet(self, mpk):
1761 self.seed_version = OLD_SEED_VERSION
1762 self.storage.put('seed_version', self.seed_version, True)
1763 self.storage.put('master_public_key', mpk, True)
1764 self.create_account(mpk)
1766 def get_seed(self, password):
1767 seed = pw_decode(self.seed, password)
1768 self.accounts[0].check_seed(seed)
1771 def get_mnemonic(self, password):
1773 s = pw_decode(self.seed, password)
1774 return ' '.join(mnemonic.mn_encode(s))
1777 def add_keypairs_from_KeyID(self, tx, keypairs, password):
1778 # first check the provided password
1779 seed = self.get_seed(password)
1780 for txin in tx.inputs:
1781 keyid = txin.get('KeyID')
1783 m = re.match("old\(([0-9a-f]+),(\d+),(\d+)", keyid)
1786 if mpk != self.storage.get('master_public_key'): continue
1787 for_change = int(m.group(2))
1788 num = int(m.group(3))
1789 account = self.accounts[0]
1790 addr = account.get_address(for_change, num)
1791 txin['address'] = addr # fixme: side effect
1792 pk = account.get_private_key(seed, (for_change, num))
1793 pubkey = public_key_from_private_key(pk)
1794 keypairs[pubkey] = pk
1797 def get_account_name(self, k):
1799 return 'Main account'
1801 def is_seeded(self, account):
1802 return self.seed is not None
1804 def get_private_key(self, address, password):
1805 if self.is_watching_only():
1808 # first check the provided password
1809 seed = self.get_seed(password)
1812 if address in self.imported_keys.keys():
1813 out.append( pw_decode( self.imported_keys[address], password ) )
1815 account_id, sequence = self.get_address_index(address)
1816 pk = self.accounts[0].get_private_key(seed, sequence)
1820 def check_pending_accounts(self):
1824 # former WalletFactory
1825 class Wallet(object):
1827 def __new__(self, storage):
1828 config = storage.config
1829 if config.get('bitkey', False):
1830 # if user requested support for Bitkey device,
1831 # import Bitkey driver
1832 from wallet_bitkey import WalletBitkey
1833 return WalletBitkey(config)
1835 if storage.get('wallet_type') == '2of2':
1836 return Wallet_2of2(storage)
1838 if storage.get('wallet_type') == '2of3':
1839 return Wallet_2of3(storage)
1842 if not storage.file_exists:
1843 seed_version = NEW_SEED_VERSION if config.get('bip32') is True else OLD_SEED_VERSION
1845 seed_version = storage.get('seed_version')
1846 if not seed_version:
1847 seed_version = OLD_SEED_VERSION if len(storage.get('master_public_key')) == 128 else NEW_SEED_VERSION
1849 if seed_version == OLD_SEED_VERSION:
1850 return OldWallet(storage)
1851 elif seed_version == NEW_SEED_VERSION:
1852 return NewWallet(storage)
1854 msg = "This wallet seed is not supported."
1855 if seed_version in [5]:
1856 msg += "\nTo open this wallet, try 'git checkout seed_v%d'"%seed_version
1863 def is_seed(self, seed):
1866 elif is_old_seed(seed):
1868 elif is_new_seed(seed):
1874 def is_mpk(self, mpk):
1882 return len(mpk) == 128
1885 deserialize_xkey(mpk)
1892 def from_seed(self, seed, storage):
1893 if is_old_seed(seed):
1895 elif is_new_seed(seed):
1901 def from_mpk(self, mpk, storage):
1910 w = OldWallet(storage)
1912 w.create_watching_only_wallet(mpk)
1914 w = NewWallet(storage)
1915 w.create_watching_only_wallet(mpk)