@meshcore-cz/meshpkt 0.1.0 → 0.1.1

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.
package/README.md CHANGED
@@ -1,25 +1,189 @@
1
1
  # @meshcore-cz/meshpkt
2
2
 
3
+ [![npm](https://img.shields.io/npm/v/@meshcore-cz/meshpkt)](https://www.npmjs.com/package/@meshcore-cz/meshpkt)
4
+ [![Go Reference](https://pkg.go.dev/badge/github.com/meshcore-cz/meshpkt.svg)](https://pkg.go.dev/github.com/meshcore-cz/meshpkt)
5
+
3
6
  MeshCore radio packet codec for JavaScript and TypeScript, powered by WebAssembly.
4
7
 
8
+ - **npm:** [npmjs.com/package/@meshcore-cz/meshpkt](https://www.npmjs.com/package/@meshcore-cz/meshpkt)
9
+ - **Go source & docs:** [pkg.go.dev/github.com/meshcore-cz/meshpkt](https://pkg.go.dev/github.com/meshcore-cz/meshpkt)
10
+
5
11
  ## Install
6
12
 
7
13
  ```sh
8
14
  npm install @meshcore-cz/meshpkt
9
- ````
15
+ ```
10
16
 
11
- ## Usage
17
+ ## Quick start
12
18
 
13
19
  ```ts
14
20
  import { load } from "@meshcore-cz/meshpkt";
15
21
 
16
22
  const meshpkt = await load();
23
+ ```
17
24
 
18
- const envelope = meshpkt.decodeEnvelope(
19
- "your-hex-encoded-meshcore-packet"
25
+ `load()` fetches and initialises the bundled TinyGo WASM module. Call it once; subsequent calls return the same promise. All methods are synchronous after that.
26
+
27
+ ## Error handling
28
+
29
+ Every method returns either the typed result or `{ error: string }`. The `"error"` key is the reliable way to distinguish them:
30
+
31
+ ```ts
32
+ const result = meshpkt.decodeEnvelope(hex);
33
+ if ("error" in result) {
34
+ console.error("decode failed:", result.error);
35
+ } else {
36
+ console.log(result.type, result.hopCount);
37
+ }
38
+ ```
39
+
40
+ ---
41
+
42
+ ## Examples
43
+
44
+ ### Decode a packet from the air
45
+
46
+ ```ts
47
+ const env = meshpkt.decodeEnvelope(
48
+ "15453287568f06bf2d5ad94765d9aaa4aef45a465a5a84142b5abb55eafe11980bc7b891"
20
49
  );
21
50
 
22
- console.log(envelope);
51
+ if ("error" in env) throw new Error(env.error);
52
+
53
+ console.log(env.route); // "FLOOD"
54
+ console.log(env.type); // "ADVERT"
55
+ console.log(env.hopCount); // 5
56
+ console.log(env.hops); // ["3287", "568f", "06bf", "2d5a", "d947"]
57
+ console.log(env.payloadHex); // raw payload, pass to a decode* call below
58
+ ```
59
+
60
+ ---
61
+
62
+ ### Channel (GRP_TXT) messages
63
+
64
+ ```ts
65
+ // Encode — by channel name
66
+ const pkt = meshpkt.encodeGroupText("#general", "Alice", "Hello mesh!");
67
+ if ("error" in pkt) throw new Error(pkt.error);
68
+ console.log(pkt.hex); // ready to send over the radio link
69
+
70
+ // Encode — by pre-shared secret (16 bytes hex)
71
+ const secret = meshpkt.deriveChannelSecret("#general");
72
+ if ("error" in secret) throw new Error(secret.error);
73
+ const pkt2 = meshpkt.encodeGroupTextSecret(secret.hex, "Alice", "Hello mesh!");
74
+
75
+ // Decode — extract payload first, then decrypt
76
+ const env = meshpkt.decodeEnvelope(rawHex);
77
+ if ("error" in env || env.type !== "GRP_TXT") return;
78
+
79
+ const msg = meshpkt.decodeGroupText(env.payloadHex, "#general");
80
+ if ("error" in msg) throw new Error(msg.error);
81
+
82
+ console.log(msg.sender); // "Alice"
83
+ console.log(msg.text); // "Hello mesh!"
84
+ console.log(new Date(msg.timestamp * 1000));
85
+ ```
86
+
87
+ ---
88
+
89
+ ### Direct (TXT_MSG) messages
90
+
91
+ ```ts
92
+ // Generate a keypair (or load an existing one)
93
+ const kp = meshpkt.generateKeypair();
94
+ if ("error" in kp) throw new Error(kp.error);
95
+ // kp.privateKey / kp.publicKey — 64-char hex strings
96
+
97
+ // Encode
98
+ const pkt = meshpkt.encodeDirectText(myPrivKey, peerPubKey, "Hey, direct!");
99
+ if ("error" in pkt) throw new Error(pkt.error);
100
+
101
+ // Decode
102
+ const env = meshpkt.decodeEnvelope(rawHex);
103
+ if ("error" in env || env.type !== "TXT_MSG") return;
104
+
105
+ const msg = meshpkt.decodeDirectText(env.payloadHex, myPrivKey, peerPubKey);
106
+ if ("error" in msg) throw new Error(msg.error);
107
+
108
+ console.log(msg.text); // "Hey, direct!"
109
+ console.log(msg.destHash); // first-byte hash of recipient's public key
110
+ ```
111
+
112
+ ---
113
+
114
+ ### Node advertisements (ADVERT)
115
+
116
+ ```ts
117
+ const env = meshpkt.decodeEnvelope(rawHex);
118
+ if ("error" in env || env.type !== "ADVERT") return;
119
+
120
+ const adv = meshpkt.decodeAdvert(env.payloadHex);
121
+ if ("error" in adv) throw new Error(adv.error);
122
+
123
+ console.log(adv.name); // "CZ.NIC Repeater"
124
+ console.log(adv.nodeType); // 2 = repeater
125
+ if (adv.hasGPS) {
126
+ console.log(adv.lat, adv.lon);
127
+ }
128
+ ```
129
+
130
+ ---
131
+
132
+ ### Key utilities
133
+
134
+ ```ts
135
+ // Generate a fresh X25519 keypair
136
+ const kp = meshpkt.generateKeypair();
137
+ if ("error" in kp) throw new Error(kp.error);
138
+ console.log(kp.publicKey); // 64-char hex
139
+ console.log(kp.privateKey); // 64-char hex
140
+
141
+ // Derive a channel PSK from its name
142
+ const secret = meshpkt.deriveChannelSecret("#ops");
143
+ if ("error" in secret) throw new Error(secret.error);
144
+ console.log(secret.hex); // 32-char hex (16 bytes)
145
+
146
+ // Compute X25519 shared secret for direct-message crypto
147
+ const shared = meshpkt.sharedSecret(myPrivKey, peerPubKey);
148
+ if ("error" in shared) throw new Error(shared.error);
149
+ console.log(shared.hex); // 64-char hex (32 bytes; use first 16 as AES key)
150
+ ```
151
+
152
+ ---
153
+
154
+ ### Loader options
155
+
156
+ By default `load()` resolves the `.wasm` and `wasm_exec.js` files relative to the package using `import.meta.url`. Override either URL when your bundler places assets elsewhere:
157
+
158
+ ```ts
159
+ const meshpkt = await load({
160
+ wasmURL: new URL("/assets/meshpkt.wasm", import.meta.url),
161
+ wasmExecURL: new URL("/assets/wasm_exec.js", import.meta.url),
162
+ });
163
+ ```
164
+
165
+ ---
166
+
167
+ ## TypeScript types
168
+
169
+ All result interfaces are re-exported from the package root:
170
+
171
+ ```ts
172
+ import type {
173
+ Envelope,
174
+ GroupTextPayload,
175
+ DirectTextPayload,
176
+ AdvertPayload,
177
+ GrpDataPayload,
178
+ AckPayload,
179
+ ReqPayload,
180
+ ResponsePayload,
181
+ PathPayload,
182
+ AnonReqPayload,
183
+ ControlPayload,
184
+ KeypairResult,
185
+ ErrResult,
186
+ } from "@meshcore-cz/meshpkt";
23
187
  ```
24
188
 
25
- The package includes the TinyGo WebAssembly module and generated TypeScript types.
189
+ The `RouteTypes` and `PayloadTypes` constant arrays (code + label) are also exported for building UI dropdowns or switch statements.
@@ -97,7 +97,7 @@ export interface MeshcoreWasm {
97
97
  encodeGrpData(channelName: string, dataType: number, data: string): HexResult | ErrResult;
98
98
  encodeGrpDataSecret(secret: string, dataType: number, data: string): HexResult | ErrResult;
99
99
  encodeDirectText(privKey: string, peerPubKey: string, text: string): HexResult | ErrResult;
100
- encodeAdvert(pubKey: string, signature: string, name: string, hasGPS: number, lat: unknown, lon: unknown): HexResult | ErrResult;
100
+ encodeAdvert(pubKey: string, signature: string, name: string, hasGPS: number, lat: number, lon: number): HexResult | ErrResult;
101
101
  encodeAck(crc: number): HexResult | ErrResult;
102
102
  encodeReq(privKey: string, peerPubKey: string, reqType: number, data: string): HexResult | ErrResult;
103
103
  encodeAnonReq(destPubKey: string, myPrivKey: string, data: string): HexResult | ErrResult;
@@ -174,3 +174,42 @@ export declare const PayloadTypes: readonly [{
174
174
  readonly code: 15;
175
175
  readonly label: "RAW_CUSTOM";
176
176
  }];
177
+ export interface ParamMeta {
178
+ name: string;
179
+ kind: string;
180
+ label: string;
181
+ placeholder: string;
182
+ optional: boolean;
183
+ choices: {
184
+ value: number;
185
+ label: string;
186
+ }[];
187
+ showWhen: string;
188
+ showValue: number;
189
+ group: string;
190
+ action: string;
191
+ widget: string;
192
+ autoFill: string;
193
+ secret: boolean;
194
+ }
195
+ export interface ResultMeta {
196
+ name: string;
197
+ kind: string;
198
+ optional: boolean;
199
+ label: string;
200
+ }
201
+ export interface OpMeta {
202
+ name: string;
203
+ category: string;
204
+ label: string;
205
+ tabGroup: string;
206
+ tabGroupLabel: string;
207
+ tabGroupSub: string;
208
+ tabGroupDoc: string;
209
+ tabLabel: string;
210
+ packetType: string;
211
+ params: ParamMeta[];
212
+ result: ResultMeta[];
213
+ resultTypeName: string;
214
+ }
215
+ export declare const OpMetas: OpMeta[];