Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #469 from hermeznetwork/fix-auth-storage
Browse files Browse the repository at this point in the history
Fix CreateAccountsAuth localStorage struct
  • Loading branch information
elias-garcia authored May 6, 2021
2 parents 35e9855 + b2ce56c commit 43801bc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/store/login/login.reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ function loginReducer (state = initialLoginState, action) {
...state.accountAuthSignatures,
[action.chainId]: {
...chainIdAuthSignatures,
[action.hermezEthereumAddress]: [action.signature]
[action.hermezEthereumAddress]: action.signature
}
}
}
Expand Down
25 changes: 15 additions & 10 deletions src/store/login/login.thunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,7 +17,7 @@ function CreateAccountAuth ({
onCreateAccountAuthorization(wallet)
}, [wallet, onCreateAccountAuthorization])

if (hermezAddressAuthSignatures.length === 0) {
if (!hermezAddressAuthSignature) {
return (
<div className={classes.accountAuth}>
<h2 className={classes.accountAuthTitle}>Create accounts for new tokens</h2>
Expand Down
10 changes: 3 additions & 7 deletions src/views/login/login.view.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 (
<CreateAccountAuth
hermezAddressAuthSignatures={hermezAddressAuthSignatures}
hermezAddressAuthSignature={hermezAddressAuthSignature}
steps={steps}
onCreateAccountAuthorization={onCreateAccountAuthorization}
/>
Expand Down

0 comments on commit 43801bc

Please sign in to comment.