From d30aa0ce83e8d01f7739a70d4085c0377b014f43 Mon Sep 17 00:00:00 2001 From: Vit Stanislav Date: Fri, 10 Nov 2017 11:16:25 +0100 Subject: [PATCH] Handle invalid accounts in localStorage --- src/utils/savedAccounts.js | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/utils/savedAccounts.js b/src/utils/savedAccounts.js index 013401f7e..5d01f0e32 100644 --- a/src/utils/savedAccounts.js +++ b/src/utils/savedAccounts.js @@ -1,12 +1,22 @@ +import { validateUrl } from './login'; +import { extractAddress } from './api/account'; + +const isValidSavedAccount = ({ publicKey, network, address }) => { + try { + return extractAddress(publicKey) && + network >= 0 && network <= 2 && + (validateUrl(address).addressValidity === '' || network !== 2); + } catch (e) { + return false; + } +}; + export const getSavedAccounts = () => { - const savedAccounts = localStorage.getItem('accounts'); - if (savedAccounts) { - const accounts = JSON.parse(savedAccounts); - if (accounts instanceof Array) { - return accounts; - } + try { + return JSON.parse(localStorage.getItem('accounts')).filter(isValidSavedAccount); + } catch (e) { + return []; } - return []; }; export const getLastActiveAccount = () => (