-
Notifications
You must be signed in to change notification settings - Fork 7
/
cryptoconditions.py
executable file
·62 lines (41 loc) · 1.36 KB
/
cryptoconditions.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
#!/usr/bin/env python
import sys
import json
import ctypes
import base64
import os.path
import argparse
from ctypes import *
so = cdll.LoadLibrary('.libs/libcryptoconditions.so')
so.cc_jsonRPC.restype = c_char_p
def jsonRPC(method, params, load=True):
out = so.cc_jsonRPC(json.dumps({
'method': method,
'params': params,
}))
return json.loads(out) if load else out
def b16_to_b64(b16):
return base64.urlsafe_b64encode(base64.b16decode(b16)).rstrip('=')
USAGE = "cryptoconditions [-h] {method} {request_json}"
def get_help():
methods = jsonRPC("listMethods", {})['methods']
txt = USAGE + "\n\nmethods:\n"
for method in methods:
txt += ' %s: %s\n' % (method['name'], method['description'])
txt += """\noptional arguments:
-h, --help show this help message and exit
"""
return txt
def get_parser():
class Parser(argparse.ArgumentParser):
def format_help(self):
return get_help()
parser = Parser(description='Crypto Conditions JSON interface', usage=USAGE)
json_loads = lambda r: json.loads(r)
json_loads.__name__ = 'json'
parser.add_argument("method")
parser.add_argument("request", type=json_loads)
return parser
if __name__ == '__main__':
args = get_parser().parse_args()
print(jsonRPC(args.method, args.request, load=False))