@johnhenry/browsermesh-primitives 0.1.0 → 0.2.0

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
@@ -25,7 +25,7 @@ Every other package in this monorepo depends on `browsermesh-primitives` directl
25
25
 
26
26
  ## Provenance
27
27
 
28
- Previously maintained as an independent, standalone repository and published to npm, unscoped, as `browsermesh-primitives@0.1.1` (initial release `0.1.0`, 2026-03-15), with its own CI already wired up (tests, CodeQL, dependency review). Imported into the `@johnhenry/browsermesh` monorepo via `git subtree` -- preserving its full commit history -- and rescoped to `@johnhenry/browsermesh-primitives`; the version restarts at `0.0.0` per family convention.
28
+ Previously maintained as an independent, standalone repository and published to npm, unscoped, as `browsermesh-primitives@0.1.1` (initial release `0.2.0`, 2026-03-15), with its own CI already wired up (tests, CodeQL, dependency review). Imported into the `@johnhenry/browsermesh` monorepo via `git subtree` -- preserving its full commit history -- and rescoped to `@johnhenry/browsermesh-primitives`; the version restarts at `0.0.0` per family convention.
29
29
 
30
30
  ## Install
31
31
 
@@ -61,17 +61,10 @@ console.log(identity.podId) // base64url-encoded SHA-256 of public key
61
61
  // Sign and verify data
62
62
  const data = new TextEncoder().encode('hello mesh')
63
63
  const sig = await identity.sign(data)
64
- // Canonical object form (preferred): named fields, no argument-order pitfalls
65
- const ok = await PodIdentity.verify({
66
- publicKey: identity.keyPair.publicKey,
67
- signature: sig,
68
- message: data,
69
- })
70
- // Positional form still works, but NOTE its order is (publicKey, data, signature)
71
- // -- unlike WebCrypto/wsh (key, signature, data) and raijin (message, signature, key).
72
- // Swapping data/signature returns false instead of throwing.
73
- const ok2 = await PodIdentity.verify(identity.keyPair.publicKey, data, sig)
74
- // sign also accepts the object form: identity.sign({ message: data })
64
+ // Argument order is (publicKey, signature, data) -- same as crypto.subtle.verify.
65
+ // BREAKING in 0.2.0: earlier versions took (publicKey, data, signature); the old
66
+ // order now throws a TypeError instead of silently returning false.
67
+ const ok = await PodIdentity.verify(identity.keyPair.publicKey, sig, data)
75
68
 
76
69
  // CRDTs -- merge state across peers
77
70
  const clockA = new VectorClock()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnhenry/browsermesh-primitives",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Shared primitives for browser mesh networking — wire format, identity (Ed25519), CRDTs, capabilities, trust model, and ACL engine",
5
5
  "type": "module",
6
6
  "main": "./src/index.mjs",
package/src/identity.mjs CHANGED
@@ -147,22 +147,10 @@ export class PodIdentity {
147
147
  /**
148
148
  * Sign data with this identity's private key.
149
149
  *
150
- * Two call forms:
151
- * - `sign(data)` -- positional (original form).
152
- * - `sign({ message })` -- canonical object form shared with the sibling
153
- * libraries.
154
- *
155
- * @param {BufferSource | { message: BufferSource }} data - Data to sign, or
156
- * an object carrying it as `message`
150
+ * @param {BufferSource} data - Data to sign
157
151
  * @returns {Promise<Uint8Array>} Ed25519 signature
158
152
  */
159
153
  async sign(data) {
160
- if (isPlainObject(data)) {
161
- if (data.message === undefined) {
162
- throw new TypeError('PodIdentity.sign({ message }) requires `message`');
163
- }
164
- data = data.message;
165
- }
166
154
  return new Uint8Array(
167
155
  await crypto.subtle.sign('Ed25519', this.keyPair.privateKey, data)
168
156
  );
@@ -171,43 +159,40 @@ export class PodIdentity {
171
159
  /**
172
160
  * Verify a signature against a public key.
173
161
  *
174
- * Two call forms:
162
+ * Argument order is `(publicKey, signature, data)`, the same as WebCrypto's
163
+ * `crypto.subtle.verify('Ed25519', publicKey, signature, data)`.
175
164
  *
176
- * - **Canonical (preferred):** `verify({ publicKey, signature, message })`.
177
- * Named fields, so argument order cannot be confused.
178
- * - **Positional (kept for compatibility):** `verify(publicKey, data, signature)`.
179
- * WARNING: this order is (key, DATA, SIGNATURE), the odd one out. WebCrypto's
180
- * own `crypto.subtle.verify` and wsh use (key, SIGNATURE, DATA); raijin uses
181
- * (message, signature, key). Swapping data and signature here silently
182
- * returns `false` rather than throwing. Prefer the object form.
165
+ * BREAKING (primitives 0.2.0): releases before this took
166
+ * `(publicKey, data, signature)`. A guard throws a TypeError when the old
167
+ * order is detected (Ed25519 signatures are exactly 64 bytes) rather than
168
+ * silently returning false.
183
169
  *
184
- * @param {CryptoKey | { publicKey: CryptoKey, signature: BufferSource, message: BufferSource }} publicKey
185
- * Ed25519 public key, or the canonical options object
186
- * @param {BufferSource} [data] - Original data (positional form only)
187
- * @param {BufferSource} [signature] - Signature to verify (positional form only)
170
+ * @param {CryptoKey} publicKey - Ed25519 public key
171
+ * @param {BufferSource} signature - Signature to verify (64 bytes)
172
+ * @param {BufferSource} data - Original data
188
173
  * @returns {Promise<boolean>}
189
174
  */
190
- static async verify(publicKey, data, signature) {
191
- if (isPlainObject(publicKey)) {
192
- ({ publicKey, signature, message: data } = publicKey);
193
- if (!publicKey || signature === undefined || data === undefined) {
194
- throw new TypeError(
195
- 'PodIdentity.verify({ publicKey, signature, message }) requires all three fields'
196
- );
197
- }
175
+ static async verify(publicKey, signature, data) {
176
+ if (
177
+ byteLengthOf(signature) !== ED25519_SIGNATURE_BYTES &&
178
+ byteLengthOf(data) === ED25519_SIGNATURE_BYTES
179
+ ) {
180
+ throw new TypeError(
181
+ 'PodIdentity.verify: argument order changed to (publicKey, signature, data) ' +
182
+ 'in this version (previously (publicKey, data, signature)); ' +
183
+ 'the signature argument is not 64 bytes but the data argument is'
184
+ );
198
185
  }
199
186
  return crypto.subtle.verify('Ed25519', publicKey, signature, data);
200
187
  }
201
188
  }
202
189
 
190
+ const ED25519_SIGNATURE_BYTES = 64;
191
+
203
192
  /**
204
- * True for a plain `{...}` options object (not a CryptoKey, TypedArray,
205
- * ArrayBuffer, etc.).
206
- * @param {unknown} v
207
- * @returns {boolean}
193
+ * @param {BufferSource} v
194
+ * @returns {number}
208
195
  */
209
- function isPlainObject(v) {
210
- if (v === null || typeof v !== 'object') return false;
211
- const proto = Object.getPrototypeOf(v);
212
- return proto === Object.prototype || proto === null;
196
+ function byteLengthOf(v) {
197
+ return v && typeof v.byteLength === 'number' ? v.byteLength : -1;
213
198
  }
package/src/index.d.ts CHANGED
@@ -43,13 +43,12 @@ export declare class PodIdentity {
43
43
  constructor(opts: { keyPair: CryptoKeyPair; podId: string });
44
44
  static generate(): Promise<PodIdentity>;
45
45
  sign(data: BufferSource): Promise<Uint8Array>;
46
- sign(opts: { message: BufferSource }): Promise<Uint8Array>;
47
- /** Canonical form (preferred): named fields, no argument-order pitfalls. */
48
- static verify(opts: {
49
- publicKey: CryptoKey;
50
- signature: BufferSource;
51
- message: BufferSource;
52
- }): Promise<boolean>;
46
+ /**
47
+ * Argument order is (publicKey, signature, data), matching WebCrypto.
48
+ * BREAKING in 0.2.0: previously (publicKey, data, signature). Throws a
49
+ * TypeError if the old order is detected (64-byte data, non-64-byte signature).
50
+ */
51
+ static verify(publicKey: CryptoKey, signature: BufferSource, data: BufferSource): Promise<boolean>;
53
52
  /**
54
53
  * Positional form, kept for compatibility. WARNING: order is
55
54
  * (publicKey, DATA, SIGNATURE) -- unlike WebCrypto/wsh (key, signature, data)