From babb746dbd2a3f73fc374ff3838703d9594c0890 Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Thu, 5 Dec 2019 11:02:17 +0100 Subject: [PATCH] Create common package Moves cryptographer to common. Also adds constants and defines the leghtn of the locator to avoid hardcoding it in almost every file --- common/__init__.py | 0 common/constants.py | 8 ++++++++ {pisa => common}/cryptographer.py | 0 pisa/api.py | 7 +------ pisa/inspector.py | 4 +++- test/unit/test_cryptographer.py | 2 +- 6 files changed, 13 insertions(+), 8 deletions(-) create mode 100644 common/__init__.py create mode 100644 common/constants.py rename {pisa => common}/cryptographer.py (100%) diff --git a/common/__init__.py b/common/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/common/constants.py b/common/constants.py new file mode 100644 index 0000000..b577044 --- /dev/null +++ b/common/constants.py @@ -0,0 +1,8 @@ +# Locator +LOCATOR_LEN_HEX = 32 +LOCATOR_LEN_BYTES = LOCATOR_LEN_HEX // 2 + +# HTTP +HTTP_OK = 200 +HTTP_BAD_REQUEST = 400 +HTTP_SERVICE_UNAVAILABLE = 503 diff --git a/pisa/cryptographer.py b/common/cryptographer.py similarity index 100% rename from pisa/cryptographer.py rename to common/cryptographer.py diff --git a/pisa/api.py b/pisa/api.py index 8f1b1a1..32dd51a 100644 --- a/pisa/api.py +++ b/pisa/api.py @@ -8,17 +8,12 @@ from pisa.logger import Logger from pisa.inspector import Inspector from pisa.appointment import Appointment from pisa.block_processor import BlockProcessor +from common.constants import HTTP_OK, HTTP_BAD_REQUEST, HTTP_SERVICE_UNAVAILABLE # ToDo: #5-add-async-to-api app = Flask(__name__) - -HTTP_OK = 200 -HTTP_BAD_REQUEST = 400 -HTTP_SERVICE_UNAVAILABLE = 503 - logger = Logger("API") - watcher = None diff --git a/pisa/inspector.py b/pisa/inspector.py index 81125a7..6694c6a 100644 --- a/pisa/inspector.py +++ b/pisa/inspector.py @@ -8,6 +8,8 @@ from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.serialization import load_pem_public_key from cryptography.exceptions import InvalidSignature +from common.constants import LOCATOR_LEN_HEX + from pisa import errors import pisa.conf as conf from pisa.logger import Logger @@ -64,7 +66,7 @@ class Inspector: rcode = errors.APPOINTMENT_WRONG_FIELD_TYPE message = "wrong locator data type ({})".format(type(locator)) - elif len(locator) != 32: + elif len(locator) != LOCATOR_LEN_HEX: rcode = errors.APPOINTMENT_WRONG_FIELD_SIZE message = "wrong locator size ({})".format(len(locator)) # TODO: #12-check-txid-regexp diff --git a/test/unit/test_cryptographer.py b/test/unit/test_cryptographer.py index 331fcca..9d488df 100644 --- a/test/unit/test_cryptographer.py +++ b/test/unit/test_cryptographer.py @@ -1,6 +1,6 @@ import binascii -from pisa.cryptographer import Cryptographer +from common.cryptographer import Cryptographer from pisa.encrypted_blob import EncryptedBlob from test.unit.conftest import get_random_value_hex