@bsv/expo-wallet-toolbox 0.21.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 +222 -0
- package/README.md +82 -13
- 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/contacts/contactActivity.ts +48 -28
- package/core/contacts/contactsStore.ts +2 -2
- package/core/context/WalletConnectionContext.tsx +7 -1
- package/core/context/WalletContext.tsx +420 -348
- package/core/context/sessionTeardown.ts +46 -0
- package/core/demo/demoAssets.ts +8 -10
- package/core/demo/demoLedger.ts +32 -38
- package/core/demo/demoMandalaRuntime.ts +67 -49
- package/core/demo/index.ts +1 -1
- package/core/hooks/usePermissionQueue.ts +89 -34
- package/core/i18n/translations.tsx +983 -12
- package/core/index.ts +70 -37
- package/core/localpay/build.ts +110 -569
- package/core/localpay/codec.ts +190 -93
- package/core/localpay/pending.ts +217 -202
- package/core/localpay/session.ts +99 -25
- package/core/localpay/types.ts +6 -5
- package/core/localpay/verify.ts +118 -173
- package/core/mandala/abortGuard.ts +97 -120
- package/core/mandala/accessPolicy.ts +133 -0
- package/core/mandala/assets.ts +370 -0
- package/core/mandala/colorGuard.ts +178 -0
- package/core/mandala/colorModule.ts +82 -0
- package/core/mandala/createRuntime.ts +483 -2392
- package/core/mandala/handle.ts +694 -0
- package/core/mandala/kvStore.ts +105 -0
- package/core/mandala/nearby.ts +185 -0
- package/core/mandala/receipt.ts +121 -0
- package/core/mandala/runtime.ts +166 -312
- package/core/mandala/settle.ts +540 -0
- package/core/mandala/transferJournal.ts +395 -0
- package/core/mandala/types.ts +61 -311
- package/core/networks.ts +5 -5
- package/core/offline/cancelParked.ts +80 -63
- package/core/offline/payerHold.ts +52 -118
- package/core/parsePeerPayURI.ts +25 -12
- package/core/pay/rails/handle.ts +15 -14
- package/core/pay/rails/index.ts +5 -3
- package/core/pay/rails/nearby.ts +25 -87
- package/core/profiles/assertProfileEmpty.ts +19 -10
- 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 +15 -4
- package/core/services/walletArgLimits.ts +17 -2
- package/core/storage/StorageExpoSQLite.ts +93 -229
- package/core/storage/encryptedDbImage.ts +325 -0
- package/core/storage/methods/processOfflineActions.ts +86 -136
- package/core/storage/schema/createTables.ts +9 -184
- package/core/toolboxConfig.ts +18 -1
- package/package.json +9 -8
- package/ui/bridgeSheet/ContextRelay.tsx +86 -0
- package/ui/bridgeSheet/relayedContexts.ts +21 -0
- package/ui/components/pay/AssetPicker.tsx +2 -2
- package/ui/components/pay/HandleReceive.tsx +4 -4
- package/ui/components/pay/NearbyFlow.tsx +324 -239
- package/ui/components/pay/RequestHub.tsx +10 -0
- package/ui/components/pay/UniversalSend.tsx +31 -28
- package/ui/components/pay/useRecipientInput.ts +2 -2
- package/ui/components/ui/PermissionSheet.tsx +625 -315
- package/ui/components/ui/Sheet.tsx +59 -7
- package/ui/components/ui/colorPrompt.ts +236 -0
- package/ui/components/ui/sheetResumeSignal.ts +19 -0
- package/ui/components/wallet/AmountInput.tsx +9 -6
- package/ui/components/wallet/AssetAmount.tsx +6 -6
- package/ui/components/wallet/AssetSwitcherDropdown.tsx +26 -7
- package/ui/components/wallet/Balance.tsx +3 -1
- package/ui/components/wallet/ContactActivityList.tsx +10 -13
- package/ui/components/wallet/tokenEviction.ts +9 -9
- package/ui/exportDatabases.ts +94 -5
- package/ui/hooks/useExportWalletData.ts +37 -24
- package/ui/hooks/useMandala.ts +6 -6
- package/ui/hooks/useSpendableBalance.ts +3 -1
- package/ui/importDatabases.ts +124 -29
- package/ui/index.ts +4 -0
- package/ui/screens/PayScreen.tsx +14 -20
- package/ui/screens/SettingsScreen.tsx +3 -1
- package/ui/screens/WalletCheckScreen.tsx +7 -2
- package/ui/screens/WalletConfigScreen.tsx +13 -1
- package/ui/screens/WalletHomeScreen.tsx +40 -14
- package/ui/screens/tokenRowTitle.ts +28 -15
- package/ui/tokenFormat.ts +133 -69
- package/ui/tokenStatus.ts +5 -8
- package/core/localpay/settlementAck.ts +0 -155
- package/core/mandala/basketMigration.ts +0 -166
- package/core/mandala/bundle.ts +0 -244
- package/core/mandala/drain.ts +0 -754
- package/core/mandala/inboxPrehold.ts +0 -193
- package/core/mandala/permissionModule.ts +0 -931
- package/core/mandala/resendTransfer.ts +0 -178
- package/core/mandala/settlementStore.ts +0 -434
- package/core/mandala/token.ts +0 -45
- package/core/offline/tokenFrames.ts +0 -197
|
@@ -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
|
+
}
|
package/core/demo/demoAssets.ts
CHANGED
|
@@ -13,8 +13,8 @@
|
|
|
13
13
|
import type { TokenAssetInfo } from '../mandala/runtime'
|
|
14
14
|
|
|
15
15
|
/** Demo overlay identity — 66 hex chars, the length a real one has. */
|
|
16
|
-
const DEMO_OVERLAY_KEY = '02' + 'de'.repeat(32)
|
|
17
|
-
const DEMO_OVERLAY_URL = 'https://overlay.demo.invalid'
|
|
16
|
+
export const DEMO_OVERLAY_KEY = '02' + 'de'.repeat(32)
|
|
17
|
+
export const DEMO_OVERLAY_URL = 'https://overlay.demo.invalid'
|
|
18
18
|
|
|
19
19
|
export const DEMO_USD: TokenAssetInfo = {
|
|
20
20
|
assetId: 'd0'.repeat(32) + '_0',
|
|
@@ -22,8 +22,7 @@ export const DEMO_USD: TokenAssetInfo = {
|
|
|
22
22
|
ticker: 'USDX',
|
|
23
23
|
decimals: 2,
|
|
24
24
|
issuerName: 'Acme Bank',
|
|
25
|
-
|
|
26
|
-
overlayIdentityKey: DEMO_OVERLAY_KEY
|
|
25
|
+
resolved: true
|
|
27
26
|
}
|
|
28
27
|
|
|
29
28
|
export const DEMO_CHF: TokenAssetInfo = {
|
|
@@ -32,8 +31,7 @@ export const DEMO_CHF: TokenAssetInfo = {
|
|
|
32
31
|
ticker: 'CHFX',
|
|
33
32
|
decimals: 2,
|
|
34
33
|
issuerName: 'Helvetia Bank',
|
|
35
|
-
|
|
36
|
-
overlayUrl: DEMO_OVERLAY_URL
|
|
34
|
+
resolved: true
|
|
37
35
|
}
|
|
38
36
|
|
|
39
37
|
export const DEMO_ASSETS: TokenAssetInfo[] = [DEMO_USD, DEMO_CHF]
|
|
@@ -45,10 +43,10 @@ export function demoAssetById(assetId: string): TokenAssetInfo | undefined {
|
|
|
45
43
|
/** Opening BSV balance, in satoshis. 0.25 BSV — enough to read as funded. */
|
|
46
44
|
export const DEMO_OPENING_SATS = 25_000_000
|
|
47
45
|
|
|
48
|
-
/** Opening token balances, in
|
|
49
|
-
export const
|
|
50
|
-
[DEMO_USD.assetId]:
|
|
51
|
-
[DEMO_CHF.assetId]:
|
|
46
|
+
/** Opening token balances, in exact token units (2 dp): 1,240.00 USDX / 860.50 CHFX. */
|
|
47
|
+
export const DEMO_OPENING_TOKEN_UNITS: Record<string, bigint> = {
|
|
48
|
+
[DEMO_USD.assetId]: 124_000n,
|
|
49
|
+
[DEMO_CHF.assetId]: 86_050n
|
|
52
50
|
}
|
|
53
51
|
|
|
54
52
|
/** Counterparties the demo history is transacted against. */
|
package/core/demo/demoLedger.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The demo wallet's whole world: one mutable, in-memory ledger.
|
|
3
3
|
*
|
|
4
|
-
* DEV-ONLY. Holds a BSV balance with its action history and
|
|
5
|
-
* balance per demo stablecoin with its settlement history, and mutates both
|
|
4
|
+
* DEV-ONLY. Holds a BSV balance with its action history and an exact token
|
|
5
|
+
* unit balance per demo stablecoin with its settlement history, and mutates both
|
|
6
6
|
* when the UI "sends". Nothing here touches SQLite, the keychain, an overlay
|
|
7
7
|
* or the network, so a demo send can never become a real one — and everything
|
|
8
8
|
* is gone at reload, which is what makes the mode safe to hand to someone.
|
|
9
9
|
*
|
|
10
|
-
* Figures are integers throughout (satoshis, token
|
|
11
|
-
* reason `ui/tokenFormat.ts` is: a float balance prints
|
|
12
|
-
* owned.
|
|
10
|
+
* Figures are integers throughout (satoshis as numbers, token units as
|
|
11
|
+
* `bigint`) for the same reason `ui/tokenFormat.ts` is: a float balance prints
|
|
12
|
+
* money that is not owned.
|
|
13
13
|
*/
|
|
14
14
|
import type { TokenActivityRow, TokenActivityStatus } from '../mandala/runtime'
|
|
15
15
|
import type { WalletAction } from '@bsv/sdk'
|
|
@@ -17,7 +17,7 @@ import {
|
|
|
17
17
|
DEMO_CHF,
|
|
18
18
|
DEMO_COUNTERPARTIES,
|
|
19
19
|
DEMO_OPENING_SATS,
|
|
20
|
-
|
|
20
|
+
DEMO_OPENING_TOKEN_UNITS,
|
|
21
21
|
DEMO_USD,
|
|
22
22
|
demoAssetById
|
|
23
23
|
} from './demoAssets'
|
|
@@ -32,7 +32,7 @@ export type DemoAction = WalletAction & {
|
|
|
32
32
|
interface DemoState {
|
|
33
33
|
satoshis: number
|
|
34
34
|
actions: DemoAction[]
|
|
35
|
-
|
|
35
|
+
tokenUnits: Record<string, bigint>
|
|
36
36
|
tokenActivity: TokenActivityRow[]
|
|
37
37
|
}
|
|
38
38
|
|
|
@@ -70,14 +70,14 @@ function bsvAction(over: Partial<DemoAction> & { satoshis: number; description:
|
|
|
70
70
|
function tokenRow(
|
|
71
71
|
assetId: string,
|
|
72
72
|
role: 'sent' | 'received',
|
|
73
|
-
|
|
73
|
+
units: bigint,
|
|
74
74
|
at: string,
|
|
75
75
|
counterpartyKey?: string,
|
|
76
76
|
status: TokenActivityStatus = 'settled'
|
|
77
77
|
): TokenActivityRow {
|
|
78
78
|
const asset = demoAssetById(assetId)
|
|
79
79
|
if (!asset) throw new Error(`demo: unknown assetId ${assetId}`)
|
|
80
|
-
return { txid: demoTxid(), role, asset,
|
|
80
|
+
return { txid: demoTxid(), role, asset, units, counterpartyKey, status, at, rail: 'handle' }
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
/**
|
|
@@ -98,16 +98,16 @@ function seed(): DemoState {
|
|
|
98
98
|
bsvAction({ satoshis: 15_000_000, description: `From ${lena.name}`, created_at: daysAgo(2, 13) }),
|
|
99
99
|
bsvAction({ satoshis: -1_000_000, description: 'Lunch', created_at: daysAgo(0, 12) })
|
|
100
100
|
],
|
|
101
|
-
|
|
101
|
+
tokenUnits: { ...DEMO_OPENING_TOKEN_UNITS },
|
|
102
102
|
tokenActivity: [
|
|
103
|
-
tokenRow(DEMO_USD.assetId, 'received',
|
|
104
|
-
tokenRow(DEMO_CHF.assetId, 'received',
|
|
105
|
-
tokenRow(DEMO_USD.assetId, 'sent',
|
|
106
|
-
tokenRow(DEMO_CHF.assetId, 'sent',
|
|
107
|
-
tokenRow(DEMO_USD.assetId, 'received',
|
|
108
|
-
tokenRow(DEMO_CHF.assetId, 'received',
|
|
103
|
+
tokenRow(DEMO_USD.assetId, 'received', 50_000n, daysAgo(16, 11), sofia.key),
|
|
104
|
+
tokenRow(DEMO_CHF.assetId, 'received', 40_000n, daysAgo(13, 15), lena.key),
|
|
105
|
+
tokenRow(DEMO_USD.assetId, 'sent', 12_500n, daysAgo(9, 19), store.key),
|
|
106
|
+
tokenRow(DEMO_CHF.assetId, 'sent', 7_450n, daysAgo(6, 12), marco.key),
|
|
107
|
+
tokenRow(DEMO_USD.assetId, 'received', 86_500n, daysAgo(3, 14), marco.key),
|
|
108
|
+
tokenRow(DEMO_CHF.assetId, 'received', 53_500n, daysAgo(1, 17), sofia.key),
|
|
109
109
|
// One row mid-flight, so the "settling" copy has something to render.
|
|
110
|
-
tokenRow(DEMO_USD.assetId, 'sent',
|
|
110
|
+
tokenRow(DEMO_USD.assetId, 'sent', 2_000n, daysAgo(0, 11), lena.key, 'settling')
|
|
111
111
|
]
|
|
112
112
|
}
|
|
113
113
|
}
|
|
@@ -148,8 +148,8 @@ export function demoTotalActions(): number {
|
|
|
148
148
|
return state.actions.length
|
|
149
149
|
}
|
|
150
150
|
|
|
151
|
-
export function
|
|
152
|
-
return state.
|
|
151
|
+
export function demoTokenUnits(assetId: string): bigint {
|
|
152
|
+
return state.tokenUnits[assetId] ?? 0n
|
|
153
153
|
}
|
|
154
154
|
|
|
155
155
|
export function demoTokenActivity(limit?: number): TokenActivityRow[] {
|
|
@@ -157,11 +157,11 @@ export function demoTokenActivity(limit?: number): TokenActivityRow[] {
|
|
|
157
157
|
return limit == null ? sorted : sorted.slice(0, limit)
|
|
158
158
|
}
|
|
159
159
|
|
|
160
|
-
/**
|
|
161
|
-
export function
|
|
160
|
+
/** Units received but not yet settled — what the UI shows as pending. */
|
|
161
|
+
export function demoOfflineUnits(assetId: string): bigint {
|
|
162
162
|
return state.tokenActivity
|
|
163
163
|
.filter(r => r.asset.assetId === assetId && r.role === 'received' && r.status === 'settling')
|
|
164
|
-
.reduce((sum, r) => sum + r.
|
|
164
|
+
.reduce((sum, r) => sum + r.units, 0n)
|
|
165
165
|
}
|
|
166
166
|
|
|
167
167
|
// ── writes ─────────────────────────────────────────────────────────────
|
|
@@ -194,16 +194,15 @@ export function demoReceiveBsv(satoshis: number, description: string): DemoActio
|
|
|
194
194
|
}
|
|
195
195
|
|
|
196
196
|
/** Spend a token. Returns the new row, or null when the balance cannot cover it. */
|
|
197
|
-
export function demoSendToken(assetId: string,
|
|
197
|
+
export function demoSendToken(assetId: string, units: bigint, counterpartyKey?: string): TokenActivityRow | null {
|
|
198
198
|
if (!demoAssetById(assetId)) return null
|
|
199
|
-
if (
|
|
200
|
-
const held =
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
const row = tokenRow(assetId, 'sent', amount, new Date().toISOString(), counterpartyKey, 'settled')
|
|
199
|
+
if (typeof units !== 'bigint' || units <= 0n) return null
|
|
200
|
+
const held = demoTokenUnits(assetId)
|
|
201
|
+
if (units > held) return null
|
|
202
|
+
const row = tokenRow(assetId, 'sent', units, new Date().toISOString(), counterpartyKey, 'settled')
|
|
204
203
|
state = {
|
|
205
204
|
...state,
|
|
206
|
-
|
|
205
|
+
tokenUnits: { ...state.tokenUnits, [assetId]: held - units },
|
|
207
206
|
tokenActivity: [...state.tokenActivity, row]
|
|
208
207
|
}
|
|
209
208
|
notify()
|
|
@@ -211,18 +210,13 @@ export function demoSendToken(assetId: string, baseUnits: number, counterpartyKe
|
|
|
211
210
|
}
|
|
212
211
|
|
|
213
212
|
/** Receive a token — for demonstrating an incoming payment on cue. */
|
|
214
|
-
export function demoReceiveToken(
|
|
215
|
-
assetId: string,
|
|
216
|
-
baseUnits: number,
|
|
217
|
-
counterpartyKey?: string
|
|
218
|
-
): TokenActivityRow | null {
|
|
213
|
+
export function demoReceiveToken(assetId: string, units: bigint, counterpartyKey?: string): TokenActivityRow | null {
|
|
219
214
|
if (!demoAssetById(assetId)) return null
|
|
220
|
-
if (
|
|
221
|
-
const
|
|
222
|
-
const row = tokenRow(assetId, 'received', amount, new Date().toISOString(), counterpartyKey, 'settled')
|
|
215
|
+
if (typeof units !== 'bigint' || units <= 0n) return null
|
|
216
|
+
const row = tokenRow(assetId, 'received', units, new Date().toISOString(), counterpartyKey, 'settled')
|
|
223
217
|
state = {
|
|
224
218
|
...state,
|
|
225
|
-
|
|
219
|
+
tokenUnits: { ...state.tokenUnits, [assetId]: demoTokenUnits(assetId) + units },
|
|
226
220
|
tokenActivity: [...state.tokenActivity, row]
|
|
227
221
|
}
|
|
228
222
|
notify()
|
|
@@ -2,52 +2,69 @@
|
|
|
2
2
|
* A `MandalaRuntime` backed entirely by the demo ledger.
|
|
3
3
|
*
|
|
4
4
|
* DEV-ONLY. Implements the members the UI actually reads or calls; everything
|
|
5
|
-
* else on the interface is
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* else on the interface is settlement machinery that has nothing to settle
|
|
6
|
+
* here, so it answers "nothing happened" rather than throwing — a demo must
|
|
7
|
+
* never crash on a background tick.
|
|
8
8
|
*
|
|
9
9
|
* `endpoints` deliberately carries the `.invalid` demo overlay rather than the
|
|
10
10
|
* configured one: if any code path ever did try to settle one of these rows,
|
|
11
|
-
* it must fail to resolve rather than reach a real issuer.
|
|
11
|
+
* it must fail to resolve rather than reach a real issuer. The journal refuses
|
|
12
|
+
* every call for the same reason.
|
|
12
13
|
*/
|
|
13
|
-
import type {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
} from './
|
|
14
|
+
import type {
|
|
15
|
+
MandalaRuntime,
|
|
16
|
+
TokenAssetInfo,
|
|
17
|
+
TokenAssetStatus,
|
|
18
|
+
TokenBalance,
|
|
19
|
+
TokenSendResult
|
|
20
|
+
} from '../mandala/runtime'
|
|
21
|
+
import type { TransferJournal } from '../mandala/transferJournal'
|
|
22
|
+
import { DEMO_ASSETS, DEMO_OVERLAY_KEY, DEMO_OVERLAY_URL } from './demoAssets'
|
|
23
|
+
import { demoOfflineUnits, demoSendToken, demoTokenActivity, demoTokenUnits, subscribeDemoLedger } from './demoLedger'
|
|
24
|
+
|
|
25
|
+
const DEMO_MESSAGEBOX_URL = 'https://messagebox.demo.invalid'
|
|
22
26
|
|
|
23
27
|
function statusFor(): TokenAssetStatus {
|
|
24
28
|
return {
|
|
25
29
|
paused: false,
|
|
26
|
-
|
|
30
|
+
frozenUnits: 0n,
|
|
27
31
|
selfAdmitted: true,
|
|
28
32
|
recipientAdmitted: async () => true,
|
|
29
33
|
metadataResolved: true,
|
|
30
|
-
messageBoxUrl:
|
|
34
|
+
messageBoxUrl: DEMO_MESSAGEBOX_URL
|
|
31
35
|
}
|
|
32
36
|
}
|
|
33
37
|
|
|
34
38
|
function balanceFor(asset: TokenAssetInfo): TokenBalance {
|
|
39
|
+
const units = demoTokenUnits(asset.assetId)
|
|
40
|
+
const offline = demoOfflineUnits(asset.assetId)
|
|
41
|
+
return { asset, units, offlineUnits: offline > units ? units : offline }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function demoJournal(): TransferJournal {
|
|
45
|
+
const refuse = async (): Promise<never> => {
|
|
46
|
+
throw new Error('demo mode has no color_transfers journal')
|
|
47
|
+
}
|
|
35
48
|
return {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
49
|
+
get: refuse,
|
|
50
|
+
getByReference: refuse,
|
|
51
|
+
list: refuse,
|
|
52
|
+
insert: refuse,
|
|
53
|
+
advance: refuse,
|
|
54
|
+
setDelivered: refuse,
|
|
55
|
+
statesOf: refuse
|
|
39
56
|
}
|
|
40
57
|
}
|
|
41
58
|
|
|
42
59
|
export function createDemoMandalaRuntime(): MandalaRuntime {
|
|
43
|
-
const runtime = {
|
|
60
|
+
const runtime: MandalaRuntime = {
|
|
44
61
|
available: true,
|
|
45
62
|
endpoints: {
|
|
46
|
-
overlayUrl:
|
|
47
|
-
overlayIdentityKey:
|
|
48
|
-
messageBoxUrl:
|
|
63
|
+
overlayUrl: DEMO_OVERLAY_URL,
|
|
64
|
+
overlayIdentityKey: DEMO_OVERLAY_KEY,
|
|
65
|
+
messageBoxUrl: DEMO_MESSAGEBOX_URL
|
|
49
66
|
},
|
|
50
|
-
|
|
67
|
+
journal: demoJournal(),
|
|
51
68
|
|
|
52
69
|
// ── what the UI reads ───────────────────────────────────────────
|
|
53
70
|
listAssets: async () => DEMO_ASSETS,
|
|
@@ -56,41 +73,42 @@ export function createDemoMandalaRuntime(): MandalaRuntime {
|
|
|
56
73
|
stuck: async () => [],
|
|
57
74
|
assetStatus: async () => statusFor(),
|
|
58
75
|
refreshAssetStatus: async () => statusFor(),
|
|
76
|
+
rememberDeploy: async () => {},
|
|
59
77
|
recipientRefusal: () => null,
|
|
60
78
|
subscribe: subscribeDemoLedger,
|
|
61
79
|
|
|
62
80
|
// ── what the UI calls ───────────────────────────────────────────
|
|
63
|
-
sendToHandle: async (args: {
|
|
64
|
-
|
|
81
|
+
sendToHandle: async (args: {
|
|
82
|
+
assetId: string
|
|
83
|
+
recipientIdentityKey: string
|
|
84
|
+
units: bigint
|
|
85
|
+
}): Promise<TokenSendResult> => {
|
|
86
|
+
const row = demoSendToken(args.assetId, args.units, args.recipientIdentityKey)
|
|
65
87
|
if (!row) {
|
|
66
|
-
return {
|
|
67
|
-
kind: 'refused' as const,
|
|
68
|
-
code: 'ERR_INSUFFICIENT_FUNDS',
|
|
69
|
-
message: 'Not enough balance in demo mode.'
|
|
70
|
-
}
|
|
88
|
+
return { kind: 'refused', code: 'ERR_INSUFFICIENT_FUNDS', message: 'Not enough balance in demo mode.' }
|
|
71
89
|
}
|
|
72
|
-
return { kind: 'sent'
|
|
90
|
+
return { kind: 'sent', txid: row.txid, settled: true, notified: true }
|
|
73
91
|
},
|
|
74
92
|
receiveFromInbox: async () => ({ credited: 0, failed: 0 }),
|
|
75
|
-
settleNow: async () => '
|
|
76
|
-
|
|
93
|
+
settleNow: async () => 'settled',
|
|
94
|
+
resendTransfer: async () => 'unavailable',
|
|
77
95
|
|
|
78
96
|
// ── machinery with nothing to do ────────────────────────────────
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
97
|
+
settleAll: async () => 0,
|
|
98
|
+
reviewTokenHoldings: async () => ({ settled: 0, refused: 0, pending: 0, unreachable: false }),
|
|
99
|
+
isHandoverAdmitted: async () => undefined,
|
|
100
|
+
reconcileHeld: async () => {},
|
|
101
|
+
tick: async () => {},
|
|
102
|
+
nearby: {
|
|
103
|
+
handover: async () => {
|
|
104
|
+
throw new Error('demo mode does not build nearby token payments')
|
|
105
|
+
},
|
|
106
|
+
onHandedOver: async () => {},
|
|
107
|
+
accept: async () => {
|
|
108
|
+
throw new Error('demo mode does not receive nearby token payments')
|
|
109
|
+
},
|
|
110
|
+
onReceived: async () => {}
|
|
111
|
+
}
|
|
88
112
|
}
|
|
89
|
-
|
|
90
|
-
// The remaining members are injection points for machinery demo mode never
|
|
91
|
-
// starts (the drain, the nearby build, the frame verifier, the pending
|
|
92
|
-
// queue). Casting rather than stubbing each one keeps this file honest about
|
|
93
|
-
// the fact that nothing here is a real settlement path — the same approach
|
|
94
|
-
// `__tests__/__mocks__/fakeMandalaRuntime.ts` takes.
|
|
95
|
-
return runtime as unknown as MandalaRuntime
|
|
113
|
+
return runtime
|
|
96
114
|
}
|
package/core/demo/index.ts
CHANGED
|
@@ -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
|
}
|