@bsv/expo-wallet-toolbox 0.7.0 → 0.9.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 (151) hide show
  1. package/CHANGELOG.md +1745 -0
  2. package/README.md +47 -1
  3. package/core/amountFormatHelpers.ts +20 -2
  4. package/core/backup/RemoteSyncReader.ts +131 -19
  5. package/core/backup/appData.ts +157 -0
  6. package/core/backup/client.ts +15 -1
  7. package/core/backup/codec.ts +147 -9
  8. package/core/backup/constants.ts +30 -1
  9. package/core/backup/cursor.ts +31 -2
  10. package/core/backup/push.ts +70 -4
  11. package/core/backup/restore.ts +104 -16
  12. package/core/backup/restoreOnImport.ts +94 -30
  13. package/core/backup/status.ts +13 -8
  14. package/core/constants.ts +14 -1
  15. package/core/context/ExchangeRateContext.tsx +34 -27
  16. package/core/context/LocalStorageProvider.tsx +43 -40
  17. package/core/context/UserContext.tsx +18 -7
  18. package/core/context/VaultContext.tsx +49 -2
  19. package/core/context/WalletConnectionContext.tsx +203 -11
  20. package/core/context/WalletContext.tsx +631 -162
  21. package/core/headers/OfflineFirstChaintracks.ts +48 -4
  22. package/core/headers/headerStore.ts +26 -4
  23. package/core/headers/syncHeaders.ts +12 -1
  24. package/core/i18n/translations.tsx +953 -25
  25. package/core/identity/handleRegistry/client.ts +31 -2
  26. package/core/identity/handleRegistry/keyPinStore.ts +70 -0
  27. package/core/identity/handleRegistry/resolver.ts +71 -3
  28. package/core/index.ts +33 -3
  29. package/core/localpay/build.ts +225 -24
  30. package/core/localpay/pending.ts +110 -7
  31. package/core/localpay/pendingAbortAuthority.ts +95 -0
  32. package/core/localpay/pendingAborts.ts +264 -28
  33. package/core/localpay/session.ts +42 -17
  34. package/core/localpay/settlementAck.ts +10 -1
  35. package/core/localpay/transport/socket.ts +52 -3
  36. package/core/localpay/verify.ts +100 -10
  37. package/core/mandala/abortGuard.ts +96 -20
  38. package/core/mandala/createRuntime.ts +206 -33
  39. package/core/mandala/drain.ts +89 -11
  40. package/core/mandala/inboxPrehold.ts +186 -0
  41. package/core/mandala/permissionModule.ts +202 -20
  42. package/core/mandala/settlementStore.ts +24 -2
  43. package/core/mandala/types.ts +40 -5
  44. package/core/monitor/TaskBackupPush.ts +14 -0
  45. package/core/monitor/TaskDrainOutbox.ts +14 -0
  46. package/core/monitor/TaskSendOffline.ts +14 -0
  47. package/core/net/publicDestination.ts +91 -0
  48. package/core/net/validateArcUrl.ts +22 -0
  49. package/core/offline/cancelParked.ts +134 -13
  50. package/core/offline/payerHold.ts +100 -24
  51. package/core/offline/plan.ts +36 -7
  52. package/core/pay/beefRepair.ts +6 -2
  53. package/core/pay/damagedInbox.ts +24 -0
  54. package/core/pay/rails/address.ts +107 -12
  55. package/core/pay/rails/handle.ts +117 -22
  56. package/core/pay/rails/index.ts +45 -7
  57. package/core/pay/rails/nativeIntent.ts +61 -0
  58. package/core/pay/receiveHistory.ts +93 -0
  59. package/core/pay/recordProof.ts +19 -1
  60. package/core/pay/refreshProofGuard.ts +42 -1
  61. package/core/pay/watchlist.ts +5 -2
  62. package/core/peerpay/control.ts +17 -18
  63. package/core/peerpay/handleResendRequests.ts +58 -4
  64. package/core/peerpay/outbox.ts +3 -1
  65. package/core/recovery/backupMaterial.ts +61 -0
  66. package/core/recovery/createWallet.ts +95 -0
  67. package/core/recovery/recoverWallet.ts +120 -0
  68. package/core/recovery/restoreWallet.ts +170 -0
  69. package/core/recovery/secret.ts +92 -0
  70. package/core/recovery/shareCollector.ts +71 -0
  71. package/core/recovery/shareParsing.ts +87 -0
  72. package/core/recovery/shares.ts +116 -0
  73. package/core/recovery/useRecoveryDeps.ts +70 -0
  74. package/core/services/arcTokenStorage.ts +52 -0
  75. package/core/services/arcadeBroadcastProvider.ts +232 -48
  76. package/core/services/autoApprovePolicy.ts +105 -0
  77. package/core/services/callbackToken.ts +19 -0
  78. package/core/services/capWalletArgs.ts +14 -2
  79. package/core/services/connectionAuthority.ts +98 -0
  80. package/core/services/exchangeRate.ts +35 -12
  81. package/core/services/externalOrigin.ts +11 -0
  82. package/core/services/secrets/kek.ts +126 -29
  83. package/core/services/secrets/migration.ts +16 -4
  84. package/core/services/secrets/store.ts +42 -14
  85. package/core/services/vault/VaultKeyService.ts +228 -45
  86. package/core/services/vault/chainRecovery.ts +483 -0
  87. package/core/services/vault/driver.ts +9 -1
  88. package/core/services/vault/enrolledSerialRegistry.ts +150 -0
  89. package/core/services/vault/guard.ts +218 -32
  90. package/core/services/vault/hardwareLease.ts +19 -0
  91. package/core/services/vault/metaAuthority.ts +210 -0
  92. package/core/services/vault/pivReset.ts +64 -23
  93. package/core/services/vault/r1comb.ts +139 -3
  94. package/core/services/vault/session.ts +25 -4
  95. package/core/services/vault/transfers.ts +866 -170
  96. package/core/services/vault/types.ts +19 -4
  97. package/core/services/vault/vaultStore.ts +256 -29
  98. package/core/services/walletArgLimits.ts +45 -2
  99. package/core/services/walletConnectionValidation.ts +76 -6
  100. package/core/services/walletServiceConfig.ts +10 -8
  101. package/core/storage/StorageExpoSQLite.ts +47 -13
  102. package/core/storage/methods/findSql.ts +21 -2
  103. package/core/storage/methods/offlineActions.ts +10 -1
  104. package/core/storage/methods/processOfflineActions.ts +116 -23
  105. package/core/storage/schema/createTables.ts +126 -2
  106. package/core/stores/ConnectionStore.ts +34 -0
  107. package/core/toolboxConfig.ts +45 -11
  108. package/core/walletDbRegistry.ts +63 -0
  109. package/core/walletMonitor.ts +17 -1
  110. package/core/walletRepair/releaseStuckReservations.ts +72 -0
  111. package/core/walletRepair/shouldMarkUnspendable.ts +20 -0
  112. package/core/walletRepair/spenderConsumesOutpoint.ts +29 -0
  113. package/package.json +2 -2
  114. package/ui/backupShares.ts +26 -169
  115. package/ui/components/pay/AddressReceive.tsx +13 -6
  116. package/ui/components/pay/AssetPicker.tsx +30 -9
  117. package/ui/components/pay/MessageBoxConfig.tsx +16 -0
  118. package/ui/components/pay/NearbyFlow.tsx +480 -243
  119. package/ui/components/pay/PaymentSuccessOverlay.tsx +39 -4
  120. package/ui/components/pay/UniversalSend.tsx +167 -73
  121. package/ui/components/ui/AlertCard.tsx +38 -5
  122. package/ui/components/ui/PermissionSheet.tsx +197 -66
  123. package/ui/components/vault/EnrollWizard.tsx +61 -13
  124. package/ui/components/vault/vaultErrorCopy.ts +3 -1
  125. package/ui/components/wallet/ActivityRow.tsx +5 -3
  126. package/ui/components/wallet/AmountInput.tsx +8 -2
  127. package/ui/components/wallet/AssetSwitcherDropdown.tsx +22 -2
  128. package/ui/components/wallet/Balance.tsx +42 -23
  129. package/ui/components/wallet/BiometricAdvisoryModal.tsx +16 -3
  130. package/ui/exportTransactions.ts +36 -4
  131. package/ui/hooks/useExportWalletData.ts +25 -2
  132. package/ui/importDatabases.ts +339 -24
  133. package/ui/index.ts +9 -3
  134. package/ui/isImageUrl.ts +6 -0
  135. package/ui/printRecoveryShares.ts +9 -10
  136. package/ui/recoveryPrompts.ts +91 -0
  137. package/ui/resolveIdentity.ts +31 -9
  138. package/ui/screens/ConnectionsScreen.tsx +86 -19
  139. package/ui/screens/IdentifierScreen.tsx +30 -8
  140. package/ui/screens/PairScreen.tsx +74 -2
  141. package/ui/screens/SettingsScreen.tsx +10 -4
  142. package/ui/screens/TrustScreen.tsx +99 -4
  143. package/ui/screens/VaultScreen.tsx +410 -61
  144. package/ui/screens/VaultTransferScreen.tsx +59 -7
  145. package/ui/screens/WalletCheckScreen.tsx +17 -6
  146. package/ui/screens/WalletConfigScreen.tsx +82 -28
  147. package/ui/screens/WalletHomeScreen.tsx +179 -88
  148. package/ui/screens/detailActionKeys.ts +55 -0
  149. package/ui/tokenFormat.ts +44 -4
  150. package/ui/txStatus.ts +5 -0
  151. package/core/storage/LocalStorageAdapter.ts +0 -58
@@ -14,9 +14,11 @@
14
14
  * whose ancestry we resolved once should verify offline forever after.
15
15
  */
16
16
  import { Utils } from '@bsv/sdk'
17
- import type { Chain, ChaintracksClientApi } from '@bsv/wallet-toolbox-mobile'
17
+ import { utils as chaintracksUtils, type Chain, type ChaintracksClientApi } from '@bsv/wallet-toolbox-mobile'
18
18
  import type { HeaderStore } from './headerStore'
19
19
 
20
+ const { blockHash, validateHeaderProofOfWork } = chaintracksUtils
21
+
20
22
  /** A merkle root as display-order hex, whether the source gave us a hex string
21
23
  * or raw bytes. The remote's `findHeaderForHeight` may return either; plain
22
24
  * `String(bytes)` yields "1,2,3,…" which never matches a real root — the source
@@ -68,12 +70,30 @@ export class OfflineFirstChaintracks implements ChaintracksClientApi {
68
70
 
69
71
  async isValidRootForHeight(root: string, height: number): Promise<boolean> {
70
72
  // Fast path: a local root that AGREES is trusted offline forever (validated
71
- // window headers, or a previously-resolved root). We do NOT trust a local
72
- // DISAGREEMENT — it can be a stale/poisoned cache entry or an index error —
73
- // so a miss OR a mismatch both fall through to the authoritative network.
73
+ // window headers, or a previously-resolved root).
74
74
  const local = this.store?.rootForHeight(height)
75
75
  if (local === root) return true
76
76
 
77
+ // A DISAGREEMENT for a height the window has already PoW-validated (its
78
+ // body — everything except the last 6 heights) is refused outright: no
79
+ // remote lookup, no putExtraRoot. Either the window is right and the BEEF
80
+ // is lying, or the window is corrupt — deferring to an unauthenticated
81
+ // network answer and caching it over headers we linked ourselves is
82
+ // exactly the hole this closes (misc-p2-02; a MITM has the same power as a
83
+ // compromised chaintracks deployment absent TLS pinning, misc-p2-13).
84
+ // Everything else — the last-6 reorg tail, and any height the window does
85
+ // not cover at all — keeps the old behaviour below: a disagreement can be
86
+ // a stale/poisoned cache entry or a legitimate reorg, so it falls through
87
+ // to the authoritative network.
88
+ if (local !== undefined && this.store?.isWindowBody(height)) {
89
+ console.warn(
90
+ `[OfflineFirstChaintracks] REFUSED height ${height}: window-covered mismatch ` +
91
+ `(local=${local} wanted=${root}); not consulting the network.`
92
+ )
93
+ this.lastMissHeight = height
94
+ return false
95
+ }
96
+
77
97
  if (!(await this.online())) {
78
98
  // Logged, because this branch is otherwise indistinguishable from a bad
79
99
  // proof: it returns false without consulting anything, and the caller
@@ -99,6 +119,30 @@ export class OfflineFirstChaintracks implements ChaintracksClientApi {
99
119
  this.lastMissHeight = height
100
120
  return false
101
121
  }
122
+
123
+ // XR-068: outside the PoW-validated window body (a miss entirely, or the
124
+ // last-6 reorg tail) this response is otherwise a bare, unauthenticated
125
+ // claim from a single chaintracks call — exactly what a compromised or
126
+ // MITM'd deployment would need to make a forged BEEF root verify. Never
127
+ // trust the remote's self-reported `header.hash`: recompute it from the
128
+ // header's own fields (the same way HeaderStore.append does for window
129
+ // headers) and require it to satisfy its own declared difficulty. This
130
+ // does not establish chain-of-custody back to a trusted anchor — it
131
+ // only turns a zero-cost forgery into one that needs a real,
132
+ // difficulty-valid header.
133
+ let computedHash: string
134
+ try {
135
+ computedHash = blockHash(header)
136
+ validateHeaderProofOfWork({ ...header, hash: computedHash })
137
+ } catch (e: any) {
138
+ console.warn(
139
+ `[OfflineFirstChaintracks] REFUSED height ${height}: chaintracks-reported header failed ` +
140
+ `proof-of-work validation (${e?.message ?? e}).`
141
+ )
142
+ this.lastMissHeight = height
143
+ return false
144
+ }
145
+
102
146
  const remoteRoot = rootHex(header.merkleRoot)
103
147
  // Refresh the cache with the authoritative value (self-heals a poisoned
104
148
  // extra entry from an earlier bad conversion).
@@ -146,17 +146,39 @@ export class HeaderStore {
146
146
  }
147
147
 
148
148
  rootForHeight(height: number): string | undefined {
149
- // A heal written to extra is the value to consult, even for a height the
150
- // window also covers — otherwise a logged heal is stored and then ignored.
149
+ const index = height - this.baseHeight
150
+ const inWindow = index >= 0 && index < this.headerCount
151
+ // The window's own PoW-linked root is authoritative for its validated BODY
152
+ // (every height except the last 6, which can still legitimately reorg): an
153
+ // extra entry there is ignored outright, never consulted, because letting
154
+ // it win is exactly how an unauthenticated remote answer could overwrite a
155
+ // root this device already validated for itself off linked headers — see
156
+ // misc-p2-02 / OfflineFirstChaintracks.isValidRootForHeight.
157
+ if (inWindow && !this.inReorgWindow(height)) {
158
+ return Utils.toHex(Array.from(this.roots.subarray(index * ROOT_BYTES, (index + 1) * ROOT_BYTES)))
159
+ }
160
+ // Outside the body — either the last-6 reorg tail, or a height the window
161
+ // does not cover at all — a heal written to extra is the value to consult;
162
+ // falling through to the window covers the tail case where no heal exists.
151
163
  const extra = this.extra[String(height)]
152
164
  if (extra !== undefined) return extra
153
- const index = height - this.baseHeight
154
- if (index >= 0 && index < this.headerCount) {
165
+ if (inWindow) {
155
166
  return Utils.toHex(Array.from(this.roots.subarray(index * ROOT_BYTES, (index + 1) * ROOT_BYTES)))
156
167
  }
157
168
  return undefined
158
169
  }
159
170
 
171
+ /**
172
+ * True when `height` sits in the window's validated BODY — inside
173
+ * `[baseHeight, tipHeight]` but outside the last-6 reorg tail — where the
174
+ * window's own root is authoritative and a network answer must never be
175
+ * allowed to override it. Used by `OfflineFirstChaintracks` to decide
176
+ * whether a mismatch may fall through to the remote at all.
177
+ */
178
+ isWindowBody(height: number): boolean {
179
+ return height >= this.baseHeight && height <= this.tipHeight - 6
180
+ }
181
+
160
182
  /**
161
183
  * Validates and appends a chunk. Returns the number of headers added.
162
184
  *
@@ -26,6 +26,9 @@ export interface SyncHeadersResult {
26
26
  /** Walk back at most this many headers before giving up and resetting. */
27
27
  const REWIND_CAP = 144
28
28
 
29
+ /** Bytes in one serialized block header. */
30
+ const HEADER_BYTES = 80
31
+
29
32
  function isFirstHeaderPrevHashError(err: unknown, firstHeight: number): boolean {
30
33
  const msg = err instanceof Error ? err.message : String(err)
31
34
  return /previous hash/i.test(msg) && msg.includes(`header at height ${firstHeight} `)
@@ -49,8 +52,16 @@ export async function syncHeaders(args: {
49
52
  const want = Math.min(chunkSize, presentHeight - store.tipHeight)
50
53
  const hex = await client.getHeaders(from, want)
51
54
  if (!hex) break
52
- const bytes = new Uint8Array(Utils.toArray(hex, 'hex'))
55
+ let bytes = new Uint8Array(Utils.toArray(hex, 'hex'))
53
56
  if (bytes.length === 0) break
57
+ // XR-060: each header still has to pass real PoW and chain-linkage
58
+ // validation below, so a compromised/misbehaving chaintracks endpoint
59
+ // cannot inject fabricated headers this way — but it could still hand
60
+ // back a far larger batch of genuine headers than `want`, forcing
61
+ // unbounded validation work and disk persistence in one call. Never
62
+ // accept more than was actually requested.
63
+ const maxBytes = want * HEADER_BYTES
64
+ if (bytes.length > maxBytes) bytes = bytes.subarray(0, maxBytes)
54
65
  try {
55
66
  added += await store.append(bytes, from)
56
67
  onProgress?.(store.tipHeight, presentHeight)