@bsv/expo-wallet-toolbox 0.21.0 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +114 -0
- package/README.md +78 -13
- package/core/contacts/contactActivity.ts +48 -28
- package/core/contacts/contactsStore.ts +2 -2
- package/core/context/WalletContext.tsx +166 -219
- 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/i18n/translations.tsx +72 -0
- package/core/index.ts +55 -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/vault/guard.ts +5 -4
- package/core/storage/StorageExpoSQLite.ts +93 -229
- package/core/storage/methods/processOfflineActions.ts +86 -136
- package/core/storage/schema/createTables.ts +9 -184
- package/package.json +6 -5
- 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 +94 -160
- package/ui/components/ui/colorPrompt.ts +236 -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/hooks/useMandala.ts +6 -6
- package/ui/hooks/useSpendableBalance.ts +3 -1
- package/ui/importDatabases.ts +1 -1
- package/ui/screens/PayScreen.tsx +14 -20
- package/ui/screens/SettingsScreen.tsx +3 -1
- package/ui/screens/WalletCheckScreen.tsx +7 -2
- 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
package/core/localpay/session.ts
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
import { Random } from '@bsv/sdk'
|
|
2
|
+
import { MAX_UINT64, parseDecimalAmount } from '@bsv/mandala'
|
|
2
3
|
import { CodecError } from './codec'
|
|
3
4
|
|
|
4
|
-
|
|
5
|
+
/**
|
|
6
|
+
* 2: a token request's figure `a` is a canonical decimal string of base units
|
|
7
|
+
* (1..2^64-1, IC §0), and the asset block no longer carries the issuer's
|
|
8
|
+
* overlay url/key — the payer uses its own configured overlay (IC §7.3). A v1
|
|
9
|
+
* session is refused outright; there is no back-compat.
|
|
10
|
+
*/
|
|
11
|
+
export const SESSION_VERSION = 2
|
|
5
12
|
|
|
6
13
|
/**
|
|
7
14
|
* The capability word `c` in the session QR.
|
|
@@ -55,22 +62,21 @@ export type SessionOs = 'ios' | 'android'
|
|
|
55
62
|
export const MAX_TOKEN_DECIMALS = 18
|
|
56
63
|
|
|
57
64
|
/**
|
|
58
|
-
* The asset a token request names
|
|
59
|
-
*
|
|
60
|
-
* label/ticker/decimals are payee-supplied
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
* protocol exists, so the request carries them.
|
|
65
|
+
* The asset a token request names (`<64 lowercase hex>_0`, IC §0). When
|
|
66
|
+
* present on a Session, the binding figure is `units` (BASE UNITS of this
|
|
67
|
+
* asset) and `amount` is absent. label/ticker/decimals are payee-supplied
|
|
68
|
+
* display hints only. The issuer overlay is NOT carried: the payer settles
|
|
69
|
+
* against its own configured overlay (IC §7.3), so a QR cannot steer it.
|
|
64
70
|
*/
|
|
65
71
|
export interface SessionAsset {
|
|
66
72
|
id: string
|
|
67
73
|
label?: string
|
|
68
74
|
ticker?: string
|
|
69
75
|
decimals?: number
|
|
70
|
-
overlayUrl: string
|
|
71
|
-
overlayIdentityKey: string
|
|
72
76
|
}
|
|
73
77
|
|
|
78
|
+
const ASSET_ID_RE = /^[0-9a-f]{64}_0$/
|
|
79
|
+
|
|
74
80
|
export interface Session {
|
|
75
81
|
/** Verified host display metadata; never encoded into the base pairing QR. */
|
|
76
82
|
merchantName?: string
|
|
@@ -96,6 +102,13 @@ export interface Session {
|
|
|
96
102
|
* frame belongs to this request, and they apply either way.
|
|
97
103
|
*/
|
|
98
104
|
amount?: number
|
|
105
|
+
/**
|
|
106
|
+
* TOKEN SESSIONS ONLY: the base units the payee is asking for, exact
|
|
107
|
+
* (1..2^64-1). Same binding/open semantics as `amount`; never present
|
|
108
|
+
* beside it. On the wire it is a canonical decimal string, never a JSON
|
|
109
|
+
* number, so a figure above 2^53 survives the QR exactly.
|
|
110
|
+
*/
|
|
111
|
+
units?: bigint
|
|
99
112
|
asset?: SessionAsset
|
|
100
113
|
derivationPrefix: string
|
|
101
114
|
derivationSuffix: string
|
|
@@ -109,8 +122,10 @@ export interface Session {
|
|
|
109
122
|
|
|
110
123
|
export function mintSession(args: {
|
|
111
124
|
identityKey: string
|
|
112
|
-
/** Omit for an open request — the payer enters the amount. */
|
|
125
|
+
/** Satoshis, BSV requests only. Omit for an open request — the payer enters the amount. */
|
|
113
126
|
amount?: number
|
|
127
|
+
/** Base units, token requests (`asset` present) only. Omit for an open token request. */
|
|
128
|
+
units?: bigint
|
|
114
129
|
asset?: SessionAsset
|
|
115
130
|
derivationPrefix: string
|
|
116
131
|
derivationSuffix: string
|
|
@@ -131,11 +146,26 @@ export function mintSession(args: {
|
|
|
131
146
|
// Validated at the mint too, not only at decode: an invalid figure minted
|
|
132
147
|
// here would render on the payee's own screen and be echoed to the payer as
|
|
133
148
|
// an authentic request before any decoder ever saw it.
|
|
149
|
+
if (args.amount !== undefined && args.units !== undefined) {
|
|
150
|
+
throw new CodecError('a session carries amount (satoshis) or units (token), never both')
|
|
151
|
+
}
|
|
134
152
|
if (args.amount !== undefined && !isRequestableAmount(args.amount)) {
|
|
135
153
|
throw new CodecError('bad amount')
|
|
136
154
|
}
|
|
137
|
-
|
|
138
|
-
|
|
155
|
+
// A satoshi figure on a token request would encode as a JSON number beside
|
|
156
|
+
// `t` and every decoder would refuse it; units without an asset name no
|
|
157
|
+
// asset. Both are refused here, where there is still a payee to tell.
|
|
158
|
+
if (args.asset !== undefined && args.amount !== undefined) {
|
|
159
|
+
throw new CodecError('a token request carries units, not a satoshi amount')
|
|
160
|
+
}
|
|
161
|
+
if (args.units !== undefined && args.asset === undefined) {
|
|
162
|
+
throw new CodecError('units require an asset')
|
|
163
|
+
}
|
|
164
|
+
if (args.units !== undefined && !isRequestableUnits(args.units)) {
|
|
165
|
+
throw new CodecError('bad token amount')
|
|
166
|
+
}
|
|
167
|
+
if (args.asset !== undefined && (typeof args.asset.id !== 'string' || !ASSET_ID_RE.test(args.asset.id))) {
|
|
168
|
+
throw new CodecError('bad assetId')
|
|
139
169
|
}
|
|
140
170
|
// XR-043: same bound `decodeSession` enforces below, checked here too for
|
|
141
171
|
// the same reason `amount` is — an out-of-range figure minted on THIS
|
|
@@ -159,7 +189,8 @@ export function mintSession(args: {
|
|
|
159
189
|
psk: new Uint8Array(Random(32)),
|
|
160
190
|
identityKey: args.identityKey,
|
|
161
191
|
...(args.amount === undefined ? {} : { amount: args.amount }),
|
|
162
|
-
...(args.
|
|
192
|
+
...(args.units === undefined ? {} : { units: args.units }),
|
|
193
|
+
...(args.asset === undefined ? {} : { asset: assetOf(args.asset) }),
|
|
163
194
|
derivationPrefix: args.derivationPrefix,
|
|
164
195
|
derivationSuffix: args.derivationSuffix,
|
|
165
196
|
...(args.os === undefined ? {} : { os: args.os })
|
|
@@ -176,6 +207,24 @@ export function isRequestableAmount(a: unknown): a is number {
|
|
|
176
207
|
return typeof a === 'number' && Number.isSafeInteger(a) && a > 0
|
|
177
208
|
}
|
|
178
209
|
|
|
210
|
+
/**
|
|
211
|
+
* Token base units: a bigint in the per-output domain 1..2^64-1 (0162:143).
|
|
212
|
+
* Never a JS number — a figure above 2^53 is not representable as one.
|
|
213
|
+
*/
|
|
214
|
+
export function isRequestableUnits(u: unknown): u is bigint {
|
|
215
|
+
return typeof u === 'bigint' && u >= 1n && u <= MAX_UINT64
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Only the four known fields, so a caller's extra keys never reach the wire or the Session. */
|
|
219
|
+
function assetOf(a: SessionAsset): SessionAsset {
|
|
220
|
+
return {
|
|
221
|
+
id: a.id,
|
|
222
|
+
...(a.label === undefined ? {} : { label: a.label }),
|
|
223
|
+
...(a.ticker === undefined ? {} : { ticker: a.ticker }),
|
|
224
|
+
...(a.decimals === undefined ? {} : { decimals: a.decimals })
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
179
228
|
// Base64url, no padding — QR alphanumeric-safe and dependency-free.
|
|
180
229
|
function toB64url(b: Uint8Array): string {
|
|
181
230
|
let s = ''
|
|
@@ -194,6 +243,22 @@ export function encodeSession(s: Session): string {
|
|
|
194
243
|
// `a` is omitted entirely for an open request rather than written as null.
|
|
195
244
|
// JSON.stringify already drops an undefined value, so this is explicit only
|
|
196
245
|
// to make the wire shape obvious: absent key === payer chooses.
|
|
246
|
+
//
|
|
247
|
+
// A token request writes `a` as a canonical decimal string (IC §0); a BSV
|
|
248
|
+
// request keeps the JSON number. The figure is chosen by `asset`, never by
|
|
249
|
+
// which field happens to be set, so a malformed Session cannot put a JSON
|
|
250
|
+
// number beside `t` or a string on a BSV request.
|
|
251
|
+
let a: number | string | undefined
|
|
252
|
+
if (s.asset !== undefined) {
|
|
253
|
+
if (s.amount !== undefined) throw new CodecError('a token request carries units, not a satoshi amount')
|
|
254
|
+
if (s.units !== undefined) {
|
|
255
|
+
if (!isRequestableUnits(s.units)) throw new CodecError('bad token amount')
|
|
256
|
+
a = s.units.toString()
|
|
257
|
+
}
|
|
258
|
+
} else {
|
|
259
|
+
if (s.units !== undefined) throw new CodecError('units require an asset')
|
|
260
|
+
a = s.amount
|
|
261
|
+
}
|
|
197
262
|
const body = JSON.stringify({
|
|
198
263
|
v: s.version,
|
|
199
264
|
c: s.caps,
|
|
@@ -201,7 +266,7 @@ export function encodeSession(s: Session): string {
|
|
|
201
266
|
k: toB64url(s.psk),
|
|
202
267
|
i: s.identityKey,
|
|
203
268
|
...(s.os === undefined ? {} : { o: s.os === 'ios' ? 'i' : 'a' }),
|
|
204
|
-
...(
|
|
269
|
+
...(a === undefined ? {} : { a }),
|
|
205
270
|
...(s.asset === undefined
|
|
206
271
|
? {}
|
|
207
272
|
: {
|
|
@@ -209,9 +274,7 @@ export function encodeSession(s: Session): string {
|
|
|
209
274
|
i: s.asset.id,
|
|
210
275
|
...(s.asset.label === undefined ? {} : { n: s.asset.label }),
|
|
211
276
|
...(s.asset.ticker === undefined ? {} : { s: s.asset.ticker }),
|
|
212
|
-
...(s.asset.decimals === undefined ? {} : { d: s.asset.decimals })
|
|
213
|
-
u: s.asset.overlayUrl,
|
|
214
|
-
k: s.asset.overlayIdentityKey
|
|
277
|
+
...(s.asset.decimals === undefined ? {} : { d: s.asset.decimals })
|
|
215
278
|
}
|
|
216
279
|
}),
|
|
217
280
|
p: s.derivationPrefix,
|
|
@@ -241,8 +304,22 @@ export function decodeSession(text: string): Session {
|
|
|
241
304
|
// other value is a figure the payee is asserting, and a bad one must be
|
|
242
305
|
// refused rather than quietly reinterpreted as "any amount" — that would turn
|
|
243
306
|
// a corrupt 0 into an open request with a live Send button under it.
|
|
307
|
+
//
|
|
308
|
+
// Which figure `a` is depends on `t`: a token request's `a` is base units as
|
|
309
|
+
// a canonical decimal string (a JSON number is refused — above 2^53 it has
|
|
310
|
+
// already lost precision in JSON.parse); a BSV request's `a` is satoshis as
|
|
311
|
+
// a JSON number.
|
|
244
312
|
const open = a === undefined
|
|
245
|
-
|
|
313
|
+
let units: bigint | undefined
|
|
314
|
+
if (t !== undefined) {
|
|
315
|
+
if (!open) {
|
|
316
|
+
const parsedUnits = typeof a === 'string' ? parseDecimalAmount(a) : null
|
|
317
|
+
if (parsedUnits === null || !isRequestableUnits(parsedUnits)) throw new CodecError('bad token amount')
|
|
318
|
+
units = parsedUnits
|
|
319
|
+
}
|
|
320
|
+
} else if (!open && !isRequestableAmount(a)) {
|
|
321
|
+
throw new CodecError('bad amount')
|
|
322
|
+
}
|
|
246
323
|
if (typeof s !== 'string') throw new CodecError('bad sessionId encoding')
|
|
247
324
|
if (typeof k !== 'string') throw new CodecError('bad psk encoding')
|
|
248
325
|
if (typeof p !== 'string') throw new CodecError('bad derivationPrefix encoding')
|
|
@@ -257,18 +334,15 @@ export function decodeSession(text: string): Session {
|
|
|
257
334
|
let asset: SessionAsset | undefined
|
|
258
335
|
if (t !== undefined) {
|
|
259
336
|
if (typeof t !== 'object' || t === null) throw new CodecError('bad asset block')
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
if (typeof
|
|
263
|
-
if (typeof ok !== 'string' || ok.length !== 66) throw new CodecError('bad overlayIdentityKey')
|
|
337
|
+
// Unknown keys (a v1 builder's `u`/`k`, a newer peer's hints) are ignored.
|
|
338
|
+
const { i: ai, n, s: tick, d } = t as Record<string, unknown>
|
|
339
|
+
if (typeof ai !== 'string' || !ASSET_ID_RE.test(ai)) throw new CodecError('bad assetId')
|
|
264
340
|
if (n !== undefined && typeof n !== 'string') throw new CodecError('bad asset label')
|
|
265
341
|
if (tick !== undefined && typeof tick !== 'string') throw new CodecError('bad asset ticker')
|
|
266
342
|
if (d !== undefined && (typeof d !== 'number' || !Number.isSafeInteger(d) || d < 0 || d > MAX_TOKEN_DECIMALS))
|
|
267
343
|
throw new CodecError('bad asset decimals')
|
|
268
344
|
asset = {
|
|
269
345
|
id: ai,
|
|
270
|
-
overlayUrl: u,
|
|
271
|
-
overlayIdentityKey: ok,
|
|
272
346
|
...(n === undefined ? {} : { label: n }),
|
|
273
347
|
...(tick === undefined ? {} : { ticker: tick }),
|
|
274
348
|
...(d === undefined ? {} : { decimals: d })
|
|
@@ -280,7 +354,7 @@ export function decodeSession(text: string): Session {
|
|
|
280
354
|
sessionId,
|
|
281
355
|
psk,
|
|
282
356
|
identityKey: i,
|
|
283
|
-
...(open ? {} : { amount: a }),
|
|
357
|
+
...(open ? {} : asset === undefined ? { amount: a as number } : { units }),
|
|
284
358
|
...(asset === undefined ? {} : { asset }),
|
|
285
359
|
derivationPrefix: p,
|
|
286
360
|
derivationSuffix: x,
|
package/core/localpay/types.ts
CHANGED
|
@@ -29,11 +29,12 @@ export type DeclineReason =
|
|
|
29
29
|
/** The frame did not decode — version skew, truncation, trailing bytes. */
|
|
30
30
|
| 'decode_failed'
|
|
31
31
|
/**
|
|
32
|
-
* Token frames only: the frame's
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
32
|
+
* Token frames only: the frame's σ / envelope evidence is malformed, or
|
|
33
|
+
* does not reach an admission by this device's own overlay key under
|
|
34
|
+
* offline COVER (design §9 B7), so the coin being offered is not one the
|
|
35
|
+
* issuer's overlay has vouched for. The lib verifies again (online submit
|
|
36
|
+
* or COVER) before the receipt is internalized; this is the payee's
|
|
37
|
+
* pre-check (core/localpay/verify.ts).
|
|
37
38
|
*
|
|
38
39
|
* Distinct from `session_mismatch` because it is not retryable and not the
|
|
39
40
|
* payer's mistake about WHICH request they are paying: the payer must get
|
package/core/localpay/verify.ts
CHANGED
|
@@ -1,44 +1,34 @@
|
|
|
1
1
|
import { Hash, P2PKH, PublicKey, Transaction, Utils } from '@bsv/sdk'
|
|
2
|
-
import type { ChainTracker } from '@bsv/sdk'
|
|
3
|
-
import {
|
|
2
|
+
import type { ChainTracker, WalletProtocol } from '@bsv/sdk'
|
|
3
|
+
import { OneColor } from '@bsv/templates'
|
|
4
|
+
import { MAX_UINT64, colorProtocolID, cover, decodeToken, parseHandoverBody, verifyAttestation } from '@bsv/mandala'
|
|
5
|
+
import { bodyFromFrame } from '../mandala/receipt'
|
|
4
6
|
import type { PaymentFrame } from './codec'
|
|
5
|
-
import { coverFromFrame, type CoverVerifier } from '../mandala/bundle'
|
|
6
7
|
import { PEERPAY_PROTOCOL_ID } from './pending'
|
|
7
8
|
import { isRequestableAmount } from './session'
|
|
8
9
|
import type { DeclineReason } from './types'
|
|
9
|
-
import { trimTrailingSlashes } from '../net/trimTrailingSlashes'
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
export const FT_PROTOCOL_ID: [2, string] = [2, 'p mandala token']
|
|
11
|
+
/** keyID of every `p color` derivation (0207:540-546), as lib `sendHandover` derives it. */
|
|
12
|
+
const COLOR_KEY_ID = 'color'
|
|
14
13
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
amount: number
|
|
21
|
-
/**
|
|
22
|
-
* COVER's own answer: the chain transactions that still have to reach the
|
|
23
|
-
* overlay, parents first, tip last. The payee is the party nominally
|
|
24
|
-
* responsible for submitting them (settlement rule 3), so it is handed
|
|
25
|
-
* the list it proved rather than recomputing it later from storage.
|
|
26
|
-
*/
|
|
27
|
-
mustSubmit: string[]
|
|
28
|
-
}
|
|
14
|
+
/**
|
|
15
|
+
* What a verified frame pays this device: satoshis on the BSV path, exact
|
|
16
|
+
* token base units (1..2^64-1, read from the script) on the token path.
|
|
17
|
+
*/
|
|
18
|
+
export type VerifiedPayment = { kind: 'bsv'; satoshis: number } | { kind: 'token'; assetId: string; units: bigint }
|
|
29
19
|
|
|
30
20
|
/**
|
|
31
21
|
* Why a frame could not be shown to pay this device.
|
|
32
22
|
*
|
|
33
23
|
* Four kinds, because the payee's decline reason differs: bytes that are not a
|
|
34
24
|
* transaction are a decode problem the payer can retry from; a transaction that
|
|
35
|
-
* pays someone else
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* confirm, which is what an offline payee
|
|
40
|
-
* That last one is fixed by the PAYEE
|
|
41
|
-
* needs to hear which of the four it was.
|
|
25
|
+
* pays someone else (or another asset than the session's) is a frame that was
|
|
26
|
+
* never for us; a token frame whose `profileData` is not the attested evidence
|
|
27
|
+
* this wallet's module requires is a frame the issuer's evidence does not
|
|
28
|
+
* cover — retrying it will not help; and a transaction whose ancestry ends at
|
|
29
|
+
* a block root this device could not confirm, which is what an offline payee
|
|
30
|
+
* with no header for that block sees. That last one is fixed by the PAYEE
|
|
31
|
+
* getting online briefly, so the payer needs to hear which of the four it was.
|
|
42
32
|
*/
|
|
43
33
|
export type FrameVerifyKind = 'unparseable' | 'not_mine' | 'not_covered' | 'root_unverified'
|
|
44
34
|
|
|
@@ -93,14 +83,16 @@ export interface FrameVerifyDeps {
|
|
|
93
83
|
}
|
|
94
84
|
|
|
95
85
|
/**
|
|
96
|
-
*
|
|
86
|
+
* What a delivered frame actually pays this device: satoshis, or exact token
|
|
87
|
+
* units.
|
|
97
88
|
*
|
|
98
89
|
* `internalizeAction` credits the output, not any field beside it, so the
|
|
99
90
|
* figure a payee renders as a receipt has to come from the transaction — and
|
|
100
|
-
* is only worth reading once the output is shown to be ours.
|
|
101
|
-
* below is the payee-side mirror of the payer's in `build.ts
|
|
102
|
-
*
|
|
103
|
-
*
|
|
91
|
+
* is only worth reading once the output is shown to be ours. Each derivation
|
|
92
|
+
* below is the payee-side mirror of the payer's (PeerPay in `build.ts`, the
|
|
93
|
+
* `p color` recipient key in lib `sendHandover`): same protocol, same keyID,
|
|
94
|
+
* same counterparty, `forSelf` flipped. If the script matches, the output is
|
|
95
|
+
* spendable by this device and its value is the payment.
|
|
104
96
|
*
|
|
105
97
|
* Throws on every failure and returns on none, so a caller cannot mistake a
|
|
106
98
|
* refusal for a zero-value payment. MUST be called before the settle path
|
|
@@ -112,33 +104,13 @@ export async function verifyFramePayment(
|
|
|
112
104
|
frame: PaymentFrame,
|
|
113
105
|
originator: string,
|
|
114
106
|
/**
|
|
115
|
-
* Token frames only
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
* indistinguishable from a covered one. The BSV path never reads it.
|
|
107
|
+
* Token frames only: THIS DEVICE's own session asset id, the asset the payee
|
|
108
|
+
* asked to be paid in. The frame's `token.assetId` is data that must equal
|
|
109
|
+
* it, and the script must move it. Its ABSENCE refuses a token frame: no
|
|
110
|
+
* session asset is no binding. `overlayIdentityKey` is THIS DEVICE's
|
|
111
|
+
* configured Mandala overlay key K: the evidence must reach σ by it.
|
|
121
112
|
*/
|
|
122
|
-
opts?: {
|
|
123
|
-
cover?: CoverVerifier
|
|
124
|
-
/**
|
|
125
|
-
* Token frames only. THIS DEVICE's configured facts about the asset it
|
|
126
|
-
* agreed to be paid in — the session's own `asset` block, which the payee
|
|
127
|
-
* minted and the payer echoed back, not anything read off the frame.
|
|
128
|
-
*
|
|
129
|
-
* Wire contract §9.10: the verifier's trust anchor comes from its own
|
|
130
|
-
* configuration, and the bundle's `overlayIdentityKey` is DATA that must
|
|
131
|
-
* EQUAL it. Without this comparison a frame names its own overlay, and
|
|
132
|
-
* `coverFromFrame` — which builds the COVER bundle straight from
|
|
133
|
-
* `frame.token.overlayIdentityKey` — would happily verify every σ_I in the
|
|
134
|
-
* frame against a key the payer chose. A payer with any keypair could then
|
|
135
|
-
* sign its own admissions and be credited for a coin no issuer ever saw.
|
|
136
|
-
*
|
|
137
|
-
* Its ABSENCE refuses a token frame, exactly as a missing `cover` does: no
|
|
138
|
-
* configured asset is no anchor.
|
|
139
|
-
*/
|
|
140
|
-
asset?: { overlayUrl: string; overlayIdentityKey: string }
|
|
141
|
-
}
|
|
113
|
+
opts?: { assetId?: string; overlayIdentityKey?: string }
|
|
142
114
|
): Promise<VerifiedPayment> {
|
|
143
115
|
let tx: Transaction
|
|
144
116
|
try {
|
|
@@ -156,6 +128,11 @@ export async function verifyFramePayment(
|
|
|
156
128
|
}
|
|
157
129
|
|
|
158
130
|
if (frame.kind === 'bsv') {
|
|
131
|
+
// A 1Color output elsewhere in the transaction is not refused here: the
|
|
132
|
+
// broadcast hold applies only to transactions in which THIS wallet's
|
|
133
|
+
// token custody is involved (its own `p color` inputs, or a receipt it
|
|
134
|
+
// recorded in the host ledger or journal), so a payer's decoy 1Color
|
|
135
|
+
// output cannot park this BSV credit (`colorGuard.ts`).
|
|
159
136
|
const { publicKey } = await wallet.getPublicKey(
|
|
160
137
|
{
|
|
161
138
|
protocolID: PEERPAY_PROTOCOL_ID,
|
|
@@ -241,141 +218,109 @@ export async function verifyFramePayment(
|
|
|
241
218
|
|
|
242
219
|
if (!frame.token) throw new FrameVerifyError('unparseable', 'token frame without token block')
|
|
243
220
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
221
|
+
// What this function checks is the cheap, certain part that must decide the
|
|
222
|
+
// payer's decline BEFORE the settle path latches or writes: the output is a
|
|
223
|
+
// 1Color value output of the session's asset whose script amount is the
|
|
224
|
+
// frame's, it pays a key this device derives, the v4 body the frame stands
|
|
225
|
+
// for parses strictly, and its evidence reaches an admission by THIS
|
|
226
|
+
// device's own overlay key (offline COVER, pure). The online submit, the
|
|
227
|
+
// host-ledger record and the receipt internalize are the settle path's
|
|
228
|
+
// (`nearby.accept`, design §9 B7).
|
|
229
|
+
|
|
230
|
+
// (1) The named output is a 1Color VALUE output of the session's asset.
|
|
231
|
+
const decoded = decodeToken(output.lockingScript, tx.id('hex'), frame.outputIndex)
|
|
232
|
+
if (decoded === null || decoded.role !== 'value' || decoded.assetId === undefined) {
|
|
233
|
+
throw new FrameVerifyError('not_mine', 'the named output is not a 1Color value output')
|
|
249
234
|
}
|
|
250
235
|
if (decoded.assetId !== frame.token.assetId) {
|
|
251
236
|
throw new FrameVerifyError('not_mine', 'the output moves a different asset than the frame declares')
|
|
252
237
|
}
|
|
238
|
+
if (opts?.assetId === undefined || decoded.assetId !== opts.assetId) {
|
|
239
|
+
throw new FrameVerifyError('not_mine', 'the output moves a different asset than this request asked for')
|
|
240
|
+
}
|
|
241
|
+
const units = decoded.amount
|
|
242
|
+
if (typeof units !== 'bigint' || units < 1n || units > MAX_UINT64) {
|
|
243
|
+
throw new FrameVerifyError('not_mine', `the named output carries no usable token amount: ${String(units)}`)
|
|
244
|
+
}
|
|
245
|
+
if (frame.token.amount !== units.toString()) {
|
|
246
|
+
throw new FrameVerifyError('not_mine', 'the frame’s amount is not the named output’s')
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// (2) It pays us: the payee-side mirror of lib `sendHandover`'s recipient
|
|
250
|
+
// derivation — `p color <assetTag> <prefix> <suffix>`, keyID 'color',
|
|
251
|
+
// the frame's sender (A, or A′ when blinded) as counterparty, forSelf.
|
|
252
|
+
// Through the PUBLISHED manager: the module passes getPublicKey through
|
|
253
|
+
// after checking the key args (0207:597). Remittance strings the lib
|
|
254
|
+
// refuses to name a protocol with cannot be ours either.
|
|
255
|
+
let protocolID: [2, string]
|
|
256
|
+
try {
|
|
257
|
+
protocolID = colorProtocolID(decoded.assetId, frame.derivationPrefix, frame.derivationSuffix)
|
|
258
|
+
} catch (e) {
|
|
259
|
+
throw new FrameVerifyError('not_mine', `the frame’s remittance names no p color key: ${messageOf(e)}`)
|
|
260
|
+
}
|
|
253
261
|
const { publicKey } = await wallet.getPublicKey(
|
|
254
262
|
{
|
|
255
|
-
protocolID:
|
|
256
|
-
keyID:
|
|
263
|
+
protocolID: protocolID as WalletProtocol,
|
|
264
|
+
keyID: COLOR_KEY_ID,
|
|
257
265
|
counterparty: frame.senderIdentityKey,
|
|
258
266
|
forSelf: true
|
|
259
267
|
},
|
|
260
268
|
originator
|
|
261
269
|
)
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
270
|
+
let expected: string
|
|
271
|
+
try {
|
|
272
|
+
expected = new OneColor()
|
|
273
|
+
.lock(decoded.assetId, units, Hash.hash160(Utils.toArray(publicKey, 'hex')))
|
|
274
|
+
.toHex()
|
|
275
|
+
} catch (e) {
|
|
276
|
+
throw new FrameVerifyError('not_mine', `could not derive this device’s expected token script: ${messageOf(e)}`)
|
|
268
277
|
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
// point sums a token input against a token output, so a payer could spend a
|
|
272
|
-
// real, small admitted coin into an output naming any larger amount it
|
|
273
|
-
// likes and have it accepted. Cheap and certain, like the ownership checks
|
|
274
|
-
// above it, so it runs before the COVER walk below.
|
|
275
|
-
if (!tokenConservationHolds(tx, frame.token.assetId)) {
|
|
276
|
-
throw new FrameVerifyError('not_covered', 'the frame’s admission evidence does not cover it: conservation')
|
|
278
|
+
if (output.lockingScript.toHex() !== expected) {
|
|
279
|
+
throw new FrameVerifyError('not_mine', 'the named token output does not pay this device')
|
|
277
280
|
}
|
|
278
281
|
|
|
279
|
-
//
|
|
280
|
-
//
|
|
281
|
-
//
|
|
282
|
-
//
|
|
283
|
-
//
|
|
284
|
-
//
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
// is refused here, before the settle path latches or writes anything, so the
|
|
289
|
-
// refusal stays an unlatched "queued nothing" by construction.
|
|
290
|
-
if (!opts?.cover) {
|
|
291
|
-
throw new FrameVerifyError('not_covered', 'no coverage verifier was supplied for a token frame')
|
|
282
|
+
// (3) The evidence: the v4 body parses strictly (exact members, σ and
|
|
283
|
+
// envelope row shapes), and either a σ by THIS device's configured K
|
|
284
|
+
// covers the named output of the tip itself, or COVER over the body
|
|
285
|
+
// reaches σ by K with every unanchored hop checked (S1) — the same two
|
|
286
|
+
// offline arms as the lib's `acceptHandover`. No K is no binding: the
|
|
287
|
+
// frame is refused.
|
|
288
|
+
const K = opts?.overlayIdentityKey
|
|
289
|
+
if (K === undefined || !/^0[23][0-9a-f]{64}$/.test(K)) {
|
|
290
|
+
throw new FrameVerifyError('not_covered', 'this device has no Mandala overlay key to verify the evidence against')
|
|
292
291
|
}
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
const
|
|
302
|
-
|
|
303
|
-
|
|
292
|
+
let parsed: ReturnType<typeof parseHandoverBody>
|
|
293
|
+
try {
|
|
294
|
+
parsed = parseHandoverBody(bodyFromFrame(frame))
|
|
295
|
+
} catch (e) {
|
|
296
|
+
throw new FrameVerifyError('not_covered', `the frame’s hand-over evidence is malformed: ${messageOf(e)}`)
|
|
297
|
+
}
|
|
298
|
+
const tipTxid = tx.id('hex')
|
|
299
|
+
const rows = [...(parsed.admission !== undefined ? [parsed.admission] : []), ...parsed.admissions]
|
|
300
|
+
const tipCovered = rows.some(
|
|
301
|
+
a => a.txid === tipTxid && a.outputs.includes(frame.outputIndex) && verifyAttestation(a, decoded.assetId as string, K)
|
|
302
|
+
)
|
|
303
|
+
if (tipCovered) return { kind: 'token', assetId: decoded.assetId, units }
|
|
304
|
+
const admissions = new Map<string, (typeof parsed.admissions)[number]>()
|
|
305
|
+
for (const a of rows) {
|
|
306
|
+
if (!admissions.has(a.txid)) admissions.set(a.txid, a)
|
|
307
|
+
}
|
|
308
|
+
let covered: ReturnType<typeof cover>
|
|
309
|
+
try {
|
|
310
|
+
covered = cover({ assetId: decoded.assetId, tip: tx, admissions, linkage: parsed.linkage }, { expectedSignerKey: K })
|
|
311
|
+
} catch (e) {
|
|
312
|
+
throw new FrameVerifyError('not_covered', `the frame’s evidence could not be checked: ${messageOf(e)}`)
|
|
304
313
|
}
|
|
305
|
-
|
|
306
|
-
const covered = await coverFromFrame(frame, opts.cover)
|
|
307
314
|
if (!covered.ok) {
|
|
308
|
-
throw new FrameVerifyError(
|
|
315
|
+
throw new FrameVerifyError(
|
|
316
|
+
'not_covered',
|
|
317
|
+
`the frame’s evidence does not reach an admission (${covered.reason}${covered.detail ? `: ${covered.detail}` : ''})`
|
|
318
|
+
)
|
|
309
319
|
}
|
|
310
320
|
|
|
311
|
-
return { kind: 'token', assetId: decoded.assetId,
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
/**
|
|
315
|
-
* Both overlay facts, compared the way each is actually written.
|
|
316
|
-
*
|
|
317
|
-
* The identity key is hex and case-insensitive — a frame writing it uppercase
|
|
318
|
-
* is the same overlay and must not be refused. The URL is compared exactly
|
|
319
|
-
* apart from a trailing slash: anything cleverer (host-only, scheme-agnostic)
|
|
320
|
-
* would start accepting `http://` for `https://`, or a path under the
|
|
321
|
-
* configured origin, which is precisely what this check exists to stop.
|
|
322
|
-
*/
|
|
323
|
-
function sameOverlay(
|
|
324
|
-
token: { overlayUrl: string; overlayIdentityKey: string },
|
|
325
|
-
asset: { overlayUrl: string; overlayIdentityKey: string }
|
|
326
|
-
): boolean {
|
|
327
|
-
const url = (u: string) => trimTrailingSlashes(u.trim())
|
|
328
|
-
return (
|
|
329
|
-
token.overlayIdentityKey.toLowerCase() === asset.overlayIdentityKey.toLowerCase() &&
|
|
330
|
-
url(token.overlayUrl) === url(asset.overlayUrl)
|
|
331
|
-
)
|
|
321
|
+
return { kind: 'token', assetId: decoded.assetId, units }
|
|
332
322
|
}
|
|
333
323
|
|
|
334
324
|
function messageOf(e: unknown): string {
|
|
335
325
|
return e instanceof Error ? e.message : String(e)
|
|
336
326
|
}
|
|
337
|
-
|
|
338
|
-
/**
|
|
339
|
-
* XR-099. Sums `tip`'s own decodable token inputs and outputs for `assetId`
|
|
340
|
-
* and reports whether the outputs are actually covered by the inputs.
|
|
341
|
-
*
|
|
342
|
-
* Deliberately scoped to the tip's OWN direct inputs, not the whole ancestor
|
|
343
|
-
* tree COVER walks: a direct input's previous output is always present in the
|
|
344
|
-
* frame's AtomicBEEF regardless (the payer needs its locking script to build
|
|
345
|
-
* and sign against it), so this is real evidence, not an assumption. An input
|
|
346
|
-
* whose `sourceTransaction` is absent, or whose source output is not a
|
|
347
|
-
* MandalaToken of this asset at all (an ordinary BSV fee input, most
|
|
348
|
-
* commonly), contributes nothing to `inputSum` — which only ever makes this
|
|
349
|
-
* check MORE likely to refuse, never less: there is no way to under-count an
|
|
350
|
-
* output or over-count an input by omission, so this cannot be defeated by a
|
|
351
|
-
* payer hiding a real input behind a hole.
|
|
352
|
-
*/
|
|
353
|
-
function tokenConservationHolds(tip: Transaction, assetId: string): boolean {
|
|
354
|
-
let inputSum = 0
|
|
355
|
-
for (const input of tip.inputs) {
|
|
356
|
-
const source = input.sourceTransaction
|
|
357
|
-
const output = source?.outputs[input.sourceOutputIndex]
|
|
358
|
-
if (!output?.lockingScript) continue
|
|
359
|
-
try {
|
|
360
|
-
const decoded = MandalaToken.decode(output.lockingScript)
|
|
361
|
-
if (decoded.assetId === assetId && Number.isSafeInteger(decoded.amount) && decoded.amount > 0) {
|
|
362
|
-
inputSum += decoded.amount
|
|
363
|
-
}
|
|
364
|
-
} catch {
|
|
365
|
-
// Not a token script — an ordinary fee input, per FIX K (see
|
|
366
|
-
// core/mandala/bundle.ts's `tokenParentsOf`).
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
let outputSum = 0
|
|
370
|
-
for (const output of tip.outputs) {
|
|
371
|
-
try {
|
|
372
|
-
const decoded = MandalaToken.decode(output.lockingScript)
|
|
373
|
-
if (decoded.assetId === assetId && Number.isSafeInteger(decoded.amount) && decoded.amount > 0) {
|
|
374
|
-
outputSum += decoded.amount
|
|
375
|
-
}
|
|
376
|
-
} catch {
|
|
377
|
-
// Not a token output of this asset.
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
return outputSum <= inputSum
|
|
381
|
-
}
|