diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-12-08 18:58:33 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-12-11 19:21:44 +0800 |
commit | f1e7ea118b0c8c9a9a013b09d0e63bdd3694b21a (patch) | |
tree | 4816997b033912fd517a77dc2149bf0efac395a3 /packages/assert/src/address_utils.ts | |
parent | a6f97181310c9acba176b2a28ffba1ca7d53727d (diff) | |
download | dexon-0x-contracts-f1e7ea118b0c8c9a9a013b09d0e63bdd3694b21a.tar dexon-0x-contracts-f1e7ea118b0c8c9a9a013b09d0e63bdd3694b21a.tar.gz dexon-0x-contracts-f1e7ea118b0c8c9a9a013b09d0e63bdd3694b21a.tar.bz2 dexon-0x-contracts-f1e7ea118b0c8c9a9a013b09d0e63bdd3694b21a.tar.lz dexon-0x-contracts-f1e7ea118b0c8c9a9a013b09d0e63bdd3694b21a.tar.xz dexon-0x-contracts-f1e7ea118b0c8c9a9a013b09d0e63bdd3694b21a.tar.zst dexon-0x-contracts-f1e7ea118b0c8c9a9a013b09d0e63bdd3694b21a.zip |
Remove ethereum-address dependency and add this logic to the repo
Diffstat (limited to 'packages/assert/src/address_utils.ts')
-rw-r--r-- | packages/assert/src/address_utils.ts | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/packages/assert/src/address_utils.ts b/packages/assert/src/address_utils.ts new file mode 100644 index 000000000..bdbcd3a89 --- /dev/null +++ b/packages/assert/src/address_utils.ts @@ -0,0 +1,34 @@ +import * as jsSHA3 from 'js-sha3'; + +const BASIC_ADDRESS_REGEX = /^(0x)?[0-9a-f]{40}$/i; +const SAME_CASE_ADDRESS_REGEX = /^(0x)?([0-9a-f]{40}|[0-9A-F]{40})$/; + +export const addressUtils = { + isChecksumAddress(address: string): boolean { + // Check each case + const unprefixedAddress = address.replace('0x', ''); + const addressHash = jsSHA3.keccak256(unprefixedAddress.toLowerCase()); + + for (let i = 0; i < 40; i++) { + // The nth letter should be uppercase if the nth digit of casemap is 1 + if ((parseInt(addressHash[i], 16) > 7 && unprefixedAddress[i].toUpperCase() !== unprefixedAddress[i]) || + (parseInt(addressHash[i], 16) <= 7 && unprefixedAddress[i].toLowerCase() !== unprefixedAddress[i])) { + return false; + } + } + return true; + }, + isAddress(address: string): boolean { + if (!BASIC_ADDRESS_REGEX.test(address)) { + // Check if it has the basic requirements of an address + return false; + } else if (SAME_CASE_ADDRESS_REGEX.test(address)) { + // If it's all small caps or all all caps, return true + return true; + } else { + // Otherwise check each case + const isValidChecksummedAddress = addressUtils.isChecksumAddress(address); + return isValidChecksummedAddress; + } + }, +}; |