From 6e5c54b44794178f9d6fb5cbe8acfb3bdd05186b Mon Sep 17 00:00:00 2001 From: tecnovert Date: Fri, 18 Nov 2022 23:31:52 +0200 Subject: [PATCH] ui: Send locked status to templates. --- basicswap/basicswap.py | 38 +++++++++++-- basicswap/http_server.py | 4 ++ basicswap/interface/btc.py | 6 ++ basicswap/js_server.py | 6 +- basicswap/templates/changepassword.html | 73 +++++++++++++++++++++++-- basicswap/templates/header.html | 26 ++++----- basicswap/ui/page_encryption.py | 14 ++--- 7 files changed, 129 insertions(+), 38 deletions(-) diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index 4ad95a0..4e0dbcb 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -246,6 +246,8 @@ class BasicSwap(BaseApp): self._keep_notifications = self.settings.get('keep_notifications', 50) self._show_notifications = self.settings.get('show_notifications', 10) self._notifications_cache = {} + self._is_encrypted = None + self._is_locked = None # TODO: Adjust ranges self.min_delay_event = self.settings.get('min_delay_event', 10) @@ -743,7 +745,7 @@ class BasicSwap(BaseApp): if self.coin_clients[c]['connection_type'] == 'rpc': yield c - def changeWalletPasswords(self, old_password, new_password): + def changeWalletPasswords(self, old_password, new_password, coin=None): # Only the main wallet password is changed for monero, avoid issues by preventing until active swaps are complete if len(self.swaps_in_progress) > 0: raise ValueError('Can\'t change passwords while swaps are in progress') @@ -751,8 +753,13 @@ class BasicSwap(BaseApp): if old_password == new_password: raise ValueError('Passwords must differ') - # Unlock all wallets to ensure they all have the same password. + if len(new_password) < 4: + raise ValueError('New password is too short') + + # Unlock wallets to ensure they all have the same password. for c in self.activeCoins(): + if coin and c != coin: + continue ci = self.ci(c) try: ci.unlockWallet(old_password) @@ -760,15 +767,29 @@ class BasicSwap(BaseApp): raise ValueError('Failed to unlock {}'.format(ci.coin_name())) for c in self.activeCoins(): + if coin and c != coin: + continue self.ci(c).changeWalletPassword(old_password, new_password) - def unlockWallets(self, password): - for c in self.activeCoins(): - self.ci(c).unlockWallet(password) + # Update cached state + if coin is None or coin == Coins.PART: + self._is_encrypted, self._is_locked = self.ci(Coins.PART).isWalletEncryptedLocked() - def lockWallets(self): + def unlockWallets(self, password, coin=None): for c in self.activeCoins(): + if coin and c != coin: + continue + self.ci(c).unlockWallet(password) + if c == Coins.PART: + self._is_locked = False + + def lockWallets(self, coin=None): + for c in self.activeCoins(): + if coin and c != coin: + continue self.ci(c).lockWallet() + if c == Coins.PART: + self._is_locked = True def initialiseWallet(self, coin_type, raise_errors=False): if coin_type == Coins.PART: @@ -6059,6 +6080,11 @@ class BasicSwap(BaseApp): return {'Error': 'Not Initialised'} return self._network.get_info() + def getLockedState(self): + if self._is_encrypted is None or self._is_locked is None: + self._is_encrypted, self._is_locked = self.ci(Coins.PART).isWalletEncryptedLocked() + return self._is_encrypted, self._is_locked + def lookupRates(self, coin_from, coin_to, output_array=False): self.log.debug('lookupRates {}, {}'.format(coin_from, coin_to)) diff --git a/basicswap/http_server.py b/basicswap/http_server.py index 9500f4f..0616e1f 100644 --- a/basicswap/http_server.py +++ b/basicswap/http_server.py @@ -150,6 +150,10 @@ class HttpHandler(BaseHTTPRequestHandler): self.server.session_tokens['shutdown'] = shutdown_token args_dict['shutdown_token'] = shutdown_token + encrypted, locked = swap_client.getLockedState() + args_dict['encrypted'] = encrypted + args_dict['locked'] = locked + if self.server.msg_id_counter >= 0x7FFFFFFF: self.server.msg_id_counter = 0 diff --git a/basicswap/interface/btc.py b/basicswap/interface/btc.py index ef0b749..6206f85 100644 --- a/basicswap/interface/btc.py +++ b/basicswap/interface/btc.py @@ -1302,6 +1302,12 @@ class BTCInterface(CoinInterface): return True return False + def isWalletEncryptedLocked(self): + wallet_info = self.rpc_callback('getwalletinfo') + encrypted = 'unlocked_until' in wallet_info + locked = encrypted and wallet_info['unlocked_until'] <= 0 + return encrypted, locked + def changeWalletPassword(self, old_password, new_password): self._log.info('changeWalletPassword - {}'.format(self.ticker())) if old_password == '': diff --git a/basicswap/js_server.py b/basicswap/js_server.py index 7104aec..fbff5dc 100644 --- a/basicswap/js_server.py +++ b/basicswap/js_server.py @@ -454,7 +454,7 @@ def js_setpassword(self, url_split, post_string, is_json): coin = getCoinType(get_data_entry(post_data, 'coin')) if coin in (Coins.PART_ANON, Coins.PART_BLIND): raise ValueError('Invalid coin.') - swap_client.ci(coin).changeWalletPassword(old_password, new_password) + swap_client.changeWalletPasswords(old_password, new_password, coin) return bytes(json.dumps({'success': True}), 'UTF-8') # Set password for all coins @@ -472,7 +472,7 @@ def js_unlock(self, url_split, post_string, is_json): coin = getCoinType(str(get_data_entry(post_data, 'coin'))) if coin in (Coins.PART_ANON, Coins.PART_BLIND): raise ValueError('Invalid coin.') - swap_client.ci(coin).unlockWallet(password) + swap_client.unlockWallets(password, coin) return bytes(json.dumps({'success': True}), 'UTF-8') swap_client.unlockWallets(password) @@ -487,7 +487,7 @@ def js_lock(self, url_split, post_string, is_json): coin = getCoinType(get_data_entry(post_data, 'coin')) if coin in (Coins.PART_ANON, Coins.PART_BLIND): raise ValueError('Invalid coin.') - swap_client.ci(coin).lockWallet() + swap_client.lockWallets(coin) return bytes(json.dumps({'success': True}), 'UTF-8') swap_client.lockWallets() diff --git a/basicswap/templates/changepassword.html b/basicswap/templates/changepassword.html index 786e2c3..e46773e 100644 --- a/basicswap/templates/changepassword.html +++ b/basicswap/templates/changepassword.html @@ -23,7 +23,7 @@ - +
  • Change Password
  • @@ -70,19 +70,55 @@ Old Password - +
    +
    + + +
    + +
    New Password - +
    +
    + + +
    + +
    Confirm Password - +
    +
    + + +
    + +
    @@ -111,5 +147,32 @@ {% include 'footer.html' %} + - \ No newline at end of file + diff --git a/basicswap/templates/header.html b/basicswap/templates/header.html index ab20a02..a2bec1b 100644 --- a/basicswap/templates/header.html +++ b/basicswap/templates/header.html @@ -290,11 +290,10 @@ {% endif %} - - {% if debug_mode == true %} - - + + {% endif %} + {% endif %} {% if use_tor_proxy == true %} @@ -650,7 +648,7 @@ - + {% if ws_url %} - {% endif %} \ No newline at end of file + {% endif %} diff --git a/basicswap/ui/page_encryption.py b/basicswap/ui/page_encryption.py index 1cf051d..55d2c58 100644 --- a/basicswap/ui/page_encryption.py +++ b/basicswap/ui/page_encryption.py @@ -76,13 +76,7 @@ def page_lock(self, url_split, post_string): swap_client.checkSystemStatus() swap_client.lockWallets() - - messages = [] - err_messages = [] - - template = server.env.get_template('info.html') - return self.render_template(template, { - 'messages': messages, - 'err_messages': err_messages, - 'message_str': 'Wallets locked' - }) + self.send_response(302) + self.send_header('Location', '/') + self.end_headers() + return bytes()