meshcoredecoder 0.3.0__tar.gz → 0.3.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. {meshcoredecoder-0.3.0/meshcoredecoder.egg-info → meshcoredecoder-0.3.2}/PKG-INFO +1 -1
  2. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/cli.py +3 -0
  3. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/index.py +1 -1
  4. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/__init__.py +1 -1
  5. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/crypto/channel_crypto.py +18 -8
  6. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/crypto/ed25519_verifier.py +14 -10
  7. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/decoder/packet_decoder.py +124 -57
  8. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/decoder/payload_decoders/path.py +37 -18
  9. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/types/packet.py +10 -2
  10. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/types/payloads.py +4 -0
  11. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2/meshcoredecoder.egg-info}/PKG-INFO +1 -1
  12. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/pyproject.toml +1 -1
  13. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/setup.py +1 -1
  14. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/LICENSE +0 -0
  15. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/MANIFEST.in +0 -0
  16. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/README.md +0 -0
  17. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/crypto/__init__.py +0 -0
  18. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/crypto/key_manager.py +0 -0
  19. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/decoder/__init__.py +0 -0
  20. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/decoder/payload_decoders/__init__.py +0 -0
  21. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/decoder/payload_decoders/ack.py +0 -0
  22. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/decoder/payload_decoders/advert.py +0 -0
  23. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/decoder/payload_decoders/anon_request.py +0 -0
  24. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/decoder/payload_decoders/control.py +0 -0
  25. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/decoder/payload_decoders/group_data.py +0 -0
  26. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/decoder/payload_decoders/group_text.py +0 -0
  27. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/decoder/payload_decoders/request.py +0 -0
  28. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/decoder/payload_decoders/response.py +0 -0
  29. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/decoder/payload_decoders/text_message.py +0 -0
  30. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/decoder/payload_decoders/trace.py +0 -0
  31. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/types/__init__.py +0 -0
  32. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/types/crypto.py +0 -0
  33. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/types/enums.py +0 -0
  34. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/utils/__init__.py +0 -0
  35. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/utils/auth_token.py +0 -0
  36. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/utils/enum_names.py +0 -0
  37. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder/utils/hex.py +0 -0
  38. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder.egg-info/SOURCES.txt +0 -0
  39. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder.egg-info/dependency_links.txt +0 -0
  40. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder.egg-info/entry_points.txt +0 -0
  41. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder.egg-info/requires.txt +0 -0
  42. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/meshcoredecoder.egg-info/top_level.txt +0 -0
  43. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/requirements.txt +0 -0
  44. {meshcoredecoder-0.3.0 → meshcoredecoder-0.3.2}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: meshcoredecoder
3
- Version: 0.3.0
3
+ Version: 0.3.2
4
4
  Summary: Complete Python implementation of the MeshCore Packet Decoder
5
5
  Home-page: https://github.com/chrisdavis2110/meshcore-decoder-py
6
6
  Author: Chris Davis
@@ -36,6 +36,9 @@ def print_formatted_packet(packet, keys: Optional[List[str]] = None):
36
36
  print(f'{bold("Payload Type:")} {get_payload_type_name(packet.payload_type)}')
37
37
  print(f'{bold("Total Bytes:")} {packet.total_bytes}')
38
38
 
39
+ # Path comes from the decoder (which applies path-length fallback for repeater-style adverts)
40
+ if packet.path_length and packet.path_length > 0:
41
+ print(f'{bold("Path Length:")} {packet.path_length} byte(s)')
39
42
  if packet.path and len(packet.path) > 0:
40
43
  print(f'{bold("Path:")} {" → ".join(packet.path)}')
41
44
 
@@ -6,7 +6,7 @@ MIT License
6
6
  Main entry point for the MeshCore decoder Python port
7
7
  """
8
8
 
9
- __version__ = "0.3.0"
9
+ __version__ = "0.3.2"
10
10
 
11
11
  # Main decoder
12
12
  from src.decoder.packet_decoder import MeshCorePacketDecoder
@@ -5,4 +5,4 @@ from .decoder.packet_decoder import MeshCorePacketDecoder
5
5
  MeshCoreDecoder = MeshCorePacketDecoder
6
6
 
7
7
  __all__ = ['MeshCoreDecoder', 'MeshCorePacketDecoder']
8
- __version__ = "0.3.0"
8
+ __version__ = "0.3.2"
@@ -1147,20 +1147,30 @@ class ChannelCrypto:
1147
1147
  if not decrypted_bytes or len(decrypted_bytes) < 2:
1148
1148
  return DecryptionResult(success=False, error='Decrypted content too short')
1149
1149
 
1150
- # Parse MeshCore path format: path_len(1) + path(path_len) + extra_type(1) + extra(variable)
1151
- path_len = decrypted_bytes[0]
1152
-
1153
- if len(decrypted_bytes) < 1 + path_len + 1:
1150
+ # Parse MeshCore path format (same encoding as packet header): path_len_byte(1) with
1151
+ # bits 7:6 = hash_size-1 (1/2/3 bytes per hop), bits 5:0 = hop count (0-63).
1152
+ # path_byte_len = hop_count * hash_size; then extra_type(1) + extra(variable).
1153
+ path_len_byte = decrypted_bytes[0]
1154
+ path_hash_size = (path_len_byte >> 6) + 1 # 1, 2, or 3; 4 = reserved
1155
+ path_hash_count = path_len_byte & 63
1156
+ path_byte_len = path_hash_count * path_hash_size
1157
+
1158
+ if path_hash_size == 4:
1159
+ return DecryptionResult(success=False, error='Reserved path hash size in Path payload')
1160
+
1161
+ if len(decrypted_bytes) < 1 + path_byte_len + 1:
1154
1162
  return DecryptionResult(success=False, error='Decrypted content too short for path structure')
1155
1163
 
1156
- path = decrypted_bytes[1:1 + path_len]
1157
- extra_type = decrypted_bytes[1 + path_len]
1158
- extra = decrypted_bytes[2 + path_len:] if len(decrypted_bytes) > 2 + path_len else b''
1164
+ path = decrypted_bytes[1:1 + path_byte_len]
1165
+ extra_type = decrypted_bytes[1 + path_byte_len]
1166
+ extra = decrypted_bytes[2 + path_byte_len:] if len(decrypted_bytes) > 2 + path_byte_len else b''
1159
1167
 
1160
1168
  return DecryptionResult(
1161
1169
  success=True,
1162
1170
  data={
1163
- 'path_len': path_len,
1171
+ 'path_len_byte': path_len_byte,
1172
+ 'path_hash_size': path_hash_size,
1173
+ 'path_hash_count': path_hash_count,
1164
1174
  'path': path,
1165
1175
  'extra_type': extra_type,
1166
1176
  'extra': extra
@@ -34,24 +34,28 @@ class Ed25519SignatureVerifier:
34
34
  app_data_hex: str
35
35
  ) -> bool:
36
36
  """
37
- Verify an Ed25519 signature for MeshCore advertisement packets
37
+ Verify an Ed25519 signature for MeshCore advertisement packets.
38
38
 
39
- According to MeshCore protocol, the signed message for advertisements is:
40
- public_key (32 bytes) + timestamp (4 bytes LE) + app_data (variable)
39
+ Signed message is public_key + timestamp + app_data (docs/payloads.md).
40
+ We also try timestamp + public_key + app_data in case firmware varies.
41
41
  """
42
42
  try:
43
- # Convert hex strings to bytes
44
43
  public_key = hex_to_bytes(public_key_hex)
45
44
  signature = hex_to_bytes(signature_hex)
46
45
  app_data = hex_to_bytes(app_data_hex)
46
+ timestamp_bytes = timestamp.to_bytes(4, byteorder='little')
47
47
 
48
- # Construct the signed message according to MeshCore format
49
- message = Ed25519SignatureVerifier._construct_advert_signed_message(
50
- public_key_hex, timestamp, app_data
51
- )
48
+ # Primary: public_key + timestamp (LE) + app_data (per payloads.md)
49
+ message_pk_first = public_key + timestamp_bytes + app_data
50
+ if Ed25519SignatureVerifier._verify_signature(signature, message_pk_first, public_key):
51
+ return True
52
52
 
53
- # Verify the signature
54
- return Ed25519SignatureVerifier._verify_signature(signature, message, public_key)
53
+ # Alternative: timestamp + public_key + app_data (in case firmware uses this order)
54
+ message_ts_first = timestamp_bytes + public_key + app_data
55
+ if Ed25519SignatureVerifier._verify_signature(signature, message_ts_first, public_key):
56
+ return True
57
+
58
+ return False
55
59
  except Exception as error:
56
60
  print(f'Ed25519 signature verification failed: {error}')
57
61
  return False
@@ -152,40 +152,70 @@ class MeshCorePacketDecoder:
152
152
  ))
153
153
  offset += 4
154
154
 
155
- # Parse path
155
+ # Parse path (multi-byte path encoding per meshcore-decoder PR #8 / Packet.h)
156
+ # Bits 7:6 = hash size selector: (path_len_byte >> 6) + 1 = 1, 2, or 3 bytes per hop
157
+ # Bits 5:0 = hop count (0-63). path_byte_length = hop_count * hash_size
156
158
  if len(bytes_data) < offset + 1:
157
159
  raise ValueError('Packet too short for path length')
158
- path_length = bytes_data[offset]
160
+ path_length_byte = bytes_data[offset]
161
+ path_hash_size, path_hop_count, path_byte_length = MeshCorePacketDecoder._decode_path_len_byte(path_length_byte)
162
+
163
+ payload_start = offset + 1
164
+ remaining_after_path = len(bytes_data) - payload_start
165
+
166
+ # Use legacy (raw path byte count) only when:
167
+ # - Reserved hash size (bits 7:6 = 11), or
168
+ # - Path would extend past packet (path_byte_length > remaining_after_path).
169
+ # Do NOT use min_payload to trigger legacy; valid new-encoding packets can have small payloads.
170
+ use_legacy = False
171
+ if path_hash_size == 4:
172
+ use_legacy = True
173
+ elif path_byte_length > remaining_after_path:
174
+ use_legacy = True
175
+
176
+ if use_legacy:
177
+ # Legacy: path_length byte = raw path byte count (1 byte per hop). Cap at packet and MAX_PATH_SIZE (64).
178
+ path_byte_length = min(path_length_byte, remaining_after_path, 64)
179
+ path_hop_count = path_byte_length
180
+ path_hash_size = 1
159
181
 
160
182
  if include_structure:
161
- if route_type in (RouteType.Direct, RouteType.TransportDirect):
162
- path_length_description = f'For "Direct" packets, this contains routing instructions. {path_length} bytes of routing instructions (decreases as packet travels)'
183
+ hash_desc = f' × {path_hash_size}-byte hashes' if path_hash_size > 1 else ''
184
+ if path_hop_count == 0:
185
+ path_length_description = f'No path data{hash_desc}' if path_hash_size > 1 else 'No path data'
186
+ elif route_type in (RouteType.Direct, RouteType.TransportDirect):
187
+ path_length_description = f'{path_hop_count} hop(s){hash_desc} of routing instructions (decreases as packet travels)'
163
188
  elif route_type in (RouteType.Flood, RouteType.TransportFlood):
164
- path_length_description = f'{path_length} bytes showing route taken (increases as packet floods)'
189
+ path_length_description = f'{path_hop_count} hop(s){hash_desc} showing route taken (increases as packet floods)'
165
190
  else:
166
- path_length_description = f'Path contains {path_length} bytes'
191
+ path_length_description = f'Path: {path_hop_count} hop(s){hash_desc}'
167
192
 
168
193
  segments.append(PacketSegment(
169
194
  name='Path Length',
170
195
  description=path_length_description,
171
196
  start_byte=offset,
172
197
  end_byte=offset,
173
- value=f'0x{path_length:02x}'
198
+ value=f'0x{path_length_byte:02x}'
174
199
  ))
175
200
  offset += 1
176
201
 
177
- if len(bytes_data) < offset + path_length:
202
+ if len(bytes_data) < offset + path_byte_length:
178
203
  raise ValueError('Packet too short for path data')
179
204
 
180
- # Convert path data to hex strings
181
- path_bytes = bytes_data[offset:offset + path_length]
182
- path: Optional[List[str]] = [byte_to_hex(b) for b in path_bytes] if path_length > 0 else None
205
+ # Convert path data to list of hex strings (one per hop, hash_size bytes each)
206
+ path: Optional[List[str]] = None
207
+ if path_hop_count > 0 and path_byte_length > 0:
208
+ path = []
209
+ for i in range(path_hop_count):
210
+ hop_start = offset + i * path_hash_size
211
+ hop_bytes = bytes_data[hop_start:hop_start + path_hash_size]
212
+ path.append(bytes_to_hex(hop_bytes))
183
213
 
184
- if include_structure and path_length > 0:
214
+ if include_structure and path_byte_length > 0:
185
215
  if payload_type == PayloadType.Trace:
186
- # TRACE packets have SNR values in path
216
+ # TRACE packets: path holds SNR values (single-byte entries)
187
217
  snr_values = []
188
- for i in range(path_length):
218
+ for i in range(path_byte_length):
189
219
  snr_raw = bytes_data[offset + i]
190
220
  snr_signed = snr_raw - 256 if snr_raw > 127 else snr_raw
191
221
  snr_db = snr_signed / 4.0
@@ -194,25 +224,24 @@ class MeshCorePacketDecoder:
194
224
  name='Path SNR Data',
195
225
  description=f'SNR values collected during trace: {", ".join(snr_values)}',
196
226
  start_byte=offset,
197
- end_byte=offset + path_length - 1,
198
- value=bytes_to_hex(bytes_data[offset:offset + path_length])
227
+ end_byte=offset + path_byte_length - 1,
228
+ value=bytes_to_hex(bytes_data[offset:offset + path_byte_length])
199
229
  ))
200
230
  else:
231
+ path_description = f'Routing path ({path_hash_size}-byte hash per hop)' if path_hash_size > 1 else 'Routing path information'
201
232
  if route_type in (RouteType.Direct, RouteType.TransportDirect):
202
- path_description = 'Routing instructions (bytes are stripped at each hop as packet travels to destination)'
233
+ path_description = f'Routing instructions ({path_hash_size}-byte hashes stripped at each hop)'
203
234
  elif route_type in (RouteType.Flood, RouteType.TransportFlood):
204
- path_description = 'Historical route taken (bytes are added as packet floods through network)'
205
- else:
206
- path_description = 'Routing path information'
235
+ path_description = f'Historical route taken ({path_hash_size}-byte hashes per hop)'
207
236
 
208
237
  segments.append(PacketSegment(
209
238
  name='Path Data',
210
239
  description=path_description,
211
240
  start_byte=offset,
212
- end_byte=offset + path_length - 1,
213
- value=bytes_to_hex(bytes_data[offset:offset + path_length])
241
+ end_byte=offset + path_byte_length - 1,
242
+ value=bytes_to_hex(bytes_data[offset:offset + path_byte_length])
214
243
  ))
215
- offset += path_length
244
+ offset += path_byte_length
216
245
 
217
246
  # Extract payload
218
247
  payload_bytes = bytes_data[offset:]
@@ -236,6 +265,8 @@ class MeshCorePacketDecoder:
236
265
  decoded_payload = result
237
266
  if result and hasattr(result, 'segments') and result.segments:
238
267
  payload_segments.extend(result.segments)
268
+ if decoded_payload is not None:
269
+ decoded_payload.hash_mode = path_hash_size
239
270
  elif payload_type == PayloadType.Trace:
240
271
  result = TracePayloadDecoder.decode(payload_bytes, path, {'include_segments': include_structure, 'segment_offset': 0})
241
272
  decoded_payload = result
@@ -315,8 +346,10 @@ class MeshCorePacketDecoder:
315
346
  value=bytes_to_hex(payload_bytes)
316
347
  ))
317
348
 
318
- # Calculate message hash
319
- message_hash = MeshCorePacketDecoder._calculate_message_hash(bytes_data, route_type, payload_type, payload_version)
349
+ # Calculate message hash (use same path_byte_length we used for decoding)
350
+ message_hash = MeshCorePacketDecoder._calculate_message_hash(
351
+ bytes_data, route_type, payload_type, payload_version, path_byte_length_override=path_byte_length
352
+ )
320
353
 
321
354
  packet = DecodedPacket(
322
355
  message_hash=message_hash,
@@ -324,11 +357,13 @@ class MeshCorePacketDecoder:
324
357
  payload_type=payload_type,
325
358
  payload_version=payload_version,
326
359
  transport_codes=transport_codes,
327
- path_length=path_length,
360
+ path_length=path_hop_count,
328
361
  path=path,
329
362
  payload={'raw': payload_hex, 'decoded': decoded_payload},
330
363
  total_bytes=len(bytes_data),
331
- is_valid=True
364
+ is_valid=True,
365
+ path_byte_length=path_byte_length,
366
+ path_hash_size=path_hash_size if path_hash_size != 1 else None,
332
367
  )
333
368
 
334
369
  structure = PacketStructure(
@@ -387,7 +422,7 @@ class MeshCorePacketDecoder:
387
422
  advert_payload = result['packet'].payload['decoded']
388
423
  bytes_data = hex_to_bytes(hex_data)
389
424
 
390
- # Calculate payload start offset
425
+ # Calculate payload start offset (use same path_length as decoded packet, which may use fallback)
391
426
  offset = 1 # Skip header
392
427
 
393
428
  # Skip transport codes if present
@@ -395,10 +430,9 @@ class MeshCorePacketDecoder:
395
430
  if route_type in (RouteType.TransportFlood, RouteType.TransportDirect):
396
431
  offset += 4
397
432
 
398
- # Skip path data
399
- if len(bytes_data) > offset:
400
- path_length = bytes_data[offset]
401
- offset += 1 + path_length
433
+ # Skip path data: use the packet's path_byte_length (total path bytes after path_len byte)
434
+ path_byte_length = getattr(result['packet'], 'path_byte_length', result['packet'].path_length) or 0
435
+ offset += 1 + path_byte_length
402
436
 
403
437
  # Get the payload bytes
404
438
  payload_bytes = bytes_data[offset:]
@@ -410,13 +444,11 @@ class MeshCorePacketDecoder:
410
444
  )
411
445
 
412
446
  if verified_advert:
447
+ verified_advert.hash_mode = getattr(advert_payload, 'hash_mode', None)
413
448
  # Update the payload with verification results
414
449
  result['packet'].payload['decoded'] = verified_advert
415
-
416
- # If the advertisement signature is invalid, mark the whole packet as invalid
417
- if not verified_advert.is_valid:
418
- result['packet'].is_valid = False
419
- result['packet'].errors = verified_advert.errors or ['Invalid advertisement signature']
450
+ # Do not mark the whole packet invalid when only the advert signature fails;
451
+ # packet stays valid so decoded advert and signature_valid/errors are visible.
420
452
 
421
453
  # Update structure segments if needed
422
454
  if include_structure and hasattr(verified_advert, 'segments') and verified_advert.segments:
@@ -447,16 +479,18 @@ class MeshCorePacketDecoder:
447
479
  errors.append('Packet too short for transport codes')
448
480
  offset += 4
449
481
 
450
- # Check path length
482
+ # Check path (multi-byte encoding)
451
483
  if len(bytes_data) < offset + 1:
452
484
  errors.append('Packet too short for path length')
453
485
  else:
454
- path_length = bytes_data[offset]
486
+ path_len_byte = bytes_data[offset]
455
487
  offset += 1
456
-
457
- if len(bytes_data) < offset + path_length:
488
+ hash_size, hop_count, byte_length = MeshCorePacketDecoder._decode_path_len_byte(path_len_byte)
489
+ if hash_size == 4:
490
+ errors.append('Invalid path length byte: reserved hash size (bits 7:6 = 11)')
491
+ if len(bytes_data) < offset + byte_length:
458
492
  errors.append('Packet too short for path data')
459
- offset += path_length
493
+ offset += byte_length
460
494
 
461
495
  # Check if we have payload data
462
496
  if offset >= len(bytes_data):
@@ -468,20 +502,59 @@ class MeshCorePacketDecoder:
468
502
  return ValidationResult(is_valid=len(errors) == 0, errors=errors if errors else None)
469
503
 
470
504
  @staticmethod
471
- def _calculate_message_hash(bytes_data: bytes, route_type: RouteType, payload_type: PayloadType, payload_version: PayloadVersion) -> str:
505
+ def _decode_path_len_byte(path_len_byte: int) -> tuple:
506
+ """
507
+ Decode path length byte per meshcore-decoder PR #8 / Packet.h.
508
+ Bits 7:6 = hash size selector: (path_len_byte >> 6) + 1 = 1, 2, or 3 bytes per hop.
509
+ Bits 5:0 = hop count (0-63).
510
+ Returns (hash_size, hop_count, byte_length). hash_size 4 = reserved.
511
+ """
512
+ hash_size = (path_len_byte >> 6) + 1 # 1, 2, or 3; 4 if bits 7:6 = 11 (reserved)
513
+ hop_count = path_len_byte & 63
514
+ byte_length = hop_count * hash_size
515
+ return (hash_size, hop_count, byte_length)
516
+
517
+ @staticmethod
518
+ def _min_payload_for_type(payload_type: PayloadType) -> int:
519
+ """Minimum payload size in bytes for the payload type (for path-length fallback)."""
520
+ mins = {
521
+ PayloadType.Advert: 101, # public_key(32) + timestamp(4) + signature(64) + flags(1)
522
+ PayloadType.AnonRequest: 35, # dest(1) + pubkey(32) + MAC(2)
523
+ PayloadType.Trace: 9,
524
+ PayloadType.Ack: 4,
525
+ PayloadType.Control: 1,
526
+ PayloadType.Request: 4,
527
+ PayloadType.Response: 4,
528
+ PayloadType.TextMessage: 4,
529
+ PayloadType.Path: 4,
530
+ PayloadType.GroupText: 3,
531
+ PayloadType.GroupData: 3,
532
+ }
533
+ return mins.get(payload_type, 0)
534
+
535
+ @staticmethod
536
+ def _calculate_message_hash(
537
+ bytes_data: bytes,
538
+ route_type: RouteType,
539
+ payload_type: PayloadType,
540
+ payload_version: PayloadVersion,
541
+ path_byte_length_override: Optional[int] = None,
542
+ ) -> str:
472
543
  """Calculate message hash for a packet"""
544
+ def skip_path(off: int) -> int:
545
+ if path_byte_length_override is not None:
546
+ return off + 1 + path_byte_length_override
547
+ if len(bytes_data) <= off:
548
+ return off
549
+ _, _, path_byte_len = MeshCorePacketDecoder._decode_path_len_byte(bytes_data[off])
550
+ return off + 1 + path_byte_len
551
+
473
552
  # For TRACE packets, use the trace tag as hash
474
553
  if payload_type == PayloadType.Trace and len(bytes_data) >= 13:
475
554
  offset = 1
476
-
477
- # Skip transport codes if present
478
555
  if route_type in (RouteType.TransportFlood, RouteType.TransportDirect):
479
556
  offset += 4
480
-
481
- # Skip path data
482
- if len(bytes_data) > offset:
483
- path_len = bytes_data[offset]
484
- offset += 1 + path_len
557
+ offset = skip_path(offset)
485
558
 
486
559
  # Extract trace tag
487
560
  if len(bytes_data) >= offset + 4:
@@ -494,15 +567,9 @@ class MeshCorePacketDecoder:
494
567
  # For other packets, create hash from constant parts
495
568
  constant_header = (payload_type.value << 2) | (payload_version.value << 6)
496
569
  offset = 1
497
-
498
- # Skip transport codes if present
499
570
  if route_type in (RouteType.TransportFlood, RouteType.TransportDirect):
500
571
  offset += 4
501
-
502
- # Skip path data
503
- if len(bytes_data) > offset:
504
- path_len = bytes_data[offset]
505
- offset += 1 + path_len
572
+ offset = skip_path(offset)
506
573
 
507
574
  payload_data = bytes_data[offset:]
508
575
  hash_input = [constant_header] + list(payload_data)
@@ -149,22 +149,29 @@ class PathPayloadDecoder:
149
149
  )
150
150
 
151
151
  if decryption_result.success and decryption_result.data:
152
- # Parse decrypted path structure
153
- path_len = decryption_result.data['path_len']
152
+ # Parse decrypted path structure (multi-byte path encoding)
153
+ path_hash_size = decryption_result.data['path_hash_size']
154
+ path_hash_count = decryption_result.data['path_hash_count']
154
155
  path_bytes = decryption_result.data['path']
155
156
  extra_type = decryption_result.data['extra_type']
156
157
  extra_bytes = decryption_result.data['extra']
157
158
 
158
- # Convert path bytes to hex strings
159
- path_hashes = [byte_to_hex(b) for b in path_bytes]
159
+ # One hex string per hop (each hop is path_hash_size bytes)
160
+ path_hashes = []
161
+ for i in range(path_hash_count):
162
+ hop_start = i * path_hash_size
163
+ hop_bytes = path_bytes[hop_start:hop_start + path_hash_size]
164
+ path_hashes.append(bytes_to_hex(hop_bytes))
160
165
  extra_data = bytes_to_hex(extra_bytes)
161
166
 
162
- result.path_length = path_len
167
+ result.path_length = path_hash_count
163
168
  result.path_hashes = path_hashes
164
169
  result.extra_type = extra_type
165
170
  result.extra_data = extra_data
166
171
  result.decrypted = {
167
- 'path_len': path_len,
172
+ 'path_len_byte': decryption_result.data['path_len_byte'],
173
+ 'path_hash_size': path_hash_size,
174
+ 'path_hash_count': path_hash_count,
168
175
  'path_hashes': path_hashes,
169
176
  'extra_type': extra_type,
170
177
  'extra_data': extra_data
@@ -204,22 +211,28 @@ class PathPayloadDecoder:
204
211
  )
205
212
 
206
213
  if decryption_result.success and decryption_result.data:
207
- # Parse decrypted path structure
208
- path_len = decryption_result.data['path_len']
214
+ # Parse decrypted path structure (multi-byte path encoding)
215
+ path_hash_size = decryption_result.data['path_hash_size']
216
+ path_hash_count = decryption_result.data['path_hash_count']
209
217
  path_bytes = decryption_result.data['path']
210
218
  extra_type = decryption_result.data['extra_type']
211
219
  extra_bytes = decryption_result.data['extra']
212
220
 
213
- # Convert path bytes to hex strings
214
- path_hashes = [byte_to_hex(b) for b in path_bytes]
221
+ path_hashes = []
222
+ for i in range(path_hash_count):
223
+ hop_start = i * path_hash_size
224
+ hop_bytes = path_bytes[hop_start:hop_start + path_hash_size]
225
+ path_hashes.append(bytes_to_hex(hop_bytes))
215
226
  extra_data = bytes_to_hex(extra_bytes)
216
227
 
217
- result.path_length = path_len
228
+ result.path_length = path_hash_count
218
229
  result.path_hashes = path_hashes
219
230
  result.extra_type = extra_type
220
231
  result.extra_data = extra_data
221
232
  result.decrypted = {
222
- 'path_len': path_len,
233
+ 'path_len_byte': decryption_result.data['path_len_byte'],
234
+ 'path_hash_size': path_hash_size,
235
+ 'path_hash_count': path_hash_count,
223
236
  'path_hashes': path_hashes,
224
237
  'extra_type': extra_type,
225
238
  'extra_data': extra_data
@@ -240,22 +253,28 @@ class PathPayloadDecoder:
240
253
  shared_secret
241
254
  )
242
255
  if decryption_result.success and decryption_result.data:
243
- # Parse decrypted path structure
244
- path_len = decryption_result.data['path_len']
256
+ # Parse decrypted path structure (multi-byte path encoding)
257
+ path_hash_size = decryption_result.data['path_hash_size']
258
+ path_hash_count = decryption_result.data['path_hash_count']
245
259
  path_bytes = decryption_result.data['path']
246
260
  extra_type = decryption_result.data['extra_type']
247
261
  extra_bytes = decryption_result.data['extra']
248
262
 
249
- # Convert path bytes to hex strings
250
- path_hashes = [byte_to_hex(b) for b in path_bytes]
263
+ path_hashes = []
264
+ for i in range(path_hash_count):
265
+ hop_start = i * path_hash_size
266
+ hop_bytes = path_bytes[hop_start:hop_start + path_hash_size]
267
+ path_hashes.append(bytes_to_hex(hop_bytes))
251
268
  extra_data = bytes_to_hex(extra_bytes)
252
269
 
253
- result.path_length = path_len
270
+ result.path_length = path_hash_count
254
271
  result.path_hashes = path_hashes
255
272
  result.extra_type = extra_type
256
273
  result.extra_data = extra_data
257
274
  result.decrypted = {
258
- 'path_len': path_len,
275
+ 'path_len_byte': decryption_result.data['path_len_byte'],
276
+ 'path_hash_size': path_hash_size,
277
+ 'path_hash_count': path_hash_count,
259
278
  'path_hashes': path_hashes,
260
279
  'extra_type': extra_type,
261
280
  'extra_data': extra_data
@@ -20,7 +20,9 @@ class DecodedPacket:
20
20
  is_valid: bool,
21
21
  transport_codes: Optional[List[int]] = None,
22
22
  path: Optional[List[str]] = None,
23
- errors: Optional[List[str]] = None
23
+ errors: Optional[List[str]] = None,
24
+ path_byte_length: Optional[int] = None,
25
+ path_hash_size: Optional[int] = None,
24
26
  ):
25
27
  # Packet metadata
26
28
  self.message_hash = message_hash
@@ -30,10 +32,12 @@ class DecodedPacket:
30
32
  self.payload_type = payload_type
31
33
  self.payload_version = payload_version
32
34
 
33
- # Transport and routing
35
+ # Transport and routing (path_length = hop count; path_byte_length = total path bytes)
34
36
  self.transport_codes = transport_codes
35
37
  self.path_length = path_length
36
38
  self.path = path
39
+ self.path_byte_length = path_byte_length if path_byte_length is not None else path_length
40
+ self.path_hash_size = path_hash_size # 1, 2, or 3 bytes per hop
37
41
 
38
42
  # Payload data
39
43
  self.payload = payload
@@ -62,6 +66,10 @@ class DecodedPacket:
62
66
 
63
67
  if self.transport_codes:
64
68
  result['transportCodes'] = list(self.transport_codes)
69
+ if getattr(self, 'path_byte_length', None) is not None and self.path_byte_length != self.path_length:
70
+ result['pathByteLength'] = self.path_byte_length
71
+ if getattr(self, 'path_hash_size', None) is not None:
72
+ result['pathHashSize'] = self.path_hash_size
65
73
 
66
74
  if self.errors:
67
75
  result['errors'] = self.errors
@@ -43,6 +43,7 @@ class AdvertPayload(BasePayload):
43
43
  app_data: Dict[str, Any],
44
44
  signature_valid: Optional[bool] = None,
45
45
  signature_error: Optional[str] = None,
46
+ hash_mode: Optional[int] = None,
46
47
  **kwargs
47
48
  ):
48
49
  super().__init__(**kwargs)
@@ -52,6 +53,7 @@ class AdvertPayload(BasePayload):
52
53
  self.signature_valid = signature_valid
53
54
  self.signature_error = signature_error
54
55
  self.app_data = app_data
56
+ self.hash_mode = hash_mode
55
57
 
56
58
  def to_dict(self) -> Dict[str, Any]:
57
59
  """Convert to dictionary for JSON serialization"""
@@ -71,6 +73,8 @@ class AdvertPayload(BasePayload):
71
73
  result['signatureValid'] = self.signature_valid
72
74
  if self.signature_error:
73
75
  result['signatureError'] = self.signature_error
76
+ if self.hash_mode is not None:
77
+ result['hashMode'] = self.hash_mode
74
78
 
75
79
  # Add location if present
76
80
  if self.app_data.get('location'):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: meshcoredecoder
3
- Version: 0.3.0
3
+ Version: 0.3.2
4
4
  Summary: Complete Python implementation of the MeshCore Packet Decoder
5
5
  Home-page: https://github.com/chrisdavis2110/meshcore-decoder-py
6
6
  Author: Chris Davis
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "meshcoredecoder"
7
- version = "0.3.0"
7
+ version = "0.3.2"
8
8
  authors = [
9
9
  {name = "Chris Davis", email = "chrisdavis2110@gmail.com"},
10
10
  {name = "Michael Hart", email = "michaelhart@michaelhart.me"}
@@ -16,7 +16,7 @@ all_packages = find_packages()
16
16
 
17
17
  setup(
18
18
  name="meshcoredecoder",
19
- version="0.3.0",
19
+ version="0.3.2",
20
20
  author="Chris Davis",
21
21
  author_email="chrisdavis2110@gmail.com",
22
22
  description="Complete Python implementation of the MeshCore Packet Decoder",
File without changes