forked from iammortimer/TN-ETH-Gateway
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgateway.py
268 lines (230 loc) · 12.1 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
import re
import sqlite3 as sqlite
from web3 import Web3
from ethtoken.abi import EIP20_ABI
import PyCWaves
import json
from verification import verifier
import os
import datetime
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
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)
w3 = Web3(Web3.HTTPProvider(config['erc20']['node']))
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):
raise HTTPException(
status_code=HTTP_401_UNAUTHORIZED,
detail="Incorrect email or password",
headers={"WWW-Authenticate": "Basic"},
)
return credentials.username
def get_tnBalance():
pwTN = PyCWaves.PyCWaves()
pwTN.THROW_EXCEPTION_ON_ERROR = True
pwTN.setNode(node=config['tn']['node'], chain=config['tn']['network'], chain_id='L')
seed = os.getenv(config['tn']['seedenvname'], config['tn']['gatewaySeed'])
tnAddress = pwTN.Address(seed=seed)
myBalance = tnAddress.balance(assetId=config['tn']['assetId'])
myBalance /= pow(10, config['tn']['decimals'])
return int(round(myBalance))
def get_otherBalance():
balance = w3.eth.getBalance(config['erc20']['gatewayAddress'])
balance /= pow(10, config['erc20']['decimals'])
return int(round(balance))
@app.get("/")
async def index(request: Request):
heights = await getHeights()
return templates.TemplateResponse("index.html", {"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['erc20']['gateway_fee'],
"eth_network_fee":config['erc20']['network_fee'],
"eth_total_fee":config['erc20']['network_fee'] + config['erc20']['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['ETH'],
"tnHeight": heights['TN'],
"tnAddress": config['tn']['gatewayAddress'],
"ethAddress": config['erc20']['gatewayAddress'],
"disclaimer": config['main']['disclaimer']})
@app.get('/heights')
async def getHeights():
dbCon = sqlite.connect('gateway.db')
result = dbCon.cursor().execute('SELECT chain, height FROM heights WHERE chain = "ETH" or chain = "TN"').fetchall()
return { result[0][0]: result[0][1], result[1][0]: result[1][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"]:
dbCon = sqlite.connect('gateway.db')
result = dbCon.cursor().execute('SELECT * FROM errors').fetchall()
return templates.TemplateResponse("errors.html", {"request": request, "errors": result})
@app.get('/executed')
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"]:
dbCon = sqlite.connect('gateway.db')
result = dbCon.cursor().execute('SELECT * FROM executed').fetchall()
result2 = dbCon.cursor().execute('SELECT * FROM verified').fetchall()
return templates.TemplateResponse("tx.html", {"request": request, "txs": result, "vtxs": result2})
@app.get('/ethAddress/{address}')
async def checkTunnel(address):
dbCon = sqlite.connect('gateway.db')
address = re.sub('[\W_]+', '', address)
values = (address,)
result = dbCon.cursor().execute('SELECT targetAddress FROM tunnel WHERE sourceAddress = ?', values).fetchall()
if len(result) == 0:
targetAddress = None
else:
targetAddress = result[0][0]
return { 'sourceAddress': address, 'targetAddress': targetAddress }
@app.get('/tunnel/{sourceAddress}/{targetAddress}')
async def createTunnel(sourceAddress, targetAddress):
pwTN = PyCWaves.PyCWaves()
pwTN.setNode(node=config['tn']['node'], chain=config['tn']['network'], chain_id='L')
if not pwTN.validateAddress(targetAddress):
return {'successful': '0'}
dbCon = sqlite.connect('gateway.db')
sourceAddress = re.sub('[\W_]+', '', sourceAddress)
targetAddress = re.sub('[\W_]+', '', targetAddress)
values = (sourceAddress, targetAddress)
result = dbCon.cursor().execute('SELECT targetAddress FROM tunnel WHERE sourceAddress = ?', (sourceAddress,)).fetchall()
if len(result) == 0:
try:
sourceAddress = w3.toChecksumAddress(sourceAddress)
except:
return { 'successful': False }
if w3.isAddress(sourceAddress):
dbCon.cursor().execute('INSERT INTO TUNNEL ("sourceAddress", "targetAddress") VALUES (?, ?)', values)
dbCon.commit()
return { 'successful': True }
else:
return { 'successful': False }
else:
result = dbCon.cursor().execute('SELECT targetAddress FROM tunnel WHERE sourceAddress = ? AND targetAddress = ?', values).fetchall()
if len(result) == 0:
return { 'successful': False }
else:
return { 'successful': True }
@app.get('/dustkey/{targetAddress}')
async def createTunnel(targetAddress):
pwTN = PyCWaves.PyCWaves()
pwTN.setNode(node=config['tn']['node'], chain=config['tn']['network'], chain_id='L')
if not pwTN.validateAddress(targetAddress):
return {'successful': False}
sourceAddress = str(round(datetime.datetime.now().timestamp()))
sourceAddress = sourceAddress[-6:]
dbCon = sqlite.connect('gateway.db')
values = (sourceAddress, targetAddress)
result = dbCon.cursor().execute('SELECT targetAddress FROM tunnel WHERE sourceAddress = ?', (sourceAddress,)).fetchall()
if len(result) == 0:
result = dbCon.cursor().execute('SELECT sourceAddress FROM tunnel WHERE targetAddress = ?', (sourceAddress,)).fetchall()
if len(result) == 0:
dbCon.cursor().execute('INSERT INTO TUNNEL ("sourceAddress", "targetAddress") VALUES (?, ?)', values)
dbCon.commit()
return { 'successful': True, 'dustkey': sourceAddress}
else:
return { 'successful': True, 'dustkey': result[0][0] }
else:
result = dbCon.cursor().execute('SELECT sourceAddress FROM tunnel WHERE sourceAddress = ? AND targetAddress = ?', values).fetchall()
if len(result) == 0:
return { 'successful': False }
else:
return { 'successful': True, 'dustkey': result[0][0] }
@app.get("/api/fullinfo")
async def api_fullinfo(request: Request):
heights = await getHeights()
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['erc20']['gateway_fee'],
"other_network_fee":config['erc20']['network_fee'],
"other_total_fee":config['erc20']['network_fee'] + config['erc20']['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['ETH'],
"tnHeight": heights['TN'],
"tnAddress": config['tn']['gatewayAddress'],
"otherAddress": config['erc20']['gatewayAddress'],
"disclaimer": config['main']['disclaimer'],
"minAmount": config['main']['min'],
"maxAmount": config['main']['max'],
"type": "tunnel",
"usageinfo": ""}
@app.get("/api/fullinfo/balance")
async def api_fullinfo(request: Request):
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['erc20']['gateway_fee'],
"other_network_fee":config['erc20']['network_fee'],
"other_total_fee":config['erc20']['network_fee'] + config['erc20']['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['ETH'],
"tnHeight": heights['TN'],
"tnAddress": config['tn']['gatewayAddress'],
"otherAddress": config['erc20']['gatewayAddress'],
"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}")
async def api_depositCheck(tnAddress):
checkit = verifier(config)
result = checkit.checkDeposit(address=tnAddress)
return result
@app.get("/api/wd/{tnAddress}")
async def api_wdCheck(tnAddress):
checkit = verifier(config)
result = checkit.checkWD(address=tnAddress)
return result