2020-09-29 13:01:34 +00:00
|
|
|
import { toChecksumAddress, checkAddressChecksum } from "ethereum-checksum-address";
|
2020-08-10 14:56:02 +00:00
|
|
|
import { reverseCase } from "./types";
|
|
|
|
|
2020-09-29 13:01:34 +00:00
|
|
|
export const isChecksumEthereum = (address: string): boolean => checkAddressChecksum(address);
|
|
|
|
export const toChecksumEthereum = (address: string): string => toChecksumAddress(address);
|
2020-08-10 14:56:02 +00:00
|
|
|
|
2020-09-18 14:39:31 +00:00
|
|
|
export function toChecksum(address: string, chain = "ethereum"): string {
|
|
|
|
const checksumEthereum = toChecksumEthereum(address);
|
2020-08-10 14:56:02 +00:00
|
|
|
|
|
|
|
// special handling for Wanchain
|
|
|
|
if (chain.toLowerCase() === "wanchain") {
|
|
|
|
const checksumWanchain = reverseCase(checksumEthereum).replace("X", "x");
|
|
|
|
return checksumWanchain;
|
|
|
|
}
|
|
|
|
|
|
|
|
return checksumEthereum;
|
|
|
|
}
|
|
|
|
|
2020-09-18 14:39:31 +00:00
|
|
|
export function isChecksum(address: string, chain = "ethereum"): boolean {
|
2020-08-10 14:56:02 +00:00
|
|
|
// special handling for Wanchain
|
|
|
|
if (chain.toLowerCase() === "wanchain") {
|
|
|
|
const addressEthereum = reverseCase(address).replace("X", "x");
|
|
|
|
return isChecksumEthereum(addressEthereum);
|
|
|
|
}
|
|
|
|
|
|
|
|
return isChecksumEthereum(address);
|
|
|
|
}
|
|
|
|
|
2020-08-31 21:33:20 +00:00
|
|
|
export function isEthereumAddress(address: string): boolean {
|
2020-09-29 13:01:34 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-08-31 21:33:20 +00:00
|
|
|
}
|