-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgateway.py
355 lines (297 loc) · 12.3 KB
/
gateway.py
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
import re
import json
import datetime
import os
from typing import List, Optional
from pydantic import BaseModel
from verification import verifier
from dbClass import dbCalls
from dbPGClass import dbPGCalls
from otherClass import otherCalls
from tnClass import tnCalls
from etherscanClass import etherscanCalls
from fastapi import FastAPI, Depends, HTTPException
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from starlette.status import HTTP_401_UNAUTHORIZED
import secrets
import uvicorn
from starlette.requests import Request
from starlette.staticfiles import StaticFiles
from starlette.templating import Jinja2Templates
from starlette.middleware.cors import CORSMiddleware
class cHeights(BaseModel):
TN: int
Other: int
class cAdresses(BaseModel):
sourceAddress: str
targetAddress: str
class cExecResult(BaseModel):
successful: bool
class cDustkey(BaseModel):
successful: bool
dustKey: int = None
class cFullInfo(BaseModel):
chainName: str
assetID: str
tn_gateway_fee: float
tn_network_fee: float
tn_total_fee: float
other_gateway_fee: float
other_network_fee: float
other_total_fee: float
fee: float
company: str
email: str
telegram: str
recovery_amount: float
recovery_fee: float
otherHeight: int
tnHeight: int
tnAddress: str
tnColdAddress: str
otherAddress: str
otherNetwork: str
disclaimer: str
tn_balance: int
other_balance: int
minAmount: float
maxAmount: float
type: str
usageinfo: str
class cDepositWD(BaseModel):
status: str
tx: str
block: str
error: str
class cTx(BaseModel):
sourceAddress: str
targetAddress: str
tnTxId: str
OtherTxId: str
TNVerBlock: int = 0
OtherVerBlock: int = 0
amount: float
TypeTX: str
Status: str
class cTxs(BaseModel):
transactions: List[cTx] = []
error: str = ""
class cFees(BaseModel):
totalFees: float
class cHealth(BaseModel):
chainName: str
assetID: str
status: str
connectionTN: bool
connectionOther: bool
blocksbehindTN: int
blockbehindOther: int
balanceTN: float
balanceOther: float
numberErrors: int
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
security = HTTPBasic()
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
with open('config.json') as json_file:
config = json.load(json_file)
if config['main']['use-pg']:
dbc = dbPGCalls(config)
else:
dbc = dbCalls(config)
tnc = tnCalls(config, dbc)
if config['other']['etherscan-on']:
otc = etherscanCalls(config, dbc)
else:
otc = otherCalls(config, dbc)
checkit = verifier(config, dbc)
def get_current_username(credentials: HTTPBasicCredentials = Depends(security)):
correct_username = secrets.compare_digest(credentials.username, config["main"]["admin-username"])
correct_password = secrets.compare_digest(credentials.password, config["main"]["admin-password"])
if not (correct_username and correct_password):
print("ERROR: invalid logon details")
raise HTTPException(
status_code=HTTP_401_UNAUTHORIZED,
detail="Incorrect email or password",
headers={"WWW-Authenticate": "Basic"},
)
return credentials.username
def get_tnBalance():
return tnc.currentBalance()
def get_otherBalance():
return otc.currentBalance()
@app.get("/")
async def index(request: Request):
heights = await getHeights()
index = config['main']['index-file']
if index == "": index = "index.html"
return templates.TemplateResponse(index, {"request": request,
"chainName": config['main']['name'],
"assetID": config['tn']['assetId'],
"tn_gateway_fee":config['tn']['gateway_fee'],
"tn_network_fee":config['tn']['network_fee'],
"tn_total_fee":config['tn']['network_fee']+config['tn']['gateway_fee'],
"eth_gateway_fee":config['other']['gateway_fee'],
"eth_network_fee":config['other']['network_fee'],
"eth_total_fee":config['other']['network_fee'] + config['other']['gateway_fee'],
"fee": config['tn']['fee'],
"company": config['main']['company'],
"email": config['main']['contact-email'],
"telegram": config['main']['contact-telegram'],
"recovery_amount":config['main']['recovery_amount'],
"recovery_fee":config['main']['recovery_fee'],
"ethHeight": heights['Other'],
"tnHeight": heights['TN'],
"tnAddress": config['tn']['gatewayAddress'],
"ethAddress": config['other']['gatewayAddress'],
"disclaimer": config['main']['disclaimer']})
@app.get('/heights', response_model=cHeights)
async def getHeights():
result = dbc.getHeights()
return {'TN': result[1][1], 'Other': result[0][1]}
@app.get('/errors')
async def getErrors(request: Request, username: str = Depends(get_current_username)):
if (config["main"]["admin-username"] == "admin" and config["main"]["admin-password"] == "admin"):
return {"message": "change the default username and password please!"}
if username == config["main"]["admin-username"]:
print("INFO: displaying errors page")
result = dbc.getErrors()
return templates.TemplateResponse("errors.html", {"request": request, "errors": result})
@app.get('/executed')
async def getExecuted(request: Request, username: str = Depends(get_current_username)):
if (config["main"]["admin-username"] == "admin" and config["main"]["admin-password"] == "admin"):
return {"message": "change the default username and password please!"}
if username == config["main"]["admin-username"]:
print("INFO: displaying executed page")
result = dbc.getExecutedAll()
result2 = dbc.getVerifiedAll()
return templates.TemplateResponse("tx.html", {"request": request, "txs": result, "vtxs": result2})
@app.get('/ethAddress/{address}', response_model=cAdresses)
async def checkTunnel(address: str):
address = re.sub('[\W_]+', '', address)
result = dbc.getTargetAddress(address)
if len(result) == 0:
targetAddress = ""
else:
targetAddress = result
return cAdresses(sourceAddress=address, targetAddress=targetAddress)
#TODO: rewrite to post
@app.get('/tunnel/{sourceAddress}/{targetAddress}', response_model=cExecResult)
async def createTunnel(sourceAddress: str, targetAddress: str):
sourceAddress = re.sub('[\W_]+', '', sourceAddress)
targetAddress = re.sub('[\W_]+', '', targetAddress)
if not tnc.validateAddress(targetAddress):
return {'successful': False}
if not otc.validateAddress(sourceAddress):
return { 'successful': False }
sourceAddress = otc.normalizeAddress(sourceAddress)
result = dbc.getTargetAddress(sourceAddress)
if len(result) == 0:
dbc.insTunnel("created", sourceAddress, targetAddress)
print("INFO: tunnel created")
return { 'successful': True }
else:
if result != targetAddress:
return { 'successful': False }
else:
return { 'successful': True }
#TODO: rewrite to post
@app.get('/dustkey/{targetAddress}', response_model=cDustkey)
async def createTunnelDK(targetAddress: str):
if not tnc.validateAddress(targetAddress):
return {'successful': False}
sourceAddress = str(round(datetime.datetime.now().timestamp()))
sourceAddress = sourceAddress[-6:]
result = dbc.getTargetAddress(sourceAddress)
if len(result) == 0:
result = dbc.getSourceAddress(targetAddress)
if len(result) == 0:
dbc.insTunnel("created", sourceAddress, targetAddress)
print("INFO: tunnel created - dustkey")
return { 'successful': True, 'dustkey': sourceAddress}
else:
return { 'successful': True, 'dustkey': result }
else:
result = dbc.getSourceAddress(targetAddress)
if len(result) == 0:
return { 'successful': False }
else:
return { 'successful': True, 'dustkey': result }
@app.get("/api/fullinfo", response_model=cFullInfo)
async def api_fullinfo():
heights = await getHeights()
tnBalance = get_tnBalance()
otherBalance = get_otherBalance()
return {"chainName": config['main']['name'],
"assetID": config['tn']['assetId'],
"tn_gateway_fee":config['tn']['gateway_fee'],
"tn_network_fee":config['tn']['network_fee'],
"tn_total_fee":config['tn']['network_fee']+config['tn']['gateway_fee'],
"other_gateway_fee":config['other']['gateway_fee'],
"other_network_fee":config['other']['network_fee'],
"other_total_fee":config['other']['network_fee'] + config['other']['gateway_fee'],
"fee": config['tn']['fee'],
"company": config['main']['company'],
"email": config['main']['contact-email'],
"telegram": config['main']['contact-telegram'],
"recovery_amount":config['main']['recovery_amount'],
"recovery_fee":config['main']['recovery_fee'],
"otherHeight": heights['Other'],
"tnHeight": heights['TN'],
"tnAddress": config['tn']['gatewayAddress'],
"tnColdAddress": config['tn']['coldwallet'],
"otherAddress": config['other']['gatewayAddress'],
"otherNetwork": config['other']['network'],
"disclaimer": config['main']['disclaimer'],
"tn_balance": tnBalance,
"other_balance": otherBalance,
"minAmount": config['main']['min'],
"maxAmount": config['main']['max'],
"type": "tunnel",
"usageinfo": ""}
@app.get("/api/deposit/{tnAddress}", response_model=cDepositWD)
async def api_depositCheck(tnAddress:str):
result = checkit.checkTX(targetAddress=tnAddress)
return result
@app.get("/api/wd/{tnAddress}", response_model=cDepositWD)
async def api_wdCheck(tnAddress: str):
result = checkit.checkTX(sourceAddress=tnAddress)
return result
@app.get("/api/checktxs/{tnAddress}", response_model=cTxs)
async def api_checktxs(tnAddress: str):
if not tnc.validateAddress(tnAddress):
temp = cTxs(error='invalid address')
else:
result = dbc.checkTXs(address=tnAddress)
if 'error' in result:
temp = cTxs(error=result['error'])
else:
temp = cTxs(transactions=result)
return temp
@app.get("/api/checktxs", response_model=cTxs)
async def api_checktxs():
result = dbc.checkTXs(address='')
if 'error' in result:
temp = cTxs(error=result['error'])
else:
temp = cTxs(transactions=result)
return temp
@app.get('/api/fees/{fromdate}/{todate}', response_model=cFees)
async def api_getFees(fromdate: str, todate: str):
return dbc.getFees(fromdate, todate)
@app.get('/api/fees/{fromdate}', response_model=cFees)
async def api_getFees(fromdate: str):
return dbc.getFees(fromdate, '')
@app.get('/api/fees', response_model=cFees)
async def api_getFees():
return dbc.getFees('','')
@app.get('/api/health', response_model=cHealth)
async def api_getHealth():
return checkit.checkHealth()