forked from macterra/metatron
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvault.py
198 lines (158 loc) · 5.41 KB
/
vault.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
import os
import redis
import xidb
from version import version
from flask import Flask, render_template, redirect, request, flash
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from authorize import Authorizer
from scanner import ScannerDb
from urllib.parse import urlparse
class TransferForm(FlaskForm):
cid = StringField('cid')
addr = StringField('address')
authorize = SubmitField('authorize')
transfer = SubmitField('transfer')
confirm = SubmitField('confirm')
cancel = SubmitField('cancel')
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True
bootstrap = Bootstrap(app)
#print(app.config)
@app.route("/")
def index():
return render_template('index.html')
@app.route("/about")
def about():
return render_template('about.html', version=version)
@app.route("/dumpdb")
def dumpdb():
db = ScannerDb()
return db.dump()
@app.route("/explorer")
def explorer():
db = ScannerDb()
return render_template('explorer.html', assets=db.getAssets())
@app.route("/scanner")
def scanner():
db = ScannerDb()
return render_template('scanner.html', status=db.getStatus())
@app.route("/reset")
def resetDb():
db = ScannerDb()
db.flushall()
return redirect("/scanner")
@app.route("/vault/<chain>")
def vault(chain):
authorizer = Authorizer(chain)
authorizer.updateWallet()
return render_template('vault.html', authorizer=authorizer)
@app.route("/receive/<chain>")
def receive(chain):
authorizer = Authorizer(chain)
addr = authorizer.getAddress()
flash(f"receive address: {addr}")
return redirect(f"/vault/{chain}")
@app.route("/pin/chain/<chain>")
def pinAssets(chain):
authorizer = Authorizer(chain)
authorizer.updateWallet()
for asset in authorizer.assets:
if xidb.pin(asset.cid):
flash(f"pinned {asset.cid}")
return redirect(f"/vault/{chain}")
@app.route("/ipfs/<path:path>")
def ipfs(path):
o = urlparse(request.base_url)
return redirect(f"http://{o.hostname}:8080/ipfs/{path}", 302)
@app.route("/chain/<chain>")
def chainExplorer(chain):
url = os.environ.get(f"{chain}_EXPLORER")
return redirect(url, 302)
@app.route("/chain/<chain>/block/<blockhash>")
def blockExplorer(chain, blockhash):
url = os.environ.get(f"{chain}_EXPLORER_BL").format(blockhash=blockhash)
return redirect(url, 302)
@app.route("/chain/<chain>/tx/<txid>")
def txExplorer(chain, txid):
url = os.environ.get(f"{chain}_EXPLORER_TX").format(txid=txid)
return redirect(url, 302)
@app.route("/versions/xid/<xid>")
def xidVersions(xid):
try:
db = ScannerDb()
latest = db.getLatestVersion(xid)
versions = xidb.getVersions(latest)
return render_template('versions.html', xid=xid, versions=versions)
except Exception as e:
print(e)
return redirect("/explorer")
@app.route("/versions/cid/<cid>")
def cidVersions(cid):
db = ScannerDb()
xid = xidb.getXid(cid)
latest = db.getLatestVersion(xid)
versions = xidb.getVersions(latest)
return render_template('versions.html', xid=xid, versions=versions)
@app.route("/pin/xid/<xid>")
def pinVersions(xid):
db = ScannerDb()
latest = db.getLatestVersion(xid)
versions = xidb.getVersions(latest)
for version in versions:
cid = version['cid']
if xidb.pin(cid):
flash(f"pinned {cid}")
cid = version['auth_cid']
if xidb.pin(cid):
flash(f"pinned {cid}")
return redirect(f"/versions/xid/{xid}")
@app.route("/authorize/<chain>", methods=['GET', 'POST'])
def authorize(chain):
form = TransferForm()
if request.method == 'GET':
return render_template('transfer.html', chain=chain, form=form)
cid = form.cid.data
if form.cancel.data or not cid:
flash("authorization canceled")
return redirect(f"/vault/{chain}")
authorizer = Authorizer(chain)
meta = xidb.getMeta(cid)
if not form.confirm.data:
authorizer.updateWallet()
return render_template('transfer.html', confirm=True, form=form, meta=meta, authorizer=authorizer)
if meta and xidb.pin(cid):
txid = authorizer.authorize(cid)
flash(f"authorized with txid {txid}")
else:
flash('authorization cancelled')
return redirect(f"/vault/{chain}")
@app.route("/transfer/<chain>", methods=['GET', 'POST'])
def transfer(chain):
form = TransferForm()
if request.method == 'GET':
return render_template('transfer.html', transfer=True, chain=chain, form=form)
cid = form.cid.data
addr = form.addr.data
if form.cancel.data or not cid or not addr:
flash("transfer canceled")
return redirect(f"/vault/{chain}")
authorizer = Authorizer(chain)
meta = xidb.getMeta(cid)
if not form.confirm.data:
authorizer.updateWallet()
return render_template('transfer.html', transfer=True, confirm=True, form=form, meta=meta, authorizer=authorizer)
if meta and xidb.pin(cid):
txid = authorizer.transfer(cid, addr)
flash(f"transferred with txid {txid}")
else:
flash('transfer cancelled')
return redirect(f"/vault/{chain}")
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
if __name__ == "__main__":
db = ScannerDb()
print(db.getAssets())