diff --git a/pyproject.toml b/pyproject.toml index 878fcb7..73b679c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "meshcore" -version = "2.1.6" +version = "2.1.7" authors = [ { name="Florent de Lamotte", email="florent@frizoncorrea.fr" }, { name="Alex Wolden", email="awolden@gmail.com" }, diff --git a/src/meshcore/commands/device.py b/src/meshcore/commands/device.py index 73ba033..5e39dcf 100644 --- a/src/meshcore/commands/device.py +++ b/src/meshcore/commands/device.py @@ -1,4 +1,5 @@ import logging +from hashlib import sha256 from typing import Optional from ..events import Event, EventType @@ -184,7 +185,7 @@ class DeviceCommands(CommandHandlerBase): return await self.send(data, [EventType.CHANNEL_INFO, EventType.ERROR]) async def set_channel( - self, channel_idx: int, channel_name: str, channel_secret: bytes + self, channel_idx: int, channel_name: str, channel_secret: bytes = None ) -> Event: logger.debug(f"Setting channel {channel_idx}: name={channel_name}") @@ -192,6 +193,9 @@ class DeviceCommands(CommandHandlerBase): name_bytes = channel_name.encode("utf-8")[:32] name_bytes = name_bytes.ljust(32, b"\x00") + if channel_name.startswith("#") or channel_secret is None: # auto name => key calculated from hash + channel_secret = sha256(channel_name.encode("utf-8")).digest()[0:16] + # Ensure channel secret is exactly 16 bytes if len(channel_secret) != 16: raise ValueError("Channel secret must be exactly 16 bytes")