Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
Return SignedOrder from signing utils.
Browse files Browse the repository at this point in the history
Create a helper back in EIP712Utils for code cleanup.
Moved constants in order-utils into the constants object
  • Loading branch information
dekz committed Oct 5, 2018
1 parent 6e462b7 commit 75d274f
Show file tree
Hide file tree
Showing 25 changed files with 380 additions and 192 deletions.
2 changes: 1 addition & 1 deletion packages/0x.js/CHANGELOG.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
{
"note":
"Removed `SignerType` (including `SignerType.Metamask`). Please use the `MetamaskSubprovider` to wrap web3.currentProvider.",
"Removed `SignerType` (including `SignerType.Metamask`). Please use the `MetamaskSubprovider` to wrap `web3.currentProvider`.",
"pr": 1102
}
]
Expand Down
27 changes: 3 additions & 24 deletions packages/contract-wrappers/src/utils/transaction_encoder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { schemas } from '@0xproject/json-schemas';
import { EIP712_DOMAIN_NAME, EIP712_DOMAIN_SCHEMA, EIP712_DOMAIN_VERSION } from '@0xproject/order-utils';
import { eip712Utils } from '@0xproject/order-utils';
import { Order, SignedOrder } from '@0xproject/types';
import { BigNumber, signTypedDataUtils } from '@0xproject/utils';
import _ = require('lodash');
Expand All @@ -8,15 +8,6 @@ import { ExchangeContract } from '../contract_wrappers/generated/exchange';

import { assert } from './assert';

const EIP712_ZEROEX_TRANSACTION_SCHEMA = {
name: 'ZeroExTransaction',
parameters: [
{ name: 'salt', type: 'uint256' },
{ name: 'signerAddress', type: 'address' },
{ name: 'data', type: 'bytes' },
],
};

/**
* Transaction Encoder. Transaction messages exist for the purpose of calling methods on the Exchange contract
* in the context of another address. For example, UserA can encode and sign a fillOrder transaction and UserB
Expand All @@ -37,23 +28,11 @@ export class TransactionEncoder {
public getTransactionHex(data: string, salt: BigNumber, signerAddress: string): string {
const exchangeAddress = this._getExchangeContract().address;
const executeTransactionData = {
salt: salt.toString(),
salt,
signerAddress,
data,
};
const typedData = {
types: {
EIP712Domain: EIP712_DOMAIN_SCHEMA.parameters,
ZeroExTransaction: EIP712_ZEROEX_TRANSACTION_SCHEMA.parameters,
},
domain: {
name: EIP712_DOMAIN_NAME,
version: EIP712_DOMAIN_VERSION,
verifyingContract: exchangeAddress,
},
message: executeTransactionData,
primaryType: EIP712_ZEROEX_TRANSACTION_SCHEMA.name,
};
const typedData = eip712Utils.createZeroExTransactionTypedData(executeTransactionData, exchangeAddress);
const eip712MessageBuffer = signTypedDataUtils.signTypedDataHash(typedData);
const messageHex = `0x${eip712MessageBuffer.toString('hex')}`;
return messageHex;
Expand Down
34 changes: 4 additions & 30 deletions packages/contracts/test/utils/transaction_factory.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
import {
EIP712_DOMAIN_NAME,
EIP712_DOMAIN_SCHEMA,
EIP712_DOMAIN_VERSION,
generatePseudoRandomSalt,
} from '@0xproject/order-utils';
import { eip712Utils, generatePseudoRandomSalt } from '@0xproject/order-utils';
import { SignatureType } from '@0xproject/types';
import { signTypedDataUtils } from '@0xproject/utils';
import * as ethUtil from 'ethereumjs-util';

import { signingUtils } from './signing_utils';
import { SignedTransaction } from './types';

const EIP712_ZEROEX_TRANSACTION_SCHEMA = {
name: 'ZeroExTransaction',
parameters: [
{ name: 'salt', type: 'uint256' },
{ name: 'signerAddress', type: 'address' },
{ name: 'data', type: 'bytes' },
],
};

export class TransactionFactory {
private readonly _signerBuff: Buffer;
private readonly _exchangeAddress: string;
Expand All @@ -33,30 +19,18 @@ export class TransactionFactory {
const salt = generatePseudoRandomSalt();
const signerAddress = `0x${this._signerBuff.toString('hex')}`;
const executeTransactionData = {
salt: salt.toString(),
salt,
signerAddress,
data,
};
const typedData = {
types: {
EIP712Domain: EIP712_DOMAIN_SCHEMA.parameters,
ZeroExTransaction: EIP712_ZEROEX_TRANSACTION_SCHEMA.parameters,
},
domain: {
name: EIP712_DOMAIN_NAME,
version: EIP712_DOMAIN_VERSION,
verifyingContract: this._exchangeAddress,
},
message: executeTransactionData,
primaryType: EIP712_ZEROEX_TRANSACTION_SCHEMA.name,
};

const typedData = eip712Utils.createZeroExTransactionTypedData(executeTransactionData, this._exchangeAddress);
const eip712MessageBuffer = signTypedDataUtils.signTypedDataHash(typedData);
const signature = signingUtils.signMessage(eip712MessageBuffer, this._privateKey, signatureType);
const signedTx = {
exchangeAddress: this._exchangeAddress,
signature: `0x${signature.toString('hex')}`,
...executeTransactionData,
salt,
};
return signedTx;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/json-schemas/schemas/eip712_typed_data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const eip712TypedData = {
id: 'eip712TypedData',
id: '/eip712TypedData',
type: 'object',
properties: {
types: {
Expand Down
6 changes: 3 additions & 3 deletions packages/order-utils/CHANGELOG.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
"version": "2.0.0",
"changes": [
{
"note": "Added ecSignOrderAsync to first sign an order as EIP712 and fallback to EthSign",
"note": "Added `ecSignOrderAsync` to first sign an order as EIP712 and fallback to EthSign",
"pr": 1102
},
{
"note": "Added ecSignTypedDataOrderAsync to sign an order exclusively as EIP712",
"note": "Added `ecSignTypedDataOrderAsync` to sign an order exclusively as EIP712",
"pr": 1102
},
{
"note": "Rename ecSignOrderHashAsync to ecSignHashAsync removing SignerType parameter",
"note": "Rename `ecSignOrderHashAsync` to `ecSignHashAsync` removing `SignerType` parameter",
"pr": 1102
}
]
Expand Down
47 changes: 35 additions & 12 deletions packages/order-utils/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,39 @@ export const constants = {
BASE_16: 16,
INFINITE_TIMESTAMP_SEC: new BigNumber(2524604400), // Close to infinite
ZERO_AMOUNT: new BigNumber(0),
};

export const EIP712_DOMAIN_NAME = '0x Protocol';
export const EIP712_DOMAIN_VERSION = '2';

export const EIP712_DOMAIN_SCHEMA = {
name: 'EIP712Domain',
parameters: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'verifyingContract', type: 'address' },
],
EIP712_DOMAIN_NAME: '0x Protocol',
EIP712_DOMAIN_VERSION: '2',
EIP712_DOMAIN_SCHEMA: {
name: 'EIP712Domain',
parameters: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'verifyingContract', type: 'address' },
],
},
EIP712_ORDER_SCHEMA: {
name: 'Order',
parameters: [
{ name: 'makerAddress', type: 'address' },
{ name: 'takerAddress', type: 'address' },
{ name: 'feeRecipientAddress', type: 'address' },
{ name: 'senderAddress', type: 'address' },
{ name: 'makerAssetAmount', type: 'uint256' },
{ name: 'takerAssetAmount', type: 'uint256' },
{ name: 'makerFee', type: 'uint256' },
{ name: 'takerFee', type: 'uint256' },
{ name: 'expirationTimeSeconds', type: 'uint256' },
{ name: 'salt', type: 'uint256' },
{ name: 'makerAssetData', type: 'bytes' },
{ name: 'takerAssetData', type: 'bytes' },
],
},
EIP712_ZEROEX_TRANSACTION_SCHEMA: {
name: 'ZeroExTransaction',
parameters: [
{ name: 'salt', type: 'uint256' },
{ name: 'signerAddress', type: 'address' },
{ name: 'data', type: 'bytes' },
],
},
};
75 changes: 75 additions & 0 deletions packages/order-utils/src/eip712_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { EIP712Object, EIP712TypedData, EIP712Types, Order, ZeroExTransaction } from '@0xproject/types';
import * as _ from 'lodash';

import { constants } from './constants';

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 exchangeAddress The address of the exchange contract
* @return A typed data object
*/
createTypedData: (
primaryType: string,
types: EIP712Types,
message: EIP712Object,
exchangeAddress: string,
): EIP712TypedData => {
const typedData = {
types: {
EIP712Domain: constants.EIP712_DOMAIN_SCHEMA.parameters,
...types,
},
domain: {
name: constants.EIP712_DOMAIN_NAME,
version: constants.EIP712_DOMAIN_VERSION,
verifyingContract: exchangeAddress,
},
message,
primaryType,
};
return typedData;
},
/**
* Creates an Order EIP712TypedData object for use with signTypedData.
* @param Order the order
* @return A typed data object
*/
createOrderTypedData: (order: Order): EIP712TypedData => {
const normalizedOrder = _.mapValues(order, value => {
return !_.isString(value) ? value.toString() : value;
});
const typedData = eip712Utils.createTypedData(
constants.EIP712_ORDER_SCHEMA.name,
{ Order: constants.EIP712_ORDER_SCHEMA.parameters },
normalizedOrder,
order.exchangeAddress,
);
return typedData;
},
/**
* Creates an ExecuteTransaction EIP712TypedData object for use with signTypedData and
* 0x Exchange executeTransaction.
* @param ZeroExTransaction the 0x transaction
* @param exchangeAddress The address of the exchange contract
* @return A typed data object
*/
createZeroExTransactionTypedData: (
zeroExTransaction: ZeroExTransaction,
exchangeAddress: string,
): EIP712TypedData => {
const normalizedTransaction = _.mapValues(zeroExTransaction, value => {
return !_.isString(value) ? value.toString() : value;
});
const typedData = eip712Utils.createTypedData(
constants.EIP712_ZEROEX_TRANSACTION_SCHEMA.name,
{ ZeroExTransaction: constants.EIP712_ZEROEX_TRANSACTION_SCHEMA.parameters },
normalizedTransaction,
exchangeAddress,
);
return typedData;
},
};
9 changes: 8 additions & 1 deletion packages/order-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ 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 { EIP712_DOMAIN_NAME, EIP712_DOMAIN_SCHEMA, EIP712_DOMAIN_VERSION } from './constants';
export { constants } from './constants';
export { eip712Utils } from './eip712_utils';

export { Provider, JSONRPCRequestPayload, JSONRPCErrorCallback, JSONRPCResponsePayload } from 'ethereum-types';
export {
Expand All @@ -34,6 +35,12 @@ export {
OrderStateValid,
OrderStateInvalid,
ExchangeContractErrs,
EIP712Parameter,
EIP712TypedData,
EIP712Types,
EIP712Object,
EIP712ObjectValue,
ZeroExTransaction,
} from '@0xproject/types';
export {
OrderError,
Expand Down
43 changes: 5 additions & 38 deletions packages/order-utils/src/order_hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,10 @@ import { signTypedDataUtils } from '@0xproject/utils';
import * as _ from 'lodash';

import { assert } from './assert';
import { EIP712_DOMAIN_NAME, EIP712_DOMAIN_SCHEMA, EIP712_DOMAIN_VERSION } from './constants';
import { eip712Utils } from './eip712_utils';

const INVALID_TAKER_FORMAT = 'instance.takerAddress is not of a type(s) string';

export const EIP712_ORDER_SCHEMA = {
name: 'Order',
parameters: [
{ name: 'makerAddress', type: 'address' },
{ name: 'takerAddress', type: 'address' },
{ name: 'feeRecipientAddress', type: 'address' },
{ name: 'senderAddress', type: 'address' },
{ name: 'makerAssetAmount', type: 'uint256' },
{ name: 'takerAssetAmount', type: 'uint256' },
{ name: 'makerFee', type: 'uint256' },
{ name: 'takerFee', type: 'uint256' },
{ name: 'expirationTimeSeconds', type: 'uint256' },
{ name: 'salt', type: 'uint256' },
{ name: 'makerAssetData', type: 'bytes' },
{ name: 'takerAssetData', type: 'bytes' },
],
};

export const orderHashUtils = {
/**
* Checks if the supplied hex encoded order hash is valid.
Expand All @@ -45,7 +27,7 @@ export const orderHashUtils = {
/**
* Computes the orderHash for a supplied order.
* @param order An object that conforms to the Order or SignedOrder interface definitions.
* @return The resulting orderHash from hashing the supplied order.
* @return Hex encoded string orderHash from hashing the supplied order.
*/
getOrderHashHex(order: SignedOrder | Order): string {
try {
Expand All @@ -64,27 +46,12 @@ export const orderHashUtils = {
return orderHashHex;
},
/**
* Computes the orderHash for a supplied order and returns it as a Buffer
* Computes the orderHash for a supplied order
* @param order An object that conforms to the Order or SignedOrder interface definitions.
* @return The resulting orderHash from hashing the supplied order as a Buffer
* @return A Buffer containing the resulting orderHash from hashing the supplied order
*/
getOrderHashBuffer(order: SignedOrder | Order): Buffer {
const normalizedOrder = _.mapValues(order, value => {
return _.isObject(value) ? value.toString() : value;
});
const typedData = {
types: {
EIP712Domain: EIP712_DOMAIN_SCHEMA.parameters,
Order: EIP712_ORDER_SCHEMA.parameters,
},
domain: {
name: EIP712_DOMAIN_NAME,
version: EIP712_DOMAIN_VERSION,
verifyingContract: order.exchangeAddress,
},
message: normalizedOrder,
primaryType: EIP712_ORDER_SCHEMA.name,
};
const typedData = eip712Utils.createOrderTypedData(order);
const orderHashBuff = signTypedDataUtils.signTypedDataHash(typedData);
return orderHashBuff;
},
Expand Down
Loading

0 comments on commit 75d274f

Please sign in to comment.