Files
python-teos/pisa-btc/apps/pisa-cli.py
Sergi Delgado Segura 9aea751307 Fixes several bugs related with the encrypted blob
Uses a proper KDF to extend the LSB of the txid (HKDF). Standardizes the representation of the keys / hashes in both sides (encrypt/decrypt deals with bytes now instead of string). Adds further logging.
2019-08-09 15:00:02 +01:00

98 lines
3.2 KiB
Python

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
commands = ['add_appointment']
def build_appointment(tx, tx_id, start_block, end_block, dispute_delta, debug, logging):
locator = tx_id[:32]
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, debug, logging)
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) != 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
def show_usage():
print("usage: python pisa-cli.py argument [additional_arguments]."
"\nArguments:"
"\nadd_appointment appointment: \tregisters a json formatted appointment "
"\nhelp: \t\tshows this message.")
if __name__ == '__main__':
debug = False
command = None
opts, args = getopt(argv[1:], 'a:d', ['add_appointment, debug'])
for opt, arg in opts:
if opt in ['-a', '--add_appointment']:
if arg:
if not os.path.isfile(arg):
raise Exception("Can't find file " + arg)
else:
command = 'add_appointment'
json_file = arg
else:
raise Exception("Path to appointment_data.json missing.")
if opt in ['-d', '--debug']:
debug = True
# Configure logging
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO, handlers=[
logging.FileHandler(CLIENT_LOG_FILE),
logging.StreamHandler()
])
if command in commands:
if command == 'add_appointment':
appointment_data = json.load(open(json_file))
valid_locator = check_txid_format(appointment_data.get('tx_id'))
if valid_locator:
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'), debug, logging)
if debug:
logging.info("[Client] sending appointment to PISA")
r = requests.post(url=pisa_url, json=json.dumps(appointment))
if debug:
logging.info("[Client] {} (code: {})".format(r.text, r.status_code))
else:
raise ValueError("The provided locator is not valid.")
else:
show_usage()