@johnhenry/browsermesh-primitives 0.0.0 → 0.0.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/package.json +1 -1
- package/src/identity.mjs +78 -5
- package/src/index.mjs +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@johnhenry/browsermesh-primitives",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.1",
|
|
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
|
@@ -25,6 +25,63 @@ export function decodeBase64url(str) {
|
|
|
25
25
|
return bytes;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
/** @type {boolean|null} Cached Ed25519 probe result; null until it resolves. */
|
|
29
|
+
let _ed25519Supported = null;
|
|
30
|
+
|
|
31
|
+
/** @type {Promise<boolean>|null} In-flight probe, so it runs at most once. */
|
|
32
|
+
let _ed25519Probe = null;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Probe for WebCrypto Ed25519 support by asking for a key. Resolves true or
|
|
36
|
+
* false, never throws, and caches the answer.
|
|
37
|
+
*
|
|
38
|
+
* This library has no software fallback for Ed25519, so a false here means the
|
|
39
|
+
* whole identity layer is unavailable in this environment. WebCrypto Ed25519
|
|
40
|
+
* shipped in **Safari 17** and **Chromium 137** — the latter is recent enough
|
|
41
|
+
* that Android devices with an un-updated System WebView will not have it.
|
|
42
|
+
* Call this before PodIdentity.generate() if you need to degrade gracefully
|
|
43
|
+
* rather than catch a NotSupportedError from four frames down.
|
|
44
|
+
*
|
|
45
|
+
* @returns {Promise<boolean>}
|
|
46
|
+
*/
|
|
47
|
+
export function probeEd25519Support() {
|
|
48
|
+
if (_ed25519Probe) return _ed25519Probe;
|
|
49
|
+
_ed25519Probe = (async () => {
|
|
50
|
+
if (typeof crypto === 'undefined' || !crypto.subtle ||
|
|
51
|
+
typeof crypto.subtle.generateKey !== 'function') {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
await crypto.subtle.generateKey({ name: 'Ed25519' }, false, ['sign', 'verify']);
|
|
56
|
+
return true;
|
|
57
|
+
} catch {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
})().then((ok) => {
|
|
61
|
+
_ed25519Supported = ok;
|
|
62
|
+
return ok;
|
|
63
|
+
});
|
|
64
|
+
return _ed25519Probe;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Cached answer from probeEd25519Support(), or null when the probe has not
|
|
69
|
+
* resolved yet. Treat null as "unknown"; await probeEd25519Support() for a
|
|
70
|
+
* definite answer.
|
|
71
|
+
*
|
|
72
|
+
* @returns {boolean|null}
|
|
73
|
+
*/
|
|
74
|
+
export function supportsEd25519() {
|
|
75
|
+
if (_ed25519Supported === null) probeEd25519Support();
|
|
76
|
+
return _ed25519Supported;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Reset the cached Ed25519 probe. Tests only. */
|
|
80
|
+
export function _resetEd25519Probe() {
|
|
81
|
+
_ed25519Supported = null;
|
|
82
|
+
_ed25519Probe = null;
|
|
83
|
+
}
|
|
84
|
+
|
|
28
85
|
/**
|
|
29
86
|
* Derive a pod ID from an Ed25519 public key.
|
|
30
87
|
* Pod ID = base64url(SHA-256(raw public key bytes)).
|
|
@@ -59,14 +116,30 @@ export class PodIdentity {
|
|
|
59
116
|
/**
|
|
60
117
|
* Generate a new PodIdentity with a fresh Ed25519 key pair.
|
|
61
118
|
*
|
|
119
|
+
* Requires WebCrypto Ed25519 (Safari 17+, Chromium 137+). There is no
|
|
120
|
+
* software fallback: where the algorithm is missing this throws with a
|
|
121
|
+
* message naming the requirement rather than a bare NotSupportedError.
|
|
122
|
+
* Use probeEd25519Support() to check first.
|
|
123
|
+
*
|
|
62
124
|
* @returns {Promise<PodIdentity>}
|
|
63
125
|
*/
|
|
64
126
|
static async generate() {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
127
|
+
let keyPair;
|
|
128
|
+
try {
|
|
129
|
+
keyPair = await crypto.subtle.generateKey(
|
|
130
|
+
{ name: 'Ed25519' },
|
|
131
|
+
true, // extractable
|
|
132
|
+
['sign', 'verify']
|
|
133
|
+
);
|
|
134
|
+
} catch (err) {
|
|
135
|
+
throw new Error(
|
|
136
|
+
'WebCrypto Ed25519 is unavailable in this environment, and this library ' +
|
|
137
|
+
'has no software fallback. Ed25519 requires Safari 17+ or Chromium 137+ ' +
|
|
138
|
+
'(including the Android System WebView). Original error: ' +
|
|
139
|
+
(err && err.message ? err.message : String(err)),
|
|
140
|
+
{ cause: err }
|
|
141
|
+
);
|
|
142
|
+
}
|
|
70
143
|
const podId = await derivePodId(keyPair.publicKey);
|
|
71
144
|
return new PodIdentity({ keyPair, podId });
|
|
72
145
|
}
|
package/src/index.mjs
CHANGED
|
@@ -5,7 +5,7 @@ export { MESH_TYPE, MESH_ERROR } from "./constants.mjs";
|
|
|
5
5
|
export { MeshError, MeshProtocolError, MeshCapabilityError } from "./errors.mjs";
|
|
6
6
|
|
|
7
7
|
// Identity
|
|
8
|
-
export { PodIdentity, derivePodId, encodeBase64url, decodeBase64url } from "./identity.mjs";
|
|
8
|
+
export { PodIdentity, derivePodId, encodeBase64url, decodeBase64url, supportsEd25519, probeEd25519Support, _resetEd25519Probe } from "./identity.mjs";
|
|
9
9
|
|
|
10
10
|
// Wire format
|
|
11
11
|
export { messageTypeRegistry, encodeMeshMessage, decodeMeshMessage } from "./wire.mjs";
|