From b2ce56c8d7aa92e2e5a5566b877971ab8e93f44f Mon Sep 17 00:00:00 2001 From: Elias Date: Thu, 6 May 2021 15:22:55 +0200 Subject: [PATCH] Fix CreateAccountsAuth localStorage struct --- src/store/login/login.reducer.js | 2 +- src/store/login/login.thunks.js | 25 +++++++++++-------- .../create-account-auth.view.jsx | 4 +-- src/views/login/login.view.jsx | 10 +++----- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/store/login/login.reducer.js b/src/store/login/login.reducer.js index 28c622b8f..fdb371ed6 100644 --- a/src/store/login/login.reducer.js +++ b/src/store/login/login.reducer.js @@ -189,7 +189,7 @@ function loginReducer (state = initialLoginState, action) { ...state.accountAuthSignatures, [action.chainId]: { ...chainIdAuthSignatures, - [action.hermezEthereumAddress]: [action.signature] + [action.hermezEthereumAddress]: action.signature } } } diff --git a/src/store/login/login.thunks.js b/src/store/login/login.thunks.js index 1e0255bc8..4b53977d8 100644 --- a/src/store/login/login.thunks.js +++ b/src/store/login/login.thunks.js @@ -8,7 +8,6 @@ import { ACCOUNT_AUTH_SIGNATURES_KEY, TREZOR_MANIFEST_MAIL } from '../../constan import { buildEthereumBIP44Path } from '../../utils/hw-wallets' import { STEP_NAME } from './login.reducer' import { WalletName } from '../../views/login/login.view' -import * as storage from '../../utils/storage' import { HttpStatusCode } from '../../utils/http' async function getSignerData (provider, walletName, accountData) { @@ -110,14 +109,11 @@ function postCreateAccountAuthorization (wallet) { global: { redirectRoute, ethereumNetworkTask, nextForgers } } = getState() - const hermezAddressAuthSignatures = storage.getItemsByHermezAddress( - accountAuthSignatures, - ethereumNetworkTask.data.chainId, - wallet.hermezEthereumAddress - ) - const getSignature = hermezAddressAuthSignatures.length === 0 - ? wallet.signCreateAccountAuthorization.bind(wallet) - : () => Promise.resolve(hermezAddressAuthSignatures[0]) + const chainIdSignatures = accountAuthSignatures[ethereumNetworkTask.data.chainId] || {} + const currentSignature = chainIdSignatures[wallet.hermezEthereumAddress] + const getSignature = currentSignature + ? () => Promise.resolve(currentSignature) + : wallet.signCreateAccountAuthorization.bind(wallet) getSignature() .then((signature) => { @@ -163,7 +159,16 @@ function setAccountAuthSignature (hermezEthereumAddress, signature) { const { global: { ethereumNetworkTask } } = getState() const { data: { chainId } } = ethereumNetworkTask - storage.addItem(ACCOUNT_AUTH_SIGNATURES_KEY, chainId, hermezEthereumAddress, signature) + const storage = JSON.parse(localStorage.getItem(ACCOUNT_AUTH_SIGNATURES_KEY)) + const chainIdStorage = storage[chainId] || {} + const newAccountAuthSignature = { + ...storage, + [chainId]: { + ...chainIdStorage, + [hermezEthereumAddress]: signature + } + } + localStorage.setItem(ACCOUNT_AUTH_SIGNATURES_KEY, JSON.stringify(newAccountAuthSignature)) dispatch(loginActions.setAccountAuthSignature(chainId, hermezEthereumAddress, signature)) } } diff --git a/src/views/login/components/create-account-auth/create-account-auth.view.jsx b/src/views/login/components/create-account-auth/create-account-auth.view.jsx index 192cfc0d5..1bfdd8b13 100644 --- a/src/views/login/components/create-account-auth/create-account-auth.view.jsx +++ b/src/views/login/components/create-account-auth/create-account-auth.view.jsx @@ -5,7 +5,7 @@ import Spinner from '../../../shared/spinner/spinner.view' import { STEP_NAME } from '../../../../store/login/login.reducer' function CreateAccountAuth ({ - hermezAddressAuthSignatures, + hermezAddressAuthSignature, addAccountAuthTask, steps, onCreateAccountAuthorization @@ -17,7 +17,7 @@ function CreateAccountAuth ({ onCreateAccountAuthorization(wallet) }, [wallet, onCreateAccountAuthorization]) - if (hermezAddressAuthSignatures.length === 0) { + if (!hermezAddressAuthSignature) { return (

Create accounts for new tokens

diff --git a/src/views/login/login.view.jsx b/src/views/login/login.view.jsx index 8422e1cdc..41a524f57 100644 --- a/src/views/login/login.view.jsx +++ b/src/views/login/login.view.jsx @@ -21,7 +21,6 @@ import ChainIdError from './components/chain-id-error/chain-id-error.view' import MetaMaskError from './components/metamask-error/metamask-error.view' import { PRIVACY_POLICY_URL, TERMS_OF_SERVICE_URL } from '../../constants' import UnderMaintenanceError from './components/under-maintenance-error/under-maintenance-error.view' -import * as storage from '../../utils/storage' export const WalletName = { METAMASK: 'metaMask', @@ -158,15 +157,12 @@ function Login ({ ) } case STEP_NAME.CREATE_ACCOUNT_AUTH: { - const hermezAddressAuthSignatures = storage.getItemsByHermezAddress( - accountAuthSignatures, - ethereumNetworkTask.data.chainId, - stepData.wallet.hermezEthereumAddress - ) + const chainIdSignatures = accountAuthSignatures[ethereumNetworkTask.data.chainId] || {} + const hermezAddressAuthSignature = chainIdSignatures[stepData.wallet.hermezEthereumAddress] return (