-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #233 from xmtp/ar/address-lowercase
feat: Added support for handling lowercase addresses
- Loading branch information
Showing
7 changed files
with
139 additions
and
10 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
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
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
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
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
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,50 @@ | ||
import { keccak_256 } from '@noble/hashes/sha3' | ||
import { TextEncoder } from 'text-encoding' | ||
|
||
const addressRegex = /^0x[a-fA-F0-9]{40}$/ | ||
const encoder = new TextEncoder() | ||
|
||
export function stringToBytes(value: string): Uint8Array { | ||
const bytes = encoder.encode(value) | ||
return bytes | ||
} | ||
|
||
export function keccak256(value: Uint8Array): Uint8Array { | ||
const bytes = keccak_256(value) | ||
return bytes | ||
} | ||
|
||
export function isAddress(address: string): boolean { | ||
return addressRegex.test(address) | ||
} | ||
|
||
export function checksumAddress(address_: string, chainId?: number): string { | ||
const hexAddress = chainId | ||
? `${chainId}${address_.toLowerCase()}` | ||
: address_.substring(2).toLowerCase() | ||
const hash = keccak256(stringToBytes(hexAddress)) | ||
|
||
const address = ( | ||
chainId ? hexAddress.substring(`${chainId}0x`.length) : hexAddress | ||
).split('') | ||
for (let i = 0; i < 40; i += 2) { | ||
if (hash[i >> 1] >> 4 >= 8 && address[i]) { | ||
address[i] = address[i].toUpperCase() | ||
} | ||
if ((hash[i >> 1] & 0x0f) >= 8 && address[i + 1]) { | ||
address[i + 1] = address[i + 1].toUpperCase() | ||
} | ||
} | ||
|
||
return `0x${address.join('')}` | ||
} | ||
|
||
const addressCache = new Map<string, string>() | ||
|
||
export function getAddress(address: string, chainId?: number): string { | ||
if (addressCache.has(address)) return addressCache.get(address) as string | ||
if (!isAddress(address)) throw new Error('Invalid address' + address) | ||
const checksumedAddress = checksumAddress(address, chainId) | ||
addressCache.set(address, checksumedAddress) | ||
return checksumedAddress | ||
} |
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