aboutsummaryrefslogtreecommitdiffstats
path: root/packages/utils/src/sign_typed_data_utils.ts
blob: b72fd099b935036f8f3d3b8e9089800dff6c0892 (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
72
73
import * as ethUtil from 'ethereumjs-util';
import * as ethers from 'ethers';

import { EIP712TypedData, EIP712Types } from '@0xproject/types';

export const signTypedDataUtils = {
    /**
     * Computes the Sign Typed Data hash
     * @param   typedData An object that conforms to the EIP712TypedData interface
     * @return  A Buffer containing the hash of the sign typed data.
     */
    signTypedDataHash(typedData: EIP712TypedData): Buffer {
        return ethUtil.sha3(
            Buffer.concat([
                Buffer.from('1901', 'hex'),
                signTypedDataUtils._structHash('EIP712Domain', typedData.domain, typedData.types),
                signTypedDataUtils._structHash(typedData.primaryType, typedData.message, typedData.types),
            ]),
        );
    },
    _findDependencies(primaryType: string, types: EIP712Types, found: string[] = []): string[] {
        if (found.includes(primaryType) || types[primaryType] === undefined) {
            return found;
        }
        found.push(primaryType);
        for (const field of types[primaryType]) {
            for (const dep of signTypedDataUtils._findDependencies(field.type, types, found)) {
                if (!found.includes(dep)) {
                    found.push(dep);
                }
            }
        }
        return found;
    },
    _encodeType(primaryType: string, types: EIP712Types): string {
        let deps = signTypedDataUtils._findDependencies(primaryType, types);
        deps = deps.filter(d => d !== primaryType);
        deps = [primaryType].concat(deps.sort());
        let result = '';
        for (const dep of deps) {
            result += `${dep}(${types[dep].map(({ name, type }) => `${type} ${name}`).join(',')})`;
        }
        return result;
    },
    _encodeData(primaryType: string, data: any, types: EIP712Types): string {
        const encodedTypes = ['bytes32'];
        const encodedValues = [signTypedDataUtils._typeHash(primaryType, types)];
        for (const field of types[primaryType]) {
            let value = data[field.name];
            if (field.type === 'string' || field.type === 'bytes') {
                value = ethUtil.sha3(value);
                encodedTypes.push('bytes32');
                encodedValues.push(value);
            } else if (types[field.type] !== undefined) {
                encodedTypes.push('bytes32');
                value = ethUtil.sha3(signTypedDataUtils._encodeData(field.type, value, types));
                encodedValues.push(value);
            } else if (field.type.lastIndexOf(']') === field.type.length - 1) {
                throw new Error('Arrays currently unimplemented in encodeData');
            } else {
                encodedTypes.push(field.type);
                encodedValues.push(value);
            }
        }
        return ethers.utils.defaultAbiCoder.encode(encodedTypes, encodedValues);
    },
    _typeHash(primaryType: string, types: EIP712Types): Buffer {
        return ethUtil.sha3(signTypedDataUtils._encodeType(primaryType, types));
    },
    _structHash(primaryType: string, data: any, types: EIP712Types): Buffer {
        return ethUtil.sha3(signTypedDataUtils._encodeData(primaryType, data, types));
    },
};