-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
377 additions
and
163 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
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,22 @@ | ||
import { z } from 'zod' | ||
import { isAddress } from './evm.util' | ||
|
||
/** | ||
* Schema backward compatible with viem's Address type. | ||
* | ||
* @see https://viem.sh/docs/glossary/types#address | ||
*/ | ||
export const addressSchema = z.custom<`0x${string}`>( | ||
(value) => { | ||
const parse = z.string().safeParse(value) | ||
|
||
if (parse.success) { | ||
return isAddress(parse.data) | ||
} | ||
|
||
return false | ||
}, | ||
{ | ||
message: 'value is an invalid Ethereum 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,47 @@ | ||
// eslint-disable-next-line no-restricted-imports | ||
import { InvalidAddressError, getAddress as viemGetAddress, isAddress as viemIsAddress } from 'viem' | ||
|
||
type Address = `0x${string}` | ||
|
||
/** | ||
* Checks if a string is a valid Ethereum address without regard of its format. | ||
* | ||
* @param address - The string to be checked. | ||
* @returns Returns true if the string is a valid Ethereum address, otherwise | ||
* returns false. | ||
*/ | ||
export const isAddress = (address: string): boolean => { | ||
if (!/^(0x)?[0-9a-fA-F]{40}$/.test(address)) { | ||
return false | ||
} else if (/^(0x)?[0-9a-f]{40}$/.test(address) || /^(0x)?[0-9A-F]{40}$/.test(address)) { | ||
return true | ||
} else { | ||
return viemIsAddress(address) | ||
} | ||
} | ||
|
||
/** | ||
* Retrieves the Ethereum address from a given string representation without | ||
* regard of its format. | ||
* | ||
* @param address - The string representation of the Ethereum address. | ||
* @param options - Optional parameters for address retrieval. | ||
* @param options.checksum - Specifies whether the retrieved address should be | ||
* checksummed. | ||
* @param options.chainId - The chain ID to be used for address retrieval. | ||
* @returns The Ethereum address. | ||
* @throws {InvalidAddressError} if the provided address is invalid. | ||
*/ | ||
export const getAddress = (address: string, options?: { checksum?: boolean; chainId?: number }): Address => { | ||
if (isAddress(address)) { | ||
const validAddress = address as Address | ||
|
||
if (options?.checksum || options?.chainId) { | ||
return viemGetAddress(validAddress, options.chainId) | ||
} | ||
|
||
return validAddress | ||
} | ||
|
||
throw new InvalidAddressError({ address }) | ||
} |
Oops, something went wrong.