def restore_accounts(self): '''Restore into the global namespace all the account objects represented in the wallet. ''' self.open_unlock() account_map = manager.account_map() new_map = {} wallet_keys = cleos.WalletKeys(is_verbose=0) if len(account_map) > 0: logger.INFO(''' ######### Restore cached account objects: ''') for name, object_name in account_map.items(): try: account_ = cleos.GetAccount(name, is_info=False, is_verbose=False) if account_.owner_key in wallet_keys.json and \ account_.active_key in wallet_keys.json: new_map[name] = object_name from eosfactory.shell.account import create_account create_account(object_name, name, restore=True) except errors.AccountDoesNotExistError: pass manager.save_account_map(new_map) else: logger.INFO(''' * The wallet is empty. ''')
def keys_in_wallets(self, keys): self.open_unlock() result = cleos.WalletKeys(is_verbose=False) for key in keys: if not key in result.json: return False return True
def remove_key(self, account_or_key): ''' ''' self.open_unlock() removed_keys = [] account_name = None if isinstance(account_or_key, interface.Account): cleos.WalletRemove_key(interface.key_arg(account_or_key, is_owner_key=True, is_private_key=True), self.name, is_verbose=False) removed_keys.append( interface.key_arg(account_or_key, is_owner_key=True, is_private_key=False)) cleos.WalletRemove_key(interface.key_arg(account_or_key, is_owner_key=False, is_private_key=True), self.name, is_verbose=False) removed_keys.append( interface.key_arg(account_or_key, is_owner_key=False, is_private_key=False)) else: cleos.WalletRemove_key(interface.key_arg(account_or_key, is_private_key=True), self.name, is_verbose=False) removed_keys.append( interface.key_arg(account_or_key, is_private_key=False)) if account_name is None: if len(removed_keys) > 0: logger.TRACE( ''' Removing key '{}' from the wallet '{}' '''.format(removed_keys[0], self.name), verbosity) else: logger.TRACE(''' Removing keys of the account '{}' from the wallet '{}' '''.format(account_name, self.name)) wallet_keys = cleos.WalletKeys(is_verbose=False) for key in removed_keys: if key in wallet_keys.json: raise errors.Error(''' Failed to remove key '{}' from the wallet '{}' '''.format(key, self.name)) logger.TRACE(''' * Cross-checked: all listed keys removed from the wallet. ''') return True
def keys(self): ''' Lists public keys from all unlocked wallets. Returns `cleos.WalletKeys` object. ''' self.open_unlock() wallet_keys = cleos.WalletKeys(is_verbose=False) logger.TRACE(''' Keys in all open walets: {} '''.format(wallet_keys.out_msg)) return wallet_keys
def keys_in_wallets(self, keys): '''Check whether all listed keys are in the wallet. Args: keys ([str]): List of public keys to be verified. Returns: bool: Whether all listed keys are in the wallet. ''' self.open_unlock() result = cleos.WalletKeys(is_verbose=False) for key in keys: if not key in result.json: return False return True
def import_key(self, account_or_key): ''' Imports private keys into wallet. Return list of `cleos.WalletImport` objects Args: account_or_key (str or .interface.Key or .interface.Account): A private key to import. If *account_or_key* is an .interface.Account object, both owner and active keys are imported. ''' self.open_unlock() imported_keys = [] account_name = None if isinstance(account_or_key, interface.Account): account_name = account_or_key.name cleos.WalletImport(interface.key_arg(account_or_key, is_owner_key=True, is_private_key=True), self.name, is_verbose=False) imported_keys.append( interface.key_arg(account_or_key, is_owner_key=True, is_private_key=False)) cleos.WalletImport(interface.key_arg(account_or_key, is_owner_key=False, is_private_key=True), self.name, is_verbose=False) imported_keys.append( interface.key_arg(account_or_key, is_owner_key=False, is_private_key=False)) logger.TRACE(''' * Importing keys of the account ``{}`` into the wallet ``{}`` '''.format(account_name, self.name)) else: cleos.WalletImport(interface.key_arg(account_or_key, is_private_key=True), self.name, is_verbose=False) logger.TRACE(''' * Importing keys into the wallet ``{}`` '''.format(self.name)) return True wallet_keys = cleos.WalletKeys(is_verbose=False) if len(imported_keys) == 0: raise errors.Error(''' The list of imported keys is empty. ''') ok = True for key in imported_keys: if not key in wallet_keys.json: ok = False raise errors.Error(''' Failed to import keys of the account '{}' into the wallet '{}' '''.format(account_name if account_name else "n/a", self.name)) if ok: logger.TRACE(''' * Cross-checked: all account keys are in the wallet. ''') return True