X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=lib%2Fwallet.py;h=df3efa8c27cbee27c74f963c918b67469998e8e7;hb=a195ca5c07f5c04314537b29cd77e8d14c5c0f9c;hp=eb9a03954c3f5e8f92ce7ce6c6f9e1aa2d7f9e06;hpb=0c440ee6a6dfc246d943b90ebcf70874cec50e7f;p=electrum-nvc.git diff --git a/lib/wallet.py b/lib/wallet.py index eb9a039..df3efa8 100644 --- a/lib/wallet.py +++ b/lib/wallet.py @@ -43,11 +43,10 @@ DUST_THRESHOLD = 5430 IMPORTED_ACCOUNT = '/x' - -class WalletStorage: +class WalletStorage(object): def __init__(self, config): - self.lock = threading.Lock() + self.lock = threading.RLock() self.config = config self.data = {} self.file_exists = False @@ -56,7 +55,6 @@ class WalletStorage: if self.path: self.read(self.path) - def init_path(self, config): """Set the path of the wallet.""" @@ -84,7 +82,6 @@ class WalletStorage: return new_path - def read(self, path): """Read the contents of the wallet file.""" try: @@ -100,12 +97,13 @@ class WalletStorage: self.data = d self.file_exists = True - def get(self, key, default=None): - v = self.data.get(key) - if v is None: - v = default - return v + + with self.lock: + v = self.data.get(key) + if v is None: + v = default + return v def put(self, key, value, save = True): @@ -127,8 +125,11 @@ class WalletStorage: os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE) -class Abstract_Wallet: - +class Abstract_Wallet(object): + """ + Wallet classes are created to handle various address generation methods. + Completion states (watching-only, single account, no seed, etc) are handled inside classes. + """ def __init__(self, storage): self.storage = storage self.electrum_version = ELECTRUM_VERSION @@ -147,46 +148,26 @@ class Abstract_Wallet: self.fee = int(storage.get('fee_per_kb', 10000)) - self.master_public_keys = storage.get('master_public_keys',{}) - self.master_private_keys = storage.get('master_private_keys', {}) - self.next_addresses = storage.get('next_addresses',{}) - # This attribute is set when wallet.start_threads is called. self.synchronizer = None - self.load_accounts() - - self.transactions = {} - tx_list = self.storage.get('transactions',{}) - for k,v in tx_list.items(): - try: - tx = Transaction(v) - except Exception: - print_msg("Warning: Cannot deserialize transactions. skipping") - continue - - self.add_extra_addresses(tx) - self.transactions[k] = tx + # imported_keys is deprecated. The GUI should call convert_imported_keys + self.imported_keys = self.storage.get('imported_keys',{}) - for h,tx in self.transactions.items(): - if not self.check_new_tx(h, tx): - print_error("removing unreferenced tx", h) - self.transactions.pop(h) + self.load_accounts() + self.load_transactions() # not saved self.prevout_values = {} # my own transaction outputs self.spent_outputs = [] - # spv self.verifier = None - # there is a difference between wallet.up_to_date and interface.is_up_to_date() # interface.is_up_to_date() returns true when all requests have been answered and processed # wallet.up_to_date is true when the wallet is synchronized (stronger requirement) - self.up_to_date = False self.lock = threading.Lock() self.transaction_lock = threading.Lock() @@ -194,14 +175,34 @@ class Abstract_Wallet: for tx_hash, tx in self.transactions.items(): self.update_tx_outputs(tx_hash) - def add_extra_addresses(self, tx): - h = tx.hash() + def load_transactions(self): + self.transactions = {} + tx_list = self.storage.get('transactions',{}) + for k, raw in tx_list.items(): + try: + tx = Transaction.deserialize(raw) + except Exception: + print_msg("Warning: Cannot deserialize transactions. skipping") + continue + self.add_pubkey_addresses(tx) + self.transactions[k] = tx + for h,tx in self.transactions.items(): + if not self.check_new_tx(h, tx): + print_error("removing unreferenced tx", h) + self.transactions.pop(h) + + def add_pubkey_addresses(self, tx): # find the address corresponding to pay-to-pubkey inputs - tx.add_extra_addresses(self.transactions) - for o in tx.d.get('outputs'): - if o.get('is_pubkey'): + h = tx.hash() + + # inputs + tx.add_pubkey_addresses(self.transactions) + + # outputs of tx: inputs of tx2 + for type, x, v in tx.outputs: + if type == 'pubkey': for tx2 in self.transactions.values(): - tx2.add_extra_addresses({h:tx}) + tx2.add_pubkey_addresses({h:tx}) def get_action(self): pass @@ -218,7 +219,6 @@ class Abstract_Wallet: def load_accounts(self): self.accounts = {} - self.imported_keys = self.storage.get('imported_keys',{}) d = self.storage.get('accounts', {}) for k, v in d.items(): @@ -331,7 +331,6 @@ class Abstract_Wallet: return s[0] == 1 def get_address_index(self, address): - for account in self.accounts.keys(): for for_change in [0,1]: addresses = self.accounts[account].get_addresses(for_change) @@ -345,12 +344,6 @@ class Abstract_Wallet: raise Exception("Address not found", address) - def getpubkeys(self, addr): - assert is_valid(addr) and self.is_mine(addr) - account, sequence = self.get_address_index(addr) - a = self.accounts[account] - return a.get_pubkeys( sequence ) - def get_private_key(self, address, password): if self.is_watching_only(): return [] @@ -359,27 +352,7 @@ class Abstract_Wallet: def get_public_keys(self, address): account_id, sequence = self.get_address_index(address) - return self.accounts[account_id].get_pubkeys(sequence) - - def can_sign(self, tx): - - if self.is_watching_only(): - return False - - if tx.is_complete(): - return False - - addr_list, xpub_list = tx.inputs_to_sign() - for addr in addr_list: - if self.is_mine(addr): - return True - - mpk = [ self.master_public_keys[k] for k in self.master_private_keys.keys() ] - for xpub, sequence in xpub_list: - if xpub in mpk: - return True - - return False + return self.accounts[account_id].get_pubkeys(*sequence) def add_keypairs(self, tx, keypairs, password): # first check the provided password. This will raise if invalid. @@ -400,28 +373,22 @@ class Abstract_Wallet: break else: continue - - addr = account.get_address(*sequence) - pk = self.get_private_key(addr, password) + pk = account.get_private_key(sequence, self, password) for sec in pk: pubkey = public_key_from_private_key(sec) keypairs[pubkey] = sec def signrawtransaction(self, tx, private_keys, password): # check that the password is correct. This will raise if it's not. - self.get_seed(password) - + self.check_password(password) # build a list of public/private keys keypairs = {} - # add private keys from parameter for sec in private_keys: pubkey = public_key_from_private_key(sec) keypairs[ pubkey ] = sec - # add private_keys self.add_keypairs(tx, keypairs, password) - # sign the transaction self.sign_transaction(tx, keypairs, password) @@ -459,7 +426,7 @@ class Abstract_Wallet: for tx_hash, tx in self.transactions.items(): is_relevant, is_send, _, _ = self.get_tx_value(tx) if is_send: - for addr, v in tx.outputs: + for addr in tx.get_output_addresses(): if not self.is_mine(addr) and addr not in self.addressbook: self.addressbook.append(addr) # redo labels @@ -468,14 +435,9 @@ class Abstract_Wallet: def get_num_tx(self, address): n = 0 for tx in self.transactions.values(): - if address in map(lambda x:x[0], tx.outputs): n += 1 + if address in tx.get_output_addresses(): n += 1 return n - def get_address_flags(self, addr): - flags = "C" if self.is_change(addr) else "I" if addr in self.imported_keys.keys() else "-" - flags += "F" if addr in self.frozen_addresses else "-" - return flags - def get_tx_value(self, tx, account=None): domain = self.get_account_addresses(account) return tx.get_value(domain, self.prevout_values) @@ -483,7 +445,7 @@ class Abstract_Wallet: def update_tx_outputs(self, tx_hash): tx = self.transactions.get(tx_hash) - for i, (addr, value) in enumerate(tx.outputs): + for i, (addr, value) in enumerate(tx.get_outputs()): key = tx_hash+ ':%d'%i self.prevout_values[key] = value @@ -503,7 +465,7 @@ class Abstract_Wallet: tx = self.transactions.get(tx_hash) if not tx: continue - for i, (addr, value) in enumerate(tx.outputs): + for i, (addr, value) in enumerate(tx.get_outputs()): if addr == address: key = tx_hash + ':%d'%i received_coins.append(key) @@ -521,7 +483,7 @@ class Abstract_Wallet: if key in received_coins: v -= value - for i, (addr, value) in enumerate(tx.outputs): + for i, (addr, value) in enumerate(tx.get_outputs()): key = tx_hash + ':%d'%i if addr == address: v += value @@ -543,7 +505,7 @@ class Abstract_Wallet: def get_account_addresses(self, a, include_change=True): if a is None: - o = self.addresses(True) + o = self.addresses(include_change) elif a in self.accounts: ac = self.accounts[a] o = ac.get_addresses(0) @@ -575,10 +537,10 @@ class Abstract_Wallet: tx = self.transactions.get(tx_hash) if tx is None: raise Exception("Wallet not synchronized") is_coinbase = tx.inputs[0].get('prevout_hash') == '0'*64 - for o in tx.d.get('outputs'): - output = o.copy() - if output.get('address') != addr: continue - key = tx_hash + ":%d" % output.get('prevout_n') + for i, (address, value) in enumerate(tx.get_outputs()): + output = {'address':address, 'value':value, 'prevout_n':i} + if address != addr: continue + key = tx_hash + ":%d"%i if key in self.spent_outputs: continue output['prevout_hash'] = tx_hash output['height'] = tx_height @@ -647,7 +609,7 @@ class Abstract_Wallet: # Insert the change output at a random position in the outputs posn = random.randint(0, len(outputs)) - outputs[posn:posn] = [( change_addr, change_amount)] + outputs[posn:posn] = [( 'address', change_addr, change_amount)] return outputs def get_history(self, address): @@ -665,7 +627,7 @@ class Abstract_Wallet: def receive_tx_callback(self, tx_hash, tx, tx_height): with self.transaction_lock: - self.add_extra_addresses(tx) + self.add_pubkey_addresses(tx) if not self.check_new_tx(tx_hash, tx): # may happen due to pruning print_error("received transaction that is no longer referenced in history", tx_hash) @@ -742,8 +704,7 @@ class Abstract_Wallet: if tx: is_relevant, is_mine, _, _ = self.get_tx_value(tx) if is_mine: - for o in tx.outputs: - o_addr, _ = o + for o_addr in tx.get_output_addresses(): if not self.is_mine(o_addr): try: default_label = self.labels[o_addr] @@ -753,13 +714,11 @@ class Abstract_Wallet: else: default_label = '(internal)' else: - for o in tx.outputs: - o_addr, _ = o + for o_addr in tx.get_output_addresses(): if self.is_mine(o_addr) and not self.is_change(o_addr): break else: - for o in tx.outputs: - o_addr, _ = o + for o_addr in tx.get_output_addresses(): if self.is_mine(o_addr): break else: @@ -774,16 +733,19 @@ class Abstract_Wallet: return default_label def make_unsigned_transaction(self, outputs, fee=None, change_addr=None, domain=None, coins=None ): - for address, x in outputs: - assert is_valid(address), "Address " + address + " is invalid!" - amount = sum( map(lambda x:x[1], outputs) ) + for type, address, x in outputs: + if type == 'op_return': + continue + if type == 'address': + assert is_address(address), "Address " + address + " is invalid!" + amount = sum( map(lambda x:x[2], outputs) ) inputs, total, fee = self.choose_tx_inputs( amount, fee, len(outputs), domain, coins ) if not inputs: raise ValueError("Not enough funds") for txin in inputs: self.add_input_info(txin) outputs = self.add_tx_change(inputs, outputs, amount, fee, total, change_addr) - return Transaction.from_io(inputs, outputs) + return Transaction(inputs, outputs) def mktx(self, outputs, password, fee=None, change_addr=None, domain= None, coins = None ): tx = self.make_unsigned_transaction(outputs, fee, change_addr, domain, coins) @@ -797,9 +759,13 @@ class Abstract_Wallet: address = txin['address'] account_id, sequence = self.get_address_index(address) account = self.accounts[account_id] - redeemScript = account.redeem_script(sequence) - txin['x_pubkeys'] = account.get_xpubkeys(sequence) - txin['pubkeys'] = pubkeys = account.get_pubkeys(sequence) + redeemScript = account.redeem_script(*sequence) + pubkeys = account.get_pubkeys(*sequence) + x_pubkeys = account.get_xpubkeys(*sequence) + # sort pubkeys and x_pubkeys, using the order of pubkeys + pubkeys, x_pubkeys = zip( *sorted(zip(pubkeys, x_pubkeys))) + txin['pubkeys'] = list(pubkeys) + txin['x_pubkeys'] = list(x_pubkeys) txin['signatures'] = [None] * len(pubkeys) if redeemScript: @@ -850,11 +816,12 @@ class Abstract_Wallet: imported_account.update_password(old_password, new_password) self.save_accounts() - for k, v in self.master_private_keys.items(): - b = pw_decode(v, old_password) - c = pw_encode(b, new_password) - self.master_private_keys[k] = c - self.storage.put('master_private_keys', self.master_private_keys, True) + if hasattr(self, 'master_private_keys'): + for k, v in self.master_private_keys.items(): + b = pw_decode(v, old_password) + c = pw_encode(b, new_password) + self.master_private_keys[k] = c + self.storage.put('master_private_keys', self.master_private_keys, True) self.use_encryption = (new_password != None) self.storage.put('use_encryption', self.use_encryption,True) @@ -928,7 +895,7 @@ class Abstract_Wallet: print_error("new history is orphaning transaction:", tx_hash) # check that all outputs are not mine, request histories ext_requests = [] - for _addr, _v in tx.outputs: + for _addr in tx.get_output_addresses(): # assert not self.is_mine(_addr) ext_requests.append( ('blockchain.address.get_history', [_addr]) ) @@ -1054,6 +1021,7 @@ class Imported_Wallet(Abstract_Wallet): def is_beyond_limit(self, address, account, is_change): return False + class Deterministic_Wallet(Abstract_Wallet): def __init__(self, storage): @@ -1135,25 +1103,27 @@ class Deterministic_Wallet(Abstract_Wallet): if n > nmax: nmax = n return nmax + 1 + def create_new_address(self, account=None, for_change=0): + if account is None: + account = self.default_account() + address = account.create_new_address(for_change) + self.history[address] = [] + if self.synchronizer: + self.synchronizer.add(address) + self.save_accounts() + return address + def synchronize_sequence(self, account, for_change): limit = self.gap_limit_for_change if for_change else self.gap_limit - new_addresses = [] while True: addresses = account.get_addresses(for_change) if len(addresses) < limit: - address = account.create_new_address(for_change) - self.history[address] = [] - new_addresses.append( address ) + self.create_new_address(account, for_change) continue - if map( lambda a: self.address_is_old(a), addresses[-limit:] ) == limit*[False]: break else: - address = account.create_new_address(for_change) - self.history[address] = [] - new_addresses.append( address ) - - return new_addresses + self.create_new_address(account, for_change) def check_pending_accounts(self): for account_id, addr in self.next_addresses.items(): @@ -1165,22 +1135,15 @@ class Deterministic_Wallet(Abstract_Wallet): self.next_addresses.pop(account_id) def synchronize_account(self, account): - new = [] - new += self.synchronize_sequence(account, 0) - new += self.synchronize_sequence(account, 1) - return new + self.synchronize_sequence(account, 0) + self.synchronize_sequence(account, 1) def synchronize(self): self.check_pending_accounts() - new = [] for account in self.accounts.values(): if type(account) in [ImportedAccount, PendingAccount]: continue - new += self.synchronize_account(account) - if new: - self.save_accounts() - self.storage.put('addr_history', self.history, True) - return new + self.synchronize_account(account) def restore(self, callback): from i18n import _ @@ -1252,22 +1215,36 @@ class Deterministic_Wallet(Abstract_Wallet): return False prev_addresses = prev_addresses[max(0, i - limit):] for addr in prev_addresses: - num, is_used = self.is_used(addr) - if num > 0: + if self.history.get(addr): return False return True + def get_action(self): + if not self.get_master_public_key(): + return 'create_seed' + if not self.accounts: + return 'create_accounts' + class NewWallet(Deterministic_Wallet): def __init__(self, storage): Deterministic_Wallet.__init__(self, storage) + self.master_public_keys = storage.get('master_public_keys', {}) + self.master_private_keys = storage.get('master_private_keys', {}) + + def default_account(self): + return self.accounts["m/0'"] + + def is_watching_only(self): + return not bool(self.master_private_keys) def can_create_accounts(self): - return not self.is_watching_only() + return 'm/' in self.master_private_keys.keys() def get_master_public_key(self): - return self.master_public_keys["m/"] + """xpub of the main account""" + return self.master_public_keys.get("m/0'") def get_master_public_keys(self): out = {} @@ -1288,19 +1265,30 @@ class NewWallet(Deterministic_Wallet): xpub = self.master_public_keys["m/"] assert deserialize_xkey(xpriv)[3] == deserialize_xkey(xpub)[3] - def create_watching_only_wallet(self, xpub): + def create_xprv_wallet(self, xprv, password): + xpub = bitcoin.xpub_from_xprv(xprv) + account = BIP32_Account({'xpub':xpub}) + account_id = 'm/' + bitcoin.get_xkey_name(xpub) self.storage.put('seed_version', self.seed_version, True) - self.add_master_public_key("m/", xpub) + self.add_master_private_key(account_id, xprv, password) + self.add_master_public_key(account_id, xpub) + self.add_account(account_id, account) + + def create_watching_only_wallet(self, xpub): account = BIP32_Account({'xpub':xpub}) - self.add_account("m/", account) + account_id = 'm/' + bitcoin.get_xkey_name(xpub) + self.storage.put('seed_version', self.seed_version, True) + self.add_master_public_key(account_id, xpub) + self.add_account(account_id, account) def create_accounts(self, password): # First check the password is valid (this raises if it isn't). - self.check_password(password) + if not self.is_watching_only(): + self.check_password(password) self.create_account('Main account', password) - def add_master_public_key(self, name, mpk): - self.master_public_keys[name] = mpk + def add_master_public_key(self, name, xpub): + self.master_public_keys[name] = xpub self.storage.put('master_public_keys', self.master_public_keys, True) def add_master_private_key(self, name, xpriv, password): @@ -1325,11 +1313,20 @@ class NewWallet(Deterministic_Wallet): self.add_master_public_key("m/", xpub) self.add_master_private_key("m/", xpriv, password) - def find_root_by_master_key(self, xpub): - for key, xpub2 in self.master_public_keys.items(): - if key == "m/":continue - if xpub == xpub2: - return key + def can_sign(self, tx): + if self.is_watching_only(): + return False + if tx.is_complete(): + return False + addr_list, xpub_list = tx.inputs_to_sign() + for addr in addr_list: + if self.is_mine(addr): + return True + mpk = [ self.master_public_keys[k] for k in self.master_private_keys.keys() ] + for xpub, sequence in xpub_list: + if xpub in mpk: + return True + return False def num_accounts(self): keys = [] @@ -1394,13 +1391,16 @@ class Wallet_2of2(NewWallet): NewWallet.__init__(self, storage) self.storage.put('wallet_type', '2of2', True) + def default_account(self): + return self.accounts['m/'] + def can_create_accounts(self): return False def can_import(self): return False - def create_account(self): + def create_account(self, name, password): xpub1 = self.master_public_keys.get("m/") xpub2 = self.master_public_keys.get("cold/") account = BIP32_Account_2of2({'xpub':xpub1, 'xpub2':xpub2}) @@ -1415,9 +1415,11 @@ class Wallet_2of2(NewWallet): xpub1 = self.master_public_keys.get("m/") xpub2 = self.master_public_keys.get("cold/") if xpub1 is None: - return 'create_2of2_1' + return 'create_seed' if xpub2 is None: - return 'create_2of2_2' + return 'add_cosigner' + if not self.accounts: + return 'create_accounts' class Wallet_2of3(Wallet_2of2): @@ -1427,7 +1429,7 @@ class Wallet_2of3(Wallet_2of2): Wallet_2of2.__init__(self, storage) self.storage.put('wallet_type', '2of3', True) - def create_account(self): + def create_account(self, name, password): xpub1 = self.master_public_keys.get("m/") xpub2 = self.master_public_keys.get("cold/") xpub3 = self.master_public_keys.get("remote/") @@ -1444,17 +1446,19 @@ class Wallet_2of3(Wallet_2of2): xpub1 = self.master_public_keys.get("m/") xpub2 = self.master_public_keys.get("cold/") xpub3 = self.master_public_keys.get("remote/") - # fixme: we use order of creation - if xpub2 and xpub1 is None: - return 'create_2fa_2' if xpub1 is None: - return 'create_2of3_1' + return 'create_seed' if xpub2 is None or xpub3 is None: - return 'create_2of3_2' + return 'add_two_cosigners' + if not self.accounts: + return 'create_accounts' class OldWallet(Deterministic_Wallet): + def default_account(self): + return self.accounts[0] + def make_seed(self): import mnemonic seed = random_seed(128) @@ -1519,6 +1523,19 @@ class OldWallet(Deterministic_Wallet): def check_pending_accounts(self): pass + def can_sign(self, tx): + if self.is_watching_only(): + return False + if tx.is_complete(): + return False + addr_list, xpub_list = tx.inputs_to_sign() + for addr in addr_list: + if self.is_mine(addr): + return True + for xpub, sequence in xpub_list: + if xpub == self.master_public_key: + return True + return False # former WalletFactory class Wallet(object): @@ -1528,20 +1545,18 @@ class Wallet(object): def __new__(self, storage): config = storage.config - if config.get('bitkey', False): - # if user requested support for Bitkey device, - # import Bitkey driver - from wallet_bitkey import WalletBitkey - return WalletBitkey(config) - if storage.get('wallet_type') == '2of2': - return Wallet_2of2(storage) + self.wallet_types = [ + ('standard', ("Standard wallet"), NewWallet if config.get('bip32') else OldWallet), + ('imported', ("Imported wallet"), Imported_Wallet), + ('2of2', ("Multisig wallet (2 of 2)"), Wallet_2of2), + ('2of3', ("Multisig wallet (2 of 3)"), Wallet_2of3) + ] + run_hook('add_wallet_types', self.wallet_types) - if storage.get('wallet_type') == '2of3': - return Wallet_2of3(storage) - - if storage.get('wallet_type') == 'imported': - return Imported_Wallet(storage) + for t, l, WalletClass in self.wallet_types: + if t == storage.get('wallet_type'): + return WalletClass(storage) if not storage.file_exists: seed_version = NEW_SEED_VERSION if config.get('bip32') is True else OLD_SEED_VERSION @@ -1573,21 +1588,31 @@ class Wallet(object): return False @classmethod - def is_mpk(self, mpk): + def is_old_mpk(self, mpk): try: int(mpk, 16) - old = True + assert len(mpk) == 128 + return True except: - old = False + return False - if old: - return len(mpk) == 128 - else: - try: - deserialize_xkey(mpk) - return True - except: - return False + @classmethod + def is_xpub(self, text): + try: + assert text[0:4] == 'xpub' + deserialize_xkey(text) + return True + except: + return False + + @classmethod + def is_xprv(self, text): + try: + assert text[0:4] == 'xprv' + deserialize_xkey(text) + return True + except: + return False @classmethod def is_address(self, text): @@ -1632,19 +1657,20 @@ class Wallet(object): return w @classmethod - def from_mpk(self, mpk, storage): - try: - int(mpk, 16) - old = True - except: - old = False + def from_old_mpk(self, mpk, storage): + w = OldWallet(storage) + w.seed = '' + w.create_watching_only_wallet(mpk) + return w - if old: - w = OldWallet(storage) - w.seed = '' - w.create_watching_only_wallet(mpk) - else: - w = NewWallet(storage) - w.create_watching_only_wallet(mpk) + @classmethod + def from_xpub(self, xpub, storage): + w = NewWallet(storage) + w.create_watching_only_wallet(xpub) + return w + @classmethod + def from_xprv(self, xprv, password, storage): + w = NewWallet(storage) + w.create_xprv_wallet(xprv, password) return w