diff options
author | Fabio Berger <me@fabioberger.com> | 2017-12-13 01:38:13 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2017-12-13 01:38:13 +0800 |
commit | 4f72c527def4adef60200b60f9bf6221956af7a3 (patch) | |
tree | cfe74030bf9ebbfee9063da6faa878f995b65715 /packages/utils/src/address_utils.ts | |
parent | 71cb2e05d1d3d7e2abbbe319dc7a39d7d79d01ac (diff) | |
parent | cb7188d47390719b74d7e09c938068a9f201c1b0 (diff) | |
download | dexon-sol-tools-4f72c527def4adef60200b60f9bf6221956af7a3.tar dexon-sol-tools-4f72c527def4adef60200b60f9bf6221956af7a3.tar.gz dexon-sol-tools-4f72c527def4adef60200b60f9bf6221956af7a3.tar.bz2 dexon-sol-tools-4f72c527def4adef60200b60f9bf6221956af7a3.tar.lz dexon-sol-tools-4f72c527def4adef60200b60f9bf6221956af7a3.tar.xz dexon-sol-tools-4f72c527def4adef60200b60f9bf6221956af7a3.tar.zst dexon-sol-tools-4f72c527def4adef60200b60f9bf6221956af7a3.zip |
Merge branch 'development' of github.com:0xProject/0x.js into development
* 'development' of github.com:0xProject/0x.js:
Re-hard code the dependency
Add missing dependency
Add missing dependency
Fix a dependency
Fix an invocation
Move dependency
Merge dependencies
Don't unsubscribe on error. It's done automatically
Rename blockAndLogStreamer to blockAndLogStreamerIfExists
Move isAddress to shared utils and remove all dependencies on ethereum-address
Remove ethereum-address dependency and add this logic to the repo
Diffstat (limited to 'packages/utils/src/address_utils.ts')
-rw-r--r-- | packages/utils/src/address_utils.ts | 34 |
1 files changed, 34 insertions, 0 deletions
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; + } + }, +}; |