Add tests for hashing libs
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"artifactsDir": "./generated-artifacts",
|
||||
"contractsDir": "./contracts",
|
||||
"useDockerisedSolc": true,
|
||||
"compilerSettings": {
|
||||
"optimizer": {
|
||||
"enabled": true,
|
||||
|
@@ -1,3 +1,3 @@
|
||||
export * from './artifacts';
|
||||
export * from './wrappers';
|
||||
// export * from '../test/utils';
|
||||
export * from '../test/utils';
|
||||
|
65
contracts/tec/test/libs.ts
Normal file
65
contracts/tec/test/libs.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { chaiSetup, constants, provider, txDefaults, web3Wrapper } from '@0x/contracts-test-utils';
|
||||
import { BlockchainLifecycle } from '@0x/dev-utils';
|
||||
import { BigNumber } from '@0x/utils';
|
||||
import * as chai from 'chai';
|
||||
|
||||
import { approvalHashUtils, artifacts, TestLibsContract } from '../src';
|
||||
|
||||
chaiSetup.configure();
|
||||
const expect = chai.expect;
|
||||
const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
|
||||
|
||||
describe('Libs tests', () => {
|
||||
let testLibs: TestLibsContract;
|
||||
|
||||
before(async () => {
|
||||
await blockchainLifecycle.startAsync();
|
||||
});
|
||||
after(async () => {
|
||||
await blockchainLifecycle.revertAsync();
|
||||
});
|
||||
before(async () => {
|
||||
testLibs = await TestLibsContract.deployFrom0xArtifactAsync(artifacts.TestLibs, provider, txDefaults);
|
||||
});
|
||||
beforeEach(async () => {
|
||||
await blockchainLifecycle.startAsync();
|
||||
});
|
||||
afterEach(async () => {
|
||||
await blockchainLifecycle.revertAsync();
|
||||
});
|
||||
|
||||
describe('getTransactionHash', () => {
|
||||
it('should return the correct transaction hash', async () => {
|
||||
const tx = {
|
||||
verifyingContractAddress: testLibs.address,
|
||||
salt: new BigNumber(0),
|
||||
signerAddress: constants.NULL_ADDRESS,
|
||||
data: '0x1234',
|
||||
};
|
||||
const expectedTxHash = approvalHashUtils._getTransactionHashHex(tx);
|
||||
const txHash = await testLibs.publicGetTransactionHash.callAsync(tx);
|
||||
expect(expectedTxHash).to.eq(txHash);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getApprovalHash', () => {
|
||||
it('should return the correct approval hash', async () => {
|
||||
const signedTx = {
|
||||
verifyingContractAddress: testLibs.address,
|
||||
salt: new BigNumber(0),
|
||||
signerAddress: constants.NULL_ADDRESS,
|
||||
data: '0x1234',
|
||||
signature: '0x5678',
|
||||
};
|
||||
const approvalExpirationTimeSeconds = new BigNumber(0);
|
||||
const approval = {
|
||||
transactionHash: approvalHashUtils._getTransactionHashHex(signedTx),
|
||||
transactionSignature: signedTx.signature,
|
||||
approvalExpirationTimeSeconds,
|
||||
};
|
||||
const expectedApprovalHash = approvalHashUtils.getApprovalHashHex(signedTx, approvalExpirationTimeSeconds);
|
||||
const approvalHash = await testLibs.publicGetTECApprovalHash.callAsync(approval);
|
||||
expect(expectedApprovalHash).to.eq(approvalHash);
|
||||
});
|
||||
});
|
||||
});
|
60
contracts/tec/test/utils/approval_hash.ts
Normal file
60
contracts/tec/test/utils/approval_hash.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { eip712Utils, EXCHANGE_ZEROEX_TRANSACTION_SCHEMA } from '@0x/order-utils';
|
||||
import { SignedZeroExTransaction, ZeroExTransaction } from '@0x/types';
|
||||
import { BigNumber, signTypedDataUtils } from '@0x/utils';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
import { constants } from './constants';
|
||||
|
||||
export const approvalHashUtils = {
|
||||
getApprovalHashBuffer(transaction: SignedZeroExTransaction, approvalExpirationTimeSeconds: BigNumber): Buffer {
|
||||
const domain = {
|
||||
name: constants.TEC_DOMAIN_NAME,
|
||||
version: constants.TEC_DOMAIN_VERSION,
|
||||
verifyingContractAddress: transaction.verifyingContractAddress,
|
||||
};
|
||||
const transactionHash = approvalHashUtils._getTransactionHashHex(transaction);
|
||||
const approval = {
|
||||
transactionHash,
|
||||
transactionSignature: transaction.signature,
|
||||
approvalExpirationTimeSeconds: approvalExpirationTimeSeconds.toString(),
|
||||
};
|
||||
const typedData = eip712Utils.createTypedData(
|
||||
constants.TEC_APPROVAL_SCHEMA.name,
|
||||
{
|
||||
TECApproval: constants.TEC_APPROVAL_SCHEMA.parameters,
|
||||
},
|
||||
approval,
|
||||
domain,
|
||||
);
|
||||
const hashBuffer = signTypedDataUtils.generateTypedDataHash(typedData);
|
||||
return hashBuffer;
|
||||
},
|
||||
getApprovalHashHex(transaction: SignedZeroExTransaction, approvalExpirationTimeSeconds: BigNumber): string {
|
||||
const hashHex = `0x${approvalHashUtils
|
||||
.getApprovalHashBuffer(transaction, approvalExpirationTimeSeconds)
|
||||
.toString('hex')}`;
|
||||
return hashHex;
|
||||
},
|
||||
_getTransactionHashBuffer(transaction: ZeroExTransaction | SignedZeroExTransaction): Buffer {
|
||||
const domain = {
|
||||
name: constants.TEC_DOMAIN_NAME,
|
||||
version: constants.TEC_DOMAIN_VERSION,
|
||||
verifyingContractAddress: transaction.verifyingContractAddress,
|
||||
};
|
||||
const normalizedTransaction = _.mapValues(transaction, value => {
|
||||
return !_.isString(value) ? value.toString() : value;
|
||||
});
|
||||
const typedData = eip712Utils.createTypedData(
|
||||
EXCHANGE_ZEROEX_TRANSACTION_SCHEMA.name,
|
||||
{ ZeroExTransaction: EXCHANGE_ZEROEX_TRANSACTION_SCHEMA.parameters },
|
||||
normalizedTransaction,
|
||||
domain,
|
||||
);
|
||||
const hashBuffer = signTypedDataUtils.generateTypedDataHash(typedData);
|
||||
return hashBuffer;
|
||||
},
|
||||
_getTransactionHashHex(transaction: ZeroExTransaction | SignedZeroExTransaction): string {
|
||||
const hashHex = `0x${approvalHashUtils._getTransactionHashBuffer(transaction).toString('hex')}`;
|
||||
return hashHex;
|
||||
},
|
||||
};
|
1
contracts/tec/test/utils/index.ts
Normal file
1
contracts/tec/test/utils/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { approvalHashUtils } from './approval_hash';
|
Reference in New Issue
Block a user