@bsv/expo-wallet-toolbox 0.3.1 → 0.5.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 (119) hide show
  1. package/CHANGELOG.md +311 -0
  2. package/README.md +52 -1
  3. package/assets/sounds/vault-deposit.wav +0 -0
  4. package/assets/sounds/vault-withdraw.wav +0 -0
  5. package/core/config.tsx +21 -18
  6. package/core/context/VaultContext.tsx +8 -11
  7. package/core/context/WalletConnectionContext.tsx +137 -52
  8. package/core/context/WalletContext.tsx +413 -23
  9. package/core/hooks/useConfirmationSound.ts +14 -8
  10. package/core/i18n/translations.tsx +3368 -952
  11. package/core/index.ts +107 -14
  12. package/core/localpay/build.ts +674 -37
  13. package/core/localpay/codec.ts +68 -5
  14. package/core/localpay/pending.ts +240 -22
  15. package/core/localpay/settlementAck.ts +149 -0
  16. package/core/localpay/types.ts +14 -1
  17. package/core/localpay/verify.ts +118 -69
  18. package/core/mandala/abortGuard.ts +134 -0
  19. package/core/mandala/basketMigration.ts +166 -0
  20. package/core/mandala/bundle.ts +244 -0
  21. package/core/mandala/createRuntime.ts +2437 -0
  22. package/core/mandala/drain.ts +670 -0
  23. package/core/mandala/permissionModule.ts +642 -0
  24. package/core/mandala/resendTransfer.ts +175 -0
  25. package/core/mandala/runtime.ts +357 -0
  26. package/core/mandala/settlementStore.ts +412 -0
  27. package/core/mandala/types.ts +290 -0
  28. package/core/offline/cancelParked.ts +94 -1
  29. package/core/offline/payerHold.ts +152 -12
  30. package/core/offline/tokenFrames.ts +197 -0
  31. package/core/parsePeerPayURI.ts +70 -17
  32. package/core/pay/rails/address.ts +51 -3
  33. package/core/pay/rails/handle.ts +88 -9
  34. package/core/pay/rails/index.ts +16 -6
  35. package/core/pay/rails/nearby.ts +114 -4
  36. package/core/services/capWalletArgs.ts +8 -9
  37. package/core/services/externalOrigin.ts +39 -0
  38. package/core/services/vault/VaultKeyService.ts +912 -251
  39. package/core/services/vault/ceremony.ts +435 -367
  40. package/core/services/vault/ceremonyHost.ts +27 -21
  41. package/core/services/vault/devMock.ts +30 -7
  42. package/core/services/vault/driver.ts +95 -41
  43. package/core/services/vault/guard.ts +514 -56
  44. package/core/services/vault/hardwareLease.ts +22 -0
  45. package/core/services/vault/mockYubiKey.ts +258 -90
  46. package/core/services/vault/pivReset.ts +145 -0
  47. package/core/services/vault/r1comb.ts +976 -0
  48. package/core/services/vault/session.ts +93 -26
  49. package/core/services/vault/transfers.ts +2617 -747
  50. package/core/services/vault/types.ts +100 -43
  51. package/core/services/vault/vaultStore.ts +991 -74
  52. package/core/services/walletConnectionValidation.ts +293 -0
  53. package/core/services/walletServiceConfig.ts +19 -24
  54. package/core/storage/StorageExpoSQLite.ts +382 -56
  55. package/core/storage/dbImage.ts +48 -0
  56. package/core/storage/methods/findSql.ts +0 -54
  57. package/core/storage/methods/listOutputsSql.ts +7 -3
  58. package/core/storage/methods/offlineActions.ts +15 -0
  59. package/core/storage/methods/processOfflineActions.ts +263 -6
  60. package/core/storage/methods/walletBalanceSql.ts +16 -4
  61. package/core/storage/schema/createTables.ts +123 -0
  62. package/core/storage/sqlUpdateValue.ts +24 -0
  63. package/core/toolboxConfig.ts +240 -0
  64. package/core/walletRepair/runWalletCheck.ts +19 -2
  65. package/package.json +25 -24
  66. package/ui/components/QRScanner.tsx +1 -1
  67. package/ui/components/pay/AdmissionNotice.tsx +99 -0
  68. package/ui/components/pay/AssetPicker.tsx +184 -0
  69. package/ui/components/pay/AvailableBalance.tsx +33 -2
  70. package/ui/components/pay/HandleReceive.tsx +213 -50
  71. package/ui/components/pay/NearbyFlow.tsx +616 -69
  72. package/ui/components/pay/PayForm.tsx +73 -8
  73. package/ui/components/pay/PaymentSuccessOverlay.tsx +43 -2
  74. package/ui/components/pay/RecipientField.tsx +49 -4
  75. package/ui/components/pay/RequestHub.tsx +68 -8
  76. package/ui/components/pay/ResultBanner.tsx +41 -4
  77. package/ui/components/pay/UniversalSend.tsx +534 -30
  78. package/ui/components/pay/tokenSendCopy.ts +116 -0
  79. package/ui/components/pay/useRecipientInput.ts +36 -7
  80. package/ui/components/ui/PermissionSheet.tsx +65 -4
  81. package/ui/components/ui/Sheet.tsx +9 -2
  82. package/ui/components/vault/EnrollWizard.tsx +1432 -488
  83. package/ui/components/vault/KeyChooser.tsx +96 -0
  84. package/ui/components/vault/VaultCeremonySheet.tsx +107 -43
  85. package/ui/components/vault/vaultErrorCopy.ts +145 -0
  86. package/ui/components/wallet/ActivityRow.tsx +101 -17
  87. package/ui/components/wallet/AmountInput.tsx +77 -25
  88. package/ui/components/wallet/AssetAmount.tsx +95 -0
  89. package/ui/components/wallet/AssetSwitcherDropdown.tsx +261 -0
  90. package/ui/components/wallet/tokenEviction.ts +65 -0
  91. package/ui/hooks/useExportWalletData.ts +39 -0
  92. package/ui/hooks/useMandala.ts +303 -0
  93. package/ui/hooks/useVaultBalance.ts +105 -7
  94. package/ui/hooks/useVaultCoverage.ts +52 -0
  95. package/ui/importDatabases.ts +7 -3
  96. package/ui/index.ts +50 -7
  97. package/ui/screens/ConnectionsScreen.tsx +46 -29
  98. package/ui/screens/PairScreen.tsx +12 -12
  99. package/ui/screens/PayScreen.tsx +130 -10
  100. package/ui/screens/SettingsScreen.tsx +18 -9
  101. package/ui/screens/VaultScreen.tsx +925 -171
  102. package/ui/screens/VaultTransferScreen.tsx +376 -123
  103. package/ui/screens/WalletCheckScreen.tsx +10 -1
  104. package/ui/screens/WalletConfigScreen.tsx +42 -20
  105. package/ui/screens/WalletHomeScreen.tsx +417 -86
  106. package/ui/screens/homeBadges.ts +9 -1
  107. package/ui/screens/tokenRowTitle.ts +68 -0
  108. package/ui/tokenFormat.ts +108 -0
  109. package/ui/tokenSeen.ts +75 -0
  110. package/ui/tokenStatus.ts +79 -0
  111. package/assets/sounds/vault-close.wav +0 -0
  112. package/assets/sounds/vault-open.wav +0 -0
  113. package/core/services/vault/k1.ts +0 -75
  114. package/core/services/vault/sealing.ts +0 -84
  115. package/core/services/vault/vaultDerivation.ts +0 -152
  116. package/core/services/vault/vaultPassphrase.ts +0 -287
  117. package/ui/components/vault/PassphraseField.tsx +0 -306
  118. package/ui/components/vault/PhraseBackupSheet.tsx +0 -227
  119. package/ui/screens/VaultRecoverScreen.tsx +0 -226
@@ -1,26 +1,15 @@
1
1
  /**
2
- * Vault domain types — shared by the sealing crypto, the store, the ceremony
3
- * controller, and the UI. No React, no I/O.
2
+ * Vault domain types — shared by the store, the ceremony controller, the
3
+ * transfers and the UI. No React, no I/O.
4
+ *
5
+ * There is no sealed private YubiKey material or seed-derived spending
6
+ * authority: the enrolled YubiKeys are the spending keys. The wallet root
7
+ * derives deterministic HMAC salts from the ordered serial list.
8
+ * Each output carries its salt in the locking script; customInstructions
9
+ * mirror it for indexed discovery and are accepted only after exact script and
10
+ * salt-derivation verification.
4
11
  */
5
12
 
6
- /** Persisted seal: everything needed to recover the vault key EXCEPT the
7
- * on-token ECDH. The blob alone is useless without the physical YubiKey. */
8
- export interface SealedBlob {
9
- v: 1
10
- /** PIV slot holding the P-256 key (0x82, first "retired" slot). */
11
- slot: number
12
- /** Ephemeral P-256 public key, hex, 65-byte uncompressed SEC1 point. */
13
- ePub: string
14
- /** HKDF salt, hex, 32 bytes. */
15
- salt: string
16
- /** AES-256-GCM ciphertext of the vault key (SymmetricKey wire format), hex. */
17
- c: string
18
- /** Serial of the enrolled YubiKey — ceremony rejects other keys early. */
19
- yubiSerial: string
20
- /** sha256 of the token public key, hex — sanity check against slot rewrites. */
21
- yubiPubSha256: string
22
- }
23
-
24
13
  export type VaultErrorCode =
25
14
  | 'unsupported-platform'
26
15
  | 'no-key'
@@ -28,19 +17,27 @@ export type VaultErrorCode =
28
17
  | 'pin-required'
29
18
  | 'pin-invalid'
30
19
  | 'pin-locked'
20
+ | 'puk-invalid'
21
+ | 'puk-locked'
31
22
  | 'touch-timeout'
32
23
  | 'key-removed-mid-op'
24
+ /** Another transfer still owns the single process-wide hardware signer. */
25
+ | 'ceremony-active'
26
+ /** Wallet or chain changed while a scoped vault operation was in flight. */
27
+ | 'scope-changed'
33
28
  | 'mgmt-key-custom'
29
+ /** Factory F9 or generated-slot manufacturer attestation was missing,
30
+ * malformed, mismatched, or outside the pinned Yubico production chains. */
31
+ | 'attestation-invalid'
34
32
  | 'slot-occupied'
35
- /** unsealVaultKey could not open a SealedBlob — wrong shared secret,
36
- * tampered ciphertext, or a malformed blob. Never distinguishes which. */
37
- | 'seal-corrupt'
38
- /** A vault key digest or key material failed a structural check. Usually a
39
- * corrupted dependency or a native bug, but a user CAN cause this: choosing
40
- * to adopt a foreign PIV slot (VaultKeyService's `adoptExisting`) whose key
41
- * material isn't a compatible EC point makes sealVaultKey's ECDH reject it
42
- * the same way. Distinct from 'wrong-key', which vaultErrorFromNative may
43
- * reclassify to 'nfc-lost'. */
33
+ /** A slot key exists, but management-key rotation did not confirm. The
34
+ * attached VaultEnrollmentPartialError record can be challenged/recovered;
35
+ * it must never be silently persisted as enrolled. */
36
+ | 'enrollment-partial'
37
+ /** A vault key digest, DER signature, pubkey or script failed a structural
38
+ * check (r1comb.ts throws it for malformed SEC1 points, DER, or a lock that
39
+ * is not an R1C lock). Distinct from 'wrong-key', which vaultErrorFromNative
40
+ * may reclassify to 'nfc-lost'. */
44
41
  | 'template-invalid'
45
42
  | 'serial-mismatch'
46
43
  | 'user-cancelled'
@@ -51,16 +48,6 @@ export type VaultErrorCode =
51
48
  | 'below-dust'
52
49
  | 'no-transaction'
53
50
  | 'nfc-lost'
54
- /** Vault passphrase missing or empty — an empty one would collide with the
55
- * main wallet's master key. */
56
- | 'bad-passphrase'
57
- /** Main wallet recovery phrase failed BIP39 validation. */
58
- | 'bad-mnemonic'
59
- /** Deposit index outside the non-hardened BIP32 range. */
60
- | 'bad-derivation-index'
61
- /** No backup attestation for this wallet — depositing would create funds
62
- * with no recovery path. Advisory gate, not a security control. */
63
- | 'backup-required'
64
51
  /** More vault inputs would be needed than one transaction may safely carry.
65
52
  * See VAULT_MAX_INPUTS — the remedy is a smaller withdrawal, which also
66
53
  * consolidates the vault. */
@@ -68,17 +55,68 @@ export type VaultErrorCode =
68
55
  /** The device is offline. Vault transfers never enter the offline queue — see
69
56
  * VaultTransferOptions.isOnline. */
70
57
  | 'requires-online'
58
+ // ── R1C (1-of-N comb vault) ──────────────────────────────────────────────
59
+ /** `vaultEnabled` is off in this build (spec §0 / D15): no enrollment,
60
+ * deposit, re-vault or re-lock may create a vault output. */
61
+ | 'not-released'
62
+ /** The wallet is on `test` or `teratest` and the vault is mainnet-only
63
+ * (task 11). Deliberately distinct from 'not-released': the copy that code
64
+ * maps to tells the user the BUILD is off, which is false here and would
65
+ * hide the one thing they can act on — switching network. */
66
+ | 'not-on-mainnet'
67
+ /** No private backup service is configured, or encrypted backup push is
68
+ * opted out. New Vault outputs retain recovery metadata in that backup. */
69
+ | 'backup-off'
70
+ /** Fewer than VAULT_MIN_KEYS keys — defensive; the wizard cannot persist it. */
71
+ | 'not-enough-keys'
72
+ /** The tapped serial is already in meta.keys or in the wizard's pending list.
73
+ * The message carries the serial. */
74
+ | 'key-already-enrolled'
75
+ /** Restored metadata exists, but this serial has not yet passed the fresh
76
+ * possession challenge on this device. */
77
+ | 'key-not-adopted'
78
+ /** VAULT_MAX_KEYS keys already enrolled. */
79
+ | 'too-many-keys'
80
+ /** Removing this key would leave fewer than VAULT_MIN_KEYS. */
81
+ | 'last-keys'
82
+ /** Removing this key would orphan an output only it can open — re-lock first.
83
+ * Strictly that meaning: a re-lock is the remedy the copy names, so a
84
+ * refusal that a re-lock cannot clear belongs on 'action-pending'. */
85
+ | 'relock-required'
86
+ /** An unfinished vault transaction is in the way — a signed but unbroadcast
87
+ * deposit still reserving its inputs, a pending or failed action holding a
88
+ * vault output, or a deposit whose state cannot be classified. Unlike
89
+ * 'relock-required' a re-lock is NOT the remedy (and is itself refused):
90
+ * the transaction has to settle, or be reconciled, first. */
91
+ | 'action-pending'
92
+ /** The chosen key is not among the commitments baked into an output's real
93
+ * lock (or no reachable output exists). The message names the outpoint. */
94
+ | 'key-not-committed'
95
+ /** The chosen key can open less than the requested amount while other keys
96
+ * could open more. */
97
+ | 'key-cannot-cover'
98
+ /** acc − feeEstimate < VAULT_DEPOSIT_MIN: nothing worth re-locking. */
99
+ | 'too-small-to-relock'
100
+ /** The signable transaction is not consensus-bound version 1 — refused
101
+ * before any signature. */
102
+ | 'bad-version'
71
103
 
72
104
  export class VaultError extends Error {
73
105
  code: VaultErrorCode
74
106
  /** PIN attempts remaining, present on pin-invalid. */
75
107
  retriesLeft?: number
108
+ /** Structured context for the specific failure — e.g. serial-mismatch's
109
+ * { tapped, chosen } or key-cannot-cover's { reachable, total } (see the
110
+ * Interfaces note above for the full convention). Additive: most codes
111
+ * leave it undefined, and no existing call site needs to change. */
112
+ details?: Record<string, string | number>
76
113
 
77
- constructor(code: VaultErrorCode, message?: string, retriesLeft?: number) {
114
+ constructor(code: VaultErrorCode, message?: string, retriesLeft?: number, details?: Record<string, string | number>) {
78
115
  super(message ?? code)
79
116
  this.name = 'VaultError'
80
117
  this.code = code
81
118
  this.retriesLeft = retriesLeft
119
+ this.details = details
82
120
  }
83
121
  }
84
122
 
@@ -89,11 +127,30 @@ export class VaultError extends Error {
89
127
  * description it doesn't specifically recognize, which includes this one. */
90
128
  const NFC_LOST_PATTERN = /tag response error|no response|tag connection lost|session invalidated/i
91
129
 
92
- /** Parse a native-module rejection (`VAULT_ERR:<code>:<detail>`) into a
93
- * VaultError; anything unrecognized becomes a generic driver failure. */
130
+ /**
131
+ * Parse a native-module rejection (`VAULT_ERR:<code>:<detail>`) into a
132
+ * VaultError; anything unrecognized becomes a generic driver failure.
133
+ *
134
+ * The payload is SEARCHED for, not anchored to the start of the message. iOS
135
+ * rejects with an NSError, and what reaches JS is that error's `description`,
136
+ * not its `localizedDescription` — so the real payload arrives wrapped:
137
+ *
138
+ * Error Domain=YubiKeyPiv Code=1 "VAULT_ERR:attestation-invalid:…" UserInfo={…}
139
+ *
140
+ * An anchored `^VAULT_ERR:` never matched that, so EVERY iOS native vault error
141
+ * — wrong PIN, blocked PIN, occupied slot, failed attestation — collapsed into
142
+ * `driver-unavailable`, whose copy reads "YubiKey support is unavailable on
143
+ * this device." That told the user their phone lacked NFC when the real answer
144
+ * was on the card. Android is unaffected: it rejects with a bare string, which
145
+ * this still matches.
146
+ *
147
+ * The detail stops at a double quote so the NSError's trailing ` UserInfo={…}`
148
+ * does not get absorbed into it; an unwrapped Android message has no quote and
149
+ * runs to the end as before.
150
+ */
94
151
  export function vaultErrorFromNative(e: unknown): VaultError {
95
152
  const msg = e instanceof Error ? e.message : String(e)
96
- const m = /^VAULT_ERR:([a-z-]+):?(.*)$/.exec(msg)
153
+ const m = /VAULT_ERR:([a-z-]+):?([^"]*)/.exec(msg)
97
154
  if (m) {
98
155
  let code = m[1] as VaultErrorCode
99
156
  const detailMatch = /retries=(\d+)/.exec(m[2])