-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathhumanizer.ts
599 lines (563 loc) · 17.4 KB
/
humanizer.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
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
import { LIT_ERROR } from '@lit-protocol/constants';
import {
AccsCOSMOSParams,
AccsDefaultParams,
AccsEVMParams,
AccsRegularParams,
AccsSOLV2Params,
HumanizedAccsProps,
UnifiedAccessControlConditions,
} from '@lit-protocol/types';
import { decimalPlaces, log, throwError } from '@lit-protocol/misc';
import { formatEther, formatUnits } from 'ethers/lib/utils';
/**
*
* Format SOL number using Ether Units
*
* @param { number } amount
*
* @returns { string } formatted unit
*
*/
export const formatSol = (amount: number): string => {
return formatUnits(amount, 9);
};
/**
*
* Format Atom number using Ether Units
*
* @param { number } amount
*
* @returns { string } formatted unit
*
*/
export const formatAtom = (amount: number): string => {
return formatUnits(amount, 6);
};
/**
*
* Comparator translator
*
* @param { string } comparator
*
* @returns { string } humanized version of the comparator
*/
export const humanizeComparator = (comparator: string): string | undefined => {
let list: any = {
'>': 'more than',
'>=': 'at least',
'=': 'exactly',
'<': 'less than',
'<=': 'at most',
contains: 'contains',
};
let selected: string | undefined = list[comparator];
if (!selected) {
log(`Unregonized comparator ${comparator}`);
return;
}
return selected;
};
/**
*
* Humanize EVM basic access control conditions
*
* @property { Array<AccsRegularParams | AccsDefaultParams | any> } accessControlConditions
* @property { Array<any | string> } tokenList
* @property { string } myWalletAddress
*
* @returns
*/
export const humanizeEvmBasicAccessControlConditions = async ({
accessControlConditions,
tokenList,
myWalletAddress,
}: {
accessControlConditions: Array<AccsRegularParams | AccsDefaultParams | any>;
tokenList?: Array<any | string>;
myWalletAddress?: string;
}): Promise<string> => {
log('humanizing evm basic access control conditions');
log('myWalletAddress', myWalletAddress);
log('accessControlConditions', accessControlConditions);
let fixedConditions: any = accessControlConditions;
// inject and operator if needed
// this is done because before we supported operators,
// we let users specify an entire array of conditions
// that would be "AND"ed together. this injects those ANDs
if (accessControlConditions.length > 1) {
let containsOperator = false;
for (let i = 0; i < accessControlConditions.length; i++) {
if (accessControlConditions[i].operator) {
containsOperator = true;
}
}
if (!containsOperator) {
fixedConditions = [];
// insert ANDs between conditions
for (let i = 0; i < accessControlConditions.length; i++) {
fixedConditions.push(accessControlConditions[i]);
if (i < accessControlConditions.length - 1) {
fixedConditions.push({
operator: 'and',
});
}
}
}
}
// -- execute
const promises = await Promise.all(
fixedConditions.map(async (acc: any) => {
if (Array.isArray(acc)) {
// this is a group. recurse.
const group = await humanizeEvmBasicAccessControlConditions({
accessControlConditions: acc,
tokenList,
myWalletAddress,
});
return `( ${group} )`;
}
if (acc.operator) {
if (acc.operator.toLowerCase() === 'and') {
return ' and ';
} else if (acc.operator.toLowerCase() === 'or') {
return ' or ';
}
}
if (
acc.standardContractType === 'timestamp' &&
acc.method === 'eth_getBlockByNumber'
) {
return `Latest mined block must be past the unix timestamp ${acc.returnValueTest.value}`;
} else if (
acc.standardContractType === 'MolochDAOv2.1' &&
acc.method === 'members'
) {
// molochDAOv2.1 membership
return `Is a member of the DAO at ${acc.contractAddress}`;
} else if (
acc.standardContractType === 'ERC1155' &&
acc.method === 'balanceOf'
) {
// erc1155 owns an amount of specific tokens
return `Owns ${humanizeComparator(acc.returnValueTest.comparator)} ${
acc.returnValueTest.value
} of ${acc.contractAddress} tokens with token id ${acc.parameters[1]}`;
} else if (
acc.standardContractType === 'ERC1155' &&
acc.method === 'balanceOfBatch'
) {
// erc1155 owns an amount of specific tokens from a batch of token ids
return `Owns ${humanizeComparator(acc.returnValueTest.comparator)} ${
acc.returnValueTest.value
} of ${acc.contractAddress} tokens with token id ${acc.parameters[1]
.split(',')
.join(' or ')}`;
} else if (
acc.standardContractType === 'ERC721' &&
acc.method === 'ownerOf'
) {
// specific erc721
return `Owner of tokenId ${acc.parameters[0]} from ${acc.contractAddress}`;
} else if (
acc.standardContractType === 'ERC721' &&
acc.method === 'balanceOf' &&
acc.contractAddress === '0x22C1f6050E56d2876009903609a2cC3fEf83B415' &&
acc.returnValueTest.comparator === '>' &&
acc.returnValueTest.value === '0'
) {
// for POAP main contract where the user owns at least 1 poap
return `Owns any POAP`;
} else if (
acc.standardContractType === 'POAP' &&
acc.method === 'tokenURI'
) {
// owns a POAP
return `Owner of a ${acc.returnValueTest.value} POAP on ${acc.chain}`;
} else if (
acc.standardContractType === 'POAP' &&
acc.method === 'eventId'
) {
// owns a POAP
return `Owner of a POAP from event ID ${acc.returnValueTest.value} on ${acc.chain}`;
} else if (
acc.standardContractType === 'CASK' &&
acc.method === 'getActiveSubscriptionCount'
) {
// Cask powered subscription
return `Cask subscriber to provider ${acc.parameters[1]} for plan ${acc.parameters[2]} on ${acc.chain}`;
} else if (
acc.standardContractType === 'ERC721' &&
acc.method === 'balanceOf'
) {
// any erc721 in collection
return `Owns ${humanizeComparator(acc.returnValueTest.comparator)} ${
acc.returnValueTest.value
} of ${acc.contractAddress} tokens`;
} else if (
acc.standardContractType === 'ERC20' &&
acc.method === 'balanceOf'
) {
let tokenFromList;
if (tokenList) {
tokenFromList = tokenList.find(
(t: any) => t.address === acc.contractAddress
);
}
let decimals, name;
if (tokenFromList) {
decimals = tokenFromList.decimals;
name = tokenFromList.symbol;
} else {
decimals = await decimalPlaces({
contractAddress: acc.contractAddress,
chain: acc.chain,
});
}
log('decimals', decimals);
return `Owns ${humanizeComparator(
acc.returnValueTest.comparator
)} ${formatUnits(acc.returnValueTest.value, decimals)} of ${
name || acc.contractAddress
} tokens`;
} else if (
acc.standardContractType === '' &&
acc.method === 'eth_getBalance'
) {
return `Owns ${humanizeComparator(
acc.returnValueTest.comparator
)} ${formatEther(acc.returnValueTest.value)} ETH`;
} else if (acc.standardContractType === '' && acc.method === '') {
if (
myWalletAddress &&
acc.returnValueTest.value.toLowerCase() ===
myWalletAddress.toLowerCase()
) {
return `Controls your wallet (${myWalletAddress})`;
} else {
return `Controls wallet with address ${acc.returnValueTest.value}`;
}
}
return 'Oops. something went wrong!';
})
);
return promises.join('');
};
/**
*
* Humanize EVM contract conditions
*
* @property { Array<AccsEVMParams> } evmContractConditions
* @property { Array<any | string> } tokenList
* @property { string } myWalletAddress
*
* @returns { Promise<string> } A promise containing a human readable description of the access control conditions
*
*/
export const humanizeEvmContractConditions = async ({
evmContractConditions,
tokenList,
myWalletAddress,
}: {
evmContractConditions: Array<AccsEVMParams>;
tokenList?: Array<any | string>;
myWalletAddress?: string;
}): Promise<string> => {
log('humanizing evm contract conditions');
log('myWalletAddress', myWalletAddress);
log('evmContractConditions', evmContractConditions);
const promises = await Promise.all(
evmContractConditions.map(async (acc: any) => {
if (Array.isArray(acc)) {
// this is a group. recurse.
const group = await humanizeEvmContractConditions({
evmContractConditions: acc,
tokenList,
myWalletAddress,
});
return `( ${group} )`;
}
if (acc.operator) {
if (acc.operator.toLowerCase() === 'and') {
return ' and ';
} else if (acc.operator.toLowerCase() === 'or') {
return ' or ';
}
}
let msg = `${acc.functionName}(${acc.functionParams.join(
', '
)}) on contract address ${
acc.contractAddress
} should have a result of ${humanizeComparator(
acc.returnValueTest.comparator
)} ${acc.returnValueTest.value}`;
if (acc.returnValueTest.key !== '') {
msg += ` for key ${acc.returnValueTest.key}`;
}
return msg;
})
);
return promises.join('');
};
/**
*
* Humanize SOL RPC Conditions
*
* @property { Array<AccsSOLV2Params> } solRpcConditions
* @property { Array<any | string> } tokenList
* @property { string } myWalletAddress
*
* @returns { Promise<string> } A promise containing a human readable description of the access control conditions
*
*/
export const humanizeSolRpcConditions = async ({
solRpcConditions,
tokenList,
myWalletAddress,
}: {
solRpcConditions: Array<AccsSOLV2Params>;
tokenList?: Array<any | string>;
myWalletAddress?: string;
}): Promise<string> => {
log('humanizing sol rpc conditions');
log('myWalletAddress', myWalletAddress);
log('solRpcConditions', solRpcConditions);
const promises = await Promise.all(
solRpcConditions.map(async (acc: any) => {
if (Array.isArray(acc)) {
// this is a group. recurse.
const group = await humanizeSolRpcConditions({
solRpcConditions: acc,
tokenList,
myWalletAddress,
});
return `( ${group} )`;
}
if (acc.operator) {
if (acc.operator.toLowerCase() === 'and') {
return ' and ';
} else if (acc.operator.toLowerCase() === 'or') {
return ' or ';
}
}
if (acc.method === 'getBalance') {
return `Owns ${humanizeComparator(
acc.returnValueTest.comparator
)} ${formatSol(acc.returnValueTest.value)} SOL`;
} else if (acc.method === '') {
if (
myWalletAddress &&
acc.returnValueTest.value.toLowerCase() ===
myWalletAddress.toLowerCase()
) {
return `Controls your wallet (${myWalletAddress})`;
} else {
return `Controls wallet with address ${acc.returnValueTest.value}`;
}
} else {
let msg = `Solana RPC method ${acc.method}(${acc.params.join(
', '
)}) should have a result of ${humanizeComparator(
acc.returnValueTest.comparator
)} ${acc.returnValueTest.value}`;
if (acc.returnValueTest.key !== '') {
msg += ` for key ${acc.returnValueTest.key}`;
}
return msg;
}
})
);
return promises.join('');
};
/**
*
* Humanize Cosmos Conditions
*
* @property { Array<AccsCOSMOSParams> } cosmosConditions
* @property { Array<any | string> } tokenList
* @property { string } myWalletAddress
*
* @returns { Promise<string> } A promise containing a human readable description of the access control conditions
*
*/
export const humanizeCosmosConditions = async ({
cosmosConditions,
tokenList,
myWalletAddress,
}: {
cosmosConditions: Array<AccsCOSMOSParams | any>;
tokenList?: Array<any | string>;
myWalletAddress?: string;
}): Promise<string> => {
log('humanizing cosmos conditions');
log('myWalletAddress', myWalletAddress);
log('cosmosConditions', cosmosConditions);
const promises = await Promise.all(
cosmosConditions.map(async (acc: any) => {
if (Array.isArray(acc)) {
// this is a group. recurse.
const group = await humanizeCosmosConditions({
cosmosConditions: acc,
tokenList,
myWalletAddress,
});
return `( ${group} )`;
}
if (acc.operator) {
if (acc.operator.toLowerCase() === 'and') {
return ' and ';
} else if (acc.operator.toLowerCase() === 'or') {
return ' or ';
}
}
if (acc.path === '/cosmos/bank/v1beta1/balances/:userAddress') {
return `Owns ${humanizeComparator(
acc.returnValueTest.comparator
)} ${formatAtom(acc.returnValueTest.value)} ATOM`;
} else if (acc.path === ':userAddress') {
if (
myWalletAddress &&
acc.returnValueTest.value.toLowerCase() ===
myWalletAddress.toLowerCase()
) {
return `Controls your wallet (${myWalletAddress})`;
} else {
return `Controls wallet with address ${acc.returnValueTest.value}`;
}
} else if (
acc.chain === 'kyve' &&
acc.path === '/kyve/registry/v1beta1/funders_list/0'
) {
return `Is a current KYVE funder`;
} else {
let msg = `Cosmos RPC request for ${
acc.path
} should have a result of ${humanizeComparator(
acc.returnValueTest.comparator
)} ${acc.returnValueTest.value}`;
if (acc.returnValueTest.key !== '') {
msg += ` for key ${acc.returnValueTest.key}`;
}
return msg;
}
})
);
return promises.join('');
};
/**
*
* Humanize unified access control conditions
*
* @property { Array<AccsRegularParams | AccsDefaultParams | AccsSOLV2Params | AccsEVMParams | AccsCOSMOSParams> } unifiedAccessControlConditions
* @property { Array<any | string> } tokenList
* @property { string } myWalletAddress
*
* @returns { Promise<string> } A promise containing a human readable description of the access control conditions
*/
export const humanizeUnifiedAccessControlConditions = async ({
unifiedAccessControlConditions,
tokenList,
myWalletAddress,
}: {
unifiedAccessControlConditions: UnifiedAccessControlConditions;
tokenList?: Array<any | string>;
myWalletAddress?: string;
}): Promise<string> => {
const promises = await Promise.all(
unifiedAccessControlConditions.map(async (acc: any): Promise<any> => {
if (Array.isArray(acc)) {
// this is a group. recurse.
const group = await humanizeUnifiedAccessControlConditions({
unifiedAccessControlConditions: acc,
tokenList,
myWalletAddress,
});
return `( ${group} )`;
}
if (acc.operator) {
if (acc.operator.toLowerCase() === 'and') {
return ' and ';
} else if (acc.operator.toLowerCase() === 'or') {
return ' or ';
}
}
if (acc.conditionType === 'evmBasic') {
return humanizeEvmBasicAccessControlConditions({
accessControlConditions: [acc],
tokenList,
myWalletAddress,
});
} else if (acc.conditionType === 'evmContract') {
return humanizeEvmContractConditions({
evmContractConditions: [acc],
tokenList,
myWalletAddress,
});
} else if (acc.conditionType === 'solRpc') {
return humanizeSolRpcConditions({
solRpcConditions: [acc],
tokenList,
myWalletAddress,
});
} else if (acc.conditionType === 'cosmos') {
return humanizeCosmosConditions({
cosmosConditions: [acc],
tokenList,
myWalletAddress,
});
} else {
throwError({
message: `Unrecognized condition type: ${acc.conditionType}`,
errorKind: LIT_ERROR.INVALID_UNIFIED_CONDITION_TYPE.kind,
errorCode: LIT_ERROR.INVALID_UNIFIED_CONDITION_TYPE.name,
});
}
})
);
return promises.join('');
};
/**
*
* The human readable name for an access control condition
*
* @param { HumanizedAccsProps } params
*
* @returns { Promise<string> } A promise containing a human readable description of the access control conditions
*/
export const humanizeAccessControlConditions = async ({
accessControlConditions,
evmContractConditions,
solRpcConditions,
unifiedAccessControlConditions,
tokenList,
myWalletAddress,
}: HumanizedAccsProps): Promise<string | undefined> => {
// -- check if each condition exists in linear
if (accessControlConditions) {
return humanizeEvmBasicAccessControlConditions({
accessControlConditions,
tokenList,
myWalletAddress,
});
} else if (evmContractConditions) {
return humanizeEvmContractConditions({
evmContractConditions,
tokenList,
myWalletAddress,
});
} else if (solRpcConditions) {
return humanizeSolRpcConditions({
solRpcConditions,
tokenList,
myWalletAddress,
});
} else if (unifiedAccessControlConditions) {
return humanizeUnifiedAccessControlConditions({
unifiedAccessControlConditions,
tokenList,
myWalletAddress,
});
}
// -- undefined
return;
};