Repository navigation
Expand file tree
/
Copy pathapp solution.py
More file actions
96 lines (81 loc) · 2.38 KB
/
Copy pathapp solution.py
File metadata and controls
96 lines (81 loc) · 2.38 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
from flask import Flask, jsonify, request
from flask_cors import CORS
from blockchain import Blockchain
from argparse import ArgumentParser
import requests
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():
block = test.addBlcok()
if block != None:
dictBlock = block.__dict__.copy()
dictBlock['transactions'] = [tx.__dict__ for tx in dictBlock['transactions']]
res = {
'Block': dictBlock,
}
return jsonify(res), 201
else:
res = {
'Error': 'Not able to mine a block',
}
return jsonify(res), 500
@app.route('/opentxs', methods=['GET'])
def opentxs():
txs = test.unconfirmed
if txs != None:
dictTxs = [tx.__dict__ for tx in txs]
res = {
'transactions': dictTxs
}
return jsonify(res), 200
else:
res = {
'Error': 'There is no a transaction',
}
return jsonify(res), 500
@app.route('/sendtx', methods=['POST'])
def sendtx():
values = request.get_json()
if not values:
res = {
'message': 'There is no input'
}
return jsonify(res), 400
required = ['sender', 'receiver', 'amount']
if not all(key in values for key in required):
res = {
'Message': 'missing a value'
}
return jsonify(res), 400
sender = values['sender']
receiver = values['receiver']
amount = values['amount']
passedTransaction = test.addTransaction(sender, receiver, amount)
if passedTransaction != None:
res = {'transaction': {
'sender': values['sender'],
'receiver': values['receiver'],
'amount': values['amount']
}
}
return jsonify(res), 200
else:
res = {
'Error': 'The transaction did 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)