on master help sign in
bridge/src/lib/utils/address.ts
34 lines 1.0 kB history raw
1import { isAddress, toChecksumAddress } from 'web3-utils';
2import { pubToAddress } from '@ethereumjs/util';
3import { ETH_ZERO_ADDR, ETH_ZERO_ADDR_SHORT } from 'lib/constants';
5export const stripHexPrefix = (hex: string) => {
6 return hex.startsWith('0x') ? hex.slice(2) : hex;
7};
9export const addHexPrefix = (hex: string) => {
10 return hex.startsWith('0x') ? hex : `0x${hex}`;
11};
13export const isValidAddress = (a: string | boolean | undefined) => {
14 if (!a || typeof a !== 'string') {
15 return;
16 }
18 return '0x0' === a || isAddress(a);
19};
21export const abbreviateAddress = (address: string) =>
22 `${address.slice(0, 6)}…${address.slice(-4)}`;
24export const isZeroAddress = (a: string) =>
25 a === ETH_ZERO_ADDR || a === ETH_ZERO_ADDR_SHORT;
27export const eqAddr = (addr0: string, addr1: string) =>
28 !addr0 || !addr1
29 ? false
30 : toChecksumAddress(addr0) === toChecksumAddress(addr1);
32export const publicToAddress = (publicKey: Buffer): string => {
33 return '0x' + pubToAddress(publicKey, true).toString('hex');
34};