mirror of
https://github.com/rightup/pyMC_Repeater.git
synced 2026-03-28 17:43:06 +01:00
- Introduced `normalize_companion_identity_key` function to standardize identity key formatting by stripping whitespace and removing the optional 0x prefix. - Updated `RepeaterDaemon` to utilize the new normalization function when processing identity keys, improving consistency and error handling. - Added a `stop` method in `CompanionFrameServer` to persist contacts and channels before stopping, ensuring data survives daemon restarts.
26 lines
948 B
Python
26 lines
948 B
Python
"""Shared utilities for Companion (e.g. validation for config sync)."""
|
|
|
|
_INVALID_NODE_NAME_CHARS = "\n\r\x00"
|
|
|
|
|
|
def normalize_companion_identity_key(identity_key: str) -> str:
|
|
"""Strip whitespace and remove optional 0x prefix so fromhex() is consistent across installs."""
|
|
s = identity_key.strip()
|
|
if s.lower().startswith("0x"):
|
|
s = s[2:].strip()
|
|
return s
|
|
|
|
|
|
def validate_companion_node_name(value: str) -> str:
|
|
"""Validate node_name for config sync: non-empty, max 31 bytes UTF-8, no control chars."""
|
|
if not isinstance(value, str):
|
|
raise ValueError("node_name must be a string")
|
|
s = value.strip()
|
|
if not s:
|
|
raise ValueError("node_name cannot be empty")
|
|
if len(s.encode("utf-8")) > 31:
|
|
raise ValueError("node_name too long (max 31 bytes UTF-8)")
|
|
if any(c in s for c in _INVALID_NODE_NAME_CHARS):
|
|
raise ValueError("node_name contains invalid characters")
|
|
return s
|