-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathallowCheck.service.ts
127 lines (110 loc) · 3.58 KB
/
allowCheck.service.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
import { Injectable } from '@nestjs/common';
import { ComparisonOperation } from 'src/config/transactionAllowList';
import { BigNumber } from 'ethers';
import {
getDeployAllowList,
getUnlimitedTxRateAddresses,
} from 'src/config/transactionAllowList';
@Injectable()
export class AllowCheckService {
private deployAllowList: Array<string>;
private unlimitedTxRateAddresses: Array<string>;
constructor() {
this.deployAllowList = getDeployAllowList();
this.unlimitedTxRateAddresses = getUnlimitedTxRateAddresses();
this.checkAddressList(this.deployAllowList);
this.checkAddressList(this.unlimitedTxRateAddresses);
}
checkAddressList(addressList: Array<string>) {
if (addressList.includes('*')) {
if (addressList.length !== 1)
throw new Error('You can not set wildcard with another address');
return;
}
let isAllow: boolean;
const firstAddress = addressList[0];
if (firstAddress[0] === '!') {
isAllow = addressList.every((address) => {
return address[0] === '!';
});
} else {
isAllow = addressList.every((address) => {
return address[0] !== '!';
});
}
if (!isAllow)
throw new Error(
'You can not set setting with address and address_denial(!address)',
);
}
isIncludedAddress(addressList: Array<string>, input: string) {
if (addressList.includes('*')) return true;
let isAllow: boolean;
const firstAddress = addressList[0];
if (firstAddress[0] === '!') {
isAllow = this.isNotProhibitedAddress(addressList, input);
} else {
isAllow = this.isAllowedAddress(addressList, input);
}
return isAllow;
}
isAllowedDeploy(from: string): boolean {
const isAllow = this.isIncludedAddress(this.deployAllowList, from);
return isAllow;
}
isUnlimitedTxRate(from: string): boolean {
const isAllow = this.isIncludedAddress(this.unlimitedTxRateAddresses, from);
return isAllow;
}
isAllowedValue(
valueCondition: ComparisonOperation | undefined,
value: BigNumber,
): boolean {
if (valueCondition === undefined) return true;
if (Object.keys(valueCondition).length === 0) return true;
let isAllow = true;
for (const key in valueCondition) {
switch (key) {
case 'eq':
if (valueCondition.eq && !value.eq(valueCondition.eq))
isAllow = false;
break;
case 'nq':
if (valueCondition.nq && value.eq(valueCondition.nq)) isAllow = false;
break;
case 'gt':
if (valueCondition.gt && value.lte(valueCondition.gt))
isAllow = false;
break;
case 'gte':
if (valueCondition.gte && value.lt(valueCondition.gte))
isAllow = false;
break;
case 'lt':
if (valueCondition.lt && value.gte(valueCondition.lt))
isAllow = false;
break;
case 'lte':
if (valueCondition.lte && value.gt(valueCondition.lte))
isAllow = false;
break;
default: // key is not invalid(e.g. leq)
isAllow = false;
break;
}
}
return isAllow;
}
private isNotProhibitedAddress(addressList: Array<string>, input: string) {
const isAllow = addressList.every((allowPattern) => {
return allowPattern.slice(1).toLowerCase() !== input.toLowerCase();
});
return isAllow;
}
private isAllowedAddress(addressList: Array<string>, input: string) {
const isAllow = addressList.some((allowPattern) => {
return allowPattern.toLowerCase() === input.toLowerCase();
});
return isAllow;
}
}