Add more LibEIP712 tests to exchange-libs

This commit is contained in:
Lawrence Forman
2019-03-28 12:09:12 -04:00
committed by Amir Bandeali
parent b380952ff9
commit f7fb1225d2
5 changed files with 82 additions and 9 deletions

View File

@@ -9,13 +9,17 @@ import {
} from '@0x/contracts-test-utils';
import { BlockchainLifecycle } from '@0x/dev-utils';
import { assetDataUtils, orderHashUtils } from '@0x/order-utils';
import { constants as orderConstants } from '@0x/order-utils/lib/src/constants';
import { SignedOrder } from '@0x/types';
import { BigNumber, providerUtils } from '@0x/utils';
import * as chai from 'chai';
import * as ethUtil from 'ethereumjs-util';
import { TestLibsContract } from '../generated-wrappers/test_libs';
import { artifacts } from '../src/artifacts';
import { stringifySchema } from './utils';
chaiSetup.configure();
const expect = chai.expect;
@@ -26,6 +30,7 @@ describe('Exchange libs', () => {
let signedOrder: SignedOrder;
let orderFactory: OrderFactory;
let libs: TestLibsContract;
let libsAlternateChain: TestLibsContract;
before(async () => {
await blockchainLifecycle.startAsync();
@@ -43,6 +48,14 @@ describe('Exchange libs', () => {
txDefaults,
new BigNumber(chainId),
);
// Deploy a version with a different chain ID.
const alternateChainId = chainId + 1;
libsAlternateChain = await TestLibsContract.deployFrom0xArtifactAsync(
artifacts.TestLibs,
provider,
txDefaults,
new BigNumber(alternateChainId),
);
const defaultOrderParams = {
...constants.STATIC_ORDER_PARAMS,
@@ -129,19 +142,45 @@ describe('Exchange libs', () => {
expect(orderHashUtils.getOrderHashHex(signedOrder)).to.be.equal(orderHashHex);
});
it('orderHash should differ if chainId is different', async () => {
// Deploy a version of the contract with a different chain ID.
const newChainId = chainId + 1;
const _libs = await TestLibsContract.deployFrom0xArtifactAsync(
artifacts.TestLibs,
provider,
txDefaults,
new BigNumber(newChainId),
);
signedOrder = await orderFactory.newSignedOrderAsync();
const orderHashHex1 = await _libs.publicGetOrderHash.callAsync(signedOrder);
const orderHashHex1 = await libsAlternateChain.publicGetOrderHash.callAsync(signedOrder);
const orderHashHex2 = await libs.publicGetOrderHash.callAsync(signedOrder);
expect(orderHashHex1).to.be.not.equal(orderHashHex2);
});
});
});
describe('LibEIP712', () => {
it('should return the correct domain separator schema hash', async () => {
const schema = stringifySchema(orderConstants.DEFAULT_DOMAIN_SCHEMA);
const expectedSchemaHash = ethUtil.bufferToHex(ethUtil.sha3(Buffer.from(schema)));
const actualSchemaHash = await libs.getDomainSeparatorSchemaHash.callAsync();
expect(actualSchemaHash).to.be.equal(expectedSchemaHash);
});
it('should return the correct order schema hash', async () => {
const schema = stringifySchema(orderConstants.EXCHANGE_ORDER_SCHEMA);
const expectedSchemaHash = ethUtil.bufferToHex(ethUtil.sha3(Buffer.from(schema)));
const actualSchemaHash = await libs.getOrderSchemaHash.callAsync();
expect(actualSchemaHash).to.be.equal(expectedSchemaHash);
});
it('should return the correct domain separator', async () => {
const schema = stringifySchema(orderConstants.DEFAULT_DOMAIN_SCHEMA);
const schemaHash = ethUtil.sha3(Buffer.from(schema));
const payload = Buffer.concat([
schemaHash,
ethUtil.sha3(Buffer.from(orderConstants.EXCHANGE_DOMAIN_NAME)),
ethUtil.sha3(Buffer.from(orderConstants.EXCHANGE_DOMAIN_VERSION)),
ethUtil.setLengthLeft(ethUtil.toBuffer(chainId), 32),
ethUtil.setLengthLeft(ethUtil.toBuffer(libs.address), 32),
]);
const expectedDomain = ethUtil.bufferToHex(ethUtil.sha3(payload));
const actualDomain = await libs.getDomainSeparator.callAsync();
expect(actualDomain).to.be.equal(expectedDomain);
});
it('should return the a different domain separator if chainId is different', async () => {
const domain1 = await libsAlternateChain.getDomainSeparator.callAsync();
const domain2 = await libs.getDomainSeparator.callAsync();
expect(domain1).to.be.not.equal(domain2);
});
});
});

View File

@@ -0,0 +1 @@
export { SchemaDefinition, SchemaParameterDefinition, stringifySchema } from './schema';

View File

@@ -0,0 +1,24 @@
import * as _ from 'lodash';
export interface SchemaParameterDefinition {
name: string;
type: string;
}
export interface SchemaDefinition {
name: string;
parameters: SchemaParameterDefinition[];
}
/**
* Convert a schema definition into a function string.
* @param The schema definition.
* @return The function string of the schema definition..
*/
export function stringifySchema(schema: SchemaDefinition): string {
const parameters = [];
for (const parameter of schema.parameters) {
parameters.push(`${parameter.type} ${parameter.name}`);
}
return `${schema.name}(${parameters.join(',')})`;
}