@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,166 @@
1
+ /**
2
+ * BRC-185 Android loopback launch proof (spec 7.5): the MACs under the per-launch secret `k` and the
3
+ * launch URI codec. Pure and synchronous; depends only on `@bsv/sdk` HMAC and the protocol codecs, so it
4
+ * bundles into the page-side stand-in client as well as the wallet.
5
+ *
6
+ * Every output is HMAC-SHA-256 under `k` with its own label, so no MAC can serve as an oracle for another.
7
+ * `k`, the epoch and every output here are secret-like: callers must never log them.
8
+ */
9
+ import { Hash } from '@bsv/sdk'
10
+ import { b64uDecode, b64uEncode } from '../protocol/base64url'
11
+ import { canonicalize } from '../protocol/json'
12
+
13
+ const encoder = new TextEncoder()
14
+
15
+ const LABEL_HELLO = 'BRC-185 android hello v1\n'
16
+ const LABEL_DESCRIBE_ACK = 'BRC-185 android describe ack v1\n'
17
+ const LABEL_LAUNCH = 'BRC-185 android launch v1\n'
18
+ const LABEL_ACK = 'BRC-185 android ack v1\n'
19
+ const LABEL_BINDING = 'BRC-185 android binding v1\n'
20
+
21
+ /** The wallet's identity for one origin (spec 6.9). */
22
+ export interface LaunchWallet {
23
+ instanceId: string
24
+ continuityKey: string
25
+ }
26
+
27
+ export interface LaunchMessage {
28
+ wallet: LaunchWallet
29
+ epoch: string
30
+ origin: string
31
+ challenge: string
32
+ host: string
33
+ nonce: string
34
+ }
35
+
36
+ function mac(k: Uint8Array, label: string, payload: unknown): string {
37
+ const data = Array.from(encoder.encode(label + canonicalize(payload)))
38
+ return b64uEncode(Uint8Array.from(Hash.sha256hmac(Array.from(k), data)))
39
+ }
40
+
41
+ function walletPart(w: LaunchWallet): LaunchWallet {
42
+ return { instanceId: w.instanceId, continuityKey: w.continuityKey }
43
+ }
44
+
45
+ function message(m: LaunchMessage): Record<string, unknown> {
46
+ return {
47
+ v: 1,
48
+ wallet: walletPart(m.wallet),
49
+ epoch: m.epoch,
50
+ origin: m.origin,
51
+ challenge: m.challenge,
52
+ host: m.host,
53
+ nonce: m.nonce
54
+ }
55
+ }
56
+
57
+ export function helloMac(k: Uint8Array, p: { origin: string; challenge: string; host: string }): string {
58
+ return mac(k, LABEL_HELLO, { v: 1, origin: p.origin, challenge: p.challenge, host: p.host })
59
+ }
60
+
61
+ export function describeAck(
62
+ k: Uint8Array,
63
+ p: { wallet: LaunchWallet; origin: string; challenge: string; host: string; nonce: string }
64
+ ): string {
65
+ return mac(k, LABEL_DESCRIBE_ACK, {
66
+ v: 1,
67
+ wallet: walletPart(p.wallet),
68
+ origin: p.origin,
69
+ challenge: p.challenge,
70
+ host: p.host,
71
+ nonce: p.nonce
72
+ })
73
+ }
74
+
75
+ /** Body member `android.launchProof` of the connect request. */
76
+ export function launchProof(k: Uint8Array, m: LaunchMessage): string {
77
+ return mac(k, LABEL_LAUNCH, message(m))
78
+ }
79
+
80
+ /** Response header `BRC-185-Android-Ack` of a successful connect. */
81
+ export function connectAck(k: Uint8Array, m: LaunchMessage): string {
82
+ return mac(k, LABEL_ACK, message(m))
83
+ }
84
+
85
+ /** Never sent: both sides derive it and the page presents it as `BRC-185-Android-Binding`. */
86
+ export function bindingSecret(k: Uint8Array, m: LaunchMessage): string {
87
+ return mac(k, LABEL_BINDING, message(m))
88
+ }
89
+
90
+ /**
91
+ * Constant-time comparison of two base64url strings. A length difference returns false immediately
92
+ * (lengths of MAC outputs are public); equal-length strings are compared without early exit.
93
+ */
94
+ export function timingSafeEqualB64u(a: string, b: string): boolean {
95
+ if (typeof a !== 'string' || typeof b !== 'string' || a.length !== b.length) return false
96
+ let diff = 0
97
+ for (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i)
98
+ return diff === 0
99
+ }
100
+
101
+ const MAX_URI_BYTES = 512
102
+ const EPOCH_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/
103
+ const B64U_RE = /^[A-Za-z0-9_-]+$/
104
+
105
+ function canonicalBytes(s: string, length: number): Uint8Array | undefined {
106
+ if (!B64U_RE.test(s)) return undefined
107
+ try {
108
+ const bytes = b64uDecode(s)
109
+ return bytes.length === length ? bytes : undefined
110
+ } catch {
111
+ return undefined
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Parse the plain URI the trampoline receives, `<scheme>://launch?v=1&e=&k=&n=`. Returns undefined for
117
+ * anything off-shape: wrong scheme or host, a fragment, a percent escape, an unknown or duplicate
118
+ * parameter, `v` other than `1`, an epoch that is not a lowercase UUIDv4, `k` not 32 and `n` not 16
119
+ * canonical base64url bytes, or more than 512 bytes.
120
+ */
121
+ export function parseLaunchUri(uri: string, scheme: string): { epoch: string; k: Uint8Array; n: string } | undefined {
122
+ if (typeof uri !== 'string' || encoder.encode(uri).length > MAX_URI_BYTES) return undefined
123
+ const prefix = `${scheme}://launch?`
124
+ if (!uri.startsWith(prefix)) return undefined
125
+ const query = uri.slice(prefix.length)
126
+ if (query.includes('#') || query.includes('%')) return undefined
127
+ const seen = new Map<string, string>()
128
+ for (const part of query.split('&')) {
129
+ const eq = part.indexOf('=')
130
+ if (eq <= 0) return undefined
131
+ const name = part.slice(0, eq)
132
+ if (name !== 'v' && name !== 'e' && name !== 'k' && name !== 'n') return undefined
133
+ if (seen.has(name)) return undefined
134
+ seen.set(name, part.slice(eq + 1))
135
+ }
136
+ if (seen.size !== 4 || seen.get('v') !== '1') return undefined
137
+ const epoch = seen.get('e')!
138
+ if (!EPOCH_RE.test(epoch)) return undefined
139
+ const kBytes = canonicalBytes(seen.get('k')!, 32)
140
+ const n = seen.get('n')!
141
+ if (!kBytes || !canonicalBytes(n, 16)) return undefined
142
+ return { epoch, k: kBytes, n }
143
+ }
144
+
145
+ /** The plain URI the trampoline receives. Parameter order is fixed. */
146
+ export function buildLaunchUri(o: { scheme: string; epoch: string; k: Uint8Array; n: string }): string {
147
+ if (!EPOCH_RE.test(o.epoch)) throw new Error('launch epoch must be a lowercase UUIDv4')
148
+ if (o.k.length !== 32) throw new Error('launch secret must be 32 bytes')
149
+ if (!canonicalBytes(o.n, 16)) throw new Error('launch nonce must be 16 canonical base64url bytes')
150
+ return `${o.scheme}://launch?v=1&e=${o.epoch}&k=${b64uEncode(o.k)}&n=${o.n}`
151
+ }
152
+
153
+ /** Page side: the `intent://` link Chrome rewrites into {@link buildLaunchUri}'s form. */
154
+ export function buildIntentLink(opts: {
155
+ scheme: string
156
+ packageId: string
157
+ epoch: string
158
+ k: Uint8Array
159
+ n: string
160
+ fallbackUrl?: string
161
+ }): string {
162
+ const plain = buildLaunchUri({ scheme: opts.scheme, epoch: opts.epoch, k: opts.k, n: opts.n })
163
+ const rest = plain.slice(`${opts.scheme}://`.length)
164
+ const fallback = opts.fallbackUrl ? `S.browser_fallback_url=${encodeURIComponent(opts.fallbackUrl)};` : ''
165
+ return `intent://${rest}#Intent;scheme=${opts.scheme};package=${opts.packageId};${fallback}end`
166
+ }