Skip to content
This repository has been archived by the owner on Sep 28, 2023. It is now read-only.

Commit

Permalink
Store transactions in Firebase DB, removed usage of nacl.encoding
Browse files Browse the repository at this point in the history
 - Using bytes.from_hex and bytes.hex instead of nacl.encoding since it is
   planned to be made deprecated
   (pyca/pynacl#532)
 - Unconfirmed transactions are now stored in the firebase realtime database
   since using in-memory variables in cloud run does not guarantee safety.
  • Loading branch information
Samyak2 committed Jun 30, 2020
1 parent ee984be commit 48fd351
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
5 changes: 4 additions & 1 deletion blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from google.cloud import firestore
import firebase_admin as firebase
import firebase_admin.db as firebase_db
from flask_cors import CORS

import encryption

Expand Down Expand Up @@ -57,6 +58,7 @@ def verify_transaction(self):
<sender_public_key receiver_public_key message>
"""
if self.signature is None:
print("No signature")
return False
try:
sender_key = encryption.decode_verify_key(self.sender)
Expand Down Expand Up @@ -395,6 +397,7 @@ def parse_node_addr(addr):

# Instantiate the Node
app = Flask(__name__)
CORS(app)

# custom JSONEncoder for our custom classes
class BlockchatJSONEncoder(JSONEncoder):
Expand Down Expand Up @@ -424,7 +427,7 @@ def custom_obj_hook(self, dct):
node_url = os.getenv("NODE_ADDR", None)
assert node_url is not None
node_url = parse_node_addr(node_url)
node_identifier = encryption.encode_verify_key(node_secret.verify_key).decode()
node_identifier = encryption.encode_verify_key(node_secret.verify_key)

db = firestore.Client.from_service_account_json("./firebase-adminsdk.json")
blockchain_c = db.collection("blockchain")
Expand Down
9 changes: 4 additions & 5 deletions encryption.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import nacl.encoding
import nacl.signing
import nacl.exceptions
import nacl.utils
Expand All @@ -19,13 +18,13 @@ def generate_signing_key() -> (nacl.signing.VerifyKey, nacl.signing.SigningKey):

return verify_key, signing_key

def encode_verify_key(verify_key: nacl.signing.VerifyKey) -> bytes:
def encode_verify_key(verify_key: nacl.signing.VerifyKey) -> str:
"""Encodes given verify_key into a bytes object"""
return verify_key.encode(encoder=nacl.encoding.HexEncoder)
return verify_key.encode().hex()

def decode_verify_key(verify_key_hex: bytes) -> nacl.signing.VerifyKey:
def decode_verify_key(verify_key_hex: str) -> nacl.signing.VerifyKey:
"""Decodes given verify_key_hex into a verify_key"""
return nacl.signing.VerifyKey(verify_key_hex, encoder=nacl.encoding.HexEncoder)
return nacl.signing.VerifyKey(bytes.fromhex(verify_key_hex))

def sign_message(signing_key: nacl.signing.SigningKey, message):
"""Signs message using signing_key"""
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ PyNaCl==1.3.0
gunicorn==20.0.4
google-cloud-firestore==1.7.0
firebase-admin==4.3.0
flask-cors==3.0.8

0 comments on commit 48fd351

Please sign in to comment.