diff options
author | Amir Bandeali <abandeali1@gmail.com> | 2019-02-04 09:19:47 +0800 |
---|---|---|
committer | Amir Bandeali <abandeali1@gmail.com> | 2019-02-06 01:23:09 +0800 |
commit | 9abd6f569538025f7777e78963cc8aa24ce0052b (patch) | |
tree | c1991be73acbef6d45aff598dfca27ceb5ffd94a /packages/order-utils/src | |
parent | a394967268be3f3d3f7da3507887ee88befee253 (diff) | |
download | dexon-0x-contracts-9abd6f569538025f7777e78963cc8aa24ce0052b.tar.gz dexon-0x-contracts-9abd6f569538025f7777e78963cc8aa24ce0052b.tar.zst dexon-0x-contracts-9abd6f569538025f7777e78963cc8aa24ce0052b.zip |
Allow custom domains in eip712Utils
Diffstat (limited to 'packages/order-utils/src')
-rw-r--r-- | packages/order-utils/src/constants.ts | 10 | ||||
-rw-r--r-- | packages/order-utils/src/eip712_utils.ts | 47 | ||||
-rw-r--r-- | packages/order-utils/src/index.ts | 10 | ||||
-rw-r--r-- | packages/order-utils/src/order_hash.ts | 1 |
4 files changed, 47 insertions, 21 deletions
diff --git a/packages/order-utils/src/constants.ts b/packages/order-utils/src/constants.ts index a9a687719..3c93575b3 100644 --- a/packages/order-utils/src/constants.ts +++ b/packages/order-utils/src/constants.ts @@ -68,9 +68,9 @@ export const constants = { SELECTOR_CHAR_LENGTH_WITH_PREFIX: 10, INFINITE_TIMESTAMP_SEC: new BigNumber(2524604400), // Close to infinite ZERO_AMOUNT: new BigNumber(0), - EIP712_DOMAIN_NAME: '0x Protocol', - EIP712_DOMAIN_VERSION: '2', - EIP712_DOMAIN_SCHEMA: { + EXCHANGE_DOMAIN_NAME: '0x Protocol', + EXCHANGE_DOMAIN_VERSION: '2', + DEFAULT_DOMAIN_SCHEMA: { name: 'EIP712Domain', parameters: [ { name: 'name', type: 'string' }, @@ -78,7 +78,7 @@ export const constants = { { name: 'verifyingContract', type: 'address' }, ], }, - EIP712_ORDER_SCHEMA: { + EXCHANGE_ORDER_SCHEMA: { name: 'Order', parameters: [ { name: 'makerAddress', type: 'address' }, @@ -95,7 +95,7 @@ export const constants = { { name: 'takerAssetData', type: 'bytes' }, ], }, - EIP712_ZEROEX_TRANSACTION_SCHEMA: { + EXCHANGE_ZEROEX_TRANSACTION_SCHEMA: { name: 'ZeroExTransaction', parameters: [ { name: 'salt', type: 'uint256' }, diff --git a/packages/order-utils/src/eip712_utils.ts b/packages/order-utils/src/eip712_utils.ts index 4b37dba41..313653c63 100644 --- a/packages/order-utils/src/eip712_utils.ts +++ b/packages/order-utils/src/eip712_utils.ts @@ -1,36 +1,49 @@ import { assert } from '@0x/assert'; import { schemas } from '@0x/json-schemas'; -import { EIP712Object, EIP712TypedData, EIP712Types, Order, ZeroExTransaction } from '@0x/types'; +import { + EIP712DomainWithDefaultSchema, + EIP712Object, + EIP712TypedData, + EIP712Types, + Order, + ZeroExTransaction, +} from '@0x/types'; import * as _ from 'lodash'; import { constants } from './constants'; +export const DEFAULT_DOMAIN_SCHEMA = constants.DEFAULT_DOMAIN_SCHEMA; +export const EXCHANGE_DOMAIN_NAME = constants.EXCHANGE_DOMAIN_NAME; +export const EXCHANGE_DOMAIN_VERSION = constants.EXCHANGE_DOMAIN_VERSION; +export const EXCHANGE_ORDER_SCHEMA = constants.EXCHANGE_ORDER_SCHEMA; +export const EXCHANGE_ZEROEX_TRANSACTION_SCHEMA = constants.EXCHANGE_ZEROEX_TRANSACTION_SCHEMA; + export const eip712Utils = { /** * Creates a EIP712TypedData object specific to the 0x protocol for use with signTypedData. * @param primaryType The primary type found in message * @param types The additional types for the data in message * @param message The contents of the message - * @param verifyingContractAddress The address of the verifying contract + * @param domain Domain containing a name (optional), version (optional), and verifying contract address * @return A typed data object */ createTypedData: ( primaryType: string, types: EIP712Types, message: EIP712Object, - verifyingContractAddress: string, + domain: EIP712DomainWithDefaultSchema, ): EIP712TypedData => { - assert.isETHAddressHex('verifyingContractAddress', verifyingContractAddress); + assert.isETHAddressHex('verifyingContractAddress', domain.verifyingContractAddress); assert.isString('primaryType', primaryType); const typedData = { types: { - EIP712Domain: constants.EIP712_DOMAIN_SCHEMA.parameters, + EIP712Domain: DEFAULT_DOMAIN_SCHEMA.parameters, ...types, }, domain: { - name: constants.EIP712_DOMAIN_NAME, - version: constants.EIP712_DOMAIN_VERSION, - verifyingContract: verifyingContractAddress, + name: _.isUndefined(domain.name) ? EXCHANGE_DOMAIN_NAME : domain.name, + version: _.isUndefined(domain.version) ? EXCHANGE_DOMAIN_VERSION : domain.version, + verifyingContract: domain.verifyingContractAddress, }, message, primaryType, @@ -48,11 +61,14 @@ export const eip712Utils = { const normalizedOrder = _.mapValues(order, value => { return !_.isString(value) ? value.toString() : value; }); + const domain = { + verifyingContractAddress: order.exchangeAddress, + }; const typedData = eip712Utils.createTypedData( - constants.EIP712_ORDER_SCHEMA.name, - { Order: constants.EIP712_ORDER_SCHEMA.parameters }, + EXCHANGE_ORDER_SCHEMA.name, + { Order: EXCHANGE_ORDER_SCHEMA.parameters }, normalizedOrder, - order.exchangeAddress, + domain, ); return typedData; }, @@ -68,11 +84,14 @@ export const eip712Utils = { const normalizedTransaction = _.mapValues(zeroExTransaction, value => { return !_.isString(value) ? value.toString() : value; }); + const domain = { + verifyingContractAddress: zeroExTransaction.verifyingContractAddress, + }; const typedData = eip712Utils.createTypedData( - constants.EIP712_ZEROEX_TRANSACTION_SCHEMA.name, - { ZeroExTransaction: constants.EIP712_ZEROEX_TRANSACTION_SCHEMA.parameters }, + EXCHANGE_ZEROEX_TRANSACTION_SCHEMA.name, + { ZeroExTransaction: EXCHANGE_ZEROEX_TRANSACTION_SCHEMA.parameters }, normalizedTransaction, - zeroExTransaction.verifyingContractAddress, + domain, ); return typedData; }, diff --git a/packages/order-utils/src/index.ts b/packages/order-utils/src/index.ts index 26cbf9665..436677efc 100644 --- a/packages/order-utils/src/index.ts +++ b/packages/order-utils/src/index.ts @@ -19,7 +19,14 @@ export { ExchangeTransferSimulator } from './exchange_transfer_simulator'; export { BalanceAndProxyAllowanceLazyStore } from './store/balance_and_proxy_allowance_lazy_store'; export { OrderFilledCancelledLazyStore } from './store/order_filled_cancelled_lazy_store'; -export { eip712Utils } from './eip712_utils'; +export { + eip712Utils, + DEFAULT_DOMAIN_SCHEMA, + EXCHANGE_DOMAIN_NAME, + EXCHANGE_DOMAIN_VERSION, + EXCHANGE_ORDER_SCHEMA, + EXCHANGE_ZEROEX_TRANSACTION_SCHEMA, +} from './eip712_utils'; export { Provider, @@ -51,6 +58,7 @@ export { EIP712Types, EIP712Object, EIP712ObjectValue, + EIP712DomainWithDefaultSchema, ZeroExTransaction, SignedZeroExTransaction, } from '@0x/types'; diff --git a/packages/order-utils/src/order_hash.ts b/packages/order-utils/src/order_hash.ts index b15ccacf7..3bc23c14e 100644 --- a/packages/order-utils/src/order_hash.ts +++ b/packages/order-utils/src/order_hash.ts @@ -61,7 +61,6 @@ export const orderHashUtils = { } throw error; } - const typedData = eip712Utils.createOrderTypedData(order); const orderHashBuff = signTypedDataUtils.generateTypedDataHash(typedData); return orderHashBuff; |