@bsv/expo-wallet-toolbox 0.4.0 → 0.7.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 (182) hide show
  1. package/CHANGELOG.md +547 -0
  2. package/README.md +19 -1
  3. package/assets/sounds/vault-deposit.wav +0 -0
  4. package/assets/sounds/vault-withdraw.wav +0 -0
  5. package/core/amountFormatHelpers.ts +238 -34
  6. package/core/backup/RemoteSyncReader.ts +2 -5
  7. package/core/backup/codec.ts +2 -2
  8. package/core/backup/push.ts +1 -1
  9. package/core/config.tsx +16 -1
  10. package/core/contacts/contactActivity.ts +153 -0
  11. package/core/contacts/contactCache.ts +41 -0
  12. package/core/contacts/contactsStore.ts +183 -0
  13. package/core/context/VaultContext.tsx +8 -11
  14. package/core/context/WalletConnectionContext.tsx +137 -52
  15. package/core/context/WalletContext.tsx +464 -30
  16. package/core/demo/demoAssets.ts +60 -0
  17. package/core/demo/demoLedger.ts +240 -0
  18. package/core/demo/demoMandalaRuntime.ts +96 -0
  19. package/core/demo/demoMode.ts +38 -0
  20. package/core/demo/demoWallet.ts +25 -0
  21. package/core/demo/index.ts +26 -0
  22. package/core/demo/useDemo.ts +59 -0
  23. package/core/headers/OfflineFirstChaintracks.ts +52 -14
  24. package/core/headers/headerStore.ts +7 -10
  25. package/core/hooks/useConfirmationSound.ts +14 -8
  26. package/core/i18n/translations.tsx +4562 -986
  27. package/core/identity/contactLink.ts +36 -0
  28. package/core/identity/handleRegistry/client.ts +285 -0
  29. package/core/identity/handleRegistry/profileCert.ts +254 -0
  30. package/core/identity/handleRegistry/registration.ts +559 -0
  31. package/core/identity/handleRegistry/resolver.ts +247 -0
  32. package/core/identity/handleRegistry/rules.ts +104 -0
  33. package/core/index.ts +116 -15
  34. package/core/localpay/build.ts +674 -37
  35. package/core/localpay/codec.ts +68 -5
  36. package/core/localpay/pending.ts +240 -22
  37. package/core/localpay/pendingAborts.ts +5 -1
  38. package/core/localpay/settlementAck.ts +149 -0
  39. package/core/localpay/types.ts +14 -1
  40. package/core/localpay/verify.ts +118 -69
  41. package/core/mandala/abortGuard.ts +134 -0
  42. package/core/mandala/basketMigration.ts +166 -0
  43. package/core/mandala/bundle.ts +244 -0
  44. package/core/mandala/createRuntime.ts +2437 -0
  45. package/core/mandala/drain.ts +670 -0
  46. package/core/mandala/permissionModule.ts +642 -0
  47. package/core/mandala/resendTransfer.ts +175 -0
  48. package/core/mandala/runtime.ts +357 -0
  49. package/core/mandala/settlementStore.ts +412 -0
  50. package/core/mandala/types.ts +290 -0
  51. package/core/monitor/MonitorSupervisor.ts +121 -0
  52. package/core/monitor/TaskBackupPush.ts +1 -1
  53. package/core/monitor/TaskCreditInbox.ts +1 -1
  54. package/core/monitor/TaskDrainOutbox.ts +1 -1
  55. package/core/monitor/TaskSendOffline.ts +1 -1
  56. package/core/net/online.ts +49 -0
  57. package/core/numberFormat.ts +19 -0
  58. package/core/offline/cancelParked.ts +94 -1
  59. package/core/offline/payerHold.ts +152 -12
  60. package/core/offline/plan.ts +1 -1
  61. package/core/offline/tokenFrames.ts +197 -0
  62. package/core/parsePeerPayURI.ts +70 -17
  63. package/core/pay/rails/address.ts +51 -3
  64. package/core/pay/rails/handle.ts +88 -9
  65. package/core/pay/rails/index.ts +16 -6
  66. package/core/pay/rails/nearby.ts +114 -4
  67. package/core/pay/recordProof.ts +2 -6
  68. package/core/services/arcadeBroadcastProvider.ts +1 -4
  69. package/core/services/capWalletArgs.ts +8 -9
  70. package/core/services/exchangeRate.ts +1 -1
  71. package/core/services/externalOrigin.ts +42 -0
  72. package/core/services/vault/VaultKeyService.ts +912 -251
  73. package/core/services/vault/ceremony.ts +435 -367
  74. package/core/services/vault/ceremonyHost.ts +27 -21
  75. package/core/services/vault/devMock.ts +30 -7
  76. package/core/services/vault/driver.ts +95 -41
  77. package/core/services/vault/guard.ts +514 -56
  78. package/core/services/vault/hardwareLease.ts +22 -0
  79. package/core/services/vault/mockYubiKey.ts +258 -90
  80. package/core/services/vault/pivReset.ts +145 -0
  81. package/core/services/vault/r1comb.ts +976 -0
  82. package/core/services/vault/session.ts +93 -26
  83. package/core/services/vault/transfers.ts +2617 -747
  84. package/core/services/vault/types.ts +100 -43
  85. package/core/services/vault/vaultStore.ts +991 -74
  86. package/core/services/walletConnectionValidation.ts +293 -0
  87. package/core/services/walletServiceConfig.ts +31 -20
  88. package/core/storage/StorageExpoSQLite.ts +420 -87
  89. package/core/storage/dbImage.ts +48 -0
  90. package/core/storage/methods/findSql.ts +3 -54
  91. package/core/storage/methods/listActionsSql.ts +8 -5
  92. package/core/storage/methods/listOutputsSql.ts +9 -5
  93. package/core/storage/methods/offlineActions.ts +15 -0
  94. package/core/storage/methods/processOfflineActions.ts +265 -9
  95. package/core/storage/methods/reviewStatusSql.ts +7 -2
  96. package/core/storage/methods/walletBalanceSql.ts +16 -4
  97. package/core/storage/schema/createTables.ts +152 -0
  98. package/core/storage/sqlUpdateValue.ts +24 -0
  99. package/core/toolboxConfig.ts +182 -1
  100. package/core/toolboxTypes.ts +40 -0
  101. package/core/userAvatar.ts +79 -0
  102. package/core/walletMonitor.ts +8 -3
  103. package/core/walletRepair/runWalletCheck.ts +19 -2
  104. package/package.json +31 -30
  105. package/ui/activityFilter.ts +115 -0
  106. package/ui/components/QRScanner.tsx +1 -1
  107. package/ui/components/pay/AdmissionNotice.tsx +99 -0
  108. package/ui/components/pay/AssetPicker.tsx +184 -0
  109. package/ui/components/pay/AvailableBalance.tsx +33 -2
  110. package/ui/components/pay/HandleReceive.tsx +213 -50
  111. package/ui/components/pay/NearbyFlow.tsx +616 -69
  112. package/ui/components/pay/PayForm.tsx +73 -8
  113. package/ui/components/pay/PaymentSuccessOverlay.tsx +89 -8
  114. package/ui/components/pay/RecipientField.tsx +132 -73
  115. package/ui/components/pay/RequestHub.tsx +68 -8
  116. package/ui/components/pay/ResultBanner.tsx +41 -4
  117. package/ui/components/pay/StepBar.tsx +32 -0
  118. package/ui/components/pay/UniversalSend.tsx +1118 -86
  119. package/ui/components/pay/tokenSendCopy.ts +116 -0
  120. package/ui/components/pay/useRecipientInput.ts +36 -7
  121. package/ui/components/ui/PencilEditField.tsx +109 -0
  122. package/ui/components/ui/PermissionSheet.tsx +65 -4
  123. package/ui/components/ui/Sheet.tsx +9 -2
  124. package/ui/components/ui/SlideOverFromRight.tsx +86 -0
  125. package/ui/components/vault/EnrollWizard.tsx +1432 -488
  126. package/ui/components/vault/KeyChooser.tsx +96 -0
  127. package/ui/components/vault/VaultCeremonySheet.tsx +107 -43
  128. package/ui/components/vault/vaultErrorCopy.ts +145 -0
  129. package/ui/components/wallet/ActivityRow.tsx +283 -97
  130. package/ui/components/wallet/AmountInput.tsx +92 -28
  131. package/ui/components/wallet/AssetAmount.tsx +95 -0
  132. package/ui/components/wallet/AssetSwitcherDropdown.tsx +261 -0
  133. package/ui/components/wallet/ContactActivityList.tsx +149 -0
  134. package/ui/components/wallet/ContactSigil.tsx +49 -0
  135. package/ui/components/wallet/EditPictureSheet.tsx +99 -0
  136. package/ui/components/wallet/FitAmount.tsx +148 -0
  137. package/ui/components/wallet/IconPickerSheet.tsx +168 -0
  138. package/ui/components/wallet/IdentifierRow.tsx +69 -0
  139. package/ui/components/wallet/ImportFromBackupPrompt.tsx +35 -13
  140. package/ui/components/wallet/ProfileButton.tsx +62 -0
  141. package/ui/components/wallet/UserAvatar.tsx +85 -0
  142. package/ui/components/wallet/avatarIcons.ts +122 -0
  143. package/ui/components/wallet/tokenEviction.ts +65 -0
  144. package/ui/hooks/useContactsStore.ts +10 -0
  145. package/ui/hooks/useExportWalletData.ts +39 -0
  146. package/ui/hooks/useMandala.ts +320 -0
  147. package/ui/hooks/useOnline.ts +65 -20
  148. package/ui/hooks/useVaultBalance.ts +105 -7
  149. package/ui/hooks/useVaultCoverage.ts +52 -0
  150. package/ui/importDatabases.ts +7 -3
  151. package/ui/index.ts +75 -7
  152. package/ui/screens/ConnectionsScreen.tsx +46 -29
  153. package/ui/screens/ContactScreen.tsx +289 -0
  154. package/ui/screens/ContactsScreen.tsx +373 -0
  155. package/ui/screens/IdentifierScreen.tsx +234 -0
  156. package/ui/screens/NewContactScreen.tsx +362 -0
  157. package/ui/screens/PairScreen.tsx +12 -12
  158. package/ui/screens/PayScreen.tsx +145 -12
  159. package/ui/screens/ProfileScreen.tsx +783 -0
  160. package/ui/screens/SettingsScreen.tsx +18 -9
  161. package/ui/screens/TransactionDetailScreen.tsx +419 -0
  162. package/ui/screens/TrustScreen.tsx +1 -1
  163. package/ui/screens/VaultScreen.tsx +925 -171
  164. package/ui/screens/VaultTransferScreen.tsx +376 -123
  165. package/ui/screens/WalletCheckScreen.tsx +10 -1
  166. package/ui/screens/WalletConfigScreen.tsx +80 -16
  167. package/ui/screens/WalletHomeScreen.tsx +915 -139
  168. package/ui/screens/homeBadges.ts +9 -1
  169. package/ui/screens/tokenRowTitle.ts +68 -0
  170. package/ui/tokenFormat.ts +108 -0
  171. package/ui/tokenSeen.ts +75 -0
  172. package/ui/tokenStatus.ts +100 -0
  173. package/ui/txStatus.ts +43 -5
  174. package/assets/sounds/vault-close.wav +0 -0
  175. package/assets/sounds/vault-open.wav +0 -0
  176. package/core/services/vault/k1.ts +0 -75
  177. package/core/services/vault/sealing.ts +0 -84
  178. package/core/services/vault/vaultDerivation.ts +0 -152
  179. package/core/services/vault/vaultPassphrase.ts +0 -287
  180. package/ui/components/vault/PassphraseField.tsx +0 -306
  181. package/ui/components/vault/PhraseBackupSheet.tsx +0 -227
  182. package/ui/screens/VaultRecoverScreen.tsx +0 -226
@@ -5,42 +5,48 @@
5
5
  * and the React vault context drive the SAME ceremony. Kept out of any React
6
6
  * module so importing it never pulls in the component graph.
7
7
  */
8
- import { CeremonyController, VaultKeyHandle, VaultProgress } from './ceremony'
8
+ import { CeremonyController, VaultProgress, VaultSigner } from './ceremony'
9
9
  import { getVaultDriver } from './driver'
10
10
  import { vaultStore } from './vaultStore'
11
11
 
12
- /** How long an armed session stays usable before it relocks — and, since the
13
- * ceremony now holds the unwrapped vault key rather than a signing session,
14
- * how long that key may live in memory. */
12
+ /** How long an armed signer stays usable before it relocks — refreshed by
13
+ * progress notes and sign() calls, capped at 3× (see ceremony.ts). */
15
14
  export const VAULT_RETENTION_MS = 120_000
16
15
 
16
+ /** Digests one NFC tap covers before the ceremony closes the session and
17
+ * asks for a fresh tap (spec §4.2 step 6). PROVISIONAL: pinned by the §0
18
+ * device run (a 32-input withdrawal on iOS) against the card's 15 s touch
19
+ * cache and CoreNFC's 60 s session. Persistent readers (Android USB) ignore it. */
20
+ export const VAULT_INPUTS_PER_TAP = 16
21
+
17
22
  export const ceremony = new CeremonyController({
18
23
  getDriver: getVaultDriver,
19
24
  store: {
20
- // Only what the ceremony needs to run the tap: which slot to talk to and
21
- // which card is allowed to answer. v4 meta holds no key material at all —
22
- // everything spendable lives inside the seal.
25
+ // Only what the ceremony needs to run a tap: which serials may answer and
26
+ // which slot / public key each carries. Nicknames and timestamps stay in
27
+ // the store — the sheet reads them from vaultStore directly.
23
28
  getMeta: async () => {
24
29
  const m = await vaultStore.getMeta()
25
- return m ? { slot: m.slot, yubiSerial: m.yubiSerial } : null
26
- },
27
- getSeal: () => vaultStore.getSeal()
30
+ return m ? { keys: m.keys.map(k => ({ serial: k.serial, slot: k.slot, pubkey: k.pubkey })) } : null
31
+ }
28
32
  },
29
- retentionMs: VAULT_RETENTION_MS
33
+ retentionMs: VAULT_RETENTION_MS,
34
+ inputsPerTap: VAULT_INPUTS_PER_TAP
30
35
  })
31
36
 
32
- /** Tap the YubiKey to unwrap the vault key for one operation. Callers MUST
33
- * release() in a finally — that is what drops the key and dismisses the NFC
34
- * sheet. */
35
- export function requestVaultKey(reason: string): Promise<VaultKeyHandle> {
36
- return ceremony.requestKey(reason)
37
+ /** Tap the CHOSEN YubiKey to obtain a signer for one operation. Callers MUST
38
+ * release() in a finally — that is what dismisses the NFC sheet and drops the
39
+ * PIN. `reason` is what the sheet shows, and (Task 6, decision 1) the iOS NFC
40
+ * alert text. */
41
+ export function requestVaultSigner(reason: string, chosenSerial: string): Promise<VaultSigner> {
42
+ return ceremony.requestSigner(reason, chosenSerial)
37
43
  }
38
44
 
39
- /** Report post-arm progress (preparing / broadcasting) so the ceremony sheet
40
- * can show activity through the seconds-long stretches where the JS thread or
41
- * the network is busy, and so the retention window tracks a live operation
42
- * instead of expiring underneath it. A no-op when nothing is armed, which is
43
- * what keeps the K1 recovery sweep from raising a sheet. */
45
+ /** Report post-arm progress (preparing with signed/total, broadcasting) so
46
+ * the ceremony sheet can show activity through the seconds-long stretches
47
+ * where the JS thread or the network is busy, and so the retention window
48
+ * tracks a live operation instead of expiring underneath it. A no-op when
49
+ * nothing is armed — which is what keeps the hardware-free deposit sheet-free. */
44
50
  export function noteVaultProgress(p: VaultProgress): void {
45
51
  ceremony.noteProgress(p)
46
52
  }
@@ -2,21 +2,44 @@
2
2
  * DEV-only convenience: run the whole vault stack against the software mock
3
3
  * YubiKey without hardware. Toggled from wallet-config (DEV builds only).
4
4
  *
5
- * In-session only — installs a MockYubiKey with a key already "inserted" and
6
- * a non-default PIN so enrollment does not force a PIN change. Clearing it
7
- * reverts getVaultDriver() to null (or the real native module, if present).
5
+ * ONE module-held MockYubiKey: its per-serial records (generated keys, PINs)
6
+ * survive toggling the driver off and on within the session, so a vault
7
+ * enrolled with MOCK-DEV-1 and MOCK-DEV-2 can be exercised by switching the
8
+ * "present" key with setMockPresentKey — the DEV wallet-config row's selector.
8
9
  * Kept in its own module so driver.ts never imports the mock.
9
10
  */
10
11
  import { setMockDriver } from './driver'
11
12
  import { MockYubiKey } from './mockYubiKey'
12
13
 
14
+ export type MockPresentKey = 'MOCK-DEV-1' | 'MOCK-DEV-2' | 'MOCK-DEV-3'
15
+
16
+ let instance: MockYubiKey | null = null
17
+ let present: MockPresentKey = 'MOCK-DEV-1'
18
+
13
19
  export function setMockDriverEnabled(on: boolean): void {
20
+ // Keep the software signer unreachable even if a release caller invokes the
21
+ // exported convenience function directly instead of going through the
22
+ // WalletConfigScreen's render gate.
23
+ if (!__DEV__) {
24
+ setMockDriver(null)
25
+ return
26
+ }
14
27
  if (on) {
15
- const mock = new MockYubiKey()
16
- mock.setPin('123456')
17
- mock.insertKey('MOCK-DEV-1')
18
- setMockDriver(mock)
28
+ if (!instance) instance = new MockYubiKey()
29
+ instance.insertKey(present)
30
+ setMockDriver(instance)
19
31
  } else {
20
32
  setMockDriver(null)
21
33
  }
22
34
  }
35
+
36
+ /** Which of the three dev keys is "held to the phone". Applies immediately
37
+ * when the mock is installed; otherwise remembered for the next enable. */
38
+ export function setMockPresentKey(serial: MockPresentKey): void {
39
+ present = serial
40
+ instance?.insertKey(serial)
41
+ }
42
+
43
+ export function getMockPresentKey(): MockPresentKey {
44
+ return present
45
+ }
@@ -11,7 +11,6 @@
11
11
  * "no YubiKey capability on this device", and every caller treats it as such —
12
12
  * the vault UI hides, exactly like localpay's getLocalPayTransport() null path.
13
13
  */
14
- import { Platform } from 'react-native'
15
14
  import { vaultErrorFromNative } from './types'
16
15
 
17
16
  export interface KeyEvent {
@@ -26,44 +25,93 @@ export interface KeyEvent {
26
25
 
27
26
  export interface VaultDriver {
28
27
  isSupported(): boolean
29
- /** True when the transport is a modal per-ceremony session (iOS NFC: start()
30
- * shows the scan sheet, stop() dismisses it) rather than a persistent reader
31
- * (Android USB, mock). Session-based drivers are started only when a ceremony
32
- * begins — never at launch — and stopped when it arms or fails. */
28
+ /** True when discovery and the selected device are owned by one ceremony.
29
+ * Real native drivers are always session-based: iOS presents its NFC sheet;
30
+ * Android discovers either USB or NFC for the ceremony and tears both down
31
+ * before releasing the process-wide hardware lease. Test mocks may model a
32
+ * persistent reader. */
33
33
  sessionBased: boolean
34
- start(): void
34
+ /** Open discovery. `message` is the localised NFC alert text shown on the
35
+ * iOS scan sheet for this session (spec §4.2 step 6); Android and persistent
36
+ * test readers ignore it. */
37
+ start(message?: string): void
35
38
  stop(): void
36
39
  onKeyEvent(cb: (e: KeyEvent) => void): () => void
37
40
  getKeyInfo(): Promise<{ serial: string; firmwareVersion: string; pinRetries: number }>
38
- verifyPin(pin: string): Promise<{ ok: boolean; retriesLeft: number }>
39
- changePin(oldPin: string, newPin: string): Promise<{ ok: boolean; retriesLeft: number }>
40
- generateVaultKey(slot: number): Promise<{ publicKey: string }>
41
- readVaultPublicKey(slot: number): Promise<{ publicKey: string } | null>
42
- /** On-token ECDH (PIV KeyAgreement): derive the shared secret between the
43
- * slot's P-256 key and a peer (ephemeral) public key. Returns the 32-byte
44
- * x-coordinate as hex — the touch-gated step that unwraps a sealed vault
45
- * key (see sealing.ts's seal/unseal pair). TOUCH-gated, PIN-gated. */
46
- ecdh(slot: number, pin: string, peerPublicKey: string): Promise<{ secret: string }>
41
+ verifyPin(expectedSerial: string, pin: string): Promise<{ ok: boolean; retriesLeft: number }>
42
+ changePin(expectedSerial: string, oldPin: string, newPin: string): Promise<{ ok: boolean; retriesLeft: number }>
43
+ /** Rotate the PIV unblock code. Neither value may be logged or persisted. */
44
+ changePuk(expectedSerial: string, oldPuk: string, newPuk: string): Promise<{ ok: boolean; retriesLeft: number }>
45
+ /** Authenticate the default management key and reject occupied user slots
46
+ * before global PIV credentials are changed. Explicit replacement may exempt
47
+ * only Vault slot 0x82; every other user slot remains protected. Native also verifies
48
+ * the factory F9 certificate through the pinned production Yubico chain,
49
+ * offline. Never mutates. */
50
+ preflightDedicatedPiv(
51
+ expectedSerial: string,
52
+ allowOccupiedVaultSlot?: boolean
53
+ ): Promise<{
54
+ ok: true
55
+ inspection: 'metadata' | 'attestation'
56
+ manufacturerAttestation: 'verified'
57
+ }>
58
+ /** Generate a fresh P-256 key in the fixed Vault slot 0x82, with touch
59
+ * policy CACHED and PIN policy ONCE (spec D6). Native returns only after a
60
+ * same-session manufacturer attestation binds the exact key and policies. */
61
+ generateVaultKey(expectedSerial: string): Promise<{
62
+ publicKey: string
63
+ manufacturerAttestation: 'verified'
64
+ }>
65
+ /**
66
+ * Replace the factory management key with native CSPRNG material and discard
67
+ * it. Called immediately after vault-key generation, in the same session.
68
+ */
69
+ protectManagementKey(expectedSerial: string): Promise<{ ok: true }>
70
+ /** Reset the entire PIV application to factory state, destroying every key
71
+ * and certificate in it — Vault slot 0x82 included. Both native SDKs block
72
+ * the PIN and PUK before the RESET APDU, so the retry counters are spent
73
+ * whatever the outcome. Never call this for a serial that is already an
74
+ * enrolled vault key; see pivReset.ts. */
75
+ resetPivApplication(expectedSerial: string): Promise<{ ok: true }>
76
+ /** The Vault slot's public key, or null when none can be READ.
77
+ *
78
+ * Null is not proof of an empty slot. iOS cannot read a retired slot's
79
+ * certificate at all and returns null for every 0x82, occupied or not — so
80
+ * "is the slot empty?" must go to `isVaultSlotOccupied`, and callers here
81
+ * only ever use a non-null result to compare against a key they already
82
+ * hold. */
83
+ readVaultPublicKey(expectedSerial: string): Promise<{ publicKey: string } | null>
84
+ /** Whether the Vault slot holds a key, answered by the card on both
85
+ * platforms (iOS attests the slot, Android reads its metadata).
86
+ *
87
+ * FAILS CLOSED: only the card's explicit reference-not-found reports false.
88
+ * An imported key, a missing attestation slot or any other status reports
89
+ * true, because none of them prove the slot empty. */
90
+ isVaultSlotOccupied(expectedSerial: string): Promise<{ occupied: boolean }>
47
91
  /** Sign a pre-computed 32-byte digest (64 hex chars) with the slot's P-256
48
92
  * key. Returns a DER signature as hex. TOUCH-gated, PIN-gated. */
49
- signEcdsa(slot: number, pin: string, digest: string): Promise<{ signature: string }>
93
+ signEcdsa(expectedSerial: string, pin: string, digest: string): Promise<{ signature: string }>
50
94
  }
51
95
 
52
96
  /** Shape of the native Nitro module (JSON-string API). Kept local so a missing
53
97
  * package never breaks the type-check. */
54
98
  interface NativeYubiKeyPiv {
55
99
  isSupported(): boolean
56
- startDiscovery(): void
100
+ startDiscovery(message: string): void
57
101
  stopDiscovery(): void
58
102
  setKeyListener(listener: (eventType: string, serial: string, transport: string) => void): void
59
103
  clearKeyListener(): void
60
104
  getKeyInfo(): Promise<string>
61
- verifyPin(pin: string): Promise<string>
62
- changePin(oldPin: string, newPin: string): Promise<string>
63
- generateVaultKey(slot: number, touchPolicy: string, pinPolicy: string): Promise<string>
64
- readVaultPublicKey(slot: number): Promise<string>
65
- ecdh(slot: number, pin: string, peerPublicKey: string): Promise<string>
66
- signEcdsa(slot: number, pin: string, digest: string): Promise<string>
105
+ verifyPin(expectedSerial: string, pin: string): Promise<string>
106
+ changePin(expectedSerial: string, oldPin: string, newPin: string): Promise<string>
107
+ changePuk(expectedSerial: string, oldPuk: string, newPuk: string): Promise<string>
108
+ preflightDedicatedPiv(expectedSerial: string, allowOccupiedVaultSlot: boolean): Promise<string>
109
+ generateVaultKey(expectedSerial: string): Promise<string>
110
+ protectManagementKey(expectedSerial: string): Promise<string>
111
+ resetPivApplication(expectedSerial: string): Promise<string>
112
+ readVaultPublicKey(expectedSerial: string): Promise<string>
113
+ isVaultSlotOccupied(expectedSerial: string): Promise<string>
114
+ signEcdsa(expectedSerial: string, pin: string, digest: string): Promise<string>
67
115
  }
68
116
 
69
117
  let injectedMock: VaultDriver | null = null
@@ -119,9 +167,11 @@ function adaptNative(native: NativeYubiKeyPiv): VaultDriver {
119
167
  const listeners = new Set<(e: KeyEvent) => void>()
120
168
  return {
121
169
  isSupported: () => native.isSupported(),
122
- // iOS = NFC (a modal per-tap session); Android = persistent USB reader.
123
- sessionBased: Platform.OS === 'ios',
124
- start: () => {
170
+ // Both native implementations scope discovery to one hardware ceremony.
171
+ // Android listens for USB and NFC together, then synchronously tears both
172
+ // down on stop so an NFC device object can never survive into a later run.
173
+ sessionBased: true,
174
+ start: (message?: string) => {
125
175
  // (Re)install the native listener each start; it forwards into the
126
176
  // persistent JS `listeners` set so app subscribers survive stop/start
127
177
  // cycles (a session-based transport starts and stops per ceremony).
@@ -130,7 +180,7 @@ function adaptNative(native: NativeYubiKeyPiv): VaultDriver {
130
180
  listeners.forEach(cb => cb(e))
131
181
  })
132
182
  try {
133
- native.startDiscovery()
183
+ native.startDiscovery(message ?? '')
134
184
  } catch (e) {
135
185
  // iOS throws VAULT_ERR:driver-unavailable when NFC reading is
136
186
  // unavailable (NFC off, or a wedged nfcd needing a device restart)
@@ -149,22 +199,26 @@ function adaptNative(native: NativeYubiKeyPiv): VaultDriver {
149
199
  return () => listeners.delete(cb)
150
200
  },
151
201
  getKeyInfo: () => parse(native.getKeyInfo()),
152
- verifyPin: pin => parse(native.verifyPin(pin)),
153
- changePin: (o, n) => parse(native.changePin(o, n)),
154
- // 'always' (not 'cached'): the YubiKey is now an ECDH unwrap oracle — one
155
- // on-token ECDH per ceremony recovers the vault key, then every vault
156
- // input signs in software from that recovered key, so there's no longer
157
- // a per-input touch to spare. A single per-op touch is affordable and is
158
- // the stronger policy (no cached-touch window an attacker with a stolen,
159
- // still-inserted key could ride). Existing enrolled keys keep whatever
160
- // policy they were generated under — this only affects new enrollments.
161
- generateVaultKey: slot => parse(native.generateVaultKey(slot, 'always', 'once')),
162
- readVaultPublicKey: async slot => {
163
- const r = await parse<{ publicKey: string | null }>(native.readVaultPublicKey(slot))
202
+ verifyPin: (serial, pin) => parse(native.verifyPin(serial, pin)),
203
+ changePin: (serial, o, n) => parse(native.changePin(serial, o, n)),
204
+ changePuk: (serial, o, n) => parse(native.changePuk(serial, o, n)),
205
+ preflightDedicatedPiv: (serial, allowOccupiedVaultSlot = false) =>
206
+ parse(native.preflightDedicatedPiv(serial, allowOccupiedVaultSlot)),
207
+ // 'cached' (spec D6): the card signs every vault input on-chain, up to
208
+ // VAULT_INPUTS_PER_TAP digests per tap, so one touch must cover a batch —
209
+ // the card keeps a touch valid for 15 s. 'always' would need a touch per
210
+ // input. 'once' lets the PIN verified at session start cover the batch.
211
+ generateVaultKey: serial => parse(native.generateVaultKey(serial)),
212
+ protectManagementKey: serial => parse(native.protectManagementKey(serial)),
213
+ resetPivApplication: serial => parse(native.resetPivApplication(serial)),
214
+ readVaultPublicKey: async serial => {
215
+ const r = await parse<{ publicKey: string | null }>(native.readVaultPublicKey(serial))
164
216
  return r.publicKey ? { publicKey: r.publicKey } : null
165
217
  },
166
- ecdh: (slot, pin, peer) => parse(native.ecdh(slot, pin, peer)),
167
- signEcdsa: (slot, pin, digest) => parse(native.signEcdsa(slot, pin, digest))
218
+ // No `?? true` fallback: a native module too old to implement this would
219
+ // report every slot empty, which is the answer that authorizes an erase.
220
+ isVaultSlotOccupied: serial => parse<{ occupied: boolean }>(native.isVaultSlotOccupied(serial)),
221
+ signEcdsa: (serial, pin, digest) => parse(native.signEcdsa(serial, pin, digest))
168
222
  }
169
223
  }
170
224