From f1e7ea118b0c8c9a9a013b09d0e63bdd3694b21a Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 8 Dec 2017 13:58:33 +0300 Subject: Remove ethereum-address dependency and add this logic to the repo --- packages/assert/src/address_utils.ts | 34 ++++++++++++++++++++++++++++++++++ packages/assert/src/index.ts | 7 ++++--- 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 packages/assert/src/address_utils.ts (limited to 'packages/assert/src') 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`, ); }, -- cgit From a14424ae5f45784db5e75c2d6a9098dc96914c9e Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 11 Dec 2017 12:43:19 +0100 Subject: Move isAddress to shared utils and remove all dependencies on ethereum-address --- packages/assert/src/address_utils.ts | 34 ---------------------------------- packages/assert/src/globals.d.ts | 4 ---- packages/assert/src/index.ts | 3 +-- 3 files changed, 1 insertion(+), 40 deletions(-) delete mode 100644 packages/assert/src/address_utils.ts (limited to 'packages/assert/src') diff --git a/packages/assert/src/address_utils.ts b/packages/assert/src/address_utils.ts deleted file mode 100644 index bdbcd3a89..000000000 --- a/packages/assert/src/address_utils.ts +++ /dev/null @@ -1,34 +0,0 @@ -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/globals.d.ts b/packages/assert/src/globals.d.ts index cc47f3113..91ed2021e 100644 --- a/packages/assert/src/globals.d.ts +++ b/packages/assert/src/globals.d.ts @@ -1,5 +1 @@ declare module 'dirty-chai'; - -declare module 'ethereum-address' { - const isAddress: (arg: any) => boolean; -} diff --git a/packages/assert/src/index.ts b/packages/assert/src/index.ts index 94900074a..fadc31d09 100644 --- a/packages/assert/src/index.ts +++ b/packages/assert/src/index.ts @@ -2,12 +2,11 @@ import { Schema, SchemaValidator, } from '@0xproject/json-schemas'; +import {addressUtils} from '@0xproject/utils'; import BigNumber from 'bignumber.js'; 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 = { -- cgit