3 from flask import request, jsonify, make_response, current_app, render_template
4 #from sqlalchemy.exc import SQLAlchemyError, IntegrityError
7 from .Connection import Connection
9 from ..models import KeyPair, PeginAddress
12 @api.route('/create_key_pair', methods=['POST', 'GET', 'OPTIONS'])
13 def create_key_pair():
14 keypairs = KeyPair.query.all()
15 if len(keypairs) > 5 :
16 return json_contents(jsonify(code=200, msg="There are already 5 private-public key pairs"))
18 connection = Connection("http://127.0.0.1:8888")
19 response = connection.request("/create-key-pair")
20 resp_json = json.loads(response.text)
21 if resp_json['status'] == 'success':
22 xprv = resp_json['data']['xprv']
23 xpub = resp_json['data']['xpub']
24 key = KeyPair(xprv=xprv,xpub=xpub)
27 elif resp_json['status'] == 'fail':
28 return json_contents(jsonify(code=-1, msg=resp_json['msg']))
30 return json_contents(jsonify(code=-1, msg="create key pair fail"))
32 return json_contents(jsonify(code=200, msg=""))
34 @api.route('/get_key_pair',methods=['POST', 'GET', 'OPTIONS'])
36 keypairs = KeyPair.query.all()
40 for keypair in keypairs:
42 "xprv":keypair.xprv.encode('utf-8'),
43 "xpub": keypair.xpub.encode('utf-8')
45 json_data = json_data + json.dumps(data)
48 json_data = json_data + ","
49 return json_contents(jsonify(code=200, msg="sucess", data="[" + json_data + "]"))
52 @api.route('/create_pegin_address', methods=['GET', 'POST', 'OPTIONS'])
53 def create_pegin_address():
54 if not request.json or not 'account_id' in request.json:
55 return json_contents(jsonify(code=-1, msg="The json format is incorrect"))
56 accountID = request.json['account_id']
57 connSide = Connection("http://127.0.0.1:8888")
58 body_json = {"account_id": accountID}
59 response = connSide.request("/get-pegin-address",body_json)
60 resp_json = json.loads(response.text)
61 if resp_json['status'] == 'success':
62 mainchain_address = resp_json['data']['mainchain_address']
63 claim_script = resp_json['data']['claim_script']
64 pegin = PeginAddress(address=mainchain_address, claim_script=claim_script)
67 elif resp_json['status'] == 'fail':
68 return json_contents(jsonify(code=-1, msg=resp_json['msg']))
70 return json_contents(jsonify(code=-1, msg="get pegin address fail"))
71 #return render_template('peginaddress.html', mainchain_address=mainchain_address,claim_script=claim_script)
72 return json_contents(jsonify(code=200, msg=""))
74 @api.route('/get_pegin_address',methods=['POST', 'GET', 'OPTIONS'])
75 def get_pegin_address():
76 peginAddrs = PeginAddress.query.all()
80 for peginAddr in peginAddrs:
82 "mainchain_address":peginAddr.address.encode('utf-8'),
83 "claim_script": peginAddr.claim_script.encode('utf-8')
85 json_data = json_data + json.dumps(data)
88 json_data = json_data + ","
89 return json_contents(jsonify(code=200, msg="sucess", data="[" + json_data + "]"))
92 @api.route('/claim_tx', methods=['GET', 'POST', 'OPTIONS'])
95 if not request.json or not 'claim_script' in request.json or not 'block_height' in request.json or not 'tx_id' in request.json or not 'password' in request.json:
96 return json_contents(jsonify(code=-1, msg="The json format is incorrect"))
97 block_height = int(request.json['block_height'])
98 tx_id = request.json['tx_id'].encode('utf-8')
99 password = request.json['password'].encode('utf-8')
100 claim_script = request.json['claim_script'].encode('utf-8')
101 connSide = Connection("http://127.0.0.1:8888")
102 connMain = Connection("http://127.0.0.1:9888")
108 body_json = {"block_height": block_height}
109 response = connMain.request("/get-raw-block",body_json)
110 resp_json = json.loads(response.text)
111 if resp_json['status'] == 'success':
112 raw_block = resp_json['data']['raw_block'].encode('utf-8')
113 elif resp_json['status'] == 'fail':
114 return json_contents(jsonify(code=-1, msg="get-raw-block: " + resp_json['msg']))
116 return json_contents(jsonify(code=-1, msg="get raw block fail"))
119 body_json = {"tx_id": tx_id,"raw_block": raw_block}
120 response = connSide.request("/get-raw-transaction",body_json)
121 resp_json = json.loads(response.text.encode('utf-8'))
122 if resp_json['status'] == 'success':
123 raw_transaction = resp_json['data']['raw_transaction'].encode('utf-8')
124 block_hash = resp_json['data']['block_hash'].encode('utf-8')
125 elif resp_json['status'] == 'fail':
126 return json_contents(jsonify(code=-1, msg="get-raw-transaction: " + resp_json['msg']))
128 return json_contents(jsonify(code=-1, msg="get raw transaction fail"))
131 body_json = {"tx_id": tx_id,"block_hash": block_hash}
132 response = connMain.request("/get-merkle-proof",body_json)
133 resp_json = json.loads(response.text)
134 if resp_json['status'] == 'success':
135 proof = json.dumps(resp_json['data']).strip('{}')
136 elif resp_json['status'] == 'fail':
137 return json_contents(jsonify(code=-1, msg="get-merkle-proof:" + resp_json['msg']))
139 return json_contents(jsonify(code=-1, msg="get raw transaction fail"))
142 body_json = '{"password": "%s","raw_transaction": "%s","claim_script":"%s",%s}' % (password,raw_transaction,claim_script,proof)
144 response = connSide.request("/claim-pegin-transaction",json.loads(body_json))
145 resp_json = json.loads(response.text)
146 if resp_json['status'] == 'success':
147 return json_contents(jsonify(code=200, msg=resp_json['data']))
148 elif resp_json['status'] == 'fail':
149 return json_contents(jsonify(code=-1, msg="claim-pegin-transaction: " + resp_json['msg']))
151 return json_contents(jsonify(code=-1, msg="claim pegin transaction fail"))
154 @api.route('/send_to_mainchain', methods=['GET', 'POST', 'OPTIONS'])
155 def send_to_mainchain():
156 #if not request.json or not 'claim_script' in request.json or not 'xprvs' in request.json or not 'tx_id' in request.json or not 'id' in request.json or not 'side_tx_id' in request.json or not 'block_height' in request.json or not 'side_block_height' in request.json or not 'alias' in request.json or not 'root_xpubs' in request.json or not 'address' in request.json or not 'control_program' in request.json:
157 if not request.json or not 'claim_script' in request.json or not 'tx_id' in request.json or not 'id' in request.json or not 'side_tx_id' in request.json or not 'block_height' in request.json or not 'side_block_height' in request.json or not 'alias' in request.json or not 'address' in request.json or not 'control_program' in request.json:
158 return json_contents(jsonify(code=-1, msg="The json format is incorrect"))
160 connSide = Connection("http://127.0.0.1:8888")
161 connMain = Connection("http://127.0.0.1:9888")
162 tx_id = request.json['tx_id'].encode('utf-8')
163 id = request.json['id'].encode('utf-8')
164 block_height = int(request.json['block_height'])
165 claim_script = request.json['claim_script'].encode('utf-8')
169 body_json = {"block_height": block_height}
170 response = connMain.request("/get-raw-block",body_json)
171 resp_json = json.loads(response.text)
172 if resp_json['status'] == 'success':
173 raw_block = resp_json['data']['raw_block'].encode('utf-8')
174 elif resp_json['status'] == 'fail':
175 return json_contents(jsonify(code=-1, msg="get-raw-block: " + resp_json['msg']))
177 return json_contents(jsonify(code=-1, msg="get raw block fail"))
180 address = request.json['address'].encode('utf-8')
181 body_json = {"tx_id": tx_id, "id": id, "raw_block": raw_block, "address": address}
182 response = connSide.request("/get-utxo-from-transaction",body_json)
183 resp_json = json.loads(response.text)
184 if resp_json['status'] == 'success':
185 utxo = json.dumps(resp_json['data']).strip('{}')+"}"
186 elif resp_json['status'] == 'fail':
187 return json_contents(jsonify(code=-1, msg="get-utxo-from-transaction: " + resp_json['msg']))
189 return json_contents(jsonify(code=-1, msg="fail get utxo from transaction"))
191 block_height = int(request.json['side_block_height'])
192 tx_id = request.json['side_tx_id'].encode('utf-8')
193 alias = request.json['alias'].encode('utf-8')
194 #root_xpubs = json.dumps(request.json['root_xpubs']).strip('{}')
196 control_program = request.json['control_program'].encode('utf-8')
198 f = open("key_pair","r")
199 lines = f.readlines()
203 tmp = json.loads(line)
204 xprv = tmp['data']['xprv'].encode('utf-8')
205 xpub = tmp['data']['xpub'].encode('utf-8')
206 key_pair[xprv] = xpub
207 root_xpubs += "\"" + xpub + "\""
210 root_xpubs = root_xpubs + ","
212 root_xpubs = root_xpubs + "]"
213 # 获取侧链raw transaction
214 body_json = {"tx_id": tx_id,"block_height": block_height}
215 response = connSide.request("/get-side-raw-transaction",body_json)
216 resp_json = json.loads(response.text.encode('utf-8'))
217 if resp_json['status'] == 'success':
218 raw_transaction = resp_json['data']['raw_transaction'].encode('utf-8')
219 block_hash = resp_json['data']['block_hash'].encode('utf-8')
220 elif resp_json['status'] == 'fail':
221 return json_contents(jsonify(code=-1, msg="get-side-raw-transaction: " + resp_json['msg']))
223 return json_contents(jsonify(code=-1, msg="get side raw transaction fail"))
226 body_json = '{"claim_script":"%s","raw_transaction": "%s","alias": "%s","control_program":"%s","root_xpubs":%s,%s}' % (claim_script,raw_transaction,alias,control_program,root_xpubs,utxo)
228 response = connSide.request("/build-mainchain-tx",json.loads(body_json))
229 resp_json = json.loads(response.text.encode('utf-8'))
231 if resp_json['status'] == 'success':
232 tmpl = json.dumps(resp_json['data'])
233 elif resp_json['status'] == 'fail':
235 return json_contents(jsonify(code=-1, msg="build-mainchain-tx: " + resp_json['msg']))
237 return json_contents(jsonify(code=-1, msg="build mainchain transaction fail"))
240 #xpubs = request.json['root_xpubs']
241 #xprvs = request.json['xprvs']
242 #for index in range(len(xpubs)):
243 for key,value in key_pair.items():
244 #xprv = request.json['xprv'].encode('utf-8')
245 body_json = '{"xprv": "%s","xpub":"%s","transaction":%s,"claim_script":"%s"}' % (key,value,tmpl,claim_script)
246 response = connSide.request("/sign-with-key",json.loads(body_json))
247 resp_json = json.loads(response.text.encode('utf-8'))
248 if resp_json['status'] == 'success':
249 raw_transaction = resp_json['data']['transaction']['raw_transaction'].encode('utf-8')
250 elif resp_json['status'] == 'fail':
251 return json_contents(jsonify(code=-1, msg="sign-with-key: " + resp_json['msg']))
253 return json_contents(jsonify(code=-1, msg="sign-with-key fail"))
256 body_json = '{"raw_transaction": "%s"}' % (raw_transaction)
257 response = connMain.request("/submit-transaction",json.loads(body_json))
258 resp_json = json.loads(response.text.encode('utf-8'))
259 if resp_json['status'] == 'success':
260 return json_contents(jsonify(code=200, msg=resp_json['data']))
261 elif resp_json['status'] == 'fail':
262 return json_contents(jsonify(code=-1, msg="submit-transaction: " + resp_json['msg']))
264 return json_contents(jsonify(code=-1, msg="submit-transaction fail"))
267 def json_contents(jsonify):
268 response = make_response(jsonify)
269 response.headers['Access-Control-Allow-Origin'] = '*'
270 response.headers['Access-Control-Allow-Methods'] = '*'
271 response.headers['Access-Control-Allow-Headers'] = '*'