-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
444 lines (383 loc) · 16 KB
/
main.py
File metadata and controls
444 lines (383 loc) · 16 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
from quart import Quart, jsonify, request, redirect, render_template, Response
import asyncio
from marshmallow import Schema, fields, EXCLUDE
import sqlite3
import re
from base64 import b64decode
from typing import Dict, Union
from functools import wraps
import json
import requests
import uvicorn
import os
import aiohttp
from abiconverter import convert_abi
from config import USERNAME, PASSWORD, PORT, DB_PATH, EnableManager, PROXY_URL
from dark_theme_css import CSS
app = Quart(__name__)
CONFIG_DICT = {}
async def query_sc(sc_address, func, endpoints, args=None):
if args is None:
args = []
endpoint_data = next((d for d in endpoints if d['name'] == func), None)
if endpoint_data is None:
return None
body = {
"args": args,
"group": 0,
"address": sc_address,
"methodIndex": endpoint_data["endpointIndex"]
}
url = f"{PROXY_URL}contracts/call-contract"
async with aiohttp.ClientSession() as session:
async with session.post(url, json=body) as response:
try:
output = []
response_json = await response.json()
for item in response_json["returns"]:
output.append(item["value"])
if len(output) == 1:
output = output[0]
return 200, {"output": output}
except:
return 500, "Failed to load JSON response from gateway"
def requires_auth(f):
@wraps(f)
async def decorated(*args, **kwargs):
auth_header = request.headers.get('Authorization')
if auth_header:
auth_type, auth_string = auth_header.split(' ')
auth_string = b64decode(auth_string).decode('utf-8')
username, password = auth_string.split(':')
if username == USERNAME and password == PASSWORD:
return await f(*args, **kwargs)
return Response('Please authenticate', 401, {'WWW-Authenticate': 'Basic realm="Login!"'})
return decorated
async def api_docs(name) -> str:
swagger_ui_html = f'''
<!DOCTYPE html>
<html>
<head>
<title>ABI2API - {name.replace('/', '')}</title>
<link rel="icon" type="image/png" size="32x32" href="https://explorer.alephium.org/favicon.ico">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.52.1/swagger-ui.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.52.1/swagger-ui-bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.52.1/swagger-ui-standalone-preset.min.js"></script>
<style>
''' + CSS + '''
</style>
</head>
<body>
<div class="topbar"><div class="wrapper"><div class="topbar-wrapper"><center><img src="https://cdn.discordapp.com/attachments/1002615966598967358/1131252032616005812/new_logo.png?ex=65e8910e&is=65d61c0e&hm=437ffbd0ccef5818fa7a8b5ff2c84c21495359544443716d28969ac47375c198&" height=50% width=50%/></center></div></div></div>
<div id="swagger-ui"></div>
<script>
SwaggerUIBundle({
url: window.location.origin + "/" + ''' + f"'{name}/{name}swagger.json'," + '''
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
]
});
</script>
</body>
</html>
'''
return swagger_ui_html
def resolve_input_type(input_type) -> str:
cleaned_type = re.sub(r"<.*?>", "", input_type)
cleaned_type = re.sub(r"optional|variadic", "", cleaned_type)
datatypes = {
"BigUint": "integer",
"u64": "integer",
"Address": "string",
"bool": "boolean",
"TokenIdentifier": "string",
"EgldOrEsdtTokenIdentifier": "string",
"u32": "integer",
"u8": "integer"
}
return datatypes.get(cleaned_type, "string")
def resolve_output_type(output_type: Union[str, Dict]) -> Dict:
basic_types = {
'I256': {'type': 'integer', 'example': 12345678},
'U256': {'type': 'integer', 'example': 12345678},
'ByteVec': {'type': 'string', 'example': 'When the time of the White Frost comes, do not eat the yellow snow!'},
'Bool': {'type': 'boolean', 'example': False},
'BigUint': {'type': 'string', 'example': '69000000000000000000'},
'BigInt': {'type': 'string', 'example': '69000000000000000000'},
'Address': {'type': 'string', 'example': '1AfBFvSU92Sp1GTgFQKD4LQN91vXj22GijqVztpC56ozA'},
'Array': {'type': 'list', 'example': ['one', 'two']}
}
if isinstance(output_type, list):
output_type = output_type[0]
if isinstance(output_type, str):
resolved_type = None
if output_type in basic_types:
resolved_type = basic_types[output_type]
return resolved_type
else:
# If the output type is not a string, it means it's already resolved, so return as is
return output_type
class ABITypeSchema(Schema):
class Meta:
ordered = True
unknown = EXCLUDE
name = fields.Str(required=True)
mutability = fields.Str(required=True)
inputs = fields.List(fields.Dict(), required=True)
outputs = fields.List(fields.Dict())
def generate_custom_swagger_json(name: str = "") -> Dict:
display_name = name.replace('/', '')
# Generate the Swagger JSON specification
swagger_json = {
'swagger': '2.0',
'info': {
'title': f"ABI2API - API for Smart Contract: {CONFIG_DICT[display_name]['abi_json']['name']}",
'description': f'## Description\nSwagger API documentation for ABI JSON on the Alephium Blockchain.\n## Credits\nBuilt by: SkullElf\nFeel free to follow Bobbet on <a href=\"https://twitter.com/BobbetBot\">Twitter</a>\n\n## Details\nThis API instance provides data from a smart contract in the address: <a href=\"https://explorer.alephium.org/addresses/{CONFIG_DICT[display_name]["SCADDRESS"]}\">{CONFIG_DICT[display_name]["SCADDRESS"]}</a>',
'version': '1.0'
},
'paths': {},
'definitions': {},
'tags': [
{
'name': name.replace('/', ''),
'description': f'Endpoints with `readonly` mutability for smart contract: `{name.replace("/", "")}`'
}
]
}
for endpoint in CONFIG_DICT[display_name]["endpoints"]:
if endpoint["mutability"] == "mutable":
continue
schema = ABITypeSchema()
endpoint_data = schema.load(endpoint)
# Generate the path for the Swagger JSON specification
swagger_path = f"/{name}/{endpoint['name']}"
swagger_parameters = []
for input_data in endpoint_data['inputs']:
input_name = input_data['name']
input_type = input_data['type']
is_optional = input_type.startswith("optional")
swagger_parameter = {
'name': input_name,
'in': 'query',
'required': not is_optional
}
if input_type == "Array":
swagger_parameter['type'] = 'array'
swagger_parameter['items'] = {
'type': 'string'
}
elif input_type == "U256":
swagger_parameter['type'] = 'integer'
else:
swagger_parameter['type'] = 'string'
swagger_parameters.append(swagger_parameter)
# Additional handling for the "docs" field
if "docs" in endpoint:
description = "\n".join(endpoint["docs"])
else:
description = f"No documentation available for {endpoint['name']}."
swagger_json['paths'][swagger_path] = {
'get': {
'summary': endpoint['name'],
'description': description,
'parameters': swagger_parameters,
'responses': {
'200': {
'description': 'Success',
'schema': {
'type': 'object',
'properties': {
output_data.get('name', 'output'): resolve_output_type(
output_data.get('type', 'output'))
for output_data in endpoint.get('outputs', [])
}
}
}
},
'tags': [display_name]
}
}
# Generate the definition for the Swagger JSON specification
swagger_definition = {
'type': 'object',
'properties': {
output_data.get('name', 'output'): resolve_output_type(output_data.get('type', 'output'))
for output_data in endpoint.get('outputs', [])
}
}
swagger_json['definitions'][f"{endpoint['name']}_response"] = swagger_definition
# Update the Swagger parameter to represent the Array input as an array
for parameter in swagger_parameters:
if parameter['name'] in endpoint_data['inputs']:
parameter['x-multi-item'] = True
return swagger_json
@app.route('/<name>/<path:path>', methods=['GET'])
async def dynamic_route(name, path):
if path.endswith('.json'):
return jsonify(generate_custom_swagger_json(name))
inputs = {}
args = []
scaddress = str(request.args.get("smartcontractaddress", default=CONFIG_DICT[name]["SCADDRESS"]))
endpoint_data = next((item for item in CONFIG_DICT[name]["endpoints"] if item["name"] == path), None)
for input_data in endpoint_data['inputs']:
input_name = input_data['name']
input_value = str(request.args.get(input_name, default=''))
input_type = input_data['type']
is_multi_arg = input_type == "Array"
if is_multi_arg:
input_values = input_value.split(',')
inputs[input_name] = input_values
else:
inputs[input_name] = input_value
args.append({
"value": str(input_value),
"type": input_data["type"]
})
# Process the input and call the smart contract based on the endpoint name
output = await query_sc(scaddress, path, CONFIG_DICT[name]["endpoints"], args)
code, output = output
if code != 200:
message = {"error": output}
response = jsonify(message)
response.status_code = code
return response
return jsonify(output)
def create_database_and_table():
global conn
# Check if the directory "databases" exists, if not, create it
if not os.path.exists("databases"):
os.makedirs("databases")
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
# Create the "abis" table if it does not exist
cursor.execute("""
CREATE TABLE IF NOT EXISTS abis (
SCADDRESS TEXT NOT NULL,
ABI_PATH TEXT NOT NULL,
NAME TEXT NOT NULL
);
""")
# Commit the transaction and close the connection
conn.commit()
async def config():
# Call the function to create the database and table
create_database_and_table()
rows = conn.execute("SELECT * FROM abis").fetchall()
for row in rows:
sc_address, abi_path, name = row
name += "/"
if abi_path.startswith("https://") or abi_path.startswith("http://"):
abi_json = requests.get(abi_path).json()
abi_json = convert_abi(abi_json)
dummy = []
for endpoint in abi_json["endpoints"]:
if endpoint["mutability"] == "readonly":
dummy.append(endpoint)
else:
# Load ABI JSON from file
with open(abi_path) as f:
abi_json = json.load(f)
for endpoint in abi_json["endpoints"]:
if "inputs" not in endpoint:
endpoint["inputs"] = []
endpoints = abi_json["endpoints"]
CONFIG_DICT[name.replace('/', '')] = {
"abi_json": abi_json,
"endpoints": endpoints,
"SCADDRESS": sc_address
}
async def update_config():
while True:
await config()
await asyncio.sleep(3600)
@app.route('/<name>/', methods=['GET'])
async def dynamic_swagger(name):
rows = conn.execute("SELECT * FROM abis WHERE NAME=?", [name]).fetchall()
if len(rows) > 0:
return await api_docs(name)
return jsonify({'error': 'This API was not found'}), 404
async def create_update_task():
asyncio.create_task(update_config())
def activate_config_manager():
def is_valid_json(content: str) -> bool:
try:
json.loads(content)
return True
except json.JSONDecodeError:
return False
@app.route('/remove_api/<int:api_index>', methods=['GET'])
@requires_auth
async def remove_api(api_index):
rows = conn.execute("SELECT * FROM abis").fetchall()
if (api_index + 1) <= len(rows):
sc_address, abi_path, name = rows[api_index]
conn.execute("DELETE FROM abis WHERE NAME=? AND SCADDRESS=?", (name, sc_address))
conn.commit()
return redirect('/')
@app.route('/add_api', methods=['POST'])
@requires_auth
async def add_api():
# Process the form data and update the config file
data = await request.form
data = data.to_dict()
sc_address = data['sc_address']
api_name = data['api_name']
input_type = data['input_type']
# Determine whether the user provided a file or a URL for the ABI JSON
if input_type == 'file':
# Handle the file upload
files = (await request.files).to_dict()
abi_file = files.get('abi_file')
# Read the file content
abi_content = abi_file.read().decode()
if not is_valid_json(abi_content):
response = jsonify({"Error: ABI JSON is not a valid JSON"})
response.status_code = 400
return response
abi_content = json.dumps(convert_abi(json.loads(abi_content)), indent=4)
# Check if the directory "abis" exists, if not, create it
if not os.path.exists("abis"):
os.makedirs("abis")
with open(f"./abis/{api_name}.json", "w") as outfile:
outfile.write(abi_content)
abi_path = f"./abis/{api_name}.json"
else:
# Handle the URL input
abi_path = data['abi_url']
rows = conn.execute("SELECT * FROM abis WHERE name=?", [api_name]).fetchall()
if len(rows) == 0:
conn.execute("INSERT INTO abis VALUES(?,?,?)", (
sc_address,
abi_path,
api_name
))
conn.commit()
await config()
else:
response = jsonify({"Error: API name already exists"})
response.status_code = 400
return response
return redirect('/')
@app.route('/', methods=['GET', 'POST'])
@requires_auth
async def index():
if request.method == 'POST':
return await add_api()
api_list = []
rows = conn.execute("SELECT * FROM abis").fetchall()
for row in rows:
sc_address, abi_path, name = row
api_list.append({
"SCADDRESS": sc_address,
"ABI_PATH": abi_path,
"NAME": name
})
return await render_template('index.html', api_list=api_list, enumerate=enumerate)
if __name__ == "__main__":
if EnableManager:
activate_config_manager()
asyncio.run(create_update_task())
uvicorn.run(app, port=PORT, host="0.0.0.0")