-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
83 lines (71 loc) · 2.22 KB
/
app.py
File metadata and controls
83 lines (71 loc) · 2.22 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
from flask import Flask, jsonify, request
# from flask_cors import CORS
from blockchain import Blockchain
from argparse import ArgumentParser
app = Flask(__name__)
# CORS(app)
@app.route('/', methods=['GET'])
def chian():
chain = test.chain
dictChain = [block.__dict__.copy() for block in chain]
for dictBlock in dictChain:
dictBlock['transactions'] = [tx.__dict__ for tx in dictBlock['transactions']]
return jsonify(dictChain), 200
@app.route('/mine', methods=['POST'])
def mine():
""" mine a block """
@app.route('/opentxs', methods=['GET'])
def opentxs():
""" get the unconfirmed transactions or any transaction has not been included in a block """
txs = test.unconfirmed
if txs != None:
dictTx = [tx.__dict__ for tx in txs]
res = {
'Transactions': dictTx
}
return jsonify(res), 500
else:
res = {
'Message':'There is no transaction'
}
return jsonify(res), 500
@app.route('/sendtx', methods=['POST'])
def sendtx():
""" send a transaction"""
values = request.get_json()
if not values:
res = {
'Message': 'There is no input'
}
return jsonify(res), 400
reqKeys = ['sender', 'receiver','amount']
if not all (key in values for key in reqKeys):
res = {
'Message': 'There is a missing value'
}
return jsonify(res), 400
sender = values['sender']
receiver = values['receiver']
amount = values['amount']
addTx = test.addTransaction(sender, receiver, amount)
if addTx !=None:
res = {
'Transaction': {
'amount': values['amount'],
'sender': values['sender'],
'receiver': values['receiver']
}
}
return jsonify(res), 200
else:
res = {
'Message':'The transaction does not pass'
}
return jsonify(res), 500
if __name__ == '__main__':
ser = ArgumentParser()
ser.add_argument('-p', '--port', default=8020)
args = ser.parse_args()
port = args.port
test = Blockchain()
app.run(debug=True, port=port)