@openllmsh/sdk 0.0.0-stage → 2.10.12-alpha.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/ARCHITECTURE.md +43 -0
- package/LICENSE +202 -0
- package/README.md +142 -2
- package/assets/openllm-light.svg +18 -0
- package/assets/openllm.svg +18 -0
- package/client/chat.ts +140 -0
- package/client/devices.ts +269 -0
- package/client/http.ts +72 -0
- package/client/index.ts +129 -0
- package/client/relay-mux.ts +547 -0
- package/client/types.ts +51 -0
- package/client/vault.ts +239 -0
- package/connect/chat.ts +58 -0
- package/connect/client.ts +240 -0
- package/connect/config.ts +47 -0
- package/connect/errors.ts +121 -0
- package/connect/index.ts +23 -0
- package/connect/oauth.ts +265 -0
- package/connect/routes.ts +234 -0
- package/connect/store.ts +107 -0
- package/mcp/handle.ts +24 -0
- package/mcp/index.ts +76 -0
- package/mcp/server.ts +319 -0
- package/mcp/tools/marketplace.ts +439 -0
- package/mcp/tools/memory-client.ts +297 -0
- package/mcp/tools/memory-config.ts +32 -0
- package/mcp/tools/memory.ts +369 -0
- package/mcp/tools/model-priority.ts +88 -0
- package/mcp/tools/openllm.ts +883 -0
- package/mcp/types.ts +28 -0
- package/openapi/call.ts +90 -0
- package/openapi/index.ts +8 -0
- package/openapi/openapi.json +80213 -0
- package/openapi/operations.ts +2075 -0
- package/package.json +39 -4
- package/protocol/CONTRIBUTING.md +21 -0
- package/protocol/README.md +134 -0
- package/protocol/access.ts +186 -0
- package/protocol/agent-prompt-text.ts +32 -0
- package/protocol/agent.ts +991 -0
- package/protocol/api-key-syntax.ts +105 -0
- package/protocol/assets/openllm-light.svg +18 -0
- package/protocol/assets/openllm.svg +18 -0
- package/protocol/audio.ts +29 -0
- package/protocol/auth.ts +142 -0
- package/protocol/changelog.ts +155 -0
- package/protocol/chat.ts +458 -0
- package/protocol/client-registry.ts +356 -0
- package/protocol/config.ts +345 -0
- package/protocol/control-channel.ts +115 -0
- package/protocol/cooldown-reason.ts +78 -0
- package/protocol/cost.ts +10 -0
- package/protocol/daemon-port.ts +31 -0
- package/protocol/daemon.ts +1524 -0
- package/protocol/default-chains.ts +66 -0
- package/protocol/device-state.ts +85 -0
- package/protocol/doctor-report-local.ts +443 -0
- package/protocol/doctor-report-policy.ts +119 -0
- package/protocol/doctor-report.ts +947 -0
- package/protocol/errors.ts +14 -0
- package/protocol/execution-selection.ts +141 -0
- package/protocol/hop-grant.ts +33 -0
- package/protocol/image-edit-parse.ts +263 -0
- package/protocol/images.ts +298 -0
- package/protocol/index.ts +74 -0
- package/protocol/inference-headers.ts +3 -0
- package/protocol/keepalive.ts +69 -0
- package/protocol/local-auth.ts +300 -0
- package/protocol/local-device-control.ts +133 -0
- package/protocol/local-provider.ts +288 -0
- package/protocol/local-session.ts +118 -0
- package/protocol/marketplace.ts +187 -0
- package/protocol/media-attempt.ts +81 -0
- package/protocol/media-bytes.ts +28 -0
- package/protocol/media-default.ts +357 -0
- package/protocol/media-dimensions.ts +132 -0
- package/protocol/media-input.ts +61 -0
- package/protocol/media-model.ts +142 -0
- package/protocol/media-option-issue.ts +90 -0
- package/protocol/media-types.ts +70 -0
- package/protocol/model-match.ts +1051 -0
- package/protocol/model-settings.ts +327 -0
- package/protocol/models.ts +767 -0
- package/protocol/mux.ts +722 -0
- package/protocol/plan-surface.ts +59 -0
- package/protocol/provider-models.ts +16 -0
- package/protocol/provider-status.ts +206 -0
- package/protocol/provider-tier.ts +67 -0
- package/protocol/provider-usage.ts +263 -0
- package/protocol/providers.ts +901 -0
- package/protocol/public-usage-index.ts +354 -0
- package/protocol/realtime-generated.ts +31 -0
- package/protocol/realtime.ts +324 -0
- package/protocol/registry-pointer.ts +118 -0
- package/protocol/relay.ts +702 -0
- package/protocol/replay-session.ts +11 -0
- package/protocol/responses.ts +280 -0
- package/protocol/semver.ts +182 -0
- package/protocol/setup-mode.ts +22 -0
- package/protocol/sha256-hex.ts +98 -0
- package/protocol/stats.ts +406 -0
- package/protocol/streaming.ts +19 -0
- package/protocol/subscription-daemon-floors.ts +33 -0
- package/protocol/subscription-provider-compat.ts +104 -0
- package/protocol/subscription-provider.ts +32 -0
- package/protocol/team-grant.ts +17 -0
- package/protocol/team-shareable.ts +61 -0
- package/protocol/transcription-encoding.ts +76 -0
- package/protocol/url-locality-dns.ts +37 -0
- package/protocol/url-locality.ts +288 -0
- package/protocol/usage-snapshots.ts +9 -0
- package/protocol/vendor-cli-install.ts +29 -0
- package/protocol/video-job-plan.ts +50 -0
- package/protocol/videos.ts +227 -0
- package/tsconfig.json +14 -0
- package/tunnel/ARCHITECTURE.md +111 -0
- package/tunnel/LICENSE +33 -0
- package/tunnel/README.md +45 -0
- package/tunnel/assets/openllm-light.svg +18 -0
- package/tunnel/assets/openllm.svg +18 -0
- package/tunnel/channel-envelope.ts +139 -0
- package/tunnel/codec.ts +191 -0
- package/tunnel/device-anchor.ts +229 -0
- package/tunnel/device-control.ts +118 -0
- package/tunnel/device-grant.ts +173 -0
- package/tunnel/fleet.ts +77 -0
- package/tunnel/index.ts +22 -0
- package/tunnel/mux.ts +580 -0
- package/tunnel/peer-link.ts +390 -0
- package/tunnel/rtc-auth.ts +415 -0
- package/tunnel/rtc-duplex.ts +187 -0
- package/tunnel/rtc-ice.ts +136 -0
- package/tunnel/secure.ts +1298 -0
- package/tunnel/stream-reset-error.ts +14 -0
- package/tunnel/streams.ts +1430 -0
- package/tunnel/team-grant.ts +302 -0
- package/tunnel/wire-bytes.ts +102 -0
- package/vault/ARCHITECTURE.md +470 -0
- package/vault/LICENSE +33 -0
- package/vault/api-key.ts +190 -0
- package/vault/argon2.ts +106 -0
- package/vault/cli-auth-seal.ts +157 -0
- package/vault/cred-seal.ts +146 -0
- package/vault/credential.ts +55 -0
- package/vault/crypto.ts +210 -0
- package/vault/daemon-attest.ts +229 -0
- package/vault/derive.ts +113 -0
- package/vault/device-access.ts +87 -0
- package/vault/device-anchor.ts +185 -0
- package/vault/device-control.ts +174 -0
- package/vault/handoff.ts +396 -0
- package/vault/index.ts +21 -0
- package/vault/passkey.ts +243 -0
- package/vault/phrase.ts +65 -0
- package/vault/pin.ts +153 -0
- package/vault/public-key-seal.ts +131 -0
- package/vault/seed.ts +272 -0
- package/vault/team.ts +355 -0
- package/vault/vault-item.ts +131 -0
- package/vault/vault-migrate-digest.ts +29 -0
- package/vault/wrapped.ts +77 -0
- package/wire/ARCHITECTURE.md +90 -0
- package/wire/CONTRIBUTING.md +19 -0
- package/wire/LICENSE +33 -0
- package/wire/README.md +51 -0
- package/wire/adapters/messages/anthropic-map.ts +37 -0
- package/wire/adapters/messages/index.ts +7 -0
- package/wire/adapters/messages/reasoning-from-items.ts +38 -0
- package/wire/adapters/messages/reasoning-signature.ts +243 -0
- package/wire/adapters/messages/request.ts +574 -0
- package/wire/adapters/messages/response.ts +215 -0
- package/wire/adapters/messages/streaming.ts +1027 -0
- package/wire/adapters/messages/usage.ts +43 -0
- package/wire/adapters/responses/index.ts +3 -0
- package/wire/adapters/responses/request.ts +395 -0
- package/wire/adapters/responses/response.ts +88 -0
- package/wire/adapters/responses/streaming.ts +344 -0
- package/wire/assets/openllm-light.svg +18 -0
- package/wire/assets/openllm.svg +18 -0
- package/wire/features/account-rank.ts +166 -0
- package/wire/features/compaction/compact-request.ts +862 -0
- package/wire/features/compaction/compaction-text.ts +43 -0
- package/wire/features/context-demote.ts +47 -0
- package/wire/features/context-overflow-strategy.ts +26 -0
- package/wire/features/context-skip.ts +170 -0
- package/wire/features/conversation-affinity.ts +144 -0
- package/wire/features/max-tokens-backfill.ts +141 -0
- package/wire/features/model-caps.ts +53 -0
- package/wire/features/model-settings.ts +24 -0
- package/wire/features/output-limit-clamp.ts +64 -0
- package/wire/features/quota-gate.ts +85 -0
- package/wire/index.ts +44 -0
- package/wire/lib/attempt-outcome.ts +43 -0
- package/wire/lib/canonical/content-part.ts +38 -0
- package/wire/lib/canonical/count-tokens-capability.ts +138 -0
- package/wire/lib/canonical/encoding-select.ts +82 -0
- package/wire/lib/canonical/image-signature.ts +202 -0
- package/wire/lib/canonical/image-sse.ts +216 -0
- package/wire/lib/canonical/message.ts +43 -0
- package/wire/lib/canonical/token-estimate.ts +266 -0
- package/wire/lib/encrypted-content.ts +66 -0
- package/wire/lib/error-class.ts +154 -0
- package/wire/lib/error-envelope.ts +56 -0
- package/wire/lib/forwarded-headers.ts +180 -0
- package/wire/lib/provider-error-rules.ts +48 -0
- package/wire/lib/refusal.ts +38 -0
- package/wire/lib/responses-apply-patch.ts +104 -0
- package/wire/lib/responses-namespace.ts +53 -0
- package/wire/lib/secret-scrub.ts +101 -0
- package/wire/lib/streaming/accumulate.ts +221 -0
- package/wire/lib/streaming/finish-reason.ts +33 -0
- package/wire/lib/streaming/peek.ts +154 -0
- package/wire/lib/streaming/provider-decode.ts +259 -0
- package/wire/lib/streaming/response-stream.ts +71 -0
- package/wire/lib/streaming/sse.ts +699 -0
- package/wire/lib/streaming/strip-tool-calls.ts +95 -0
- package/wire/lib/streaming/upstream-error.ts +58 -0
- package/wire/lib/strip-subagent-isolation.ts +284 -0
- package/wire/lib/thinking-signature.ts +156 -0
- package/wire/lib/token-ruler/bpe-core.ts +426 -0
- package/wire/lib/token-ruler/index.ts +2 -0
- package/wire/lib/token-ruler/ruler.ts +79 -0
- package/wire/lib/token-ruler/vocab/claude-ranks.ts +16 -0
- package/wire/lib/token-ruler/vocab/claude.ts +22 -0
- package/wire/lib/token-ruler/vocab/o200k-ranks.ts +18 -0
- package/wire/lib/token-ruler/vocab/o200k.ts +19 -0
- package/wire/lib/tool-name-map.ts +271 -0
- package/wire/lib/tool-schema.ts +353 -0
- package/wire/lib/usage-observer.ts +125 -0
- package/wire/providers/anthropic/adaptive-thinking.ts +291 -0
- package/wire/providers/anthropic/beta-headers.ts +165 -0
- package/wire/providers/anthropic/index.ts +10 -0
- package/wire/providers/anthropic/request.ts +607 -0
- package/wire/providers/anthropic/response.ts +155 -0
- package/wire/providers/anthropic/streaming.ts +315 -0
- package/wire/providers/anthropic/web-search-domains.ts +49 -0
- package/wire/providers/chatgpt/common.ts +136 -0
- package/wire/providers/chatgpt/index.ts +9 -0
- package/wire/providers/chatgpt/request.ts +959 -0
- package/wire/providers/chatgpt/streaming.ts +1007 -0
- package/wire/providers/chatgpt/web-search.ts +63 -0
- package/wire/providers/grok/web-search.ts +87 -0
- package/wire/providers/kimi/web-search.ts +145 -0
- package/wire/providers/upstream-deny.ts +355 -0
- package/wire/providers/upstream-request.ts +619 -0
package/vault/crypto.ts
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure WebCrypto primitives shared by every vault flow. Runs in any
|
|
3
|
+
* runtime that exposes `globalThis.crypto.subtle` — browser, Node 24+,
|
|
4
|
+
* Edge, Bun. Nothing in this file is server- or browser-specific;
|
|
5
|
+
* everything is bytes-in/bytes-out.
|
|
6
|
+
*
|
|
7
|
+
* Wire / on-disk encoding is base64 (`toB64` / `fromB64`) — keeps the
|
|
8
|
+
* existing text-column infrastructure (drizzle, neon HTTP driver,
|
|
9
|
+
* migrations) carrying the blobs without a bytea customType.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const subtle = (): SubtleCrypto => {
|
|
13
|
+
const s = globalThis.crypto?.subtle;
|
|
14
|
+
if (s === undefined) {
|
|
15
|
+
throw new Error("WebCrypto SubtleCrypto is not available in this runtime");
|
|
16
|
+
}
|
|
17
|
+
return s;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const randomBytes = (length: number): Uint8Array => {
|
|
21
|
+
return globalThis.crypto.getRandomValues(new Uint8Array(length));
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const toB64 = (bytes: Uint8Array): string => {
|
|
25
|
+
let bin = "";
|
|
26
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
27
|
+
bin += String.fromCharCode(bytes[i] as number);
|
|
28
|
+
}
|
|
29
|
+
return btoa(bin);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const fromB64 = (b64: string): Uint8Array => {
|
|
33
|
+
const bin = atob(b64);
|
|
34
|
+
const out = new Uint8Array(bin.length);
|
|
35
|
+
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
|
|
36
|
+
return out;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const toB64Url = (bytes: Uint8Array): string => {
|
|
40
|
+
return toB64(bytes)
|
|
41
|
+
.replace(/\+/g, "-")
|
|
42
|
+
.replace(/\//g, "_")
|
|
43
|
+
.replace(/=+$/, "");
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export const fromB64Url = (b64url: string): Uint8Array => {
|
|
47
|
+
const pad =
|
|
48
|
+
b64url.length % 4 === 0 ? "" : "=".repeat(4 - (b64url.length % 4));
|
|
49
|
+
return fromB64(b64url.replace(/-/g, "+").replace(/_/g, "/") + pad);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* A standalone copy of a view's bytes — what `importKey`/`deriveBits` need,
|
|
54
|
+
* since a `Uint8Array` may be a window onto a larger buffer.
|
|
55
|
+
*
|
|
56
|
+
* Returned as a `Uint8Array` rather than a bare `ArrayBuffer` so the caller
|
|
57
|
+
* can `zero()` it. Every caller here does: the copy holds the same key
|
|
58
|
+
* material as its source, and a source that is faithfully zeroed while an
|
|
59
|
+
* unzeroed duplicate lingers is the hygiene contract at the bottom of this
|
|
60
|
+
* file quietly not being kept.
|
|
61
|
+
*/
|
|
62
|
+
const copyOf = (bytes: Uint8Array): Uint8Array =>
|
|
63
|
+
new Uint8Array(
|
|
64
|
+
bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength),
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
const importAesGcmKey = async (keyBytes: Uint8Array): Promise<CryptoKey> => {
|
|
68
|
+
// WebCrypto copies key data into the CryptoKey during import, so wiping our
|
|
69
|
+
// copy afterwards leaves the key usable and the bytes gone.
|
|
70
|
+
const copy = copyOf(keyBytes);
|
|
71
|
+
try {
|
|
72
|
+
return await subtle().importKey(
|
|
73
|
+
"raw",
|
|
74
|
+
copy as BufferSource,
|
|
75
|
+
"AES-GCM",
|
|
76
|
+
false,
|
|
77
|
+
["encrypt", "decrypt"],
|
|
78
|
+
);
|
|
79
|
+
} finally {
|
|
80
|
+
zero(copy);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const gcmParams = (
|
|
85
|
+
nonce: Uint8Array,
|
|
86
|
+
additionalData: Uint8Array | undefined,
|
|
87
|
+
): AesGcmParams =>
|
|
88
|
+
additionalData === undefined
|
|
89
|
+
? { name: "AES-GCM", iv: nonce as BufferSource }
|
|
90
|
+
: {
|
|
91
|
+
name: "AES-GCM",
|
|
92
|
+
iv: nonce as BufferSource,
|
|
93
|
+
additionalData: additionalData as BufferSource,
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* AES-256-GCM encrypt. `nonce` MUST be 12 bytes. The auth tag is
|
|
98
|
+
* appended to the ciphertext by WebCrypto — `aesGcmDecrypt` expects
|
|
99
|
+
* the same shape. Reusing a (key, nonce) pair across two encrypts
|
|
100
|
+
* is a critical failure; callers must pass `randomBytes(12)` fresh
|
|
101
|
+
* each call.
|
|
102
|
+
*
|
|
103
|
+
* `additionalData` (optional AAD) is authenticated but not encrypted:
|
|
104
|
+
* the ciphertext only opens under the exact same bytes. Used to bind a
|
|
105
|
+
* blob to the public facts it is filed under (see `./passkey.ts`).
|
|
106
|
+
*/
|
|
107
|
+
export const aesGcmEncrypt = async (
|
|
108
|
+
key: Uint8Array,
|
|
109
|
+
nonce: Uint8Array,
|
|
110
|
+
plaintext: Uint8Array,
|
|
111
|
+
additionalData?: Uint8Array,
|
|
112
|
+
): Promise<Uint8Array> => {
|
|
113
|
+
if (key.length !== 32) throw new Error("AES-GCM key must be 32 bytes");
|
|
114
|
+
if (nonce.length !== 12) throw new Error("AES-GCM nonce must be 12 bytes");
|
|
115
|
+
const cryptoKey = await importAesGcmKey(key);
|
|
116
|
+
const ct = await subtle().encrypt(
|
|
117
|
+
gcmParams(nonce, additionalData),
|
|
118
|
+
cryptoKey,
|
|
119
|
+
plaintext as BufferSource,
|
|
120
|
+
);
|
|
121
|
+
return new Uint8Array(ct);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export const aesGcmDecrypt = async (
|
|
125
|
+
key: Uint8Array,
|
|
126
|
+
nonce: Uint8Array,
|
|
127
|
+
ciphertext: Uint8Array,
|
|
128
|
+
additionalData?: Uint8Array,
|
|
129
|
+
): Promise<Uint8Array> => {
|
|
130
|
+
if (key.length !== 32) throw new Error("AES-GCM key must be 32 bytes");
|
|
131
|
+
if (nonce.length !== 12) throw new Error("AES-GCM nonce must be 12 bytes");
|
|
132
|
+
const cryptoKey = await importAesGcmKey(key);
|
|
133
|
+
const pt = await subtle().decrypt(
|
|
134
|
+
gcmParams(nonce, additionalData),
|
|
135
|
+
cryptoKey,
|
|
136
|
+
ciphertext as BufferSource,
|
|
137
|
+
);
|
|
138
|
+
return new Uint8Array(pt);
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* HKDF-SHA256. Used to derive `api_wrap_key` from a 32-byte
|
|
143
|
+
* `api_key_secret` plus the public `api_key_id` as salt. Input is
|
|
144
|
+
* already high-entropy so HKDF (not Argon2) is the correct choice.
|
|
145
|
+
*/
|
|
146
|
+
export const hkdfSha256 = async (
|
|
147
|
+
ikm: Uint8Array,
|
|
148
|
+
salt: Uint8Array,
|
|
149
|
+
info: string,
|
|
150
|
+
bytes: number,
|
|
151
|
+
): Promise<Uint8Array> => {
|
|
152
|
+
const ikmCopy = copyOf(ikm);
|
|
153
|
+
let key: CryptoKey;
|
|
154
|
+
try {
|
|
155
|
+
key = await subtle().importKey(
|
|
156
|
+
"raw",
|
|
157
|
+
ikmCopy as BufferSource,
|
|
158
|
+
"HKDF",
|
|
159
|
+
false,
|
|
160
|
+
["deriveBits"],
|
|
161
|
+
);
|
|
162
|
+
} finally {
|
|
163
|
+
zero(ikmCopy);
|
|
164
|
+
}
|
|
165
|
+
// The salt is public (it's the api_key_id), so its copy needs no wipe.
|
|
166
|
+
const out = await subtle().deriveBits(
|
|
167
|
+
{
|
|
168
|
+
name: "HKDF",
|
|
169
|
+
hash: "SHA-256",
|
|
170
|
+
salt: copyOf(salt) as BufferSource,
|
|
171
|
+
info: new TextEncoder().encode(info),
|
|
172
|
+
},
|
|
173
|
+
key,
|
|
174
|
+
bytes * 8,
|
|
175
|
+
);
|
|
176
|
+
return new Uint8Array(out);
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Best-effort overwrite of sensitive bytes. JS engines may have
|
|
181
|
+
* already copied the buffer elsewhere; this is hygiene, not a
|
|
182
|
+
* guarantee. Pair with `finally{}` blocks at the end of any function
|
|
183
|
+
* that touches a DEK or plaintext credential.
|
|
184
|
+
*/
|
|
185
|
+
export const zero = (bytes: Uint8Array): void => {
|
|
186
|
+
bytes.fill(0);
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* A uniform integer in `[0, max)` from the CSPRNG.
|
|
191
|
+
*
|
|
192
|
+
* Rejection-sampled rather than `% max`, which skews toward the low end when
|
|
193
|
+
* `max` doesn't divide 2^32. Used where `Math.random()` would do the job but
|
|
194
|
+
* shouldn't be within reach of anything cryptographic — a UI helper that
|
|
195
|
+
* migrates into key material later is a real way to end up with a predictable
|
|
196
|
+
* secret, and the cost of never having that conversation is one function.
|
|
197
|
+
*/
|
|
198
|
+
export const randomInt = (max: number): number => {
|
|
199
|
+
if (!Number.isInteger(max) || max <= 0) {
|
|
200
|
+
throw new Error("randomInt: max must be a positive integer");
|
|
201
|
+
}
|
|
202
|
+
const limit = Math.floor(0x1_0000_0000 / max) * max;
|
|
203
|
+
const buf = new Uint32Array(1);
|
|
204
|
+
let n: number;
|
|
205
|
+
do {
|
|
206
|
+
globalThis.crypto.getRandomValues(buf);
|
|
207
|
+
n = buf[0] as number;
|
|
208
|
+
} while (n >= limit);
|
|
209
|
+
return n % max;
|
|
210
|
+
};
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Control-key attestation of a daemon's X25519 pubkey (`dpk`).
|
|
3
|
+
*
|
|
4
|
+
* After the install-time trust anchor, attestations are a cache/distribution
|
|
5
|
+
* aid — never the sole trust root. They MUST bind `key_id` and are only
|
|
6
|
+
* written after the browser verifies the daemon's `dpk` proof (or a
|
|
7
|
+
* loopback/local confirmation). The cloud can deny service by swapping
|
|
8
|
+
* rows, but cannot forge a verifying attestation.
|
|
9
|
+
*
|
|
10
|
+
* Rotation carry-forward (`pf`): an anchored daemon keeps proving its `dpk`
|
|
11
|
+
* with the pair secret derived from the seed it was installed under, so after
|
|
12
|
+
* a phrase rotation that proof no longer verifies under the new seed. The
|
|
13
|
+
* rotate flow — old seed still in hand — verifies the proof with the OLD pair
|
|
14
|
+
* secret and signs a new-key attestation that names the exact proof string
|
|
15
|
+
* it checked (`pf`). A browser that sees a failing proof accepts an
|
|
16
|
+
* attestation ONLY when it binds that same proof; any other attestation is
|
|
17
|
+
* refused (`lib/vault/daemon-attestation-cache.ts`).
|
|
18
|
+
*/
|
|
19
|
+
import {
|
|
20
|
+
decodeB64Json,
|
|
21
|
+
encodeB64Json,
|
|
22
|
+
labelledMessage,
|
|
23
|
+
} from "@openllmsh/sdk/tunnel";
|
|
24
|
+
import {
|
|
25
|
+
rawFromSpkiB64,
|
|
26
|
+
signDeviceGrant,
|
|
27
|
+
verifyDeviceGrant,
|
|
28
|
+
} from "./device-access";
|
|
29
|
+
|
|
30
|
+
export const DAEMON_ATTEST_LABEL = "openllm-daemon-attest-v1";
|
|
31
|
+
export const DAEMON_ATTEST_VERSION = 1 as const;
|
|
32
|
+
/** Soft wire cap — matches the design's ≤ 1024 chars envelope. */
|
|
33
|
+
export const DAEMON_ATTEST_B64_MAX = 1024;
|
|
34
|
+
|
|
35
|
+
export type TDaemonAttestationFields = {
|
|
36
|
+
readonly v: 1;
|
|
37
|
+
readonly user_id: string;
|
|
38
|
+
/** API key id this dpk is bound to — closes cross-key attestation swaps. */
|
|
39
|
+
readonly key_id: string;
|
|
40
|
+
/** Daemon X25519 SPKI, base64. */
|
|
41
|
+
readonly dpk: string;
|
|
42
|
+
/** `users.vault_version` (control version) at signing time. */
|
|
43
|
+
readonly cv: number;
|
|
44
|
+
/** ms since epoch at signing time. */
|
|
45
|
+
readonly ts: number;
|
|
46
|
+
/**
|
|
47
|
+
* Rotation carry-forward only: the daemon's dpk proof the signer verified
|
|
48
|
+
* under the PREVIOUS control seed. Absent on ordinary attestations.
|
|
49
|
+
*/
|
|
50
|
+
readonly pf?: string;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/** Max length of a carried proof (`pf`) — a base64url HMAC is 43 chars. */
|
|
54
|
+
const DAEMON_ATTEST_PF_MAX = 64;
|
|
55
|
+
|
|
56
|
+
export type TDaemonAttestationEnvelope = TDaemonAttestationFields & {
|
|
57
|
+
readonly sig: string;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Canonical signed bytes: `utf8(LABEL) || 0x00 || utf8(json)`.
|
|
62
|
+
* Fixed key order `v, user_id, key_id, dpk, cv, ts[, pf]` — `pf` only when
|
|
63
|
+
* present, so ordinary attestations keep their original bytes.
|
|
64
|
+
*/
|
|
65
|
+
export const buildDaemonAttestMessage = (
|
|
66
|
+
fields: TDaemonAttestationFields,
|
|
67
|
+
): Uint8Array =>
|
|
68
|
+
labelledMessage(
|
|
69
|
+
DAEMON_ATTEST_LABEL,
|
|
70
|
+
JSON.stringify({
|
|
71
|
+
v: fields.v,
|
|
72
|
+
user_id: fields.user_id,
|
|
73
|
+
key_id: fields.key_id,
|
|
74
|
+
dpk: fields.dpk,
|
|
75
|
+
cv: fields.cv,
|
|
76
|
+
ts: fields.ts,
|
|
77
|
+
...(fields.pf === undefined ? {} : { pf: fields.pf }),
|
|
78
|
+
}),
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
export const encodeDaemonAttestation = (
|
|
82
|
+
envelope: TDaemonAttestationEnvelope,
|
|
83
|
+
): string =>
|
|
84
|
+
encodeB64Json(
|
|
85
|
+
envelope,
|
|
86
|
+
DAEMON_ATTEST_B64_MAX,
|
|
87
|
+
"daemon attestation exceeds DAEMON_ATTEST_B64_MAX",
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
export const decodeDaemonAttestation = (
|
|
91
|
+
b64: string,
|
|
92
|
+
): TDaemonAttestationEnvelope | null => {
|
|
93
|
+
const o = decodeB64Json(b64, DAEMON_ATTEST_B64_MAX);
|
|
94
|
+
if (o === null) return null;
|
|
95
|
+
if (o.v !== DAEMON_ATTEST_VERSION) return null;
|
|
96
|
+
if (typeof o.user_id !== "string" || o.user_id.length === 0) return null;
|
|
97
|
+
if (typeof o.key_id !== "string" || o.key_id.length === 0) return null;
|
|
98
|
+
if (typeof o.dpk !== "string" || o.dpk.length === 0) return null;
|
|
99
|
+
if (typeof o.cv !== "number" || !Number.isInteger(o.cv) || o.cv < 1) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
if (
|
|
103
|
+
typeof o.ts !== "number" ||
|
|
104
|
+
!Number.isFinite(o.ts) ||
|
|
105
|
+
!Number.isInteger(o.ts) ||
|
|
106
|
+
o.ts <= 0
|
|
107
|
+
) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
if (typeof o.sig !== "string" || o.sig.length === 0) return null;
|
|
111
|
+
if (
|
|
112
|
+
o.pf !== undefined &&
|
|
113
|
+
(typeof o.pf !== "string" ||
|
|
114
|
+
o.pf.length === 0 ||
|
|
115
|
+
o.pf.length > DAEMON_ATTEST_PF_MAX)
|
|
116
|
+
) {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
v: DAEMON_ATTEST_VERSION,
|
|
121
|
+
user_id: o.user_id,
|
|
122
|
+
key_id: o.key_id,
|
|
123
|
+
dpk: o.dpk,
|
|
124
|
+
cv: o.cv,
|
|
125
|
+
ts: o.ts,
|
|
126
|
+
...(typeof o.pf === "string" ? { pf: o.pf } : {}),
|
|
127
|
+
sig: o.sig,
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
export type TSignDaemonAttestationParams = {
|
|
132
|
+
readonly seed: Uint8Array;
|
|
133
|
+
readonly userId: string;
|
|
134
|
+
readonly keyId: string;
|
|
135
|
+
readonly dpk: string;
|
|
136
|
+
readonly cv: number;
|
|
137
|
+
readonly ts?: number;
|
|
138
|
+
/** Rotation carry-forward: the proof verified under the previous seed. */
|
|
139
|
+
readonly carriedProof?: string;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/** Sign a daemon attestation; returns the base64 envelope. */
|
|
143
|
+
export const signDaemonAttestation = async (
|
|
144
|
+
params: TSignDaemonAttestationParams,
|
|
145
|
+
): Promise<string> => {
|
|
146
|
+
if (params.userId.length === 0) {
|
|
147
|
+
throw new Error("daemon attestation requires a non-empty userId");
|
|
148
|
+
}
|
|
149
|
+
if (params.keyId.length === 0) {
|
|
150
|
+
throw new Error("daemon attestation requires a non-empty keyId");
|
|
151
|
+
}
|
|
152
|
+
if (params.dpk.length === 0) {
|
|
153
|
+
throw new Error("daemon attestation requires a non-empty dpk");
|
|
154
|
+
}
|
|
155
|
+
if (!Number.isInteger(params.cv) || params.cv < 1) {
|
|
156
|
+
throw new Error("daemon attestation requires cv >= 1");
|
|
157
|
+
}
|
|
158
|
+
if (
|
|
159
|
+
params.carriedProof !== undefined &&
|
|
160
|
+
(params.carriedProof.length === 0 ||
|
|
161
|
+
params.carriedProof.length > DAEMON_ATTEST_PF_MAX)
|
|
162
|
+
) {
|
|
163
|
+
throw new Error("daemon attestation carried proof is malformed");
|
|
164
|
+
}
|
|
165
|
+
const fields: TDaemonAttestationFields = {
|
|
166
|
+
v: 1,
|
|
167
|
+
user_id: params.userId,
|
|
168
|
+
key_id: params.keyId,
|
|
169
|
+
dpk: params.dpk,
|
|
170
|
+
cv: params.cv,
|
|
171
|
+
ts: params.ts ?? Date.now(),
|
|
172
|
+
...(params.carriedProof === undefined ? {} : { pf: params.carriedProof }),
|
|
173
|
+
};
|
|
174
|
+
const msg = buildDaemonAttestMessage(fields);
|
|
175
|
+
const sig = await signDeviceGrant(params.seed, msg);
|
|
176
|
+
return encodeDaemonAttestation({ ...fields, sig });
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
export type TVerifyDaemonAttestationParams = {
|
|
180
|
+
/** Browser's own derived control public key (SPKI base64). */
|
|
181
|
+
readonly controlPubkey: string;
|
|
182
|
+
readonly attestation: string;
|
|
183
|
+
/** Expected user id — must equal the envelope's. */
|
|
184
|
+
readonly userId: string;
|
|
185
|
+
/** Expected API key id — must equal the envelope's. */
|
|
186
|
+
readonly keyId: string;
|
|
187
|
+
/** Expected dpk — must equal the envelope's. */
|
|
188
|
+
readonly dpk: string;
|
|
189
|
+
/** Expected control/vault version — must equal the envelope's `cv`. */
|
|
190
|
+
readonly cv: number;
|
|
191
|
+
/**
|
|
192
|
+
* When set, the envelope MUST carry exactly this proof as `pf` (rotation
|
|
193
|
+
* carry-forward). When unset, `pf` is neither required nor checked.
|
|
194
|
+
*/
|
|
195
|
+
readonly carriedProof?: string;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Verify an attestation against the browser's own control public key.
|
|
200
|
+
* Never throws — returns false on any failure (malformed, wrong binding,
|
|
201
|
+
* bad signature).
|
|
202
|
+
*/
|
|
203
|
+
export const verifyDaemonAttestation = async (
|
|
204
|
+
params: TVerifyDaemonAttestationParams,
|
|
205
|
+
): Promise<boolean> => {
|
|
206
|
+
if (rawFromSpkiB64(params.controlPubkey) === null) return false;
|
|
207
|
+
const envelope = decodeDaemonAttestation(params.attestation);
|
|
208
|
+
if (envelope === null) return false;
|
|
209
|
+
if (envelope.user_id !== params.userId) return false;
|
|
210
|
+
if (envelope.key_id !== params.keyId) return false;
|
|
211
|
+
if (envelope.dpk !== params.dpk) return false;
|
|
212
|
+
if (envelope.cv !== params.cv) return false;
|
|
213
|
+
if (
|
|
214
|
+
params.carriedProof !== undefined &&
|
|
215
|
+
envelope.pf !== params.carriedProof
|
|
216
|
+
) {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
const msg = buildDaemonAttestMessage({
|
|
220
|
+
v: envelope.v,
|
|
221
|
+
user_id: envelope.user_id,
|
|
222
|
+
key_id: envelope.key_id,
|
|
223
|
+
dpk: envelope.dpk,
|
|
224
|
+
cv: envelope.cv,
|
|
225
|
+
ts: envelope.ts,
|
|
226
|
+
...(envelope.pf === undefined ? {} : { pf: envelope.pf }),
|
|
227
|
+
});
|
|
228
|
+
return verifyDeviceGrant(params.controlPubkey, msg, envelope.sig);
|
|
229
|
+
};
|
package/vault/derive.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { deriveWrapKey, requireKdfVersion } from "./argon2";
|
|
2
|
+
import {
|
|
3
|
+
aesGcmDecrypt,
|
|
4
|
+
aesGcmEncrypt,
|
|
5
|
+
fromB64,
|
|
6
|
+
randomBytes,
|
|
7
|
+
toB64,
|
|
8
|
+
zero,
|
|
9
|
+
} from "./crypto";
|
|
10
|
+
import { normalizePhrase } from "./phrase";
|
|
11
|
+
import { decodeKdfWrapped, encodeKdfWrapped } from "./wrapped";
|
|
12
|
+
|
|
13
|
+
const WRAP_NONCE_BYTES = 12;
|
|
14
|
+
const WRAP_SALT_BYTES = 16;
|
|
15
|
+
const DEK_BYTES = 32;
|
|
16
|
+
|
|
17
|
+
export type TInitialVault = {
|
|
18
|
+
readonly dek: Uint8Array;
|
|
19
|
+
readonly wrap_salt_b64: string;
|
|
20
|
+
/** `v<n>:<base64>` — see `./wrapped.ts`. Not plain base64. */
|
|
21
|
+
readonly wrapped_dek: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Build a fresh vault from a recovery phrase. Runs the full
|
|
26
|
+
* Argon2id + AES-GCM-wrap pipeline; intended for the vault
|
|
27
|
+
* dialogs' commit + reset-begin transitions. Caller keeps the returned
|
|
28
|
+
* `dek` in memory and POSTs the two base64 fields to
|
|
29
|
+
* `/api/vault/{commit,reset/begin}`.
|
|
30
|
+
*/
|
|
31
|
+
export const buildVault = async (phrase: string): Promise<TInitialVault> => {
|
|
32
|
+
const wrapSalt = randomBytes(WRAP_SALT_BYTES);
|
|
33
|
+
const dek = randomBytes(DEK_BYTES);
|
|
34
|
+
const wrapKey = await deriveWrapKey(normalizePhrase(phrase), wrapSalt);
|
|
35
|
+
try {
|
|
36
|
+
const nonce = randomBytes(WRAP_NONCE_BYTES);
|
|
37
|
+
const ciphertext = await aesGcmEncrypt(wrapKey, nonce, dek);
|
|
38
|
+
return {
|
|
39
|
+
dek,
|
|
40
|
+
wrap_salt_b64: toB64(wrapSalt),
|
|
41
|
+
wrapped_dek: encodeKdfWrapped(nonce, ciphertext),
|
|
42
|
+
};
|
|
43
|
+
} finally {
|
|
44
|
+
zero(wrapKey);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Stage 2 variant — called after `/api/vault/begin` has already
|
|
50
|
+
* persisted `wrap_salt`. We need the same `wrap_salt` (so wrap_key
|
|
51
|
+
* is stable across the two steps) but produce a fresh DEK and
|
|
52
|
+
* wrapped_dek for `/api/vault/commit`.
|
|
53
|
+
*/
|
|
54
|
+
export const buildVaultWithSalt = async (
|
|
55
|
+
phrase: string,
|
|
56
|
+
wrapSaltB64: string,
|
|
57
|
+
): Promise<{
|
|
58
|
+
readonly dek: Uint8Array;
|
|
59
|
+
readonly wrapped_dek: string;
|
|
60
|
+
}> => {
|
|
61
|
+
const wrapSalt = fromB64(wrapSaltB64);
|
|
62
|
+
const dek = randomBytes(DEK_BYTES);
|
|
63
|
+
const wrapKey = await deriveWrapKey(normalizePhrase(phrase), wrapSalt);
|
|
64
|
+
try {
|
|
65
|
+
const nonce = randomBytes(WRAP_NONCE_BYTES);
|
|
66
|
+
const ciphertext = await aesGcmEncrypt(wrapKey, nonce, dek);
|
|
67
|
+
return {
|
|
68
|
+
dek,
|
|
69
|
+
wrapped_dek: encodeKdfWrapped(nonce, ciphertext),
|
|
70
|
+
};
|
|
71
|
+
} finally {
|
|
72
|
+
zero(wrapKey);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Unlock an existing vault — used on every page that needs to add
|
|
78
|
+
* credentials or mint API keys. Returns the DEK in memory; the
|
|
79
|
+
* caller is responsible for `zero()`-ing it on logout / page unload.
|
|
80
|
+
*
|
|
81
|
+
* The KDF version comes from the BLOB, never from what this build considers
|
|
82
|
+
* current — deriving an old wrap with new parameters produces a key that
|
|
83
|
+
* fails the auth tag, which the UI can only report as "wrong phrase".
|
|
84
|
+
*/
|
|
85
|
+
export const unlockDek = async (
|
|
86
|
+
phrase: string,
|
|
87
|
+
wrapSaltB64: string,
|
|
88
|
+
wrappedDekB64: string,
|
|
89
|
+
): Promise<Uint8Array> => {
|
|
90
|
+
const wrapSalt = fromB64(wrapSaltB64);
|
|
91
|
+
const decoded = decodeKdfWrapped(wrappedDekB64, "wrapped_dek");
|
|
92
|
+
// Throws on a version this build doesn't know, BEFORE spending ~64 MiB and
|
|
93
|
+
// a second of Argon2id on parameters that cannot be right.
|
|
94
|
+
const version = requireKdfVersion(decoded.version);
|
|
95
|
+
const wrapKey = await deriveWrapKey(
|
|
96
|
+
normalizePhrase(phrase),
|
|
97
|
+
wrapSalt,
|
|
98
|
+
version,
|
|
99
|
+
);
|
|
100
|
+
try {
|
|
101
|
+
const dek = await aesGcmDecrypt(wrapKey, decoded.nonce, decoded.ciphertext);
|
|
102
|
+
// A wrong phrase fails the auth tag above, so reaching here with the wrong
|
|
103
|
+
// length means the blob itself is malformed. Saying so beats letting the
|
|
104
|
+
// first AES call downstream throw about a key size nobody chose.
|
|
105
|
+
if (dek.length !== DEK_BYTES) {
|
|
106
|
+
zero(dek);
|
|
107
|
+
throw new Error("wrapped_dek did not contain a 32-byte key");
|
|
108
|
+
}
|
|
109
|
+
return dek;
|
|
110
|
+
} finally {
|
|
111
|
+
zero(wrapKey);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ed25519 primitives for device grants: SPKI encoding plus sign / verify.
|
|
3
|
+
* The SIGNING seed is the account device-control key
|
|
4
|
+
* (`./device-control.ts`), derived from the recovery phrase — never from the
|
|
5
|
+
* DEK, which the cloud can unwrap from `dek_for_key` on any `sk-llm` request.
|
|
6
|
+
* Pure bytes-in/bytes-out — runs identically in the browser, Bun, and Node
|
|
7
|
+
* (mirrors `crypto.ts` style).
|
|
8
|
+
*
|
|
9
|
+
* `@noble/ed25519` v3 `*Async` functions use WebCrypto SHA-512
|
|
10
|
+
* automatically (`crypto.subtle.digest`) — no `hashes.sha512` wiring
|
|
11
|
+
* is needed as long as we stay on the async API.
|
|
12
|
+
*/
|
|
13
|
+
import { getPublicKeyAsync, signAsync, verifyAsync } from "@noble/ed25519";
|
|
14
|
+
import { fromB64, toB64 } from "./crypto";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* DER SPKI prefix for an Ed25519 public key
|
|
18
|
+
* (SEQUENCE > AlgorithmIdentifier(id-Ed25519) > BIT STRING header).
|
|
19
|
+
*/
|
|
20
|
+
export const ED25519_SPKI_PREFIX: Uint8Array = new Uint8Array([
|
|
21
|
+
0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x03, 0x21, 0x00,
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
const ED25519_RAW_KEY_BYTES = 32;
|
|
25
|
+
const ED25519_SIGNATURE_BYTES = 64;
|
|
26
|
+
const ED25519_SPKI_B64_LENGTH = 60;
|
|
27
|
+
const ED25519_SIGNATURE_B64_LENGTH = 88;
|
|
28
|
+
|
|
29
|
+
/** Ed25519 public key as base64(SPKI DER) — importable by node:crypto. */
|
|
30
|
+
export const deviceAccessPublicKeySpkiB64 = async (
|
|
31
|
+
seed: Uint8Array,
|
|
32
|
+
): Promise<string> => {
|
|
33
|
+
const raw = await getPublicKeyAsync(seed);
|
|
34
|
+
const spki = new Uint8Array(ED25519_SPKI_PREFIX.length + raw.length);
|
|
35
|
+
spki.set(ED25519_SPKI_PREFIX, 0);
|
|
36
|
+
spki.set(raw, ED25519_SPKI_PREFIX.length);
|
|
37
|
+
return toB64(spki);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/** Extract the 32-byte raw key from base64(SPKI DER); null if malformed. */
|
|
41
|
+
export const rawFromSpkiB64 = (spkiB64: string): Uint8Array | null => {
|
|
42
|
+
if (spkiB64.length > ED25519_SPKI_B64_LENGTH) return null;
|
|
43
|
+
let spki: Uint8Array;
|
|
44
|
+
try {
|
|
45
|
+
spki = fromB64(spkiB64);
|
|
46
|
+
} catch {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
if (spki.length !== ED25519_SPKI_PREFIX.length + ED25519_RAW_KEY_BYTES) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
for (let i = 0; i < ED25519_SPKI_PREFIX.length; i++) {
|
|
53
|
+
if (spki[i] !== ED25519_SPKI_PREFIX[i]) return null;
|
|
54
|
+
}
|
|
55
|
+
return spki.slice(ED25519_SPKI_PREFIX.length);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/** Sign canonical device-grant bytes; returns base64 signature. */
|
|
59
|
+
export const signDeviceGrant = async (
|
|
60
|
+
seed: Uint8Array,
|
|
61
|
+
message: Uint8Array,
|
|
62
|
+
): Promise<string> => {
|
|
63
|
+
return toB64(await signAsync(message, seed));
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Verify a device-grant signature against a base64(SPKI) public key.
|
|
68
|
+
* Returns false on ANY failure (malformed key, bad base64, noble
|
|
69
|
+
* throw) — never throws.
|
|
70
|
+
*/
|
|
71
|
+
export const verifyDeviceGrant = async (
|
|
72
|
+
pubSpkiB64: string,
|
|
73
|
+
message: Uint8Array,
|
|
74
|
+
sigB64: string,
|
|
75
|
+
): Promise<boolean> => {
|
|
76
|
+
const raw = rawFromSpkiB64(pubSpkiB64);
|
|
77
|
+
if (raw === null || sigB64.length > ED25519_SIGNATURE_B64_LENGTH) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
try {
|
|
81
|
+
const signature = fromB64(sigB64);
|
|
82
|
+
if (signature.length !== ED25519_SIGNATURE_BYTES) return false;
|
|
83
|
+
return await verifyAsync(signature, message, raw);
|
|
84
|
+
} catch {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
};
|