@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.
- package/CHANGELOG.md +108 -0
- package/README.md +9 -5
- package/core/bridge/BrowserBridgeProvider.tsx +273 -0
- package/core/bridge/bindings.ts +459 -0
- package/core/bridge/conformance.ts +222 -0
- package/core/bridge/consent.ts +494 -0
- package/core/bridge/consentSheetSource.ts +98 -0
- package/core/bridge/dispatcher.ts +621 -0
- package/core/bridge/inbox.ts +464 -0
- package/core/bridge/index.ts +48 -0
- package/core/bridge/loopback/cors.ts +57 -0
- package/core/bridge/loopback/endpoints.ts +11 -0
- package/core/bridge/loopback/hostCheck.ts +25 -0
- package/core/bridge/loopback/index.ts +7 -0
- package/core/bridge/loopback/launchProof.ts +166 -0
- package/core/bridge/loopback/router.ts +770 -0
- package/core/bridge/loopback/routerTypes.ts +115 -0
- package/core/bridge/loopback/status.ts +52 -0
- package/core/bridge/memoryInboxStore.ts +222 -0
- package/core/bridge/permissionPolicy.ts +247 -0
- package/core/bridge/policy.ts +72 -0
- package/core/bridge/protocol/base64url.ts +64 -0
- package/core/bridge/protocol/connectionError.ts +79 -0
- package/core/bridge/protocol/constants.ts +21 -0
- package/core/bridge/protocol/continuity.ts +21 -0
- package/core/bridge/protocol/descriptor.ts +395 -0
- package/core/bridge/protocol/envelopes.ts +188 -0
- package/core/bridge/protocol/errorCodec.ts +164 -0
- package/core/bridge/protocol/json.ts +237 -0
- package/core/bridge/protocol/limits.ts +43 -0
- package/core/bridge/protocol/methods.ts +55 -0
- package/core/bridge/protocol/origin.ts +130 -0
- package/core/bridge/runtime.ts +642 -0
- package/core/bridge/runtimeFlags.ts +34 -0
- package/core/bridge/testing/conformanceCases.ts +149 -0
- package/core/bridge/testing/pageClient.ts +44 -0
- package/core/bridge/unverifiedManager.ts +97 -0
- package/core/bridge/unverifiedOriginator.ts +79 -0
- package/core/config.tsx +1 -16
- package/core/context/WalletConnectionContext.tsx +7 -1
- package/core/context/WalletContext.tsx +258 -133
- package/core/context/sessionTeardown.ts +46 -0
- package/core/hooks/usePermissionQueue.ts +89 -34
- package/core/i18n/translations.tsx +911 -12
- package/core/index.ts +15 -0
- package/core/services/autoApprovePolicy.ts +7 -1
- package/core/services/capWalletArgs.ts +2 -1
- package/core/services/permissionPrompts.ts +132 -0
- package/core/services/signingPermissionPolicy.ts +230 -2
- package/core/services/vault/guard.ts +10 -0
- package/core/services/walletArgLimits.ts +17 -2
- package/core/storage/encryptedDbImage.ts +325 -0
- package/core/toolboxConfig.ts +18 -1
- package/package.json +6 -6
- package/ui/bridgeSheet/ContextRelay.tsx +86 -0
- package/ui/bridgeSheet/relayedContexts.ts +21 -0
- package/ui/components/ui/PermissionSheet.tsx +534 -158
- package/ui/components/ui/Sheet.tsx +59 -7
- package/ui/components/ui/sheetResumeSignal.ts +19 -0
- package/ui/exportDatabases.ts +94 -5
- package/ui/hooks/useExportWalletData.ts +37 -24
- package/ui/importDatabases.ts +123 -28
- package/ui/index.ts +4 -0
- 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
|
-
/**
|
|
16
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
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?: (
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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
|
-
[
|
|
120
|
+
[emptied, replace]
|
|
66
121
|
)
|
|
67
122
|
|
|
68
|
-
return { requests, enqueue, advance }
|
|
123
|
+
return { requests, enqueue, advance, removeById, clear }
|
|
69
124
|
}
|