@learncard/sss-key-manager 0.2.3 → 0.2.5

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.
@@ -14,6 +14,15 @@
14
14
  */
15
15
  import type { SSSKeyDerivationStrategy, EscrowAttestationPolicy } from './types';
16
16
  import type { EmailRelayBranding } from './email-relay-crypto';
17
+ /**
18
+ * OFF by default. Every input to the uncarryable-PIN inference (recovery methods,
19
+ * share version, PIN state) comes from lca-api, so a compromised API could forge it
20
+ * for a healthy PIN account and have the client sign away the PIN path (Custard7's
21
+ * PR #1815 review). With it off the strategy returns `pin-reset-required` and the
22
+ * app asks the user to set a new PIN. Enabling it needs an independently trustworthy
23
+ * recovery signal and security sign-off: POST_RECOVERY_ESCROW_REPAIR_DESIGN.md, Q1.
24
+ */
25
+ export declare const DEFAULT_AUTO_CLEAR_UNCARRYABLE_PIN = false;
17
26
  export interface SSSStorageFunctions {
18
27
  storeDeviceShare: (share: string, id?: string) => Promise<void>;
19
28
  getDeviceShare: (id?: string) => Promise<string | null>;
@@ -31,6 +40,20 @@ export interface SSSStrategyConfig {
31
40
  };
32
41
  /** Report enrollment failures without logging recovery material. */
33
42
  onEscrowError?: (err: unknown) => void;
43
+ /**
44
+ * Nitro repair only: when the signed authority still has a PIN but lca-api holds
45
+ * no PIN to carry (an identity-recovery rebind deleted it), sign a PIN `clear`
46
+ * instead of a `carry` that lca-api would reject with PRECONDITION_FAILED.
47
+ * Defaults to {@link DEFAULT_AUTO_CLEAR_UNCARRYABLE_PIN} (off). Do not enable
48
+ * without security sign-off: POST_RECOVERY_ESCROW_REPAIR_DESIGN.md, open question Q1.
49
+ */
50
+ autoClearUncarryablePin?: boolean;
51
+ /**
52
+ * Called once after every committed automatic PIN clear
53
+ * ({@link SSSStrategyConfig.autoClearUncarryablePin}). Must not throw; defaults
54
+ * to a console warning. Never receives the DID, email or PIN material.
55
+ */
56
+ onEscrowPinAutoCleared?: (event: EscrowPinAutoClearedEvent) => void;
34
57
  /** Server URL for key share operations */
35
58
  serverUrl: string;
36
59
  /** Custom storage functions (defaults to IndexedDB) */
@@ -56,12 +79,24 @@ export interface SSSStrategyConfig {
56
79
  tenantId?: string;
57
80
  }
58
81
  export declare const SERVER_REQUEST_TIMEOUT_MS = 30000;
82
+ /** Telemetry for an automatic post-rebind PIN clear. Carries no DID, email or PIN data. */
83
+ export interface EscrowPinAutoClearedEvent {
84
+ type: 'escrow.pin.auto_cleared';
85
+ /** Share version the PIN-free enrollment was committed at. */
86
+ shareVersion: number;
87
+ /** Share version the signed authority was still active at (pre-rebind). */
88
+ authorityShareVersion: number | null;
89
+ }
59
90
  /** HTTP failure metadata without potentially sensitive server response text. */
60
91
  export declare class EscrowRequestError extends Error {
61
92
  readonly status: number;
62
93
  readonly reason?: "codeMismatch" | "codeExpired" | "codeLocked" | undefined;
63
94
  constructor(status: number, reason?: "codeMismatch" | "codeExpired" | "codeLocked" | undefined);
64
95
  }
96
+ /** Independently generated splits can reuse a share's final-byte x-coordinate. */
97
+ export declare class DuplicateShareCoordinateError extends Error {
98
+ constructor();
99
+ }
65
100
  /**
66
101
  * Signals that an identity-recovery attempt failed after the one-shot server
67
102
  * session was submitted and must be restarted with a new verification code.
@@ -1 +1 @@
1
- {"version":3,"file":"sss-strategy.d.ts","sourceRoot":"","sources":["../src/sss-strategy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,EACR,wBAAwB,EAaxB,uBAAuB,EAI1B,MAAM,SAAS,CAAC;AAoCjB,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAyC/D,MAAM,WAAW,mBAAmB;IAChC,gBAAgB,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAChE,cAAc,EAAE,CAAC,EAAE,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,IA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;AACH,eAAO,MAAM,6BAA6B,GACtC,SAAS,mBAAmB,EAC5B,WAAW,MAAM,GAAG,SAAS,EAC7B,cAAc,MAAM,OAAO,CAAC,oBAAoB,CAAC,EACjD,aAAa,MAAM,EACnB,mBAAmB,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,EACnD,mBAAkB,MAAM,IAAe,EACvC,aAAa,CAAC,QAAQ,EAAE,oBAAoB,KAAK,IAAI,EACrD,kBAAkB,CAAC,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,KAC9D,OAAO,CAAC;IAAE,QAAQ,EAAE,oBAAoB,CAAC;IAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAwGvE,CAAC;AAgJF;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,iBAAiB,GAAG,wBAAwB,CA4rFrF;AAED,YAAY,EAAE,wBAAwB,EAAE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@learncard/sss-key-manager",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "description": "Shamir Secret Sharing key manager for LearnCard - replaces Web3Auth SFA",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/sss-key-manager.esm.js",
@@ -39,7 +39,7 @@
39
39
  "vitest": "4.1.11"
40
40
  },
41
41
  "dependencies": {
42
- "@learncard/types": "5.23.2",
42
+ "@learncard/types": "5.24.0",
43
43
  "@noble/ed25519": "^2.0.0",
44
44
  "@peculiar/x509": "^2.1.0",
45
45
  "@scure/bip39": "^1.2.1",
@@ -210,33 +210,29 @@ describe('Critical: Key must NEVER be lost', () => {
210
210
  ).rejects.toThrow();
211
211
  });
212
212
 
213
- it('should handle various password strengths', async () => {
214
- const passwords = [
215
- 'short',
216
- 'medium-length-password',
217
- 'very-long-password-with-special-characters-!@#$%^&*()',
218
- '日本語パスワード', // Unicode
219
- '🔐🔑🔒', // Emoji
220
- ];
221
-
222
- for (const password of passwords) {
223
- const privateKey = await generateEd25519PrivateKey();
224
- const shares = await splitPrivateKey(privateKey);
225
- const encrypted = await encryptWithPassword(shares.recoveryShare, password);
213
+ it.each([
214
+ 'short',
215
+ 'medium-length-password',
216
+ 'very-long-password-with-special-characters-!@#$%^&*()',
217
+ '日本語パスワード', // Unicode
218
+ '🔐🔑🔒', // Emoji
219
+ ])('should round-trip a backup share with password %j', async password => {
220
+ const privateKey = await generateEd25519PrivateKey();
221
+ const shares = await splitPrivateKey(privateKey);
222
+ const encrypted = await encryptWithPassword(shares.recoveryShare, password);
226
223
 
227
- const decrypted = await decryptWithPassword(
228
- encrypted.ciphertext,
229
- encrypted.iv,
230
- encrypted.salt,
231
- password,
232
- encrypted.kdfParams
233
- );
224
+ const decrypted = await decryptWithPassword(
225
+ encrypted.ciphertext,
226
+ encrypted.iv,
227
+ encrypted.salt,
228
+ password,
229
+ encrypted.kdfParams
230
+ );
234
231
 
235
- const reconstructed = await reconstructFromShares([decrypted, shares.authShare]);
232
+ const reconstructed = await reconstructFromShares([decrypted, shares.authShare]);
236
233
 
237
- expect(reconstructed).toBe(privateKey);
238
- }
239
- }, 15_000);
234
+ expect(reconstructed).toBe(privateKey);
235
+ });
240
236
  });
241
237
 
242
238
  describe('Recovery phrase round-trip', () => {
package/src/index.ts CHANGED
@@ -173,11 +173,16 @@ export type {
173
173
 
174
174
  export {
175
175
  createSSSStrategy,
176
+ DuplicateShareCoordinateError,
176
177
  IdentityRecoverySessionConsumedError,
177
178
  EscrowRequestError,
178
179
  } from './sss-strategy';
179
180
 
180
- export type { SSSStorageFunctions, SSSStrategyConfig } from './sss-strategy';
181
+ export type {
182
+ EscrowPinAutoClearedEvent,
183
+ SSSStorageFunctions,
184
+ SSSStrategyConfig,
185
+ } from './sss-strategy';
181
186
 
182
187
  export {
183
188
  generateEphemeralKeypair,
@@ -3,6 +3,7 @@ import { afterEach, describe, expect, it, vi } from 'vitest';
3
3
  import { getPublicKeyAsync, verifyAsync } from '@noble/ed25519';
4
4
  import {
5
5
  createSSSStrategy,
6
+ formatVersionedEmailShare,
6
7
  type SSSStorageFunctions,
7
8
  type SSSStrategyConfig,
8
9
  } from './sss-strategy';
@@ -78,7 +79,8 @@ const frame = (value: Buffer): Buffer => {
78
79
  size.writeUInt32BE(value.length);
79
80
  return Buffer.concat([size, value]);
80
81
  };
81
- const response = (value: unknown): Response => new Response(JSON.stringify(value), { status: 200 });
82
+ const response = (value: unknown, status = 200): Response =>
83
+ new Response(JSON.stringify(value), { status });
82
84
 
83
85
  const setup = async (legacyEnrolled = false) => {
84
86
  const pins = [
@@ -191,6 +193,12 @@ const setup = async (legacyEnrolled = false) => {
191
193
  snapshotOnly?: boolean;
192
194
  legacySnapshot?: boolean;
193
195
  releaseDid?: string;
196
+ failEnrollment?: boolean;
197
+ missingPinMetadata?: boolean;
198
+ /** Server predating `priorEscrowEnrollment` on /keys/auth-share. */
199
+ oldServer?: boolean;
200
+ /** A compromised lca-api forging post-rebind status for a healthy account. */
201
+ forgedRebindStatus?: boolean;
194
202
  } = {};
195
203
  const signHold = async (record: EscrowHoldRecordLike): Promise<EscrowHoldRecordLike> => {
196
204
  const h = record.hold;
@@ -230,10 +238,21 @@ const setup = async (legacyEnrolled = false) => {
230
238
  enrolled = false;
231
239
  return response({ shareVersion: version, expectedShareVersionChecked: true });
232
240
  }
241
+ if (faults.forgedRebindStatus)
242
+ return response({
243
+ primaryDid: owner.intent.did,
244
+ shareVersion: version + 1,
245
+ authShare: { encryptedData: authShare, encryptedDek: '', iv: '' },
246
+ recoveryMethods: [],
247
+ priorEscrowEnrollment: true,
248
+ escrowPin: { state: 'none', enabled: false, attemptsRemaining: 0 },
249
+ });
233
250
  return response({
234
251
  primaryDid: owner.intent.did,
235
252
  shareVersion: version,
236
253
  authShare: { encryptedData: authShare, encryptedDek: '', iv: '' },
254
+ // Mirror lca-api: only confirmed methods are listed; the rebind's
255
+ // `pending` escrow method surfaces only as priorEscrowEnrollment.
237
256
  recoveryMethods: enrolled
238
257
  ? [
239
258
  {
@@ -243,6 +262,12 @@ const setup = async (legacyEnrolled = false) => {
243
262
  },
244
263
  ]
245
264
  : [],
265
+ ...(faults.oldServer
266
+ ? {}
267
+ : { priorEscrowEnrollment: !enrolled && !!faults.missingPinMetadata }),
268
+ ...(faults.missingPinMetadata
269
+ ? { escrowPin: { state: 'none', enabled: false, attemptsRemaining: 0 } }
270
+ : {}),
246
271
  });
247
272
  }
248
273
  if (path === '/keys/escrow/attestation') return response({ attestation: {} });
@@ -271,6 +296,19 @@ const setup = async (legacyEnrolled = false) => {
271
296
  ).toBe(true);
272
297
  expect(signed.intent.expectedEnrollmentVersion).toBe(state.enrollmentVersion);
273
298
  intents.push(signed);
299
+ if (faults.failEnrollment) throw new Error('Temporary enrollment outage');
300
+ // Mirror escrow.enroll: rebind removes Mongo metadata, so carry cannot work.
301
+ if (
302
+ faults.missingPinMetadata &&
303
+ state.hasPin &&
304
+ signed.intent.pinAction === 'keep'
305
+ ) {
306
+ return response(
307
+ { message: 'Set or clear your recovery PIN before continuing.' },
308
+ 412
309
+ );
310
+ }
311
+ expect(!!body.clearPin).toBe(signed.intent.pinAction === 'clear');
274
312
  let output: EscrowEnvelope | null = null;
275
313
  if (path === '/keys/escrow') {
276
314
  const plaintext = await decryptEscrowBlob(
@@ -462,7 +500,10 @@ const setup = async (legacyEnrolled = false) => {
462
500
  did: faults.releaseDid ?? owner.intent.did,
463
501
  holdId: pendingHold.hold.holdId,
464
502
  shareVersion: version,
465
- recoveryShare: recoveryShares.recoveryShare,
503
+ recoveryShare: state.envelope
504
+ ? (await decryptEscrowBlob(state.envelope, keys.privateKey))
505
+ .recoveryShare
506
+ : recoveryShares.recoveryShare,
466
507
  },
467
508
  pendingHold.hold.clientEphemeralPublicKey
468
509
  ),
@@ -473,6 +514,22 @@ const setup = async (legacyEnrolled = false) => {
473
514
  });
474
515
  }
475
516
  if (path === '/keys/escrow/opt-in') return response({ success: true });
517
+ if (path === '/keys/recovery-session/recover') {
518
+ return response({
519
+ authShare: { encryptedData: authShare },
520
+ primaryDid: owner.intent.did,
521
+ rebindSessionToken: 'rebind-session',
522
+ });
523
+ }
524
+ if (path === '/keys/recovery-session/rebind') {
525
+ enrolled = false;
526
+ faults.missingPinMetadata = true;
527
+ authShare = (body.authShare as { encryptedData: string }).encryptedData;
528
+ return response({
529
+ shareVersion: ++version,
530
+ recoveryMethodsRequireConfirmation: [],
531
+ });
532
+ }
476
533
  throw new Error('Unexpected test request');
477
534
  })
478
535
  );
@@ -508,6 +565,7 @@ const setup = async (legacyEnrolled = false) => {
508
565
  identity,
509
566
  measurements,
510
567
  reload: () => createSSSStrategy(config),
568
+ emailShare: () => formatVersionedEmailShare(recoveryShares.emailShare, version),
511
569
  state: () => state,
512
570
  advance: () => {
513
571
  version++;
@@ -525,6 +583,324 @@ afterEach(() => {
525
583
  });
526
584
 
527
585
  describe('Nitro recovery initiating-client bindings and measurement rollover', () => {
586
+ it('retries recovered enrollment with clear-PIN intent after failure before enclave mutation', async () => {
587
+ const f = await setup();
588
+ await f.strategy.ensureEscrowEnrollment!({
589
+ ...f.params,
590
+ options: { ...f.params.options, pin: '135790' },
591
+ });
592
+ expect(f.state()).toMatchObject({ status: 'active', hasPin: true });
593
+ const hold = await f.strategy.startEscrowRecovery!({
594
+ recoverySessionToken: 'otp-session',
595
+ expectedDid: owner.intent.did,
596
+ });
597
+ await f.strategy.executeRecovery({
598
+ token: '',
599
+ providerType: 'firebase',
600
+ input: { method: 'escrow', ...hold, resumeToken: hold.resumeToken! },
601
+ didFromPrivateKey: async () => owner.intent.did,
602
+ });
603
+ f.faults.failEnrollment = true;
604
+ await f.strategy.completeIdentityRecovery!(f.params);
605
+ expect(f.intents.at(-1)?.intent).toMatchObject({ action: 'replace', pinAction: 'clear' });
606
+ expect(f.state()).toMatchObject({ status: 'active', hasPin: true });
607
+ const writes = f.calls.filter(call => call === 'PUT:/keys/auth-share').length;
608
+ f.faults.failEnrollment = false;
609
+ await expect(
610
+ f.strategy.ensureEscrowEnrollment!({
611
+ ...f.params,
612
+ options: { ...f.params.options, clearPin: true },
613
+ })
614
+ ).resolves.toMatchObject({ enrolled: true });
615
+ expect(f.intents.at(-1)?.intent).toMatchObject({ action: 'replace', pinAction: 'clear' });
616
+ expect(f.state()).toMatchObject({ status: 'active', hasPin: false });
617
+ expect(f.calls.filter(call => call === 'PUT:/keys/auth-share')).toHaveLength(writes);
618
+ });
619
+ it('treats a successful recovered clear-PIN enrollment as satisfying the repair', async () => {
620
+ const f = await setup();
621
+ await f.strategy.ensureEscrowEnrollment!({
622
+ ...f.params,
623
+ options: { ...f.params.options, pin: '135790' },
624
+ });
625
+ const hold = await f.strategy.startEscrowRecovery!({
626
+ recoverySessionToken: 'otp-session',
627
+ expectedDid: owner.intent.did,
628
+ });
629
+ await f.strategy.executeRecovery({
630
+ token: '',
631
+ providerType: 'firebase',
632
+ input: { method: 'escrow', ...hold, resumeToken: hold.resumeToken! },
633
+ didFromPrivateKey: async () => owner.intent.did,
634
+ });
635
+ await f.strategy.completeIdentityRecovery!(f.params);
636
+ expect(f.state()).toMatchObject({ status: 'active', hasPin: false });
637
+ const writes = f.calls.filter(call => call === 'PUT:/keys/auth-share').length;
638
+ const intents = f.intents.length;
639
+ await expect(
640
+ f.strategy.ensureEscrowEnrollment!({
641
+ ...f.params,
642
+ options: { ...f.params.options, clearPin: true },
643
+ })
644
+ ).resolves.toMatchObject({ enrolled: true, changed: false });
645
+ expect(f.calls.filter(call => call === 'PUT:/keys/auth-share')).toHaveLength(writes);
646
+ expect(f.intents).toHaveLength(intents);
647
+ });
648
+ it('repairs a prior enrollment after non-escrow identity recovery outside rollout', async () => {
649
+ const f = await setup();
650
+ await f.strategy.ensureEscrowEnrollment!({
651
+ ...f.params,
652
+ options: { ...f.params.options, pin: '135790' },
653
+ });
654
+ const intents = f.intents.length;
655
+ await f.strategy.prepareIdentityRecovery!({
656
+ recoverySessionToken: 'session',
657
+ input: { method: 'email', emailShare: f.emailShare() },
658
+ didFromPrivateKey: async () => owner.intent.did,
659
+ });
660
+ await expect(f.strategy.completeIdentityRecovery!(f.params)).resolves.toMatchObject({
661
+ escrowEnrolled: false,
662
+ escrowRecovery: false,
663
+ });
664
+ expect(f.intents).toHaveLength(intents);
665
+ // Outside rollout the verified authority proves prior enrollment, so the
666
+ // repair still runs and clears the retired PIN instead of carrying it.
667
+ await expect(
668
+ f.strategy.ensureEscrowEnrollment!({
669
+ ...f.params,
670
+ options: { ...f.params.options, clearPin: true, requirePriorEnrollment: true },
671
+ })
672
+ ).resolves.toMatchObject({ enrolled: true });
673
+ expect(f.intents.at(-1)?.intent).toMatchObject({ action: 'replace', pinAction: 'clear' });
674
+ expect(f.state()).toMatchObject({ status: 'active', hasPin: false });
675
+ });
676
+ const recoverWithFailedDirectEnrollment = async () => {
677
+ const f = await setup();
678
+ await f.strategy.ensureEscrowEnrollment!({
679
+ ...f.params,
680
+ options: { ...f.params.options, pin: '135790' },
681
+ });
682
+ const hold = await f.strategy.startEscrowRecovery!({
683
+ recoverySessionToken: 'otp-session',
684
+ expectedDid: owner.intent.did,
685
+ });
686
+ await f.strategy.executeRecovery({
687
+ token: '',
688
+ providerType: 'firebase',
689
+ input: { method: 'escrow', ...hold, resumeToken: hold.resumeToken! },
690
+ didFromPrivateKey: async () => owner.intent.did,
691
+ });
692
+ f.faults.failEnrollment = true;
693
+ await f.strategy.completeIdentityRecovery!(f.params);
694
+ f.faults.failEnrollment = false;
695
+ expect(f.state()).toMatchObject({ status: 'active', hasPin: true });
696
+ return f;
697
+ };
698
+ const writes = (f: { calls: string[] }) =>
699
+ f.calls.filter(call => call === 'PUT:/keys/auth-share').length;
700
+ it.each([false, true])(
701
+ 'after a reload, asks for a new PIN instead of rotating or signing (outside rollout: %s)',
702
+ async requirePriorEnrollment => {
703
+ const f = await recoverWithFailedDirectEnrollment();
704
+ const intents = f.intents.length;
705
+ const before = writes(f);
706
+ await expect(
707
+ f.reload().ensureEscrowEnrollment!({
708
+ ...f.params,
709
+ options: {
710
+ ...f.params.options,
711
+ ...(requirePriorEnrollment ? { requirePriorEnrollment } : {}),
712
+ },
713
+ })
714
+ ).resolves.toEqual({ enrolled: false, reason: 'pin-reset-required' });
715
+ expect(f.intents).toHaveLength(intents);
716
+ expect(writes(f)).toBe(before);
717
+ expect(f.state()).toMatchObject({ status: 'active', hasPin: true });
718
+ }
719
+ );
720
+ it('lets the user set a new PIN after a rebind left the old one uncarryable', async () => {
721
+ const f = await recoverWithFailedDirectEnrollment();
722
+ const intents = f.intents.length;
723
+ await f.reload().setEscrowPin!({ ...f.params, pin: '246813' });
724
+ expect(f.intents.slice(intents).map(signed => signed.intent)).toEqual([
725
+ // The authority still has the old PIN, so the user's new PIN is a `change`:
726
+ // a replace with fresh PIN material that needs no carry source in lca-api.
727
+ expect.objectContaining({ action: 'replace', pinAction: 'change' }),
728
+ ]);
729
+ expect(f.state()).toMatchObject({ status: 'active', hasPin: true });
730
+ });
731
+ it('signs nothing for a healthy PIN account when lca-api forges rebind status', async () => {
732
+ const f = await setup();
733
+ await f.strategy.ensureEscrowEnrollment!({
734
+ ...f.params,
735
+ options: { ...f.params.options, pin: '135790' },
736
+ });
737
+ expect(f.state()).toMatchObject({ status: 'active', hasPin: true });
738
+ const intents = f.intents.length;
739
+ const before = writes(f);
740
+ f.faults.forgedRebindStatus = true;
741
+ await expect(f.reload().ensureEscrowEnrollment!(f.params)).resolves.toEqual({
742
+ enrolled: false,
743
+ reason: 'pin-reset-required',
744
+ });
745
+ expect(f.intents).toHaveLength(intents);
746
+ expect(writes(f)).toBe(before);
747
+ expect(f.state()).toMatchObject({ status: 'active', hasPin: true });
748
+ });
749
+ it('still clears on forged status when auto-clear is explicitly enabled (needs security sign-off)', async () => {
750
+ const f = await setup();
751
+ await f.strategy.ensureEscrowEnrollment!({
752
+ ...f.params,
753
+ options: { ...f.params.options, pin: '135790' },
754
+ });
755
+ f.faults.forgedRebindStatus = true;
756
+ await expect(
757
+ createSSSStrategy({ ...f.config, autoClearUncarryablePin: true })
758
+ .ensureEscrowEnrollment!(f.params)
759
+ ).resolves.toMatchObject({ enrolled: true, pinCleared: 'automatic' });
760
+ expect(f.intents.at(-1)?.intent).toMatchObject({ action: 'replace', pinAction: 'clear' });
761
+ expect(f.state()).toMatchObject({ hasPin: false });
762
+ });
763
+ it.each([false, true])(
764
+ 'with auto-clear opted in, repairs a recovered PIN account by signing a clear (outside rollout: %s)',
765
+ async requirePriorEnrollment => {
766
+ const f = await recoverWithFailedDirectEnrollment();
767
+ const intents = f.intents.length;
768
+ // Fresh instance: no unenrolledRotation and no clear-PIN intent from the coordinator.
769
+ await expect(
770
+ createSSSStrategy({ ...f.config, autoClearUncarryablePin: true })
771
+ .ensureEscrowEnrollment!({
772
+ ...f.params,
773
+ options: {
774
+ ...f.params.options,
775
+ ...(requirePriorEnrollment ? { requirePriorEnrollment } : {}),
776
+ },
777
+ })
778
+ ).resolves.toMatchObject({ enrolled: true, changed: true });
779
+ expect(f.intents.slice(intents).map(signed => signed.intent)).toEqual([
780
+ expect.objectContaining({ action: 'replace', pinAction: 'clear' }),
781
+ ]);
782
+ expect(f.state()).toMatchObject({ status: 'active', hasPin: false });
783
+ }
784
+ );
785
+ it('reports an automatic clear once, without the DID', async () => {
786
+ const f = await recoverWithFailedDirectEnrollment();
787
+ const onEscrowPinAutoCleared = vi.fn();
788
+ await expect(
789
+ createSSSStrategy({
790
+ ...f.config,
791
+ autoClearUncarryablePin: true,
792
+ onEscrowPinAutoCleared,
793
+ }).ensureEscrowEnrollment!(f.params)
794
+ ).resolves.toEqual({
795
+ enrolled: true,
796
+ changed: true,
797
+ shareVersion: expect.any(Number),
798
+ pinCleared: 'automatic',
799
+ });
800
+ expect(onEscrowPinAutoCleared).toHaveBeenCalledTimes(1);
801
+ expect(onEscrowPinAutoCleared).toHaveBeenCalledWith({
802
+ type: 'escrow.pin.auto_cleared',
803
+ shareVersion: expect.any(Number),
804
+ authorityShareVersion: expect.any(Number),
805
+ });
806
+ expect(JSON.stringify(onEscrowPinAutoCleared.mock.calls)).not.toContain(owner.intent.did);
807
+ });
808
+ it('does not report a clear the caller asked for as automatic', async () => {
809
+ const f = await recoverWithFailedDirectEnrollment();
810
+ const onEscrowPinAutoCleared = vi.fn();
811
+ const result = await createSSSStrategy({
812
+ ...f.config,
813
+ onEscrowPinAutoCleared,
814
+ }).ensureEscrowEnrollment!({
815
+ ...f.params,
816
+ options: { ...f.params.options, clearPin: true },
817
+ });
818
+ expect(result).toMatchObject({ enrolled: true, changed: true });
819
+ expect(result).not.toHaveProperty('pinCleared');
820
+ expect(f.intents.at(-1)?.intent).toMatchObject({ pinAction: 'clear' });
821
+ expect(onEscrowPinAutoCleared).not.toHaveBeenCalled();
822
+ });
823
+ it('does not clear automatically without rebind evidence from the authority', async () => {
824
+ const f = await recoverWithFailedDirectEnrollment();
825
+ const intents = f.intents.length;
826
+ const onEscrowPinAutoCleared = vi.fn();
827
+ // An authority already at the server's share version is no rebind.
828
+ f.state().shareVersion = (
829
+ await f.reload().fetchServerKeyStatus(f.params.token, f.params.providerType)
830
+ ).shareVersion;
831
+ await expect(
832
+ createSSSStrategy({
833
+ ...f.config,
834
+ autoClearUncarryablePin: true,
835
+ onEscrowPinAutoCleared,
836
+ }).ensureEscrowEnrollment!(f.params)
837
+ ).resolves.toEqual({ enrolled: false, reason: 'pin-reset-required' });
838
+ expect(f.intents).toHaveLength(intents);
839
+ expect(onEscrowPinAutoCleared).not.toHaveBeenCalled();
840
+ });
841
+ it('does not burn a share version after a reload when the authority read fails', async () => {
842
+ const f = await recoverWithFailedDirectEnrollment();
843
+ const writes = f.calls.filter(call => call === 'PUT:/keys/auth-share').length;
844
+ const intents = f.intents.length;
845
+ f.faults.snapshotNonce = true;
846
+ await expect(f.reload().ensureEscrowEnrollment!(f.params)).rejects.toThrow();
847
+ expect(f.calls.filter(call => call === 'PUT:/keys/auth-share')).toHaveLength(writes);
848
+ expect(f.intents).toHaveLength(intents);
849
+ });
850
+ it('reports a leftover pending escrow method as prior enrollment after a reload', async () => {
851
+ const f = await recoverWithFailedDirectEnrollment();
852
+ await expect(f.reload().getEscrowEnrollmentState!(f.params)).resolves.toMatchObject({
853
+ state: 'not-enrolled',
854
+ priorEscrowMethod: true,
855
+ });
856
+ });
857
+ it('falls back to the confirmed method list on servers without priorEscrowEnrollment', async () => {
858
+ const f = await recoverWithFailedDirectEnrollment();
859
+ f.faults.oldServer = true;
860
+ const state = await f.reload().getEscrowEnrollmentState!(f.params);
861
+ expect(state).toMatchObject({ state: 'not-enrolled' });
862
+ expect(state).not.toHaveProperty('priorEscrowMethod');
863
+ });
864
+ it('does not enroll a never-enrolled account through prior-enrollment repair', async () => {
865
+ const f = await setup();
866
+ await f.strategy.prepareIdentityRecovery!({
867
+ recoverySessionToken: 'session',
868
+ input: { method: 'email', emailShare: f.emailShare() },
869
+ didFromPrivateKey: async () => owner.intent.did,
870
+ });
871
+ await f.strategy.completeIdentityRecovery!(f.params);
872
+ await expect(
873
+ f.strategy.ensureEscrowEnrollment!({
874
+ ...f.params,
875
+ options: { ...f.params.options, clearPin: true, requirePriorEnrollment: true },
876
+ })
877
+ ).resolves.toEqual({ enrolled: false, reason: 'not-enrolled' });
878
+ expect(f.intents).toHaveLength(0);
879
+ expect(f.state()).toMatchObject({ status: 'empty' });
880
+ });
881
+ it('binds recovery contact into the enrollment intent after identity recovery', async () => {
882
+ const f = await setup();
883
+ const hold = await f.strategy.startEscrowRecovery!({
884
+ recoverySessionToken: 'otp-session',
885
+ expectedDid: owner.intent.did,
886
+ });
887
+ await f.strategy.executeRecovery({
888
+ token: '',
889
+ providerType: 'firebase',
890
+ input: { method: 'escrow', ...hold, resumeToken: hold.resumeToken! },
891
+ didFromPrivateKey: async () => owner.intent.did,
892
+ });
893
+ await f.strategy.completeIdentityRecovery!({
894
+ ...f.params,
895
+ options: { recoveryEmail: 'recovered@example.com', recoveryLanguage: 'es' },
896
+ });
897
+ expect(f.intents.at(-1)?.intent).toMatchObject({
898
+ recoveryEmail: 'recovered@example.com',
899
+ recoveryLanguage: 'es',
900
+ action: 'enroll',
901
+ });
902
+ expect(f.state()).toMatchObject({ status: 'active', hasPin: false });
903
+ });
528
904
  const start = (f: Awaited<ReturnType<typeof setup>>) =>
529
905
  f.strategy.startEscrowRecovery!({
530
906
  token: 'token',