@bsv/expo-wallet-toolbox 0.20.0 → 0.22.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 (85) hide show
  1. package/CHANGELOG.md +147 -0
  2. package/README.md +78 -13
  3. package/core/contacts/contactActivity.ts +48 -28
  4. package/core/contacts/contactsStore.ts +2 -2
  5. package/core/context/WalletContext.tsx +166 -219
  6. package/core/demo/demoAssets.ts +8 -10
  7. package/core/demo/demoLedger.ts +32 -38
  8. package/core/demo/demoMandalaRuntime.ts +67 -49
  9. package/core/demo/index.ts +1 -1
  10. package/core/directpay/DirectPayManager.ts +148 -29
  11. package/core/directpay/DirectPayProvider.tsx +11 -2
  12. package/core/directpay/payFlow.ts +10 -1
  13. package/core/directpay/settings.ts +56 -9
  14. package/core/directpay/types.ts +11 -2
  15. package/core/directpay/webrtpay.ts +4 -0
  16. package/core/i18n/translations.tsx +185 -84
  17. package/core/index.ts +55 -37
  18. package/core/localpay/build.ts +110 -569
  19. package/core/localpay/codec.ts +190 -93
  20. package/core/localpay/pending.ts +217 -202
  21. package/core/localpay/session.ts +99 -25
  22. package/core/localpay/types.ts +6 -5
  23. package/core/localpay/verify.ts +118 -173
  24. package/core/mandala/abortGuard.ts +97 -120
  25. package/core/mandala/accessPolicy.ts +133 -0
  26. package/core/mandala/assets.ts +370 -0
  27. package/core/mandala/colorGuard.ts +178 -0
  28. package/core/mandala/colorModule.ts +82 -0
  29. package/core/mandala/createRuntime.ts +483 -2392
  30. package/core/mandala/handle.ts +694 -0
  31. package/core/mandala/kvStore.ts +105 -0
  32. package/core/mandala/nearby.ts +185 -0
  33. package/core/mandala/receipt.ts +121 -0
  34. package/core/mandala/runtime.ts +166 -312
  35. package/core/mandala/settle.ts +540 -0
  36. package/core/mandala/transferJournal.ts +395 -0
  37. package/core/mandala/types.ts +61 -311
  38. package/core/networks.ts +5 -5
  39. package/core/offline/cancelParked.ts +80 -63
  40. package/core/offline/payerHold.ts +52 -118
  41. package/core/parsePeerPayURI.ts +25 -12
  42. package/core/pay/rails/direct.ts +6 -2
  43. package/core/pay/rails/handle.ts +15 -14
  44. package/core/pay/rails/index.ts +5 -3
  45. package/core/pay/rails/nearby.ts +25 -87
  46. package/core/profiles/assertProfileEmpty.ts +19 -10
  47. package/core/services/vault/guard.ts +5 -4
  48. package/core/storage/StorageExpoSQLite.ts +93 -229
  49. package/core/storage/methods/processOfflineActions.ts +86 -136
  50. package/core/storage/schema/createTables.ts +9 -184
  51. package/package.json +6 -5
  52. package/ui/components/pay/AssetPicker.tsx +2 -2
  53. package/ui/components/pay/HandleReceive.tsx +10 -4
  54. package/ui/components/pay/NearbyFlow.tsx +324 -239
  55. package/ui/components/pay/RequestHub.tsx +10 -0
  56. package/ui/components/pay/UniversalSend.tsx +31 -28
  57. package/ui/components/pay/useRecipientInput.ts +2 -2
  58. package/ui/components/ui/PermissionSheet.tsx +94 -160
  59. package/ui/components/ui/colorPrompt.ts +236 -0
  60. package/ui/components/wallet/AmountInput.tsx +9 -6
  61. package/ui/components/wallet/AssetAmount.tsx +6 -6
  62. package/ui/components/wallet/AssetSwitcherDropdown.tsx +26 -7
  63. package/ui/components/wallet/Balance.tsx +3 -1
  64. package/ui/components/wallet/ContactActivityList.tsx +10 -13
  65. package/ui/components/wallet/tokenEviction.ts +9 -9
  66. package/ui/hooks/useMandala.ts +6 -6
  67. package/ui/hooks/useSpendableBalance.ts +3 -1
  68. package/ui/importDatabases.ts +1 -1
  69. package/ui/screens/PayScreen.tsx +14 -20
  70. package/ui/screens/SettingsScreen.tsx +3 -1
  71. package/ui/screens/WalletCheckScreen.tsx +7 -2
  72. package/ui/screens/WalletHomeScreen.tsx +40 -14
  73. package/ui/screens/tokenRowTitle.ts +28 -15
  74. package/ui/tokenFormat.ts +133 -69
  75. package/ui/tokenStatus.ts +5 -8
  76. package/core/localpay/settlementAck.ts +0 -155
  77. package/core/mandala/basketMigration.ts +0 -166
  78. package/core/mandala/bundle.ts +0 -244
  79. package/core/mandala/drain.ts +0 -754
  80. package/core/mandala/inboxPrehold.ts +0 -193
  81. package/core/mandala/permissionModule.ts +0 -931
  82. package/core/mandala/resendTransfer.ts +0 -178
  83. package/core/mandala/settlementStore.ts +0 -434
  84. package/core/mandala/token.ts +0 -45
  85. package/core/offline/tokenFrames.ts +0 -197
@@ -1,197 +0,0 @@
1
- /**
2
- * Turning the frames this device already holds durably into drain sources.
3
- *
4
- * This is the supply side of FIX G and FIX I. `processOfflineActions` calls
5
- * `token.frames()` at the start of every pass and hands the result to
6
- * `reconcileSettlements`, which re-derives the evidence cache and any missing
7
- * `token_settlements` row. The two durable stores have very different shapes,
8
- * so each gets its own reader:
9
- *
10
- * · `localpay_pending` (receiver) holds the frame ALREADY DECODED, so nothing
11
- * is needed but the queue entries.
12
- * · `offline_actions.framePayload` (payer) holds the SEALED `bsvpayf1:` QR
13
- * string. Opening it needs the session pre-shared key, which the drain does
14
- * not hold and should not — so the decoder is injected by whoever still has
15
- * the session, and a row that cannot be opened is simply skipped. The payer
16
- * losing its own settlement row is survivable in a way the recipient's is
17
- * not: the recipient submits (rule 3), and the payer's optional submit
18
- * (rule 6) is a race it is always safe to lose.
19
- *
20
- * **Deliberately NOT filtered by `MAX_PENDING_ATTEMPTS` (FIX I).** That ceiling
21
- * governs `localpay_pending`'s own BSV-shaped internalize retry loop and burns
22
- * an attempt on any failure, including a transient
23
- * `Block header not found for height N` from a fresh-block BUMP this device has
24
- * not caught up on. A token payment's state of record is `token_settlements`,
25
- * not the pending queue — so an exhausted queue entry still contributes its
26
- * frame here, the settlement row stays alive, and the row is abandoned only by
27
- * an explicit `refused`/`orphaned` verdict from the overlay.
28
- */
29
- import { Beef, Transaction } from '@bsv/sdk'
30
- import { MandalaToken } from '../mandala/token'
31
- import type { EvidenceFrame, TokenFrameSource } from '../mandala/drain'
32
- import type { OfflineActionRow } from '../storage/methods/offlineActions'
33
- import { frameBytesFromQr, unsealFrame } from '../localpay/codec'
34
-
35
- /** Structurally satisfied by `localpay/pending`'s `PendingPayment`. */
36
- export interface PendingLike {
37
- frame: EvidenceFrame
38
- status?: string
39
- }
40
-
41
- /**
42
- * The subject txid of an AtomicBEEF — the same txid `internalizeAction` used
43
- * internally, and therefore the one a `token_settlements` row is keyed by.
44
- */
45
- /**
46
- * The token figure a frame moves: the amount in the PAYEE output's own script.
47
- *
48
- * Read off the atomic transaction rather than trusted from any body, and off
49
- * `outputIndex` (0 when the frame does not say — the nearby build never
50
- * randomises outputs) so the payer's own frame answers the payee's figure and
51
- * never the change. `undefined` for anything that is not a readable token
52
- * output: the settlement row then has no amount, which the activity list says
53
- * out loud rather than inventing a number.
54
- */
55
- export function frameTokenAmount(frame: EvidenceFrame): number | undefined {
56
- try {
57
- const tx = Transaction.fromAtomicBEEF(Array.from(frame.transaction))
58
- const script = tx.outputs[frame.outputIndex ?? 0]?.lockingScript
59
- if (!script) return undefined
60
- const amount = MandalaToken.decode(script).amount
61
- return Number.isFinite(amount) && amount >= 0 ? amount : undefined
62
- } catch {
63
- return undefined
64
- }
65
- }
66
-
67
- export function frameTxid(frame: EvidenceFrame): string | undefined {
68
- try {
69
- return Beef.fromBinary(frame.transaction).atomicTxid
70
- } catch {
71
- return undefined
72
- }
73
- }
74
-
75
- /**
76
- * Receiver-side sources. Every non-completed entry with a token block, whether
77
- * or not the pending queue has given up on it.
78
- */
79
- export function tokenFrameSourcesFromPending(entries: readonly PendingLike[]): TokenFrameSource[] {
80
- const sources: TokenFrameSource[] = []
81
- for (const entry of entries) {
82
- if (!entry.frame?.token) continue
83
- const txid = frameTxid(entry.frame)
84
- if (!txid) continue
85
- const amountBaseUnits = frameTokenAmount(entry.frame)
86
- sources.push({
87
- txid,
88
- role: 'received',
89
- frame: entry.frame,
90
- ...(amountBaseUnits !== undefined ? { amountBaseUnits } : {})
91
- })
92
- }
93
- return sources
94
- }
95
-
96
- /**
97
- * Payer-side sources, for the rows whose sealed `framePayload` the caller can
98
- * open. A row this device cannot decode contributes nothing rather than
99
- * throwing: the drain must reach every other row in the same pass.
100
- */
101
- export function tokenFrameSourcesFromOfflineActions(
102
- rows: readonly OfflineActionRow[],
103
- decode: (framePayload: string) => EvidenceFrame | undefined
104
- ): TokenFrameSource[] {
105
- const sources: TokenFrameSource[] = []
106
- for (const row of rows) {
107
- if (!row.framePayload) continue
108
- let frame: EvidenceFrame | undefined
109
- try {
110
- frame = decode(row.framePayload)
111
- } catch {
112
- continue
113
- }
114
- if (!frame?.token) continue
115
- const amountBaseUnits = frameTokenAmount(frame)
116
- sources.push({
117
- txid: row.txid,
118
- role: row.role,
119
- frame,
120
- // A parked row must keep its own state: it is the user's, not the
121
- // drain's, and reconciliation may not promote it to a submittable one.
122
- state: row.status === 'parked' ? 'parked' : undefined,
123
- ...(amountBaseUnits !== undefined ? { amountBaseUnits } : {})
124
- })
125
- }
126
- return sources
127
- }
128
-
129
- // ───────────────────── the payer's sealed-frame decoder ─────────────────────
130
- //
131
- // `offline_actions.framePayload` is the `bsvpayf1:` envelope around a frame
132
- // SEALED with the nearby session's pre-shared key. The drain holds no session
133
- // and should not: the PSK is a hand-over secret, not durable wallet state. So
134
- // the keys of the sessions this process has actually taken part in are kept
135
- // here, in memory only, and the decoder tries each of them.
136
- //
137
- // What "in memory only" costs is bounded and known: a payer that restarts
138
- // before its own optional submit (rule 6) loses the ability to re-derive its
139
- // `token_settlements` row from the frame. That is the race it is always safe
140
- // to lose — the RECIPIENT submits (rule 3), and the payer's submit only makes
141
- // its own change spendable sooner. What it buys is that a durable copy of the
142
- // key that opens every stored frame is never written to disk.
143
-
144
- /** How many session keys are remembered. Well past any plausible number of live hand-overs. */
145
- export const MAX_REMEMBERED_SESSION_KEYS = 32
146
-
147
- const sessionKeys: Uint8Array[] = []
148
-
149
- /**
150
- * Remember a nearby session's PSK for this process.
151
- *
152
- * Called by whoever mints or scans a session (the nearby flow), not by the
153
- * drain. Idempotent per key, and bounded: the oldest key is dropped once the
154
- * ring is full, which is correct because a hand-over's frame is reconciled on
155
- * the very next online tick.
156
- */
157
- export function rememberSessionPsk(psk: Uint8Array): void {
158
- if (!(psk instanceof Uint8Array) || psk.length !== 32) return
159
- const already = sessionKeys.some(known => known.length === psk.length && known.every((b, i) => b === psk[i]))
160
- if (already) return
161
- sessionKeys.push(new Uint8Array(psk))
162
- while (sessionKeys.length > MAX_REMEMBERED_SESSION_KEYS) sessionKeys.shift()
163
- }
164
-
165
- /** Drop every remembered key. Logout, network switch, and the tests. */
166
- export function forgetSessionPsks(): void {
167
- sessionKeys.length = 0
168
- }
169
-
170
- /**
171
- * A decoder for `tokenFrameSourcesFromOfflineActions`.
172
- *
173
- * Tries each remembered session key in turn and returns the first frame that
174
- * opens. Undefined — never a throw — for a truncated row, a foreign string, or
175
- * a frame from a session this process no longer holds the key for: the drain
176
- * must reach every other row in the same pass.
177
- */
178
- export function sealedFramePayloadDecoder(
179
- keys: () => readonly Uint8Array[] = () => sessionKeys
180
- ): (framePayload: string) => EvidenceFrame | undefined {
181
- return framePayload => {
182
- let bytes: Uint8Array
183
- try {
184
- bytes = frameBytesFromQr(framePayload)
185
- } catch {
186
- return undefined
187
- }
188
- for (const psk of keys()) {
189
- try {
190
- return unsealFrame(bytes, psk)
191
- } catch {
192
- // Wrong key for this frame (a GCM tag mismatch). Try the next.
193
- }
194
- }
195
- return undefined
196
- }
197
- }