Skip to content

Commit 8c4a976

Browse files
committed
Serialize the transaction
1 parent 7bd90f9 commit 8c4a976

3 files changed

Lines changed: 49 additions & 6 deletions

File tree

backend/app/__init__.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
import requests
33
import random
44

5-
from flask import Flask, jsonify
5+
from flask import Flask, jsonify, request
66

77
from backend.blockchain.blockchain import Blockchain
8+
from backend.wallet.wallet import Wallet
9+
from backend.wallet.transaction import Transaction
810
from backend.pubsub import PubSub
911

1012
app = Flask(__name__)
1113
blockchain = Blockchain()
14+
wallet = Wallet()
1215
pubsub = PubSub(blockchain)
1316

1417
@app.route('/')
@@ -30,6 +33,19 @@ def route_blockchain_mine():
3033

3134
return jsonify(block.to_json())
3235

36+
@app.route('/wallet/transact', methods=['POST'])
37+
def route_wallet_transact():
38+
transaction_data = request.get_json()
39+
transaction = Transaction(
40+
wallet,
41+
transaction_data['recipient'],
42+
transaction_data['amount']
43+
)
44+
45+
print(f'transaction.to_json(): {transaction.to_json()}')
46+
47+
return jsonify(transaction.to_json())
48+
3349
ROOT_PORT = 5000
3450
PORT = ROOT_PORT
3551

backend/wallet/transaction.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ def update(self, sender_wallet, recipient, amount):
6060

6161
self.input = self.create_input(sender_wallet, self.output)
6262

63+
def to_json(self):
64+
"""
65+
Serialize the transaction.
66+
"""
67+
return self.__dict__
68+
6369
@staticmethod
6470
def is_valid_transaction(transaction):
6571
"""

backend/wallet/wallet.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
from backend.config import STARTING_BALANCE
55
from cryptography.hazmat.backends import default_backend
66
from cryptography.hazmat.primitives.asymmetric import ec
7-
from cryptography.hazmat.primitives import hashes
7+
from cryptography.hazmat.primitives.asymmetric.utils import (
8+
encode_dss_signature,
9+
decode_dss_signature
10+
)
11+
from cryptography.hazmat.primitives import hashes, serialization
812
from cryptography.exceptions import InvalidSignature
913

1014
class Wallet:
@@ -21,24 +25,41 @@ def __init__(self):
2125
default_backend()
2226
)
2327
self.public_key = self.private_key.public_key()
28+
self.serialize_public_key()
2429

2530
def sign(self, data):
2631
"""
2732
Generate a signature based on the data using the local private key.
2833
"""
29-
return self.private_key.sign(
34+
return decode_dss_signature(self.private_key.sign(
3035
json.dumps(data).encode('utf-8'),
3136
ec.ECDSA(hashes.SHA256())
32-
)
37+
))
38+
39+
def serialize_public_key(self):
40+
"""
41+
Reset the public key to its serialized version.
42+
"""
43+
self.public_key = self.public_key.public_bytes(
44+
encoding=serialization.Encoding.PEM,
45+
format=serialization.PublicFormat.SubjectPublicKeyInfo
46+
).decode('utf-8')
3347

3448
@staticmethod
3549
def verify(public_key, data, signature):
3650
"""
3751
Verify a signature based on the original public key and data.
3852
"""
53+
deserialized_public_key = serialization.load_pem_public_key(
54+
public_key.encode('utf-8'),
55+
default_backend()
56+
)
57+
58+
(r, s) = signature
59+
3960
try:
40-
public_key.verify(
41-
signature,
61+
deserialized_public_key.verify(
62+
encode_dss_signature(r, s),
4263
json.dumps(data).encode('utf-8'),
4364
ec.ECDSA(hashes.SHA256())
4465
)

0 commit comments

Comments
 (0)