@bsv/expo-wallet-toolbox 0.24.3 → 0.25.1
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 +79 -0
- package/PATENTS +12 -0
- package/core/context/WalletContext.tsx +8 -14
- package/core/i18n/translations.tsx +144 -13
- package/core/index.ts +12 -0
- package/core/net/wocLimiter.ts +133 -0
- package/core/pay/beefRepair.ts +3 -1
- package/core/pay/rails/address.ts +2 -2
- package/core/recovery/shareParsing.ts +30 -6
- package/core/recovery/shares.ts +34 -5
- package/core/services/arcadeBroadcastProvider.ts +6 -0
- package/core/services/exchangeRate.ts +8 -1
- package/core/services/vault/VaultKeyService.ts +88 -33
- package/core/services/vault/chainRecovery.ts +763 -312
- package/core/services/vault/lockKeys.ts +101 -0
- package/core/services/vault/markerLookup.ts +104 -0
- package/core/services/vault/metaAuthority.ts +24 -12
- package/core/services/vault/outputAuthority.ts +46 -1
- package/core/services/vault/pivReset.ts +18 -0
- package/core/services/vault/resetClearance.ts +66 -0
- package/core/services/vault/setupGate.ts +74 -0
- package/core/services/vault/transfers.ts +480 -23
- package/core/services/vault/types.ts +16 -0
- package/core/services/vault/vaultStore.ts +324 -34
- package/core/services/vault/walletKind.ts +36 -0
- package/core/services/vault/wocLookup.ts +310 -0
- package/package.json +3 -2
- package/ui/backupShares.ts +1 -1
- package/ui/components/vault/vaultErrorCopy.ts +9 -2
|
@@ -118,6 +118,22 @@ export type VaultErrorCode =
|
|
|
118
118
|
* rather than looping forever. Distinct from 'no-transaction': nothing
|
|
119
119
|
* about a specific transfer failed, the scan itself could not complete. */
|
|
120
120
|
| 'chain-scan-failed'
|
|
121
|
+
/** A chain lookup could not give a clear answer (failed, rate limited,
|
|
122
|
+
* window full, or probe limit reached). Nothing was changed; retry later.
|
|
123
|
+
* details: `{ reason: string, index?: number }`. */
|
|
124
|
+
| 'lookup-unknown'
|
|
125
|
+
/** A new lock would include a key that has not been checked by a tap on
|
|
126
|
+
* this phone. details: `{ serials: string }` (comma-joined). */
|
|
127
|
+
| 'keys-unproven'
|
|
128
|
+
/** Fewer than two checked keys are available for a new lock.
|
|
129
|
+
* details: `{ proven: number }`. */
|
|
130
|
+
| 'not-enough-proven-keys'
|
|
131
|
+
/** A destructive card reset was asked without a chain check or an explicit
|
|
132
|
+
* loss acknowledgement. No details. */
|
|
133
|
+
| 'chain-check-required'
|
|
134
|
+
/** The wallet was restored from older key slices and has no recovery
|
|
135
|
+
* phrase, so it cannot make a vault. No details. */
|
|
136
|
+
| 'no-mnemonic'
|
|
121
137
|
|
|
122
138
|
export class VaultError extends Error {
|
|
123
139
|
code: VaultErrorCode
|
|
@@ -83,12 +83,19 @@ export interface VaultMetaV6 {
|
|
|
83
83
|
/** Fresh-device recovery remains locked until a restored key proves
|
|
84
84
|
* possession with a live random-signature challenge. */
|
|
85
85
|
recovery?: VaultRecoveryState
|
|
86
|
+
/** A key enrolled on a card but not yet part of the committed list. It is
|
|
87
|
+
* never in `keys`; a re-lock signed by an existing proven key commits it. */
|
|
88
|
+
staged?: VaultKeyRecord
|
|
86
89
|
keys: VaultKeyRecord[]
|
|
87
90
|
}
|
|
88
91
|
|
|
89
92
|
export interface VaultRecoveryState {
|
|
90
93
|
required: true
|
|
94
|
+
/** Kept for older readers; derived from `adopted`. */
|
|
91
95
|
adoptedSerials: string[]
|
|
96
|
+
/** Pairs that passed a live tap. Proof is bound to serial AND pubkey.
|
|
97
|
+
* Absent in metas written before this field existed: those read as unproven. */
|
|
98
|
+
adopted?: { serial: string; pubkey: string }[]
|
|
92
99
|
}
|
|
93
100
|
|
|
94
101
|
export interface VaultPendingRemoval {
|
|
@@ -138,6 +145,13 @@ export type VaultMeta = VaultMetaV6
|
|
|
138
145
|
let activeScope: VaultStoreScope | null = null
|
|
139
146
|
let scopeGeneration = 0
|
|
140
147
|
let mutationTail: Promise<void> = Promise.resolve()
|
|
148
|
+
/** In-memory live-tap proofs, keyed by scope storageKey. Never persisted, so
|
|
149
|
+
* the stored meta and its tag stay valid. Dropped by clearScope. */
|
|
150
|
+
const sessionProofs = new Map<string, Set<string>>()
|
|
151
|
+
|
|
152
|
+
function pairId(p: { serial: string; pubkey: string }): string {
|
|
153
|
+
return `${p.serial}:${p.pubkey}`
|
|
154
|
+
}
|
|
141
155
|
|
|
142
156
|
/** INT-08: subscribers notified whenever configureScope/clearScope actually
|
|
143
157
|
* runs, so a reader started before the wallet-build chain reaches
|
|
@@ -227,7 +241,7 @@ function draftTagStoreKey(token: VaultScopeToken): string {
|
|
|
227
241
|
* refuse to trust, never as though nothing had happened.
|
|
228
242
|
*/
|
|
229
243
|
export type VaultMetaTagger = (
|
|
230
|
-
next: Pick<VaultMeta, 'vaultId' | 'revision' | 'createdAt' | 'keys' | 'pendingRemoval'>
|
|
244
|
+
next: Pick<VaultMeta, 'vaultId' | 'revision' | 'createdAt' | 'keys' | 'pendingRemoval' | 'staged' | 'recovery'>
|
|
231
245
|
) => Promise<string>
|
|
232
246
|
|
|
233
247
|
/** Match the record actually read inside the mutation queue to the snapshot
|
|
@@ -241,6 +255,20 @@ function requireAuthenticatedSnapshot(meta: VaultMeta, expected?: VaultMeta, tag
|
|
|
241
255
|
}
|
|
242
256
|
}
|
|
243
257
|
|
|
258
|
+
/** True only when the tag on file is exactly the tag `tagger` computes over
|
|
259
|
+
* `meta` (tags are deterministic HMACs). Without a tagger, a stored tag or on
|
|
260
|
+
* any failure: false (fail closed). Used so a fresh tag is never written over
|
|
261
|
+
* adoption or staging fields that no authentic tag ever covered. */
|
|
262
|
+
async function storedTagMatches(token: VaultScopeToken, meta: VaultMeta, tagger?: VaultMetaTagger): Promise<boolean> {
|
|
263
|
+
if (!tagger) return false
|
|
264
|
+
try {
|
|
265
|
+
const stored = await readMetaTag(token)
|
|
266
|
+
return !!stored && (await tagger(meta)) === stored
|
|
267
|
+
} catch {
|
|
268
|
+
return false
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
244
272
|
/** XR-001: same contract as VaultMetaTagger, for one `ready` draft record. */
|
|
245
273
|
export type VaultDraftTagger = (record: VaultKeyRecord) => Promise<string>
|
|
246
274
|
|
|
@@ -435,6 +463,7 @@ export function isVaultMeta(value: unknown): value is VaultMeta {
|
|
|
435
463
|
'lastUsedSerial',
|
|
436
464
|
'pendingRemoval',
|
|
437
465
|
'recovery',
|
|
466
|
+
'staged',
|
|
438
467
|
'keys'
|
|
439
468
|
]
|
|
440
469
|
if (Object.keys(m).some(name => !allowed.includes(name))) return false
|
|
@@ -466,17 +495,46 @@ export function isVaultMeta(value: unknown): value is VaultMeta {
|
|
|
466
495
|
if (!isSafeTime(pending.startedAt) || pending.revision !== m.revision) return false
|
|
467
496
|
if (pending.state !== 'prepared' && pending.state !== 'broadcast') return false
|
|
468
497
|
}
|
|
498
|
+
if (m.staged !== undefined) {
|
|
499
|
+
if (!isVaultKeyRecord(m.staged)) return false
|
|
500
|
+
const staged = m.staged as VaultKeyRecord
|
|
501
|
+
const removedKey = (m.pendingRemoval as VaultPendingRemoval | undefined)?.key
|
|
502
|
+
if (keys.some(k => k.serial === staged.serial || k.pubkey === staged.pubkey)) return false
|
|
503
|
+
if (removedKey && (removedKey.serial === staged.serial || removedKey.pubkey === staged.pubkey)) return false
|
|
504
|
+
if (keys.length + 1 > MAX_ACTIVE_KEYS) return false
|
|
505
|
+
}
|
|
469
506
|
if (m.recovery !== undefined) {
|
|
470
507
|
const r = m.recovery
|
|
471
508
|
if (r === null || typeof r !== 'object' || Array.isArray(r)) return false
|
|
472
509
|
const recovery = r as Record<string, unknown>
|
|
473
|
-
if (Object.keys(recovery).some(name => !['required', 'adoptedSerials'].includes(name))) return false
|
|
510
|
+
if (Object.keys(recovery).some(name => !['required', 'adoptedSerials', 'adopted'].includes(name))) return false
|
|
474
511
|
if (recovery.required !== true || !Array.isArray(recovery.adoptedSerials)) return false
|
|
475
512
|
if (!recovery.adoptedSerials.every(isVaultSerial)) return false
|
|
476
513
|
if (new Set(recovery.adoptedSerials as string[]).size !== recovery.adoptedSerials.length) return false
|
|
477
514
|
const known = new Set(keys.map(k => k.serial))
|
|
478
515
|
if (m.pendingRemoval) known.add((m.pendingRemoval as VaultPendingRemoval).key.serial)
|
|
516
|
+
if (m.staged) known.add((m.staged as VaultKeyRecord).serial)
|
|
479
517
|
if (!(recovery.adoptedSerials as string[]).every(serial => known.has(serial))) return false
|
|
518
|
+
if (recovery.adopted !== undefined) {
|
|
519
|
+
if (!Array.isArray(recovery.adopted)) return false
|
|
520
|
+
const knownPairs = new Set(
|
|
521
|
+
[
|
|
522
|
+
...keys,
|
|
523
|
+
...(m.pendingRemoval ? [(m.pendingRemoval as VaultPendingRemoval).key] : []),
|
|
524
|
+
...(m.staged ? [m.staged as VaultKeyRecord] : [])
|
|
525
|
+
].map(pairId)
|
|
526
|
+
)
|
|
527
|
+
const seen = new Set<string>()
|
|
528
|
+
for (const entry of recovery.adopted as unknown[]) {
|
|
529
|
+
if (entry === null || typeof entry !== 'object' || Array.isArray(entry)) return false
|
|
530
|
+
const e = entry as Record<string, unknown>
|
|
531
|
+
if (Object.keys(e).some(name => name !== 'serial' && name !== 'pubkey')) return false
|
|
532
|
+
if (typeof e.serial !== 'string' || typeof e.pubkey !== 'string') return false
|
|
533
|
+
const id = pairId(e as { serial: string; pubkey: string })
|
|
534
|
+
if (!knownPairs.has(id) || seen.has(id)) return false
|
|
535
|
+
seen.add(id)
|
|
536
|
+
}
|
|
537
|
+
}
|
|
480
538
|
}
|
|
481
539
|
return true
|
|
482
540
|
}
|
|
@@ -598,6 +656,23 @@ function mergePendingRemoval(
|
|
|
598
656
|
}
|
|
599
657
|
}
|
|
600
658
|
|
|
659
|
+
/** Every key pair of `meta` a tap can prove: the list, the key being removed
|
|
660
|
+
* and the staged key. */
|
|
661
|
+
function metaPairs(meta: VaultMeta): VaultKeyRecord[] {
|
|
662
|
+
return [...meta.keys, ...(meta.pendingRemoval ? [meta.pendingRemoval.key] : []), ...(meta.staged ? [meta.staged] : [])]
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
/** Pairs of `meta` proven by a durable record or a session proof of this scope.
|
|
666
|
+
* A meta enrolled on this phone (no recovery state) proves all its pairs: each
|
|
667
|
+
* passed the enrollment challenge. A legacy recovery meta (serials only) proves
|
|
668
|
+
* none. */
|
|
669
|
+
function provenPairRecords(meta: VaultMeta, token: VaultScopeToken | null): VaultKeyRecord[] {
|
|
670
|
+
if (!meta.recovery?.required) return metaPairs(meta)
|
|
671
|
+
const durable = new Set((meta.recovery.adopted ?? []).map(pairId))
|
|
672
|
+
const session = token ? sessionProofs.get(token.storageKey) : undefined
|
|
673
|
+
return metaPairs(meta).filter(k => durable.has(pairId(k)) || session?.has(pairId(k)) === true)
|
|
674
|
+
}
|
|
675
|
+
|
|
601
676
|
export const vaultStore = {
|
|
602
677
|
/** Select the only wallet+chain namespace subsequent calls may access. */
|
|
603
678
|
configureScope(scope: VaultStoreScope): void {
|
|
@@ -609,6 +684,7 @@ export const vaultStore = {
|
|
|
609
684
|
/** Drop the in-memory authority during logout, wallet rebuild or teardown. */
|
|
610
685
|
clearScope(): void {
|
|
611
686
|
activeScope = null
|
|
687
|
+
sessionProofs.clear()
|
|
612
688
|
scopeGeneration++
|
|
613
689
|
notifyScopeChange()
|
|
614
690
|
},
|
|
@@ -626,6 +702,30 @@ export const vaultStore = {
|
|
|
626
702
|
return activeScope ? { ...activeScope } : null
|
|
627
703
|
},
|
|
628
704
|
|
|
705
|
+
/**
|
|
706
|
+
* The keys of `meta` that a live tap proved: the one definition every new
|
|
707
|
+
* lock is built from. Synchronous. A chain record alone never proves a key,
|
|
708
|
+
* and proof is bound to serial AND pubkey, so a planted record that reuses a
|
|
709
|
+
* real serial inherits nothing.
|
|
710
|
+
*/
|
|
711
|
+
provenKeyRecords(meta: VaultMeta, scopeToken?: VaultScopeToken): VaultKeyRecord[] {
|
|
712
|
+
const proven = new Set(provenPairRecords(meta, scopeToken ?? captureScope(false)).map(pairId))
|
|
713
|
+
return meta.keys.filter(k => proven.has(pairId(k)))
|
|
714
|
+
},
|
|
715
|
+
|
|
716
|
+
/** Remember a live-tap proof in memory only (nothing is written, so the
|
|
717
|
+
* stored meta and its tag stay valid). Dropped by clearScope. */
|
|
718
|
+
noteSessionProof(record: Pick<VaultKeyRecord, 'serial' | 'pubkey'>, scopeToken?: VaultScopeToken): void {
|
|
719
|
+
const token = scopeToken ?? captureScope(true)
|
|
720
|
+
assertScope(token)
|
|
721
|
+
let set = sessionProofs.get(token.storageKey)
|
|
722
|
+
if (!set) {
|
|
723
|
+
set = new Set()
|
|
724
|
+
sessionProofs.set(token.storageKey, set)
|
|
725
|
+
}
|
|
726
|
+
set.add(pairId(record))
|
|
727
|
+
},
|
|
728
|
+
|
|
629
729
|
/** Capture the current wallet+chain epoch before starting async work. */
|
|
630
730
|
captureScopeToken(): VaultScopeToken {
|
|
631
731
|
return captureScope(true)
|
|
@@ -970,9 +1070,12 @@ export const vaultStore = {
|
|
|
970
1070
|
* ONLY after the equality check just above already proved them identical
|
|
971
1071
|
* to `checked` — itself independently authenticated against a real,
|
|
972
1072
|
* chain-verified R1C lock by the caller; in the higher-revision branch
|
|
973
|
-
* they are taken from `checked` directly, never from `current`.
|
|
974
|
-
*
|
|
975
|
-
*
|
|
1073
|
+
* they are taken from `checked` directly, never from `current`.
|
|
1074
|
+
*
|
|
1075
|
+
* The tag-covered recovery.adopted and staged fields are NOT proven by the
|
|
1076
|
+
* chain, so they are kept from `current` only when `current`'s own tag
|
|
1077
|
+
* verifies (storedTagMatches); otherwise adopted becomes [] and staged is
|
|
1078
|
+
* dropped. A forged adoption therefore never launders into the fresh tag. */
|
|
976
1079
|
async restoreVerifiedMeta(
|
|
977
1080
|
meta: VaultMeta,
|
|
978
1081
|
scopeToken?: VaultScopeToken,
|
|
@@ -1007,24 +1110,51 @@ export const vaultStore = {
|
|
|
1007
1110
|
if (current && checked.revision === current.revision) {
|
|
1008
1111
|
const pendingRemoval = mergePendingRemoval(current.pendingRemoval, checked.pendingRemoval)
|
|
1009
1112
|
const restored: VaultMeta = { ...current }
|
|
1113
|
+
// adopted and staged decide which keys a new lock commits. Keep them
|
|
1114
|
+
// only when current's own tag verifies; otherwise a store-only edit
|
|
1115
|
+
// would be blessed by the fresh tag below. With no tagger no tag is
|
|
1116
|
+
// written, so nothing is blessed and current stays as it is.
|
|
1117
|
+
if (tagger && !(await storedTagMatches(token, current, tagger))) {
|
|
1118
|
+
restored.recovery = { required: true, adoptedSerials: [], adopted: [] }
|
|
1119
|
+
delete restored.staged
|
|
1120
|
+
}
|
|
1010
1121
|
if (pendingRemoval) restored.pendingRemoval = pendingRemoval
|
|
1011
1122
|
else delete restored.pendingRemoval
|
|
1012
1123
|
await writeMetaAndTag(token, restored, tagger)
|
|
1013
1124
|
return restored
|
|
1014
1125
|
}
|
|
1015
1126
|
|
|
1016
|
-
const knownSerials = new Set([
|
|
1017
|
-
...checked.keys.map(key => key.serial),
|
|
1018
|
-
...(checked.pendingRemoval ? [checked.pendingRemoval.key.serial] : [])
|
|
1019
|
-
])
|
|
1020
1127
|
const activeSerials = new Set(checked.keys.map(key => key.serial))
|
|
1128
|
+
// Adoption carries over by serial AND pubkey. A device with no recovery
|
|
1129
|
+
// state proved all its keys at enrollment; a legacy recovery meta proved
|
|
1130
|
+
// none. The staged key passed its own live challenge as a ready draft.
|
|
1131
|
+
const checkedPairs = new Set(
|
|
1132
|
+
[...checked.keys, ...(checked.pendingRemoval ? [checked.pendingRemoval.key] : [])].map(pairId)
|
|
1133
|
+
)
|
|
1134
|
+
// current's adoption state counts only when its own tag verifies: a
|
|
1135
|
+
// store-only attacker may have edited adopted or stripped recovery.
|
|
1136
|
+
const currentTrusted = current ? await storedTagMatches(token, current, tagger) : false
|
|
1137
|
+
const currentProven = current && currentTrusted
|
|
1138
|
+
? current.recovery?.required
|
|
1139
|
+
? metaPairs(current).filter(k =>
|
|
1140
|
+
(current.recovery!.adopted ?? []).some(p => p.serial === k.serial && p.pubkey === k.pubkey)
|
|
1141
|
+
)
|
|
1142
|
+
: metaPairs(current)
|
|
1143
|
+
: []
|
|
1144
|
+
const stagedPair = current?.staged && currentTrusted ? [current.staged] : []
|
|
1145
|
+
const carried: { serial: string; pubkey: string }[] = []
|
|
1146
|
+
for (const k of [...currentProven, ...stagedPair]) {
|
|
1147
|
+
const pair = { serial: k.serial, pubkey: k.pubkey }
|
|
1148
|
+
if (checkedPairs.has(pairId(pair)) && !carried.some(c => pairId(c) === pairId(pair))) carried.push(pair)
|
|
1149
|
+
}
|
|
1021
1150
|
const restored: VaultMeta = {
|
|
1022
1151
|
...checked,
|
|
1023
1152
|
// last-use and adoption status are device-local facts, never output
|
|
1024
1153
|
// instructions supplied by a recovery caller.
|
|
1025
1154
|
recovery: {
|
|
1026
1155
|
required: true,
|
|
1027
|
-
adoptedSerials:
|
|
1156
|
+
adoptedSerials: carried.map(p => p.serial),
|
|
1157
|
+
adopted: carried
|
|
1028
1158
|
}
|
|
1029
1159
|
}
|
|
1030
1160
|
delete restored.lastUsedAt
|
|
@@ -1038,42 +1168,72 @@ export const vaultStore = {
|
|
|
1038
1168
|
})
|
|
1039
1169
|
},
|
|
1040
1170
|
|
|
1041
|
-
|
|
1171
|
+
/**
|
|
1172
|
+
* Record that a live tap proved `record`. The pair must match a key of the
|
|
1173
|
+
* list, the key being removed or the staged key. With a `tagger` the pair is
|
|
1174
|
+
* written (meta and tag together). Without one it is a session proof only:
|
|
1175
|
+
* nothing is written, so the stored tag stays valid. A meta enrolled on this
|
|
1176
|
+
* phone (no recovery state) needs neither: it is a no-op.
|
|
1177
|
+
*
|
|
1178
|
+
* Caller contract (laundering): with a `tagger`, the caller must have
|
|
1179
|
+
* established that the stored meta is authentic (adoptVaultKey verifies its
|
|
1180
|
+
* tag first and passes that snapshot as `expectedMeta`), because the fresh
|
|
1181
|
+
* tag covers the whole stored meta.
|
|
1182
|
+
*/
|
|
1183
|
+
async markKeyAdopted(
|
|
1184
|
+
record: Pick<VaultKeyRecord, 'serial' | 'pubkey'>,
|
|
1185
|
+
scopeToken?: VaultScopeToken,
|
|
1186
|
+
tagger?: VaultMetaTagger,
|
|
1187
|
+
expectedMeta?: VaultMeta
|
|
1188
|
+
): Promise<void> {
|
|
1042
1189
|
const token = scopeToken ?? captureScope(true)
|
|
1043
1190
|
return enqueueMutation(async () => {
|
|
1044
1191
|
const meta = await readMeta(token)
|
|
1045
1192
|
if (!meta) throw new VaultError('not-enrolled', 'Vault is not set up')
|
|
1046
|
-
|
|
1047
|
-
if (!
|
|
1193
|
+
if (tagger || expectedMeta) requireAuthenticatedSnapshot(meta, expectedMeta, tagger)
|
|
1194
|
+
if (!metaPairs(meta).some(k => k.serial === record.serial && k.pubkey === record.pubkey)) {
|
|
1048
1195
|
throw new VaultError('wrong-key', 'Challenged key does not match restored vault metadata')
|
|
1049
1196
|
}
|
|
1050
|
-
if (!meta.recovery
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
}
|
|
1197
|
+
if (!meta.recovery) return
|
|
1198
|
+
if (!tagger) {
|
|
1199
|
+
this.noteSessionProof(record, token)
|
|
1200
|
+
return
|
|
1201
|
+
}
|
|
1202
|
+
const adopted = meta.recovery.adopted ?? []
|
|
1203
|
+
if (adopted.some(p => p.serial === record.serial && p.pubkey === record.pubkey)) return
|
|
1204
|
+
const pair = { serial: record.serial, pubkey: record.pubkey }
|
|
1205
|
+
const adoptedSerials = meta.recovery.adoptedSerials.includes(record.serial)
|
|
1206
|
+
? meta.recovery.adoptedSerials
|
|
1207
|
+
: [...meta.recovery.adoptedSerials, record.serial]
|
|
1208
|
+
await writeMetaAndTag(
|
|
1209
|
+
token,
|
|
1210
|
+
{ ...meta, recovery: { required: true, adoptedSerials, adopted: [...adopted, pair] } },
|
|
1211
|
+
tagger
|
|
1212
|
+
)
|
|
1055
1213
|
})
|
|
1056
1214
|
},
|
|
1057
1215
|
|
|
1216
|
+
/** Withdrawals need the tapped key proven: a durable pair or a session proof
|
|
1217
|
+
* that matches the stored serial AND pubkey (the key being removed counts, so
|
|
1218
|
+
* a half-finished removal never strands its funds). */
|
|
1058
1219
|
async requireKeyAdopted(serial: string, scopeToken?: VaultScopeToken): Promise<void> {
|
|
1059
1220
|
const token = scopeToken ?? captureScope(true)
|
|
1060
1221
|
const meta = await requireMeta(token)
|
|
1061
|
-
if (meta.recovery?.required && !meta.
|
|
1222
|
+
if (meta.recovery?.required && !provenPairRecords(meta, token).some(k => k.serial === serial)) {
|
|
1062
1223
|
throw new VaultError('key-not-adopted', 'Prove possession of this restored YubiKey before withdrawing')
|
|
1063
1224
|
}
|
|
1064
1225
|
},
|
|
1065
1226
|
|
|
1066
1227
|
/**
|
|
1067
|
-
*
|
|
1068
|
-
*
|
|
1069
|
-
*
|
|
1070
|
-
*
|
|
1071
|
-
*
|
|
1072
|
-
*
|
|
1073
|
-
*
|
|
1074
|
-
* requireTaggedMeta call, which does exactly that.
|
|
1228
|
+
* Stage a new key (add-key, late-key). The key is written to `meta.staged`
|
|
1229
|
+
* only: `keys` and `revision` do not change, so no lock can commit it. It
|
|
1230
|
+
* joins `keys` only through commitLockKeySet after a re-lock (or a tap proof
|
|
1231
|
+
* when nothing is funded) signed by an existing key. Laundering caveat: the
|
|
1232
|
+
* new meta is built from `meta`, so a forged `meta` would otherwise launder
|
|
1233
|
+
* into a validly tagged `next`: `expectedMeta` must be the caller's
|
|
1234
|
+
* authenticated snapshot (see VaultKeyService.addVaultKey's requireTaggedMeta).
|
|
1075
1235
|
*/
|
|
1076
|
-
async
|
|
1236
|
+
async stageKey(
|
|
1077
1237
|
k: VaultKeyRecord,
|
|
1078
1238
|
scopeToken?: VaultScopeToken,
|
|
1079
1239
|
tagger?: VaultMetaTagger,
|
|
@@ -1085,19 +1245,49 @@ export const vaultStore = {
|
|
|
1085
1245
|
const meta = await requireMeta(token)
|
|
1086
1246
|
requireAuthenticatedSnapshot(meta, expectedMeta, tagger)
|
|
1087
1247
|
if (meta.pendingRemoval) throw new VaultError('relock-required', 'Finish the pending key removal first')
|
|
1088
|
-
if (meta.
|
|
1089
|
-
|
|
1248
|
+
if (meta.staged) throw new VaultError('relock-required', 'Finish adding the previous key first')
|
|
1249
|
+
if (meta.keys.length + 1 > MAX_ACTIVE_KEYS) {
|
|
1250
|
+
throw new VaultError('too-many-keys', `The vault already has ${meta.keys.length} keys`)
|
|
1090
1251
|
}
|
|
1091
1252
|
if (meta.keys.some(x => x.serial === k.serial || x.pubkey === k.pubkey)) {
|
|
1092
1253
|
throw new VaultError('key-already-enrolled', k.serial, undefined, { serial: k.serial })
|
|
1093
1254
|
}
|
|
1094
|
-
const next: VaultMeta = { ...meta,
|
|
1255
|
+
const next: VaultMeta = { ...meta, staged: { ...k } }
|
|
1095
1256
|
await writeMetaAndTag(token, next, tagger)
|
|
1096
1257
|
return next
|
|
1097
1258
|
})
|
|
1098
1259
|
},
|
|
1099
1260
|
|
|
1100
|
-
/**
|
|
1261
|
+
/** Drop the staged key. It only lowers authority, so no tap is needed. The
|
|
1262
|
+
* adoption pair of the staged key goes with it. Same laundering caveat as
|
|
1263
|
+
* stageKey. */
|
|
1264
|
+
async discardStagedKey(
|
|
1265
|
+
scopeToken?: VaultScopeToken,
|
|
1266
|
+
tagger?: VaultMetaTagger,
|
|
1267
|
+
expectedMeta?: VaultMeta
|
|
1268
|
+
): Promise<VaultMeta> {
|
|
1269
|
+
const token = scopeToken ?? captureScope(true)
|
|
1270
|
+
return enqueueMutation(async () => {
|
|
1271
|
+
const meta = await requireMeta(token)
|
|
1272
|
+
requireAuthenticatedSnapshot(meta, expectedMeta, tagger)
|
|
1273
|
+
const staged = meta.staged
|
|
1274
|
+
if (!staged) throw new VaultError('not-enrolled', 'No key is waiting to be added')
|
|
1275
|
+
const next: VaultMeta = { ...meta }
|
|
1276
|
+
delete next.staged
|
|
1277
|
+
if (next.recovery) {
|
|
1278
|
+
const adopted = (next.recovery.adopted ?? []).filter(p => pairId(p) !== pairId(staged))
|
|
1279
|
+
next.recovery = {
|
|
1280
|
+
required: true,
|
|
1281
|
+
adoptedSerials: next.recovery.adoptedSerials.filter(s => s !== staged.serial),
|
|
1282
|
+
...(next.recovery.adopted === undefined ? {} : { adopted })
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
await writeMetaAndTag(token, next, tagger)
|
|
1286
|
+
return next
|
|
1287
|
+
})
|
|
1288
|
+
},
|
|
1289
|
+
|
|
1290
|
+
/** Same laundering caveat as stageKey: `meta` must already be established as
|
|
1101
1291
|
* trustworthy by the caller (transfers.ts's beginVaultKeyRemoval gates its
|
|
1102
1292
|
* whole flow on requireAuthenticatedMeta before reaching here) before a
|
|
1103
1293
|
* `tagger` is supplied. */
|
|
@@ -1162,6 +1352,89 @@ export const vaultStore = {
|
|
|
1162
1352
|
})
|
|
1163
1353
|
},
|
|
1164
1354
|
|
|
1355
|
+
/**
|
|
1356
|
+
* After a spend created a lock with a different key set than the list (a
|
|
1357
|
+
* re-lock or remainder that dropped unproven keys, or activated the staged
|
|
1358
|
+
* key): write `keys`, `revision`, and the pruned adoption state in ONE tagged
|
|
1359
|
+
* write. The staged key leaves `staged` only when `clearStaged` is set. The
|
|
1360
|
+
* adoption pairs of keys that stay (list, key being removed, staged key that
|
|
1361
|
+
* stays staged) are kept; pairs of dropped keys go. The caller passes the
|
|
1362
|
+
* authenticated snapshot as `expectedMeta` (same contract as stageKey).
|
|
1363
|
+
*/
|
|
1364
|
+
async commitLockKeySet(
|
|
1365
|
+
args: {
|
|
1366
|
+
keys: VaultKeyRecord[]
|
|
1367
|
+
revision: number
|
|
1368
|
+
clearStaged: boolean
|
|
1369
|
+
/** The staged key this commit activates. Counts as adopted only if it is
|
|
1370
|
+
* exactly meta.staged and is in `keys`. */
|
|
1371
|
+
activated?: VaultKeyRecord
|
|
1372
|
+
},
|
|
1373
|
+
scopeToken?: VaultScopeToken,
|
|
1374
|
+
tagger?: VaultMetaTagger,
|
|
1375
|
+
expectedMeta?: VaultMeta
|
|
1376
|
+
): Promise<VaultMeta> {
|
|
1377
|
+
const token = scopeToken ?? captureScope(true)
|
|
1378
|
+
return enqueueMutation(async () => {
|
|
1379
|
+
const meta = await requireMeta(token)
|
|
1380
|
+
requireAuthenticatedSnapshot(meta, expectedMeta, tagger)
|
|
1381
|
+
if (!Number.isSafeInteger(args.revision) || args.revision <= meta.revision) {
|
|
1382
|
+
throw new VaultError('template-invalid', 'The new key set must carry a higher revision')
|
|
1383
|
+
}
|
|
1384
|
+
if (!args.keys.every(isVaultKeyRecord) || args.keys.length < MIN_KEYS || args.keys.length > MAX_KEYS) {
|
|
1385
|
+
throw new VaultError('template-invalid', 'Invalid vault lock key set')
|
|
1386
|
+
}
|
|
1387
|
+
const next: VaultMeta = { ...meta, revision: args.revision, keys: args.keys.map(k => ({ ...k })) }
|
|
1388
|
+
if (args.clearStaged) delete next.staged
|
|
1389
|
+
// The removal's replacement output carries the new revision. keyIndex is
|
|
1390
|
+
// where the removed key sat in the list; keep it within the shorter list.
|
|
1391
|
+
if (next.pendingRemoval) {
|
|
1392
|
+
next.pendingRemoval = {
|
|
1393
|
+
...next.pendingRemoval,
|
|
1394
|
+
revision: args.revision,
|
|
1395
|
+
keyIndex: Math.min(next.pendingRemoval.keyIndex, next.keys.length)
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1398
|
+
if (next.lastUsedSerial !== undefined && !next.keys.some(k => k.serial === next.lastUsedSerial)) {
|
|
1399
|
+
delete next.lastUsedSerial
|
|
1400
|
+
delete next.lastUsedAt
|
|
1401
|
+
}
|
|
1402
|
+
if (next.recovery) {
|
|
1403
|
+
const kept = new Set(metaPairs(next).map(pairId))
|
|
1404
|
+
// The activated key passed its own live challenge and the existing-key
|
|
1405
|
+
// signed re-lock named it. Only the key that was staged and is now in
|
|
1406
|
+
// the committed list qualifies; nothing else gains proof here.
|
|
1407
|
+
const act = args.clearStaged ? args.activated : undefined
|
|
1408
|
+
const activatedPair =
|
|
1409
|
+
act !== undefined &&
|
|
1410
|
+
meta.staged !== undefined &&
|
|
1411
|
+
pairId(act) === pairId(meta.staged) &&
|
|
1412
|
+
next.keys.some(k => pairId(k) === pairId(act))
|
|
1413
|
+
? { serial: act.serial, pubkey: act.pubkey }
|
|
1414
|
+
: undefined
|
|
1415
|
+
const prior = next.recovery.adopted
|
|
1416
|
+
// Legacy shape (serials only, `adopted` undefined) proves nothing and is
|
|
1417
|
+
// left as it was.
|
|
1418
|
+
const base =
|
|
1419
|
+
activatedPair && prior !== undefined && !prior.some(p => pairId(p) === pairId(activatedPair))
|
|
1420
|
+
? [...prior, activatedPair]
|
|
1421
|
+
: prior
|
|
1422
|
+
const adopted = (base ?? []).filter(p => kept.has(pairId(p)))
|
|
1423
|
+
const serials = new Set(metaPairs(next).map(k => k.serial))
|
|
1424
|
+
next.recovery = {
|
|
1425
|
+
required: true,
|
|
1426
|
+
adoptedSerials:
|
|
1427
|
+
base === undefined
|
|
1428
|
+
? next.recovery.adoptedSerials.filter(s => serials.has(s))
|
|
1429
|
+
: [...new Set(adopted.map(p => p.serial))],
|
|
1430
|
+
...(base === undefined ? {} : { adopted })
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
await writeMetaAndTag(token, next, tagger)
|
|
1434
|
+
return next
|
|
1435
|
+
})
|
|
1436
|
+
},
|
|
1437
|
+
|
|
1165
1438
|
/** Caller must establish from authenticated action history and two complete
|
|
1166
1439
|
* output scans that no spendable lock still authorizes the removed key and
|
|
1167
1440
|
* that no relevant action is pending before invoking. */
|
|
@@ -1238,7 +1511,7 @@ export const vaultStore = {
|
|
|
1238
1511
|
})
|
|
1239
1512
|
},
|
|
1240
1513
|
|
|
1241
|
-
/** Same laundering caveat as
|
|
1514
|
+
/** Same laundering caveat as stageKey: the caller (VaultScreen's rename
|
|
1242
1515
|
* action) must establish `meta` is trustworthy via requireAuthenticatedMeta
|
|
1243
1516
|
* before supplying a `tagger`. */
|
|
1244
1517
|
async renameKey(
|
|
@@ -1295,12 +1568,29 @@ export const vaultStore = {
|
|
|
1295
1568
|
})
|
|
1296
1569
|
},
|
|
1297
1570
|
|
|
1571
|
+
/**
|
|
1572
|
+
* Forget the local setup of the active scope: the meta item, its authority
|
|
1573
|
+
* tag and the in-memory session proofs. Unlike `clear` it works with no meta,
|
|
1574
|
+
* with a corrupt one and with a pendingRemoval, and it keeps enrollment
|
|
1575
|
+
* drafts. It does not touch the chain; the caller relinquishes local outputs.
|
|
1576
|
+
*/
|
|
1577
|
+
async forgetSetup(scopeToken?: VaultScopeToken): Promise<void> {
|
|
1578
|
+
const token = scopeToken ?? captureScope(true)
|
|
1579
|
+
return enqueueMutation(async () => {
|
|
1580
|
+
assertScope(token)
|
|
1581
|
+
await SecureStore.deleteItemAsync(token.storageKey, SECURE_OPTIONS)
|
|
1582
|
+
await clearMetaTag(token)
|
|
1583
|
+
sessionProofs.delete(token.storageKey)
|
|
1584
|
+
assertScope(token)
|
|
1585
|
+
})
|
|
1586
|
+
},
|
|
1587
|
+
|
|
1298
1588
|
/** XR-002: the wallet-root authority tag on file for this scope's VaultMeta,
|
|
1299
1589
|
* or null if none exists (never tagged — every enrollment created before
|
|
1300
1590
|
* this fix shipped — or explicitly cleared). Read by
|
|
1301
1591
|
* transfers.ts's requireAuthenticatedMeta and by every meta-mutating
|
|
1302
1592
|
* caller that must verify `current` before trusting it as the basis for a
|
|
1303
|
-
* freshly tagged `next` (see
|
|
1593
|
+
* freshly tagged `next` (see stageKey/beginKeyRemoval/renameKey's laundering
|
|
1304
1594
|
* caveat above). */
|
|
1305
1595
|
async getMetaTag(scopeToken?: VaultScopeToken): Promise<string | null> {
|
|
1306
1596
|
const token = scopeToken ?? captureScope(true)
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which kind of wallet is this, as far as Vault is concerned (audit finding
|
|
3
|
+
* primary-key).
|
|
4
|
+
*
|
|
5
|
+
* A wallet restored from older key slices keeps a `recoveredKey` secret and has
|
|
6
|
+
* no `mnemonic` secret. Vault needs the recovery phrase (R1: phrase plus any
|
|
7
|
+
* one YubiKey), so it must not be set up on such a wallet.
|
|
8
|
+
*
|
|
9
|
+
* Rule: not allowed only when `recoveredKey` is present AND `mnemonic` is
|
|
10
|
+
* absent. Both absent (tests, dev builds) is allowed. A keychain read error
|
|
11
|
+
* fails closed.
|
|
12
|
+
*/
|
|
13
|
+
import { hasSecret } from '../secrets/store'
|
|
14
|
+
import { VaultError } from './types'
|
|
15
|
+
|
|
16
|
+
export type VaultSetupAvailability = { ok: true } | { ok: false; reason: 'no-mnemonic' }
|
|
17
|
+
|
|
18
|
+
export async function canSetUpVault(): Promise<VaultSetupAvailability> {
|
|
19
|
+
let hasRecoveredKey: boolean
|
|
20
|
+
let hasMnemonic: boolean
|
|
21
|
+
try {
|
|
22
|
+
hasRecoveredKey = await hasSecret('recoveredKey', { strict: true })
|
|
23
|
+
hasMnemonic = await hasSecret('mnemonic', { strict: true })
|
|
24
|
+
} catch {
|
|
25
|
+
throw new VaultError('template-invalid', 'Could not read the wallet secret state')
|
|
26
|
+
}
|
|
27
|
+
if (hasRecoveredKey && !hasMnemonic) return { ok: false, reason: 'no-mnemonic' }
|
|
28
|
+
return { ok: true }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function requireMnemonicWallet(): Promise<void> {
|
|
32
|
+
const availability = await canSetUpVault()
|
|
33
|
+
if (!availability.ok) {
|
|
34
|
+
throw new VaultError('no-mnemonic', 'Vault needs a wallet made from a recovery phrase')
|
|
35
|
+
}
|
|
36
|
+
}
|