@johnhenry/browsermesh-primitives 0.0.2 → 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 +11 -1
- package/package.json +7 -2
- package/src/identity.mjs +47 -4
- package/src/index.d.ts +30 -0
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
|
-
|
|
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
|
+
"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",
|
|
@@ -32,7 +32,12 @@
|
|
|
32
32
|
},
|
|
33
33
|
"homepage": "https://opensource.johnhenry.me/browsermesh/",
|
|
34
34
|
"scripts": {
|
|
35
|
-
"
|
|
35
|
+
"pretest": "npm run typecheck",
|
|
36
|
+
"test": "node --test test/*.test.mjs",
|
|
37
|
+
"typecheck": "tsc --noEmit -p test/tsconfig.json"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"typescript": "^7.0.2"
|
|
36
41
|
},
|
|
37
42
|
"engines": {
|
|
38
43
|
"node": ">=26.0.0"
|
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
|
-
*
|
|
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
|
-
*
|
|
163
|
-
*
|
|
164
|
-
*
|
|
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
|
@@ -19,12 +19,42 @@ export declare function encodeBase64url(bytes: Uint8Array): string;
|
|
|
19
19
|
export declare function decodeBase64url(str: string): Uint8Array;
|
|
20
20
|
export declare function derivePodId(publicKey: CryptoKey): Promise<string>;
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Probe for WebCrypto Ed25519 support by asking for a key. Resolves true or
|
|
24
|
+
* false, never throws, and caches the answer. Call this before
|
|
25
|
+
* `PodIdentity.generate()` if you need to degrade gracefully rather than
|
|
26
|
+
* catch a NotSupportedError from four frames down.
|
|
27
|
+
*/
|
|
28
|
+
export declare function probeEd25519Support(): Promise<boolean>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Cached answer from `probeEd25519Support()`, or `null` when the probe has
|
|
32
|
+
* not resolved yet. Treat `null` as "unknown"; await `probeEd25519Support()`
|
|
33
|
+
* for a definite answer.
|
|
34
|
+
*/
|
|
35
|
+
export declare function supportsEd25519(): boolean | null;
|
|
36
|
+
|
|
37
|
+
/** Reset the cached Ed25519 probe. Tests only. */
|
|
38
|
+
export declare function _resetEd25519Probe(): void;
|
|
39
|
+
|
|
22
40
|
export declare class PodIdentity {
|
|
23
41
|
keyPair: CryptoKeyPair;
|
|
24
42
|
podId: string;
|
|
25
43
|
constructor(opts: { keyPair: CryptoKeyPair; podId: string });
|
|
26
44
|
static generate(): Promise<PodIdentity>;
|
|
27
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
|
+
*/
|
|
28
58
|
static verify(publicKey: CryptoKey, data: BufferSource, signature: BufferSource): Promise<boolean>;
|
|
29
59
|
}
|
|
30
60
|
|