forked from valerio-vaccaro/liquidtestnet.com
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfaucet.py
428 lines (339 loc) · 12.9 KB
/
faucet.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
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
from flask import (
Flask,
request,
jsonify,
)
from flask_limiter import Limiter
from flask_limiter.util import get_ipaddr
from flask_stache import render_template
from flask_qrcode import QRcode
from bitcoin_rpc_class import RPCHost
import os
import configparser
import json
import wallycore as wally
app = Flask(__name__, static_url_path='/static')
limiter = Limiter(
app,
key_func=get_ipaddr,
default_limits=["200 per day", "50 per hour"]
)
qrcode = QRcode(app)
config = configparser.RawConfigParser()
config.read('liquid.conf')
liquid_instance = config.get('GENERAL', 'liquid_instance')
rpcHost = config.get(liquid_instance, 'host')
rpcPort = config.get(liquid_instance, 'port')
rpcUser = config.get(liquid_instance, 'username')
rpcPassword = config.get(liquid_instance, 'password')
rpcPassphrase = config.get(liquid_instance, 'passphrase')
rpcWallet = config.get(liquid_instance, 'wallet')
if (len(rpcWallet) > 0):
serverURL = 'http://' + rpcUser + ':' + rpcPassword + '@' + rpcHost + ':' + str(rpcPort) + '/wallet/' + rpcWallet
else:
serverURL = 'http://' + rpcUser + ':' + rpcPassword + '@' + rpcHost + ':' + str(rpcPort)
host = RPCHost(serverURL)
if (len(rpcPassphrase) > 0):
result = host.call('walletpassphrase', rpcPassphrase, 60)
def stats():
info = host.call('getblockchaininfo')
mem = host.call('getmempoolinfo')
data = {
'heigh': info['headers'],
'mempool': str(mem['size']) + ' tx (' + str(round(mem['size'] / (1024 * 1024), 3)) + ' MB)',
'space': str(round(info['size_on_disk'] / (1024 * 1024), 3)) + ' MB',
'uptime': os.popen("uptime").read(),
'uname': os.popen("uname -a").read(),
}
return data
@app.route('/api/stats', methods=['GET'])
@limiter.exempt
def api_stats():
data = stats()
return jsonify(data)
@app.route('/', methods=['GET'])
@limiter.exempt
def url_home():
data = stats()
return render_template('home', **data)
def explorer(start, last):
data = []
for i in range(start, last, -1):
hash = host.call('getblockhash', i)
block = host.call('getblock', hash)
data.append({'id': i, 'hash': hash, 'size': block['size'], 'time': block['time'], 'nTx': block['nTx']})
return data
@app.route('/api/explorer', methods=['GET'])
@limiter.exempt
def api_explorer():
elements = 10
start = request.args.get('start')
max = host.call('getblockcount')
try:
start = int(start)
except:
start = max
if start > max:
start = max
last = start - elements
if (last < 0):
last = 0
data = explorer(start, last)
return jsonify(data)
@app.route('/explorer', methods=['GET'])
@limiter.exempt
def url_explorer():
elements = 10
start = request.args.get('start')
max = host.call('getblockcount')
try:
start = int(start)
except:
start = max
if start > max:
start = max
last = start - elements
if (last < 0):
last = 0
data = {'blocks_list': explorer(start, last), 'prev': start - elements, 'next': start + elements}
return render_template('explorer', **data)
def block(height):
if height is None:
return {'error': 'missing height'}
try:
id = host.call('getblockhash', int(height))
data = host.call('getblock', id, 2)
except:
data = {'error': 'unknown block'}
return data
@app.route('/api/block', methods=['GET'])
@limiter.exempt
def api_block():
height = request.args.get('height')
data = block(height)
return jsonify(data)
@app.route('/block', methods=['GET'])
@limiter.exempt
def url_block():
height = request.args.get('height')
res_block = block(height)
data = {'block': height, 'result': json.dumps(res_block, indent=4, sort_keys=True), 'transaction_list': res_block['tx']}
return render_template('block', **data)
def transaction(txid):
if txid is None:
return {'error': 'missing txid'}
if not len(txid) == 64:
return {'error': 'txid must be of length 64'}
try:
data = host.call('getrawtransaction', txid, True)
except:
data = {'error': 'unknown txid'}
return data
@app.route('/api/transaction', methods=['GET'])
@limiter.exempt
def api_transaction():
txid = request.args.get('txid')
data = transaction(txid)
return jsonify(data)
@app.route('/transaction', methods=['GET'])
@limiter.exempt
def url_transaction():
txid = request.args.get('txid')
data = {'txid': txid, 'result': json.dumps(transaction(txid), indent=4, sort_keys=True)}
return render_template('transaction', **data)
def faucet(address, amount):
if host.call('validateaddress', address)['isvalid']:
tx = host.call('sendtoaddress', address, amount)
data = "Sent " + str(amount) + " LBTC to address " + address + " with transaction " + tx + "."
else:
data = "Error"
return data
@app.route('/api/faucet', methods=['GET'])
@limiter.limit('1000/day;100/hour;3/minute')
def api_faucet():
balance = host.call('getbalance')['bitcoin']
address = request.args.get('address')
ip = request.headers.get('X-Forwarded-For', request.remote_addr)
if address is None:
data = {'result': 'missing address', 'balance': balance}
return jsonify(data)
amount = 0.001
data = {'result': faucet(address, amount), 'balance': balance}
return jsonify(data)
@app.route('/faucet', methods=['GET'])
@limiter.limit('1000/day;100/hour;3/minute')
def url_faucet():
balance = host.call('getbalance')['bitcoin']
address = request.args.get('address')
ip = request.headers.get('X-Forwarded-For', request.remote_addr)
if address is None:
data = {'result': 'missing address', 'balance': balance}
data['form'] = True
return render_template('faucet', **data)
amount = 0.1
data = {'result': faucet(address, amount), 'balance': balance}
data['form'] = False
return render_template('faucet', **data)
def issuer(asset_amount, asset_address, token_amount, token_address, issuer_pubkey, name, ticker, precision, domain):
data = {}
version = 0 # don't change
blind = False
feerate = 0.00003000
asset_amount = int(asset_amount) / 10 ** (8 - int(precision))
token_amount = int(token_amount) / 10 ** (8 - int(precision))
# Create funded base tx
base = host.call('createrawtransaction', [], [{'data': '00'}])
funded = host.call('fundrawtransaction', base, {'feeRate': feerate})
# Create the contact and calculate the asset id (Needed for asset registry!)
contract = json.dumps({'name': name,
'ticker': ticker,
'precision': precision,
'entity': {'domain': domain},
'issuer_pubkey': issuer_pubkey,
'version': version}, separators=(',', ':'), sort_keys=True)
contract_hash = wally.hex_from_bytes(wally.sha256(contract.encode('ascii')))
data['contract'] = contract
# Create the rawissuance transaction
contract_hash_rev = wally.hex_from_bytes(wally.hex_to_bytes(contract_hash)[::-1])
rawissue = host.call('rawissueasset', funded['hex'], [{'asset_amount': asset_amount,
'asset_address': asset_address,
'token_amount': token_amount,
'token_address': token_address,
'blind': blind,
'contract_hash': contract_hash_rev}])
# Blind the transaction
blind = host.call('blindrawtransaction', rawissue[0]['hex'], True, [], False)
# Sign transaction
signed = host.call('signrawtransactionwithwallet', blind)
decoded = host.call('decoderawtransaction', signed['hex'])
data['asset_id'] = decoded['vin'][0]['issuance']['asset']
# Test transaction
test = host.call('testmempoolaccept', [signed['hex']])
if test[0]['allowed'] is True:
txid = host.call('sendrawtransaction', signed['hex'])
data['txid'] = txid
data['registry'] = {'asset_id': data['asset_id'],
'contract': json.loads(data['contract'])}
return data
@app.route('/api/issuer', methods=['GET'])
@limiter.limit('1000/day;100/hour;3/minute')
def api_issuer():
ip = request.headers.get('X-Forwarded-For', request.remote_addr)
command = request.args.get('command')
if command == 'asset':
asset_amount = int(request.args.get('asset_amount'))
asset_address = request.args.get('asset_address')
token_amount = int(request.args.get('token_amount'))
token_address = request.args.get('token_address')
issuer_pubkey = request.args.get('pubkey')
name = request.args.get('name')
ticker = request.args.get('ticker')
precision = request.args.get('precision')
domain = request.args.get('domain')
data = issuer(asset_amount, asset_address, token_amount, token_address, issuer_pubkey, name, ticker, precision, domain)
data['domain'] = domain
data['name'] = name
else:
data = {}
return jsonify(data)
@app.route('/issuer', methods=['GET'])
@limiter.limit('1000/day;100/hour;3/minute')
def url_issuer():
ip = request.headers.get('X-Forwarded-For', request.remote_addr)
command = request.args.get('command')
if command == 'asset':
asset_amount = int(request.args.get('asset_amount'))
asset_address = request.args.get('asset_address')
token_amount = int(request.args.get('token_amount'))
token_address = request.args.get('token_address')
issuer_pubkey = request.args.get('pubkey')
name = request.args.get('name')
ticker = request.args.get('ticker')
precision = request.args.get('precision')
domain = request.args.get('domain')
data = issuer(asset_amount, asset_address, token_amount, token_address, issuer_pubkey, name, ticker, precision, domain)
data['form'] = False
data['domain'] = domain
data['name'] = name
else:
data = {}
data['form'] = True
return render_template('issuer', **data)
def opreturn(text):
base = host.call('createrawtransaction', [], [{'data': text}])
funded = host.call('fundrawtransaction', base)
blind = host.call('blindrawtransaction', funded['hex'], True, [], False)
signed = host.call('signrawtransactionwithwallet', blind)
test = host.call('testmempoolaccept', [signed['hex']])
if test[0]['allowed'] is True:
return host.call('sendrawtransaction', signed['hex'])
return
def test(tx):
return host.call('testmempoolaccept', [tx])
def broadcast(tx):
test = host.call('testmempoolaccept', [tx])
if test[0]['allowed'] is True:
return host.call('sendrawtransaction', tx)
return
@app.route('/api/utils', methods=['GET'])
@limiter.limit('1000/day;100/hour;3/minute')
def api_utils():
ip = request.headers.get('X-Forwarded-For', request.remote_addr)
command = request.args.get('command')
if command == 'opreturn':
text = request.args.get('text')
data = {'result_opreturn': opreturn(text)}
elif command == 'test':
tx = request.args.get('tx')
data = {'result_test': test(tx)}
elif command == 'broadcast':
tx = request.args.get('tx')
data = {'result_broadcast': broadcast(tx)}
else:
data = {}
return jsonify(data)
@app.route('/utils', methods=['GET'])
@limiter.limit('1000/day;100/hour;3/minute')
def url_utils():
ip = request.headers.get('X-Forwarded-For', request.remote_addr)
command = request.args.get('command')
if command == 'opreturn':
text = request.args.get('text')
data = {'result_opreturn': opreturn(text)}
data['form_opreturn'] = False
data['form_test'] = True
data['form_broadcast'] = True
elif command == 'test':
tx = request.args.get('tx')
data = {'result_test': test(tx)}
data['form_opreturn'] = True
data['form_test'] = False
data['form_broadcast'] = True
elif command == 'broadcast':
tx = request.args.get('tx')
data = {'result_broadcast': broadcast(tx)}
data['form_opreturn'] = True
data['form_test'] = True
data['form_broadcast'] = False
else:
data = {}
data['form_opreturn'] = True
data['form_test'] = True
data['form_broadcast'] = True
return render_template('utils', **data)
def about():
data = {}
return data
@app.route('/api/about', methods=['GET'])
@limiter.exempt
def api_about():
data = about()
return jsonify(data)
@app.route('/about', methods=['GET'])
@limiter.exempt
def url_about():
data = about()
return render_template('about', **data)
if __name__ == '__main__':
app.import_name = '.'
app.run(host='0.0.0.0', port=8123)