aboutsummaryrefslogtreecommitdiffstats
path: root/packages/order-utils/test/eip712_utils_test.ts
blob: 4208e9beb43f2e5768bb7b51e5adf28a955bd44e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { BigNumber } from '@0x/utils';
import * as chai from 'chai';
import 'mocha';

import { constants } from '../src/constants';
import {
    eip712Utils,
    EXCHANGE_DOMAIN_NAME,
    EXCHANGE_DOMAIN_VERSION,
    EXCHANGE_ZEROEX_TRANSACTION_SCHEMA,
} from '../src/eip712_utils';

import { chaiSetup } from './utils/chai_setup';

chaiSetup.configure();
const expect = chai.expect;

describe('EIP712 Utils', () => {
    describe('createTypedData', () => {
        it('adds in the EIP712DomainSeparator with default values', () => {
            const primaryType = 'Test';
            const typedData = eip712Utils.createTypedData(
                primaryType,
                { Test: [{ name: 'testValue', type: 'uint256' }] },
                { testValue: '1' },
                { verifyingContractAddress: constants.NULL_ADDRESS },
            );
            expect(typedData.domain).to.not.be.undefined();
            expect(typedData.types.EIP712Domain).to.not.be.undefined();
            const domainObject = typedData.domain;
            expect(domainObject.name).to.eq(EXCHANGE_DOMAIN_NAME);
            expect(domainObject.version).to.eq(EXCHANGE_DOMAIN_VERSION);
            expect(domainObject.verifyingContract).to.eq(constants.NULL_ADDRESS);
            expect(typedData.primaryType).to.eq(primaryType);
        });
        it('adds in the EIP712DomainSeparator without default values', () => {
            const primaryType = 'Test';
            const domainName = 'testDomain';
            const domainVersion = 'testVersion';
            const typedData = eip712Utils.createTypedData(
                primaryType,
                { Test: [{ name: 'testValue', type: 'uint256' }] },
                { testValue: '1' },
                { name: domainName, version: domainVersion, verifyingContractAddress: constants.NULL_ADDRESS },
            );
            expect(typedData.domain).to.not.be.undefined();
            expect(typedData.types.EIP712Domain).to.not.be.undefined();
            const domainObject = typedData.domain;
            expect(domainObject.name).to.eq(domainName);
            expect(domainObject.version).to.eq(domainVersion);
            expect(domainObject.verifyingContract).to.eq(constants.NULL_ADDRESS);
            expect(typedData.primaryType).to.eq(primaryType);
        });
    });
    describe('createZeroExTransactionTypedData', () => {
        it('adds in the EIP712DomainSeparator', () => {
            const typedData = eip712Utils.createZeroExTransactionTypedData({
                salt: new BigNumber('0'),
                data: constants.NULL_BYTES,
                signerAddress: constants.NULL_ADDRESS,
                verifyingContractAddress: constants.NULL_ADDRESS,
            });
            expect(typedData.primaryType).to.eq(EXCHANGE_ZEROEX_TRANSACTION_SCHEMA.name);
            expect(typedData.types.EIP712Domain).to.not.be.undefined();
            const domainObject = typedData.domain;
            expect(domainObject.name).to.eq(EXCHANGE_DOMAIN_NAME);
            expect(domainObject.version).to.eq(EXCHANGE_DOMAIN_VERSION);
            expect(domainObject.verifyingContract).to.eq(constants.NULL_ADDRESS);
        });
    });
});