From a3f96962ff83d02e72488100185dde88da67290d Mon Sep 17 00:00:00 2001 From: agessaman Date: Sun, 15 Feb 2026 19:33:04 -0800 Subject: [PATCH] Refactor advert frame handling in CompanionFrameServer - Improved the `_build_advert_push_frames` function to handle optional fields and ensure thread safety. - Enhanced the `on_advert_received` method to robustly process incoming contact data, including better handling of public keys and optional fields for advert details. - Added error handling to log exceptions during advert processing, improving reliability. --- repeater/companion/frame_server.py | 132 ++++++++++++++++------------- 1 file changed, 74 insertions(+), 58 deletions(-) diff --git a/repeater/companion/frame_server.py b/repeater/companion/frame_server.py index a38bf7c..1a9062a 100644 --- a/repeater/companion/frame_server.py +++ b/repeater/companion/frame_server.py @@ -94,20 +94,28 @@ logger = logging.getLogger("CompanionFrameServer") def _build_advert_push_frames(data: dict) -> tuple[bytes, Optional[bytes]]: """Build PUSH_CODE_ADVERT short frame and optional PUSH_CODE_NEW_ADVERT full frame from extracted data. Thread-safe for asyncio.to_thread.""" - pubkey_b = data["pubkey_b"] + pubkey_b = data.get("pubkey_b", b"") + if isinstance(pubkey_b, bytes): + pubkey_b = pubkey_b[:32].ljust(32, b"\x00") + else: + pubkey_b = b"\x00" * 32 short = bytes([PUSH_CODE_ADVERT]) + pubkey_b if not data.get("include_full"): return (short, None) + op = data.get("out_path", b"") + op = (op if isinstance(op, bytes) else bytes(op or []))[:MAX_PATH_SIZE].ljust(MAX_PATH_SIZE, b"\x00") + nb = data.get("name_b", b"") + nb = (nb if isinstance(nb, bytes) else (nb.encode("utf-8", errors="replace") if isinstance(nb, str) else b""))[:32].ljust(32, b"\x00") full = ( bytes([PUSH_CODE_NEW_ADVERT]) + pubkey_b - + bytes([data["adv_type"], data["flags"], data["opl_byte"]]) - + data["out_path"] - + data["name_b"] - + struct.pack("= 32: