-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(multichain): normalize address helpers (#25022)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** New helpers to normalize address that can also be used in a non-EVM context. [](https://codespaces.new/MetaMask/metamask-extension/pull/25022?quickstart=1) ## **Related issues** None ## **Manual testing steps** None ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [x] I’ve followed [MetaMask Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I’ve included tests if applicable - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.
- Loading branch information
Showing
13 changed files
with
348 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { | ||
isEthAddress, | ||
normalizeAddress, | ||
normalizeSafeAddress, | ||
} from './address'; | ||
|
||
type TestAddress = { | ||
address: string; | ||
normalizedAddress: string; | ||
checksumAddress: string; | ||
}; | ||
|
||
const ETH_ADDRESSES = [ | ||
// Lower-case address | ||
{ | ||
address: '0x6431726eee67570bf6f0cf892ae0a3988f03903f', | ||
normalizedAddress: '0x6431726eee67570bf6f0cf892ae0a3988f03903f', | ||
checksumAddress: '0x6431726EEE67570BF6f0Cf892aE0a3988F03903F', | ||
}, | ||
// Checksum address | ||
{ | ||
address: '0x6431726EEE67570BF6f0Cf892aE0a3988F03903F', | ||
normalizedAddress: '0x6431726eee67570bf6f0cf892ae0a3988f03903f', | ||
checksumAddress: '0x6431726EEE67570BF6f0Cf892aE0a3988F03903F', | ||
}, | ||
]; | ||
|
||
const NON_EVM_ADDRESSES = [ | ||
{ | ||
address: '0xdeadbeef', | ||
}, | ||
{ | ||
address: 'bc1ql49ydapnjafl5t2cp9zqpjwe6pdgmxy98859v2', | ||
}, | ||
]; | ||
|
||
describe('address', () => { | ||
describe('isEthAddress', () => { | ||
// @ts-expect-error This is missing from the Mocha type definitions | ||
it.each(ETH_ADDRESSES)( | ||
'returns true if address is an ethereum address: $address', | ||
({ address }: TestAddress) => { | ||
expect(isEthAddress(address)).toBe(true); | ||
expect(isEthAddress(address.toLowerCase())).toBe(true); | ||
}, | ||
); | ||
|
||
// @ts-expect-error This is missing from the Mocha type definitions | ||
it.each(NON_EVM_ADDRESSES)( | ||
'returns false if address is not an ethereum address: $address', | ||
({ address }: TestAddress) => { | ||
expect(isEthAddress(address)).toBe(false); | ||
}, | ||
); | ||
}); | ||
|
||
describe('normalizeAddress', () => { | ||
// @ts-expect-error This is missing from the Mocha type definitions | ||
it.each(ETH_ADDRESSES)( | ||
'normalizes address: $address', | ||
({ address, normalizedAddress }: TestAddress) => { | ||
expect(normalizeAddress(address)).toBe(normalizedAddress); | ||
expect(normalizeAddress(address.toLowerCase())).toBe(normalizedAddress); | ||
}, | ||
); | ||
|
||
// @ts-expect-error This is missing from the Mocha type definitions | ||
it.each(NON_EVM_ADDRESSES)( | ||
'returns the original address if its a non-EVM address', | ||
({ address }: TestAddress) => { | ||
expect(normalizeAddress(address)).toBe(address); | ||
}, | ||
); | ||
}); | ||
|
||
describe('normalizeSafeAddress', () => { | ||
// @ts-expect-error This is missing from the Mocha type definitions | ||
it.each(ETH_ADDRESSES)( | ||
'normalizes address to its "safe" form: $address to: $checksumAddress', | ||
({ address, checksumAddress }: TestAddress) => { | ||
expect(normalizeSafeAddress(address)).toBe(checksumAddress); | ||
expect(normalizeSafeAddress(address.toLowerCase())).toBe( | ||
checksumAddress, | ||
); | ||
}, | ||
); | ||
|
||
// @ts-expect-error This is missing from the Mocha type definitions | ||
it.each(NON_EVM_ADDRESSES)( | ||
'returns the original address if its a non-EVM address', | ||
({ address }: TestAddress) => { | ||
expect(normalizeSafeAddress(address)).toBe(address); | ||
}, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Hex, isValidHexAddress } from '@metamask/utils'; | ||
import { normalize as normalizeEthAddress } from '@metamask/eth-sig-util'; | ||
import { toChecksumHexAddress } from '../../../../shared/modules/hexstring-utils'; | ||
|
||
/** | ||
* Checks if an address is an ethereum one. | ||
* | ||
* @param address - An address. | ||
* @returns True if the address is an ethereum one, false otherwise. | ||
*/ | ||
export function isEthAddress(address: string): boolean { | ||
return isValidHexAddress(address as Hex); | ||
} | ||
|
||
/** | ||
* Normalize an address. The address might be returned as-is, if there's no normalizer available. | ||
* | ||
* @param address - An address to normalize. | ||
* @returns The normalized address. | ||
*/ | ||
export function normalizeAddress(address: string): string { | ||
// NOTE: We assume that the overhead over checking the address format | ||
// at runtime is small | ||
return isEthAddress(address) | ||
? (normalizeEthAddress(address) as string) | ||
: address; | ||
} | ||
|
||
/** | ||
* Normalize an address to a "safer" representation. The address might be returned as-is, if | ||
* there's no normalizer available. | ||
* | ||
* @param address - An address to normalize. | ||
* @returns The "safer" normalized address. | ||
*/ | ||
export function normalizeSafeAddress(address: string): string { | ||
// NOTE: We assume that the overhead over checking the address format | ||
// at runtime is small | ||
return isEthAddress(address) ? toChecksumHexAddress(address) : address; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.