@bsv/expo-wallet-toolbox 0.3.1 → 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 +311 -0
- package/README.md +52 -1
- package/assets/sounds/vault-deposit.wav +0 -0
- package/assets/sounds/vault-withdraw.wav +0 -0
- package/core/config.tsx +21 -18
- package/core/context/VaultContext.tsx +8 -11
- package/core/context/WalletConnectionContext.tsx +137 -52
- package/core/context/WalletContext.tsx +413 -23
- package/core/hooks/useConfirmationSound.ts +14 -8
- package/core/i18n/translations.tsx +3368 -952
- package/core/index.ts +107 -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/services/walletServiceConfig.ts +19 -24
- 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 +240 -0
- 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 +42 -20
- 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
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resend for a TOKEN transfer, whatever rail it first went out on.
|
|
3
|
+
*
|
|
4
|
+
* "The counterparty says they never got it" has one honest answer for a
|
|
5
|
+
* stablecoin: deliver the transfer notification again, over the message box,
|
|
6
|
+
* because that is the one rail that confirms delivery and the one inbox every
|
|
7
|
+
* recipient wallet drains. A nearby hand-over whose code was never scanned, a
|
|
8
|
+
* handle-rail transfer whose notify failed and fell out of the lib's journal —
|
|
9
|
+
* both end here with the SAME body `transferTokens` sends at transfer time, so
|
|
10
|
+
* the recipient's `receiveTokens` needs nothing new to credit it (it acks by
|
|
11
|
+
* messageId and treats an already-internalized output as a no-op).
|
|
12
|
+
*
|
|
13
|
+
* Everything the body needs is already on the payer's device, on both rails:
|
|
14
|
+
*
|
|
15
|
+
* · recipient, the blinded sender A′ and the keyID — the lib's blinding
|
|
16
|
+
* journal, written by `transferTokens` (`blindingPut`) and by the nearby
|
|
17
|
+
* build (`blindingReserve` + `commitBlinding`). Fallback: the payee output's
|
|
18
|
+
* own `recipientCustomInstructions` marker, which both rails write too;
|
|
19
|
+
* · which output is the payee's — that same marker, because the handle rail
|
|
20
|
+
* randomises output order;
|
|
21
|
+
* · the transaction — the existing network-first / local-second resend lookup;
|
|
22
|
+
* · assetId and amount — DECODED from the output's script, never a stored claim;
|
|
23
|
+
* · a cached σ_I when this device holds one, so the recipient can credit
|
|
24
|
+
* without waiting on its own overlay round-trip (§4.5). Optional by design.
|
|
25
|
+
*
|
|
26
|
+
* Nothing here contacts the overlay, decides settlement, or touches the
|
|
27
|
+
* transaction's own status: a resend is a delivery, not a payment.
|
|
28
|
+
*/
|
|
29
|
+
import { Transaction } from '@bsv/sdk'
|
|
30
|
+
import { MandalaToken } from '@bsv/templates'
|
|
31
|
+
import { isDuplicateMessageError } from '../peerpay/control'
|
|
32
|
+
|
|
33
|
+
/** The recipient-side inbox `receiveTokens` drains (`@bsv/mandala` `MESSAGEBOX`). */
|
|
34
|
+
export const MANDALA_MESSAGE_BOX = 'mandala-payments'
|
|
35
|
+
|
|
36
|
+
/** The FT derivation protocol, as the lib's `FT_PROTOCOL`. */
|
|
37
|
+
const FT_PROTOCOL: [number, string] = [2, 'mandala token']
|
|
38
|
+
|
|
39
|
+
export type TokenResendFailure = 'no_record' | 'no_transaction'
|
|
40
|
+
export type TokenResendOutcome = { ok: true } | { ok: false; reason: TokenResendFailure }
|
|
41
|
+
|
|
42
|
+
/** What the payer remembers about who the payment was for and how it was locked. */
|
|
43
|
+
export interface TokenResendRecord {
|
|
44
|
+
recipient: string
|
|
45
|
+
/** A′ — what went on the wire as the sender; the recipient derives against it. */
|
|
46
|
+
senderBlinded: string
|
|
47
|
+
keyID: string
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface TokenResendAction {
|
|
51
|
+
labels?: string[]
|
|
52
|
+
outputs?: { outputIndex?: number; customInstructions?: string }[]
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface PendingTokenNotification {
|
|
56
|
+
txid: string
|
|
57
|
+
recipient: string
|
|
58
|
+
messageBox: string
|
|
59
|
+
body: object
|
|
60
|
+
at: number
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface TokenResendDeps {
|
|
64
|
+
/** The lib's blinding journal, by txid. */
|
|
65
|
+
blindingRecord: (txid: string) => Promise<TokenResendRecord | undefined>
|
|
66
|
+
/** The `mandala`-labelled wallet action for the txid, with its outputs. */
|
|
67
|
+
listAction: (txid: string) => Promise<TokenResendAction | undefined>
|
|
68
|
+
/** AtomicBEEF for the txid — `makeResendBeef` in production. */
|
|
69
|
+
refetch: (txid: string) => Promise<number[] | undefined>
|
|
70
|
+
/** This device's cached σ_I for the txid, if any. */
|
|
71
|
+
admission: (
|
|
72
|
+
txid: string
|
|
73
|
+
) => Promise<{ outputsToAdmit: number[]; signatureHex: string; signerKey: string } | undefined>
|
|
74
|
+
/** The lib's notification journal: `notifyPut` / `notifyRemove`. */
|
|
75
|
+
journal: { put(entry: PendingTokenNotification): Promise<void>; remove(txid: string): Promise<void> }
|
|
76
|
+
sendMessage: (args: { recipient: string; messageBox: string; body: object }) => Promise<unknown>
|
|
77
|
+
/** Unwraps metadata the permissions manager encrypted on its way to storage. */
|
|
78
|
+
decryptMetadata?: (value: string) => Promise<string>
|
|
79
|
+
now?: () => number
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
interface Marker extends Partial<TokenResendRecord> {
|
|
83
|
+
outputIndex: number
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The payee output's `recipientCustomInstructions`, and which output carried it. */
|
|
87
|
+
async function findMarker(action: TokenResendAction | undefined, decrypt?: (v: string) => Promise<string>): Promise<Marker | undefined> {
|
|
88
|
+
for (const output of action?.outputs ?? []) {
|
|
89
|
+
const raw = output.customInstructions
|
|
90
|
+
if (typeof raw !== 'string' || raw === '') continue
|
|
91
|
+
let text = raw
|
|
92
|
+
if (decrypt) {
|
|
93
|
+
try {
|
|
94
|
+
text = await decrypt(raw)
|
|
95
|
+
} catch {
|
|
96
|
+
// Not encrypted, or not ours to read — try it as it is.
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
let parsed: Record<string, unknown>
|
|
100
|
+
try {
|
|
101
|
+
parsed = JSON.parse(text) as Record<string, unknown>
|
|
102
|
+
} catch {
|
|
103
|
+
continue
|
|
104
|
+
}
|
|
105
|
+
if (typeof parsed.senderBlinded !== 'string') continue
|
|
106
|
+
return {
|
|
107
|
+
outputIndex: typeof output.outputIndex === 'number' ? output.outputIndex : 0,
|
|
108
|
+
recipient: typeof parsed.recipient === 'string' ? parsed.recipient : undefined,
|
|
109
|
+
senderBlinded: parsed.senderBlinded,
|
|
110
|
+
keyID: typeof parsed.keyID === 'string' ? parsed.keyID : undefined
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return undefined
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export async function resendTokenTransfer(txid: string, deps: TokenResendDeps): Promise<TokenResendOutcome> {
|
|
117
|
+
const [journaled, action] = await Promise.all([deps.blindingRecord(txid), deps.listAction(txid)])
|
|
118
|
+
const marker = await findMarker(action, deps.decryptMetadata)
|
|
119
|
+
|
|
120
|
+
const recipient = journaled?.recipient ?? marker?.recipient
|
|
121
|
+
const senderBlinded = journaled?.senderBlinded ?? marker?.senderBlinded
|
|
122
|
+
const keyID = journaled?.keyID ?? marker?.keyID
|
|
123
|
+
if (!recipient || !senderBlinded || !keyID) return { ok: false, reason: 'no_record' }
|
|
124
|
+
// The nearby build never randomises outputs, so a record with no marker
|
|
125
|
+
// (an older action whose instructions did not survive) still names index 0.
|
|
126
|
+
const outputIndex = marker?.outputIndex ?? 0
|
|
127
|
+
|
|
128
|
+
const transaction = await deps.refetch(txid)
|
|
129
|
+
if (!transaction) return { ok: false, reason: 'no_transaction' }
|
|
130
|
+
let assetId: string
|
|
131
|
+
let amount: number
|
|
132
|
+
try {
|
|
133
|
+
const script = Transaction.fromAtomicBEEF(transaction).outputs[outputIndex]?.lockingScript
|
|
134
|
+
if (!script) return { ok: false, reason: 'no_transaction' }
|
|
135
|
+
;({ assetId, amount } = MandalaToken.decode(script))
|
|
136
|
+
} catch {
|
|
137
|
+
return { ok: false, reason: 'no_transaction' }
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const cached = await deps.admission(txid)
|
|
141
|
+
const body = {
|
|
142
|
+
assetId,
|
|
143
|
+
amount: String(amount),
|
|
144
|
+
transaction,
|
|
145
|
+
keyID,
|
|
146
|
+
outputIndex,
|
|
147
|
+
protocolID: FT_PROTOCOL,
|
|
148
|
+
sender: senderBlinded,
|
|
149
|
+
senderMode: 'blinded' as const,
|
|
150
|
+
...(cached
|
|
151
|
+
? {
|
|
152
|
+
admission: {
|
|
153
|
+
txid,
|
|
154
|
+
outputsToAdmit: cached.outputsToAdmit,
|
|
155
|
+
signature: cached.signatureHex,
|
|
156
|
+
signerKey: cached.signerKey
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
: {})
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Journal FIRST, exactly as `transferTokens` does: a send that fails after
|
|
163
|
+
// this is retried by `reconcileNotifications` on the next drain tick, and a
|
|
164
|
+
// duplicate delivery is safe on the receiving side.
|
|
165
|
+
await deps.journal.put({ txid, recipient, messageBox: MANDALA_MESSAGE_BOX, body, at: (deps.now ?? Date.now)() })
|
|
166
|
+
try {
|
|
167
|
+
await deps.sendMessage({ recipient, messageBox: MANDALA_MESSAGE_BOX, body })
|
|
168
|
+
} catch (e) {
|
|
169
|
+
// The box already holds this exact message: the recipient has what the
|
|
170
|
+
// resend was trying to give them, which is the wanted end state.
|
|
171
|
+
if (!isDuplicateMessageError(e)) throw e
|
|
172
|
+
}
|
|
173
|
+
await deps.journal.remove(txid)
|
|
174
|
+
return { ok: true }
|
|
175
|
+
}
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MandalaRuntime — the single object the UI layer consumes for stablecoin
|
|
3
|
+
* features. Constructed once in WalletContext (core wiring), exposed via
|
|
4
|
+
* `useMandala()`. Pure types here; the implementation lives in
|
|
5
|
+
* core/mandala/createRuntime.ts (integration) and the hook in
|
|
6
|
+
* core/hooks/useMandala.ts.
|
|
7
|
+
*
|
|
8
|
+
* Design rule (ux §1): the UI never talks to the overlay or the lib directly;
|
|
9
|
+
* everything goes through this surface so failure copy, journaling and
|
|
10
|
+
* state live in one place.
|
|
11
|
+
*/
|
|
12
|
+
import type {
|
|
13
|
+
AdmissionVerifier,
|
|
14
|
+
CoverResult,
|
|
15
|
+
FetchAdmissionFn,
|
|
16
|
+
SettlementStore,
|
|
17
|
+
TokenHandedOverHook,
|
|
18
|
+
TokenHeldHook,
|
|
19
|
+
TokenSettlementRow,
|
|
20
|
+
TokenSettlementState
|
|
21
|
+
} from './types'
|
|
22
|
+
import type { LockToPayee, TokenBuildDeps } from '../localpay/build'
|
|
23
|
+
import type { CoverVerifier } from './bundle'
|
|
24
|
+
import type { VerifyAdmissionFn } from '../localpay/settlementAck'
|
|
25
|
+
import type { TokenCreditedHook } from '../localpay/pending'
|
|
26
|
+
import type { OfflineTokenDeps } from '../storage/methods/processOfflineActions'
|
|
27
|
+
import type { MandalaEndpointConfig } from '../toolboxConfig'
|
|
28
|
+
import type { TokenResendOutcome } from './resendTransfer'
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Regulatory/registry facts about one asset, for the UI's remaining copy
|
|
32
|
+
* (paused banners, frozen-balance notices, access-mode refusals, admission
|
|
33
|
+
* status). Everything here is best-effort and short-lived (~10s cache in the
|
|
34
|
+
* implementation) — a stale or unreachable overlay reads as the least
|
|
35
|
+
* alarming answer (`false`/`undefined`), never as a thrown error, because
|
|
36
|
+
* this is display copy, not a gate anything transacts against.
|
|
37
|
+
*/
|
|
38
|
+
export interface TokenAssetStatus {
|
|
39
|
+
/** The issuer has paused this asset. */
|
|
40
|
+
paused: boolean
|
|
41
|
+
/** Sum of amounts across the asset's frozen outpoints, per the overlay's admin state. */
|
|
42
|
+
frozenBaseUnits: number
|
|
43
|
+
/** 'allowlist' | 'denylist', when the overlay reports one. */
|
|
44
|
+
accessMode?: string
|
|
45
|
+
/** This wallet's own identity key, admitted under the asset's issuer registry.
|
|
46
|
+
* `undefined` when the registry is inactive (no membership gate in effect) or unknown. */
|
|
47
|
+
selfAdmitted: boolean | undefined
|
|
48
|
+
/** Same question for another identity key — e.g. a recipient before a send. */
|
|
49
|
+
recipientAdmitted(identityKey: string): Promise<boolean | undefined>
|
|
50
|
+
/** Whether the asset's on-chain label/ticker/decimals resolved (registry lookup + SPV). */
|
|
51
|
+
metadataResolved: boolean
|
|
52
|
+
/** The MessageBox host this deployment's handle rail uses. */
|
|
53
|
+
messageBoxUrl: string
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface TokenAssetInfo {
|
|
57
|
+
assetId: string
|
|
58
|
+
label: string // e.g. "Acme Dollar"
|
|
59
|
+
ticker: string // e.g. "USDX"
|
|
60
|
+
decimals: number
|
|
61
|
+
issuerName?: string // e.g. "Acme Bank" — used in every failure/settling copy
|
|
62
|
+
overlayUrl: string
|
|
63
|
+
overlayIdentityKey: string
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface TokenBalance {
|
|
67
|
+
asset: TokenAssetInfo
|
|
68
|
+
/** Spendable base units (sum of unspent outputs in the token basket). */
|
|
69
|
+
baseUnits: number
|
|
70
|
+
/** Base units in rows whose settlement is not yet 'broadcast' (received offline). */
|
|
71
|
+
unsettledBaseUnits: number
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface TokenHoldingsReview {
|
|
75
|
+
settled: number
|
|
76
|
+
removed: number
|
|
77
|
+
unattested: number
|
|
78
|
+
unreachable: boolean
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export type TokenActivityStatus = 'settling' | 'settled' | 'refused' | 'reversed' | 'stuck'
|
|
82
|
+
|
|
83
|
+
export interface TokenActivityRow {
|
|
84
|
+
txid: string
|
|
85
|
+
role: 'sent' | 'received'
|
|
86
|
+
asset: TokenAssetInfo
|
|
87
|
+
baseUnits: number
|
|
88
|
+
counterpartyKey?: string
|
|
89
|
+
status: TokenActivityStatus
|
|
90
|
+
at: string // ISO
|
|
91
|
+
refusedCode?: string
|
|
92
|
+
/** `payloadHash` of the linkage bytes this refusal was recorded against (amendment §9.1/§9.3). */
|
|
93
|
+
refusedPayloadHash?: string
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Handle (MessageBox) rail results — mirror the wallet's existing rail result shapes. */
|
|
97
|
+
export type TokenSendResult =
|
|
98
|
+
| {
|
|
99
|
+
kind: 'sent'
|
|
100
|
+
txid: string
|
|
101
|
+
/**
|
|
102
|
+
* The issuer's overlay has this transaction (`admitted`, or `broadcast`
|
|
103
|
+
* on top of it) — because a hand-over that landed while this device had
|
|
104
|
+
* signal is followed straight away by the payer's own submit
|
|
105
|
+
* (`settleNow`). False is not a failure and never a reason to retry the
|
|
106
|
+
* SEND: the money moved at hand-over, and the drain finishes the
|
|
107
|
+
* settlement on its next tick.
|
|
108
|
+
*/
|
|
109
|
+
settled: boolean
|
|
110
|
+
/**
|
|
111
|
+
* False when the transaction committed but the recipient's MessageBox
|
|
112
|
+
* notification did not go out (the lib's `TransferResult.notified`).
|
|
113
|
+
*
|
|
114
|
+
* The money is sent either way — the notify is journaled by the lib and
|
|
115
|
+
* retried by `reconcileNotifications` on the next drain tick — but until
|
|
116
|
+
* it lands the recipient has an output nothing has told them to
|
|
117
|
+
* internalize. The UI says "recipient notification pending" rather than
|
|
118
|
+
* implying either that the payment failed (it did not) or that the payee
|
|
119
|
+
* can already see it (they cannot). NEVER a reason to retry the send: a
|
|
120
|
+
* second send would be a second payment.
|
|
121
|
+
*/
|
|
122
|
+
notified: boolean
|
|
123
|
+
}
|
|
124
|
+
| { kind: 'refused'; code: string; message: string }
|
|
125
|
+
| { kind: 'unavailable'; message: string }
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Where one row stood when `settleNow` let go of it.
|
|
129
|
+
*
|
|
130
|
+
* Every `TokenSettlementState` can come back, because the answer is simply the
|
|
131
|
+
* row's state re-read after the step: `'admitted'`/`'broadcast'` are the two
|
|
132
|
+
* that mean settled, `'refused'`/`'orphaned'` are the overlay's final word, and
|
|
133
|
+
* an unchanged `'handed_over'`/`'held'` means the step stalled and the ordinary
|
|
134
|
+
* drain will try again. `'unavailable'` is the one addition — no such row, no
|
|
135
|
+
* database, or no Mandala deployment on this chain — and is never a verdict
|
|
136
|
+
* about the money.
|
|
137
|
+
*/
|
|
138
|
+
export type TokenSettleState = TokenSettlementState | 'unavailable'
|
|
139
|
+
|
|
140
|
+
export interface MandalaRuntime {
|
|
141
|
+
/** Chain gate: mainnet-only in v1 (ux §2). */
|
|
142
|
+
available: boolean
|
|
143
|
+
/**
|
|
144
|
+
* THIS DEVICE's configured Mandala deployment — the §9.10 trust anchor, in
|
|
145
|
+
* the shape every call site that has to state one wants (`verifyFramePayment`'s
|
|
146
|
+
* `opts.asset`, a session's own `asset` block).
|
|
147
|
+
*
|
|
148
|
+
* Exposed because the alternative is each call site reaching for whatever
|
|
149
|
+
* overlay facts are nearest to hand, and the nearest ones are on the incoming
|
|
150
|
+
* FRAME. A frame's `overlayUrl`/`overlayIdentityKey` are the payer's claim;
|
|
151
|
+
* verifying a payer's σ_I against the payer's own key is not a check at all.
|
|
152
|
+
* Every field is `''` when this chain has no Mandala deployment
|
|
153
|
+
* (`available === false`), which no frame can ever equal.
|
|
154
|
+
*/
|
|
155
|
+
endpoints: MandalaEndpointConfig
|
|
156
|
+
store: SettlementStore
|
|
157
|
+
/** Assets this wallet currently holds or has ever received. */
|
|
158
|
+
listAssets(): Promise<TokenAssetInfo[]>
|
|
159
|
+
balances(): Promise<TokenBalance[]>
|
|
160
|
+
activity(limit?: number): Promise<TokenActivityRow[]>
|
|
161
|
+
/** Non-terminal rows older than STUCK_AFTER_MS (FIX M). */
|
|
162
|
+
stuck(): Promise<TokenSettlementRow[]>
|
|
163
|
+
/** Handle rail: send to an identity key over MessageBox (lib transferTokens). */
|
|
164
|
+
sendToHandle(args: {
|
|
165
|
+
assetId: string
|
|
166
|
+
recipientIdentityKey: string
|
|
167
|
+
baseUnits: number
|
|
168
|
+
/** The sender's own note — becomes the recipient's action description in
|
|
169
|
+
* place of the lib's fixed "Receive N of assetId" wording. */
|
|
170
|
+
note?: string
|
|
171
|
+
}): Promise<TokenSendResult>
|
|
172
|
+
/** Handle rail: drain the MessageBox inbox (lib receiveTokens), credit, journal. */
|
|
173
|
+
receiveFromInbox(): Promise<{ credited: number; failed: number }>
|
|
174
|
+
/** Nearby rail: the blinded payee lock the localpay token build needs (lib prepareBlindedPayment). */
|
|
175
|
+
lockToPayee: LockToPayee
|
|
176
|
+
/** Pure COVER verifier over local evidence (lib cover), for verify.ts at hand-over. */
|
|
177
|
+
cover(tipTxid: string): Promise<CoverResult>
|
|
178
|
+
/** Run one settlement drain pass now (submit-then-broadcast). */
|
|
179
|
+
drainNow(): Promise<void>
|
|
180
|
+
/**
|
|
181
|
+
* ONE row's settlement step, taken now rather than on the next drain tick
|
|
182
|
+
* (2026-09-15 maintainer refinement to §4.3's "payer's optional submit").
|
|
183
|
+
*
|
|
184
|
+
* Hand-over is still first and still unconditional — this is only ever called
|
|
185
|
+
* AFTER the payee has the bytes (a MessageBox post accepted, a nearby
|
|
186
|
+
* positive ack) — but once the hand-over has landed and this device has
|
|
187
|
+
* signal, there is no reason to make the holder wait a drain interval for
|
|
188
|
+
* their own money to reach the issuer. So this runs exactly what the drain
|
|
189
|
+
* would have run for this txid: COVER, `/submit` parents-first, and a
|
|
190
|
+
* broadcast of the tip only once every ancestor is admitted.
|
|
191
|
+
*
|
|
192
|
+
* **Never a failure path.** It reports where the row ended up; it does not
|
|
193
|
+
* throw, and a caller that cannot reach the overlay simply gets the state it
|
|
194
|
+
* started with back, because the drain still owns the retry. The two states
|
|
195
|
+
* the USER owns (`'built'`, `'parked'`) are returned untouched — FIX F: a
|
|
196
|
+
* payment the payer deliberately withheld is not settled by anybody's
|
|
197
|
+
* optimisation.
|
|
198
|
+
*
|
|
199
|
+
* **Safe to race with the tick drain.** Every advance is a CAS
|
|
200
|
+
* (`advanceSettlement`) and `/submit` is idempotent (FIX C), so the worst a
|
|
201
|
+
* lost race costs is one duplicate submit that returns the same admission.
|
|
202
|
+
* Two overlapping `settleNow` calls for the same txid are coalesced onto one
|
|
203
|
+
* in-flight run and get the same answer.
|
|
204
|
+
*/
|
|
205
|
+
settleNow(txid: string): Promise<TokenSettleState>
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* The token Resend: re-deliver a sent transfer's notification over the
|
|
209
|
+
* message box, whichever rail it first went out on. The body is rebuilt from
|
|
210
|
+
* what this device holds (blinding journal or the payee output's marker, the
|
|
211
|
+
* bytes `refetch` finds, any cached σ_I) and is the same body `transferTokens`
|
|
212
|
+
* sends, so the recipient's inbox needs nothing new. Never contacts the
|
|
213
|
+
* overlay and never moves the transaction's own status — a delivery, not a
|
|
214
|
+
* payment. See `core/mandala/resendTransfer.ts`.
|
|
215
|
+
*/
|
|
216
|
+
resendTransfer(
|
|
217
|
+
txid: string,
|
|
218
|
+
deps: {
|
|
219
|
+
/** AtomicBEEF for the txid — `makeResendBeef` in the app. */
|
|
220
|
+
refetch: (txid: string) => Promise<number[] | undefined>
|
|
221
|
+
/** Unwraps metadata the permissions manager encrypted on its way to storage. */
|
|
222
|
+
decryptMetadata?: (value: string) => Promise<string>
|
|
223
|
+
}
|
|
224
|
+
): Promise<TokenResendOutcome>
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Step every drain-owned `sent` row once (`handed_over`/`submitting`/
|
|
228
|
+
* `admitted`), exactly as `settleNow` would. The handle rail writes no queue
|
|
229
|
+
* row, so without this a hand-over whose immediate submit did not settle
|
|
230
|
+
* had no retry at all (2026-09-15). Rides the drain tick. Never throws;
|
|
231
|
+
* returns how many rows changed state.
|
|
232
|
+
*/
|
|
233
|
+
settlePendingSends(): Promise<number>
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Fetch and cache a verified σ_I for every spendable token coin whose txid
|
|
237
|
+
* has none, so an offline hand-over always carries its ancestors' admissions.
|
|
238
|
+
* Rides the drain tick. Never throws; returns how many were fetched.
|
|
239
|
+
*/
|
|
240
|
+
ensureAdmissionsForHoldings(): Promise<number>
|
|
241
|
+
/**
|
|
242
|
+
* Drives the LIB's own durable journals once: `reconcileWallet` (retryable
|
|
243
|
+
* refusals, overlay-accepted-but-unbroadcast txs, pending aborts, the stuck
|
|
244
|
+
* `nosend` sweep) and `reconcileNotifications` (recipient notifications a
|
|
245
|
+
* crashed or offline send never delivered).
|
|
246
|
+
*
|
|
247
|
+
* Separate from `drainNow`'s settlement pass because it recovers the HANDLE
|
|
248
|
+
* rail's half-finished state, which has no `token_settlements` row of its
|
|
249
|
+
* own to drain — and it rides the same tick because both are "what this
|
|
250
|
+
* device owes after a reconnect". Best-effort in every direction: never
|
|
251
|
+
* throws, and an unreachable overlay or MessageBox simply leaves the entries
|
|
252
|
+
* journaled for the next pass.
|
|
253
|
+
*/
|
|
254
|
+
reconcileJournals(): Promise<void>
|
|
255
|
+
/** Plain reason a recipient string cannot receive tokens, or null (D4 / ux §5.4). */
|
|
256
|
+
recipientRefusal(recipient: string): string | null
|
|
257
|
+
/** Subscribe to changes (balances/activity/settlement state). Returns unsubscribe. */
|
|
258
|
+
subscribe(listener: () => void): () => void
|
|
259
|
+
/**
|
|
260
|
+
* GET /admin/admission/:txid, mapped and FIX-H-verified against this
|
|
261
|
+
* session's overlay identity key (an unverifiable 'admitted' answer counts
|
|
262
|
+
* as absent). Used by `cancelParkedPayment`'s overlay-authoritative cancel
|
|
263
|
+
* (FIX J) and by `recoverStaleAdmissions`' own recovery pass.
|
|
264
|
+
*/
|
|
265
|
+
fetchAdmission: FetchAdmissionFn
|
|
266
|
+
/**
|
|
267
|
+
* FIX C recovery: advances any `handed_over`/`held` row older than one
|
|
268
|
+
* drain interval with no local admission by asking the overlay once per
|
|
269
|
+
* pass. Returns the number of rows advanced. Never throws.
|
|
270
|
+
*/
|
|
271
|
+
recoverStaleAdmissions(): Promise<number>
|
|
272
|
+
/**
|
|
273
|
+
* Repair the wallet's own view of a token transaction it wrongly failed.
|
|
274
|
+
*
|
|
275
|
+
* A `token_settlements` row reading `admitted`/`broadcast` beside a wallet
|
|
276
|
+
* transaction reading `failed` (and a request reading `invalid`) is the
|
|
277
|
+
* 2026-09-15 fingerprint: the overlay admitted — and therefore broadcast —
|
|
278
|
+
* the transaction, and an abort a moment later released its input coin as
|
|
279
|
+
* spendable although it was spent on chain. For every such row where the
|
|
280
|
+
* transaction is independently confirmed to exist (the chain, or an admission
|
|
281
|
+
* the overlay still holds), this puts the transaction back to a live status,
|
|
282
|
+
* re-marks its inputs spent, restores its own outputs to spendable and
|
|
283
|
+
* repairs the request so the monitor proves it.
|
|
284
|
+
*
|
|
285
|
+
* Rides the drain tick, returns the number of transactions repaired, and
|
|
286
|
+
* never throws. Never acts on the settlement row alone.
|
|
287
|
+
*/
|
|
288
|
+
repairAdmittedAborted(): Promise<number>
|
|
289
|
+
/**
|
|
290
|
+
* The Check Wallet pass over token state, on demand rather than on the tick.
|
|
291
|
+
*
|
|
292
|
+
* Every non-terminal settlement row the drain owns takes one `settleNow`
|
|
293
|
+
* step; whatever is still not settled is put to the overlay. Every spendable
|
|
294
|
+
* token coin without a verified σ_I covering its vout is put to the overlay
|
|
295
|
+
* too. Only the overlay's OWN final verdict writes anything: a refused or
|
|
296
|
+
* evicted transaction is failed locally (its row closed, its inputs freed,
|
|
297
|
+
* its outputs made unspendable), and a coin the overlay admitted for other
|
|
298
|
+
* outputs is made unspendable. A row or coin the overlay has never heard of
|
|
299
|
+
* is counted as `unattested` and left exactly as it was — the drain still
|
|
300
|
+
* owns it, and removing it on absence would be removing it on a guess.
|
|
301
|
+
* `unreachable` is true when the overlay could not be asked; nothing was
|
|
302
|
+
* decided in that case. Never throws.
|
|
303
|
+
*/
|
|
304
|
+
reviewTokenHoldings(): Promise<TokenHoldingsReview>
|
|
305
|
+
/**
|
|
306
|
+
* Sweeps abandoned nearby-blinding reservations (a `lockToPayee` call whose
|
|
307
|
+
* payment was never built/committed) older than 24h. Returns the number
|
|
308
|
+
* removed. Never throws.
|
|
309
|
+
*/
|
|
310
|
+
pruneBlindingReservations(): Promise<number>
|
|
311
|
+
/** Regulatory/registry facts about `assetId`, from the ~10s cache. */
|
|
312
|
+
assetStatus(assetId: string): Promise<TokenAssetStatus>
|
|
313
|
+
/** Same as `assetStatus`, forcing a fresh read past the cache. */
|
|
314
|
+
refreshAssetStatus(assetId: string): Promise<TokenAssetStatus>
|
|
315
|
+
|
|
316
|
+
// ── Injection points ──
|
|
317
|
+
//
|
|
318
|
+
// Everything above is something a screen asks the runtime to DO. Everything
|
|
319
|
+
// below is something the runtime hands to machinery that already exists —
|
|
320
|
+
// the release drain, the nearby build, the frame verifier, the pending
|
|
321
|
+
// queue — so that each of those keeps its own single call site instead of
|
|
322
|
+
// growing a second, Mandala-shaped one. They are on this interface rather
|
|
323
|
+
// than on a wider "wallet runtime" type because the UI assembles some of
|
|
324
|
+
// them (NearbyFlow's build deps) and the wallet shell the rest, and both
|
|
325
|
+
// read the same `useWallet().mandala`.
|
|
326
|
+
|
|
327
|
+
/** Token deps for `processOfflineActions`, passed on every drain tick (§4.3). */
|
|
328
|
+
tokenDeps: OfflineTokenDeps
|
|
329
|
+
/** What `buildPaymentFrame` needs for a nearby token payment: `{ store, lockToPayee }`. */
|
|
330
|
+
tokenBuildDeps: TokenBuildDeps
|
|
331
|
+
/** The pure COVER verifier `coverFromFrame` injects when a frame is handed over. */
|
|
332
|
+
coverVerifier: CoverVerifier
|
|
333
|
+
/** FIX H: verifies a counterparty-supplied σ_I (a settlement ack) before it gates anything. */
|
|
334
|
+
verifyAdmissionEntry: VerifyAdmissionFn
|
|
335
|
+
/**
|
|
336
|
+
* The same check, curried onto THIS session's configured overlay identity
|
|
337
|
+
* key — the shape the drain and the frame verifier inject (`AdmissionVerifier`).
|
|
338
|
+
* A caller cannot name the key it wants an entry checked against, which is
|
|
339
|
+
* the point: §9.10's expected signer is configuration, never frame data.
|
|
340
|
+
*/
|
|
341
|
+
verifyAdmission: AdmissionVerifier
|
|
342
|
+
/** `processPending`'s token hook: journal a credited frame's settlement row and evidence. */
|
|
343
|
+
onTokenCredited: TokenCreditedHook
|
|
344
|
+
/**
|
|
345
|
+
* RECEIVER, BEFORE `internalizeAction`: writes the `'held'` row and the
|
|
346
|
+
* frame's evidence, so the broadcast guard has something to read the moment
|
|
347
|
+
* the credit forces a broadcast attempt.
|
|
348
|
+
*/
|
|
349
|
+
onTokenHeld: TokenHeldHook
|
|
350
|
+
/**
|
|
351
|
+
* PAYER, at park/hold: writes the `'parked'`/`'handed_over'` row and the
|
|
352
|
+
* evidence from the still-PLAINTEXT frame, which is the only moment this
|
|
353
|
+
* device can — `offline_actions.framePayload` is sealed with an in-memory
|
|
354
|
+
* session key a restart forgets.
|
|
355
|
+
*/
|
|
356
|
+
onTokenHandedOver: TokenHandedOverHook
|
|
357
|
+
}
|