Skip to content

Commit 597ccbc

Browse files
added a message modal, added new trasaction method, adding a transaction to the transaction and getting the chain number
1 parent c8e1532 commit 597ccbc

File tree

2 files changed

+79
-3
lines changed

2 files changed

+79
-3
lines changed

blockchain/blockchain.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from flask import Flask, render_template, jsonify
1+
from flask import Flask, render_template, jsonify, request
22
from time import time
33
from flask_cors import CORS
4+
from collections import OrderedDict
45

56

67
class Blockchain:
@@ -25,22 +26,50 @@ def create_block(self, nonce, previous_hash):
2526
self.transactions = []
2627
self.chain.append(block)
2728

29+
def submit_transaction(self, sender_public_key, recipient_public_key, signature, amount):
30+
# TODO: reward miner
31+
# TODO: Signature validation
32+
33+
transaction = OrderedDict({
34+
'sender_public_key': sender_public_key,
35+
'recipient_public_key': recipient_public_key,
36+
'signature': signature,
37+
'amount': amount,
38+
})
39+
signature_verification = True
40+
if signature_verification:
41+
self.transactions.append(transaction)
42+
return len(self.chain) + 1
43+
else:
44+
return False
45+
2846

2947
blockchain = Blockchain()
3048

3149
# =======================================================================================================================
3250
app = Flask(__name__)
3351
CORS(app)
3452

53+
3554
@app.route("/")
3655
def index():
3756
return render_template('./index.html')
3857

3958

4059
@app.route("/transactions_new", methods=['POST'])
4160
def transactions_new():
42-
response = {"message": "ok"}
43-
return jsonify(response), 201
61+
values = request.form
62+
63+
# TODO: check required fields
64+
transaction_results = blockchain.submit_transaction(values['confirmation_sender_public_key'],
65+
values['confirmation_recipient_public_key'],
66+
values['transaction_signature'], values['confirmation_amount'])
67+
if transaction_results == False:
68+
response = {"message": "Invalid Transaction/Signature"}
69+
return jsonify(response), 406
70+
else:
71+
response = {"message": "Added Transaction " + str(transaction_results)}
72+
return jsonify(response), 201
4473

4574

4675
if __name__ == '__main__':

blockchain_client/templates/make_transaction.html

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,29 @@ <h4 class="card-title">Send Coins:</h4>
158158
</div>
159159
</div>
160160

161+
<!-- Alert Message for successful transaction -->
162+
<div class="modal modal-alert fade" id="success_transaction_modal" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
163+
<div class="modal-dialog">
164+
<div class="modal-content">
165+
166+
<div class="modal-header">
167+
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
168+
</div>
161169

170+
<div class="modal-body">
171+
<div class="alert alert-success" role="alert">
172+
<h4 class="alert-heading">Successful Transaction!</h4>
173+
<p>You successfully completed your transaction. It will be added to the next block.</p>
174+
</div>
175+
</div>
176+
177+
<div class="modal-footer">
178+
<button type="button" id="button_confirm_transaction" class="btn btn-success" data-dismiss="modal">OK</button>
179+
</div>
180+
181+
</div>
182+
</div>
183+
</div>
162184

163185

164186
<script src="/static/vendor/jquery/jquery.min.js"></script>
@@ -186,6 +208,31 @@ <h4 class="card-title">Send Coins:</h4>
186208
}
187209
});
188210
});
211+
212+
$('#button_confirm_transaction').click(function() {
213+
$.ajax({
214+
url: document.getElementById('node_url').value + '/transactions_new',
215+
type: 'POST',
216+
dataType: 'json',
217+
header: {'Access-Control-Allow-Origin': '*'},
218+
data: $('#confirmation_transaction_form').serialize(),
219+
success: function(response) {
220+
$('#sender_public_key').val('');
221+
$('#sender_private_key').val('');
222+
$('#recipient_public_key').val('');
223+
$('#amount').val('');
224+
225+
$('#basic_modal').modal('hide');
226+
$('#success_transaction_modal').modal('show');
227+
228+
},
229+
error: function(error) {
230+
console.log(error);
231+
}
232+
233+
});
234+
});
235+
189236
});
190237
</script>
191238
</body>

0 commit comments

Comments
 (0)