diff options
author | Leonid <logvinov.leon@gmail.com> | 2017-12-12 22:30:04 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-12 22:30:04 +0800 |
commit | cb7188d47390719b74d7e09c938068a9f201c1b0 (patch) | |
tree | 1b67b0b849550bea8b5ad20fe4d7a383ec49163d /packages/utils | |
parent | 02f22d91555ea05fc52d342e9c91cd9477321da1 (diff) | |
parent | d778378b37b0cbd77ecdf8d1cafc7720682064ec (diff) | |
download | dexon-sol-tools-cb7188d47390719b74d7e09c938068a9f201c1b0.tar.gz dexon-sol-tools-cb7188d47390719b74d7e09c938068a9f201c1b0.tar.zst dexon-sol-tools-cb7188d47390719b74d7e09c938068a9f201c1b0.zip |
Merge pull request #257 from 0xProject/fix/ethereum-address
Remove ethereum-address dependency and add this logic to the repo
Diffstat (limited to 'packages/utils')
-rw-r--r-- | packages/utils/package.json | 1 | ||||
-rw-r--r-- | packages/utils/src/address_utils.ts | 34 | ||||
-rw-r--r-- | packages/utils/src/index.ts | 1 |
3 files changed, 36 insertions, 0 deletions
diff --git a/packages/utils/package.json b/packages/utils/package.json index 1af8a7e11..8f856d0a0 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -28,6 +28,7 @@ }, "dependencies": { "bignumber.js": "~4.1.0", + "js-sha3": "^0.7.0", "lodash": "^4.17.4" } } diff --git a/packages/utils/src/address_utils.ts b/packages/utils/src/address_utils.ts new file mode 100644 index 000000000..bdbcd3a89 --- /dev/null +++ b/packages/utils/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; + } + }, +}; diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index a61f04ad2..edfd192b2 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -1 +1,2 @@ export {promisify} from './promisify'; +export {addressUtils} from './address_utils'; |