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 | |
parent | 02f22d91555ea05fc52d342e9c91cd9477321da1 (diff) | |
parent | d778378b37b0cbd77ecdf8d1cafc7720682064ec (diff) | |
download | dexon-0x-contracts-cb7188d47390719b74d7e09c938068a9f201c1b0.tar.gz dexon-0x-contracts-cb7188d47390719b74d7e09c938068a9f201c1b0.tar.zst dexon-0x-contracts-cb7188d47390719b74d7e09c938068a9f201c1b0.zip |
Merge pull request #257 from 0xProject/fix/ethereum-address
Remove ethereum-address dependency and add this logic to the repo
-rw-r--r-- | packages/assert/package.json | 2 | ||||
-rw-r--r-- | packages/assert/src/globals.d.ts | 4 | ||||
-rw-r--r-- | packages/assert/src/index.ts | 6 | ||||
-rw-r--r-- | packages/subproviders/package.json | 1 | ||||
-rw-r--r-- | packages/subproviders/src/globals.d.ts | 5 | ||||
-rw-r--r-- | packages/subproviders/src/subproviders/ledger.ts | 4 | ||||
-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 | ||||
-rw-r--r-- | packages/website/package.json | 1 | ||||
-rw-r--r-- | packages/website/ts/components/inputs/address_input.tsx | 4 | ||||
-rw-r--r-- | packages/website/ts/globals.d.ts | 5 | ||||
-rw-r--r-- | yarn.lock | 14 |
13 files changed, 58 insertions, 24 deletions
diff --git a/packages/assert/package.json b/packages/assert/package.json index 65b163c21..2fb7746a8 100644 --- a/packages/assert/package.json +++ b/packages/assert/package.json @@ -38,8 +38,8 @@ }, "dependencies": { "@0xproject/json-schemas": "^0.6.10", + "@0xproject/utils": "^0.1.0", "bignumber.js": "~4.1.0", - "ethereum-address": "^0.0.4", "lodash": "^4.17.4", "valid-url": "^1.0.9" } 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 92bcf6f03..fadc31d09 100644 --- a/packages/assert/src/index.ts +++ b/packages/assert/src/index.ts @@ -2,8 +2,8 @@ import { Schema, SchemaValidator, } from '@0xproject/json-schemas'; +import {addressUtils} from '@0xproject/utils'; import BigNumber from 'bignumber.js'; -import * as ethereum_address from 'ethereum-address'; import * as _ from 'lodash'; import * as validUrl from 'valid-url'; @@ -35,9 +35,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`, ); }, diff --git a/packages/subproviders/package.json b/packages/subproviders/package.json index 7ff0c5b09..b0c5686a6 100644 --- a/packages/subproviders/package.json +++ b/packages/subproviders/package.json @@ -21,7 +21,6 @@ "@0xproject/utils": "^0.1.0", "bn.js": "^4.11.8", "es6-promisify": "^5.0.0", - "ethereum-address": "^0.0.4", "ethereumjs-tx": "^1.3.3", "ethereumjs-util": "^5.1.1", "ledgerco": "0xProject/ledger-node-js-api", diff --git a/packages/subproviders/src/globals.d.ts b/packages/subproviders/src/globals.d.ts index 520ca9232..400bbef5b 100644 --- a/packages/subproviders/src/globals.d.ts +++ b/packages/subproviders/src/globals.d.ts @@ -54,11 +54,6 @@ declare module 'ledgerco' { } } -// ethereum-address declarations -declare module 'ethereum-address' { - export const isAddress: (address: string) => boolean; -} - // Semaphore-async-await declarations declare module 'semaphore-async-await' { class Semaphore { diff --git a/packages/subproviders/src/subproviders/ledger.ts b/packages/subproviders/src/subproviders/ledger.ts index e0a08f792..578f6ff6f 100644 --- a/packages/subproviders/src/subproviders/ledger.ts +++ b/packages/subproviders/src/subproviders/ledger.ts @@ -1,6 +1,6 @@ import {assert} from '@0xproject/assert'; +import {addressUtils} from '@0xproject/utils'; import promisify = require('es6-promisify'); -import {isAddress} from 'ethereum-address'; import EthereumTx = require('ethereumjs-tx'); import ethUtil = require('ethereumjs-util'); import * as ledger from 'ledgerco'; @@ -47,7 +47,7 @@ export class LedgerSubprovider extends Subprovider { return isValid; } private static validateSender(sender: string) { - if (_.isUndefined(sender) || !isAddress(sender)) { + if (_.isUndefined(sender) || !addressUtils.isAddress(sender)) { throw new Error(LedgerSubproviderErrors.SenderInvalidOrNotSupplied); } } 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'; diff --git a/packages/website/package.json b/packages/website/package.json index 5162a27ff..f005668f8 100644 --- a/packages/website/package.json +++ b/packages/website/package.json @@ -28,7 +28,6 @@ "dateformat": "^2.0.0", "deep-equal": "^1.0.1", "dharma-loan-frame": "^0.0.12", - "ethereum-address": "^0.0.4", "ethereumjs-tx": "^1.3.3", "ethereumjs-util": "^5.1.1", "find-versions": "^2.0.0", diff --git a/packages/website/ts/components/inputs/address_input.tsx b/packages/website/ts/components/inputs/address_input.tsx index 8b03b8d12..40ae722d9 100644 --- a/packages/website/ts/components/inputs/address_input.tsx +++ b/packages/website/ts/components/inputs/address_input.tsx @@ -1,4 +1,4 @@ -import {isAddress} from 'ethereum-address'; +import {addressUtils} from '@0xproject/utils'; import * as _ from 'lodash'; import {colors} from 'material-ui/styles'; import TextField from 'material-ui/TextField'; @@ -62,7 +62,7 @@ export class AddressInput extends React.Component<AddressInputProps, AddressInpu } private onOrderTakerAddressUpdated(e: any) { const address = e.target.value.toLowerCase(); - const isValidAddress = isAddress(address) || address === ''; + const isValidAddress = addressUtils.isAddress(address) || address === ''; const errMsg = isValidAddress ? '' : 'Invalid ethereum address'; this.setState({ address, diff --git a/packages/website/ts/globals.d.ts b/packages/website/ts/globals.d.ts index b4611f583..3144d7eb5 100644 --- a/packages/website/ts/globals.d.ts +++ b/packages/website/ts/globals.d.ts @@ -55,11 +55,6 @@ interface System { } declare var System: System; -// ethereum-address declarations -declare module 'ethereum-address' { - export const isAddress: (address: string) => boolean; -} - // jsonschema declarations // Source: https://github.com/tdegrunt/jsonschema/blob/master/lib/index.d.ts declare interface Schema { @@ -27,6 +27,16 @@ uuid "^3.1.0" web3 "^0.20.0" +"@0xproject/assert@^0.0.6": + version "0.0.6" + resolved "https://registry.yarnpkg.com/@0xproject/assert/-/assert-0.0.6.tgz#773616620314f40ace11a9c4c65cc95398d2c178" + dependencies: + "@0xproject/json-schemas" "^0.6.9" + bignumber.js "~4.1.0" + ethereum-address "^0.0.4" + lodash "^4.17.4" + valid-url "^1.0.9" + "@types/accounting@^0.4.1": version "0.4.1" resolved "https://registry.yarnpkg.com/@types/accounting/-/accounting-0.4.1.tgz#865d9f5694fd7c438fba34eb4bc82eec6f34cdd5" @@ -4518,6 +4528,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" |