@johnhenry/browsermesh-primitives 0.0.3 → 0.1.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
@@ -61,7 +61,17 @@ 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
- const ok = await PodIdentity.verify(identity.keyPair.publicKey, data, sig)
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 })
65
75
 
66
76
  // CRDTs -- merge state across peers
67
77
  const clockA = new VectorClock()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnhenry/browsermesh-primitives",
3
- "version": "0.0.3",
3
+ "version": "0.1.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,10 +147,22 @@ export class PodIdentity {
147
147
  /**
148
148
  * Sign data with this identity's private key.
149
149
  *
150
- * @param {BufferSource} data - Data to sign
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`
151
157
  * @returns {Promise<Uint8Array>} Ed25519 signature
152
158
  */
153
159
  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
+ }
154
166
  return new Uint8Array(
155
167
  await crypto.subtle.sign('Ed25519', this.keyPair.privateKey, data)
156
168
  );
@@ -159,12 +171,43 @@ export class PodIdentity {
159
171
  /**
160
172
  * Verify a signature against a public key.
161
173
  *
162
- * @param {CryptoKey} publicKey - Ed25519 public key
163
- * @param {BufferSource} data - Original data
164
- * @param {BufferSource} signature - Signature to verify
174
+ * Two call forms:
175
+ *
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.
183
+ *
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)
165
188
  * @returns {Promise<boolean>}
166
189
  */
167
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
+ }
198
+ }
168
199
  return crypto.subtle.verify('Ed25519', publicKey, signature, data);
169
200
  }
170
201
  }
202
+
203
+ /**
204
+ * True for a plain `{...}` options object (not a CryptoKey, TypedArray,
205
+ * ArrayBuffer, etc.).
206
+ * @param {unknown} v
207
+ * @returns {boolean}
208
+ */
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;
213
+ }
package/src/index.d.ts CHANGED
@@ -43,6 +43,18 @@ 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>;
53
+ /**
54
+ * Positional form, kept for compatibility. WARNING: order is
55
+ * (publicKey, DATA, SIGNATURE) -- unlike WebCrypto/wsh (key, signature, data)
56
+ * and raijin (message, signature, key). Prefer the object form.
57
+ */
46
58
  static verify(publicKey: CryptoKey, data: BufferSource, signature: BufferSource): Promise<boolean>;
47
59
  }
48
60