-
Notifications
You must be signed in to change notification settings - Fork 3
/
checkAdmin.php
327 lines (257 loc) · 10.1 KB
/
checkAdmin.php
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
<?php
require_once './ecrecover_helper.php';
header('Access-Control-Allow-Origin: *');
$maxAccounts=1000;
function base64url_decode($data) {
return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
}
function getServerAddress() {
$protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
return $protocol.'://'.$_SERVER['HTTP_HOST'];
// return "https://node-001.cchosting.org";
}
function getServerConfig($server){
$url = getServerAddress()."/ipns/Qmcir6CzDtTZvywPt9N4uXbEjp3CJeVpW6CetMG6f93QNt/configs/".$server.".json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode($response);
return $json->{'server'};
}
function getCurrency($contract){
$url = getServerAddress()."/ipns/Qmcir6CzDtTZvywPt9N4uXbEjp3CJeVpW6CetMG6f93QNt/configs/list.json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode($response,true);
if (array_key_exists($contract, $json)) {
return $json[$contract];
} else {
return '';
}
}
function getAccType($address, $contract){
$url = getServerAddress()."/api.php";
$ch = curl_init();
$ethCall = ['to' =>$contract,
'data' => '0xba99af70000000000000000000000000'.substr($address,2)
];
$fields = ['ethCall'=>$ethCall];
$fields_string = http_build_query($fields);
curl_setopt($ch, CURLOPT_URL, $url);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode($response);
$data= $json->{'data'};
return substr($data,-1);
}
function getAccStatus($address, $contract){
$url = getServerAddress()."/api.php";
$ch = curl_init();
$ethCall = ['to' =>$contract,
'data' => '0x61242bdd000000000000000000000000'.substr($address,2)
];
$fields = ['ethCall'=>$ethCall];
$fields_string = http_build_query($fields);
curl_setopt($ch, CURLOPT_URL, $url);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode($response);
$data= $json->{'data'};
return substr($data,-1);
}
function isActive($address, $contract){
$url = getServerAddress()."/api.php";
$ch = curl_init();
$ethCall = ['to' =>$contract,
'data' => '0x9f8a13d7000000000000000000000000'.substr($address,2)
];
$fields = ['ethCall'=>$ethCall];
$fields_string = http_build_query($fields);
curl_setopt($ch, CURLOPT_URL, $url);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode($response);
$data= $json->{'data'};
return substr($data,-1);
}
function getVersion( $contract){
$url = getServerAddress()."/api.php";
$ch = curl_init();
$ethCall = ['to' =>$contract,
'data' => '0x54fd4d50'
];
$fields = ['ethCall'=>$ethCall];
$fields_string = http_build_query($fields);
curl_setopt($ch, CURLOPT_URL, $url);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode($response);
$data= $json->{'data'};
return $data; //hex2bin($data);
}
function getNumber($address, $contract, $function){
$url = getServerAddress()."/api.php";
$ch = curl_init();
$ethCall = ['to' =>$contract,
'data' => $function.'000000000000000000000000'.substr($address,2)
];
$fields = ['ethCall'=>$ethCall];
$fields_string = http_build_query($fields);
curl_setopt($ch, CURLOPT_URL, $url);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode($response);
$data= $json->{'data'};
$data = '0x'.substr($data,-12);
$val = hexdec($data);
if ($val>(34359738368*4096)){
$val=$val-68719476736*4096;
}
return $val;
}
function getNumberInMap($address1, $addresse2, $contract, $function){
$url = getServerAddress()."/api.php";
$ch = curl_init();
$ethCall = ['to' =>$contract,
'data' => $function.'000000000000000000000000'.substr($address1,2).'000000000000000000000000'.substr($addresse2,2)
];
$fields = ['ethCall'=>$ethCall];
$fields_string = http_build_query($fields);
curl_setopt($ch, CURLOPT_URL, $url);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode($response);
$data= $json->{'data'};
return hexdec($data);
}
function getBalance($address, $contract){
return getNumber($address, $contract, '0x70a08231');
}
function getNTBalance($address, $contract){
return getNumber($address, $contract, '0xae261aba');
}
function getCMBalance($address, $contract){
return getNumber($address, $contract, '0xbbc72a17');
}
function getCMLimitM($address, $contract){
return getNumber($address, $contract, '0xcc885a65');
}
function getCMLimitP($address, $contract){
return getNumber($address, $contract, '0xae7143d6');
}
function checkSign($dat, $signature, $caller){
return $caller==personal_ecRecover($dat, $signature);
}
function getAccountStatus($addresses, $contract) {
$version = getVersion($contract);
$result= array();
foreach ( $addresses as $add) {
$result[$add] = 0;
}
if (strlen(strval($version))>2) {
// New Contract use isActive
foreach ( $addresses as $add) {
$result[$add] = isActive($add, $contract);
}
} else {
// Old contract fallback on getAccStatus
foreach ( $addresses as $add) {
$result[$add] = getAccStatus($add, $contract);
}
}
return $result;
}
function getPreventTransactionRule($sender_type, $receiver_type, $contract){
$url = getServerAddress()."/api.php";
$ch = curl_init();
$ethCall = ['to' =>$contract,
'data' => '0x9582272c'.substr('0000000000000000000000000000000000000000000000000000000000000000' . $sender_type, -64).substr('0000000000000000000000000000000000000000000000000000000000000000' . $receiver_type, -64)
];
$fields = ['ethCall'=>$ethCall];
$fields_string = http_build_query($fields);
curl_setopt($ch, CURLOPT_URL, $url);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode($response);
$data= $json->{'data'};
return hexdec($data);
}
function checkPreventTransactionRule($sender_address, $receiver_address, $contract) {
$sender_type = getAccType($sender_address, $contract);
$receiver_type = getAccType($receiver_address, $contract);
$ptr = getPreventTransactionRule($sender_type, $receiver_type, $contract);
return $ptr != 1;
}
function checkLegitimateAdmin($dat, $signature, $caller, $server){
$result = false;
try {
// Check the signature is ok
if (checkSign($dat, $signature, $caller)){
// get the config and the contract
$config = getServerConfig($server);
$contract = $config->{'contract_1'};
// Get the caller type and status
$acctype = getAccType($caller, $contract);
$status =getAccountStatus(array($caller), $contract);
$accStatus = $status[$caller];
if ($acctype==2 && $accStatus==1){
$result = true;
}
}
} catch (Exception $e) { }
return $result;
}
?>