-
Notifications
You must be signed in to change notification settings - Fork 465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: address book undefined entry address #1338
Changes from 2 commits
aca50fc
0d6089c
c940445
d646e80
4dfffd5
56e921e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,6 @@ | ||||||
import { createSelector, createSlice, type PayloadAction } from '@reduxjs/toolkit' | ||||||
import { ADDRESS_RE } from '@/utils/validation' | ||||||
import { pickBy } from 'lodash' | ||||||
import type { RootState } from '.' | ||||||
|
||||||
export type AddressBook = { [address: string]: string } | ||||||
|
@@ -45,6 +47,8 @@ export const selectAllAddressBooks = (state: RootState): AddressBookState => { | |||||
export const selectAddressBookByChain = createSelector( | ||||||
[selectAllAddressBooks, (_, chainId: string) => chainId], | ||||||
(allAddressBooks, chainId): AddressBook => { | ||||||
return chainId ? allAddressBooks[chainId] || {} : {} | ||||||
const chainAddresses = allAddressBooks[chainId] | ||||||
const validAddresses = pickBy(chainAddresses, (_, key) => ADDRESS_RE.test(key)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have a function to validate addresses. This additionally validates that addresses are checksummed, which we also expect in our addressbook.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will not validate the address if it isn't checksummed. I didn't use it because it was filtering out the non-checksummed addresses |
||||||
return chainId ? validAddresses || {} : {} | ||||||
}, | ||||||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,8 @@ import chains from '@/config/chains' | |
import { parsePrefixedAddress, sameAddress, isChecksummedAddress } from './addresses' | ||
import { safeFormatUnits, safeParseUnits } from './formatters' | ||
|
||
export const ADDRESS_RE = /^0x[0-9a-f]{40}$/i | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd move it back into validateAddress because that is the function which always should be used IMO. If we need a helper that returns a boolean we should add something like export const isValidAddress = (address: string) => {
return validateAddress(address) === undefined
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then we should probably split
Initially I was using that function but it would filter out any address not checksummed and I think we don't want to go that strict in the AB, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think we can use the validation as it is -> returning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We checksum every address which gets added to the address book. And if its incorrectly checksummed it will not let you add that address. So I think we should go quite strict. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we validate them when added to the AB, and in address inputs, validating also in the selector seems like an overkill. And it's computationally not the fastest. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've included the checksum validation. Reason being an unchecksummed address might be introduced during a development stage, the same way the corrupted AB with an undefined entry was. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I didn't experience any degradation of performance but to be fair my AB is not extensively populated.
katspaugh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
export const validateAddress = (address: string) => { | ||
const ADDRESS_RE = /^0x[0-9a-f]{40}$/i | ||
|
||
if (!ADDRESS_RE.test(address)) { | ||
return 'Invalid address format' | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kudos for adding a test :)