X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=lib%2Fwallet.py;h=df3efa8c27cbee27c74f963c918b67469998e8e7;hb=a195ca5c07f5c04314537b29cd77e8d14c5c0f9c;hp=48aa098f655e928123b3f62756b39737c0743d66;hpb=2e1d24939ccb46123974f3338963a65191883981;p=electrum-nvc.git diff --git a/lib/wallet.py b/lib/wallet.py index 48aa098..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,12 +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 @@ -151,47 +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, 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) + # imported_keys is deprecated. The GUI should call convert_imported_keys + self.imported_keys = self.storage.get('imported_keys',{}) + 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() @@ -199,6 +175,22 @@ class Abstract_Wallet: for tx_hash, tx in self.transactions.items(): self.update_tx_outputs(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 h = tx.hash() @@ -207,8 +199,8 @@ class Abstract_Wallet: tx.add_pubkey_addresses(self.transactions) # outputs of tx: inputs of tx2 - for x, v in tx.outputs: - if x.startswith('pubkey:'): + for type, x, v in tx.outputs: + if type == 'pubkey': for tx2 in self.transactions.values(): tx2.add_pubkey_addresses({h:tx}) @@ -227,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(): @@ -340,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) @@ -362,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. @@ -410,19 +380,15 @@ class Abstract_Wallet: 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) @@ -472,11 +438,6 @@ class Abstract_Wallet: 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) @@ -648,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): @@ -772,11 +733,12 @@ 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: - if address.startswith('OP_RETURN:'): + for type, address, x in outputs: + if type == 'op_return': continue - assert is_address(address), "Address " + address + " is invalid!" - amount = sum( map(lambda x:x[1], outputs) ) + 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") @@ -854,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) @@ -1058,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): @@ -1251,7 +1215,7 @@ class Deterministic_Wallet(Abstract_Wallet): return False prev_addresses = prev_addresses[max(0, i - limit):] for addr in prev_addresses: - if self.address_is_old(addr): + if self.history.get(addr): return False return True @@ -1266,6 +1230,8 @@ 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'"] @@ -1277,11 +1243,8 @@ class NewWallet(Deterministic_Wallet): return 'm/' in self.master_private_keys.keys() def get_master_public_key(self): - if self.is_watching_only(): - return self.master_public_keys["m/0'"] - else: - 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 = {} @@ -1350,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 = [] @@ -1551,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): @@ -1562,8 +1547,8 @@ class Wallet(object): config = storage.config self.wallet_types = [ - ('standard', ("Standard wallet"), OldWallet), - ('imported', ("Imported wallet"), Imported_Wallet), + ('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) ]