@0x/order-utils refactors for v3: orderParsingUtils, signatureUtils, orderHashUtils, RevertErrors, transactionHashUtils (#2321)

* move orderParsingUtils from order-utils to connect

* Remove many functions from signatureUtils

Removed from the exported object, that is.  All of them are used in
other existing code, so they were all moved to be as local to their
usage as possible.

* remove orderHashUtils.isValidOrderHash()

* Move all *RevertErrors from order-utils...

...into their respective @0x/contracts- packages.

* Refactor @0x/order-utils' orderHashUtils away

- Move existing routines into @0x/contracts-test-utils

- Migrate non-contract-test callers to a newly-exposed getOrderHash()
method in DevUtils.

* Move all *RevertErrors from @0x/utils...

...into their respective @0x/contracts- packages.

* rm transactionHashUtils.isValidTransactionHash()

* DevUtils.sol: Fail yarn test if too big to deploy

* Refactor @0x/order-utils transactionHashUtils away

- Move existing routines into @0x/contracts-test-utils

- Migrate non-contract-test callers to a newly-exposed
getTransactionHash() method in DevUtils.

* Consolidate `Removed export...` CHANGELOG entries

* Rm EthBalanceChecker from devutils wrapper exports

* Stop importing from '.' or '.../src'

* fix builds

* fix prettier; dangling promise

* increase max bundle size
This commit is contained in:
F. Eugene Aumson
2019-11-14 17:14:24 -05:00
committed by GitHub
parent f0d7d10fe7
commit f11d8a5bd8
132 changed files with 584 additions and 653 deletions

View File

@@ -6,6 +6,10 @@
"note": "Drastically reduced bundle size by adding .npmignore, only exporting specific artifacts/wrappers/utils",
"pr": 2330
},
{
"note": "Add new exports orderHashUtils and transactionHashUtils",
"pr": 2321
},
{
"note": "Remove TransactionHelper and MutatorContractFunction",
"pr": 2325

View File

@@ -42,9 +42,11 @@
"typescript": "3.0.1"
},
"dependencies": {
"@0x/assert": "2.2.0-beta.1",
"@0x/dev-utils": "2.4.0-beta.1",
"@0x/base-contract": "^5.5.0-beta.0",
"@0x/dev-utils": "^2.4.0-beta.1",
"@0x/order-utils": "^8.5.0-beta.1",
"@0x/json-schemas": "4.1.0-beta.1",
"@0x/sol-coverage": "^3.1.0-beta.1",
"@0x/sol-profiler": "^3.2.0-beta.1",
"@0x/sol-trace": "^2.1.0-beta.1",

View File

@@ -72,3 +72,5 @@ export {
getPercentageOfValue,
toBaseUnitAmount,
} from './number_utils';
export { orderHashUtils } from './order_hash';
export { transactionHashUtils } from './transaction_hash';

View File

@@ -1,9 +1,10 @@
import { generatePseudoRandomSalt, orderHashUtils } from '@0x/order-utils';
import { generatePseudoRandomSalt } from '@0x/order-utils';
import { Order, SignatureType, SignedOrder } from '@0x/types';
import { BigNumber } from '@0x/utils';
import { getLatestBlockTimestampAsync } from './block_timestamp';
import { constants } from './constants';
import { orderHashUtils } from './order_hash';
import { signingUtils } from './signing_utils';
export class OrderFactory {

View File

@@ -0,0 +1,52 @@
import { assert } from '@0x/assert';
import { schemas } from '@0x/json-schemas';
import { eip712Utils } from '@0x/order-utils';
import { Order, SignedOrder } from '@0x/types';
import { signTypedDataUtils } from '@0x/utils';
import * as _ from 'lodash';
const INVALID_TAKER_FORMAT = 'instance.takerAddress is not of a type(s) string';
const NULL_ADDRESS = '0x0000000000000000000000000000000000000000';
export const orderHashUtils = {
/**
* Computes the orderHash for a supplied order.
* @param order An object that conforms to the Order or SignedOrder interface definitions.
* @return Hex encoded string orderHash from hashing the supplied order.
*/
getOrderHashHex(order: SignedOrder | Order): string {
try {
assert.doesConformToSchema('order', order, schemas.orderSchema, [schemas.hexSchema]);
} catch (error) {
if (_.includes(error.message, INVALID_TAKER_FORMAT)) {
const errMsg = `Order taker must be of type string. If you want anyone to be able to fill an order - pass ${NULL_ADDRESS}`;
throw new Error(errMsg);
}
throw error;
}
const orderHashBuff = orderHashUtils.getOrderHashBuffer(order);
const orderHashHex = `0x${orderHashBuff.toString('hex')}`;
return orderHashHex;
},
/**
* Computes the orderHash for a supplied order
* @param order An object that conforms to the Order or SignedOrder interface definitions.
* @return A Buffer containing the resulting orderHash from hashing the supplied order
*/
getOrderHashBuffer(order: SignedOrder | Order): Buffer {
try {
assert.doesConformToSchema('order', order, schemas.orderSchema, [schemas.hexSchema]);
} catch (error) {
if (_.includes(error.message, INVALID_TAKER_FORMAT)) {
const errMsg = `Order taker must be of type string. If you want anyone to be able to fill an order - pass ${NULL_ADDRESS}`;
throw new Error(errMsg);
}
throw error;
}
const typedData = eip712Utils.createOrderTypedData(order);
const orderHashBuff = signTypedDataUtils.generateTypedDataHash(typedData);
return orderHashBuff;
},
};

View File

@@ -1,8 +1,10 @@
import { generatePseudoRandomSalt, transactionHashUtils } from '@0x/order-utils';
import { generatePseudoRandomSalt } from '@0x/order-utils';
import { SignatureType, SignedZeroExTransaction, ZeroExTransaction } from '@0x/types';
import { BigNumber } from '@0x/utils';
import * as ethUtil from 'ethereumjs-util';
import { transactionHashUtils } from '../src';
import { getLatestBlockTimestampAsync } from './block_timestamp';
import { constants } from './constants';
import { signingUtils } from './signing_utils';

View File

@@ -0,0 +1,30 @@
import { assert } from '@0x/assert';
import { schemas } from '@0x/json-schemas';
import { eip712Utils } from '@0x/order-utils';
import { SignedZeroExTransaction, ZeroExTransaction } from '@0x/types';
import { signTypedDataUtils } from '@0x/utils';
import * as _ from 'lodash';
export const transactionHashUtils = {
/**
* Computes the transactionHash for a supplied 0x transaction.
* @param transaction An object that conforms to the ZeroExTransaction or SignedZeroExTransaction interface definitions.
* @return Hex encoded string transactionHash from hashing the supplied order.
*/
getTransactionHashHex(transaction: ZeroExTransaction | SignedZeroExTransaction): string {
assert.doesConformToSchema('transaction', transaction, schemas.zeroExTransactionSchema, [schemas.hexSchema]);
const transactionHashBuff = transactionHashUtils.getTransactionHashBuffer(transaction);
const transactionHashHex = `0x${transactionHashBuff.toString('hex')}`;
return transactionHashHex;
},
/**
* Computes the transactionHash for a supplied 0x transaction.
* @param transaction An object that conforms to the ZeroExTransaction or SignedZeroExTransaction interface definitions.
* @return A Buffer containing the resulting transactionHash from hashing the supplied 0x transaction.
*/
getTransactionHashBuffer(transaction: ZeroExTransaction | SignedZeroExTransaction): Buffer {
const typedData = eip712Utils.createZeroExTransactionTypedData(transaction);
const transactionHashBuff = signTypedDataUtils.generateTypedDataHash(typedData);
return transactionHashBuff;
},
};

View File

@@ -0,0 +1,66 @@
import { chaiSetup } from '@0x/dev-utils';
import { Order } from '@0x/types';
import { BigNumber } from '@0x/utils';
import * as chai from 'chai';
import 'mocha';
import { orderHashUtils } from '../src';
import { constants } from '../src/constants';
chaiSetup.configure();
const expect = chai.expect;
describe('Order hashing', () => {
describe('#getOrderHashHex', () => {
const expectedOrderHash = '0x331cb7e07a757bae130702da6646c26531798c92bcfaf671817268fd2c188531';
const fakeExchangeContractAddress = '0x1dc4c1cefef38a777b15aa20260a54e584b16c48';
const fakeChainID = 50;
const order: Order = {
makerAddress: constants.NULL_ADDRESS,
takerAddress: constants.NULL_ADDRESS,
senderAddress: constants.NULL_ADDRESS,
feeRecipientAddress: constants.NULL_ADDRESS,
makerAssetData: constants.NULL_ADDRESS,
takerAssetData: constants.NULL_ADDRESS,
makerFeeAssetData: constants.NULL_ADDRESS,
takerFeeAssetData: constants.NULL_ADDRESS,
salt: new BigNumber(0),
makerFee: new BigNumber(0),
takerFee: new BigNumber(0),
makerAssetAmount: new BigNumber(0),
takerAssetAmount: new BigNumber(0),
expirationTimeSeconds: new BigNumber(0),
exchangeAddress: fakeExchangeContractAddress,
chainId: fakeChainID,
};
it('calculates the order hash', async () => {
const orderHash = orderHashUtils.getOrderHashHex(order);
expect(orderHash).to.be.equal(expectedOrderHash);
});
it('calculates the order hash if amounts are strings', async () => {
// It's common for developers using javascript to provide the amounts
// as strings. Since we eventually toString() the BigNumber
// before encoding we should result in the same orderHash in this scenario
// tslint:disable-next-line:no-unnecessary-type-assertion
const orderHash = orderHashUtils.getOrderHashHex({
...order,
makerAssetAmount: '0',
takerAssetAmount: '0',
makerFee: '0',
takerFee: '0',
} as any);
expect(orderHash).to.be.equal(expectedOrderHash);
});
it('throws a readable error message if taker format is invalid', async () => {
const orderWithInvalidtakerFormat = {
...order,
takerAddress: (null as any) as string,
};
const expectedErrorMessage = `Order taker must be of type string. If you want anyone to be able to fill an order - pass ${
constants.NULL_ADDRESS
}`;
expect(() => orderHashUtils.getOrderHashHex(orderWithInvalidtakerFormat)).to.throw(expectedErrorMessage);
});
});
});

View File

@@ -0,0 +1,48 @@
import { chaiSetup } from '@0x/dev-utils';
import { ZeroExTransaction } from '@0x/types';
import { BigNumber } from '@0x/utils';
import * as chai from 'chai';
import 'mocha';
import { transactionHashUtils } from '../src';
import { constants } from '../src/constants';
chaiSetup.configure();
const expect = chai.expect;
describe('0x transaction hashing', () => {
describe('#getTransactionHashHex', () => {
const expectedTransactionHash = '0x7845d260300acfbebaff52f0462f984016473290b9eb865fb6ffac0503cab364';
const fakeVerifyingContractAddress = '0x5e72914535f202659083db3a02c984188fa26e9f';
const fakeChainId = 1337;
const transaction: ZeroExTransaction = {
signerAddress: constants.NULL_ADDRESS,
salt: new BigNumber(0),
expirationTimeSeconds: new BigNumber(0),
gasPrice: new BigNumber(0),
data: constants.NULL_BYTES,
domain: {
verifyingContract: fakeVerifyingContractAddress,
chainId: fakeChainId,
},
};
it('calculates the transaction hash', async () => {
const transactionHash = transactionHashUtils.getTransactionHashHex(transaction);
expect(transactionHash).to.be.equal(expectedTransactionHash);
});
it('calculates the transaction hash if amounts are strings', async () => {
// It's common for developers using javascript to provide the amounts
// as strings. Since we eventually toString() the BigNumber
// before encoding we should result in the same orderHash in this scenario
// tslint:disable-next-line:no-unnecessary-type-assertion
const transactionHash = transactionHashUtils.getTransactionHashHex({
...transaction,
salt: '0',
expirationTimeSeconds: '0',
gasPrice: '0',
} as any);
expect(transactionHash).to.be.equal(expectedTransactionHash);
});
});
});