Adds blob encryption on both sides

This commit is contained in:
Sergi Delgado Segura
2019-06-05 16:44:43 +01:00
parent 40cdcfa913
commit fb2bf05057
10 changed files with 146 additions and 42 deletions

View File

@@ -1,16 +1,41 @@
from multiprocessing.connection import Client
import requests
import re
import os
import json
from getopt import getopt
from sys import argv
import logging
from conf import CLIENT_LOG_FILE
from apps.blob import Blob
from apps import PISA_API_SERVER, PISA_API_PORT
import apps.messages as msg
import re
commands = ['add_appointment']
def build_appointment(tx, tx_id, start_block, end_block, dispute_delta):
locator = tx_id[:16]
cipher = "AES-GCM-128"
hash_function = "SHA256"
# FIXME: The blob data should contain more things that just the transaction. Leaving like this for now.
blob = Blob(tx, cipher, hash_function)
# FIXME: tx_id should not be necessary (can be derived from tx SegWit-like). Passing it for now
encrypted_blob = blob.encrypt(tx_id)
appointment = {"locator": locator, "start_block": start_block, "end_block": end_block,
"dispute_delta": dispute_delta, "encrypted_blob": encrypted_blob, "cipher": cipher, "hash_function":
hash_function}
return appointment
def check_txid_format(txid):
if len(txid) != 32:
raise Exception("txid does not matches the expected size (16-byte / 32 hex chars). " + msg.wrong_txid)
if len(txid) != 64:
raise Exception("txid does not matches the expected size (32-byte / 64 hex chars).")
return re.search(r'^[0-9A-Fa-f]+$', txid) is not None
@@ -25,6 +50,12 @@ def show_usage():
if __name__ == '__main__':
opts, args = getopt(argv[1:], '', commands)
# Configure logging
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO, handlers=[
logging.FileHandler(CLIENT_LOG_FILE),
logging.StreamHandler()
])
# Get args
if len(args) > 0:
command = args[0]
@@ -35,18 +66,25 @@ if __name__ == '__main__':
if command in commands:
if len(args) != 2:
raise Exception("txid missing. " + msg.wrong_txid)
raise Exception("Path to appointment_data.json missing.")
arg = args[1]
valid_locator = check_txid_format(arg)
if not os.path.isfile(args[1]):
raise Exception("Can't find file " + args[1])
appointment_data = json.load(open(args[1]))
valid_locator = check_txid_format(appointment_data.get('tx_id'))
if valid_locator:
conn = Client((PISA_API_SERVER, PISA_API_PORT))
pisa_url = "http://{}:{}".format(PISA_API_SERVER, PISA_API_PORT)
appointment = build_appointment(appointment_data.get('tx'), appointment_data.get('tx_id'),
appointment_data.get('start_time'), appointment_data.get('end_time'),
appointment_data.get('dispute_delta'))
# Argv could be undefined, but we only have one command so it's safe for now
conn.send((command, arg))
r = requests.post(url=pisa_url, json=json.dumps(appointment))
logging.info("[Client] {} (code: {})".format(r.text, r.status_code))
else:
raise ValueError("The provided locator is not valid. " + msg.wrong_txid)
raise ValueError("The provided locator is not valid.")
else:
show_usage()