From 057b67d78fa7fa53c96b580fa5142a1a7d92b230 Mon Sep 17 00:00:00 2001 From: schmanu Date: Mon, 23 Oct 2023 11:12:02 +0200 Subject: [PATCH] fix: review issues --- .../mpc/__tests__/useMPCWallet.test.ts | 7 ++----- src/hooks/wallets/mpc/useMPCWallet.ts | 19 +++++++++++-------- src/utils/addresses.ts | 6 +++++- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/hooks/wallets/mpc/__tests__/useMPCWallet.test.ts b/src/hooks/wallets/mpc/__tests__/useMPCWallet.test.ts index c607226ada..c84605a994 100644 --- a/src/hooks/wallets/mpc/__tests__/useMPCWallet.test.ts +++ b/src/hooks/wallets/mpc/__tests__/useMPCWallet.test.ts @@ -15,9 +15,8 @@ import { ONBOARD_MPC_MODULE_LABEL } from '@/services/mpc/module' import { ethers } from 'ethers' import BN from 'bn.js' import * as addressBookSlice from '@/store/addressBookSlice' -import * as chains from '@/hooks/useChains' +import * as useChainId from '@/hooks/useChainId' import { hexZeroPad } from 'ethers/lib/utils' -import { type ChainInfo } from '@safe-global/safe-gateway-typescript-sdk' import * as useAddressBook from '@/hooks/useAddressBook' /** time until mock login resolves */ @@ -87,9 +86,7 @@ describe('useMPCWallet', () => { beforeEach(() => { jest.resetAllMocks() setMPCCoreKitInstance(undefined) - jest - .spyOn(chains, 'useCurrentChain') - .mockReturnValue({ chainId: '100', disabledWallets: [] } as unknown as ChainInfo) + jest.spyOn(useChainId, 'default').mockReturnValue('100') }) afterAll(() => { jest.useRealTimers() diff --git a/src/hooks/wallets/mpc/useMPCWallet.ts b/src/hooks/wallets/mpc/useMPCWallet.ts index b1fc581a8c..466a02e632 100644 --- a/src/hooks/wallets/mpc/useMPCWallet.ts +++ b/src/hooks/wallets/mpc/useMPCWallet.ts @@ -10,10 +10,10 @@ import { DeviceShareRecovery } from './recovery/DeviceShareRecovery' import { trackEvent } from '@/services/analytics' import { MPC_WALLET_EVENTS } from '@/services/analytics/events/mpcWallet' import useAddressBook from '@/hooks/useAddressBook' -import { useCurrentChain } from '@/hooks/useChains' import { upsertAddressBookEntry } from '@/store/addressBookSlice' import { useAppDispatch } from '@/store' -import { ethers } from 'ethers' +import useChainId from '@/hooks/useChainId' +import { checksumAddress } from '@/utils/addresses' export enum MPCWalletState { NOT_INITIALIZED, @@ -36,7 +36,7 @@ export const useMPCWallet = (): MPCWalletHook => { const mpcCoreKit = useMPC() const onboard = useOnboard() const addressBook = useAddressBook() - const currentChainId = useCurrentChain() + const currentChainId = useChainId() const dispatch = useAppDispatch() const criticalResetAccount = async (): Promise => { @@ -114,12 +114,15 @@ export const useMPCWallet = (): MPCWalletHook => { }, }).catch((reason) => console.error('Error connecting to MPC module:', reason)) - // If the signer is not in the address book => add it as the user's first name + // If the signer is not in the address book => add the user's email as name if (wallets && currentChainId && wallets.length > 0) { - const signerAddress = ethers.utils.getAddress(wallets[0].accounts[0]?.address) - if (addressBook[signerAddress] === undefined) { - const email = mpcCoreKit.getUserInfo().email - dispatch(upsertAddressBookEntry({ address: signerAddress, chainId: currentChainId.chainId, name: email })) + const address = wallets[0].accounts[0]?.address + if (address) { + const signerAddress = checksumAddress(address) + if (addressBook[signerAddress] === undefined) { + const email = mpcCoreKit.getUserInfo().email + dispatch(upsertAddressBookEntry({ address: signerAddress, chainId: currentChainId, name: email })) + } } } setWalletState(MPCWalletState.READY) diff --git a/src/utils/addresses.ts b/src/utils/addresses.ts index e1c4bfce68..8718efbd22 100644 --- a/src/utils/addresses.ts +++ b/src/utils/addresses.ts @@ -1,6 +1,10 @@ import { getAddress } from 'ethers/lib/utils' import { isAddress } from '@ethersproject/address' - +/** + * Checksums the given address + * @param address ethereum address + * @returns the checksummed address if the given address is valid otherwise returns the address unchanged + */ export const checksumAddress = (address: string): string => { return isAddress(address) ? getAddress(address) : address }