-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
eip_712.ts
253 lines (221 loc) · 6.94 KB
/
eip_712.ts
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* The web3.eth.abi functions let you encode and decode parameters to ABI (Application Binary Interface) for function calls to the EVM (Ethereum Virtual Machine).
*
* For using Web3 ABI functions, first install Web3 package using `npm i web3` or `yarn add web3`.
* After that, Web3 ABI functions will be available.
* ```ts
* import { Web3 } from 'web3';
*
* const web3 = new Web3();
* const encoded = web3.eth.abi.encodeFunctionSignature({
* name: 'myMethod',
* type: 'function',
* inputs: [{
* type: 'uint256',
* name: 'myNumber'
* },{
* type: 'string',
* name: 'myString'
* }]
* });
*
* ```
*
* For using individual package install `web3-eth-abi` package using `npm i web3-eth-abi` or `yarn add web3-eth-abi` and only import required functions.
* This is more efficient approach for building lightweight applications.
* ```ts
* import { encodeFunctionSignature } from 'web3-eth-abi';
*
* const encoded = encodeFunctionSignature({
* name: 'myMethod',
* type: 'function',
* inputs: [{
* type: 'uint256',
* name: 'myNumber'
* },{
* type: 'string',
* name: 'myString'
* }]
* });
*
* ```
*
* @module ABI
*/
// This code was taken from: https://github.com/Mrtenz/eip-712/tree/master
import { Eip712TypedData } from 'web3-types';
import { isNullish, keccak256 } from 'web3-utils';
import { AbiError } from 'web3-errors';
import { encodeParameters } from './coders/encode.js';
const TYPE_REGEX = /^\w+/;
const ARRAY_REGEX = /^(.*)\[([0-9]*?)]$/;
/**
* Get the dependencies of a struct type. If a struct has the same dependency multiple times, it's only included once
* in the resulting array.
*/
const getDependencies = (
typedData: Eip712TypedData,
type: string,
dependencies: string[] = [],
): string[] => {
const match = type.match(TYPE_REGEX)!;
const actualType = match[0];
if (dependencies.includes(actualType)) {
return dependencies;
}
if (!typedData.types[actualType]) {
return dependencies;
}
return [
actualType,
...typedData.types[actualType].reduce<string[]>(
(previous, _type) => [
...previous,
...getDependencies(typedData, _type.type, previous).filter(
dependency => !previous.includes(dependency),
),
],
[],
),
];
};
/**
* Encode a type to a string. All dependant types are alphabetically sorted.
*
* @param {TypedData} typedData
* @param {string} type
* @param {Options} [options]
* @return {string}
*/
const encodeType = (typedData: Eip712TypedData, type: string): string => {
const [primary, ...dependencies] = getDependencies(typedData, type);
// eslint-disable-next-line @typescript-eslint/require-array-sort-compare
const types = [primary, ...dependencies.sort()];
return types
.map(
dependency =>
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`${dependency}(${typedData.types[dependency].map(
_type => `${_type.type} ${_type.name}`,
)})`,
)
.join('');
};
/**
* Get a type string as hash.
*/
const getTypeHash = (typedData: Eip712TypedData, type: string) =>
keccak256(encodeType(typedData, type));
/**
* Get encoded data as a hash. The data should be a key -> value object with all the required values. All dependant
* types are automatically encoded.
*/
const getStructHash = (
typedData: Eip712TypedData,
type: string,
data: Record<string, unknown>,
// eslint-disable-next-line no-use-before-define
): string => keccak256(encodeData(typedData, type, data));
/**
* Get the EIP-191 encoded message to sign, from the typedData object. If `hash` is enabled, the message will be hashed
* with Keccak256.
*/
export const getMessage = (typedData: Eip712TypedData, hash?: boolean): string => {
const EIP_191_PREFIX = '1901';
const message = `0x${EIP_191_PREFIX}${getStructHash(
typedData,
'EIP712Domain',
typedData.domain as Record<string, unknown>,
).substring(2)}${getStructHash(typedData, typedData.primaryType, typedData.message).substring(
2,
)}`;
if (hash) {
return keccak256(message);
}
return message;
};
/**
* Encodes a single value to an ABI serialisable string, number or Buffer. Returns the data as tuple, which consists of
* an array of ABI compatible types, and an array of corresponding values.
*/
const encodeValue = (
typedData: Eip712TypedData,
type: string,
data: unknown,
): [string, string | Uint8Array | number] => {
const match = type.match(ARRAY_REGEX);
// Checks for array types
if (match) {
const arrayType = match[1];
const length = Number(match[2]) || undefined;
if (!Array.isArray(data)) {
throw new AbiError('Cannot encode data: value is not of array type', {
data,
});
}
if (length && data.length !== length) {
throw new AbiError(
`Cannot encode data: expected length of ${length}, but got ${data.length}`,
{
data,
},
);
}
const encodedData = data.map(item => encodeValue(typedData, arrayType, item));
const types = encodedData.map(item => item[0]);
const values = encodedData.map(item => item[1]);
return ['bytes32', keccak256(encodeParameters(types, values))];
}
if (typedData.types[type]) {
return ['bytes32', getStructHash(typedData, type, data as Record<string, unknown>)];
}
// Strings and arbitrary byte arrays are hashed to bytes32
if (type === 'string') {
return ['bytes32', keccak256(data as string)];
}
if (type === 'bytes') {
return ['bytes32', keccak256(data as string)];
}
return [type, data as string];
};
/**
* Encode the data to an ABI encoded Buffer. The data should be a key -> value object with all the required values. All
* dependant types are automatically encoded.
*/
const encodeData = (
typedData: Eip712TypedData,
type: string,
data: Record<string, unknown>,
): string => {
const [types, values] = typedData.types[type].reduce<[string[], unknown[]]>(
([_types, _values], field) => {
if (isNullish(data[field.name]) || isNullish(data[field.name])) {
throw new AbiError(`Cannot encode data: missing data for '${field.name}'`, {
data,
field,
});
}
const value = data[field.name];
const [_type, encodedValue] = encodeValue(typedData, field.type, value);
return [
[..._types, _type],
[..._values, encodedValue],
];
},
[['bytes32'], [getTypeHash(typedData, type)]],
);
return encodeParameters(types, values);
};