-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: generally handle addresses in lowercase representation (#35)
- BREAKING CHANGE: `formatPrefixedAddress` renamed to `prefixAddress` - BREAKING CHANGE: `parsePrefixedAddress` renamed to `unprefixAddress` - BREAKING CHANGE: prefixed addresses are all lowercase now resolves #31
- Loading branch information
1 parent
946b81b
commit 53299bb
Showing
21 changed files
with
239 additions
and
204 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,54 @@ | ||
import { Address, getAddress, isAddress, zeroAddress } from 'viem' | ||
import { getAddress, isAddress, zeroAddress } from 'viem' | ||
import { chains } from './chains' | ||
import type { ChainId, PrefixedAddress } from './types' | ||
|
||
export const formatPrefixedAddress = ( | ||
import type { Address, ChainId, PrefixedAddress } from './types' | ||
|
||
export function prefixAddress( | ||
chainId: ChainId | undefined, | ||
address: Address | ||
) => { | ||
): PrefixedAddress { | ||
const chain = chains.find((chain) => chain.chainId === chainId) | ||
|
||
if (chainId && !chain) { | ||
throw new Error(`Unsupported chainId: ${chainId}`) | ||
} | ||
|
||
if (!isAddress(address)) { | ||
throw new Error(`Not an Address: "${address}"`) | ||
} | ||
|
||
const prefix = chain ? chain.shortName : 'eoa' | ||
return `${prefix}:${getAddress(address)}` as PrefixedAddress | ||
return `${prefix}:${getAddress(address).toLowerCase()}` as PrefixedAddress | ||
} | ||
|
||
export const splitPrefixedAddress = ( | ||
export function unprefixAddress( | ||
prefixedAddress: PrefixedAddress | Address | ||
): [ChainId | undefined, Address] => { | ||
): Address { | ||
const [, address] = splitPrefixedAddress(prefixedAddress) | ||
return address | ||
} | ||
|
||
export function splitPrefixedAddress( | ||
prefixedAddress: PrefixedAddress | Address | ||
): [ChainId | undefined, Address] { | ||
// without prefix | ||
if (prefixedAddress.length == zeroAddress.length) { | ||
if (!isAddress(prefixedAddress)) { | ||
throw new Error(`Not an Address: ${prefixedAddress}`) | ||
} | ||
return [undefined, getAddress(prefixedAddress)] | ||
} else { | ||
if (prefixedAddress.indexOf(':') == -1) { | ||
throw new Error(`Unsupported PrefixedAddress format: ${prefixedAddress}`) | ||
} | ||
const [prefix, address] = prefixedAddress.split(':') | ||
const chain = chains.find(({ shortName }) => shortName === prefix) | ||
if (prefix && prefix != 'eoa' && !chain) { | ||
throw new Error(`Unsupported chain shortName: ${prefix}`) | ||
} | ||
return [undefined, getAddress(prefixedAddress).toLowerCase() as Address] | ||
} | ||
|
||
return [chain?.chainId, getAddress(address)] as const | ||
if (prefixedAddress.indexOf(':') == -1) { | ||
throw new Error(`Unsupported PrefixedAddress format: ${prefixedAddress}`) | ||
} | ||
} | ||
|
||
export const parsePrefixedAddress = ( | ||
prefixedAddress: PrefixedAddress | Address | ||
): Address => { | ||
const [, address] = splitPrefixedAddress(prefixedAddress) | ||
return address | ||
// with prefix | ||
const [prefix, address] = prefixedAddress.split(':') | ||
const chain = chains.find(({ shortName }) => shortName === prefix) | ||
if (prefix && prefix != 'eoa' && !chain) { | ||
throw new Error(`Unsupported chain shortName: ${prefix}`) | ||
} | ||
|
||
return [chain?.chainId, getAddress(address).toLowerCase() as Address] as const | ||
} |
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
Oops, something went wrong.