* 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
145 lines
5.5 KiB
TypeScript
145 lines
5.5 KiB
TypeScript
import {
|
|
blockchainTests,
|
|
constants,
|
|
describe,
|
|
expect,
|
|
hexRandom,
|
|
transactionHashUtils,
|
|
} from '@0x/contracts-test-utils';
|
|
import { eip712Utils } from '@0x/order-utils';
|
|
import { ZeroExTransaction } from '@0x/types';
|
|
import { BigNumber, signTypedDataUtils } from '@0x/utils';
|
|
import * as ethUtil from 'ethereumjs-util';
|
|
import * as _ from 'lodash';
|
|
|
|
import { TestLibZeroExTransactionContract } from './wrappers';
|
|
|
|
import { artifacts } from './artifacts';
|
|
|
|
blockchainTests('LibZeroExTransaction', env => {
|
|
let libZeroExTransactionContract: TestLibZeroExTransactionContract;
|
|
|
|
const randomAddress = () => hexRandom(constants.ADDRESS_LENGTH);
|
|
const randomHash = () => hexRandom(constants.WORD_LENGTH);
|
|
const randomUint256 = () => new BigNumber(randomHash());
|
|
const randomAssetData = () => hexRandom(36);
|
|
|
|
const EMPTY_TRANSACTION: ZeroExTransaction = {
|
|
salt: constants.ZERO_AMOUNT,
|
|
expirationTimeSeconds: constants.ZERO_AMOUNT,
|
|
gasPrice: constants.ZERO_AMOUNT,
|
|
signerAddress: constants.NULL_ADDRESS,
|
|
data: constants.NULL_BYTES,
|
|
domain: {
|
|
verifyingContract: constants.NULL_ADDRESS,
|
|
chainId: 0,
|
|
},
|
|
};
|
|
|
|
before(async () => {
|
|
libZeroExTransactionContract = await TestLibZeroExTransactionContract.deployFrom0xArtifactAsync(
|
|
artifacts.TestLibZeroExTransaction,
|
|
env.provider,
|
|
env.txDefaults,
|
|
{},
|
|
);
|
|
});
|
|
|
|
/**
|
|
* Tests the `getTypedDataHash()` function against a reference hash.
|
|
*/
|
|
async function testGetTypedDataHashAsync(transaction: ZeroExTransaction): Promise<void> {
|
|
const expectedHash = transactionHashUtils.getTransactionHashHex(transaction);
|
|
const domainHash = ethUtil.bufferToHex(
|
|
signTypedDataUtils.generateDomainHash({
|
|
...transaction.domain,
|
|
name: constants.EIP712_DOMAIN_NAME,
|
|
version: constants.EIP712_DOMAIN_VERSION,
|
|
}),
|
|
);
|
|
const actualHash = await libZeroExTransactionContract.getTypedDataHash(transaction, domainHash).callAsync();
|
|
expect(actualHash).to.be.eq(expectedHash);
|
|
}
|
|
|
|
describe('getTypedDataHash', () => {
|
|
it('should correctly hash an empty transaction', async () => {
|
|
await testGetTypedDataHashAsync({
|
|
...EMPTY_TRANSACTION,
|
|
domain: {
|
|
...EMPTY_TRANSACTION.domain,
|
|
verifyingContract: libZeroExTransactionContract.address,
|
|
},
|
|
});
|
|
});
|
|
|
|
it('should correctly hash a non-empty transaction', async () => {
|
|
await testGetTypedDataHashAsync({
|
|
salt: randomUint256(),
|
|
expirationTimeSeconds: randomUint256(),
|
|
gasPrice: randomUint256(),
|
|
signerAddress: randomAddress(),
|
|
data: randomAssetData(),
|
|
domain: {
|
|
...EMPTY_TRANSACTION.domain,
|
|
verifyingContract: libZeroExTransactionContract.address,
|
|
},
|
|
});
|
|
});
|
|
it('transactionHash should differ if the domain hash is different', async () => {
|
|
const domainHash1 = ethUtil.bufferToHex(
|
|
signTypedDataUtils.generateDomainHash({
|
|
...EMPTY_TRANSACTION.domain,
|
|
name: constants.EIP712_DOMAIN_NAME,
|
|
version: constants.EIP712_DOMAIN_VERSION,
|
|
}),
|
|
);
|
|
const domainHash2 = ethUtil.bufferToHex(
|
|
signTypedDataUtils.generateDomainHash({
|
|
...EMPTY_TRANSACTION.domain,
|
|
name: constants.EIP712_DOMAIN_NAME,
|
|
version: constants.EIP712_DOMAIN_VERSION,
|
|
chainId: 1337,
|
|
}),
|
|
);
|
|
const transactionHashHex1 = await libZeroExTransactionContract
|
|
.getTypedDataHash(EMPTY_TRANSACTION, domainHash1)
|
|
.callAsync();
|
|
const transactionHashHex2 = await libZeroExTransactionContract
|
|
.getTypedDataHash(EMPTY_TRANSACTION, domainHash2)
|
|
.callAsync();
|
|
expect(transactionHashHex1).to.be.not.equal(transactionHashHex2);
|
|
});
|
|
});
|
|
|
|
/**
|
|
* Tests the `getStructHash()` function against a reference hash.
|
|
*/
|
|
async function testGetStructHashAsync(transaction: ZeroExTransaction): Promise<void> {
|
|
const typedData = eip712Utils.createZeroExTransactionTypedData(transaction);
|
|
const expectedHash = ethUtil.bufferToHex(signTypedDataUtils.generateTypedDataHashWithoutDomain(typedData));
|
|
const actualHash = await libZeroExTransactionContract.getStructHash(transaction).callAsync();
|
|
expect(actualHash).to.be.eq(expectedHash);
|
|
}
|
|
|
|
describe('getStructHash', () => {
|
|
it('should correctly hash an empty transaction', async () => {
|
|
await testGetStructHashAsync(EMPTY_TRANSACTION);
|
|
});
|
|
|
|
it('should correctly hash a non-empty transaction', async () => {
|
|
await testGetStructHashAsync({
|
|
salt: randomUint256(),
|
|
expirationTimeSeconds: randomUint256(),
|
|
gasPrice: randomUint256(),
|
|
signerAddress: randomAddress(),
|
|
data: randomAssetData(),
|
|
// The domain is not used in this test, so it's okay if it is left empty.
|
|
domain: {
|
|
verifyingContract: constants.NULL_ADDRESS,
|
|
chainId: 0,
|
|
},
|
|
});
|
|
});
|
|
});
|
|
});
|