-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
95 lines (73 loc) · 2.85 KB
/
main.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
import json
import subprocess
import os
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse
from pydantic import BaseModel
from decouple import config
ASSET_REG_URL: str = config("ASSET_REG_URL", default="https://assets.blockstream.info")
WELL_KNOWN_FOLDER: str = config("WELL_KNOWN_FOLDER", default="/var/www/html/.well-known")
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.mount("/.well-known", StaticFiles(directory=f"{WELL_KNOWN_FOLDER}"), name="well-known")
templates = Jinja2Templates(directory="templates")
class RegisterRequest(BaseModel):
asset_id: str
contract: dict
@app.get("/health")
def health():
return {"health": "ok"}
@app.get("/.well-known", response_class=HTMLResponse)
def list_files(request: Request):
files = os.listdir(f"{WELL_KNOWN_FOLDER}")
files_paths = sorted([f"{request.url._url}/{f}" for f in files])
return templates.TemplateResponse(
"list_files.html", {"request": request, "files": files_paths}
)
@app.post("/register_asset", status_code=201)
async def register_asset_id(request_body: RegisterRequest, request: Request):
print(f" request_body: { request_body }")
print(f" TYPE : {isinstance(request_body.contract, dict)}")
print(f" TYPE : {isinstance(request_body.contract, str)}")
if register_asset_id_local(request_body.asset_id, str(request.url.netloc)):
print(f"File succesffully written: {request_body.asset_id}")
register_asset_id_on_liquid(request_body.asset_id, request_body.contract)
return {"asset_id": request_body.asset_id}
def register_asset_id_local(asset_id: str, netloc: str):
try:
f = open(f"{WELL_KNOWN_FOLDER}/liquid-asset-proof-" + asset_id, "w")
f.write("Authorize linking the domain name " + netloc + " to the Liquid asset " + asset_id)
f.close()
except Exception as e:
print(f"File Write execption: {e}")
return False
return True
def schedule_task(task_array: list):
register_response = subprocess.run(task_array, stdout=subprocess.PIPE)
if register_response.returncode == 0:
print(register_response.stdout.decode("ASCII"))
else:
print(register_response.returncode)
def register_asset_id_on_liquid(asset_id: str, contract: dict):
contract = json.dumps(contract)
input_json = f'{{"asset_id":"{asset_id}","contract":{contract}}}'
register_request = [
"tsp",
"curl",
f"{ASSET_REG_URL}",
"-H",
"Content-Type: application/json",
"-d",
f"{input_json}",
]
schedule_task(["tsp", "sleep", "15m"])
schedule_task(register_request)