forked from trustwallet/assets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheth-address.ts
More file actions
42 lines (35 loc) · 1.38 KB
/
eth-address.ts
File metadata and controls
42 lines (35 loc) · 1.38 KB
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
42
import { toChecksumAddress, checkAddressChecksum } from "ethereum-checksum-address";
import { reverseCase } from "./types";
export const isChecksumEthereum = (address: string): boolean => checkAddressChecksum(address);
export const toChecksumEthereum = (address: string): string => toChecksumAddress(address);
export function toChecksum(address: string, chain = "ethereum"): string {
const checksumEthereum = toChecksumEthereum(address);
// special handling for Wanchain
if (chain.toLowerCase() === "wanchain") {
const checksumWanchain = reverseCase(checksumEthereum).replace("X", "x");
return checksumWanchain;
}
return checksumEthereum;
}
export function isChecksum(address: string, chain = "ethereum"): boolean {
// special handling for Wanchain
if (chain.toLowerCase() === "wanchain") {
const addressEthereum = reverseCase(address).replace("X", "x");
return isChecksumEthereum(addressEthereum);
}
return isChecksumEthereum(address);
}
export function isEthereumAddress(address: string): boolean {
if (!(address.length == 40 || (address.length == 42 && address.substring(0, 2) === '0x'))) {
return false;
}
try {
const check1 = toChecksum(address);
if (toChecksum(check1) !== check1) {
return false;
}
return true;
} catch (error) {
return false;
}
}