@openllmsh/sdk 0.0.0-stage → 2.10.12-alpha.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/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/phrase.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { generateMnemonic, validateMnemonic } from "@scure/bip39";
|
|
2
|
+
import { wordlist } from "@scure/bip39/wordlists/english.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Number of words in a recovery phrase. 128 bits of BIP39 entropy →
|
|
6
|
+
* 12 words. Single source of truth for UIs that render one cell/slot
|
|
7
|
+
* per word (e.g. the recovery-phrase grid).
|
|
8
|
+
*/
|
|
9
|
+
export const PHRASE_WORD_COUNT = 12;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* BIP39 12-word recovery phrase. 128 bits of entropy — sufficient for
|
|
13
|
+
* the AES-256 wrap (Argon2id stretches it further); ergonomic to
|
|
14
|
+
* write down and re-type.
|
|
15
|
+
*/
|
|
16
|
+
export const generatePhrase = (): string => {
|
|
17
|
+
return generateMnemonic(wordlist, 128);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Canonicalise user-typed / pasted phrase input before any crypto
|
|
22
|
+
* derivation. Trims leading/trailing whitespace, collapses internal
|
|
23
|
+
* whitespace (spaces, tabs, newlines from a multi-line paste) to a
|
|
24
|
+
* single space, and lowercases — BIP39 words are all lowercase, so
|
|
25
|
+
* a CapsLock or autocapitalisation slip shouldn't 401 the unlock.
|
|
26
|
+
*
|
|
27
|
+
* Called at the boundary in `unlockDek` and `buildVaultWithSalt`, so
|
|
28
|
+
* UI inputs don't need to pre-clean. Idempotent on already-canonical
|
|
29
|
+
* input (so `normalizePhrase(generatePhrase()) === generatePhrase()`).
|
|
30
|
+
*/
|
|
31
|
+
export const normalizePhrase = (input: string): string => {
|
|
32
|
+
return input.trim().replace(/\s+/g, " ").toLowerCase();
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const isValidPhrase = (phrase: string): boolean => {
|
|
36
|
+
return validateMnemonic(normalizePhrase(phrase), wordlist);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const phraseWords = (phrase: string): readonly string[] => {
|
|
40
|
+
return phrase
|
|
41
|
+
.trim()
|
|
42
|
+
.split(/\s+/)
|
|
43
|
+
.filter((w) => w.length > 0);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Parse a pasted recovery phrase into its words, tolerating the messy
|
|
48
|
+
* shapes people actually paste: numbered lists ("1. abandon", "1) abandon",
|
|
49
|
+
* "1 abandon", "1.abandon"), one-word-per-line dumps, tabs, and stray
|
|
50
|
+
* punctuation. Each token has any leading list-ordinal stripped, then every
|
|
51
|
+
* non-letter character removed, then is lowercased; purely-numeric tokens
|
|
52
|
+
* collapse to empty and drop out. BIP39 words are all lowercase letters, so
|
|
53
|
+
* this is lossless for a real phrase while pruning the numbering around it.
|
|
54
|
+
*
|
|
55
|
+
* Used by the recovery-phrase input grid so pasting a numbered backup into
|
|
56
|
+
* any cell yields the clean words. For the canonical single-string form fed
|
|
57
|
+
* to crypto, see {@link normalizePhrase}.
|
|
58
|
+
*/
|
|
59
|
+
export const parsePhraseWords = (text: string): readonly string[] => {
|
|
60
|
+
return text
|
|
61
|
+
.toLowerCase()
|
|
62
|
+
.split(/\s+/)
|
|
63
|
+
.map((token) => token.replace(/^\d+[.):\-\]]*/, "").replace(/[^a-z]/g, ""))
|
|
64
|
+
.filter((w) => w.length > 0);
|
|
65
|
+
};
|
package/vault/pin.ts
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CURRENT_KDF_VERSION,
|
|
3
|
+
deriveWrapKey,
|
|
4
|
+
requireKdfVersion,
|
|
5
|
+
} from "./argon2";
|
|
6
|
+
import {
|
|
7
|
+
aesGcmDecrypt,
|
|
8
|
+
aesGcmEncrypt,
|
|
9
|
+
fromB64,
|
|
10
|
+
randomBytes,
|
|
11
|
+
toB64,
|
|
12
|
+
zero,
|
|
13
|
+
} from "./crypto";
|
|
14
|
+
import { decodeKdfWrapped, encodeKdfWrapped } from "./wrapped";
|
|
15
|
+
|
|
16
|
+
const PIN_SALT_BYTES = 16;
|
|
17
|
+
const PIN_NONCE_BYTES = 12;
|
|
18
|
+
const DEK_BYTES = 32;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Client-only PIN layer that wraps the DEK for `localStorage` storage,
|
|
22
|
+
* so users don't have to retype their recovery phrase every tab. The
|
|
23
|
+
* server never sees the PIN or the PIN-wrapped blob — losing the PIN
|
|
24
|
+
* is recoverable by re-running `unlockDek` with the recovery phrase
|
|
25
|
+
* and calling `wrapDekWithPin` with a fresh PIN.
|
|
26
|
+
*
|
|
27
|
+
* Reuses the same Argon2id params as the phrase path (`./argon2.ts`), and
|
|
28
|
+
* carries the same version stamp in `wrapped_b64` (`./wrapped.ts`) for the
|
|
29
|
+
* same reason: PINs have a tiny keyspace (10K / 1M for 4 / 6 digits), so KDF
|
|
30
|
+
* cost is the only meaningful brute-force defense — and a KDF cost that can
|
|
31
|
+
* never be raised without destroying every blob is one that can never be
|
|
32
|
+
* raised at all.
|
|
33
|
+
*/
|
|
34
|
+
export type TPinWrappedDek = {
|
|
35
|
+
readonly pin_salt_b64: string;
|
|
36
|
+
/** `v<n>:<base64>` — see `./wrapped.ts`. Not plain base64. */
|
|
37
|
+
readonly wrapped_b64: string;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Strip everything that isn't an ASCII digit. Mirrors
|
|
42
|
+
* `normalizePhrase` discipline — UI inputs don't need to pre-clean.
|
|
43
|
+
*/
|
|
44
|
+
export const normalizePin = (input: string): string => {
|
|
45
|
+
return input.replace(/\D/g, "");
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const wrapDekWithPin = async (
|
|
49
|
+
dek: Uint8Array,
|
|
50
|
+
pin: string,
|
|
51
|
+
): Promise<TPinWrappedDek> => {
|
|
52
|
+
const pinSalt = randomBytes(PIN_SALT_BYTES);
|
|
53
|
+
const pinKey = await deriveWrapKey(normalizePin(pin), pinSalt);
|
|
54
|
+
try {
|
|
55
|
+
const nonce = randomBytes(PIN_NONCE_BYTES);
|
|
56
|
+
const ciphertext = await aesGcmEncrypt(pinKey, nonce, dek);
|
|
57
|
+
return {
|
|
58
|
+
pin_salt_b64: toB64(pinSalt),
|
|
59
|
+
wrapped_b64: encodeKdfWrapped(nonce, ciphertext),
|
|
60
|
+
};
|
|
61
|
+
} finally {
|
|
62
|
+
zero(pinKey);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export const unwrapDekWithPin = async (
|
|
67
|
+
blob: TPinWrappedDek,
|
|
68
|
+
pin: string,
|
|
69
|
+
): Promise<Uint8Array> => {
|
|
70
|
+
const pinSalt = fromB64(blob.pin_salt_b64);
|
|
71
|
+
const decoded = decodeKdfWrapped(blob.wrapped_b64, "pin blob");
|
|
72
|
+
const version = requireKdfVersion(decoded.version);
|
|
73
|
+
const pinKey = await deriveWrapKey(normalizePin(pin), pinSalt, version);
|
|
74
|
+
try {
|
|
75
|
+
const dek = await aesGcmDecrypt(pinKey, decoded.nonce, decoded.ciphertext);
|
|
76
|
+
if (dek.length !== DEK_BYTES) {
|
|
77
|
+
zero(dek);
|
|
78
|
+
throw new Error("pin blob did not contain a 32-byte key");
|
|
79
|
+
}
|
|
80
|
+
return dek;
|
|
81
|
+
} finally {
|
|
82
|
+
zero(pinKey);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* `true` when a blob is wrapped under KDF parameters older than the current
|
|
88
|
+
* ones — the caller holds a live DEK and can re-wrap it under the same PIN to
|
|
89
|
+
* upgrade the blob in place.
|
|
90
|
+
*
|
|
91
|
+
* Cheap to act on and worth acting on: unlike the server-side `wrapped_dek`,
|
|
92
|
+
* the PIN blob is entirely client-owned, so a raised KDF cost reaches an
|
|
93
|
+
* existing device on its next unlock instead of waiting for a rotation.
|
|
94
|
+
*/
|
|
95
|
+
export const pinBlobNeedsRewrap = (blob: TPinWrappedDek): boolean =>
|
|
96
|
+
decodeKdfWrapped(blob.wrapped_b64, "pin blob").version < CURRENT_KDF_VERSION;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The vault seed under a PIN — this browser's PIN unlock in the seed-only
|
|
100
|
+
* vault (docs/plans/seed-only-vault.md). Same Argon2id cost and version stamp
|
|
101
|
+
* as the DEK blob; the AAD keeps a seed blob and a DEK blob from ever being
|
|
102
|
+
* passed off as each other.
|
|
103
|
+
*/
|
|
104
|
+
export type TPinWrappedSeed = TPinWrappedDek;
|
|
105
|
+
|
|
106
|
+
const PIN_SEED_AAD = new TextEncoder().encode("openllm-vault-seed-pin-v1");
|
|
107
|
+
const SEED_BYTES = 16;
|
|
108
|
+
|
|
109
|
+
export const wrapSeedWithPin = async (
|
|
110
|
+
seed: Uint8Array,
|
|
111
|
+
pin: string,
|
|
112
|
+
): Promise<TPinWrappedSeed> => {
|
|
113
|
+
if (seed.length !== SEED_BYTES)
|
|
114
|
+
throw new Error("vault seed must be 16 bytes");
|
|
115
|
+
const pinSalt = randomBytes(PIN_SALT_BYTES);
|
|
116
|
+
const pinKey = await deriveWrapKey(normalizePin(pin), pinSalt);
|
|
117
|
+
try {
|
|
118
|
+
const nonce = randomBytes(PIN_NONCE_BYTES);
|
|
119
|
+
const ciphertext = await aesGcmEncrypt(pinKey, nonce, seed, PIN_SEED_AAD);
|
|
120
|
+
return {
|
|
121
|
+
pin_salt_b64: toB64(pinSalt),
|
|
122
|
+
wrapped_b64: encodeKdfWrapped(nonce, ciphertext),
|
|
123
|
+
};
|
|
124
|
+
} finally {
|
|
125
|
+
zero(pinKey);
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/** Throws on a wrong PIN (auth tag) or a blob that isn't a seed blob. */
|
|
130
|
+
export const unwrapSeedWithPin = async (
|
|
131
|
+
blob: TPinWrappedSeed,
|
|
132
|
+
pin: string,
|
|
133
|
+
): Promise<Uint8Array> => {
|
|
134
|
+
const pinSalt = fromB64(blob.pin_salt_b64);
|
|
135
|
+
const decoded = decodeKdfWrapped(blob.wrapped_b64, "pin seed blob");
|
|
136
|
+
const version = requireKdfVersion(decoded.version);
|
|
137
|
+
const pinKey = await deriveWrapKey(normalizePin(pin), pinSalt, version);
|
|
138
|
+
try {
|
|
139
|
+
const seed = await aesGcmDecrypt(
|
|
140
|
+
pinKey,
|
|
141
|
+
decoded.nonce,
|
|
142
|
+
decoded.ciphertext,
|
|
143
|
+
PIN_SEED_AAD,
|
|
144
|
+
);
|
|
145
|
+
if (seed.length !== SEED_BYTES) {
|
|
146
|
+
zero(seed);
|
|
147
|
+
throw new Error("pin seed blob did not contain a vault seed");
|
|
148
|
+
}
|
|
149
|
+
return seed;
|
|
150
|
+
} finally {
|
|
151
|
+
zero(pinKey);
|
|
152
|
+
}
|
|
153
|
+
};
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import {
|
|
2
|
+
aesGcmDecrypt,
|
|
3
|
+
aesGcmEncrypt,
|
|
4
|
+
fromB64,
|
|
5
|
+
hkdfSha256,
|
|
6
|
+
randomBytes,
|
|
7
|
+
toB64,
|
|
8
|
+
zero,
|
|
9
|
+
} from "./crypto";
|
|
10
|
+
import { generateX25519Keypair } from "./handoff";
|
|
11
|
+
|
|
12
|
+
const DOMAIN = "openllm-connector-seal-v1";
|
|
13
|
+
|
|
14
|
+
export type TConnectorSealBinding = {
|
|
15
|
+
readonly clientId: string;
|
|
16
|
+
readonly userId: string;
|
|
17
|
+
readonly grantId: string;
|
|
18
|
+
/** Recipient X25519 public key, base64 SPKI DER. */
|
|
19
|
+
readonly recipientPublicKeyB64: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type TConnectorSealed = {
|
|
23
|
+
readonly v: 1;
|
|
24
|
+
readonly epk: string;
|
|
25
|
+
readonly nonce: string;
|
|
26
|
+
readonly ct: string;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const aad = (binding: TConnectorSealBinding): Uint8Array =>
|
|
30
|
+
new TextEncoder().encode(
|
|
31
|
+
JSON.stringify([DOMAIN, binding.clientId, binding.userId, binding.grantId]),
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const decode = (value: unknown): Uint8Array => {
|
|
35
|
+
if (
|
|
36
|
+
typeof value !== "string" ||
|
|
37
|
+
!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(
|
|
38
|
+
value,
|
|
39
|
+
)
|
|
40
|
+
)
|
|
41
|
+
throw new Error("Invalid sealed envelope encoding");
|
|
42
|
+
const bytes = fromB64(value);
|
|
43
|
+
if (toB64(bytes) !== value)
|
|
44
|
+
throw new Error("Invalid sealed envelope encoding");
|
|
45
|
+
return bytes;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const deriveKey = async (
|
|
49
|
+
privateKey: CryptoKey,
|
|
50
|
+
peerB64: string,
|
|
51
|
+
epk: string,
|
|
52
|
+
recipient: string,
|
|
53
|
+
): Promise<Uint8Array> => {
|
|
54
|
+
const peer = await crypto.subtle.importKey(
|
|
55
|
+
"spki",
|
|
56
|
+
decode(peerB64) as BufferSource,
|
|
57
|
+
{ name: "X25519" },
|
|
58
|
+
false,
|
|
59
|
+
[],
|
|
60
|
+
);
|
|
61
|
+
const shared = new Uint8Array(
|
|
62
|
+
await crypto.subtle.deriveBits(
|
|
63
|
+
{ name: "X25519", public: peer },
|
|
64
|
+
privateKey,
|
|
65
|
+
256,
|
|
66
|
+
),
|
|
67
|
+
);
|
|
68
|
+
try {
|
|
69
|
+
// JSON array framing makes the public-key boundaries unambiguous.
|
|
70
|
+
return await hkdfSha256(
|
|
71
|
+
shared,
|
|
72
|
+
new Uint8Array(0),
|
|
73
|
+
JSON.stringify([DOMAIN, epk, recipient]),
|
|
74
|
+
32,
|
|
75
|
+
);
|
|
76
|
+
} finally {
|
|
77
|
+
zero(shared);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/** Caller retains ownership of plaintext; the temporary copy is wiped. */
|
|
82
|
+
export const sealToPublicKey = async (
|
|
83
|
+
binding: TConnectorSealBinding,
|
|
84
|
+
plaintext: Uint8Array,
|
|
85
|
+
): Promise<TConnectorSealed> => {
|
|
86
|
+
const eph = await generateX25519Keypair();
|
|
87
|
+
const key = await deriveKey(
|
|
88
|
+
eph.privateKey,
|
|
89
|
+
binding.recipientPublicKeyB64,
|
|
90
|
+
eph.publicKeyB64,
|
|
91
|
+
binding.recipientPublicKeyB64,
|
|
92
|
+
);
|
|
93
|
+
const copy = new Uint8Array(plaintext);
|
|
94
|
+
try {
|
|
95
|
+
const nonce = randomBytes(12);
|
|
96
|
+
return {
|
|
97
|
+
v: 1,
|
|
98
|
+
epk: eph.publicKeyB64,
|
|
99
|
+
nonce: toB64(nonce),
|
|
100
|
+
ct: toB64(await aesGcmEncrypt(key, nonce, copy, aad(binding))),
|
|
101
|
+
};
|
|
102
|
+
} finally {
|
|
103
|
+
zero(key);
|
|
104
|
+
zero(copy);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/** Caller owns the returned plaintext and must wipe it when finished. */
|
|
109
|
+
export const openSealed = async (
|
|
110
|
+
binding: TConnectorSealBinding,
|
|
111
|
+
sealed: TConnectorSealed,
|
|
112
|
+
privateKey: CryptoKey,
|
|
113
|
+
): Promise<Uint8Array> => {
|
|
114
|
+
if (typeof sealed !== "object" || sealed === null || sealed.v !== 1)
|
|
115
|
+
throw new Error("Invalid sealed envelope");
|
|
116
|
+
const nonce = decode(sealed.nonce);
|
|
117
|
+
const ct = decode(sealed.ct);
|
|
118
|
+
if (nonce.length !== 12 || ct.length < 16 || typeof sealed.epk !== "string")
|
|
119
|
+
throw new Error("Invalid sealed envelope");
|
|
120
|
+
const key = await deriveKey(
|
|
121
|
+
privateKey,
|
|
122
|
+
sealed.epk,
|
|
123
|
+
sealed.epk,
|
|
124
|
+
binding.recipientPublicKeyB64,
|
|
125
|
+
);
|
|
126
|
+
try {
|
|
127
|
+
return await aesGcmDecrypt(key, nonce, ct, aad(binding));
|
|
128
|
+
} finally {
|
|
129
|
+
zero(key);
|
|
130
|
+
}
|
|
131
|
+
};
|
package/vault/seed.ts
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import {
|
|
2
|
+
entropyToMnemonic,
|
|
3
|
+
mnemonicToEntropy,
|
|
4
|
+
validateMnemonic,
|
|
5
|
+
} from "@scure/bip39";
|
|
6
|
+
import { wordlist } from "@scure/bip39/wordlists/english.js";
|
|
7
|
+
import {
|
|
8
|
+
aesGcmDecrypt,
|
|
9
|
+
aesGcmEncrypt,
|
|
10
|
+
fromB64,
|
|
11
|
+
hkdfSha256,
|
|
12
|
+
randomBytes,
|
|
13
|
+
toB64,
|
|
14
|
+
toB64Url,
|
|
15
|
+
zero,
|
|
16
|
+
} from "./crypto";
|
|
17
|
+
import { normalizePhrase, PHRASE_WORD_COUNT } from "./phrase";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The vault seed — the ONE per-user vault secret (docs/plans/seed-only-vault.md).
|
|
21
|
+
*
|
|
22
|
+
* The seed is the 16 bytes of BIP39 entropy behind the 12-word recovery
|
|
23
|
+
* phrase. Everything else is derived from it in process, never stored for the
|
|
24
|
+
* account:
|
|
25
|
+
*
|
|
26
|
+
* - the vault key (DEK) — {@link deriveVaultDek};
|
|
27
|
+
* - the public vault key id the server checks a seed against —
|
|
28
|
+
* {@link deriveVaultKeyId};
|
|
29
|
+
* - the device-control seed (`./device-control.ts`).
|
|
30
|
+
*
|
|
31
|
+
* HKDF, not Argon2id: 128 bits of entropy can't be searched, and every input
|
|
32
|
+
* is fixed (constant salt and info), so no server-supplied value can steer the
|
|
33
|
+
* key — the same argument as `./device-control.ts`.
|
|
34
|
+
*
|
|
35
|
+
* The seed is stored only under an unlock method: a passkey's PRF output
|
|
36
|
+
* ({@link sealSeedWithPrf}, `user_passkeys.sealed_seed` + a local cache) or a
|
|
37
|
+
* PIN (`./pin.ts`, this browser only).
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
/** BIP39 entropy for a 12-word phrase. */
|
|
41
|
+
export const SEED_BYTES = 16;
|
|
42
|
+
|
|
43
|
+
/** Fixed public HKDF salt for every key derived from the seed. */
|
|
44
|
+
export const VAULT_SEED_HKDF_SALT = "openllm-vault-seed-salt-v1";
|
|
45
|
+
/** HKDF info for the vault key (DEK). */
|
|
46
|
+
export const VAULT_DEK_HKDF_INFO = "openllm-vault-dek-v1";
|
|
47
|
+
/** HKDF info for the public vault key id. */
|
|
48
|
+
export const VAULT_KEY_ID_HKDF_INFO = "openllm-vault-key-id-v1";
|
|
49
|
+
/** HKDF info for the key that seals the seed under a passkey's PRF output. */
|
|
50
|
+
export const PASSKEY_SEED_HKDF_INFO = "openllm-vault-passkey-seed-v1";
|
|
51
|
+
|
|
52
|
+
const DEK_BYTES = 32;
|
|
53
|
+
const KEY_ID_BYTES = 16;
|
|
54
|
+
const PRF_OUTPUT_BYTES = 32;
|
|
55
|
+
const PRF_SALT_BYTES = 32;
|
|
56
|
+
const NONCE_BYTES = 12;
|
|
57
|
+
|
|
58
|
+
const requireSeed = (seed: Uint8Array): void => {
|
|
59
|
+
if (seed.length !== SEED_BYTES) {
|
|
60
|
+
throw new Error(`vault seed must be ${SEED_BYTES} bytes`);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const saltBytes = (): Uint8Array =>
|
|
65
|
+
new TextEncoder().encode(VAULT_SEED_HKDF_SALT);
|
|
66
|
+
|
|
67
|
+
/** A fresh random seed, for a new vault or a rotation. */
|
|
68
|
+
export const newSeed = (): Uint8Array => randomBytes(SEED_BYTES);
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The seed behind a typed or pasted phrase (normalised first). Throws on an
|
|
72
|
+
* invalid mnemonic — check {@link isValidSeedPhrase} to report it nicely.
|
|
73
|
+
*/
|
|
74
|
+
export const seedFromPhrase = (phrase: string): Uint8Array => {
|
|
75
|
+
if (!isValidSeedPhrase(phrase)) throw new Error("invalid recovery phrase");
|
|
76
|
+
const seed = mnemonicToEntropy(normalizePhrase(phrase), wordlist);
|
|
77
|
+
requireSeed(seed);
|
|
78
|
+
return seed;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export const isValidSeedPhrase = (phrase: string): boolean => {
|
|
82
|
+
const normalized = normalizePhrase(phrase);
|
|
83
|
+
// A valid 24-word mnemonic is still not a 16-byte seed.
|
|
84
|
+
return (
|
|
85
|
+
normalized.split(" ").length === PHRASE_WORD_COUNT &&
|
|
86
|
+
validateMnemonic(normalized, wordlist)
|
|
87
|
+
);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/** The 12-word phrase for a seed — what the user writes down. */
|
|
91
|
+
export const seedToPhrase = (seed: Uint8Array): string => {
|
|
92
|
+
requireSeed(seed);
|
|
93
|
+
return entropyToMnemonic(seed, wordlist);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/** The vault key (DEK) for a seed. The caller zeroes it. */
|
|
97
|
+
export const deriveVaultDek = (seed: Uint8Array): Promise<Uint8Array> => {
|
|
98
|
+
requireSeed(seed);
|
|
99
|
+
return hkdfSha256(seed, saltBytes(), VAULT_DEK_HKDF_INFO, DEK_BYTES);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The public vault key id for a seed, base64url. Stored as
|
|
104
|
+
* `users.vault_key_id`: the server's check that a typed phrase or a handed-off
|
|
105
|
+
* seed belongs to this vault. Reveals nothing about the seed (HKDF output
|
|
106
|
+
* under a separate info).
|
|
107
|
+
*/
|
|
108
|
+
export const deriveVaultKeyId = async (seed: Uint8Array): Promise<string> => {
|
|
109
|
+
requireSeed(seed);
|
|
110
|
+
const id = await hkdfSha256(
|
|
111
|
+
seed,
|
|
112
|
+
saltBytes(),
|
|
113
|
+
VAULT_KEY_ID_HKDF_INFO,
|
|
114
|
+
KEY_ID_BYTES,
|
|
115
|
+
);
|
|
116
|
+
return toB64Url(id);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/** What a passkey-sealed seed is bound to. */
|
|
120
|
+
export type TSeedPasskeyBinding = {
|
|
121
|
+
/** base64url credential id, exactly as stored in `user_passkeys.id`. */
|
|
122
|
+
readonly credentialId: string;
|
|
123
|
+
/**
|
|
124
|
+
* The vault key id of the sealed seed. Changes exactly when the seed does
|
|
125
|
+
* (rotation, reset), so a version bump that keeps the seed (the one-time
|
|
126
|
+
* migration) never invalidates a passkey row.
|
|
127
|
+
*/
|
|
128
|
+
readonly vaultKeyId: string;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const bindingAad = (binding: TSeedPasskeyBinding): Uint8Array =>
|
|
132
|
+
new TextEncoder().encode(
|
|
133
|
+
`${PASSKEY_SEED_HKDF_INFO}|${binding.credentialId}|${binding.vaultKeyId}`,
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
const passkeySeedKey = (
|
|
137
|
+
prfOutput: Uint8Array,
|
|
138
|
+
prfSalt: Uint8Array,
|
|
139
|
+
): Promise<Uint8Array> => {
|
|
140
|
+
if (prfOutput.length !== PRF_OUTPUT_BYTES) {
|
|
141
|
+
throw new Error(`PRF output must be ${PRF_OUTPUT_BYTES} bytes`);
|
|
142
|
+
}
|
|
143
|
+
if (prfSalt.length !== PRF_SALT_BYTES) {
|
|
144
|
+
throw new Error(`PRF salt must be ${PRF_SALT_BYTES} bytes`);
|
|
145
|
+
}
|
|
146
|
+
return hkdfSha256(prfOutput, prfSalt, PASSKEY_SEED_HKDF_INFO, 32);
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Seal the seed under a passkey's PRF output: base64 `IV || ct+tag`. The PRF
|
|
151
|
+
* output never leaves the browser, so the server can store this but never
|
|
152
|
+
* open it. Never zeroes its inputs.
|
|
153
|
+
*/
|
|
154
|
+
export const sealSeedWithPrf = async (
|
|
155
|
+
seed: Uint8Array,
|
|
156
|
+
prfOutput: Uint8Array,
|
|
157
|
+
prfSalt: Uint8Array,
|
|
158
|
+
binding: TSeedPasskeyBinding,
|
|
159
|
+
): Promise<string> => {
|
|
160
|
+
requireSeed(seed);
|
|
161
|
+
const key = await passkeySeedKey(prfOutput, prfSalt);
|
|
162
|
+
try {
|
|
163
|
+
const nonce = randomBytes(NONCE_BYTES);
|
|
164
|
+
const ciphertext = await aesGcmEncrypt(
|
|
165
|
+
key,
|
|
166
|
+
nonce,
|
|
167
|
+
seed,
|
|
168
|
+
bindingAad(binding),
|
|
169
|
+
);
|
|
170
|
+
const out = new Uint8Array(nonce.length + ciphertext.length);
|
|
171
|
+
out.set(nonce, 0);
|
|
172
|
+
out.set(ciphertext, nonce.length);
|
|
173
|
+
return toB64(out);
|
|
174
|
+
} finally {
|
|
175
|
+
zero(key);
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Open a {@link sealSeedWithPrf} copy. Throws on a bad auth tag: another
|
|
181
|
+
* credential, another seed's key id, or a diverged PRF value.
|
|
182
|
+
*/
|
|
183
|
+
export const openSeedWithPrf = async (
|
|
184
|
+
sealedB64: string,
|
|
185
|
+
prfOutput: Uint8Array,
|
|
186
|
+
prfSalt: Uint8Array,
|
|
187
|
+
binding: TSeedPasskeyBinding,
|
|
188
|
+
): Promise<Uint8Array> => {
|
|
189
|
+
const sealed = fromB64(sealedB64);
|
|
190
|
+
if (sealed.length <= NONCE_BYTES) throw new Error("sealed seed is too short");
|
|
191
|
+
const key = await passkeySeedKey(prfOutput, prfSalt);
|
|
192
|
+
try {
|
|
193
|
+
const seed = await aesGcmDecrypt(
|
|
194
|
+
key,
|
|
195
|
+
sealed.subarray(0, NONCE_BYTES),
|
|
196
|
+
sealed.subarray(NONCE_BYTES),
|
|
197
|
+
bindingAad(binding),
|
|
198
|
+
);
|
|
199
|
+
if (seed.length !== SEED_BYTES) {
|
|
200
|
+
zero(seed);
|
|
201
|
+
throw new Error("sealed seed did not contain a vault seed");
|
|
202
|
+
}
|
|
203
|
+
return seed;
|
|
204
|
+
} finally {
|
|
205
|
+
zero(key);
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* The vault key tree (docs/plans/seed-only-vault.md §2a). Each branch key is
|
|
211
|
+
* HKDF from the vault root key (the DEK) under a fixed label; each leaf is
|
|
212
|
+
* HKDF from its branch under the item's stable id. A parent derives all its
|
|
213
|
+
* children, a child never its parent or siblings, and nothing is stored.
|
|
214
|
+
*
|
|
215
|
+
* A full-access holder of the root (the browser after unlock, every default
|
|
216
|
+
* API key) derives every branch, so by default every key reads what any other
|
|
217
|
+
* wrote — today's behaviour. Leaf ids must never depend on which key wrote the
|
|
218
|
+
* item (a memory's space is its scope, a chat's leaf is its session id).
|
|
219
|
+
*/
|
|
220
|
+
export const VAULT_BRANCHES = [
|
|
221
|
+
"credentials",
|
|
222
|
+
"memory",
|
|
223
|
+
"chats",
|
|
224
|
+
"apps",
|
|
225
|
+
"teams",
|
|
226
|
+
"keys",
|
|
227
|
+
] as const;
|
|
228
|
+
|
|
229
|
+
export type TVaultBranch = (typeof VAULT_BRANCHES)[number];
|
|
230
|
+
|
|
231
|
+
/** HKDF info prefix for a branch; the branch name follows. */
|
|
232
|
+
export const VAULT_BRANCH_HKDF_INFO = "openllm-vault-branch-v1";
|
|
233
|
+
/** HKDF info prefix for a leaf; the branch and the leaf id follow. */
|
|
234
|
+
export const VAULT_LEAF_HKDF_INFO = "openllm-vault-leaf-v1";
|
|
235
|
+
|
|
236
|
+
const requireKey = (key: Uint8Array, label: string): void => {
|
|
237
|
+
if (key.length !== DEK_BYTES) throw new Error(`${label} must be 32 bytes`);
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
/** A branch key from the vault root key (the DEK). The caller zeroes it. */
|
|
241
|
+
export const deriveBranchKey = async (
|
|
242
|
+
rootKey: Uint8Array,
|
|
243
|
+
branch: TVaultBranch,
|
|
244
|
+
): Promise<Uint8Array> => {
|
|
245
|
+
requireKey(rootKey, "vault root key");
|
|
246
|
+
return hkdfSha256(
|
|
247
|
+
rootKey,
|
|
248
|
+
saltBytes(),
|
|
249
|
+
JSON.stringify([VAULT_BRANCH_HKDF_INFO, branch]),
|
|
250
|
+
DEK_BYTES,
|
|
251
|
+
);
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* A leaf key from its branch key and the item's stable id (session id, memory
|
|
256
|
+
* space). JSON-encoded info, so no id can collide with another branch's.
|
|
257
|
+
* The caller zeroes it.
|
|
258
|
+
*/
|
|
259
|
+
export const deriveLeafKey = async (
|
|
260
|
+
branchKey: Uint8Array,
|
|
261
|
+
branch: TVaultBranch,
|
|
262
|
+
leafId: string,
|
|
263
|
+
): Promise<Uint8Array> => {
|
|
264
|
+
requireKey(branchKey, "branch key");
|
|
265
|
+
if (leafId.length === 0) throw new Error("leaf id must not be empty");
|
|
266
|
+
return hkdfSha256(
|
|
267
|
+
branchKey,
|
|
268
|
+
saltBytes(),
|
|
269
|
+
JSON.stringify([VAULT_LEAF_HKDF_INFO, branch, leafId]),
|
|
270
|
+
DEK_BYTES,
|
|
271
|
+
);
|
|
272
|
+
};
|