Repository navigation
Expand file tree
/
Copy pathverify.js
More file actions
580 lines (506 loc) · 18 KB
/
Copy pathverify.js
File metadata and controls
580 lines (506 loc) · 18 KB
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
const fs = require("node:fs");
const ethers = require("ethers");
const { imageSize } = require("image-size");
const logos = {};
for (let i = 0; i < fs.readdirSync("logo/").length; i++) {
const file = fs.readdirSync("logo/")[i];
logos[file] = true;
}
for (const file of Object.keys(logos)) {
const info = imageSize(fs.readFileSync(`logo/${file}`));
if (info.type !== "svg" && info.type !== "png" && info.type !== "jpg") {
throw Error(`logo file ${file} is not SVG/PNG/JPG`);
}
if (info.height !== info.width && file !== "swaap.png")
throw Error(
`logo dimensions not square: ${file} (${info.height} x ${info.width})`,
);
}
const VALID_VAULT_OVERRIDE_KEYS = new Set([
"name",
"description",
"portfolioNotice",
"deprecationReason",
"block",
"restricted",
"notExplorableLend",
"notExplorableBorrow",
"tags",
]);
const VALID_ASSET_MATCH_KEYS = new Set([
"address",
"symbols",
"symbolRegex",
"names",
"nameRegex",
]);
const VALID_ASSET_KEYS = new Set([
...VALID_ASSET_MATCH_KEYS,
"block",
"restricted",
]);
// Caps mirrored from the client/server side (server/api/labels/[file].get.ts)
// to keep locally-verified files in sync with what the proxy will accept.
const MAX_ASSET_ENTRIES = 10000;
const MAX_STRING_LEN = 16384;
const MAX_SYMBOL_LEN = 64;
const MAX_NAME_LEN = 256;
const MAX_REGEX_LEN = 512;
const COUNTRY_ALIASES = new Set(["EU", "EEA", "EFTA"]);
const ISO_ALPHA2_RE = /^[A-Za-z]{2}$/;
for (const file of fs.readdirSync(".")) {
if (!/^\d+$/.test(file)) continue;
validateChain(file);
}
validateGlobal();
console.log("OK");
///////////
function validateChain(chainId) {
const entities = loadJsonFile(`${chainId}/entities.json`);
const products = loadJsonFile(`${chainId}/products.json`);
const points = loadJsonFile(`${chainId}/points.json`);
const earnVaults = loadJsonFileIfExists(`${chainId}/earn-vaults.json`) || [];
const assets = loadJsonFileIfExists(`${chainId}/assets.json`) || [];
validateUniqueEntityAddresses(entities);
validateEarnVaults(`${chainId}/earn-vaults.json`, earnVaults);
validateAssets(`${chainId}/assets.json`, assets);
for (const entityId of Object.keys(entities)) {
const entity = entities[entityId];
if (!validSlug(entityId))
throw Error(`entities: invalid slug: ${entityId}`);
if (!entity.name) throw Error(`entities: missing name: ${entityId}`);
for (const addr of Object.keys(entity.addresses || {})) {
if (addr !== ethers.getAddress(addr))
throw Error(`entities: malformed address: ${addr}`);
}
if (entity.logo && !logos[entity.logo])
throw Error(`entities: logo not found: ${entity.logo}`);
}
const vaultsSeenInProducts = {};
for (const productId of Object.keys(products)) {
const product = products[productId];
if (!validSlug(productId))
throw Error(`products: invalid slug: ${productId}`);
if (!product.name) throw Error(`products: missing name: ${productId}`);
if (
product.description !== undefined &&
typeof product.description !== "string"
)
throw Error(`products: description must be a string: ${productId}`);
for (const addr of product.vaults) {
if (addr !== ethers.getAddress(addr))
throw Error(
`products: malformed vault address: ${ethers.getAddress(addr)}`,
);
if (vaultsSeenInProducts[addr])
throw Error(`products: vault in multiple products: ${addr}`);
vaultsSeenInProducts[addr] = true;
}
if (product.deprecatedVaults) {
for (const addr of product.deprecatedVaults) {
if (addr !== ethers.getAddress(addr))
throw Error(
`products: malformed deprecated vault address: ${ethers.getAddress(addr)}`,
);
if (product.vaults.includes(addr))
throw Error(
`products: vault ${addr} cannot be both in vaults and deprecatedVaults: ${productId}`,
);
if (vaultsSeenInProducts[addr])
throw Error(`products: vault in multiple products: ${addr}`);
vaultsSeenInProducts[addr] = true;
}
}
if (product.deprecationReason !== undefined) {
if (typeof product.deprecationReason !== "string")
throw Error(
`products: deprecationReason must be a string: ${productId}`,
);
}
for (const entity of getArray(product.entity)) {
if (!entities[entity]) throw Error(`products: no such entity ${entity}`);
}
if (product.logo && !logos[product.logo])
throw Error(`products: logo not found: ${product.logo}`);
if (
product.url !== undefined &&
product.url !== "" &&
!validUrl(product.url)
)
throw Error(`products: invalid url: ${productId}`);
if (product.portfolioNotice !== undefined) {
if (typeof product.portfolioNotice !== "string")
throw Error(`products: portfolioNotice must be a string: ${productId}`);
}
if (product.notExplorable !== undefined) {
if (typeof product.notExplorable !== "boolean")
throw Error(`products: notExplorable must be a boolean: ${productId}`);
}
if (product.isGovernanceLimited !== undefined) {
throw Error(
`products: isGovernanceLimited has migrated to tags: ${productId}`,
);
}
validateTags(product.tags, "products: tags", productId);
if (product.block !== undefined) {
if (!Array.isArray(product.block))
throw Error(`products: block must be an array: ${productId}`);
for (const code of product.block) {
if (typeof code !== "string")
throw Error(`products: block entries must be strings: ${productId}`);
}
}
if (product.recentlyAddedVaults) {
throw Error(
`products: recentlyAddedVaults has migrated to vaultOverrides tags: ${productId}`,
);
}
if (product.vaultOverrides) {
const allProductVaults = [
...product.vaults,
...(product.deprecatedVaults || []),
];
for (const [addr, override] of Object.entries(product.vaultOverrides)) {
if (addr !== ethers.getAddress(addr))
throw Error(`products: malformed vaultOverrides address: ${addr}`);
if (!allProductVaults.includes(addr))
throw Error(
`products: vaultOverrides address ${addr} not in vaults or deprecatedVaults: ${productId}`,
);
for (const key of Object.keys(override)) {
if (!VALID_VAULT_OVERRIDE_KEYS.has(key))
throw Error(
`products: unknown vaultOverrides key "${key}" for ${addr}: ${productId}`,
);
}
if (override.name !== undefined && typeof override.name !== "string")
throw Error(
`products: vaultOverrides name must be a string for ${addr}: ${productId}`,
);
if (
override.description !== undefined &&
typeof override.description !== "string"
)
throw Error(
`products: vaultOverrides description must be a string for ${addr}: ${productId}`,
);
if (
override.portfolioNotice !== undefined &&
typeof override.portfolioNotice !== "string"
)
throw Error(
`products: vaultOverrides portfolioNotice must be a string for ${addr}: ${productId}`,
);
if (
override.deprecationReason !== undefined &&
typeof override.deprecationReason !== "string"
)
throw Error(
`products: vaultOverrides deprecationReason must be a string for ${addr}: ${productId}`,
);
if (
override.notExplorableLend !== undefined &&
typeof override.notExplorableLend !== "boolean"
)
throw Error(
`products: vaultOverrides notExplorableLend must be a boolean for ${addr}: ${productId}`,
);
if (
override.notExplorableBorrow !== undefined &&
typeof override.notExplorableBorrow !== "boolean"
)
throw Error(
`products: vaultOverrides notExplorableBorrow must be a boolean for ${addr}: ${productId}`,
);
validateTags(
override.tags,
`products: vaultOverrides tags for ${addr}`,
productId,
);
if (override.block !== undefined) {
if (!Array.isArray(override.block))
throw Error(
`products: vaultOverrides block must be an array for ${addr}: ${productId}`,
);
for (const code of override.block) {
if (typeof code !== "string")
throw Error(
`products: vaultOverrides block entries must be strings for ${addr}: ${productId}`,
);
}
}
if (override.restricted !== undefined) {
if (!Array.isArray(override.restricted))
throw Error(
`products: vaultOverrides restricted must be an array for ${addr}: ${productId}`,
);
for (const code of override.restricted) {
if (typeof code !== "string")
throw Error(
`products: vaultOverrides restricted entries must be strings for ${addr}: ${productId}`,
);
}
}
}
}
}
for (const point of points) {
if (point.token && point.token !== ethers.getAddress(point.token))
throw Error(`points: malformed token: ${point.token}`);
if (!point.name) throw Error(`points: missing name: ${point.name}`);
if (point.url && !validUrl(point.url))
throw Error(`points: missing name: ${point.name}`);
if (point.logo && !logos[point.logo])
throw Error(`points: logo not found: ${product.logo}`);
if (point.skipValidation) continue;
if (!point.collateralVaults?.length && !point.liabilityVaults?.length) {
throw Error(
`points: missing collateral or liability vaults for ${point.name}`,
);
}
if (point.collateralVaults) {
for (const addr of point.collateralVaults) {
if (addr !== ethers.getAddress(addr))
throw Error(`points: malformed vault address: ${addr}`);
}
}
if (point.liabilityVaults) {
for (const addr of point.liabilityVaults) {
if (addr !== ethers.getAddress(addr))
throw Error(`points: malformed vault address: ${addr}`);
}
}
}
}
function validateTags(tags, context, id) {
if (tags === undefined) return;
if (!Array.isArray(tags)) throw Error(`${context} must be an array: ${id}`);
for (const tag of tags) {
if (typeof tag !== "string")
throw Error(`${context} entries must be strings: ${id}`);
}
}
function validateEarnVaults(fileName, earnVaults) {
if (!Array.isArray(earnVaults))
throw Error(`${fileName}: top-level value must be an array`);
const seen = new Set();
for (const entry of earnVaults) {
const address = typeof entry === "string" ? entry : entry?.address;
if (address !== ethers.getAddress(address))
throw Error(`${fileName}: malformed earn vault address: ${address}`);
if (seen.has(address))
throw Error(`${fileName}: duplicate earn vault address: ${address}`);
seen.add(address);
if (typeof entry === "string") continue;
if (!entry || typeof entry !== "object")
throw Error(`${fileName}: earn vault entries must be strings or objects`);
if (entry.recentlyAdded !== undefined)
throw Error(
`${fileName}: recentlyAdded has migrated to tags: ${address}`,
);
if (entry.deprecated !== undefined && typeof entry.deprecated !== "boolean")
throw Error(`${fileName}: deprecated must be a boolean: ${address}`);
if (
entry.deprecationReason !== undefined &&
typeof entry.deprecationReason !== "string"
)
throw Error(
`${fileName}: deprecationReason must be a string: ${address}`,
);
if (
entry.description !== undefined &&
typeof entry.description !== "string"
)
throw Error(`${fileName}: description must be a string: ${address}`);
if (
entry.portfolioNotice !== undefined &&
typeof entry.portfolioNotice !== "string"
)
throw Error(`${fileName}: portfolioNotice must be a string: ${address}`);
if (
entry.notExplorable !== undefined &&
typeof entry.notExplorable !== "boolean"
)
throw Error(`${fileName}: notExplorable must be a boolean: ${address}`);
validateTags(entry.tags, `${fileName}: tags`, address);
for (const field of ["block", "restricted"]) {
if (entry[field] === undefined) continue;
if (!Array.isArray(entry[field]))
throw Error(`${fileName}: ${field} must be an array: ${address}`);
for (const code of entry[field]) {
if (typeof code !== "string")
throw Error(
`${fileName}: ${field} entries must be strings: ${address}`,
);
}
}
}
}
/**
* Validates that each Ethereum address is only referenced once across all entities
*/
function validateUniqueEntityAddresses(entities) {
const addressMap = new Map();
for (const entityId of Object.keys(entities)) {
const entity = entities[entityId];
if (!entity.addresses) continue;
for (const address of Object.keys(entity.addresses)) {
const normalizedAddress = ethers.getAddress(address);
if (addressMap.has(normalizedAddress)) {
const previousEntity = addressMap.get(normalizedAddress);
// Allow for duplicates in gauntlet
if (previousEntity === "gauntlet" || entityId === "gauntlet") {
continue;
}
throw Error(
`Duplicate address ${normalizedAddress} found in entities: ${previousEntity} and ${entityId}`,
);
}
addressMap.set(normalizedAddress, entityId);
}
}
}
function loadJsonFile(file) {
return JSON.parse(fs.readFileSync(file).toString());
}
function loadJsonFileIfExists(file) {
if (!fs.existsSync(file)) return null;
return loadJsonFile(file);
}
function validateGlobal() {
const assets = loadJsonFileIfExists("all/assets.json") || [];
validateAssets("all/assets.json", assets, { isGlobal: true });
}
function validateAssets(fileLabel, assets, opts = {}) {
const isGlobal = opts.isGlobal === true;
const filePrefix = `assets (${fileLabel})`;
if (!Array.isArray(assets))
throw Error(`${filePrefix}: top-level value must be an array`);
if (assets.length > MAX_ASSET_ENTRIES)
throw Error(
`${filePrefix}: ${assets.length} entries exceeds cap of ${MAX_ASSET_ENTRIES}`,
);
const seenAddresses = new Set();
for (let i = 0; i < assets.length; i++) {
const entry = assets[i];
const where = `${filePrefix}: entry #${i}`;
if (!entry || typeof entry !== "object" || Array.isArray(entry))
throw Error(`${where} must be a plain object`);
for (const key of Object.keys(entry)) {
if (!VALID_ASSET_KEYS.has(key))
throw Error(`${where} has unknown key '${key}'`);
}
const hasAddress = entry.address !== undefined;
const hasSymbols = entry.symbols !== undefined;
const hasSymbolRegex = entry.symbolRegex !== undefined;
const hasNames = entry.names !== undefined;
const hasNameRegex = entry.nameRegex !== undefined;
if (hasAddress) {
if (typeof entry.address !== "string" || !entry.address)
throw Error(`${where} address must be a non-empty string`);
if (entry.address.length > MAX_STRING_LEN)
throw Error(`${where} address exceeds ${MAX_STRING_LEN} chars`);
if (entry.address !== ethers.getAddress(entry.address))
throw Error(
`${where} address ${entry.address} not in checksummed form`,
);
if (seenAddresses.has(entry.address))
throw Error(`${where} duplicate address ${entry.address}`);
seenAddresses.add(entry.address);
if (isGlobal) {
console.log(
`warn: ${where} uses address ${entry.address} in all/assets.json — addresses are chain-specific; prefer symbols/names patterns for cross-chain rules`,
);
}
}
if (hasSymbols)
validateMatchArray(entry.symbols, "symbols", where, MAX_SYMBOL_LEN);
if (hasNames) validateMatchArray(entry.names, "names", where, MAX_NAME_LEN);
if (hasSymbolRegex) validateRegex(entry.symbolRegex, "symbolRegex", where);
if (hasNameRegex) validateRegex(entry.nameRegex, "nameRegex", where);
const hasPattern = hasSymbols || hasSymbolRegex || hasNames || hasNameRegex;
if (!hasAddress && !hasPattern) throw Error(`${where} has no match fields`);
if (hasAddress && hasPattern) {
console.log(
`warn: ${where} mixes address and pattern match fields; consider splitting into two entries so block/restricted can be scoped independently`,
);
}
validateCountryCodeArray(entry.block, "block", where);
validateCountryCodeArray(entry.restricted, "restricted", where);
const hasBlockRule = Array.isArray(entry.block) && entry.block.length > 0;
const hasRestrictedRule =
Array.isArray(entry.restricted) && entry.restricted.length > 0;
if (!hasBlockRule && !hasRestrictedRule)
throw Error(`${where} has neither block nor restricted`);
}
}
function validateMatchArray(value, field, where, maxItemLen) {
if (!Array.isArray(value)) throw Error(`${where} ${field} must be an array`);
if (value.length === 0)
throw Error(`${where} ${field} must be a non-empty array`);
for (let j = 0; j < value.length; j++) {
const item = value[j];
if (typeof item !== "string")
throw Error(`${where} ${field}[${j}] must be a string`);
if (!item || !item.trim())
throw Error(
`${where} ${field}[${j}] must be a non-empty, non-whitespace string`,
);
if (item.length > maxItemLen)
throw Error(
`${where} ${field}[${j}] '${item}' exceeds ${maxItemLen} chars`,
);
if (item.length > MAX_STRING_LEN)
throw Error(`${where} ${field}[${j}] exceeds ${MAX_STRING_LEN} chars`);
}
}
function validateRegex(value, field, where) {
if (typeof value !== "string" || !value)
throw Error(`${where} ${field} must be a non-empty string`);
if (value.length > MAX_STRING_LEN)
throw Error(`${where} ${field} exceeds ${MAX_STRING_LEN} chars`);
if (value.length > MAX_REGEX_LEN)
throw Error(`${where} ${field} exceeds ${MAX_REGEX_LEN} chars`);
try {
new RegExp(value, "i");
} catch (e) {
throw Error(`${where} ${field} does not compile: ${e.message}`);
}
}
function validateCountryCodeArray(value, field, where) {
if (value === undefined) return;
if (!Array.isArray(value)) throw Error(`${where} ${field} must be an array`);
const seen = new Set();
for (let j = 0; j < value.length; j++) {
const item = value[j];
if (typeof item !== "string")
throw Error(`${where} ${field}[${j}] must be a string`);
if (item.length > MAX_STRING_LEN)
throw Error(`${where} ${field}[${j}] exceeds ${MAX_STRING_LEN} chars`);
let canonical;
if (COUNTRY_ALIASES.has(item)) {
canonical = item;
} else if (ISO_ALPHA2_RE.test(item)) {
canonical = item.toUpperCase();
} else {
throw Error(
`${where} ${field}[${j}] '${item}' is not a valid country code (expected ISO 3166-1 alpha-2 or EU/EEA/EFTA)`,
);
}
if (seen.has(canonical))
throw Error(`${where} ${field} has duplicate '${item}'`);
seen.add(canonical);
}
}
function validSlug(slug) {
return /^[a-z0-9-]+$/.test(slug);
}
function validUrl(url) {
return /^(https?:\/\/)?([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,63}(\/[^\s]*)?$/.test(
url,
);
}
function getArray(v) {
if (Array.isArray(v)) return v;
return [v];
}