@learncard/types 5.20.0 → 5.22.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@learncard/types",
3
- "version": "5.20.0",
3
+ "version": "5.22.0",
4
4
  "description": "Shared types for learn card",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
package/src/auth.ts CHANGED
@@ -6,6 +6,8 @@
6
6
  * coupling consumers to any specific implementation.
7
7
  */
8
8
 
9
+ import { z } from 'zod';
10
+
9
11
  // ---------------------------------------------------------------------------
10
12
  // Auth Session Error
11
13
  // ---------------------------------------------------------------------------
@@ -101,6 +103,24 @@ export interface PhoneVerificationHandle {
101
103
  _internal?: unknown;
102
104
  }
103
105
 
106
+ /** Sign-in features available to the UI, independent of the provider name. */
107
+ export interface SignInCapabilities {
108
+ readonly emailLink: boolean;
109
+ /** Email OTP is verified by the app's server, then exchanged via customToken. */
110
+ readonly emailOtp: boolean;
111
+ readonly phoneOtp: boolean;
112
+ readonly google: boolean;
113
+ readonly apple: boolean;
114
+ readonly social: boolean;
115
+ readonly customToken: boolean;
116
+ readonly deleteAccount: boolean;
117
+ }
118
+
119
+ export interface SocialSignInOptions {
120
+ /** Restore a session using the existing re-authentication interaction. */
121
+ intent?: 'signIn' | 'reauthenticate';
122
+ }
123
+
104
124
  /**
105
125
  * Abstract sign-in adapter interface.
106
126
  *
@@ -118,6 +138,7 @@ export interface PhoneVerificationHandle {
118
138
  */
119
139
  export interface SignInAdapter {
120
140
  readonly providerType: AuthProviderType;
141
+ readonly capabilities: SignInCapabilities;
121
142
 
122
143
  // --- Auth state ---
123
144
 
@@ -140,6 +161,9 @@ export interface SignInAdapter {
140
161
 
141
162
  isEmailLink(link: string): boolean;
142
163
 
164
+ /** Authoritative provider check; isEmailLink remains a synchronous hint. */
165
+ validateEmailLink(link: string): Promise<boolean>;
166
+
143
167
  // --- Phone OTP ---
144
168
 
145
169
  /**
@@ -153,6 +177,17 @@ export interface SignInAdapter {
153
177
  */
154
178
  confirmPhoneOtp(handle: PhoneVerificationHandle, code: string | number): Promise<AuthUser>;
155
179
 
180
+ /** Preferred API: confirm the most recent request without exposing SDK state. */
181
+ confirmPhoneOtp(code: string | number): Promise<AuthUser>;
182
+
183
+ /** Fires when a code is ready for entry, on either platform. */
184
+ onPhoneCodeSent(callback: () => void): () => void;
185
+
186
+ /** Auto-retrieved code; call confirmPhoneOtp before starting key derivation. */
187
+ onPhoneVerificationCompleted(callback: (code: string | undefined) => void): () => void;
188
+
189
+ onPhoneVerificationFailed(callback: (error: unknown) => void): () => void;
190
+
156
191
  /**
157
192
  * Confirm a phone OTP using a native verificationId (Capacitor auto-verify
158
193
  * path). Falls back to `confirmPhoneOtp` when not implemented.
@@ -161,9 +196,9 @@ export interface SignInAdapter {
161
196
 
162
197
  // --- OAuth ---
163
198
 
164
- signInWithGoogle(): Promise<AuthUser>;
199
+ signInWithGoogle(options?: SocialSignInOptions): Promise<AuthUser>;
165
200
 
166
- signInWithApple(): Promise<AuthUser>;
201
+ signInWithApple(options?: SocialSignInOptions): Promise<AuthUser>;
167
202
 
168
203
  /** Check for a pending OAuth redirect result (e.g. Apple on web). */
169
204
  checkRedirectResult?(): Promise<AuthUser | null>;
@@ -179,6 +214,15 @@ export interface SignInAdapter {
179
214
 
180
215
  deleteAccount(): Promise<void>;
181
216
 
217
+ /** Update the signed-in user's display profile, when supported. */
218
+ updateProfile?(profile: {
219
+ displayName?: string | null;
220
+ photoUrl?: string | null;
221
+ }): Promise<void>;
222
+
223
+ /** Limit the auth session to this browser tab on a public computer. */
224
+ setSessionPersistence?(sessionOnly: boolean): Promise<void>;
225
+
182
226
  signOut(): Promise<void>;
183
227
 
184
228
  // --- Cleanup ---
@@ -200,6 +244,8 @@ export interface RecoveryMethodInfo {
200
244
  type: string;
201
245
  createdAt: Date;
202
246
  credentialId?: string;
247
+ shareVersion?: number;
248
+ confirmedAt?: Date;
203
249
  }
204
250
 
205
251
  /**
@@ -211,10 +257,43 @@ export interface RecoveryResult {
211
257
  did: string;
212
258
  }
213
259
 
260
+ export interface IdentityRecoverySession {
261
+ recoverySessionToken: string;
262
+ recoveryMethods: RecoveryMethodInfo[];
263
+ }
264
+
214
265
  // ---------------------------------------------------------------------------
215
266
  // Server Key Status
216
267
  // ---------------------------------------------------------------------------
217
268
 
269
+ export type SssActivationState = 'provisional' | 'active';
270
+
271
+ /** Optional PIN enrollment requires rotating the existing escrow share. */
272
+ export type EscrowEnrollmentOptions = { pin?: string };
273
+
274
+ /** Stable error-message contract shared by PIN recovery clients and servers. */
275
+ export const ESCROW_PIN_LOCKED_MESSAGE =
276
+ 'Too many incorrect PIN attempts. You can still recover by waiting.';
277
+ export const ESCROW_PIN_UNAVAILABLE_MESSAGE = 'PIN recovery is not available for this account.';
278
+ export const ESCROW_PIN_MISMATCH_PATTERN = /^Incorrect PIN\. (\d+) attempts left\.$/;
279
+ export const escrowPinMismatchMessage = (attemptsRemaining: number): string =>
280
+ `Incorrect PIN. ${attemptsRemaining} attempts left.`;
281
+
282
+ /** Public PIN availability and remaining lifetime attempts; never includes the verifier. */
283
+ export const EscrowPinStatusValidator = z.object({
284
+ state: z.enum(['none', 'enabled', 'locked', 'stale']),
285
+ enabled: z.boolean(),
286
+ attemptsRemaining: z.number().int().nonnegative(),
287
+ salt: z.string().optional(),
288
+ });
289
+ export type EscrowPinStatus = z.infer<typeof EscrowPinStatusValidator>;
290
+
291
+ /** Enrollment details for PIN-aware strategies. Legacy strategies may still return a string. */
292
+ export interface EscrowEnrollmentState {
293
+ state: 'enrolled' | 'not-enrolled' | 'opted-out' | 'disabled';
294
+ escrowPin?: EscrowPinStatus;
295
+ }
296
+
218
297
  /**
219
298
  * Server key status returned by the strategy's fetchServerKeyStatus.
220
299
  * The strategy owns the server shape — different strategies may
@@ -228,8 +307,14 @@ export interface ServerKeyStatus {
228
307
  authShare: string | null;
229
308
  shareVersion: number | null;
230
309
  maskedRecoveryEmail?: string | null;
310
+ escrowOptedOut?: boolean;
311
+ escrowPin?: EscrowPinStatus;
312
+ sssActivationState?: SssActivationState | null;
231
313
  }
232
314
 
315
+ /** Signs a DID-Auth VP. A supplied challenge must be embedded as the VP nonce. */
316
+ export type DidAuthVpSigner = (privateKey: string, challenge?: string) => Promise<string>;
317
+
233
318
  // ---------------------------------------------------------------------------
234
319
  // Key Derivation Capabilities
235
320
  // ---------------------------------------------------------------------------
@@ -306,6 +391,7 @@ export interface KeyDerivationStrategy<
306
391
  TRecoveryInput = unknown,
307
392
  TRecoverySetupInput = unknown,
308
393
  TRecoverySetupResult = unknown,
394
+ TRecoveryConfirmationInput = unknown,
309
395
  > {
310
396
  readonly name: string;
311
397
 
@@ -323,8 +409,8 @@ export interface KeyDerivationStrategy<
323
409
  /** Store a local key component */
324
410
  storeLocalKey(key: string): Promise<void>;
325
411
 
326
- /** Clear all local key data */
327
- clearLocalKeys(): Promise<void>;
412
+ /** Clear local key data; automatic stale-key cleanup may retain unresolved writes. */
413
+ clearLocalKeys(options?: { preservePending?: boolean }): Promise<void>;
328
414
 
329
415
  /** Split a private key into shares/components */
330
416
  splitKey(privateKey: string): Promise<{ localKey: string; remoteKey: string }>;
@@ -340,19 +426,162 @@ export interface KeyDerivationStrategy<
340
426
  didFromPrivateKey: (pk: string) => Promise<string>
341
427
  ): Promise<boolean>;
342
428
 
429
+ /**
430
+ * Atomically split and persist a private key's local and remote components.
431
+ * Strategies that implement this use it for initial setup and rotations so
432
+ * callers never have to coordinate device/server writes themselves.
433
+ */
434
+ atomicUpdateShares?(params: {
435
+ token: string;
436
+ providerType: AuthProviderType;
437
+ privateKey: string;
438
+ did: string;
439
+ signDidAuthVp?: DidAuthVpSigner;
440
+ }): Promise<void>;
441
+
442
+ /**
443
+ * Repair local/server share-version skew after an ambiguous write. Returns
444
+ * the recovered key when reconciliation was needed, otherwise null.
445
+ */
446
+ reconcileShares?(params: {
447
+ token: string;
448
+ providerType: AuthProviderType;
449
+ expectedDid: string;
450
+ didFromPrivateKey: (privateKey: string) => Promise<string>;
451
+ signDidAuthVp?: DidAuthVpSigner;
452
+ }): Promise<RecoveryResult | null>;
453
+
454
+ /** Obtain a short-lived, single-use challenged DID-Auth VP for a write. */
455
+ getFreshDidAuthVp?(
456
+ privateKey: string,
457
+ did: string,
458
+ signDidAuthVp: DidAuthVpSigner
459
+ ): Promise<string>;
460
+
343
461
  // --- Server communication ---
344
462
 
345
463
  /** Fetch the server-side key status for the authenticated user */
346
464
  fetchServerKeyStatus(token: string, providerType: AuthProviderType): Promise<ServerKeyStatus>;
347
465
 
348
466
  /** Store the remote key component on the server */
349
- storeAuthShare(token: string, providerType: AuthProviderType, remoteKey: string, did: string, didAuthVp?: string): Promise<void>;
467
+ storeAuthShare(
468
+ token: string,
469
+ providerType: AuthProviderType,
470
+ remoteKey: string,
471
+ did: string,
472
+ didAuthVp?: string
473
+ ): Promise<void>;
350
474
 
351
475
  /** Mark migration complete on the server (optional — only needed for migration-capable strategies) */
352
476
  markMigrated?(token: string, providerType: AuthProviderType, didAuthVp?: string): Promise<void>;
353
477
 
478
+ /** Commit a provisioned key after the server verifies recovery enrollment. */
479
+ activate?(token: string, providerType: AuthProviderType, didAuthVp?: string): Promise<void>;
480
+
354
481
  // --- Recovery ---
355
482
 
483
+ /** Read the current automatic recovery enrollment status. */
484
+ getEscrowEnrollmentState?(params: {
485
+ token: string;
486
+ providerType: AuthProviderType;
487
+ }): Promise<EscrowEnrollmentState['state'] | EscrowEnrollmentState>;
488
+
489
+ /** Opt out with an owner proof; requires another confirmed recovery method. */
490
+ disableEscrowRecovery?(params: {
491
+ token: string;
492
+ providerType: AuthProviderType;
493
+ privateKey: string;
494
+ signDidAuthVp: DidAuthVpSigner;
495
+ }): Promise<void>;
496
+
497
+ /** Opt back in and enroll automatic recovery material. */
498
+ enableEscrowRecovery?(params: {
499
+ token: string;
500
+ providerType: AuthProviderType;
501
+ privateKey: string;
502
+ signDidAuthVp: DidAuthVpSigner;
503
+ options?: EscrowEnrollmentOptions;
504
+ }): Promise<
505
+ | { enrolled: false; reason: 'disabled' | 'opted-out' }
506
+ | { enrolled: true; changed: false }
507
+ | { enrolled: true; changed: true; shareVersion: number }
508
+ >;
509
+
510
+ /** Repair escrow enrollment, rotating shares only when no current confirmed enrollment exists. */
511
+ ensureEscrowEnrollment?(params: {
512
+ token: string;
513
+ providerType: AuthProviderType;
514
+ privateKey: string;
515
+ signDidAuthVp: DidAuthVpSigner;
516
+ options?: EscrowEnrollmentOptions;
517
+ }): Promise<
518
+ | { enrolled: false; reason: 'disabled' | 'opted-out' }
519
+ | { enrolled: true; changed: false }
520
+ | { enrolled: true; changed: true; shareVersion: number }
521
+ >;
522
+
523
+ /** Set or change a PIN by rotating escrow material with an owner proof. */
524
+ setEscrowPin?(params: {
525
+ token: string;
526
+ providerType: AuthProviderType;
527
+ privateKey: string;
528
+ signDidAuthVp: DidAuthVpSigner;
529
+ pin: string;
530
+ }): Promise<void>;
531
+
532
+ /** Remove a PIN by rotating escrow material with an owner proof. */
533
+ clearEscrowPin?(params: {
534
+ token: string;
535
+ providerType: AuthProviderType;
536
+ privateKey: string;
537
+ signDidAuthVp: DidAuthVpSigner;
538
+ }): Promise<void>;
539
+
540
+ /** Start an escrow hold. Securely persist the returned secrets; null means an existing hold. */
541
+ startEscrowRecovery?(params: {
542
+ token?: string;
543
+ providerType?: AuthProviderType;
544
+ recoverySessionToken?: string;
545
+ tenantId?: string;
546
+ options?: { releasePolicy?: 'hold' | 'pin'; restart?: boolean };
547
+ }): Promise<{
548
+ holdId: string;
549
+ status: 'pending' | 'cancelled' | 'completed' | 'expired';
550
+ requestedAt: string;
551
+ releaseAfter: string;
552
+ cancelledAt?: string;
553
+ completedAt?: string;
554
+ resumeToken: string | null;
555
+ clientEphemeralPrivateKey: string;
556
+ pinSalt?: string;
557
+ /** Absent on legacy hold-only strategies. */
558
+ releasePolicy?: 'hold' | 'pin';
559
+ }>;
560
+
561
+ /** Read a hold using its resume proof or the active device's provider session. */
562
+ getEscrowRecoveryStatus?(
563
+ params:
564
+ | { holdId: string; resumeToken: string }
565
+ | { token: string; providerType: AuthProviderType }
566
+ ): Promise<{
567
+ holdId: string;
568
+ status: 'pending' | 'cancelled' | 'completed' | 'expired';
569
+ requestedAt: string;
570
+ releaseAfter: string;
571
+ /** Absent on legacy hold-only strategies. */
572
+ releasePolicy?: 'hold' | 'pin';
573
+ cancelledAt?: string;
574
+ completedAt?: string;
575
+ } | null>;
576
+
577
+ /** Cancel a pending hold with a fresh, owner-signed DID challenge. */
578
+ cancelEscrowRecovery?(params: {
579
+ token: string;
580
+ providerType: AuthProviderType;
581
+ privateKey: string;
582
+ signDidAuthVp: DidAuthVpSigner;
583
+ }): Promise<{ cancelled: boolean }>;
584
+
356
585
  /** Execute a recovery flow and return the recovered private key + DID */
357
586
  executeRecovery(params: {
358
587
  token: string;
@@ -360,6 +589,8 @@ export interface KeyDerivationStrategy<
360
589
  input: TRecoveryInput;
361
590
  /** Optional: validate the reconstructed key's DID before rotating shares */
362
591
  didFromPrivateKey?: (privateKey: string) => Promise<string>;
592
+ /** Optional: sign the fresh challenge required to persist rotated shares */
593
+ signDidAuthVp?: DidAuthVpSigner;
363
594
  }): Promise<RecoveryResult>;
364
595
 
365
596
  /** Set up a new recovery method */
@@ -370,11 +601,51 @@ export interface KeyDerivationStrategy<
370
601
  input: TRecoverySetupInput;
371
602
  authUser?: AuthUser;
372
603
  /** Optional: sign a DID-Auth VP JWT for server write operations */
373
- signDidAuthVp?: (privateKey: string) => Promise<string>;
604
+ signDidAuthVp?: DidAuthVpSigner;
374
605
  }): Promise<TRecoverySetupResult>;
375
606
 
607
+ /** Confirm a pending method after the strategy verifies proof of receipt locally. */
608
+ confirmRecoveryMethod?(params: {
609
+ token: string;
610
+ providerType: AuthProviderType;
611
+ privateKey: string;
612
+ input: TRecoveryConfirmationInput;
613
+ signDidAuthVp?: DidAuthVpSigner;
614
+ }): Promise<void>;
615
+
376
616
  /** Get configured recovery methods for the authenticated user */
377
- getAvailableRecoveryMethods?(token: string, providerType: AuthProviderType): Promise<RecoveryMethodInfo[]>;
617
+ getAvailableRecoveryMethods?(
618
+ token: string,
619
+ providerType: AuthProviderType
620
+ ): Promise<RecoveryMethodInfo[]>;
621
+
622
+ // --- Lost login identity recovery ---
623
+
624
+ /** Send an OTP to the verified recovery email without requiring provider auth. */
625
+ startIdentityRecovery?(email: string): Promise<void>;
626
+
627
+ /** Verify the OTP and receive a one-use, recovery-scoped session. */
628
+ verifyIdentityRecovery?(email: string, code: string): Promise<IdentityRecoverySession>;
629
+
630
+ /** Reconstruct and hard-validate the key before the replacement login is bound. */
631
+ prepareIdentityRecovery?(params: {
632
+ recoverySessionToken: string;
633
+ input: TRecoveryInput;
634
+ didFromPrivateKey: (privateKey: string) => Promise<string>;
635
+ }): Promise<RecoveryResult>;
636
+
637
+ /** Whether reconstructed identity recovery is waiting for a replacement login. */
638
+ hasPendingIdentityRecovery?(): boolean;
639
+
640
+ /** Discard any reconstructed identity recovery that has not been rebound. */
641
+ cancelIdentityRecovery?(): void;
642
+
643
+ /** Bind the current provider identity and commit a full share rotation. */
644
+ completeIdentityRecovery?(params: {
645
+ token: string;
646
+ providerType: AuthProviderType;
647
+ signDidAuthVp?: DidAuthVpSigner;
648
+ }): Promise<RecoveryResult>;
378
649
 
379
650
  // --- Contact method management ---
380
651
 
@@ -416,7 +687,8 @@ export interface KeyDerivationStrategy<
416
687
  token: string,
417
688
  providerType: AuthProviderType,
418
689
  privateKey: string,
419
- email: string
690
+ email: string,
691
+ didAuthVp?: string
420
692
  ): Promise<void>;
421
693
 
422
694
  // --- Share versioning ---
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod/v4';
2
2
 
3
- import { UnsignedVCValidator, VCValidator } from './vc';
3
+ import { CredentialStatusValidator, UnsignedVCValidator, VCValidator } from './vc';
4
4
  import { JWEValidator } from './crypto';
5
5
 
6
6
  /**
@@ -73,6 +73,40 @@ export type AllocateCredentialRefreshResult = z.infer<
73
73
  typeof AllocateCredentialRefreshResultValidator
74
74
  >;
75
75
 
76
+ // --- Managed issuance receipt (returned to the issuer after a refreshable send) ---
77
+
78
+ /**
79
+ * Issuance metadata returned to the authenticated issuer after a refreshable send
80
+ * (unified send with `refresh: true`, or `sendBoost` with `enableRefresh: true`).
81
+ *
82
+ * Populated from the actual signed version 1 credential — never from the template or
83
+ * allocation alone. This is issuance metadata only: it must not contain credential
84
+ * claims, subject bodies, plaintext VCs, or JWEs. The issuer retains its own
85
+ * template/claims alongside this receipt, and preserves the exact `credentialStatus`
86
+ * descriptor when publishing an update (no new allocation).
87
+ */
88
+ export const ManagedCredentialRefreshReceiptValidator = z
89
+ .object({
90
+ refreshId: z.string().min(1),
91
+ refreshService: ManagedCredentialRefreshServiceValidator,
92
+ credentialId: z.string().min(1),
93
+ issuerDid: z.string().min(1),
94
+ holderDid: z.string().min(1),
95
+ credentialStatus: CredentialStatusValidator.or(
96
+ CredentialStatusValidator.array()
97
+ ).optional(),
98
+ })
99
+ .strip();
100
+ export type ManagedCredentialRefreshReceipt = z.infer<
101
+ typeof ManagedCredentialRefreshReceiptValidator
102
+ >;
103
+
104
+ /** Allocation metadata for deferred Inbox issuance. No holder is invented before claim. */
105
+ export const InboxCredentialRefreshReceiptValidator = ManagedCredentialRefreshReceiptValidator.omit(
106
+ { holderDid: true }
107
+ ).extend({ holderDid: z.string().min(1).optional() });
108
+ export type InboxCredentialRefreshReceipt = z.infer<typeof InboxCredentialRefreshReceiptValidator>;
109
+
76
110
  // --- Publication ------------------------------------------------------------
77
111
 
78
112
  export const CredentialRefreshSigningModeValidator = z.enum(['issuer-signed', 'signing-authority']);
package/src/index.ts CHANGED
@@ -17,3 +17,4 @@ export * from './auth';
17
17
  export * from './bitstring-status-list';
18
18
  export * from './inAppMessages';
19
19
  export * from './credential-refresh';
20
+ export * from './share-links';