Moves (and renames) check_user_pk to common so cli can use it too. Fixes the regex so it only accepts {02, 03} as prefix

This commit is contained in:
Sergi Delgado Segura
2020-03-30 12:44:48 +02:00
parent e924b57efc
commit 33966e59e1
2 changed files with 16 additions and 18 deletions

View File

@@ -1,10 +1,23 @@
import re import re
import os
import logging import logging
from pathlib import Path from pathlib import Path
from common.constants import LOCATOR_LEN_HEX 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): def check_sha256_hex_format(value):
""" """
Checks if a given value is a 32-byte hex encoded string. Checks if a given value is a 32-byte hex encoded string.

View File

@@ -1,5 +1,4 @@
import re from common.tools import check_compressed_pk_format
from common.cryptographer import Cryptographer from common.cryptographer import Cryptographer
@@ -33,20 +32,6 @@ class Gatekeeper:
self.default_slots = default_slots self.default_slots = default_slots
self.registered_users = {} 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): def add_update_user(self, user_pk):
""" """
Adds a new user or updates the subscription of an existing one, by adding additional slots. 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. :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)") raise ValueError("provided public key does not match expected format (33-byte hex string)")
if user_pk not in self.registered_users: if user_pk not in self.registered_users: