2017-09-05 20:56:29 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
2017-09-25 17:55:28 +02:00
|
|
|
import json, os
|
2017-09-05 20:11:29 +02:00
|
|
|
from bitcoinrpc.authproxy import AuthServiceProxy
|
|
|
|
from lbryschema.decode import smart_decode
|
|
|
|
from flask import Flask, url_for
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
2017-09-26 17:51:18 +02:00
|
|
|
def get_lbrycrdd_connection_details(wallet_conf):
|
|
|
|
settings = {"username": "lbry",
|
|
|
|
"password": "lbry",
|
|
|
|
"rpc_port": 9245}
|
|
|
|
if wallet_conf and os.path.exists(wallet_conf):
|
|
|
|
with open(wallet_conf, "r") as conf:
|
|
|
|
conf_lines = conf.readlines()
|
|
|
|
for l in conf_lines:
|
|
|
|
if l.startswith("rpcuser="):
|
|
|
|
settings["username"] = l[8:].rstrip('\n')
|
|
|
|
if l.startswith("rpcpassword="):
|
|
|
|
settings["password"] = l[12:].rstrip('\n')
|
|
|
|
if l.startswith("rpcport="):
|
|
|
|
settings["rpc_port"] = int(l[8:].rstrip('\n'))
|
|
|
|
rpc_user = settings["username"]
|
|
|
|
rpc_pass = settings["password"]
|
|
|
|
rpc_port = settings["rpc_port"]
|
|
|
|
rpc_url = "127.0.0.1"
|
|
|
|
return "http://%s:%s@%s:%i" % (rpc_user, rpc_pass, rpc_url, rpc_port)
|
2017-09-05 20:11:29 +02:00
|
|
|
|
|
|
|
@app.errorhandler(500)
|
|
|
|
def internal_error(error):
|
|
|
|
|
|
|
|
return 'error when decoding claims'
|
|
|
|
|
2017-09-05 20:56:29 +02:00
|
|
|
|
2017-09-05 20:11:29 +02:00
|
|
|
@app.route('/claim_decode/<txid>/<nout>')
|
|
|
|
def api_decode(txid, nout):
|
2017-09-26 17:51:18 +02:00
|
|
|
connection_string = get_lbrycrdd_connection_details(os.path.expanduser("~")+"/.lbrycrd/lbrycrd.conf")
|
2017-09-05 20:11:29 +02:00
|
|
|
rpc = AuthServiceProxy(connection_string)
|
|
|
|
result = rpc.getclaimsfortx(txid)
|
|
|
|
claim = None
|
|
|
|
for claim_out in result:
|
|
|
|
if claim_out['nOut'] == int(nout):
|
|
|
|
claim = claim_out
|
|
|
|
break
|
|
|
|
if claim:
|
2017-09-05 20:56:29 +02:00
|
|
|
converted = ''.join([chr(ord(i)) for i in claim['value']])
|
|
|
|
decoded = smart_decode(converted)
|
2017-09-05 20:11:29 +02:00
|
|
|
claim['value'] = decoded.claim_dict
|
|
|
|
return json.dumps(claim)
|
|
|
|
|
2017-09-05 20:56:29 +02:00
|
|
|
|
2017-09-05 20:11:29 +02:00
|
|
|
@app.route('/claim_decodeinv/<claimid>')
|
|
|
|
def api_decodebyclaim(claimid):
|
2017-09-26 17:51:18 +02:00
|
|
|
connection_string = get_lbrycrdd_connection_details(os.path.expanduser("~")+"/.lbrycrd/lbrycrd.conf")
|
2017-09-05 20:11:29 +02:00
|
|
|
rpc = AuthServiceProxy(connection_string)
|
|
|
|
claim = rpc.getvalueforname(claimid)
|
|
|
|
if claim:
|
2017-09-05 20:56:29 +02:00
|
|
|
converted = ''.join([chr(ord(i)) for i in claim['value']])
|
|
|
|
decoded = smart_decode(converted)
|
|
|
|
claim['value'] = decoded.claim_dict
|
2017-09-05 20:11:29 +02:00
|
|
|
return json.dumps(claim)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2017-09-05 20:56:29 +02:00
|
|
|
app.run(host='127.0.0.1')
|
|
|
|
|
|
|
|
|
2017-09-25 17:55:28 +02:00
|
|
|
|