forked from com-chain/api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckAdmin.php
197 lines (151 loc) · 5.89 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
<?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 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 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 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);
$accStatus = getAccStatus($caller, $contract);
if ($acctype==2 && $accStatus==1){
$result = true;
}
}
} catch (Exception $e) { }
return $result;
}
?>