meshcoredecoder 0.1.0__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 (42) hide show
  1. meshcoredecoder-0.1.0/LICENSE +21 -0
  2. meshcoredecoder-0.1.0/MANIFEST.in +8 -0
  3. meshcoredecoder-0.1.0/PKG-INFO +332 -0
  4. meshcoredecoder-0.1.0/README.md +293 -0
  5. meshcoredecoder-0.1.0/cli.py +249 -0
  6. meshcoredecoder-0.1.0/index.py +66 -0
  7. meshcoredecoder-0.1.0/meshcoredecoder/__init__.py +8 -0
  8. meshcoredecoder-0.1.0/meshcoredecoder/crypto/__init__.py +6 -0
  9. meshcoredecoder-0.1.0/meshcoredecoder/crypto/channel_crypto.py +113 -0
  10. meshcoredecoder-0.1.0/meshcoredecoder/crypto/ed25519_verifier.py +136 -0
  11. meshcoredecoder-0.1.0/meshcoredecoder/crypto/key_manager.py +71 -0
  12. meshcoredecoder-0.1.0/meshcoredecoder/decoder/__init__.py +26 -0
  13. meshcoredecoder-0.1.0/meshcoredecoder/decoder/packet_decoder.py +500 -0
  14. meshcoredecoder-0.1.0/meshcoredecoder/decoder/payload_decoders/__init__.py +22 -0
  15. meshcoredecoder-0.1.0/meshcoredecoder/decoder/payload_decoders/ack.py +91 -0
  16. meshcoredecoder-0.1.0/meshcoredecoder/decoder/payload_decoders/advert.py +281 -0
  17. meshcoredecoder-0.1.0/meshcoredecoder/decoder/payload_decoders/anon_request.py +137 -0
  18. meshcoredecoder-0.1.0/meshcoredecoder/decoder/payload_decoders/group_text.py +130 -0
  19. meshcoredecoder-0.1.0/meshcoredecoder/decoder/payload_decoders/path.py +89 -0
  20. meshcoredecoder-0.1.0/meshcoredecoder/decoder/payload_decoders/request.py +143 -0
  21. meshcoredecoder-0.1.0/meshcoredecoder/decoder/payload_decoders/response.py +133 -0
  22. meshcoredecoder-0.1.0/meshcoredecoder/decoder/payload_decoders/text_message.py +70 -0
  23. meshcoredecoder-0.1.0/meshcoredecoder/decoder/payload_decoders/trace.py +156 -0
  24. meshcoredecoder-0.1.0/meshcoredecoder/types/__init__.py +15 -0
  25. meshcoredecoder-0.1.0/meshcoredecoder/types/crypto.py +67 -0
  26. meshcoredecoder-0.1.0/meshcoredecoder/types/enums.py +60 -0
  27. meshcoredecoder-0.1.0/meshcoredecoder/types/packet.py +139 -0
  28. meshcoredecoder-0.1.0/meshcoredecoder/types/payloads.py +244 -0
  29. meshcoredecoder-0.1.0/meshcoredecoder/utils/__init__.py +18 -0
  30. meshcoredecoder-0.1.0/meshcoredecoder/utils/auth_token.py +234 -0
  31. meshcoredecoder-0.1.0/meshcoredecoder/utils/enum_names.py +58 -0
  32. meshcoredecoder-0.1.0/meshcoredecoder/utils/hex.py +38 -0
  33. meshcoredecoder-0.1.0/meshcoredecoder.egg-info/PKG-INFO +332 -0
  34. meshcoredecoder-0.1.0/meshcoredecoder.egg-info/SOURCES.txt +40 -0
  35. meshcoredecoder-0.1.0/meshcoredecoder.egg-info/dependency_links.txt +1 -0
  36. meshcoredecoder-0.1.0/meshcoredecoder.egg-info/entry_points.txt +2 -0
  37. meshcoredecoder-0.1.0/meshcoredecoder.egg-info/requires.txt +9 -0
  38. meshcoredecoder-0.1.0/meshcoredecoder.egg-info/top_level.txt +3 -0
  39. meshcoredecoder-0.1.0/pyproject.toml +54 -0
  40. meshcoredecoder-0.1.0/requirements.txt +19 -0
  41. meshcoredecoder-0.1.0/setup.cfg +4 -0
  42. meshcoredecoder-0.1.0/setup.py +54 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Michael Hart <michaelhart@michaelhart.me> (https://github.com/michaelhart)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,8 @@
1
+ include README.md
2
+ include LICENSE
3
+ include requirements.txt
4
+ recursive-include meshcoredecoder *.py
5
+ recursive-exclude * __pycache__
6
+ recursive-exclude * *.py[co]
7
+ recursive-exclude * *.so
8
+ recursive-exclude * .DS_Store
@@ -0,0 +1,332 @@
1
+ Metadata-Version: 2.4
2
+ Name: meshcoredecoder
3
+ Version: 0.1.0
4
+ Summary: Complete Python implementation of the MeshCore Packet Decoder
5
+ Home-page: https://github.com/chrisdavis2110/meshcore-decoder-py
6
+ Author: Chris Davis
7
+ Author-email: Chris Davis <chrisdavis2110@gmail.com>, Michael Hart <michaelhart@michaelhart.me>
8
+ License: MIT
9
+ Project-URL: Homepage, https://github.com/chrisdavis2110/meshcore-decoder-py
10
+ Project-URL: Repository, https://github.com/chrisdavis2110/meshcore-decoder-py
11
+ Project-URL: Issues, https://github.com/chrisdavis2110/meshcore-decoder-py/issues
12
+ Keywords: meshcore,decoder,packet,mesh,networking
13
+ Classifier: Development Status :: 4 - Beta
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Operating System :: OS Independent
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3.8
19
+ Classifier: Programming Language :: Python :: 3.9
20
+ Classifier: Programming Language :: Python :: 3.10
21
+ Classifier: Programming Language :: Python :: 3.11
22
+ Classifier: Programming Language :: Python :: 3.12
23
+ Classifier: Programming Language :: Python :: 3.13
24
+ Requires-Python: >=3.8
25
+ Description-Content-Type: text/markdown
26
+ License-File: LICENSE
27
+ Requires-Dist: pycryptodome>=3.19.0
28
+ Requires-Dist: cryptography>=41.0.0
29
+ Requires-Dist: click>=8.1.0
30
+ Provides-Extra: dev
31
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
32
+ Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
33
+ Requires-Dist: black>=23.0.0; extra == "dev"
34
+ Requires-Dist: pylint>=2.17.0; extra == "dev"
35
+ Dynamic: author
36
+ Dynamic: home-page
37
+ Dynamic: license-file
38
+ Dynamic: requires-python
39
+
40
+ # MeshCore Decoder - Python Port
41
+
42
+ A Python library for decoding MeshCore mesh networking packets with full cryptographic support. Complete Python implementation of the [MeshCore Packet Decoder](https://github.com/michaelhart/meshcore-decoder) by [Michael Hart](https://github.com/michaelhart).
43
+
44
+ ## Features
45
+
46
+ - **Packet Decoding**: Decode MeshCore packets
47
+ - **Built-in Decryption**: Decrypt GroupText, TextMessage, and other encrypted payloads
48
+ - **Developer Friendly**: Python-first with full type hints and data classes
49
+
50
+ ## Installation
51
+
52
+ ### Install to a single project
53
+
54
+ ```bash
55
+ pip install -r requirements.txt
56
+ ```
57
+
58
+ ### Install via pip (if published)
59
+
60
+ ```bash
61
+ pip install meshcoredecoder
62
+ ```
63
+
64
+ ## Requirements
65
+
66
+ - `pycryptodome>=3.19.0` - Core cryptography (AES, HMAC)
67
+ - `cryptography>=41.0.0` - Ed25519 signature support
68
+ - `click>=8.1.0` - CLI improvements
69
+
70
+ ## Quick Start
71
+
72
+ ```python
73
+ from meshcoredecoder import MeshCoreDecoder
74
+ from meshcoredecoder.types.enums import PayloadType
75
+ from meshcoredecoder.utils.enum_names import get_route_type_name, get_payload_type_name, get_device_role_name
76
+ import json
77
+
78
+ # Decode a MeshCore packet
79
+ hex_data = '11007E7662676F7F0850A8A355BAAFBFC1EB7B4174C340442D7D7161C9474A2C94006CE7CF682E58408DD8FCC51906ECA98EBF94A037886BDADE7ECD09FD92B839491DF3809C9454F5286D1D3370AC31A34593D569E9A042A3B41FD331DFFB7E18599CE1E60992A076D50238C5B8F85757375354522F50756765744D65736820436F75676172'
80
+ packet = MeshCoreDecoder.decode(hex_data)
81
+
82
+ print(f"Route Type: {get_route_type_name(packet.route_type)}")
83
+ print(f"Payload Type: {get_payload_type_name(packet.payload_type)}")
84
+ print(f"Message Hash: {packet.message_hash}")
85
+
86
+ if packet.payload_type == PayloadType.Advert and packet.payload.get('decoded'):
87
+ advert = packet.payload['decoded']
88
+ print(f"Device Name: {advert.app_data.get('name')}")
89
+ print(f"Device Role: {get_device_role_name(advert.app_data.get('device_role'))}")
90
+ if advert.app_data.get('location'):
91
+ location = advert.app_data['location']
92
+ print(f"Location: {location['latitude']}, {location['longitude']}")
93
+ ```
94
+
95
+ ## Full Packet Structure Example
96
+
97
+ Here's what a complete decoded packet looks like:
98
+
99
+ ```python
100
+ from meshcoredecoder import MeshCoreDecoder
101
+ import json
102
+
103
+ hex_data = '11007E7662676F7F0850A8A355BAAFBFC1EB7B4174C340442D7D7161C9474A2C94006CE7CF682E58408DD8FCC51906ECA98EBF94A037886BDADE7ECD09FD92B839491DF3809C9454F5286D1D3370AC31A34593D569E9A042A3B41FD331DFFB7E18599CE1E60992A076D50238C5B8F85757375354522F50756765744D65736820436F75676172'
104
+
105
+ packet = MeshCoreDecoder.decode(hex_data)
106
+
107
+ packet_dict = packet.to_dict()
108
+ print(json.dumps(packet_dict, indent=2, default=str))
109
+ ```
110
+
111
+ **Output:**
112
+ ```json
113
+ {
114
+ "messageHash": "F9C060FE",
115
+ "routeType": 1,
116
+ "payloadType": 4,
117
+ "payloadVersion": 0,
118
+ "pathLength": 0,
119
+ "path": null,
120
+ "payload": {
121
+ "raw": "7E7662676F7F0850A8A355BAAFBFC1EB7B4174C340442D7D7161C9474A2C94006CE7CF682E58408DD8FCC51906ECA98EBF94A037886BDADE7ECD09FD92B839491DF3809C9454F5286D1D3370AC31A34593D569E9A042A3B41FD331DFFB7E18599CE1E60992A076D50238C5B8F85757375354522F50756765744D65736820436F75676172",
122
+ "decoded": {
123
+ "type": 4,
124
+ "version": 0,
125
+ "isValid": true,
126
+ "publicKey": "7E7662676F7F0850A8A355BAAFBFC1EB7B4174C340442D7D7161C9474A2C9400",
127
+ "timestamp": 1758455660,
128
+ "signature": "2E58408DD8FCC51906ECA98EBF94A037886BDADE7ECD09FD92B839491DF3809C9454F5286D1D3370AC31A34593D569E9A042A3B41FD331DFFB7E18599CE1E609",
129
+ "appData": {
130
+ "flags": 146,
131
+ "deviceRole": 2,
132
+ "hasLocation": true,
133
+ "hasName": true,
134
+ "location": {
135
+ "latitude": 47.543968,
136
+ "longitude": -122.108616
137
+ },
138
+ "name": "WW7STR/PugetMesh Cougar"
139
+ }
140
+ }
141
+ },
142
+ "totalBytes": 134,
143
+ "isValid": true
144
+ }
145
+ ```
146
+
147
+ ## Decryption Support
148
+
149
+ Simply provide your channel secret keys and the library handles everything else:
150
+
151
+ ```python
152
+ from meshcoredecoder import MeshCoreDecoder
153
+ from meshcoredecoder.crypto import MeshCoreKeyStore
154
+ from meshcoredecoder.types.crypto import DecryptionOptions
155
+ from meshcoredecoder.types.enums import PayloadType
156
+
157
+ # Create a key store with channel secret keys
158
+ key_store = MeshCoreKeyStore({
159
+ 'channel_secrets': [
160
+ '8b3387e9c5cdea6ac9e5edbaa115cd72', # Public channel (channel hash 11)
161
+ 'ff2b7d74e8d20f71505bda9ea8d59a1c', # A different channel's secret
162
+ ]
163
+ })
164
+
165
+ group_text_hex_data = '...' # Your encrypted GroupText packet hex
166
+
167
+ # Decode encrypted GroupText message
168
+ options = DecryptionOptions(key_store=key_store)
169
+ encrypted_packet = MeshCoreDecoder.decode(group_text_hex_data, options)
170
+
171
+ if encrypted_packet.payload_type == PayloadType.GroupText and encrypted_packet.payload.get('decoded'):
172
+ group_text = encrypted_packet.payload['decoded']
173
+
174
+ if group_text.decrypted:
175
+ print(f"Sender: {group_text.decrypted.get('sender')}")
176
+ print(f"Message: {group_text.decrypted.get('message')}")
177
+ print(f"Timestamp: {group_text.decrypted.get('timestamp')}")
178
+ else:
179
+ print('Message encrypted (no key available)')
180
+ ```
181
+
182
+ The library automatically:
183
+ - Calculates channel hashes from your secret keys using SHA256
184
+ - Handles hash collisions (multiple keys with same first byte) by trying all matching keys
185
+ - Verifies message authenticity using HMAC-SHA256
186
+ - Decrypts using AES-128 ECB
187
+
188
+ ### With Signature Verification
189
+
190
+ ```python
191
+ import asyncio
192
+ from meshcoredecoder import MeshCoreDecoder
193
+
194
+ # Async verification for Ed25519 signatures
195
+ async def verify_packet():
196
+ packet = await MeshCoreDecoder.decode_with_verification(hex_data)
197
+
198
+ if packet.payload.get('decoded'):
199
+ advert = packet.payload['decoded']
200
+ if hasattr(advert, 'signature_valid'):
201
+ print(f"Signature Valid: {advert.signature_valid}")
202
+
203
+ asyncio.run(verify_packet())
204
+ ```
205
+
206
+ ## Packet Structure Analysis
207
+
208
+ For detailed packet analysis and debugging, use `analyze_structure()` to get byte-level breakdowns:
209
+
210
+ ```python
211
+ from meshcoredecoder import MeshCoreDecoder
212
+
213
+ print('=== Packet Breakdown ===')
214
+ hex_data = '11007E7662676F7F0850A8A355BAAFBFC1EB7B4174C340442D7D7161C9474A2C94006CE7CF682E58408DD8FCC51906ECA98EBF94A037886BDADE7ECD09FD92B839491DF3809C9454F5286D1D3370AC31A34593D569E9A042A3B41FD331DFFB7E18599CE1E60992A076D50238C5B8F85757375354522F50756765744D65736820436F75676172'
215
+
216
+ print(f"Packet length: {len(hex_data)}")
217
+ print(f"Expected bytes: {len(hex_data) / 2}")
218
+
219
+ structure = MeshCoreDecoder.analyze_structure(hex_data)
220
+ print('\nMain segments:')
221
+ for i, seg in enumerate(structure.segments):
222
+ print(f"{i+1}. {seg.name} (bytes {seg.start_byte}-{seg.end_byte}): {seg.value}")
223
+
224
+ print('\nPayload segments:')
225
+ for i, seg in enumerate(structure.payload['segments']):
226
+ print(f"{i+1}. {seg.name} (bytes {seg.start_byte}-{seg.end_byte}): {seg.value}")
227
+ print(f" Description: {seg.description}")
228
+ ```
229
+
230
+ **Output:**
231
+ ```
232
+ === Packet Breakdown ===
233
+ Packet length: 268
234
+ Expected bytes: 134
235
+
236
+ Main segments:
237
+ 1. Header (bytes 0-0): 0x11
238
+ 2. Path Length (bytes 1-1): 0x00
239
+ 3. Payload (bytes 2-133): 7E7662676F7F0850A8A355BAAFBFC1EB7B4174C340442D7D7161C9474A2C94006CE7CF682E58408DD8FCC51906ECA98EBF94A037886BDADE7ECD09FD92B839491DF3809C9454F5286D1D3370AC31A34593D569E9A042A3B41FD331DFFB7E18599CE1E60992A076D50238C5B8F85757375354522F50756765744D65736820436F75676172
240
+
241
+ Payload segments:
242
+ 1. Public Key (bytes 0-31): 7E7662676F7F0850A8A355BAAFBFC1EB7B4174C340442D7D7161C9474A2C9400
243
+ Description: Ed25519 public key
244
+ 2. Timestamp (bytes 32-35): 6CE7CF68
245
+ Description: 1758455660 (2025-09-21T11:54:20Z)
246
+ 3. Signature (bytes 36-99): 2E58408DD8FCC51906ECA98EBF94A037886BDADE7ECD09FD92B839491DF3809C9454F5286D1D3370AC31A34593D569E9A042A3B41FD331DFFB7E18599CE1E609
247
+ Description: Ed25519 signature
248
+ 4. App Flags (bytes 100-100): 92
249
+ Description: Binary: 10010010 | Bits 0-3 (Role): Room server | Bit 4 (Location): Yes | Bit 5 (Feature1): No | Bit 6 (Feature2): No | Bit 7 (Name): Yes
250
+ 5. Latitude (bytes 101-104): A076D502
251
+ Description: 47.543968° (47.543968)
252
+ 6. Longitude (bytes 105-108): 38C5B8F8
253
+ Description: -122.108616° (-122.108616)
254
+ 7. Node Name (bytes 109-131): 5757375354522F50756765744D65736820436F75676172
255
+ Description: Node name: "WW7STR/PugetMesh Cougar"
256
+ ```
257
+
258
+ The `analyze_structure()` method provides:
259
+ - **Header breakdown** with bit-level field analysis
260
+ - **Byte-accurate segments** with start/end positions
261
+ - **Payload field parsing** for all supported packet types
262
+ - **Human-readable descriptions** for each field
263
+
264
+ ## Ed25519 Key Derivation
265
+
266
+ The library includes MeshCore-compatible Ed25519 key derivation using the exact orlp/ed25519 algorithm:
267
+
268
+ ```python
269
+ from meshcoredecoder.crypto import derive_public_key, validate_key_pair
270
+
271
+ # Derive public key from MeshCore private key (64-byte format)
272
+ private_key = '18469d6140447f77de13cd8d761e605431f52269fbff43b0925752ed9e6745435dc6a86d2568af8b70d3365db3f88234760c8ecc645ce469829bc45b65f1d5d5'
273
+
274
+ public_key = derive_public_key(private_key)
275
+ print('Derived Public Key:', public_key)
276
+ # Output: 4852B69364572B52EFA1B6BB3E6D0ABED4F389A1CBFBB60A9BBA2CCE649CAF0E
277
+
278
+ # Validate a key pair
279
+ is_valid = validate_key_pair(private_key, public_key)
280
+ print('Key pair valid:', is_valid) # True
281
+ ```
282
+
283
+ ## Command Line Interface
284
+
285
+ For quick analysis from the terminal, use the CLI:
286
+
287
+ ```bash
288
+ # Analyze a packet
289
+ python cli.py decode 11007E7662676F7F0850A8A355BAAFBFC1EB7B4174C340442D7D7161C9474A2C94006CE7CF682E58408DD8FCC51906ECA98EBF94A037886BDADE7ECD09FD92B839491DF3809C9454F5286D1D3370AC31A34593D569E9A042A3B41FD331DFFB7E18599CE1E60992A076D50238C5B8F85757375354522F50756765744D65736820436F75676172
290
+
291
+ # With decryption (provide channel secrets)
292
+ python cli.py decode 150011C3C1354D619BAE9590E4D177DB7EEAF982F5BDCF78005D75157D9535FA90178F785D --key 8b3387e9c5cdea6ac9e5edbaa115cd72
293
+
294
+ # Show detailed structure analysis
295
+ python cli.py decode --structure 11007E7662676F7F0850A8A355BAAFBFC1EB7B4174C340442D7D7161C9474A2C94006CE7CF682E58408DD8FCC51906ECA98EBF94A037886BDADE7ECD09FD92B839491DF3809C9454F5286D1D3370AC31A34593D569E9A042A3B41FD331DFFB7E18599CE1E60992A076D50238C5B8F85757375354522F50756765744D65736820436F75676172
296
+
297
+ # JSON output
298
+ python cli.py decode --json 11007E7662676F7F0850A8A355BAAFBFC1EB7B4174C340442D7D7161C9474A2C94006CE7CF682E58408DD8FCC51906ECA98EBF94A037886BDADE7ECD09FD92B839491DF3809C9454F5286D1D3370AC31A34593D569E9A042A3B41FD331DFFB7E18599CE1E60992A076D50238C5B8F85757375354522F50756765744D65736820436F75676172
299
+
300
+ # Derive public key from MeshCore private key
301
+ python cli.py derive-key 18469d6140447f77de13cd8d761e605431f52269fbff43b0925752ed9e6745435dc6a86d2568af8b70d3365db3f88234760c8ecc645ce469829bc45b65f1d5d5
302
+
303
+ # Validate key pair
304
+ python cli.py validate-key 18469d6140447f77de13cd8d761e605431f52269fbff43b0925752ed9e6745435dc6a86d2568af8b70d3365db3f88234760c8ecc645ce469829bc45b65f1d5d5 4852b69364572b52efa1b6bb3e6d0abed4f389a1cbfbb60a9bba2cce649caf0e
305
+ ```
306
+
307
+
308
+
309
+
310
+ ## License
311
+
312
+ MIT License
313
+
314
+ Copyright (c) 2025 Michael Hart <michaelhart@michaelhart.me> (https://github.com/michaelhart)
315
+
316
+ Permission is hereby granted, free of charge, to any person obtaining a copy
317
+ of this software and associated documentation files (the "Software"), to deal
318
+ in the Software without restriction, including without limitation the rights
319
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
320
+ copies of the Software, and to permit persons to whom the Software is
321
+ furnished to do so, subject to the following conditions:
322
+
323
+ The above copyright notice and this permission notice shall be included in all
324
+ copies or substantial portions of the Software.
325
+
326
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
327
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
328
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
329
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
330
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
331
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
332
+ SOFTWARE.
@@ -0,0 +1,293 @@
1
+ # MeshCore Decoder - Python Port
2
+
3
+ A Python library for decoding MeshCore mesh networking packets with full cryptographic support. Complete Python implementation of the [MeshCore Packet Decoder](https://github.com/michaelhart/meshcore-decoder) by [Michael Hart](https://github.com/michaelhart).
4
+
5
+ ## Features
6
+
7
+ - **Packet Decoding**: Decode MeshCore packets
8
+ - **Built-in Decryption**: Decrypt GroupText, TextMessage, and other encrypted payloads
9
+ - **Developer Friendly**: Python-first with full type hints and data classes
10
+
11
+ ## Installation
12
+
13
+ ### Install to a single project
14
+
15
+ ```bash
16
+ pip install -r requirements.txt
17
+ ```
18
+
19
+ ### Install via pip (if published)
20
+
21
+ ```bash
22
+ pip install meshcoredecoder
23
+ ```
24
+
25
+ ## Requirements
26
+
27
+ - `pycryptodome>=3.19.0` - Core cryptography (AES, HMAC)
28
+ - `cryptography>=41.0.0` - Ed25519 signature support
29
+ - `click>=8.1.0` - CLI improvements
30
+
31
+ ## Quick Start
32
+
33
+ ```python
34
+ from meshcoredecoder import MeshCoreDecoder
35
+ from meshcoredecoder.types.enums import PayloadType
36
+ from meshcoredecoder.utils.enum_names import get_route_type_name, get_payload_type_name, get_device_role_name
37
+ import json
38
+
39
+ # Decode a MeshCore packet
40
+ hex_data = '11007E7662676F7F0850A8A355BAAFBFC1EB7B4174C340442D7D7161C9474A2C94006CE7CF682E58408DD8FCC51906ECA98EBF94A037886BDADE7ECD09FD92B839491DF3809C9454F5286D1D3370AC31A34593D569E9A042A3B41FD331DFFB7E18599CE1E60992A076D50238C5B8F85757375354522F50756765744D65736820436F75676172'
41
+ packet = MeshCoreDecoder.decode(hex_data)
42
+
43
+ print(f"Route Type: {get_route_type_name(packet.route_type)}")
44
+ print(f"Payload Type: {get_payload_type_name(packet.payload_type)}")
45
+ print(f"Message Hash: {packet.message_hash}")
46
+
47
+ if packet.payload_type == PayloadType.Advert and packet.payload.get('decoded'):
48
+ advert = packet.payload['decoded']
49
+ print(f"Device Name: {advert.app_data.get('name')}")
50
+ print(f"Device Role: {get_device_role_name(advert.app_data.get('device_role'))}")
51
+ if advert.app_data.get('location'):
52
+ location = advert.app_data['location']
53
+ print(f"Location: {location['latitude']}, {location['longitude']}")
54
+ ```
55
+
56
+ ## Full Packet Structure Example
57
+
58
+ Here's what a complete decoded packet looks like:
59
+
60
+ ```python
61
+ from meshcoredecoder import MeshCoreDecoder
62
+ import json
63
+
64
+ hex_data = '11007E7662676F7F0850A8A355BAAFBFC1EB7B4174C340442D7D7161C9474A2C94006CE7CF682E58408DD8FCC51906ECA98EBF94A037886BDADE7ECD09FD92B839491DF3809C9454F5286D1D3370AC31A34593D569E9A042A3B41FD331DFFB7E18599CE1E60992A076D50238C5B8F85757375354522F50756765744D65736820436F75676172'
65
+
66
+ packet = MeshCoreDecoder.decode(hex_data)
67
+
68
+ packet_dict = packet.to_dict()
69
+ print(json.dumps(packet_dict, indent=2, default=str))
70
+ ```
71
+
72
+ **Output:**
73
+ ```json
74
+ {
75
+ "messageHash": "F9C060FE",
76
+ "routeType": 1,
77
+ "payloadType": 4,
78
+ "payloadVersion": 0,
79
+ "pathLength": 0,
80
+ "path": null,
81
+ "payload": {
82
+ "raw": "7E7662676F7F0850A8A355BAAFBFC1EB7B4174C340442D7D7161C9474A2C94006CE7CF682E58408DD8FCC51906ECA98EBF94A037886BDADE7ECD09FD92B839491DF3809C9454F5286D1D3370AC31A34593D569E9A042A3B41FD331DFFB7E18599CE1E60992A076D50238C5B8F85757375354522F50756765744D65736820436F75676172",
83
+ "decoded": {
84
+ "type": 4,
85
+ "version": 0,
86
+ "isValid": true,
87
+ "publicKey": "7E7662676F7F0850A8A355BAAFBFC1EB7B4174C340442D7D7161C9474A2C9400",
88
+ "timestamp": 1758455660,
89
+ "signature": "2E58408DD8FCC51906ECA98EBF94A037886BDADE7ECD09FD92B839491DF3809C9454F5286D1D3370AC31A34593D569E9A042A3B41FD331DFFB7E18599CE1E609",
90
+ "appData": {
91
+ "flags": 146,
92
+ "deviceRole": 2,
93
+ "hasLocation": true,
94
+ "hasName": true,
95
+ "location": {
96
+ "latitude": 47.543968,
97
+ "longitude": -122.108616
98
+ },
99
+ "name": "WW7STR/PugetMesh Cougar"
100
+ }
101
+ }
102
+ },
103
+ "totalBytes": 134,
104
+ "isValid": true
105
+ }
106
+ ```
107
+
108
+ ## Decryption Support
109
+
110
+ Simply provide your channel secret keys and the library handles everything else:
111
+
112
+ ```python
113
+ from meshcoredecoder import MeshCoreDecoder
114
+ from meshcoredecoder.crypto import MeshCoreKeyStore
115
+ from meshcoredecoder.types.crypto import DecryptionOptions
116
+ from meshcoredecoder.types.enums import PayloadType
117
+
118
+ # Create a key store with channel secret keys
119
+ key_store = MeshCoreKeyStore({
120
+ 'channel_secrets': [
121
+ '8b3387e9c5cdea6ac9e5edbaa115cd72', # Public channel (channel hash 11)
122
+ 'ff2b7d74e8d20f71505bda9ea8d59a1c', # A different channel's secret
123
+ ]
124
+ })
125
+
126
+ group_text_hex_data = '...' # Your encrypted GroupText packet hex
127
+
128
+ # Decode encrypted GroupText message
129
+ options = DecryptionOptions(key_store=key_store)
130
+ encrypted_packet = MeshCoreDecoder.decode(group_text_hex_data, options)
131
+
132
+ if encrypted_packet.payload_type == PayloadType.GroupText and encrypted_packet.payload.get('decoded'):
133
+ group_text = encrypted_packet.payload['decoded']
134
+
135
+ if group_text.decrypted:
136
+ print(f"Sender: {group_text.decrypted.get('sender')}")
137
+ print(f"Message: {group_text.decrypted.get('message')}")
138
+ print(f"Timestamp: {group_text.decrypted.get('timestamp')}")
139
+ else:
140
+ print('Message encrypted (no key available)')
141
+ ```
142
+
143
+ The library automatically:
144
+ - Calculates channel hashes from your secret keys using SHA256
145
+ - Handles hash collisions (multiple keys with same first byte) by trying all matching keys
146
+ - Verifies message authenticity using HMAC-SHA256
147
+ - Decrypts using AES-128 ECB
148
+
149
+ ### With Signature Verification
150
+
151
+ ```python
152
+ import asyncio
153
+ from meshcoredecoder import MeshCoreDecoder
154
+
155
+ # Async verification for Ed25519 signatures
156
+ async def verify_packet():
157
+ packet = await MeshCoreDecoder.decode_with_verification(hex_data)
158
+
159
+ if packet.payload.get('decoded'):
160
+ advert = packet.payload['decoded']
161
+ if hasattr(advert, 'signature_valid'):
162
+ print(f"Signature Valid: {advert.signature_valid}")
163
+
164
+ asyncio.run(verify_packet())
165
+ ```
166
+
167
+ ## Packet Structure Analysis
168
+
169
+ For detailed packet analysis and debugging, use `analyze_structure()` to get byte-level breakdowns:
170
+
171
+ ```python
172
+ from meshcoredecoder import MeshCoreDecoder
173
+
174
+ print('=== Packet Breakdown ===')
175
+ hex_data = '11007E7662676F7F0850A8A355BAAFBFC1EB7B4174C340442D7D7161C9474A2C94006CE7CF682E58408DD8FCC51906ECA98EBF94A037886BDADE7ECD09FD92B839491DF3809C9454F5286D1D3370AC31A34593D569E9A042A3B41FD331DFFB7E18599CE1E60992A076D50238C5B8F85757375354522F50756765744D65736820436F75676172'
176
+
177
+ print(f"Packet length: {len(hex_data)}")
178
+ print(f"Expected bytes: {len(hex_data) / 2}")
179
+
180
+ structure = MeshCoreDecoder.analyze_structure(hex_data)
181
+ print('\nMain segments:')
182
+ for i, seg in enumerate(structure.segments):
183
+ print(f"{i+1}. {seg.name} (bytes {seg.start_byte}-{seg.end_byte}): {seg.value}")
184
+
185
+ print('\nPayload segments:')
186
+ for i, seg in enumerate(structure.payload['segments']):
187
+ print(f"{i+1}. {seg.name} (bytes {seg.start_byte}-{seg.end_byte}): {seg.value}")
188
+ print(f" Description: {seg.description}")
189
+ ```
190
+
191
+ **Output:**
192
+ ```
193
+ === Packet Breakdown ===
194
+ Packet length: 268
195
+ Expected bytes: 134
196
+
197
+ Main segments:
198
+ 1. Header (bytes 0-0): 0x11
199
+ 2. Path Length (bytes 1-1): 0x00
200
+ 3. Payload (bytes 2-133): 7E7662676F7F0850A8A355BAAFBFC1EB7B4174C340442D7D7161C9474A2C94006CE7CF682E58408DD8FCC51906ECA98EBF94A037886BDADE7ECD09FD92B839491DF3809C9454F5286D1D3370AC31A34593D569E9A042A3B41FD331DFFB7E18599CE1E60992A076D50238C5B8F85757375354522F50756765744D65736820436F75676172
201
+
202
+ Payload segments:
203
+ 1. Public Key (bytes 0-31): 7E7662676F7F0850A8A355BAAFBFC1EB7B4174C340442D7D7161C9474A2C9400
204
+ Description: Ed25519 public key
205
+ 2. Timestamp (bytes 32-35): 6CE7CF68
206
+ Description: 1758455660 (2025-09-21T11:54:20Z)
207
+ 3. Signature (bytes 36-99): 2E58408DD8FCC51906ECA98EBF94A037886BDADE7ECD09FD92B839491DF3809C9454F5286D1D3370AC31A34593D569E9A042A3B41FD331DFFB7E18599CE1E609
208
+ Description: Ed25519 signature
209
+ 4. App Flags (bytes 100-100): 92
210
+ Description: Binary: 10010010 | Bits 0-3 (Role): Room server | Bit 4 (Location): Yes | Bit 5 (Feature1): No | Bit 6 (Feature2): No | Bit 7 (Name): Yes
211
+ 5. Latitude (bytes 101-104): A076D502
212
+ Description: 47.543968° (47.543968)
213
+ 6. Longitude (bytes 105-108): 38C5B8F8
214
+ Description: -122.108616° (-122.108616)
215
+ 7. Node Name (bytes 109-131): 5757375354522F50756765744D65736820436F75676172
216
+ Description: Node name: "WW7STR/PugetMesh Cougar"
217
+ ```
218
+
219
+ The `analyze_structure()` method provides:
220
+ - **Header breakdown** with bit-level field analysis
221
+ - **Byte-accurate segments** with start/end positions
222
+ - **Payload field parsing** for all supported packet types
223
+ - **Human-readable descriptions** for each field
224
+
225
+ ## Ed25519 Key Derivation
226
+
227
+ The library includes MeshCore-compatible Ed25519 key derivation using the exact orlp/ed25519 algorithm:
228
+
229
+ ```python
230
+ from meshcoredecoder.crypto import derive_public_key, validate_key_pair
231
+
232
+ # Derive public key from MeshCore private key (64-byte format)
233
+ private_key = '18469d6140447f77de13cd8d761e605431f52269fbff43b0925752ed9e6745435dc6a86d2568af8b70d3365db3f88234760c8ecc645ce469829bc45b65f1d5d5'
234
+
235
+ public_key = derive_public_key(private_key)
236
+ print('Derived Public Key:', public_key)
237
+ # Output: 4852B69364572B52EFA1B6BB3E6D0ABED4F389A1CBFBB60A9BBA2CCE649CAF0E
238
+
239
+ # Validate a key pair
240
+ is_valid = validate_key_pair(private_key, public_key)
241
+ print('Key pair valid:', is_valid) # True
242
+ ```
243
+
244
+ ## Command Line Interface
245
+
246
+ For quick analysis from the terminal, use the CLI:
247
+
248
+ ```bash
249
+ # Analyze a packet
250
+ python cli.py decode 11007E7662676F7F0850A8A355BAAFBFC1EB7B4174C340442D7D7161C9474A2C94006CE7CF682E58408DD8FCC51906ECA98EBF94A037886BDADE7ECD09FD92B839491DF3809C9454F5286D1D3370AC31A34593D569E9A042A3B41FD331DFFB7E18599CE1E60992A076D50238C5B8F85757375354522F50756765744D65736820436F75676172
251
+
252
+ # With decryption (provide channel secrets)
253
+ python cli.py decode 150011C3C1354D619BAE9590E4D177DB7EEAF982F5BDCF78005D75157D9535FA90178F785D --key 8b3387e9c5cdea6ac9e5edbaa115cd72
254
+
255
+ # Show detailed structure analysis
256
+ python cli.py decode --structure 11007E7662676F7F0850A8A355BAAFBFC1EB7B4174C340442D7D7161C9474A2C94006CE7CF682E58408DD8FCC51906ECA98EBF94A037886BDADE7ECD09FD92B839491DF3809C9454F5286D1D3370AC31A34593D569E9A042A3B41FD331DFFB7E18599CE1E60992A076D50238C5B8F85757375354522F50756765744D65736820436F75676172
257
+
258
+ # JSON output
259
+ python cli.py decode --json 11007E7662676F7F0850A8A355BAAFBFC1EB7B4174C340442D7D7161C9474A2C94006CE7CF682E58408DD8FCC51906ECA98EBF94A037886BDADE7ECD09FD92B839491DF3809C9454F5286D1D3370AC31A34593D569E9A042A3B41FD331DFFB7E18599CE1E60992A076D50238C5B8F85757375354522F50756765744D65736820436F75676172
260
+
261
+ # Derive public key from MeshCore private key
262
+ python cli.py derive-key 18469d6140447f77de13cd8d761e605431f52269fbff43b0925752ed9e6745435dc6a86d2568af8b70d3365db3f88234760c8ecc645ce469829bc45b65f1d5d5
263
+
264
+ # Validate key pair
265
+ python cli.py validate-key 18469d6140447f77de13cd8d761e605431f52269fbff43b0925752ed9e6745435dc6a86d2568af8b70d3365db3f88234760c8ecc645ce469829bc45b65f1d5d5 4852b69364572b52efa1b6bb3e6d0abed4f389a1cbfbb60a9bba2cce649caf0e
266
+ ```
267
+
268
+
269
+
270
+
271
+ ## License
272
+
273
+ MIT License
274
+
275
+ Copyright (c) 2025 Michael Hart <michaelhart@michaelhart.me> (https://github.com/michaelhart)
276
+
277
+ Permission is hereby granted, free of charge, to any person obtaining a copy
278
+ of this software and associated documentation files (the "Software"), to deal
279
+ in the Software without restriction, including without limitation the rights
280
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
281
+ copies of the Software, and to permit persons to whom the Software is
282
+ furnished to do so, subject to the following conditions:
283
+
284
+ The above copyright notice and this permission notice shall be included in all
285
+ copies or substantial portions of the Software.
286
+
287
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
288
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
289
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
290
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
291
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
292
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
293
+ SOFTWARE.