From f3e6ef0fa96e2252e41b7ed6f2c3e88a1560153e Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 8 Feb 2018 18:01:53 +0100 Subject: Better validate ZeroExConfig on public networks --- packages/0x.js/src/0x.ts | 5 +++ .../0x.js/src/schemas/zero_ex_config_schema.ts | 24 +-------------- .../zero_ex_private_network_config_schema.ts | 35 +++++++++++++++++++++ .../zero_ex_public_network_config_schema.ts | 29 +++++++++++++++++ packages/0x.js/src/types.ts | 36 ++++++++++++++++------ 5 files changed, 96 insertions(+), 33 deletions(-) create mode 100644 packages/0x.js/src/schemas/zero_ex_private_network_config_schema.ts create mode 100644 packages/0x.js/src/schemas/zero_ex_public_network_config_schema.ts (limited to 'packages/0x.js/src') diff --git a/packages/0x.js/src/0x.ts b/packages/0x.js/src/0x.ts index 6cfa65cc2..d024e6097 100644 --- a/packages/0x.js/src/0x.ts +++ b/packages/0x.js/src/0x.ts @@ -13,6 +13,8 @@ import { TokenTransferProxyWrapper } from './contract_wrappers/token_transfer_pr import { TokenWrapper } from './contract_wrappers/token_wrapper'; import { OrderStateWatcher } from './order_watcher/order_state_watcher'; import { zeroExConfigSchema } from './schemas/zero_ex_config_schema'; +import { zeroExPrivateNetworkConfigSchema } from './schemas/zero_ex_private_network_config_schema'; +import { zeroExPublicNetworkConfigSchema } from './schemas/zero_ex_public_network_config_schema'; import { ECSignature, Order, SignedOrder, Web3Provider, ZeroExConfig, ZeroExError } from './types'; import { assert } from './utils/assert'; import { constants } from './utils/constants'; @@ -20,6 +22,9 @@ import { decorators } from './utils/decorators'; import { signatureUtils } from './utils/signature_utils'; import { utils } from './utils/utils'; +assert.schemaValidator.addSchema(zeroExPrivateNetworkConfigSchema); +assert.schemaValidator.addSchema(zeroExPublicNetworkConfigSchema); + /** * The ZeroEx class is the single entry-point into the 0x.js library. It contains all of the library's functionality * and all calls to the library should be made through a ZeroEx instance. diff --git a/packages/0x.js/src/schemas/zero_ex_config_schema.ts b/packages/0x.js/src/schemas/zero_ex_config_schema.ts index 546b1c2d0..a9c3c64fc 100644 --- a/packages/0x.js/src/schemas/zero_ex_config_schema.ts +++ b/packages/0x.js/src/schemas/zero_ex_config_schema.ts @@ -1,27 +1,5 @@ export const zeroExConfigSchema = { id: '/ZeroExConfig', - properties: { - networkId: { - type: 'number', - minimum: 0, - }, - gasPrice: { $ref: '/Number' }, - exchangeContractAddress: { $ref: '/Address' }, - tokenRegistryContractAddress: { $ref: '/Address' }, - orderWatcherConfig: { - type: 'object', - properties: { - pollingIntervalMs: { - type: 'number', - minimum: 0, - }, - numConfirmations: { - type: 'number', - minimum: 0, - }, - }, - }, - }, + oneOf: [{ $ref: '/ZeroExPrivateNetworkConfig' }, { $ref: '/ZeroExPublicNetworkConfig' }], type: 'object', - required: ['networkId'], }; diff --git a/packages/0x.js/src/schemas/zero_ex_private_network_config_schema.ts b/packages/0x.js/src/schemas/zero_ex_private_network_config_schema.ts new file mode 100644 index 000000000..f7f649a6d --- /dev/null +++ b/packages/0x.js/src/schemas/zero_ex_private_network_config_schema.ts @@ -0,0 +1,35 @@ +export const zeroExPrivateNetworkConfigSchema = { + id: '/ZeroExPrivateNetworkConfig', + properties: { + networkId: { + type: 'number', + minimum: 1, + }, + gasPrice: { $ref: '/Number' }, + zrxContractAddress: { $ref: '/Address' }, + exchangeContractAddress: { $ref: '/Address' }, + tokenRegistryContractAddress: { $ref: '/Address' }, + tokenTransferProxyContractAddress: { $ref: '/Address' }, + orderWatcherConfig: { + type: 'object', + properties: { + pollingIntervalMs: { + type: 'number', + minimum: 0, + }, + numConfirmations: { + type: 'number', + minimum: 0, + }, + }, + }, + }, + type: 'object', + required: [ + 'networkId', + 'zrxContractAddress', + 'exchangeContractAddress', + 'tokenRegistryContractAddress', + 'tokenTransferProxyContractAddress', + ], +}; diff --git a/packages/0x.js/src/schemas/zero_ex_public_network_config_schema.ts b/packages/0x.js/src/schemas/zero_ex_public_network_config_schema.ts new file mode 100644 index 000000000..9da31481a --- /dev/null +++ b/packages/0x.js/src/schemas/zero_ex_public_network_config_schema.ts @@ -0,0 +1,29 @@ +export const zeroExPublicNetworkConfigSchema = { + id: '/ZeroExPublicNetworkConfig', + properties: { + networkId: { + type: 'number', + enum: [1, 3, 4, 42, 50], + }, + gasPrice: { $ref: '/Number' }, + zrxContractAddress: { $ref: '/Address' }, + exchangeContractAddress: { $ref: '/Address' }, + tokenRegistryContractAddress: { $ref: '/Address' }, + tokenTransferProxyContractAddress: { $ref: '/Address' }, + orderWatcherConfig: { + type: 'object', + properties: { + pollingIntervalMs: { + type: 'number', + minimum: 0, + }, + numConfirmations: { + type: 'number', + minimum: 0, + }, + }, + }, + }, + type: 'object', + required: ['networkId'], +}; diff --git a/packages/0x.js/src/types.ts b/packages/0x.js/src/types.ts index ab97f7775..f0660391b 100644 --- a/packages/0x.js/src/types.ts +++ b/packages/0x.js/src/types.ts @@ -9,6 +9,10 @@ import { ExchangeContractEventArgs, ExchangeEvents } from './contract_wrappers/g import { TokenContractEventArgs, TokenEvents } from './contract_wrappers/generated/token'; export enum ZeroExError { + ZRXAddressRequired = 'ZRX_ADDREESS_REQUIRED', + ExchangeAddressRequired = 'EXCHANGE_ADDREESS_REQUIRED', + TokenRegistryAddressRequired = 'TOKEN_REGISTRY_ADDREESS_REQUIRED', + TokenTransferProxyAddressRequired = 'TOKEN_TRANSFER_PROXY_ADDREESS_REQUIRED', ExchangeContractDoesNotExist = 'EXCHANGE_CONTRACT_DOES_NOT_EXIST', ZRXContractDoesNotExist = 'ZRX_CONTRACT_DOES_NOT_EXIST', EtherTokenContractDoesNotExist = 'ETHER_TOKEN_CONTRACT_DOES_NOT_EXIST', @@ -195,8 +199,28 @@ export interface OrderStateWatcherConfig { cleanupJobIntervalMs?: number; } +export interface ZeroExPublicNetworkConfig { + networkId: 1 | 3 | 4 | 42 | 50; + gasPrice?: BigNumber; + exchangeContractAddress?: string; + zrxContractAddress?: string; + tokenRegistryContractAddress?: string; + tokenTransferProxyContractAddress?: string; + orderWatcherConfig?: OrderStateWatcherConfig; +} + +export interface ZeroExPrivateNetworkConfig { + networkId: number; + gasPrice?: BigNumber; + exchangeContractAddress: string; + zrxContractAddress: string; + tokenRegistryContractAddress: string; + tokenTransferProxyContractAddress: string; + orderWatcherConfig?: OrderStateWatcherConfig; +} + /* - * networkId: The id of the underlying ethereum network your provider is connected to. (1-mainnet, 42-kovan, 50-testrpc) + * networkId: The id of the underlying ethereum network your provider is connected to. (1-mainnet, 3-ropsten, 4-rinkeby, 42-kovan, 50-testrpc) * gasPrice: Gas price to use with every transaction * exchangeContractAddress: The address of an exchange contract to use * zrxContractAddress: The address of the ZRX contract to use @@ -204,15 +228,7 @@ export interface OrderStateWatcherConfig { * tokenTransferProxyContractAddress: The address of the token transfer proxy contract to use * orderWatcherConfig: All the configs related to the orderWatcher */ -export interface ZeroExConfig { - networkId: number; - gasPrice?: BigNumber; - exchangeContractAddress?: string; - zrxContractAddress?: string; - tokenRegistryContractAddress?: string; - tokenTransferProxyContractAddress?: string; - orderWatcherConfig?: OrderStateWatcherConfig; -} +export type ZeroExConfig = ZeroExPublicNetworkConfig | ZeroExPrivateNetworkConfig; export type ArtifactContractName = 'ZRX' | 'TokenTransferProxy' | 'TokenRegistry' | 'Token' | 'Exchange' | 'EtherToken'; -- cgit v1.2.3