diff options
-rw-r--r-- | packages/assert/package.json | 2 | ||||
-rw-r--r-- | packages/assert/src/address_utils.ts | 34 | ||||
-rw-r--r-- | packages/assert/src/index.ts | 7 | ||||
-rw-r--r-- | yarn.lock | 10 |
4 files changed, 48 insertions, 5 deletions
diff --git a/packages/assert/package.json b/packages/assert/package.json index 65b163c21..800207ae6 100644 --- a/packages/assert/package.json +++ b/packages/assert/package.json @@ -39,7 +39,7 @@ "dependencies": { "@0xproject/json-schemas": "^0.6.10", "bignumber.js": "~4.1.0", - "ethereum-address": "^0.0.4", + "js-sha3": "^0.7.0", "lodash": "^4.17.4", "valid-url": "^1.0.9" } 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; + } + }, +}; diff --git a/packages/assert/src/index.ts b/packages/assert/src/index.ts index 92bcf6f03..94900074a 100644 --- a/packages/assert/src/index.ts +++ b/packages/assert/src/index.ts @@ -3,10 +3,11 @@ import { SchemaValidator, } from '@0xproject/json-schemas'; import BigNumber from 'bignumber.js'; -import * as ethereum_address from 'ethereum-address'; import * as _ from 'lodash'; import * as validUrl from 'valid-url'; +import {addressUtils} from './address_utils'; + const HEX_REGEX = /^0x[0-9A-F]*$/i; export const assert = { @@ -35,9 +36,9 @@ export const assert = { this.typeAssertionMessage(variableName, 'HexString', value)); }, isETHAddressHex(variableName: string, value: string): void { - this.assert(ethereum_address.isAddress(value), this.typeAssertionMessage(variableName, 'ETHAddressHex', value)); + this.assert(addressUtils.isAddress(value), this.typeAssertionMessage(variableName, 'ETHAddressHex', value)); this.assert( - ethereum_address.isAddress(value) && value.toLowerCase() === value, + addressUtils.isAddress(value) && value.toLowerCase() === value, `Checksummed addresses are not supported. Convert ${variableName} to lower case before passing`, ); }, @@ -1281,6 +1281,10 @@ bignumber.js@^4.0.2, bignumber.js@^4.1.0, bignumber.js@~4.1.0: version "4.1.0" resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-4.1.0.tgz#db6f14067c140bd46624815a7916c92d9b6c24b1" +bignumber.js@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-5.0.0.tgz#fbce63f09776b3000a83185badcde525daf34833" + "bignumber.js@git+https://github.com/debris/bignumber.js#master": version "2.0.7" resolved "git+https://github.com/debris/bignumber.js#c7a38de919ed75e6fb6ba38051986e294b328df9" @@ -4538,6 +4542,10 @@ js-sha3@^0.6.1: version "0.6.1" resolved "https://registry.yarnpkg.com/js-sha3/-/js-sha3-0.6.1.tgz#5b89f77a7477679877f58c4a075240934b1f95c0" +js-sha3@^0.7.0: + version "0.7.0" + resolved "https://registry.yarnpkg.com/js-sha3/-/js-sha3-0.7.0.tgz#0a5c57b36f79882573b2d84051f8bb85dd1bd63a" + js-tokens@^3.0.0, js-tokens@^3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b" @@ -5625,7 +5633,7 @@ normalize-url@^1.4.0: query-string "^4.1.0" sort-keys "^1.0.0" -npm-run-all@^4.1.2: +npm-run-all@^4.1.1, npm-run-all@^4.1.2: version "4.1.2" resolved "https://registry.yarnpkg.com/npm-run-all/-/npm-run-all-4.1.2.tgz#90d62d078792d20669139e718621186656cea056" dependencies: |