@bsv/expo-wallet-toolbox 0.4.0 → 0.5.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 +292 -0
- package/README.md +0 -1
- package/assets/sounds/vault-deposit.wav +0 -0
- package/assets/sounds/vault-withdraw.wav +0 -0
- package/core/config.tsx +9 -1
- package/core/context/VaultContext.tsx +8 -11
- package/core/context/WalletConnectionContext.tsx +137 -52
- package/core/context/WalletContext.tsx +409 -20
- package/core/hooks/useConfirmationSound.ts +14 -8
- package/core/i18n/translations.tsx +3368 -952
- package/core/index.ts +97 -14
- package/core/localpay/build.ts +674 -37
- package/core/localpay/codec.ts +68 -5
- package/core/localpay/pending.ts +240 -22
- package/core/localpay/settlementAck.ts +149 -0
- package/core/localpay/types.ts +14 -1
- package/core/localpay/verify.ts +118 -69
- package/core/mandala/abortGuard.ts +134 -0
- package/core/mandala/basketMigration.ts +166 -0
- package/core/mandala/bundle.ts +244 -0
- package/core/mandala/createRuntime.ts +2437 -0
- package/core/mandala/drain.ts +670 -0
- package/core/mandala/permissionModule.ts +642 -0
- package/core/mandala/resendTransfer.ts +175 -0
- package/core/mandala/runtime.ts +357 -0
- package/core/mandala/settlementStore.ts +412 -0
- package/core/mandala/types.ts +290 -0
- package/core/offline/cancelParked.ts +94 -1
- package/core/offline/payerHold.ts +152 -12
- package/core/offline/tokenFrames.ts +197 -0
- package/core/parsePeerPayURI.ts +70 -17
- package/core/pay/rails/address.ts +51 -3
- package/core/pay/rails/handle.ts +88 -9
- package/core/pay/rails/index.ts +16 -6
- package/core/pay/rails/nearby.ts +114 -4
- package/core/services/capWalletArgs.ts +8 -9
- package/core/services/externalOrigin.ts +39 -0
- package/core/services/vault/VaultKeyService.ts +912 -251
- package/core/services/vault/ceremony.ts +435 -367
- package/core/services/vault/ceremonyHost.ts +27 -21
- package/core/services/vault/devMock.ts +30 -7
- package/core/services/vault/driver.ts +95 -41
- package/core/services/vault/guard.ts +514 -56
- package/core/services/vault/hardwareLease.ts +22 -0
- package/core/services/vault/mockYubiKey.ts +258 -90
- package/core/services/vault/pivReset.ts +145 -0
- package/core/services/vault/r1comb.ts +976 -0
- package/core/services/vault/session.ts +93 -26
- package/core/services/vault/transfers.ts +2617 -747
- package/core/services/vault/types.ts +100 -43
- package/core/services/vault/vaultStore.ts +991 -74
- package/core/services/walletConnectionValidation.ts +293 -0
- package/core/storage/StorageExpoSQLite.ts +382 -56
- package/core/storage/dbImage.ts +48 -0
- package/core/storage/methods/findSql.ts +0 -54
- package/core/storage/methods/listOutputsSql.ts +7 -3
- package/core/storage/methods/offlineActions.ts +15 -0
- package/core/storage/methods/processOfflineActions.ts +263 -6
- package/core/storage/methods/walletBalanceSql.ts +16 -4
- package/core/storage/schema/createTables.ts +123 -0
- package/core/storage/sqlUpdateValue.ts +24 -0
- package/core/toolboxConfig.ts +112 -1
- package/core/walletRepair/runWalletCheck.ts +19 -2
- package/package.json +25 -24
- package/ui/components/QRScanner.tsx +1 -1
- package/ui/components/pay/AdmissionNotice.tsx +99 -0
- package/ui/components/pay/AssetPicker.tsx +184 -0
- package/ui/components/pay/AvailableBalance.tsx +33 -2
- package/ui/components/pay/HandleReceive.tsx +213 -50
- package/ui/components/pay/NearbyFlow.tsx +616 -69
- package/ui/components/pay/PayForm.tsx +73 -8
- package/ui/components/pay/PaymentSuccessOverlay.tsx +43 -2
- package/ui/components/pay/RecipientField.tsx +49 -4
- package/ui/components/pay/RequestHub.tsx +68 -8
- package/ui/components/pay/ResultBanner.tsx +41 -4
- package/ui/components/pay/UniversalSend.tsx +534 -30
- package/ui/components/pay/tokenSendCopy.ts +116 -0
- package/ui/components/pay/useRecipientInput.ts +36 -7
- package/ui/components/ui/PermissionSheet.tsx +65 -4
- package/ui/components/ui/Sheet.tsx +9 -2
- package/ui/components/vault/EnrollWizard.tsx +1432 -488
- package/ui/components/vault/KeyChooser.tsx +96 -0
- package/ui/components/vault/VaultCeremonySheet.tsx +107 -43
- package/ui/components/vault/vaultErrorCopy.ts +145 -0
- package/ui/components/wallet/ActivityRow.tsx +101 -17
- package/ui/components/wallet/AmountInput.tsx +77 -25
- package/ui/components/wallet/AssetAmount.tsx +95 -0
- package/ui/components/wallet/AssetSwitcherDropdown.tsx +261 -0
- package/ui/components/wallet/tokenEviction.ts +65 -0
- package/ui/hooks/useExportWalletData.ts +39 -0
- package/ui/hooks/useMandala.ts +303 -0
- package/ui/hooks/useVaultBalance.ts +105 -7
- package/ui/hooks/useVaultCoverage.ts +52 -0
- package/ui/importDatabases.ts +7 -3
- package/ui/index.ts +50 -7
- package/ui/screens/ConnectionsScreen.tsx +46 -29
- package/ui/screens/PairScreen.tsx +12 -12
- package/ui/screens/PayScreen.tsx +130 -10
- package/ui/screens/SettingsScreen.tsx +18 -9
- package/ui/screens/VaultScreen.tsx +925 -171
- package/ui/screens/VaultTransferScreen.tsx +376 -123
- package/ui/screens/WalletCheckScreen.tsx +10 -1
- package/ui/screens/WalletConfigScreen.tsx +38 -16
- package/ui/screens/WalletHomeScreen.tsx +417 -86
- package/ui/screens/homeBadges.ts +9 -1
- package/ui/screens/tokenRowTitle.ts +68 -0
- package/ui/tokenFormat.ts +108 -0
- package/ui/tokenSeen.ts +75 -0
- package/ui/tokenStatus.ts +79 -0
- package/assets/sounds/vault-close.wav +0 -0
- package/assets/sounds/vault-open.wav +0 -0
- package/core/services/vault/k1.ts +0 -75
- package/core/services/vault/sealing.ts +0 -84
- package/core/services/vault/vaultDerivation.ts +0 -152
- package/core/services/vault/vaultPassphrase.ts +0 -287
- package/ui/components/vault/PassphraseField.tsx +0 -306
- package/ui/components/vault/PhraseBackupSheet.tsx +0 -227
- package/ui/screens/VaultRecoverScreen.tsx +0 -226
|
@@ -106,6 +106,26 @@ function loadQuietEventSourceClass(): EventSourceCtor {
|
|
|
106
106
|
return quietEventSourceClass
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
+
/**
|
|
110
|
+
* `MandalaTokenModuleDeps.resolveAssetMetadata`, over the lib's own registry
|
|
111
|
+
* lookup (`@bsv/mandala`'s `resolveAssetMetadata` — overlay lookup,
|
|
112
|
+
* SPV-verified genesis, memoized). Only ever affects the display (name AND,
|
|
113
|
+
* per adversarial-review finding 4, decimal formatting) in a permission
|
|
114
|
+
* prompt, never a decision to prompt or to grant, so a failure is null and
|
|
115
|
+
* the prompt falls back to the raw assetId in base units — including on a
|
|
116
|
+
* build where `configureMandala` has not run, where the lib throws rather
|
|
117
|
+
* than pretending the asset has no name.
|
|
118
|
+
*/
|
|
119
|
+
async function resolveMandalaAssetMetadata(assetId: string): Promise<MandalaAssetMetadata | null> {
|
|
120
|
+
try {
|
|
121
|
+
const meta = await resolveAssetMetadata(assetId)
|
|
122
|
+
if (!meta) return null
|
|
123
|
+
return { label: meta.label, ticker: meta.ticker, decimals: meta.decimals }
|
|
124
|
+
} catch {
|
|
125
|
+
return null
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
109
129
|
const DEFAULT_SETTINGS: WalletSettings = {
|
|
110
130
|
...LIB_DEFAULT_SETTINGS,
|
|
111
131
|
trustSettings: {
|
|
@@ -136,12 +156,23 @@ const DEFAULT_SETTINGS: WalletSettings = {
|
|
|
136
156
|
}
|
|
137
157
|
import type { AppChain } from '../config'
|
|
138
158
|
import { DEFAULT_STORAGE_URL, DEFAULT_CHAIN, ADMIN_ORIGINATOR, toWalletChain } from '../config'
|
|
139
|
-
import { getBackupUrl } from '../toolboxConfig'
|
|
159
|
+
import { getBackupUrl, getMandalaEndpoints } from '../toolboxConfig'
|
|
140
160
|
import { DEFAULT_AUTO_APPROVE_THRESHOLD, AUTO_APPROVE_COOLDOWN_MS, AUTO_APPROVE_STORAGE_KEY } from '../constants'
|
|
141
161
|
import AsyncStorage from '@react-native-async-storage/async-storage'
|
|
142
162
|
import { UserContext } from './UserContext'
|
|
143
163
|
import { useLocalStorage } from './LocalStorageProvider'
|
|
144
164
|
import { usePermissionQueue } from '../hooks/usePermissionQueue'
|
|
165
|
+
import { configureMandala, resolveAssetMetadata } from '@bsv/mandala'
|
|
166
|
+
import { MessageBoxClient } from '@bsv/message-box-client'
|
|
167
|
+
import { MandalaTokenModule, wrapCreateActionForTokenInputs, type MandalaAssetMetadata } from '../mandala/permissionModule'
|
|
168
|
+
import { wrapAbortActionForSettlements } from '../mandala/abortGuard'
|
|
169
|
+
import { migrateMandalaBasketName } from '../mandala/basketMigration'
|
|
170
|
+
import { bindOriginator, createMandalaKvStorage, createMandalaRuntime, type MandalaMessageBox } from '../mandala/createRuntime'
|
|
171
|
+
import type { MandalaRuntime } from '../mandala/runtime'
|
|
172
|
+
import { MANDALA_BASKET } from '../mandala/types'
|
|
173
|
+
import { mandalaSettlementDeps, type CancelParkedSettlementDeps } from '../offline/cancelParked'
|
|
174
|
+
import { drainMandalaInbox } from '../pay/rails/handle'
|
|
175
|
+
import { forgetSessionPsks, sealedFramePayloadDecoder } from '../offline/tokenFrames'
|
|
145
176
|
import { createServices, chaintracksUrlFor } from '../services/walletServiceConfig'
|
|
146
177
|
import {
|
|
147
178
|
boundReviewProvenTxs,
|
|
@@ -159,6 +190,8 @@ import {
|
|
|
159
190
|
import { getExchangeRate } from '../services/exchangeRate'
|
|
160
191
|
import { logWithTimestamp } from '../logging'
|
|
161
192
|
import { recoverMnemonicWallet } from '../mnemonicWallet'
|
|
193
|
+
import { vaultStore } from '../services/vault/vaultStore'
|
|
194
|
+
import { guardVaultAccess } from '../services/vault/guard'
|
|
162
195
|
import { StorageProvider, ChaintracksServiceClient } from '@bsv/wallet-toolbox-mobile'
|
|
163
196
|
import { StorageExpoSQLite } from '../storage'
|
|
164
197
|
import { makeBuildGeneration } from './buildGeneration'
|
|
@@ -268,11 +301,13 @@ export interface WalletContextValue {
|
|
|
268
301
|
protocolRequests: ProtocolAccessRequest[]
|
|
269
302
|
spendingRequests: SpendingRequest[]
|
|
270
303
|
btmsRequests: BtmsRequest[]
|
|
304
|
+
mandalaRequests: MandalaRequest[]
|
|
271
305
|
advanceBasketQueue: () => void
|
|
272
306
|
advanceCertificateQueue: () => void
|
|
273
307
|
advanceProtocolQueue: () => void
|
|
274
308
|
advanceSpendingQueue: () => void
|
|
275
309
|
advanceBtmsQueue: (approved: boolean) => void
|
|
310
|
+
advanceMandalaQueue: (approved: boolean) => void
|
|
276
311
|
finalizeConfig: (wabConfig: WABConfig) => boolean
|
|
277
312
|
setConfigStatus: (status: ConfigStatus) => void
|
|
278
313
|
configStatus: ConfigStatus
|
|
@@ -297,6 +332,24 @@ export interface WalletContextValue {
|
|
|
297
332
|
* "import from backup" over an auto-created wallet). */
|
|
298
333
|
rebuildWallet: (opts?: { restoreFromBackup?: boolean }) => Promise<void>
|
|
299
334
|
storage: StorageExpoSQLite | null
|
|
335
|
+
/**
|
|
336
|
+
* Mandala stablecoins, or undefined.
|
|
337
|
+
*
|
|
338
|
+
* Undefined until the wallet is built, and on every chain the host has not
|
|
339
|
+
* stated endpoints for (mainnet only in v1) — so `mandala === undefined` is
|
|
340
|
+
* exactly "this wallet cannot do stablecoins right now", which is what the UI
|
|
341
|
+
* renders. Consumed through `useMandala()`; nothing above this context may
|
|
342
|
+
* reach `@bsv/mandala` directly.
|
|
343
|
+
*/
|
|
344
|
+
mandala: MandalaRuntime | undefined
|
|
345
|
+
/**
|
|
346
|
+
* `cancelParkedPayment`'s `settlement` argument, pre-assembled from
|
|
347
|
+
* `mandala` (FIX J — an online cancel of an already-admitted token payment
|
|
348
|
+
* refuses with `'already-sent'` rather than aborting inputs the overlay
|
|
349
|
+
* considers spent). `undefined` exactly when `mandala` is, which is also
|
|
350
|
+
* the argument a plain BSV cancel needs — nothing else has to branch on it.
|
|
351
|
+
*/
|
|
352
|
+
mandalaSettlement: CancelParkedSettlementDeps | undefined
|
|
300
353
|
/** Fetch BUMP from WoC and store merkle proof, advancing tx status to completed */
|
|
301
354
|
refreshProof: (txid: string) => Promise<'confirmed' | 'pending' | 'failed'>
|
|
302
355
|
/** Peek OfflineFirstChaintracks.lastMissHeight for credit-error classification. Does not clear. */
|
|
@@ -338,11 +391,13 @@ export const WalletContext = createContext<WalletContextValue>({
|
|
|
338
391
|
protocolRequests: [],
|
|
339
392
|
spendingRequests: [],
|
|
340
393
|
btmsRequests: [],
|
|
394
|
+
mandalaRequests: [],
|
|
341
395
|
advanceBasketQueue: () => {},
|
|
342
396
|
advanceCertificateQueue: () => {},
|
|
343
397
|
advanceProtocolQueue: () => {},
|
|
344
398
|
advanceSpendingQueue: () => {},
|
|
345
399
|
advanceBtmsQueue: () => {},
|
|
400
|
+
advanceMandalaQueue: () => {},
|
|
346
401
|
finalizeConfig: () => false,
|
|
347
402
|
setConfigStatus: () => {},
|
|
348
403
|
configStatus: 'initial',
|
|
@@ -356,6 +411,8 @@ export const WalletContext = createContext<WalletContextValue>({
|
|
|
356
411
|
switchNetwork: async () => {},
|
|
357
412
|
rebuildWallet: async () => {},
|
|
358
413
|
storage: null,
|
|
414
|
+
mandala: undefined,
|
|
415
|
+
mandalaSettlement: undefined,
|
|
359
416
|
refreshProof: async () => 'pending',
|
|
360
417
|
peekLastMissHeight: () => undefined,
|
|
361
418
|
txStatusVersion: 0,
|
|
@@ -448,6 +505,16 @@ type BtmsRequest = {
|
|
|
448
505
|
resolve: (approved: boolean) => void
|
|
449
506
|
}
|
|
450
507
|
|
|
508
|
+
/** Same shape as BtmsRequest — MandalaTokenModule's requestTokenAccess queue item. */
|
|
509
|
+
type MandalaRequest = {
|
|
510
|
+
/** The originator (dApp domain) requesting Mandala token access */
|
|
511
|
+
originator: string
|
|
512
|
+
/** The raw message from MandalaTokenModule (JSON-encoded promptData) */
|
|
513
|
+
message: string
|
|
514
|
+
/** Resolve the pending Promise from MandalaTokenModule — true = approved */
|
|
515
|
+
resolve: (approved: boolean) => void
|
|
516
|
+
}
|
|
517
|
+
|
|
451
518
|
export interface WABConfig {
|
|
452
519
|
wabUrl: string
|
|
453
520
|
wabInfo?: any // Optional for noWAB (self-custodial) mode
|
|
@@ -518,6 +585,18 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
518
585
|
// buildWallet's getAuth() call below). null until a wallet is built, or if
|
|
519
586
|
// getAuth() fails — callers treat null as "unscoped" rather than a gate.
|
|
520
587
|
const [walletUserId, setWalletUserId] = useState<number | null>(null)
|
|
588
|
+
/**
|
|
589
|
+
* The Mandala stablecoin runtime, or undefined — before the wallet is built,
|
|
590
|
+
* and on every chain but the one the host stated endpoints for (mainnet in
|
|
591
|
+
* v1). `useMandala()` reads this; nothing else in the app may reach the lib.
|
|
592
|
+
*
|
|
593
|
+
* The ref beside it is what the monitor tasks read. They are registered once,
|
|
594
|
+
* inside the build, and close over whatever the ref holds at the moment they
|
|
595
|
+
* run — so a rebuild or a network switch cannot leave a task draining the
|
|
596
|
+
* previous wallet's settlement tables.
|
|
597
|
+
*/
|
|
598
|
+
const [mandala, setMandala] = useState<MandalaRuntime | undefined>(undefined)
|
|
599
|
+
const mandalaRef = useRef<MandalaRuntime | undefined>(undefined)
|
|
521
600
|
const appStateRef = useRef<AppStateStatus>(AppState.currentState)
|
|
522
601
|
const monitorRef = useRef<Monitor | null>(null)
|
|
523
602
|
// The offline-first chain tracker and the header store it wraps. Populated
|
|
@@ -737,6 +816,27 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
737
816
|
[btmsQueue.enqueue]
|
|
738
817
|
)
|
|
739
818
|
|
|
819
|
+
// Same usePermissionQueue pattern as BTMS above — a separate queue so a
|
|
820
|
+
// pending BTMS prompt and a pending Mandala prompt never resolve each
|
|
821
|
+
// other's promise.
|
|
822
|
+
const mandalaQueue = usePermissionQueue<MandalaRequest>(focusOpts)
|
|
823
|
+
|
|
824
|
+
const advanceMandalaQueue = useCallback(
|
|
825
|
+
(approved: boolean) => {
|
|
826
|
+
mandalaQueue.advance(head => head.resolve(approved))
|
|
827
|
+
},
|
|
828
|
+
[mandalaQueue.advance]
|
|
829
|
+
)
|
|
830
|
+
|
|
831
|
+
const mandalaPromptHandler = useCallback(
|
|
832
|
+
(originator: string, message: string): Promise<boolean> => {
|
|
833
|
+
return new Promise<boolean>(resolve => {
|
|
834
|
+
mandalaQueue.enqueue({ originator, message, resolve })
|
|
835
|
+
})
|
|
836
|
+
},
|
|
837
|
+
[mandalaQueue.enqueue]
|
|
838
|
+
)
|
|
839
|
+
|
|
740
840
|
const updateSettings = useCallback(
|
|
741
841
|
async (newSettings: WalletSettings) => {
|
|
742
842
|
setSettings(newSettings)
|
|
@@ -969,6 +1069,11 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
969
1069
|
const newManagers = {} as any
|
|
970
1070
|
// Deliberately the ref, not the state: see selectedNetworkRef.
|
|
971
1071
|
const chain = selectedNetworkRef.current
|
|
1072
|
+
// Mandala runs on exactly the chains the host stated endpoints for
|
|
1073
|
+
// (currently testnet only, see app/_layout.tsx): an overlay URL with no
|
|
1074
|
+
// verifiable identity key is an overlay whose admissions nothing can
|
|
1075
|
+
// check, so a partial entry reads as no entry at all (see toolboxConfig).
|
|
1076
|
+
const mandalaEndpoints = getMandalaEndpoints(chain)
|
|
972
1077
|
// Toolbox chain id ('teratest' -> 'ttn'). App keeps 'teratest' for AsyncStorage keys / env / UI.
|
|
973
1078
|
const walletChain = toWalletChain(chain)
|
|
974
1079
|
// The backup log's network name. Distinct from walletChain ('ttn' for teratest):
|
|
@@ -1085,6 +1190,15 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
1085
1190
|
}
|
|
1086
1191
|
|
|
1087
1192
|
const wallet = new Wallet(signer, services, undefined, privilegedKeyManager)
|
|
1193
|
+
// Consumed only by the toolbox storage-boundary hook. The value is set
|
|
1194
|
+
// by trusted host code and compared with WalletClient's originator;
|
|
1195
|
+
// external action fields cannot manufacture this authorization bit.
|
|
1196
|
+
Object.defineProperty(wallet, '__bsvVaultAdminOriginator', {
|
|
1197
|
+
value: adminOriginator,
|
|
1198
|
+
enumerable: false,
|
|
1199
|
+
configurable: false,
|
|
1200
|
+
writable: false
|
|
1201
|
+
})
|
|
1088
1202
|
|
|
1089
1203
|
// Every write to the transaction/output tables refreshes the money on
|
|
1090
1204
|
// screen immediately. The monitor's onTransactionStatusChanged only
|
|
@@ -1240,14 +1354,58 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
1240
1354
|
console.error('[WalletContext] Failed to add local storage provider:', error)
|
|
1241
1355
|
}
|
|
1242
1356
|
|
|
1357
|
+
let migrationUserId: number | undefined
|
|
1243
1358
|
try {
|
|
1244
1359
|
const auth = await storageManager.getAuth()
|
|
1245
1360
|
setWalletUserId(auth.userId ?? null)
|
|
1361
|
+
migrationUserId = auth.userId ?? undefined
|
|
1246
1362
|
} catch {
|
|
1247
1363
|
// Scoping is a filter, not a gate: with no id the queue reads fall
|
|
1248
1364
|
// back to unscoped, which is today's behaviour.
|
|
1249
1365
|
setWalletUserId(null)
|
|
1250
1366
|
}
|
|
1367
|
+
|
|
1368
|
+
// One-time basket rename ('mandala-tokens' -> MANDALA_BASKET), run
|
|
1369
|
+
// after storage is attached (and any backup restore above has had
|
|
1370
|
+
// its chance to write old-named rows) so a restored device's rows
|
|
1371
|
+
// are migrated too, not just a freshly-created wallet's. Guarded
|
|
1372
|
+
// internally by its own key_value_store marker; the per-user pass
|
|
1373
|
+
// still runs on every build so a restored backup's old-named rows
|
|
1374
|
+
// are caught even after the marker is set. See offline-settlement-final.md §8.3.
|
|
1375
|
+
try {
|
|
1376
|
+
await migrateMandalaBasketName(phoneStorage, migrationUserId)
|
|
1377
|
+
} catch (error) {
|
|
1378
|
+
console.error('[WalletContext] Mandala basket migration failed:', error)
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
// Hand `@bsv/mandala` this deployment's endpoints, this wallet's
|
|
1382
|
+
// basket, and a DURABLE journal store — before any lib call can run.
|
|
1383
|
+
//
|
|
1384
|
+
// The storage adapter is the load-bearing half. The lib's journals
|
|
1385
|
+
// are its recovery contract: an overlay-accepted, not-yet-broadcast
|
|
1386
|
+
// transaction is recoverable only because the 'accepted' entry
|
|
1387
|
+
// outlives the process that wrote it. React Native has no
|
|
1388
|
+
// localStorage, so a host that injects nothing here gets a
|
|
1389
|
+
// process-lifetime Map — i.e. no crash recovery at all — and the
|
|
1390
|
+
// failure is silent. See lib/src/storage.ts.
|
|
1391
|
+
//
|
|
1392
|
+
// `basket` is the wallet's own deviation from the lib default
|
|
1393
|
+
// ('mandala-tokens'): 'p mandala' is BRC-99 P-routed, which is what
|
|
1394
|
+
// puts every listOutputs/createAction/internalizeAction against it
|
|
1395
|
+
// through MandalaTokenModule regardless of originator (spec §8.3).
|
|
1396
|
+
if (mandalaEndpoints) {
|
|
1397
|
+
try {
|
|
1398
|
+
configureMandala({
|
|
1399
|
+
overlayUrl: mandalaEndpoints.overlayUrl,
|
|
1400
|
+
overlayIdentityKey: mandalaEndpoints.overlayIdentityKey,
|
|
1401
|
+
messageBoxUrl: mandalaEndpoints.messageBoxUrl,
|
|
1402
|
+
basket: MANDALA_BASKET,
|
|
1403
|
+
storage: createMandalaKvStorage(phoneStorage)
|
|
1404
|
+
})
|
|
1405
|
+
} catch (error) {
|
|
1406
|
+
console.error('[WalletContext] configureMandala failed:', error)
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1251
1409
|
}
|
|
1252
1410
|
// TODO: Re-add remote storage support in future version
|
|
1253
1411
|
|
|
@@ -1255,6 +1413,39 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
1255
1413
|
// "p btms" operations surface a UI modal rather than silently denying.
|
|
1256
1414
|
const btmsModule = loadBtmsPermissionModule().createBtmsModule({ wallet, promptHandler: btmsPromptHandler })
|
|
1257
1415
|
|
|
1416
|
+
// Adversarial-review finding (2): every outpoint this wallet holds in
|
|
1417
|
+
// MANDALA_BASKET, via the ADMIN-originator wallet -- i.e. this call
|
|
1418
|
+
// itself passes through MandalaTokenModule with zero prompts (the
|
|
1419
|
+
// module's own admin pass-through branch). Shared by the module's
|
|
1420
|
+
// deps below AND by wrapCreateActionForTokenInputs's wrapper further
|
|
1421
|
+
// down, so both use the exact same listing.
|
|
1422
|
+
const listMandalaTokenOutpoints = async (): Promise<Set<string>> => {
|
|
1423
|
+
try {
|
|
1424
|
+
const { outputs } = await wallet.listOutputs(
|
|
1425
|
+
{ basket: MANDALA_BASKET, includeCustomInstructions: false, limit: 10000 } as never,
|
|
1426
|
+
adminOriginator
|
|
1427
|
+
)
|
|
1428
|
+
return new Set(outputs.map((o: { outpoint: string }) => o.outpoint))
|
|
1429
|
+
} catch {
|
|
1430
|
+
// A wallet with no Mandala basket yet, or a storage fault -- treat
|
|
1431
|
+
// as "no known token inputs" rather than blocking createAction;
|
|
1432
|
+
// see permissionModule.ts's doc on this dep failing open.
|
|
1433
|
+
return new Set()
|
|
1434
|
+
}
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
// Mandala's own P-module (schemeID 'mandala', basket MANDALA_BASKET =
|
|
1438
|
+
// 'p mandala') -- same routing mechanism as BTMS above
|
|
1439
|
+
// (WalletPermissionsManager delegates by basket prefix regardless of
|
|
1440
|
+
// originator), but decodes Mandala's own script layout rather than
|
|
1441
|
+
// BTMS's PushDrop. See offline-settlement-final.md §8.
|
|
1442
|
+
const mandalaModule = new MandalaTokenModule({
|
|
1443
|
+
adminOriginator,
|
|
1444
|
+
requestTokenAccess: mandalaPromptHandler,
|
|
1445
|
+
resolveAssetMetadata: resolveMandalaAssetMetadata,
|
|
1446
|
+
listTokenOutpoints: listMandalaTokenOutpoints
|
|
1447
|
+
})
|
|
1448
|
+
|
|
1258
1449
|
// Setup permissions with provided callbacks and BTMS module.
|
|
1259
1450
|
const permissionsManager = new WalletPermissionsManager(wallet, adminOriginator, {
|
|
1260
1451
|
differentiatePrivilegedOperations: true,
|
|
@@ -1276,7 +1467,7 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
1276
1467
|
seekProtocolPermissionsForHMAC: false,
|
|
1277
1468
|
seekProtocolPermissionsForSigning: false,
|
|
1278
1469
|
seekSpendingPermissions: true,
|
|
1279
|
-
permissionModules: { btms: btmsModule }
|
|
1470
|
+
permissionModules: { btms: btmsModule, mandala: mandalaModule }
|
|
1280
1471
|
} as any)
|
|
1281
1472
|
|
|
1282
1473
|
if (protocolPermissionCallback) {
|
|
@@ -1292,7 +1483,90 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
1292
1483
|
permissionsManager.bindCallback('onCertificateAccessRequested', certificateAccessCallback)
|
|
1293
1484
|
}
|
|
1294
1485
|
|
|
1295
|
-
|
|
1486
|
+
// Publish one idempotently guarded manager for both trusted in-app
|
|
1487
|
+
// Vault calls and external WalletClient calls. Admin output mutations
|
|
1488
|
+
// therefore share the guard's FIFO with external inventory scan+use;
|
|
1489
|
+
// external call sites may safely apply guardVaultAccess again.
|
|
1490
|
+
//
|
|
1491
|
+
// wrapCreateActionForTokenInputs wraps the RAW permissionsManager
|
|
1492
|
+
// FIRST, and guardVaultAccess is applied to ITS result (never the
|
|
1493
|
+
// other way around) -- adversarial-review finding (2):
|
|
1494
|
+
// WalletPermissionsManager only P-routes a createAction call whose
|
|
1495
|
+
// OUTPUTS or LABELS name a 'p ' scheme, never its inputs, so a
|
|
1496
|
+
// createAction that spends 'p mandala' inputs with no basketed/
|
|
1497
|
+
// decodable change output would otherwise reach
|
|
1498
|
+
// MandalaTokenModule.onRequest for exactly zero callers of this
|
|
1499
|
+
// manager. Wrapping outside-in (guard around this wrapper) would
|
|
1500
|
+
// instead break guardVaultAccess's own re-wrap dedup, which several
|
|
1501
|
+
// screens rely on (`guardVaultAccess(managers.permissionsManager,
|
|
1502
|
+
// ADMIN_ORIGINATOR)` again) -- see permissionModule.ts's wrapper doc
|
|
1503
|
+
// for the full mechanism.
|
|
1504
|
+
//
|
|
1505
|
+
// `wrapAbortActionForSettlements` goes on the same stack, for the same
|
|
1506
|
+
// reason and in the same place: inside `guardVaultAccess`, so the
|
|
1507
|
+
// published object stays the one the guard's dedup recognises. It is
|
|
1508
|
+
// the last line of defence for the 2026-09-15 incident — an
|
|
1509
|
+
// `abortAction` against a token payment the overlay has already
|
|
1510
|
+
// admitted (and therefore broadcast) releases inputs that are spent on
|
|
1511
|
+
// chain. It reads the settlement store LATE, off `mandalaRef`, because
|
|
1512
|
+
// the runtime is built below this line and replaced on every rebuild.
|
|
1513
|
+
newManagers.permissionsManager = guardVaultAccess(
|
|
1514
|
+
wrapAbortActionForSettlements(
|
|
1515
|
+
wrapCreateActionForTokenInputs(permissionsManager, listMandalaTokenOutpoints, adminOriginator),
|
|
1516
|
+
() => mandalaRef.current?.store
|
|
1517
|
+
),
|
|
1518
|
+
adminOriginator
|
|
1519
|
+
)
|
|
1520
|
+
|
|
1521
|
+
// THE MANDALA RUNTIME'S ONE CONSTRUCTION SITE.
|
|
1522
|
+
//
|
|
1523
|
+
// Built here, after the permissions manager, because every lib call it
|
|
1524
|
+
// makes must go through the P-module gate — a runtime holding the raw
|
|
1525
|
+
// wallet would bypass exactly the routing §8.3 exists to establish. It
|
|
1526
|
+
// is published only when `available`: a runtime on a chain with no
|
|
1527
|
+
// endpoints could submit nowhere and verify nothing, and `undefined` is
|
|
1528
|
+
// what the UI already renders as "not on this network".
|
|
1529
|
+
if (phoneStorage) {
|
|
1530
|
+
try {
|
|
1531
|
+
const runtime = createMandalaRuntime({
|
|
1532
|
+
wallet: newManagers.permissionsManager as never,
|
|
1533
|
+
adminOriginator,
|
|
1534
|
+
storage: phoneStorage,
|
|
1535
|
+
chain,
|
|
1536
|
+
endpoints: mandalaEndpoints,
|
|
1537
|
+
// Lazy: constructing a runtime must do no I/O, and a wallet that
|
|
1538
|
+
// never touches stablecoins never opens a MessageBox connection.
|
|
1539
|
+
messageBox: mandalaEndpoints
|
|
1540
|
+
? async () =>
|
|
1541
|
+
new MessageBoxClient({
|
|
1542
|
+
host: mandalaEndpoints.messageBoxUrl,
|
|
1543
|
+
// The client calls getPublicKey / createSignature itself;
|
|
1544
|
+
// those must carry the admin originator or the vault guard
|
|
1545
|
+
// refuses them ("Originator is required for permission checks").
|
|
1546
|
+
walletClient: bindOriginator(newManagers.permissionsManager as object, adminOriginator) as never,
|
|
1547
|
+
enableLogging: false
|
|
1548
|
+
}) as unknown as MandalaMessageBox
|
|
1549
|
+
: undefined,
|
|
1550
|
+
// The payer's own frames are sealed with a nearby session PSK the
|
|
1551
|
+
// drain never persists; this opens the ones whose session this
|
|
1552
|
+
// process still holds. See offline/tokenFrames.ts.
|
|
1553
|
+
decodeSealedFrame: sealedFramePayloadDecoder(),
|
|
1554
|
+
// The one thing `settleNow` asks about connectivity: whether a
|
|
1555
|
+
// hand-over that just landed should be followed by this device's
|
|
1556
|
+
// own submit now, or left to the drain. The runtime deliberately
|
|
1557
|
+
// has no NetInfo dependency of its own, so the app-wide probe is
|
|
1558
|
+
// handed to it here.
|
|
1559
|
+
isOnline: getOnline,
|
|
1560
|
+
refetchBeef: makeBeefRepair({ woc: wocConfigFor(chain), online: getOnline })
|
|
1561
|
+
})
|
|
1562
|
+
mandalaRef.current = runtime.available ? runtime : undefined
|
|
1563
|
+
setMandala(runtime.available ? runtime : undefined)
|
|
1564
|
+
} catch (error) {
|
|
1565
|
+
console.error('[WalletContext] could not build the Mandala runtime:', error)
|
|
1566
|
+
mandalaRef.current = undefined
|
|
1567
|
+
setMandala(undefined)
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1296
1570
|
|
|
1297
1571
|
// Start background monitor for transaction status updates (sending → unproven → completed)
|
|
1298
1572
|
try {
|
|
@@ -1346,9 +1620,48 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
1346
1620
|
if (phoneStorage) {
|
|
1347
1621
|
monitor.addTask(
|
|
1348
1622
|
new TaskSendOffline(monitor, async () => {
|
|
1623
|
+
// FIX C recovery and the nearby-blinding reservation sweep ride
|
|
1624
|
+
// this same tick, ahead of the submit-then-broadcast pass below
|
|
1625
|
+
// so a row this recovers to `admitted` can broadcast in the
|
|
1626
|
+
// SAME pass rather than waiting for the next one. Read from the
|
|
1627
|
+
// ref for the same reason `token: mandalaRef.current?.tokenDeps`
|
|
1628
|
+
// is below — a rebuild replaces the runtime under a task
|
|
1629
|
+
// registered once. Both are best-effort and never throw.
|
|
1630
|
+
await mandalaRef.current?.recoverStaleAdmissions()
|
|
1631
|
+
// The 2026-09-15 repair: a settlement row that says the overlay
|
|
1632
|
+
// has the transaction, beside a wallet that failed it and
|
|
1633
|
+
// released its input coin as spendable although it is spent on
|
|
1634
|
+
// chain. Ahead of the release pass on purpose — a coin this
|
|
1635
|
+
// re-marks spent must not be planned into a send below.
|
|
1636
|
+
await mandalaRef.current?.repairAdmittedAborted()
|
|
1637
|
+
// The LIB's own journals, on the same tick and with the same
|
|
1638
|
+
// ref discipline: an overlay-accepted transfer whose broadcast
|
|
1639
|
+
// never went out, a liftable refusal still holding its inputs,
|
|
1640
|
+
// and the recipient notification a committed transfer owes its
|
|
1641
|
+
// payee. Nothing else in this wallet drives them — the
|
|
1642
|
+
// settlement drain below only knows `token_settlements` rows,
|
|
1643
|
+
// and the handle rail writes none of these entries. Never
|
|
1644
|
+
// throws (see `reconcileJournals`).
|
|
1645
|
+
await mandalaRef.current?.reconcileJournals()
|
|
1646
|
+
// The payer's handle-rail rows: no queue row exists for them,
|
|
1647
|
+
// so the release pass below would never step them. Each one
|
|
1648
|
+
// takes its `settleNow` step here instead (2026-09-15).
|
|
1649
|
+
await mandalaRef.current?.settlePendingSends()
|
|
1650
|
+
// Every held coin's σ_I on device, for the next offline hand-over.
|
|
1651
|
+
await mandalaRef.current?.ensureAdmissionsForHoldings()
|
|
1652
|
+
await mandalaRef.current?.pruneBlindingReservations()
|
|
1349
1653
|
const r = await processOfflineActions({
|
|
1350
1654
|
storage: phoneStorage!,
|
|
1351
|
-
refetchBeef: makeBeefRepair({ woc: wocConfigFor(chain), online: getOnline })
|
|
1655
|
+
refetchBeef: makeBeefRepair({ woc: wocConfigFor(chain), online: getOnline }),
|
|
1656
|
+
// The settlement drain, on the same cadence and in the same
|
|
1657
|
+
// pass as the BSV release. That is the whole of §4.3: a token
|
|
1658
|
+
// transaction takes the ordinary plan's place in dependency
|
|
1659
|
+
// order and simply has `/submit` in front of its broadcast,
|
|
1660
|
+
// so the recipient's `held` rows and the payer's
|
|
1661
|
+
// `handed_over` rows both settle on every online tick.
|
|
1662
|
+
// Read from the ref, not captured: a rebuild replaces the
|
|
1663
|
+
// runtime under a task that was registered once.
|
|
1664
|
+
token: mandalaRef.current?.tokenDeps
|
|
1352
1665
|
})
|
|
1353
1666
|
// The drain writes transaction statuses directly, below the
|
|
1354
1667
|
// monitor's onTransactionStatusChanged callback — bump the
|
|
@@ -1411,7 +1724,19 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
1411
1724
|
internalizeIncoming(permissionsManager as never, client, adminOriginator, p, repairBeef)
|
|
1412
1725
|
)
|
|
1413
1726
|
})
|
|
1414
|
-
|
|
1727
|
+
// The SAME rail, a second box. A handle-rail token payment
|
|
1728
|
+
// is delivered into 'mandala-payments', not 'payment_inbox',
|
|
1729
|
+
// so a receive drain that read only the satoshi box would
|
|
1730
|
+
// leave real money sitting in the other one. Never throws
|
|
1731
|
+
// (see drainMandalaInbox) — a MessageBox fault on the token
|
|
1732
|
+
// side must not take the satoshi inbox down with it.
|
|
1733
|
+
const tokens = await drainMandalaInbox(mandalaRef.current)
|
|
1734
|
+
if (tokens.credited > 0) setTxStatusVersion(v => v + 1)
|
|
1735
|
+
return {
|
|
1736
|
+
accepted: r.accepted + tokens.credited,
|
|
1737
|
+
attention: r.attentionCount,
|
|
1738
|
+
pending: r.pending
|
|
1739
|
+
}
|
|
1415
1740
|
},
|
|
1416
1741
|
Date.now,
|
|
1417
1742
|
() => {
|
|
@@ -1486,11 +1811,17 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
1486
1811
|
// the drain so the parent goes out first.
|
|
1487
1812
|
if (phoneStorage) {
|
|
1488
1813
|
const sendWaiting = monitor._tasks.find(t => t.name === 'SendWaiting') as
|
|
1489
|
-
| { processUnsent?: (reqApis: Array<{ rawTx?: number[] }>, indent?: number) => Promise<string> }
|
|
1814
|
+
| { processUnsent?: (reqApis: Array<{ txid: string; rawTx?: number[] }>, indent?: number) => Promise<string> }
|
|
1490
1815
|
| undefined
|
|
1491
1816
|
if (sendWaiting?.processUnsent) {
|
|
1492
1817
|
const orig = sendWaiting.processUnsent.bind(sendWaiting)
|
|
1493
|
-
sendWaiting.processUnsent = async (
|
|
1818
|
+
sendWaiting.processUnsent = async (allReqApis, indent) => {
|
|
1819
|
+
// Guard #2 for this path: a request with a `token_settlements`
|
|
1820
|
+
// row is the settlement drain's to broadcast, after `/submit`,
|
|
1821
|
+
// never the monitor's. Held here and dropped from this pass.
|
|
1822
|
+
const heldTokenTxids = await phoneStorage.holdTokenReqsForDrain(allReqApis as never)
|
|
1823
|
+
const reqApis = heldTokenTxids.size > 0 ? allReqApis.filter(r => !heldTokenTxids.has(r.txid)) : allReqApis
|
|
1824
|
+
if (heldTokenTxids.size > 0) TaskSendOffline.requestNow()
|
|
1494
1825
|
let queuedTxids = new Set<string>()
|
|
1495
1826
|
try {
|
|
1496
1827
|
const db = phoneStorage.sqliteDb
|
|
@@ -1509,7 +1840,10 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
1509
1840
|
}
|
|
1510
1841
|
if (deferred > 0) TaskSendOffline.requestNow()
|
|
1511
1842
|
if (ready.length === 0) {
|
|
1512
|
-
|
|
1843
|
+
const notes: string[] = []
|
|
1844
|
+
if (heldTokenTxids.size > 0) notes.push(`held ${heldTokenTxids.size} token req(s) for the settlement drain`)
|
|
1845
|
+
if (deferred > 0) notes.push(`deferred ${deferred} req(s) behind queued ancestors`)
|
|
1846
|
+
return notes.length > 0 ? `${notes.join('; ')}\n` : ''
|
|
1513
1847
|
}
|
|
1514
1848
|
return orig(ready, indent)
|
|
1515
1849
|
}
|
|
@@ -1723,6 +2057,7 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
1723
2057
|
logWithTimestamp(F, 'Discarding a build the network switch overtook')
|
|
1724
2058
|
return null
|
|
1725
2059
|
}
|
|
2060
|
+
vaultStore.configureScope({ identityKey: keyDeriver.identityKey, chain: backupChain })
|
|
1726
2061
|
updateManagers(m => ({ ...m, ...newManagers }))
|
|
1727
2062
|
logWithTimestamp(F, 'Wallet build completed successfully')
|
|
1728
2063
|
|
|
@@ -1746,6 +2081,7 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
1746
2081
|
spendingAuthorizationCallback,
|
|
1747
2082
|
certificateAccessCallback,
|
|
1748
2083
|
btmsPromptHandler,
|
|
2084
|
+
mandalaPromptHandler,
|
|
1749
2085
|
runHeaderSync,
|
|
1750
2086
|
noteLedgerChanged,
|
|
1751
2087
|
onToast
|
|
@@ -1925,6 +2261,10 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
1925
2261
|
restoreIntentRef.current = opts?.restoreFromBackup === true
|
|
1926
2262
|
// Any build already in flight belongs to the configuration being replaced.
|
|
1927
2263
|
const token = buildGenRef.current.bump()
|
|
2264
|
+
// Invalidate Vault operations before the first teardown await. Otherwise a
|
|
2265
|
+
// signer/listing already in flight can resume against the departing wallet.
|
|
2266
|
+
vaultStore.clearScope()
|
|
2267
|
+
vaultCeremony.cancel()
|
|
1928
2268
|
|
|
1929
2269
|
// Stop any running monitor and let its current pass drain before the
|
|
1930
2270
|
// storage teardown below closes the connection under it.
|
|
@@ -1952,8 +2292,15 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
1952
2292
|
// the screens reading the OLD chain's database until the new build
|
|
1953
2293
|
// replaced it — which is how a testnet wallet displayed mainnet money.
|
|
1954
2294
|
setStorage(null)
|
|
2295
|
+
// The runtime holds this build's settlement store, which is a handle on
|
|
2296
|
+
// the database just destroyed. Dropping it here is what stops a monitor
|
|
2297
|
+
// task or a screen draining the departed wallet's tables.
|
|
2298
|
+
mandalaRef.current = undefined
|
|
2299
|
+
setMandala(undefined)
|
|
2300
|
+
forgetSessionPsks()
|
|
1955
2301
|
|
|
1956
2302
|
// Tear down current wallet state (but keep mnemonic / config)
|
|
2303
|
+
vaultStore.clearScope()
|
|
1957
2304
|
updateManagers({})
|
|
1958
2305
|
walletBuiltRef.current = false
|
|
1959
2306
|
setWalletBuilt(false)
|
|
@@ -1980,6 +2327,8 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
1980
2327
|
if (network === selectedNetwork) return
|
|
1981
2328
|
logWithTimestamp(F, `Switching network from ${selectedNetwork} to ${network}`)
|
|
1982
2329
|
buildGenRef.current.bump()
|
|
2330
|
+
vaultStore.clearScope()
|
|
2331
|
+
vaultCeremony.cancel()
|
|
1983
2332
|
|
|
1984
2333
|
// Stop any running monitor and let its current pass drain before the
|
|
1985
2334
|
// storage teardown below closes the connection under it.
|
|
@@ -2002,8 +2351,12 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
2002
2351
|
} catch {}
|
|
2003
2352
|
}
|
|
2004
2353
|
setStorage(null)
|
|
2354
|
+
mandalaRef.current = undefined
|
|
2355
|
+
setMandala(undefined)
|
|
2356
|
+
forgetSessionPsks()
|
|
2005
2357
|
|
|
2006
2358
|
// Tear down current wallet state (but keep mnemonic)
|
|
2359
|
+
vaultStore.clearScope()
|
|
2007
2360
|
updateManagers({})
|
|
2008
2361
|
walletBuiltRef.current = false
|
|
2009
2362
|
setWalletBuilt(false)
|
|
@@ -2079,7 +2432,24 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
2079
2432
|
online = false
|
|
2080
2433
|
}
|
|
2081
2434
|
if (!canInternalizePending(online)) return
|
|
2082
|
-
const results = await processPending(
|
|
2435
|
+
const results = await processPending(
|
|
2436
|
+
managers.permissionsManager as any,
|
|
2437
|
+
storage,
|
|
2438
|
+
adminOriginator,
|
|
2439
|
+
undefined,
|
|
2440
|
+
// A credited TOKEN frame is the moment this device becomes
|
|
2441
|
+
// responsible for submitting its chain (settlement rule 3), so the
|
|
2442
|
+
// row and the evidence the frame carried are written here — not
|
|
2443
|
+
// inside the queue, which knows nothing about settlement. Its own
|
|
2444
|
+
// failure is never the payment's: the coin is already credited, and
|
|
2445
|
+
// the drain re-derives what this missed on the next online tick.
|
|
2446
|
+
mandalaRef.current?.onTokenCredited,
|
|
2447
|
+
// The same facts, written BEFORE the internalize — which is the only
|
|
2448
|
+
// ordering the unadmitted-broadcast guard can actually read
|
|
2449
|
+
// (§4.3 guard #2). This one IS allowed to refuse the credit: the
|
|
2450
|
+
// frame stays durable in the queue and the next tick retries it.
|
|
2451
|
+
mandalaRef.current?.onTokenHeld
|
|
2452
|
+
)
|
|
2083
2453
|
const successes = results.filter(r => r.success)
|
|
2084
2454
|
if (successes.length > 0) {
|
|
2085
2455
|
// The activity list and balance re-read on this version; without it
|
|
@@ -2270,16 +2640,12 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
2270
2640
|
return () => subscription.remove()
|
|
2271
2641
|
}, [])
|
|
2272
2642
|
|
|
2273
|
-
// Relock-on-unplug
|
|
2274
|
-
//
|
|
2275
|
-
//
|
|
2276
|
-
// ends with the hardware rather than lingering.
|
|
2643
|
+
// Relock-on-unplug for injected persistent readers. Native iOS and Android
|
|
2644
|
+
// drivers are ceremony-scoped: the ceremony owns discovery and its listener
|
|
2645
|
+
// through signer release, including Android USB unplug events.
|
|
2277
2646
|
//
|
|
2278
|
-
// Session-based
|
|
2279
|
-
//
|
|
2280
|
-
// starting discovery at launch would pop the NFC sheet, and a session-end
|
|
2281
|
-
// "detach" is normal, not an unplug. The ceremony's own session-scoped
|
|
2282
|
-
// listener (see ceremony.ts) handles relock there instead.
|
|
2647
|
+
// Session-based drivers are skipped here so no hardware reader or NFC sheet
|
|
2648
|
+
// is opened at launch. See ceremony.ts for their scoped listener lifecycle.
|
|
2283
2649
|
useEffect(() => {
|
|
2284
2650
|
const driver = getVaultDriver()
|
|
2285
2651
|
if (!driver || driver.sessionBased) return
|
|
@@ -2316,11 +2682,17 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
2316
2682
|
monitorRef.current = null
|
|
2317
2683
|
offlineChaintracksRef.current = undefined
|
|
2318
2684
|
headerStoreRef.current = undefined
|
|
2685
|
+
vaultStore.clearScope()
|
|
2686
|
+
vaultCeremony.cancel()
|
|
2319
2687
|
}
|
|
2320
2688
|
}, [])
|
|
2321
2689
|
|
|
2322
2690
|
const logout = useCallback(() => {
|
|
2323
2691
|
logWithTimestamp(F, 'Logout')
|
|
2692
|
+
// Synchronous on purpose: invalidate transfer tokens and release any PIN /
|
|
2693
|
+
// hardware session before monitor or storage teardown yields.
|
|
2694
|
+
vaultStore.clearScope()
|
|
2695
|
+
vaultCeremony.cancel()
|
|
2324
2696
|
;(async () => {
|
|
2325
2697
|
// Tear the wallet down the same way rebuildWallet does. Logout used to
|
|
2326
2698
|
// skip this, which orphaned a running monitor AND left the SQLite
|
|
@@ -2343,7 +2715,11 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
2343
2715
|
} catch {}
|
|
2344
2716
|
}
|
|
2345
2717
|
setStorage(null)
|
|
2718
|
+
mandalaRef.current = undefined
|
|
2719
|
+
setMandala(undefined)
|
|
2720
|
+
forgetSessionPsks()
|
|
2346
2721
|
|
|
2722
|
+
vaultStore.clearScope()
|
|
2347
2723
|
updateManagers({})
|
|
2348
2724
|
setConfigStatus('initial')
|
|
2349
2725
|
walletBuiltRef.current = false
|
|
@@ -2784,11 +3160,13 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
2784
3160
|
protocolRequests: protocolQueue.requests,
|
|
2785
3161
|
spendingRequests: spendingQueue.requests,
|
|
2786
3162
|
btmsRequests: btmsQueue.requests,
|
|
3163
|
+
mandalaRequests: mandalaQueue.requests,
|
|
2787
3164
|
advanceBasketQueue: basketQueue.advance,
|
|
2788
3165
|
advanceCertificateQueue: certificateQueue.advance,
|
|
2789
3166
|
advanceProtocolQueue: protocolQueue.advance,
|
|
2790
3167
|
advanceSpendingQueue: spendingQueue.advance,
|
|
2791
3168
|
advanceBtmsQueue,
|
|
3169
|
+
advanceMandalaQueue,
|
|
2792
3170
|
finalizeConfig,
|
|
2793
3171
|
setConfigStatus,
|
|
2794
3172
|
configStatus,
|
|
@@ -2802,6 +3180,8 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
2802
3180
|
switchNetwork,
|
|
2803
3181
|
rebuildWallet,
|
|
2804
3182
|
storage,
|
|
3183
|
+
mandala,
|
|
3184
|
+
mandalaSettlement: mandalaSettlementDeps(mandala),
|
|
2805
3185
|
refreshProof,
|
|
2806
3186
|
peekLastMissHeight,
|
|
2807
3187
|
txStatusVersion,
|
|
@@ -2827,11 +3207,13 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
2827
3207
|
protocolQueue.requests,
|
|
2828
3208
|
spendingQueue.requests,
|
|
2829
3209
|
btmsQueue.requests,
|
|
3210
|
+
mandalaQueue.requests,
|
|
2830
3211
|
basketQueue.advance,
|
|
2831
3212
|
certificateQueue.advance,
|
|
2832
3213
|
protocolQueue.advance,
|
|
2833
3214
|
spendingQueue.advance,
|
|
2834
3215
|
advanceBtmsQueue,
|
|
3216
|
+
advanceMandalaQueue,
|
|
2835
3217
|
finalizeConfig,
|
|
2836
3218
|
setConfigStatus,
|
|
2837
3219
|
configStatus,
|
|
@@ -2845,6 +3227,7 @@ export const WalletContextProvider: React.FC<WalletContextProps> = ({ children =
|
|
|
2845
3227
|
switchNetwork,
|
|
2846
3228
|
rebuildWallet,
|
|
2847
3229
|
storage,
|
|
3230
|
+
mandala,
|
|
2848
3231
|
refreshProof,
|
|
2849
3232
|
peekLastMissHeight,
|
|
2850
3233
|
txStatusVersion,
|
|
@@ -2923,11 +3306,13 @@ export interface WalletQueuesSlice {
|
|
|
2923
3306
|
protocolRequests: ProtocolAccessRequest[]
|
|
2924
3307
|
spendingRequests: SpendingRequest[]
|
|
2925
3308
|
btmsRequests: BtmsRequest[]
|
|
3309
|
+
mandalaRequests: MandalaRequest[]
|
|
2926
3310
|
advanceBasketQueue: () => void
|
|
2927
3311
|
advanceCertificateQueue: () => void
|
|
2928
3312
|
advanceProtocolQueue: () => void
|
|
2929
3313
|
advanceSpendingQueue: () => void
|
|
2930
3314
|
advanceBtmsQueue: (approved: boolean) => void
|
|
3315
|
+
advanceMandalaQueue: (approved: boolean) => void
|
|
2931
3316
|
}
|
|
2932
3317
|
export const useWalletQueues = (): WalletQueuesSlice => {
|
|
2933
3318
|
const ctx = useContext(WalletContext)
|
|
@@ -2938,11 +3323,13 @@ export const useWalletQueues = (): WalletQueuesSlice => {
|
|
|
2938
3323
|
protocolRequests: ctx.protocolRequests,
|
|
2939
3324
|
spendingRequests: ctx.spendingRequests,
|
|
2940
3325
|
btmsRequests: ctx.btmsRequests,
|
|
3326
|
+
mandalaRequests: ctx.mandalaRequests,
|
|
2941
3327
|
advanceBasketQueue: ctx.advanceBasketQueue,
|
|
2942
3328
|
advanceCertificateQueue: ctx.advanceCertificateQueue,
|
|
2943
3329
|
advanceProtocolQueue: ctx.advanceProtocolQueue,
|
|
2944
3330
|
advanceSpendingQueue: ctx.advanceSpendingQueue,
|
|
2945
|
-
advanceBtmsQueue: ctx.advanceBtmsQueue
|
|
3331
|
+
advanceBtmsQueue: ctx.advanceBtmsQueue,
|
|
3332
|
+
advanceMandalaQueue: ctx.advanceMandalaQueue
|
|
2946
3333
|
}),
|
|
2947
3334
|
[
|
|
2948
3335
|
ctx.basketRequests,
|
|
@@ -2950,11 +3337,13 @@ export const useWalletQueues = (): WalletQueuesSlice => {
|
|
|
2950
3337
|
ctx.protocolRequests,
|
|
2951
3338
|
ctx.spendingRequests,
|
|
2952
3339
|
ctx.btmsRequests,
|
|
3340
|
+
ctx.mandalaRequests,
|
|
2953
3341
|
ctx.advanceBasketQueue,
|
|
2954
3342
|
ctx.advanceCertificateQueue,
|
|
2955
3343
|
ctx.advanceProtocolQueue,
|
|
2956
3344
|
ctx.advanceSpendingQueue,
|
|
2957
|
-
ctx.advanceBtmsQueue
|
|
3345
|
+
ctx.advanceBtmsQueue,
|
|
3346
|
+
ctx.advanceMandalaQueue
|
|
2958
3347
|
]
|
|
2959
3348
|
)
|
|
2960
3349
|
}
|