@bsv/expo-wallet-toolbox 0.22.0 → 0.23.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 (64) hide show
  1. package/CHANGELOG.md +108 -0
  2. package/README.md +9 -5
  3. package/core/bridge/BrowserBridgeProvider.tsx +273 -0
  4. package/core/bridge/bindings.ts +459 -0
  5. package/core/bridge/conformance.ts +222 -0
  6. package/core/bridge/consent.ts +494 -0
  7. package/core/bridge/consentSheetSource.ts +98 -0
  8. package/core/bridge/dispatcher.ts +621 -0
  9. package/core/bridge/inbox.ts +464 -0
  10. package/core/bridge/index.ts +48 -0
  11. package/core/bridge/loopback/cors.ts +57 -0
  12. package/core/bridge/loopback/endpoints.ts +11 -0
  13. package/core/bridge/loopback/hostCheck.ts +25 -0
  14. package/core/bridge/loopback/index.ts +7 -0
  15. package/core/bridge/loopback/launchProof.ts +166 -0
  16. package/core/bridge/loopback/router.ts +770 -0
  17. package/core/bridge/loopback/routerTypes.ts +115 -0
  18. package/core/bridge/loopback/status.ts +52 -0
  19. package/core/bridge/memoryInboxStore.ts +222 -0
  20. package/core/bridge/permissionPolicy.ts +247 -0
  21. package/core/bridge/policy.ts +72 -0
  22. package/core/bridge/protocol/base64url.ts +64 -0
  23. package/core/bridge/protocol/connectionError.ts +79 -0
  24. package/core/bridge/protocol/constants.ts +21 -0
  25. package/core/bridge/protocol/continuity.ts +21 -0
  26. package/core/bridge/protocol/descriptor.ts +395 -0
  27. package/core/bridge/protocol/envelopes.ts +188 -0
  28. package/core/bridge/protocol/errorCodec.ts +164 -0
  29. package/core/bridge/protocol/json.ts +237 -0
  30. package/core/bridge/protocol/limits.ts +43 -0
  31. package/core/bridge/protocol/methods.ts +55 -0
  32. package/core/bridge/protocol/origin.ts +130 -0
  33. package/core/bridge/runtime.ts +642 -0
  34. package/core/bridge/runtimeFlags.ts +34 -0
  35. package/core/bridge/testing/conformanceCases.ts +149 -0
  36. package/core/bridge/testing/pageClient.ts +44 -0
  37. package/core/bridge/unverifiedManager.ts +97 -0
  38. package/core/bridge/unverifiedOriginator.ts +79 -0
  39. package/core/config.tsx +1 -16
  40. package/core/context/WalletConnectionContext.tsx +7 -1
  41. package/core/context/WalletContext.tsx +258 -133
  42. package/core/context/sessionTeardown.ts +46 -0
  43. package/core/hooks/usePermissionQueue.ts +89 -34
  44. package/core/i18n/translations.tsx +911 -12
  45. package/core/index.ts +15 -0
  46. package/core/services/autoApprovePolicy.ts +7 -1
  47. package/core/services/capWalletArgs.ts +2 -1
  48. package/core/services/permissionPrompts.ts +132 -0
  49. package/core/services/signingPermissionPolicy.ts +230 -2
  50. package/core/services/vault/guard.ts +10 -0
  51. package/core/services/walletArgLimits.ts +17 -2
  52. package/core/storage/encryptedDbImage.ts +325 -0
  53. package/core/toolboxConfig.ts +18 -1
  54. package/package.json +6 -6
  55. package/ui/bridgeSheet/ContextRelay.tsx +86 -0
  56. package/ui/bridgeSheet/relayedContexts.ts +21 -0
  57. package/ui/components/ui/PermissionSheet.tsx +534 -158
  58. package/ui/components/ui/Sheet.tsx +59 -7
  59. package/ui/components/ui/sheetResumeSignal.ts +19 -0
  60. package/ui/exportDatabases.ts +94 -5
  61. package/ui/hooks/useExportWalletData.ts +37 -24
  62. package/ui/importDatabases.ts +123 -28
  63. package/ui/index.ts +4 -0
  64. package/ui/screens/WalletConfigScreen.tsx +13 -1
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Session teardown registry. WalletContext owns the moments a wallet session
3
+ * ends (logout, profile switch, network switch, same-wallet rebuild) but sits
4
+ * above the pieces that hold live per-session state (the paired RPC relay, the
5
+ * browser bridge). Those register here instead of WalletContext importing each.
6
+ *
7
+ * Plain module state, like the single-session handles it replaces: exactly one
8
+ * wallet session exists per process.
9
+ */
10
+
11
+ export type TeardownReason = 'logout' | 'profile' | 'network' | 'rebuild'
12
+
13
+ type TeardownFn = (reason: TeardownReason) => void
14
+
15
+ interface Entry {
16
+ reasons: ReadonlySet<TeardownReason>
17
+ fn: TeardownFn
18
+ }
19
+
20
+ const entries = new Set<Entry>()
21
+
22
+ /** Register `fn` for the given reasons. Returns an unregister function. */
23
+ export function registerSessionTeardown(reasons: TeardownReason[], fn: TeardownFn): () => void {
24
+ const entry: Entry = { reasons: new Set(reasons), fn }
25
+ entries.add(entry)
26
+ return () => {
27
+ entries.delete(entry)
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Run every registered fn whose reasons include `reason`. Synchronous: callers
33
+ * rely on teardown having happened before their next await. A throwing fn never
34
+ * stops the others.
35
+ */
36
+ export function runSessionTeardown(reason: TeardownReason): void {
37
+ // Snapshot: a fn may unregister itself (or others) while running.
38
+ for (const entry of [...entries]) {
39
+ if (!entry.reasons.has(reason)) continue
40
+ try {
41
+ entry.fn(reason)
42
+ } catch (e) {
43
+ console.warn(`[sessionTeardown] ${reason} handler failed`, e)
44
+ }
45
+ }
46
+ }
@@ -1,69 +1,124 @@
1
1
  import { useState, useCallback, useRef } from 'react'
2
2
 
3
- interface UsePermissionQueueOptions {
3
+ interface UsePermissionQueueOptions<T = unknown> {
4
4
  /** Open/close the associated modal. Omit for queues without a modal (e.g. BTMS). */
5
5
  openModal?: (open: boolean) => void
6
6
  isFocused: () => Promise<boolean>
7
7
  onFocusRequested: () => void | Promise<void>
8
8
  onFocusRelinquished: () => void | Promise<void>
9
+ /** Called synchronously for every item added, whether or not the queue was empty. */
10
+ onEnqueue?: (item: T) => void
9
11
  }
10
12
 
11
13
  interface PermissionQueue<T> {
12
14
  requests: T[]
13
15
  /** Add an item to the queue. Handles focus request and modal open on first item. */
14
16
  enqueue: (item: T) => void
15
- /** Remove the first item. Closes modal and relinquishes focus when empty. */
16
- advance: (onAdvance?: (head: T) => void) => void
17
+ /**
18
+ * Remove the first item, or with `requestID` only the item carrying that ID
19
+ * (and nothing at all if it is no longer queued). Closes modal and
20
+ * relinquishes focus when empty.
21
+ */
22
+ advance: (onAdvance?: (removed: T) => void, requestID?: string) => void
23
+ /** Remove the item with this `requestID`. Returns whether it was queued. */
24
+ removeById: (requestID: string) => boolean
25
+ /** Drop every item (handing each to `onEach`), close the modal and give focus back. */
26
+ clear: (onEach?: (item: T) => void) => void
17
27
  }
18
28
 
29
+ const idOf = (item: unknown): string | undefined => (item as { requestID?: string } | null)?.requestID
30
+
19
31
  /**
20
32
  * Generic permission request queue with focus management.
21
33
  *
22
34
  * Deduplicates the identical queue logic used by basket, certificate,
23
35
  * protocol, spending, and BTMS permission systems.
24
36
  */
25
- export function usePermissionQueue<T>(opts: UsePermissionQueueOptions): PermissionQueue<T> {
26
- const { openModal, isFocused, onFocusRequested, onFocusRelinquished } = opts
37
+ export function usePermissionQueue<T>(opts: UsePermissionQueueOptions<T>): PermissionQueue<T> {
38
+ const { openModal, isFocused, onFocusRequested, onFocusRelinquished, onEnqueue } = opts
27
39
  const [requests, setRequests] = useState<T[]>([])
40
+ // The queue's current contents, updated in the same step as the state, so a
41
+ // caller (a withdrawal, an answer by ID) can tell synchronously whether an
42
+ // item is still queued without waiting for a render.
43
+ const itemsRef = useRef<T[]>([])
28
44
  const wasOriginallyFocusedRef = useRef(false)
45
+ // Which "first item" the deferred focus/modal step belongs to. A clear or an advance to empty
46
+ // moves it on, so a step that was scheduled for a queue that has since been emptied does nothing.
47
+ const openGenRef = useRef(0)
48
+
49
+ const replace = useCallback((next: T[]) => {
50
+ itemsRef.current = next
51
+ setRequests(next)
52
+ }, [])
53
+
54
+ const emptied = useCallback(() => {
55
+ openGenRef.current += 1
56
+ openModal?.(false)
57
+ if (!wasOriginallyFocusedRef.current) onFocusRelinquished()
58
+ }, [openModal, onFocusRelinquished])
29
59
 
30
60
  const enqueue = useCallback(
31
61
  (item: T) => {
32
- // Read length before updating so the side-effect runs outside the updater.
33
- // React may double-invoke updaters in Strict Mode; this avoids double focus requests.
34
- setRequests(prev => {
35
- if (prev.length === 0) {
36
- // Schedule focus/modal on next microtask, outside the updater
37
- Promise.resolve().then(() => {
38
- isFocused().then(currentlyFocused => {
39
- wasOriginallyFocusedRef.current = currentlyFocused
40
- if (!currentlyFocused) onFocusRequested()
41
- openModal?.(true)
42
- })
62
+ const wasEmpty = itemsRef.current.length === 0
63
+ replace([...itemsRef.current, item])
64
+ try {
65
+ onEnqueue?.(item)
66
+ } catch (e) {
67
+ console.warn('[permissionQueue] onEnqueue failed', e)
68
+ }
69
+ if (wasEmpty) {
70
+ // Schedule focus/modal on next microtask, after the state is set. Both steps re-check
71
+ // that this queue is still the one that was filled: a withdrawal in between has already
72
+ // closed the modal and given focus back, and must not be undone.
73
+ const gen = ++openGenRef.current
74
+ const stale = () => gen !== openGenRef.current || itemsRef.current.length === 0
75
+ Promise.resolve().then(() => {
76
+ if (stale()) return
77
+ isFocused().then(currentlyFocused => {
78
+ if (stale()) return
79
+ wasOriginallyFocusedRef.current = currentlyFocused
80
+ if (!currentlyFocused) onFocusRequested()
81
+ openModal?.(true)
43
82
  })
44
- }
45
- return [...prev, item]
46
- })
83
+ })
84
+ }
47
85
  },
48
- [isFocused, onFocusRequested, openModal]
86
+ [isFocused, onFocusRequested, openModal, replace, onEnqueue]
49
87
  )
50
88
 
51
89
  const advance = useCallback(
52
- (onAdvance?: (head: T) => void) => {
53
- setRequests(prev => {
54
- if (prev.length > 0 && onAdvance) {
55
- onAdvance(prev[0])
56
- }
57
- const newQueue = prev.slice(1)
58
- if (newQueue.length === 0) {
59
- openModal?.(false)
60
- if (!wasOriginallyFocusedRef.current) onFocusRelinquished()
61
- }
62
- return newQueue
63
- })
90
+ (onAdvance?: (removed: T) => void, requestID?: string) => {
91
+ const prev = itemsRef.current
92
+ const index = requestID === undefined ? (prev.length > 0 ? 0 : -1) : prev.findIndex(i => idOf(i) === requestID)
93
+ if (index < 0) return
94
+ const removed = prev[index]
95
+ onAdvance?.(removed)
96
+ const next = prev.filter((_, i) => i !== index)
97
+ replace(next)
98
+ if (next.length === 0) emptied()
99
+ },
100
+ [emptied, replace]
101
+ )
102
+
103
+ const removeById = useCallback(
104
+ (requestID: string): boolean => {
105
+ if (!itemsRef.current.some(i => idOf(i) === requestID)) return false
106
+ advance(undefined, requestID)
107
+ return true
108
+ },
109
+ [advance]
110
+ )
111
+
112
+ const clear = useCallback(
113
+ (onEach?: (item: T) => void) => {
114
+ const prev = itemsRef.current
115
+ if (prev.length === 0) return
116
+ replace([])
117
+ for (const item of prev) onEach?.(item)
118
+ emptied()
64
119
  },
65
- [openModal, onFocusRelinquished]
120
+ [emptied, replace]
66
121
  )
67
122
 
68
- return { requests, enqueue, advance }
123
+ return { requests, enqueue, advance, removeById, clear }
69
124
  }