@oxy.so/contracts 1.2.0 → 1.3.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.
Files changed (53) hide show
  1. package/dist/cjs/.tsbuildinfo +1 -1
  2. package/dist/cjs/identity.js +3 -2
  3. package/dist/cjs/identityMove.js +111 -40
  4. package/dist/cjs/identityProof.js +164 -0
  5. package/dist/cjs/identityRecovery.js +51 -0
  6. package/dist/cjs/index.js +50 -21
  7. package/dist/cjs/inference/identifiers.js +3 -1
  8. package/dist/cjs/inference/providerConnection.js +1 -1
  9. package/dist/cjs/inference/request.js +15 -1
  10. package/dist/cjs/inference/streamEvents.js +24 -2
  11. package/dist/cjs/username.js +70 -2
  12. package/dist/cjs/webIdentityCarrier.js +112 -53
  13. package/dist/cjs/webauthn.js +14 -0
  14. package/dist/esm/.tsbuildinfo +1 -1
  15. package/dist/esm/identity.js +3 -2
  16. package/dist/esm/identityMove.js +105 -39
  17. package/dist/esm/identityProof.js +159 -0
  18. package/dist/esm/identityRecovery.js +48 -0
  19. package/dist/esm/index.js +11 -8
  20. package/dist/esm/inference/identifiers.js +2 -0
  21. package/dist/esm/inference/providerConnection.js +2 -2
  22. package/dist/esm/inference/request.js +14 -0
  23. package/dist/esm/inference/streamEvents.js +24 -2
  24. package/dist/esm/username.js +69 -1
  25. package/dist/esm/webIdentityCarrier.js +111 -52
  26. package/dist/esm/webauthn.js +14 -0
  27. package/dist/types/.tsbuildinfo +1 -1
  28. package/dist/types/accountGraph.d.ts +7 -7
  29. package/dist/types/agency.d.ts +24 -24
  30. package/dist/types/browserHub.d.ts +16 -16
  31. package/dist/types/deviceDirectory.d.ts +28 -28
  32. package/dist/types/externalIdentity.d.ts +7 -7
  33. package/dist/types/identity.d.ts +3 -2
  34. package/dist/types/identityMove.d.ts +119 -43
  35. package/dist/types/identityProof.d.ts +156 -0
  36. package/dist/types/identityRecovery.d.ts +246 -0
  37. package/dist/types/index.d.ts +13 -11
  38. package/dist/types/inference/identifiers.d.ts +2 -0
  39. package/dist/types/inference/providerConnection.d.ts +40 -40
  40. package/dist/types/inference/request.d.ts +84 -36
  41. package/dist/types/inference/streamEvents.d.ts +57 -1
  42. package/dist/types/keyRotation.d.ts +2 -2
  43. package/dist/types/oauth.d.ts +30 -30
  44. package/dist/types/sessionStatus.d.ts +6 -6
  45. package/dist/types/transparency.d.ts +10 -10
  46. package/dist/types/userResponse.d.ts +18 -18
  47. package/dist/types/username.d.ts +25 -2
  48. package/dist/types/webIdentityCarrier.d.ts +767 -159
  49. package/dist/types/webauthn.d.ts +208 -0
  50. package/package.json +1 -1
  51. package/dist/cjs/devicePairing.js +0 -138
  52. package/dist/esm/devicePairing.js +0 -135
  53. package/dist/types/devicePairing.d.ts +0 -130
@@ -106,6 +106,71 @@ export const USERNAME_MAX_LENGTH = 30;
106
106
  /** The 400 / inline-validation copy for every path that rejects a handle. */
107
107
  export const USERNAME_INVALID_MESSAGE = 'Username must be 3-30 characters, use only letters, numbers, hyphens and underscores, ' +
108
108
  'start and end with a letter or number, and never repeat a separator';
109
+ /**
110
+ * Exact-match handles nobody may newly claim, regardless of account kind.
111
+ *
112
+ * This is NOT a namespace tightening like the bot suffix — it is a list of
113
+ * names withheld from `users_lower_username_key` before anybody asks for
114
+ * them. Compared against the same normalization the unique index applies
115
+ * (`lower(btrim(username))`), so `Admin`, ` ADMIN ` and `admin` are the one
116
+ * name this list means.
117
+ *
118
+ * `oxy`, `mention`, `homiio`, `clarity`, `faircoin`, `astro` and `mercaria`
119
+ * are deliberately NOT here: each already has an `organization`/`project`
120
+ * row, so the unique index already refuses a second one, and `oxy` is the
121
+ * name `USERNAME_MIN_LENGTH` is pinned against in
122
+ * `__tests__/username.test.ts` — listing it would make
123
+ * `usernameSchema.safeParse('oxy')` fail and falsify that comment. A brand
124
+ * with no account of its own yet (measured 2026-09-18: Alia, Allo, TNP,
125
+ * Kaana, Bloom) has no row to protect it, so it is listed until one exists.
126
+ */
127
+ export const RESERVED_USERNAMES = new Set([
128
+ // Oxy product lines with no account row of their own yet.
129
+ 'alia',
130
+ 'allo',
131
+ 'tnp',
132
+ 'kaana',
133
+ 'bloom',
134
+ // System / role words a signup impersonating staff or Oxy itself would reach for.
135
+ 'admin',
136
+ 'administrator',
137
+ 'root',
138
+ 'superadmin',
139
+ 'super',
140
+ 'superuser',
141
+ 'support',
142
+ 'staff',
143
+ 'moderator',
144
+ 'mod',
145
+ 'security',
146
+ 'system',
147
+ 'official',
148
+ 'help',
149
+ 'noreply',
150
+ 'anonymous',
151
+ 'everyone',
152
+ 'owner',
153
+ ]);
154
+ /** The 400 / inline-validation copy for a handle on {@link RESERVED_USERNAMES}. */
155
+ export const RESERVED_USERNAME_MESSAGE = 'This username is reserved and cannot be registered';
156
+ /**
157
+ * {@link RESERVED_USERNAMES}, minus `alia`, checked against each `-`/`_`
158
+ * separated SEGMENT of a candidate rather than the whole string — so
159
+ * `official-oxy`, `super-admin` and `team-kaana` are refused the same as the
160
+ * bare words, without banning every word that merely CONTAINS one as a
161
+ * substring (`superman`, `modern`, `grassroot`, `homeowner` all stay legal:
162
+ * none of them separates the reserved word from the rest with `-` or `_`).
163
+ *
164
+ * `alia` is excluded because `alia-` is the live internal-cost-centre
165
+ * namespace: `alia-production-chat` is a minted account and
166
+ * `alia-research` / `alia-voice` / `alia-evaluations` are pinned as legal
167
+ * slugs in `__tests__/username.test.ts` and `internalCostCenterSpecs.test.ts`.
168
+ * Segment-matching `alia` would refuse all four. The bare word `alia` is
169
+ * still refused — {@link RESERVED_USERNAMES} above catches it exactly.
170
+ */
171
+ const RESERVED_USERNAME_SEGMENTS = new Set([...RESERVED_USERNAMES].filter((word) => word !== 'alia'));
172
+ /** The 400 / inline-validation copy for a handle that is only digits. */
173
+ export const NUMERIC_USERNAME_MESSAGE = 'Username cannot be only numbers';
109
174
  /**
110
175
  * Alphanumeric runs joined by single separators, as a SOURCE string.
111
176
  *
@@ -142,7 +207,10 @@ export const usernameSchema = z
142
207
  .trim()
143
208
  .min(USERNAME_MIN_LENGTH, USERNAME_INVALID_MESSAGE)
144
209
  .max(USERNAME_MAX_LENGTH, USERNAME_INVALID_MESSAGE)
145
- .regex(USERNAME_PATTERN, USERNAME_INVALID_MESSAGE);
210
+ .regex(USERNAME_PATTERN, USERNAME_INVALID_MESSAGE)
211
+ .refine((username) => !/^[0-9]+$/.test(username), NUMERIC_USERNAME_MESSAGE)
212
+ .refine((username) => !RESERVED_USERNAMES.has(username.toLowerCase()), RESERVED_USERNAME_MESSAGE)
213
+ .refine((username) => !username.split(/[-_]/).some((segment) => RESERVED_USERNAME_SEGMENTS.has(segment.toLowerCase())), RESERVED_USERNAME_MESSAGE);
146
214
  /**
147
215
  * Whether a candidate handle is storable — the boolean form, for input surfaces
148
216
  * that show a message as somebody types rather than throwing.
@@ -1,30 +1,32 @@
1
1
  /**
2
- * Web identity carrier contract — "one identity, two carriers".
2
+ * Web identity holder contract — the sealed envelope that lets a browser hold an
3
+ * account's self-custody root without Oxy ever being able to use it (ADR 0024).
3
4
  *
4
- * SINGLE SOURCE OF TRUTH for the sealed envelope that lets a browser carry an
5
- * account's self-custody identity without Oxy ever holding it
6
- * (`docs/superpowers/specs/2026-09-15-one-identity-two-carriers-design.md`).
5
+ * A root is a BIP-39 phrase (12–24 words) whose seed's first 32 bytes are the
6
+ * secp256k1 key — exactly the Commons derivation — or, for a few imported
7
+ * identities, a raw private key that never had a phrase. On the web it travels as:
7
8
  *
8
- * The identity is a BIP-39 mnemonic whose seed's first 32 bytes are the
9
- * secp256k1 key — exactly the Commons derivation — so a web identity and a
10
- * Commons identity are the same thing. On the web it travels as:
11
- *
12
- * entropy (16 bytes) ── XChaCha20-Poly1305 under a random DEK ──▶ sealedEntropy
13
- * DEK ── XChaCha20-Poly1305 under KEK_i ──▶ wraps[i]
9
+ * secret ── XChaCha20-Poly1305 under a random DEK ──▶ sealedSecret
10
+ * DEK ── XChaCha20-Poly1305 under KEK_i ──▶ wraps[i]
14
11
  * KEK_i = HKDF(PRF output of passkey i)
15
12
  *
16
13
  * The server stores the envelope and can open NONE of it: the PRF output never
17
- * leaves the user's authenticator, and the mnemonic is never uploaded. The AEAD
18
- * associated data binds every ciphertext to the identity's public key (and each
19
- * wrap to its credential), so a re-labelled or transplanted envelope fails to
20
- * open instead of decrypting into the wrong identity.
14
+ * leaves the user's authenticator, and the secret is never uploaded. The AEAD
15
+ * associated data binds the secret to the root's public key and kind, and each
16
+ * wrap to its credential and RP ID, so a re-labelled or transplanted envelope
17
+ * fails to open instead of decrypting into the wrong identity.
21
18
  *
22
- * Every hex field is lowercase-or-uppercase hex. Platform-agnostic — zod only,
23
- * ESM-safe (no `require()`).
19
+ * Platform-agnostic — zod only, ESM-safe (no `require()`).
24
20
  */
25
21
  import { z } from 'zod';
26
- /** The only envelope version. A scheme change is a new literal, never a mutation. */
27
- export const WEB_IDENTITY_ENVELOPE_VERSION = 1;
22
+ import { identityProofSchema } from './identityProof.js';
23
+ /** The envelope scheme. A scheme change is a new literal, never a mutation. */
24
+ export const WEB_IDENTITY_ENVELOPE_VERSION = 2;
25
+ /**
26
+ * What an envelope seals. A raw-key identity stays a raw-key identity:
27
+ * nothing ever derives or displays a phrase for it.
28
+ */
29
+ export const WEB_IDENTITY_SECRET_KINDS = ['mnemonic-entropy', 'raw-private-key'];
28
30
  const hex = (bytes, label) => z
29
31
  .string()
30
32
  .trim()
@@ -45,6 +47,13 @@ export const webauthnCredentialIdSchema = z
45
47
  .min(16)
46
48
  .max(1024)
47
49
  .regex(/^[A-Za-z0-9_-]+$/, 'credentialId must be base64url');
50
+ /** A WebAuthn RP ID: a bare registrable host name, lowercase. */
51
+ export const webauthnRpIdSchema = z
52
+ .string()
53
+ .trim()
54
+ .min(1)
55
+ .max(253)
56
+ .regex(/^(localhost|[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)+)$/, 'rpId must be a lowercase host name');
48
57
  /** One passkey's wrap of the envelope's data key. */
49
58
  export const webIdentityWrapSchema = z.object({
50
59
  credentialId: webauthnCredentialIdSchema,
@@ -53,6 +62,13 @@ export const webIdentityWrapSchema = z.object({
53
62
  /** The 32-byte DEK sealed under this passkey's KEK, with the 16-byte tag appended (48 bytes). */
54
63
  wrappedKey: hex(48, 'wrappedKey'),
55
64
  createdAt: z.string().datetime(),
65
+ /** The RP ID the passkey was created under, asserted explicitly by every later ceremony (ADR 0024 D2). */
66
+ rpId: webauthnRpIdSchema,
67
+ /**
68
+ * When this passkey's PRF output was shown to open the envelope. A wrap is a
69
+ * root HOLDER only once this is set; a login passkey never is by default.
70
+ */
71
+ verifiedAt: z.string().datetime().optional(),
56
72
  });
57
73
  /**
58
74
  * The sealed identity as it is stored (server copy and local copy alike).
@@ -60,15 +76,27 @@ export const webIdentityWrapSchema = z.object({
60
76
  * `wraps` holds one entry per passkey able to open it; at least one, and a
61
77
  * bounded number so an envelope cannot grow without limit.
62
78
  */
63
- export const webIdentityEnvelopeSchema = z.object({
79
+ export const webIdentityEnvelopeSchema = z
80
+ .object({
64
81
  version: z.literal(WEB_IDENTITY_ENVELOPE_VERSION),
65
82
  algorithm: z.literal('xchacha20poly1305'),
66
83
  publicKey: webIdentityPublicKeySchema,
67
- /** 24-byte nonce of the entropy seal. */
68
- entropyNonce: hex(24, 'entropyNonce'),
69
- /** The 16-byte BIP-39 entropy sealed under the DEK, tag appended (32 bytes). */
70
- sealedEntropy: hex(32, 'sealedEntropy'),
84
+ secretKind: z.enum(WEB_IDENTITY_SECRET_KINDS),
85
+ /** 24-byte nonce of the secret seal. */
86
+ secretNonce: hex(24, 'secretNonce'),
87
+ /**
88
+ * The sealed secret, tag appended: 16/20/24/28/32 bytes of BIP-39 entropy
89
+ * (12–24 words) or a 32-byte private key, plus 16.
90
+ */
91
+ sealedSecret: z
92
+ .string()
93
+ .trim()
94
+ .regex(/^(?:[0-9a-fA-F]{64}|[0-9a-fA-F]{72}|[0-9a-fA-F]{80}|[0-9a-fA-F]{88}|[0-9a-fA-F]{96})$/, 'sealedSecret has an unsupported length'),
71
95
  wraps: z.array(webIdentityWrapSchema).min(1).max(10),
96
+ })
97
+ .refine((envelope) => envelope.secretKind === 'mnemonic-entropy' || envelope.sealedSecret.length === 96, {
98
+ message: 'a raw private key seals to 48 bytes',
99
+ path: ['sealedSecret'],
72
100
  });
73
101
  /**
74
102
  * `PUT /identity/web-envelope` — store or replace the caller's envelope.
@@ -79,41 +107,72 @@ export const webIdentityEnvelopeSchema = z.object({
79
107
  export const webIdentityEnvelopeUploadSchema = z.object({
80
108
  envelope: webIdentityEnvelopeSchema,
81
109
  });
82
- /** `GET /identity/web-envelope` — the caller's envelope and its recovery-phrase state. */
110
+ /** A root holder as the status read reports it — metadata only, nothing that opens anything. */
111
+ export const webIdentityHolderSchema = z.object({
112
+ credentialId: webauthnCredentialIdSchema,
113
+ rpId: webauthnRpIdSchema,
114
+ verifiedAt: z.string().datetime().nullable(),
115
+ createdAt: z.string().datetime(),
116
+ });
117
+ /**
118
+ * `GET /identity/web-envelope` — the caller's envelope and the readiness facts
119
+ * ADR 0024 D5 keeps separate. A client decides what to show from these fields
120
+ * WITHOUT decrypting anything.
121
+ */
83
122
  export const webIdentityEnvelopeResponseSchema = z.object({
84
123
  envelope: webIdentityEnvelopeSchema.nullable(),
85
- /**
86
- * When the owner confirmed they wrote the recovery phrase down, or `null`.
87
- * Until then the identity must not be unlocked on a second device, nor used
88
- * for any operation that needs the key (design decision D2).
89
- */
124
+ /** The revision a write must name as `expectedRevision`; `0` when there is no envelope. */
125
+ revision: z.number().int().nonnegative(),
126
+ /** Whether the account has a linked root at all (it may live only in Commons). */
127
+ rootLinked: z.boolean(),
128
+ /** The web wraps, as metadata. */
129
+ holders: z.array(webIdentityHolderSchema),
130
+ /** When the owner confirmed the recovery material is written down, or `null`. */
90
131
  phraseConfirmedAt: z.string().datetime().nullable(),
132
+ /** When the recovery material was shown to re-derive this root, or `null`. */
133
+ recoveryVerifiedAt: z.string().datetime().nullable(),
91
134
  updatedAt: z.string().datetime().nullable(),
92
135
  });
136
+ /** A root proof, plus the envelope revision the write expects to replace. */
137
+ export const webIdentityEnvelopeProofFieldsSchema = z.object({
138
+ proof: identityProofSchema,
139
+ expectedRevision: z.number().int().nonnegative(),
140
+ });
93
141
  /**
94
- * `POST /identity/web-envelope/phrase-confirmed` and
95
- * `DELETE /identity/web-envelope` both prove control of the identity key, not
96
- * just a bearer: a stolen session must not be able to mark a phrase as saved or
97
- * destroy the web copy of someone's identity.
98
- *
99
- * The signed message is `JSON.stringify({ action, userId, timestamp })` — the
100
- * same scheme as `link_identity`.
142
+ * `POST /identity/web-envelope/phrase-confirmed`, `/recovery-verified` and
143
+ * `DELETE /identity/web-envelope` prove control of the root, not just a bearer.
101
144
  */
102
- export const webIdentityEnvelopeProofSchema = z.object({
103
- signature: z.string().trim().min(1).max(512),
104
- timestamp: z.number().int().positive(),
105
- });
106
- /** `PUT /identity/web-envelope` body: the envelope plus a `web_envelope_put` identity-key proof. */
107
- export const webIdentityEnvelopePutSchema = webIdentityEnvelopeUploadSchema.extend(webIdentityEnvelopeProofSchema.shape);
145
+ export const webIdentityEnvelopeActionSchema = webIdentityEnvelopeProofFieldsSchema.strict();
146
+ /** `PUT /identity/web-envelope` body. */
147
+ export const webIdentityEnvelopePutSchema = webIdentityEnvelopeUploadSchema.extend(webIdentityEnvelopeProofFieldsSchema.shape).strict();
108
148
  /**
109
- * `POST /identity/web-envelope/establish` body — create an account's FIRST
110
- * identity on the web: link the key and store its envelope in ONE transaction.
111
- *
112
- * Linking and storing as two calls would let a failure (or a closed tab) in
113
- * between leave the account bound to a key that nothing carries — an identity
114
- * lost at birth. `link` is a `link_identity` proof and the outer proof a
115
- * `web_envelope_put` proof, both signed by the envelope's own key.
149
+ * A WebAuthn assertion by one of the account's EXISTING passkeys whose
150
+ * `clientDataJSON.challenge` is the proof challenge — the fresh use of the
151
+ * existing factor a keyless account needs before its first root is linked.
116
152
  */
117
- export const webIdentityEnvelopeEstablishSchema = webIdentityEnvelopePutSchema.extend({
118
- link: webIdentityEnvelopeProofSchema,
119
- });
153
+ export const webauthnAssertionResponseSchema = z
154
+ .object({
155
+ id: webauthnCredentialIdSchema,
156
+ rawId: z.string().min(1).max(2048),
157
+ type: z.literal('public-key'),
158
+ response: z
159
+ .object({
160
+ clientDataJSON: z.string().min(1).max(8192),
161
+ authenticatorData: z.string().min(1).max(8192),
162
+ signature: z.string().min(1).max(2048),
163
+ userHandle: z.string().max(2048).optional(),
164
+ })
165
+ .passthrough(),
166
+ clientExtensionResults: z.record(z.string(), z.unknown()).optional(),
167
+ authenticatorAttachment: z.string().optional(),
168
+ })
169
+ .passthrough();
170
+ /**
171
+ * `POST /identity/web-envelope/establish` body — an account's FIRST root, linked
172
+ * and stored with its envelope in ONE transaction: one root proof
173
+ * (`web_envelope_establish`, digest of the envelope) plus a fresh `assertion` by
174
+ * an existing passkey over the same challenge.
175
+ */
176
+ export const webIdentityEnvelopeEstablishSchema = webIdentityEnvelopeUploadSchema
177
+ .extend({ proof: identityProofSchema, assertion: webauthnAssertionResponseSchema })
178
+ .strict();
@@ -11,6 +11,8 @@
11
11
  * create a second, drift-prone definition of a shape we do not own.
12
12
  */
13
13
  import { z } from 'zod';
14
+ import { identityProofSchema } from './identityProof.js';
15
+ import { webIdentityEnvelopeSchema } from './webIdentityCarrier.js';
14
16
  /**
15
17
  * Device-session options shared by every first-party sign-in body
16
18
  * (`deviceName`/`deviceFingerprint`). Mirrors what
@@ -56,6 +58,18 @@ export const webauthnLoginOptionsRequestSchema = z.object({
56
58
  */
57
59
  export const webauthnRegisterVerifyRequestSchema = z.object({
58
60
  username: z.string().trim().min(1).max(60).optional(),
61
+ /**
62
+ * Sign-up only (ADR 0024 D4): the account's root, created on the holder BEFORE
63
+ * this request — sealed under the passkey being registered — and a root proof
64
+ * (`enroll_identity`) whose challenge is the registration challenge. The
65
+ * account, passkey, root and envelope are then created in one transaction.
66
+ */
67
+ identity: z
68
+ .object({
69
+ envelope: webIdentityEnvelopeSchema,
70
+ proof: identityProofSchema,
71
+ })
72
+ .optional(),
59
73
  ...deviceSessionEnvelope,
60
74
  });
61
75
  /**