diff --git a/common/tools.py b/common/tools.py index b02c1c4..ccd5973 100644 --- a/common/tools.py +++ b/common/tools.py @@ -1,10 +1,23 @@ import re -import os import logging from pathlib import Path from common.constants import LOCATOR_LEN_HEX +def check_compressed_pk_format(compressed_pk): + """ + Checks if a given value is a 33-byte hex encoded string. + + Args: + compressed_pk(:obj:`str`): the value to be checked. + + Returns: + :obj:`bool`: Whether or not the value matches the format. + """ + + return isinstance(compressed_pk, str) and re.match(r"^0[2-3][0-9A-Fa-f]{64}$", compressed_pk) is not None + + def check_sha256_hex_format(value): """ Checks if a given value is a 32-byte hex encoded string. diff --git a/teos/gatekeeper.py b/teos/gatekeeper.py index 0d5131b..2b812fd 100644 --- a/teos/gatekeeper.py +++ b/teos/gatekeeper.py @@ -1,5 +1,4 @@ -import re - +from common.tools import check_compressed_pk_format from common.cryptographer import Cryptographer @@ -33,20 +32,6 @@ class Gatekeeper: self.default_slots = default_slots self.registered_users = {} - @staticmethod - def check_user_pk(user_pk): - """ - Checks if a given value is a 33-byte hex encoded string. - - Args: - user_pk(:obj:`str`): the value to be checked. - - Returns: - :obj:`bool`: Whether or not the value matches the format. - """ - - return isinstance(user_pk, str) and re.match(r"^[0-9A-Fa-f]{66}$", user_pk) is not None - def add_update_user(self, user_pk): """ Adds a new user or updates the subscription of an existing one, by adding additional slots. @@ -58,7 +43,7 @@ class Gatekeeper: :obj:`int`: the number of available slots in the user subscription. """ - if not self.check_user_pk(user_pk): + if not check_compressed_pk_format(user_pk): raise ValueError("provided public key does not match expected format (33-byte hex string)") if user_pk not in self.registered_users: