Add tests for hashing libs

This commit is contained in:
Amir Bandeali
2019-02-04 10:51:05 -08:00
parent a52686ca3b
commit e67e822845
6 changed files with 138 additions and 1 deletions

View 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);
});
});
});

View 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;
},
};

View File

@@ -0,0 +1 @@
export { approvalHashUtils } from './approval_hash';