Skip to content

Commit

Permalink
fix: review issues
Browse files Browse the repository at this point in the history
  • Loading branch information
schmanu committed Oct 23, 2023
1 parent 99bad90 commit 057b67d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
7 changes: 2 additions & 5 deletions src/hooks/wallets/mpc/__tests__/useMPCWallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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()
Expand Down
19 changes: 11 additions & 8 deletions src/hooks/wallets/mpc/useMPCWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<void> => {
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion src/utils/addresses.ts
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down

0 comments on commit 057b67d

Please sign in to comment.