-
Notifications
You must be signed in to change notification settings - Fork 0
/
addresses.ts
41 lines (35 loc) · 1.24 KB
/
addresses.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { Address, getAddress } from 'viem'
import { chains } from './chains'
import type { ChainId, PrefixedAddress } from './types'
export const formatPrefixedAddress = (
chainId: ChainId | undefined,
address: Address
) => {
const chain = chainId && chains.find((chain) => chain.chainId === chainId)
if (!chain && chainId) {
throw new Error(`Unsupported chain ID: ${chainId}`)
}
const prefix = chain ? chain.shortName : 'eoa'
return `${prefix}:${getAddress(address)}` as PrefixedAddress
}
export const splitPrefixedAddress = (prefixedAddress: PrefixedAddress) => {
const [prefix, address] = prefixedAddress.split(':')
const chain =
prefix !== 'eoa'
? chains.find(({ shortName }) => shortName === prefix)
: undefined
if (!chain && prefix !== 'eoa') {
throw new Error(`Unknown chain prefix: ${prefix}`)
}
const checksummedAddress = getAddress(address) as `0x${string}`
return [chain?.chainId, checksummedAddress] as const
}
export const parsePrefixedAddress = (
prefixedAddress: PrefixedAddress | Address
) => {
if (!prefixedAddress.includes(':')) {
return getAddress(prefixedAddress) as `0x${string}`
}
const [, address] = prefixedAddress.split(':')
return getAddress(address) as `0x${string}`
}