-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
155 lines (124 loc) · 4.54 KB
/
backend.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
#!/usr/bin/env python3
# encoding: utf-8
from flask import Flask
import flask
from utils import myeth_utils
from utils import aes_utils
from utils import onchain_utils
from utils import req_utils
import json
import traceback
import sys
app = Flask(__name__)
@app.route('/')
def index():
return flask.send_from_directory('static', 'index.html')
@app.route('/app/<path:filename>')
def serve_static(filename):
return flask.send_from_directory('static', filename)
# Note the order is very important
@app.route("/will/create", methods=['POST'])
def create_will():
num_signature = flask.request.form.get('num_sig')
raw_data = flask.request.form.get('raw_data')
try:
req_utils.CheckCreateInput(num_signature, raw_data)
except Exception:
traceback.print_exc()
exc_tuple = sys.exc_info()
return json.dumps({
'success': False,
'error': str(exc_tuple[1]).strip()
})
num_signature = int(num_signature)
my_private_key = myeth_utils.GenerateRandomPrivateKeyInBytes()
private_keys = [myeth_utils.GenerateRandomPrivateKeyInBytes() for _ in range(num_signature)]
encrypt_data = str(raw_data)
for enc_private_key in private_keys:
encrypt_data = aes_utils.AESEncrypt(enc_private_key, encrypt_data)
onchain_utils.CreateWillToOnchain(myeth_utils.PrivToAddr(my_private_key),
encrypt_data)
return json.dumps({
'success': True,
'my_private_key': my_private_key.hex(),
'others_private_key': dict(zip(range(num_signature), [_.hex() for _ in private_keys])),
'encrypt_data': encrypt_data
})
@app.route("/will/update", methods=['POST'])
def update_will():
my_private_key = flask.request.form.get('my_private_key')
raw_data = flask.request.form.get('raw_data')
other_private_keys_dict = flask.request.form.get('others_private_key')
try:
req_utils.CheckUpdateInput(my_private_key, other_private_keys_dict, raw_data)
except Exception:
traceback.print_exc()
exc_tuple = sys.exc_info()
return json.dumps({
'success': False,
'error': str(exc_tuple[1]).strip()
})
other_private_keys = req_utils.ConvertOtherPrivateKeysStr(other_private_keys_dict)
encrypt_data = str(raw_data)
for enc_private_key in other_private_keys:
encrypt_data = aes_utils.AESEncrypt(enc_private_key, encrypt_data)
onchain_utils.UpdateWillToOnchain(myeth_utils.PrivToAddr(my_private_key),
encrypt_data)
return json.dumps({
'success': True,
'my_private_key': my_private_key,
'others_private_key': other_private_keys_dict,
'encrypt_data': encrypt_data
})
@app.route("/will/delete", methods=['POST'])
def delete_will():
my_private_key = flask.request.form.get('my_private_key')
try:
req_utils.CheckDeleteInput(my_private_key)
except Exception:
traceback.print_exc()
exc_tuple = sys.exc_info()
return json.dumps({
'success': False,
'error': str(exc_tuple[1]).strip()
})
onchain_utils.DeleteWillToOnchain(myeth_utils.PrivToAddr(my_private_key))
return json.dumps({
'success': True
})
@app.route("/will/retrieve", methods=['POST'])
def retrieve_will():
my_private_key = flask.request.form.get('my_private_key')
other_private_keys_dict = flask.request.form.get('others_private_key')
try:
req_utils.CheckRetrieveInput(my_private_key, other_private_keys_dict)
except Exception as e:
traceback.print_exc()
exc_tuple = sys.exc_info()
return json.dumps({
'success': False,
'error': str(exc_tuple[1]).strip()
})
encrypt_data = onchain_utils.RetrieveWillToOnchain(myeth_utils.PrivToAddr(my_private_key))
if not encrypt_data:
return json.dumps({
'success': True,
'raw_data': ''
})
other_private_keys = req_utils.ConvertOtherPrivateKeysStr(other_private_keys_dict)
try:
for decrypt_private_key in other_private_keys:
encrypt_data = aes_utils.AESDecrypt(decrypt_private_key, encrypt_data)
except Exception:
traceback.print_exc()
exc_tuple = sys.exc_info()
return json.dumps({
'success': False,
'error': str(exc_tuple[1]).strip()
})
return json.dumps({
'success': True,
'raw_data': encrypt_data
})
if __name__ == "__main__":
app.run(host='0.0.0.0', port=31313, debug=True)