-
Notifications
You must be signed in to change notification settings - Fork 777
/
Copy pathtypedData.ts
397 lines (359 loc) · 11.9 KB
/
typedData.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* eslint-disable no-param-reassign */
import {
BigNumberish,
TypedDataRevision as Revision,
StarkNetEnumType,
StarkNetMerkleType,
StarkNetType,
TypedData,
} from '../types';
import {
computePedersenHash,
computePedersenHashOnElements,
computePoseidonHash,
computePoseidonHashOnElements,
getSelectorFromName,
} from './hash';
import { MerkleTree } from './merkle';
import { isHex, toHex } from './num';
import { encodeShortString, splitLongString } from './shortString';
/** @deprecated prefer importing from 'types' over 'typedData' */
export * from '../types/typedData';
interface Context {
parent?: string;
key?: string;
}
interface Configuration {
domain: string;
hashMethod: (data: BigNumberish[]) => string;
hashMerkleMethod: (a: BigNumberish, b: BigNumberish) => string;
escapeTypeString: (s: string) => string;
presetTypes: TypedData['types'];
}
const presetTypes: TypedData['types'] = {
u256: JSON.parse('[{ "name": "low", "type": "u128" }, { "name": "high", "type": "u128" }]'),
TokenAmount: JSON.parse(
'[{ "name": "token_address", "type": "ContractAddress" }, { "name": "amount", "type": "u256" }]'
),
NftId: JSON.parse(
'[{ "name": "collection_address", "type": "ContractAddress" }, { "name": "token_id", "type": "u256" }]'
),
};
const revisionConfiguration: Record<Revision, Configuration> = {
[Revision.Active]: {
domain: 'StarknetDomain',
hashMethod: computePoseidonHashOnElements,
hashMerkleMethod: computePoseidonHash,
escapeTypeString: (s) => `"${s}"`,
presetTypes,
},
[Revision.Legacy]: {
domain: 'StarkNetDomain',
hashMethod: computePedersenHashOnElements,
hashMerkleMethod: computePedersenHash,
escapeTypeString: (s) => s,
presetTypes: {},
},
};
// TODO: replace with utils byteArrayFromString from PR#891 once it is available
export function byteArrayFromString(targetString: string) {
const shortStrings: string[] = splitLongString(targetString);
const remainder: string = shortStrings[shortStrings.length - 1];
const shortStringsEncoded: BigNumberish[] = shortStrings.map(encodeShortString);
const [pendingWord, pendingWordLength] =
remainder === undefined || remainder.length === 31
? ['0x00', 0]
: [shortStringsEncoded.pop()!, remainder.length];
return {
data: shortStringsEncoded.length === 0 ? ['0x00'] : shortStringsEncoded,
pending_word: pendingWord,
pending_word_len: pendingWordLength,
};
}
function identifyRevision({ types, domain }: TypedData) {
if (revisionConfiguration[Revision.Active].domain in types && domain.revision === Revision.Active)
return Revision.Active;
if (
revisionConfiguration[Revision.Legacy].domain in types &&
(domain.revision ?? Revision.Legacy) === Revision.Legacy
)
return Revision.Legacy;
return undefined;
}
function getHex(value: BigNumberish): string {
try {
return toHex(value);
} catch (e) {
if (typeof value === 'string') {
return toHex(encodeShortString(value));
}
throw new Error(`Invalid BigNumberish: ${value}`);
}
}
/**
* Validates that `data` matches the EIP-712 JSON schema.
*/
function validateTypedData(data: unknown): data is TypedData {
const typedData = data as TypedData;
return Boolean(
typedData.message && typedData.primaryType && typedData.types && identifyRevision(typedData)
);
}
export function prepareSelector(selector: string): string {
return isHex(selector) ? selector : getSelectorFromName(selector);
}
export function isMerkleTreeType(type: StarkNetType): type is StarkNetMerkleType {
return type.type === 'merkletree';
}
/**
* 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.
*/
export function getDependencies(
types: TypedData['types'],
type: string,
dependencies: string[] = [],
contains: string = '',
revision: Revision = Revision.Legacy
): string[] {
// Include pointers (struct arrays)
if (type[type.length - 1] === '*') {
type = type.slice(0, -1);
} else if (revision === Revision.Active) {
// enum base
if (type === 'enum') {
type = contains;
}
// enum element types
else if (type.match(/^\(.*\)$/)) {
type = type.slice(1, -1);
}
}
if (dependencies.includes(type) || !types[type]) {
return dependencies;
}
return [
type,
...(types[type] as StarkNetEnumType[]).reduce<string[]>(
(previous, t) => [
...previous,
...getDependencies(types, t.type, previous, t.contains, revision).filter(
(dependency) => !previous.includes(dependency)
),
],
[]
),
];
}
function getMerkleTreeType(types: TypedData['types'], ctx: Context) {
if (ctx.parent && ctx.key) {
const parentType = types[ctx.parent];
const merkleType = parentType.find((t) => t.name === ctx.key)!;
const isMerkleTree = isMerkleTreeType(merkleType);
if (!isMerkleTree) {
throw new Error(`${ctx.key} is not a merkle tree`);
}
if (merkleType.contains.endsWith('*')) {
throw new Error(`Merkle tree contain property must not be an array but was given ${ctx.key}`);
}
return merkleType.contains;
}
return 'raw';
}
/**
* Encode a type to a string. All dependent types are alphabetically sorted.
*/
export function encodeType(
types: TypedData['types'],
type: string,
revision: Revision = Revision.Legacy
): string {
const [primary, ...dependencies] = getDependencies(types, type, undefined, undefined, revision);
const newTypes = !primary ? [] : [primary, ...dependencies.sort()];
const esc = revisionConfiguration[revision].escapeTypeString;
return newTypes
.map((dependency) => {
const dependencyElements = types[dependency].map((t) => {
const targetType =
t.type === 'enum' && revision === Revision.Active
? (t as StarkNetEnumType).contains
: t.type;
// parentheses handling for enum variant types
const typeString = targetType.match(/^\(.*\)$/)
? `(${targetType
.slice(1, -1)
.split(',')
.map((e) => (e ? esc(e) : e))
.join(',')})`
: esc(targetType);
return `${esc(t.name)}:${typeString}`;
});
return `${esc(dependency)}(${dependencyElements})`;
})
.join('');
}
/**
* Get a type string as hash.
*/
export function getTypeHash(
types: TypedData['types'],
type: string,
revision: Revision = Revision.Legacy
): string {
return getSelectorFromName(encodeType(types, type, revision));
}
/**
* 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.
*/
export function encodeValue(
types: TypedData['types'],
type: string,
data: unknown,
ctx: Context = {},
revision: Revision = Revision.Legacy
): [string, string] {
if (types[type]) {
return [type, getStructHash(types, type, data as TypedData['message'], revision)];
}
if (revisionConfiguration[revision].presetTypes[type]) {
return [
type,
getStructHash(
revisionConfiguration[revision].presetTypes,
type,
data as TypedData['message'],
revision
),
];
}
if (type.endsWith('*')) {
const hashes: string[] = (data as Array<TypedData['message']>).map(
(entry) => encodeValue(types, type.slice(0, -1), entry, undefined, revision)[1]
);
return [type, revisionConfiguration[revision].hashMethod(hashes)];
}
switch (type) {
case 'enum': {
if (revision === Revision.Active) {
const [variantKey, variantData] = Object.entries(data as TypedData['message'])[0];
const parentType = types[ctx.parent as string][0] as StarkNetEnumType;
const enumType = types[parentType.contains];
const variantType = enumType.find((t) => t.name === variantKey) as StarkNetType;
const variantIndex = enumType.indexOf(variantType);
const encodedSubtypes = variantType.type
.slice(1, -1)
.split(',')
.map((subtype, index) => {
if (!subtype) return subtype;
const subtypeData = (variantData as unknown[])[index];
return encodeValue(types, subtype, subtypeData, undefined, revision)[1];
});
return [
type,
revisionConfiguration[revision].hashMethod([variantIndex, ...encodedSubtypes]),
];
} // else fall through to default
return [type, getHex(data as string)];
}
case 'merkletree': {
const merkleTreeType = getMerkleTreeType(types, ctx);
const structHashes: string[] = (data as Array<TypedData['message']>).map((struct) => {
return encodeValue(types, merkleTreeType, struct, undefined, revision)[1];
});
const { root } = new MerkleTree(
structHashes as string[],
revisionConfiguration[revision].hashMerkleMethod
);
return ['felt', root];
}
case 'selector': {
return ['felt', prepareSelector(data as string)];
}
case 'string': {
if (revision === Revision.Active) {
const byteArray = byteArrayFromString(data as string);
const elements = [
byteArray.data.length,
...byteArray.data,
byteArray.pending_word,
byteArray.pending_word_len,
];
return [type, revisionConfiguration[revision].hashMethod(elements)];
} // else fall through to default
return [type, getHex(data as string)];
}
case 'felt':
case 'bool':
case 'u128':
case 'i128':
case 'ContractAddress':
case 'ClassHash':
case 'timestamp':
case 'shortstring':
return [type, getHex(data as string)];
default: {
if (revision === Revision.Active) {
throw new Error(`Unsupported type: ${type}`);
}
return [type, getHex(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 dependent types are automatically encoded.
*/
export function encodeData<T extends TypedData>(
types: T['types'],
type: string,
data: T['message'],
revision: Revision = Revision.Legacy
) {
const targetType = types[type] ?? revisionConfiguration[revision].presetTypes[type];
const [returnTypes, values] = targetType.reduce<[string[], string[]]>(
([ts, vs], field) => {
if (data[field.name] === undefined || (data[field.name] === null && field.type !== 'enum')) {
throw new Error(`Cannot encode data: missing data for '${field.name}'`);
}
const value = data[field.name];
const ctx = { parent: type, key: field.name };
const [t, encodedValue] = encodeValue(types, field.type, value, ctx, revision);
return [
[...ts, t],
[...vs, encodedValue],
];
},
[['felt'], [getTypeHash(types, type, revision)]]
);
return [returnTypes, values];
}
/**
* Get encoded data as a hash. The data should be a key -> value object with all the required values.
* All dependent types are automatically encoded.
*/
export function getStructHash<T extends TypedData>(
types: T['types'],
type: string,
data: T['message'],
revision: Revision = Revision.Legacy
) {
return revisionConfiguration[revision].hashMethod(encodeData(types, type, data, revision)[1]);
}
/**
* Get the SNIP-12 encoded message to sign, from the typedData object.
*/
export function getMessageHash(typedData: TypedData, account: BigNumberish): string {
if (!validateTypedData(typedData)) {
throw new Error('Typed data does not match JSON schema');
}
const revision = identifyRevision(typedData) as Revision;
const { domain, hashMethod } = revisionConfiguration[revision];
const message = [
encodeShortString('StarkNet Message'),
getStructHash(typedData.types, domain, typedData.domain, revision),
account,
getStructHash(typedData.types, typedData.primaryType, typedData.message, revision),
];
return hashMethod(message);
}