def validate_node_address(address: str): if isinstance(address, str) and len(address) == 42: prefix, body = _split_icon_address(address) if prefix != 'hx' or not _is_lowercase_hex_string(body): raise InvalidFormatException("Invalid hx Address") else: raise InvalidFormatException("Invalid hx Address")
def validate_port(port: str, validating_field: str): try: port = int(port, 10) except ValueError: raise InvalidFormatException(f'Invalid {validating_field} format. port: "{port}"') if not 0 < port < 65536: raise InvalidFormatException(f"Invalid {validating_field} format. Port out of range: {port}")
def validate_p2p_endpoint(p2p_endpoint: str): network_locate_info = p2p_endpoint.split(":") if len(network_locate_info) != 2: raise InvalidFormatException("Invalid endpoint format. endpoint must have port info") validate_port(network_locate_info[1], ConstantKeys.P2P_ENDPOINT) if ENDPOINT_IP_PATTERN.match(p2p_endpoint): return if not ENDPOINT_DOMAIN_NAME_PATTERN.match(p2p_endpoint.lower()): raise InvalidFormatException("Invalid endpoint format")
def validate_prep_data(data: dict, blank_able: bool = False): if not blank_able: for key in fields_to_validate: if key not in data: raise InvalidFormatException(f'"{key}" not found') elif len(data[key].strip()) < 1: raise InvalidFormatException("Can not set empty data") for key in data: if len(data[key].strip()) < 1 and not blank_able: raise InvalidFormatException("Can not set empty data") validate_field_in_prep_data(key, data[key])
def _check_keystore(password: str): if not password: password = getpass.getpass("Input your keystore password: "******"Retype your keystore password: "******"Sorry, passwords do not match. Failed to make keystore file") if not validate_password(password): raise InvalidFormatException( "Password must be at least 8 characters long including alphabet, number, " "and special character.") return password
def validate_uri(uri: str): uri = uri.lower() if WEBSITE_DOMAIN_NAME_PATTERN.match(uri): return if WEBSITE_IP_PATTERN.match(uri): return raise InvalidFormatException("Invalid uri format")
def validate_field_in_prep_data(key: str, value: str): if not validate_field_key(key): raise InvalidFormatException(f"Invalid key : {key}") if key == ConstantKeys.P2P_ENDPOINT: validate_p2p_endpoint(value) elif key in (ConstantKeys.WEBSITE, ConstantKeys.DETAILS): validate_uri(value) elif key == ConstantKeys.EMAIL: validate_email(value) elif key == ConstantKeys.COUNTRY: validate_country(value)
def validate_country(country_code: str): if country_code.upper() not in iso3166.countries_by_alpha3: raise InvalidFormatException("Invalid alpha-3 country code")
def validate_email(email: str): if not EMAIL_PATTERN.match(email): raise InvalidFormatException("Invalid email format")