-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverusverifyapi.py
More file actions
executable file
·158 lines (131 loc) · 5.3 KB
/
verusverifyapi.py
File metadata and controls
executable file
·158 lines (131 loc) · 5.3 KB
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
#!/usr/bin/env python
'''
Basic Functionality:
$host/$querytype/?$query=$queryinput&signer=$signer&signature=$signature
example:
$host/verifymessage/?message=This is a test message&signer=jbsci@&signature=Af+4EQABQSA1qs5h3yc553W8ulMVU+cVhJgXnkXHeZyEvP7oX9Iiizq3LIY1kWCyrWromhRv7CO1mdViKffFd6jGku0SiCSM
Requires verusrpc.py (included in this repo) and configuration via rpc_api.conf
Written 2020 by Jonathan Barnes <j@jbsci.dev>
'''
#-# Imports #-#
import sys
import verusrpc as vrpc
from flask import Flask, request
from flask_api import FlaskAPI, status, exceptions
from flask_sslify import SSLify
from flask_talisman import Talisman
#--# Definitions #--#
app = FlaskAPI(__name__)
#---# Configuration #---#
class readconfig:
def __init__(self):
for line in open('rpc_api.conf', 'r'):
if line.find('#') >= 0:
line = line.split('#')[0]
line = line.strip()
if len(line) > 0:
segments = line.split('=')
inparam = segments[0].strip()
inval = segments[1].strip()
if inparam == 'apiport':
self.port = int(inval)
elif inparam == 'apihost':
self.host = inval
elif inparam == 'SSL':
if inval.upper() == 'YES':
self.ssl = True
else:
self.ssl = False
elif inparam == 'SSL_KEY':
self.sslkey = inval
elif inparam == 'SSL_CRT':
self.sslcrt = inval
apiconf = readconfig()
if apiconf.ssl:
Talisman(app)
if apiconf.sslkey == 'none' or apiconf.sslcrt == 'none':
sys.exit('ERROR: SSL enabled but no key or cert specified')
context = (apiconf.sslcrt, apiconf.sslkey)
sslify = SSLify(app)
#----# Functions #----#
def verusverify(thing_to_verify, signer, signature, method, rpcid):
'''
Uses given rpc method to perform verification of a File (verifyfile), Filehash (verifyhash),
or message (verifymessage)
'''
result = vrpc.verusquery(method, [signer, signature, thing_to_verify], rpcid=rpcid)
return result
def verusidentity(identity):
'''
Queries RPC to check if identity exists and returns information.
Can use identity or identity address.
'''
result = vrpc.verusquery("getidentity", [identity], rpcid="getidentity")
if result["result"]:
del result['result']['cansignfor']
del result['result']['canspendfor']
return result
else:
return result
#-----# API #-----#
@app.route("/")
def index():
return """
<h1>Welcome to the verus verify API</h1>
<p> Messages: </p> <code> /verifymessage/?message=[message]&signer=[signer]&signature=[signature] </code> <br>
<p> Hashes: </p> <code> /verifyhash/?hash=[hash]&signer=[signer]&signature=[signature] </code> <br>
<p> Identity information: </p> <code> /getid/?id=[identity] </code>
"""
@app.route("/verifyhash/", methods=["GET"])
def filehash():
keys = list(request.args.keys())
if 'hash' not in keys:
return {"error" : 2, "error_detail" : "No filehash specified"},400
elif 'signature' and 'signer' not in keys:
return {"error" : 1, "error_text":"Missing signature and/or signer"},400
signature = '+'.join(request.args['signature'].split(' '))
signer = request.args['signer']
fh = request.args['hash']
if len(signature) == 0 or len(signer) == 0:
return {"error" : 1, "error_text":"Missing signature and/or signer"},400
if fh is not None and len(fh) != 0:
result = verusverify(fh, signer, signature, 'verifyhash', rpcid='verifyhash')['result']
if result:
return {"valid" : "true"}
else:
return {"valid" : "false"}
else:
return {"error" : 2, "error_detail" : "No hash specified"}
@app.route("/verifymessage/", methods=["GET"])
def message():
keys = list(request.args.keys())
if 'message' not in keys:
return {"error" : 2, "error_detail" : "No message specified"},400
elif 'signature' and 'signer' not in keys:
return {"error" : 1, "error_text":"Missing signature and/or signer"},400
signature = '+'.join(request.args['signature'].split(' '))
signer = request.args['signer']
message = request.args['message']
if len(signature) == 0 or len(signer) == 0:
return {"error" : 1, "error_text":"Missing signature and/or signer"},400
if message is not None and len(message) > 0:
result = verusverify(message, signer, signature, 'verifymessage', rpcid='verifymessage')['result']
if result:
return {"valid" : "true"}
else:
return {"valid" : "false"}
else:
return {"error" : 2, "error_detail" : "No message specified"}
@app.route("/getid/", methods=["GET"])
def getid():
if request.args['id'] is None or len(request.args['id']) == 0:
return {"error" : 2, "error_detail" : "No identity specified"},400
else:
return verusidentity(request.args['id'])
#------# Run #------#
if __name__ == '__main__':
if apiconf.ssl:
context = (apiconf.sslcrt, apiconf.sslkey)
app.run(host=apiconf.host, port=apiconf.port, ssl_context = context)
else:
app.run(host=apiconf.host, port=apiconf.port)