@bsv/expo-wallet-toolbox 0.19.0 → 0.20.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 +121 -30
- package/README.md +79 -79
- package/core/backup/RemoteSyncReader.ts +26 -24
- package/core/backup/appData.ts +9 -8
- package/core/backup/codec.ts +21 -18
- package/core/backup/constants.ts +5 -5
- package/core/backup/contactsBackup.ts +8 -8
- package/core/backup/cursor.ts +12 -7
- package/core/backup/derive.ts +3 -3
- package/core/backup/deviceId.ts +2 -2
- package/core/backup/erase.ts +2 -2
- package/core/backup/preference.ts +2 -2
- package/core/backup/push.ts +10 -12
- package/core/backup/restore.ts +22 -14
- package/core/backup/restoreOnImport.ts +3 -2
- package/core/config.tsx +17 -13
- package/core/constants.ts +34 -11
- package/core/contacts/contactsStore.ts +4 -3
- package/core/context/WalletConnectionContext.tsx +294 -212
- package/core/context/WalletContext.tsx +132 -97
- package/core/context/defaultWalletSettings.ts +4 -2
- package/core/demo/demoLedger.ts +1 -5
- package/core/directpay/DirectPayProvider.tsx +15 -24
- package/core/directpay/receive.ts +2 -1
- package/core/headers/checkpoints.ts +10 -1
- package/core/headers/headerStore.ts +13 -2
- package/core/hooks/useConfirmationSound.ts +3 -3
- package/core/hooks/useHaptics.ts +23 -6
- package/core/i18n/translations.tsx +269 -125
- package/core/index.ts +2 -0
- package/core/localpay/codec.ts +12 -6
- package/core/localpay/deviceCaps.ts +4 -14
- package/core/localpay/pending.ts +2 -1
- package/core/localpay/qr.ts +1 -1
- package/core/localpay/settlementAck.ts +1 -4
- package/core/localpay/transport/ble.ts +2 -2
- package/core/localpay/transport/socket.ts +1 -1
- package/core/mandala/abortGuard.ts +1 -1
- package/core/mandala/drain.ts +3 -0
- package/core/mandala/permissionModule.ts +3 -2
- package/core/mandala/resendTransfer.ts +4 -1
- package/core/mandala/runtime.ts +4 -4
- package/core/monitor/TaskBackupPush.ts +10 -13
- package/core/net/chainMatch.ts +8 -5
- package/core/networks.ts +145 -0
- package/core/parsePeerPayURI.ts +4 -1
- package/core/pay/beefRepair.ts +6 -3
- package/core/pay/creditErrors.ts +37 -1
- package/core/pay/rails/address.ts +10 -6
- package/core/pay/rails/handle.ts +84 -11
- package/core/pay/rails/index.ts +18 -2
- package/core/pay/receiveHistory.ts +8 -8
- package/core/peerpay/handleResendRequests.ts +11 -18
- package/core/peerpay/inboxAttempts.ts +1 -4
- package/core/peerpay/rebuildToken.ts +12 -9
- package/core/profiles/profileStore.ts +2 -1
- package/core/profiles/removalProblems.ts +3 -1
- package/core/services/arcTokenStorage.ts +2 -1
- package/core/services/arcadeBroadcastProvider.ts +7 -17
- package/core/services/secrets/envelope.ts +1 -6
- package/core/services/secrets/hkdf.ts +1 -6
- package/core/services/secrets/index.ts +3 -22
- package/core/services/usdFxRates.ts +1 -4
- package/core/services/vault/VaultKeyService.ts +2 -1
- package/core/services/vault/chainRecovery.ts +48 -21
- package/core/services/vault/metaAuthority.ts +4 -1
- package/core/services/vault/r1comb.ts +113 -51
- package/core/services/vault/session.ts +5 -1
- package/core/services/vault/transfers.ts +699 -576
- package/core/services/vault/vaultStore.ts +12 -10
- package/core/services/walletArgLimits.ts +1 -2
- package/core/services/walletConnectionValidation.ts +44 -28
- package/core/services/walletServiceConfig.ts +9 -40
- package/core/storage/StorageExpoSQLite.ts +7 -21
- package/core/storage/methods/listActionsSql.ts +81 -67
- package/core/storage/methods/listOutputsSql.ts +21 -16
- package/core/storage/methods/reviewStatusSql.ts +1 -4
- package/core/stores/ConnectionStore.ts +1 -1
- package/core/theme/ThemeContext.tsx +12 -16
- package/core/theme/motion.ts +3 -3
- package/core/theme/tokens.ts +6 -6
- package/core/toolboxConfig.ts +23 -0
- package/core/walletDbRegistry.ts +7 -5
- package/core/walletRepair/runWalletCheck.ts +1 -4
- package/package.json +2 -2
- package/ui/components/pay/AddressReceive.tsx +3 -3
- package/ui/components/pay/HandleReceive.tsx +15 -15
- package/ui/components/pay/MessageBoxConfig.tsx +46 -20
- package/ui/components/pay/NearbyFlow.tsx +575 -507
- package/ui/components/pay/OfflineNotice.tsx +1 -3
- package/ui/components/pay/PayCellRow.tsx +1 -4
- package/ui/components/pay/PayForm.tsx +1 -3
- package/ui/components/pay/RequestHub.tsx +14 -6
- package/ui/components/pay/UniversalSend.tsx +79 -27
- package/ui/components/pay/tokenSendCopy.ts +1 -2
- package/ui/components/pay/useRecipientInput.ts +2 -2
- package/ui/components/security/WalletLockNotice.tsx +1 -3
- package/ui/components/ui/AlertCard.tsx +44 -45
- package/ui/components/ui/BlurChrome.tsx +4 -15
- package/ui/components/ui/Celebration.tsx +3 -3
- package/ui/components/ui/ChoiceSheet.ts +1 -3
- package/ui/components/ui/ErrorBoundary.tsx +2 -6
- package/ui/components/ui/GroupedList.tsx +13 -31
- package/ui/components/ui/IconButton.tsx +6 -9
- package/ui/components/ui/ListRow.tsx +19 -25
- package/ui/components/ui/PresenceRow.tsx +4 -4
- package/ui/components/ui/PressableScale.tsx +24 -20
- package/ui/components/ui/Toast.tsx +23 -16
- package/ui/components/vault/KeyChooser.tsx +9 -6
- package/ui/components/vault/VaultBackdrop.tsx +1 -8
- package/ui/components/vault/VaultCeremonySheet.tsx +5 -5
- package/ui/components/wallet/ActivityRow.tsx +3 -2
- package/ui/components/wallet/BackupReminderSheet.tsx +1 -5
- package/ui/components/wallet/Balance.tsx +4 -1
- package/ui/components/wallet/ContactActivityList.tsx +7 -7
- package/ui/components/wallet/IconPickerSheet.tsx +1 -5
- package/ui/components/wallet/ProfileSwitcherPopover.tsx +6 -1
- package/ui/components/wallet/tokenEviction.ts +4 -1
- package/ui/hooks/useMandala.ts +1 -7
- package/ui/hooks/useOfflineNoticeActions.ts +21 -27
- package/ui/hooks/useSpendableBalance.ts +2 -2
- package/ui/hooks/useVaultCoverage.ts +7 -1
- package/ui/index.ts +9 -1
- package/ui/resolveIdentity.ts +20 -8
- package/ui/screens/ConnectionsScreen.tsx +44 -18
- package/ui/screens/LogsScreen.tsx +24 -22
- package/ui/screens/PairScreen.tsx +85 -54
- package/ui/screens/PayScreen.tsx +5 -1
- package/ui/screens/ProfileScreen.tsx +0 -3
- package/ui/screens/TrustScreen.tsx +125 -80
- package/ui/screens/VaultScreen.tsx +19 -7
- package/ui/screens/VaultTransferScreen.tsx +5 -1
- package/ui/screens/WalletCheckScreen.tsx +14 -17
- package/ui/screens/WalletConfigScreen.tsx +414 -376
- package/ui/screens/WalletHomeScreen.tsx +47 -42
|
@@ -102,12 +102,15 @@ export async function deriveVaultMarkerScript(
|
|
|
102
102
|
chain: VaultScopeToken['chain'],
|
|
103
103
|
saltKeyId: string
|
|
104
104
|
): Promise<LockingScript> {
|
|
105
|
-
const { publicKey } = await w.getPublicKey(
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
105
|
+
const { publicKey } = await w.getPublicKey(
|
|
106
|
+
{
|
|
107
|
+
protocolID: [...VAULT_MARKER_PROTOCOL],
|
|
108
|
+
keyID: vaultChainKeyId(chain, saltKeyId),
|
|
109
|
+
counterparty: 'self',
|
|
110
|
+
seekPermission: false
|
|
111
|
+
},
|
|
112
|
+
adminOriginator
|
|
113
|
+
)
|
|
111
114
|
return new P2PKH().lock(PublicKey.fromString(publicKey).toAddress())
|
|
112
115
|
}
|
|
113
116
|
|
|
@@ -160,13 +163,16 @@ export async function encryptVaultDescriptorPlaintext(
|
|
|
160
163
|
saltKeyId: string,
|
|
161
164
|
plaintext: string
|
|
162
165
|
): Promise<number[]> {
|
|
163
|
-
const { ciphertext } = await w.encrypt(
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
166
|
+
const { ciphertext } = await w.encrypt(
|
|
167
|
+
{
|
|
168
|
+
plaintext: Utils.toArray(plaintext, 'utf8') as number[],
|
|
169
|
+
protocolID: [...VAULT_DESCRIPTOR_PROTOCOL],
|
|
170
|
+
keyID: vaultChainKeyId(chain, saltKeyId),
|
|
171
|
+
counterparty: 'self',
|
|
172
|
+
seekPermission: false
|
|
173
|
+
},
|
|
174
|
+
adminOriginator
|
|
175
|
+
)
|
|
170
176
|
if (!Array.isArray(ciphertext) || ciphertext.length === 0 || ciphertext.length > MAX_DESCRIPTOR_CIPHERTEXT_BYTES) {
|
|
171
177
|
throw new VaultError('template-invalid', 'Vault descriptor ciphertext exceeds its byte budget')
|
|
172
178
|
}
|
|
@@ -183,12 +189,15 @@ export async function decryptVaultDescriptorPlaintext(
|
|
|
183
189
|
saltKeyId: string,
|
|
184
190
|
ciphertext: number[]
|
|
185
191
|
): Promise<string> {
|
|
186
|
-
const { plaintext } = await w.decrypt(
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
+
const { plaintext } = await w.decrypt(
|
|
193
|
+
{
|
|
194
|
+
ciphertext,
|
|
195
|
+
protocolID: [...VAULT_DESCRIPTOR_PROTOCOL],
|
|
196
|
+
keyID: vaultChainKeyId(chain, saltKeyId),
|
|
197
|
+
counterparty: 'self'
|
|
198
|
+
},
|
|
199
|
+
adminOriginator
|
|
200
|
+
)
|
|
192
201
|
return Utils.toUTF8(plaintext)
|
|
193
202
|
}
|
|
194
203
|
|
|
@@ -199,9 +208,7 @@ export async function decryptVaultDescriptorPlaintext(
|
|
|
199
208
|
* — and optional, so a caller that has no storage handle keeps the original
|
|
200
209
|
* paged-scan heal. See findSpendingReferences in StorageExpoSQLite.
|
|
201
210
|
*/
|
|
202
|
-
export type SpendingReferenceLookup = (
|
|
203
|
-
outpoints: string[]
|
|
204
|
-
) => Promise<{ reference: string; status: string }[]>
|
|
211
|
+
export type SpendingReferenceLookup = (outpoints: string[]) => Promise<{ reference: string; status: string }[]>
|
|
205
212
|
|
|
206
213
|
/**
|
|
207
214
|
* Injected dependencies for a vault transfer.
|
|
@@ -315,7 +322,10 @@ export interface VaultWallet {
|
|
|
315
322
|
* output-creating operation trusts local meta. */
|
|
316
323
|
verifyHmac(args: unknown, originator: string): Promise<{ valid: boolean }>
|
|
317
324
|
createAction(args: unknown, originator: string): Promise<CreateActionResult>
|
|
318
|
-
signAction(
|
|
325
|
+
signAction(
|
|
326
|
+
args: unknown,
|
|
327
|
+
originator: string
|
|
328
|
+
): Promise<{
|
|
319
329
|
txid?: string
|
|
320
330
|
tx?: number[]
|
|
321
331
|
}>
|
|
@@ -403,7 +413,9 @@ let vaultMutationTail: Promise<void> = Promise.resolve()
|
|
|
403
413
|
async function withVaultMutation<T>(work: () => Promise<T>): Promise<T> {
|
|
404
414
|
let release!: () => void
|
|
405
415
|
const predecessor = vaultMutationTail
|
|
406
|
-
vaultMutationTail = new Promise<void>(resolve => {
|
|
416
|
+
vaultMutationTail = new Promise<void>(resolve => {
|
|
417
|
+
release = resolve
|
|
418
|
+
})
|
|
407
419
|
await predecessor
|
|
408
420
|
try {
|
|
409
421
|
return await work()
|
|
@@ -447,7 +459,7 @@ async function scanVaultOutputPages(
|
|
|
447
459
|
adminOriginator: string,
|
|
448
460
|
include: 'locking scripts' | 'entire transactions',
|
|
449
461
|
onPage: (outputs: ListOutputsResult['outputs'], page: ListOutputsResult) => void | Promise<void>,
|
|
450
|
-
scopeToken?: VaultScopeToken
|
|
462
|
+
scopeToken?: VaultScopeToken
|
|
451
463
|
): Promise<number> {
|
|
452
464
|
const seen = new Set<string>()
|
|
453
465
|
let offset = 0
|
|
@@ -494,7 +506,8 @@ async function scanVaultOutputPages(
|
|
|
494
506
|
}
|
|
495
507
|
offset += page.outputs.length
|
|
496
508
|
if (expectedTotal !== undefined) {
|
|
497
|
-
if (offset > expectedTotal)
|
|
509
|
+
if (offset > expectedTotal)
|
|
510
|
+
throw new VaultError('no-transaction', 'Vault output listing exceeded its reported total')
|
|
498
511
|
if (offset === expectedTotal) break
|
|
499
512
|
// Some storage providers return fewer rows than requested. A short page
|
|
500
513
|
// is not the end while the authoritative total says rows remain.
|
|
@@ -557,12 +570,26 @@ interface VerifiedVaultOutput {
|
|
|
557
570
|
* the serial list), so there is no separate claim to falsify: deriving IS
|
|
558
571
|
* the authentication.
|
|
559
572
|
*/
|
|
560
|
-
export async function resolveVaultSalt(
|
|
573
|
+
export async function resolveVaultSalt(
|
|
574
|
+
w: VaultWallet,
|
|
575
|
+
adminOriginator: string,
|
|
576
|
+
ci: VaultInstructions
|
|
577
|
+
): Promise<string> {
|
|
561
578
|
if (ci.v === 6) return ci.salt
|
|
562
|
-
return await deriveVaultSalt(
|
|
579
|
+
return await deriveVaultSalt(
|
|
580
|
+
w,
|
|
581
|
+
adminOriginator,
|
|
582
|
+
ci.saltKeyId,
|
|
583
|
+
ci.keys.map(key => key.serial)
|
|
584
|
+
)
|
|
563
585
|
}
|
|
564
586
|
|
|
565
|
-
function verifyInstructionsAgainstLock(
|
|
587
|
+
function verifyInstructionsAgainstLock(
|
|
588
|
+
ci: VaultInstructions,
|
|
589
|
+
lockingScript: LockingScript,
|
|
590
|
+
where: string,
|
|
591
|
+
salt: string
|
|
592
|
+
): void {
|
|
566
593
|
let baked: string[]
|
|
567
594
|
try {
|
|
568
595
|
baked = bakedCommitments(lockingScript)
|
|
@@ -604,7 +631,10 @@ async function verifyListedVaultOutput(
|
|
|
604
631
|
throw new VaultError('no-transaction', `Vault output ${output.outpoint} source has an invalid value`)
|
|
605
632
|
}
|
|
606
633
|
if (sourceSatoshis !== output.satoshis) {
|
|
607
|
-
throw new VaultError(
|
|
634
|
+
throw new VaultError(
|
|
635
|
+
'no-transaction',
|
|
636
|
+
`Vault output ${output.outpoint} value disagrees with its source transaction`
|
|
637
|
+
)
|
|
608
638
|
}
|
|
609
639
|
const salt = await resolveVaultSalt(w, adminOriginator, ci)
|
|
610
640
|
verifyInstructionsAgainstLock(ci, source.lockingScript, `Vault output ${output.outpoint}`, salt)
|
|
@@ -615,7 +645,8 @@ function sumVaultSatoshis(outputs: readonly Pick<VerifiedVaultOutput, 'satoshis'
|
|
|
615
645
|
let sum = 0
|
|
616
646
|
for (const output of outputs) {
|
|
617
647
|
sum += output.satoshis
|
|
618
|
-
if (!Number.isSafeInteger(sum))
|
|
648
|
+
if (!Number.isSafeInteger(sum))
|
|
649
|
+
throw new VaultError('no-transaction', `${where} value exceeds the safe integer range`)
|
|
619
650
|
}
|
|
620
651
|
return sum
|
|
621
652
|
}
|
|
@@ -642,23 +673,29 @@ async function reduceVerifiedVaultOutputs<T>(
|
|
|
642
673
|
if (!expectedChain) throw new VaultError('not-enrolled', 'Wallet vault scope is not configured')
|
|
643
674
|
const state = initial()
|
|
644
675
|
const inventory = emptyVaultSaltInventory()
|
|
645
|
-
const outputs = await scanVaultOutputPages(
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
676
|
+
const outputs = await scanVaultOutputPages(
|
|
677
|
+
w,
|
|
678
|
+
adminOriginator,
|
|
679
|
+
'entire transactions',
|
|
680
|
+
async (listed, page) => {
|
|
681
|
+
if (listed.length === 0) return
|
|
682
|
+
if (!page.BEEF?.length) {
|
|
683
|
+
throw new VaultError('no-transaction', 'Vault output listing did not include source transactions')
|
|
684
|
+
}
|
|
685
|
+
let sources: Beef
|
|
686
|
+
try {
|
|
687
|
+
sources = Beef.fromBinary(page.BEEF)
|
|
688
|
+
} catch {
|
|
689
|
+
throw new VaultError('no-transaction', 'Vault output listing returned malformed source transactions')
|
|
690
|
+
}
|
|
691
|
+
for (const listedOutput of listed) {
|
|
692
|
+
const output = await verifyListedVaultOutput(w, adminOriginator, listedOutput, sources)
|
|
693
|
+
rememberVaultSalt(inventory, output.ci, output.lockingScript, output.outpoint, expectedChain, output.salt)
|
|
694
|
+
reduce(state, output, sources)
|
|
695
|
+
}
|
|
696
|
+
},
|
|
697
|
+
scopeToken
|
|
698
|
+
)
|
|
662
699
|
assertVaultScope(scopeToken)
|
|
663
700
|
await verifyVaultSaltDerivations(w, adminOriginator, inventory)
|
|
664
701
|
assertVaultScope(scopeToken)
|
|
@@ -679,13 +716,15 @@ async function reduceVerifiedVaultOutputs<T>(
|
|
|
679
716
|
}
|
|
680
717
|
|
|
681
718
|
const sameInstructionKeys = (a: readonly VaultKeyRecord[], b: Readonly<VaultInstructions['keys']>): boolean =>
|
|
682
|
-
a.length === b.length &&
|
|
683
|
-
|
|
684
|
-
key
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
719
|
+
a.length === b.length &&
|
|
720
|
+
a.every(
|
|
721
|
+
(key, i) =>
|
|
722
|
+
key.serial === b[i].serial &&
|
|
723
|
+
key.slot === b[i].slot &&
|
|
724
|
+
key.pubkey === b[i].pubkey &&
|
|
725
|
+
key.nickname === b[i].nickname &&
|
|
726
|
+
key.enrolledAt === b[i].enrolledAt
|
|
727
|
+
)
|
|
689
728
|
|
|
690
729
|
/** Refuse to combine two enrollments or use a cache older than an output. Old
|
|
691
730
|
* revisions are expected until a re-lock, but the active revision must carry
|
|
@@ -740,9 +779,10 @@ async function scanVaultActions(
|
|
|
740
779
|
): Promise<void> {
|
|
741
780
|
if (!w.listActions) throw new VaultError('no-transaction', 'Wallet cannot inspect pending vault actions')
|
|
742
781
|
const seen = new Set<string>()
|
|
743
|
-
const pageSize =
|
|
744
|
-
|
|
745
|
-
|
|
782
|
+
const pageSize =
|
|
783
|
+
details.includeInputSourceLockingScripts || details.includeOutputLockingScripts
|
|
784
|
+
? VAULT_SCRIPT_ACTION_PAGE
|
|
785
|
+
: VAULT_ACTION_PAGE
|
|
746
786
|
const scan = async (query: Record<string, unknown>): Promise<void> => {
|
|
747
787
|
let expectedTotal: number | undefined
|
|
748
788
|
let offset = 0
|
|
@@ -751,7 +791,8 @@ async function scanVaultActions(
|
|
|
751
791
|
const page = await w.listActions!({ labels: ['vault'], limit: pageSize, offset, ...query }, adminOriginator)
|
|
752
792
|
assertVaultScope(scopeToken)
|
|
753
793
|
const actions = page?.actions
|
|
754
|
-
if (!Array.isArray(actions))
|
|
794
|
+
if (!Array.isArray(actions))
|
|
795
|
+
throw new VaultError('no-transaction', 'Vault action listing returned an invalid page')
|
|
755
796
|
if (actions.length > pageSize) {
|
|
756
797
|
throw new VaultError('no-transaction', 'Vault action listing exceeded its requested page size')
|
|
757
798
|
}
|
|
@@ -781,7 +822,8 @@ async function scanVaultActions(
|
|
|
781
822
|
}
|
|
782
823
|
offset += actions.length
|
|
783
824
|
if (expectedTotal !== undefined) {
|
|
784
|
-
if (offset > expectedTotal)
|
|
825
|
+
if (offset > expectedTotal)
|
|
826
|
+
throw new VaultError('no-transaction', 'Vault action listing exceeded its reported total')
|
|
785
827
|
if (offset === expectedTotal) break
|
|
786
828
|
continue
|
|
787
829
|
}
|
|
@@ -790,7 +832,9 @@ async function scanVaultActions(
|
|
|
790
832
|
}
|
|
791
833
|
await scan(details)
|
|
792
834
|
if (!includeFailed) return
|
|
793
|
-
const ordinaryLabels = Array.isArray(details.labels)
|
|
835
|
+
const ordinaryLabels = Array.isArray(details.labels)
|
|
836
|
+
? details.labels.filter(label => typeof label === 'string')
|
|
837
|
+
: ['vault']
|
|
794
838
|
await scan({ ...details, labels: [toolboxSdk.specOpFailedActions, ...ordinaryLabels] })
|
|
795
839
|
}
|
|
796
840
|
|
|
@@ -842,7 +886,8 @@ async function isValidHeldVaultDeposit(
|
|
|
842
886
|
if (inputs.length === 0 || inputs.length > VAULT_HARD_MAX_INPUTS) return false
|
|
843
887
|
for (const input of inputs) {
|
|
844
888
|
if (!input.sourceOutpoint || !/^[0-9a-fA-F]{64}[.:]\d+$/.test(input.sourceOutpoint)) return false
|
|
845
|
-
if (!Number.isSafeInteger(input.sourceSatoshis) || input.sourceSatoshis! < 0 || !input.sourceLockingScript)
|
|
889
|
+
if (!Number.isSafeInteger(input.sourceSatoshis) || input.sourceSatoshis! < 0 || !input.sourceLockingScript)
|
|
890
|
+
return false
|
|
846
891
|
try {
|
|
847
892
|
const sourceLock = LockingScript.fromHex(input.sourceLockingScript)
|
|
848
893
|
if (isR1CSourceScript(input.sourceLockingScript) || !isStandardP2PKH(sourceLock)) return false
|
|
@@ -855,7 +900,8 @@ async function isValidHeldVaultDeposit(
|
|
|
855
900
|
if (outputs.length < 1 || outputs.length > 4) return false
|
|
856
901
|
const indices = new Set<number>()
|
|
857
902
|
for (const output of outputs) {
|
|
858
|
-
if (!Number.isSafeInteger(output.outputIndex) || output.outputIndex < 0 || indices.has(output.outputIndex))
|
|
903
|
+
if (!Number.isSafeInteger(output.outputIndex) || output.outputIndex < 0 || indices.has(output.outputIndex))
|
|
904
|
+
return false
|
|
859
905
|
indices.add(output.outputIndex)
|
|
860
906
|
}
|
|
861
907
|
const vaultOutputs = outputs.filter(output => output.basket === VAULT_BASKET)
|
|
@@ -870,7 +916,8 @@ async function isValidHeldVaultDeposit(
|
|
|
870
916
|
ci.vaultId !== meta.vaultId ||
|
|
871
917
|
ci.createdAt !== meta.createdAt ||
|
|
872
918
|
ci.revision > meta.revision
|
|
873
|
-
)
|
|
919
|
+
)
|
|
920
|
+
return false
|
|
874
921
|
if (ci.revision === meta.revision && !sameInstructionKeys(meta.keys, ci.keys)) return false
|
|
875
922
|
let lock: LockingScript
|
|
876
923
|
let salt: string
|
|
@@ -906,7 +953,11 @@ async function isValidHeldVaultDeposit(
|
|
|
906
953
|
const parsedDescriptor = parseVaultDescriptorScript(LockingScript.fromHex(descriptorOutput.lockingScript))
|
|
907
954
|
if (!parsedDescriptor) return false
|
|
908
955
|
const descriptorPlaintext = await decryptVaultDescriptorPlaintext(
|
|
909
|
-
w,
|
|
956
|
+
w,
|
|
957
|
+
adminOriginator,
|
|
958
|
+
expectedChain,
|
|
959
|
+
ci.saltKeyId,
|
|
960
|
+
parsedDescriptor.ciphertext
|
|
910
961
|
)
|
|
911
962
|
if (descriptorPlaintext !== output.customInstructions) return false
|
|
912
963
|
} catch {
|
|
@@ -927,11 +978,7 @@ async function isValidHeldVaultDeposit(
|
|
|
927
978
|
const implicit = outputs.filter(candidate => candidate !== output)
|
|
928
979
|
if (implicit.length === 1) {
|
|
929
980
|
const change = implicit[0]
|
|
930
|
-
if (
|
|
931
|
-
!Number.isSafeInteger(change.satoshis) ||
|
|
932
|
-
change.satoshis <= 0 ||
|
|
933
|
-
!change.lockingScript
|
|
934
|
-
) return false
|
|
981
|
+
if (!Number.isSafeInteger(change.satoshis) || change.satoshis <= 0 || !change.lockingScript) return false
|
|
935
982
|
try {
|
|
936
983
|
if (!isStandardP2PKH(LockingScript.fromHex(change.lockingScript))) return false
|
|
937
984
|
} catch {
|
|
@@ -942,14 +989,7 @@ async function isValidHeldVaultDeposit(
|
|
|
942
989
|
}
|
|
943
990
|
}
|
|
944
991
|
if (saltInventory) {
|
|
945
|
-
rememberVaultSalt(
|
|
946
|
-
saltInventory,
|
|
947
|
-
ci,
|
|
948
|
-
lock,
|
|
949
|
-
vaultActionOutputId(action, output.outputIndex),
|
|
950
|
-
expectedChain,
|
|
951
|
-
salt
|
|
952
|
-
)
|
|
992
|
+
rememberVaultSalt(saltInventory, ci, lock, vaultActionOutputId(action, output.outputIndex), expectedChain, salt)
|
|
953
993
|
}
|
|
954
994
|
return true
|
|
955
995
|
}
|
|
@@ -973,33 +1013,39 @@ async function reconcileHeldVaultDeposits(
|
|
|
973
1013
|
const unsigned: string[] = []
|
|
974
1014
|
const saltInventory = emptyVaultSaltInventory()
|
|
975
1015
|
let signed = 0
|
|
976
|
-
await scanVaultActions(
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1016
|
+
await scanVaultActions(
|
|
1017
|
+
w,
|
|
1018
|
+
adminOriginator,
|
|
1019
|
+
{
|
|
1020
|
+
labels: [],
|
|
1021
|
+
includeLabels: true,
|
|
1022
|
+
includeInputs: true,
|
|
1023
|
+
includeInputSourceLockingScripts: true,
|
|
1024
|
+
includeOutputs: true,
|
|
1025
|
+
includeOutputLockingScripts: true
|
|
1026
|
+
},
|
|
1027
|
+
async action => {
|
|
1028
|
+
const labels = new Set(action.labels ?? [])
|
|
1029
|
+
if (!labels.has('vault-deposit') || action.status === 'completed' || action.status === 'failed') return
|
|
1030
|
+
// A deposit that reached the network is a live unconfirmed deposit, not a
|
|
1031
|
+
// held reservation: there is nothing to abort and nothing to reconcile,
|
|
1032
|
+
// and its vault output is authenticated on the ordinary listOutputs path
|
|
1033
|
+
// like any other. `unproven` is what the activity list shows as
|
|
1034
|
+
// "Accepted" and can stand for many minutes, so treating it as held would
|
|
1035
|
+
// block every vault transfer — including the re-lock the refusal asks
|
|
1036
|
+
// for — until the merkle proof arrives.
|
|
1037
|
+
if (BROADCAST_ACTION_STATUSES.has(action.status)) return
|
|
1038
|
+
if (
|
|
1039
|
+
!PENDING_ACTION_STATUSES.has(action.status) ||
|
|
1040
|
+
!(await isValidHeldVaultDeposit(w, adminOriginator, action, meta, expectedChain, saltInventory))
|
|
1041
|
+
) {
|
|
1042
|
+
throw new VaultError('action-pending', 'A Vault deposit has an unknown or potentially broadcast state')
|
|
1043
|
+
}
|
|
1044
|
+
if (action.status === 'unsigned') unsigned.push(action.reference!)
|
|
1045
|
+
else signed++
|
|
1046
|
+
},
|
|
1047
|
+
scopeToken
|
|
1048
|
+
)
|
|
1003
1049
|
assertVaultScope(scopeToken)
|
|
1004
1050
|
await verifyVaultSaltDerivations(w, adminOriginator, saltInventory)
|
|
1005
1051
|
assertVaultScope(scopeToken)
|
|
@@ -1090,22 +1136,28 @@ export async function resolveHeldVaultDeposit(
|
|
|
1090
1136
|
const expectedChain = captured.chain ?? vaultStore.getScope()?.chain
|
|
1091
1137
|
if (!expectedChain) throw new VaultError('not-enrolled', 'Wallet vault scope is not configured')
|
|
1092
1138
|
let held: VaultActionRow | undefined
|
|
1093
|
-
await scanVaultActions(
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1139
|
+
await scanVaultActions(
|
|
1140
|
+
w,
|
|
1141
|
+
adminOriginator,
|
|
1142
|
+
{
|
|
1143
|
+
labels: [],
|
|
1144
|
+
includeLabels: true,
|
|
1145
|
+
includeInputs: true,
|
|
1146
|
+
includeInputSourceLockingScripts: true,
|
|
1147
|
+
includeOutputs: true,
|
|
1148
|
+
includeOutputLockingScripts: true
|
|
1149
|
+
},
|
|
1150
|
+
async action => {
|
|
1151
|
+
if (action.status === 'unsigned') return
|
|
1152
|
+
if (!PENDING_ACTION_STATUSES.has(action.status) || BROADCAST_ACTION_STATUSES.has(action.status)) return
|
|
1153
|
+
if (await isValidHeldVaultDeposit(w, adminOriginator, action, meta, expectedChain)) {
|
|
1154
|
+
held = action
|
|
1155
|
+
} else if (isValidHeldVaultSpend(action)) {
|
|
1156
|
+
held = action
|
|
1157
|
+
}
|
|
1158
|
+
},
|
|
1159
|
+
captured
|
|
1160
|
+
)
|
|
1109
1161
|
assertVaultScope(captured)
|
|
1110
1162
|
if (!held?.txid) return { kind: 'nothing-held' }
|
|
1111
1163
|
const txid = held.txid
|
|
@@ -1163,44 +1215,56 @@ async function inspectHiddenVaultReservations(
|
|
|
1163
1215
|
repairUnsigned: boolean
|
|
1164
1216
|
): Promise<boolean> {
|
|
1165
1217
|
const abortable: string[] = []
|
|
1166
|
-
await scanVaultActions(
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1218
|
+
await scanVaultActions(
|
|
1219
|
+
w,
|
|
1220
|
+
adminOriginator,
|
|
1221
|
+
{
|
|
1222
|
+
labels: [],
|
|
1223
|
+
includeLabels: true,
|
|
1224
|
+
includeInputs: true,
|
|
1225
|
+
includeInputSourceLockingScripts: true
|
|
1226
|
+
},
|
|
1227
|
+
action => {
|
|
1228
|
+
// Completed history cannot reserve or hide an output. Ignore it before
|
|
1229
|
+
// interpreting old labels or source scripts; pre-release Vault actions
|
|
1230
|
+
// may use a different template and are not recovery authority.
|
|
1231
|
+
if (action.status === 'completed') return
|
|
1232
|
+
const inputs = action.inputs ?? []
|
|
1233
|
+
const r1cInputs = inputs.filter(input => isR1CSourceScript(input.sourceLockingScript))
|
|
1234
|
+
const claimsVaultSpend = actionClaimsVaultSpend(action)
|
|
1235
|
+
if (claimsVaultSpend && (inputs.length === 0 || r1cInputs.length !== inputs.length)) {
|
|
1236
|
+
throw new VaultError('template-invalid', 'Vault action history has missing or malformed R1C source scripts')
|
|
1237
|
+
}
|
|
1238
|
+
if (r1cInputs.length === 0) return
|
|
1239
|
+
for (const input of r1cInputs) {
|
|
1240
|
+
if (
|
|
1241
|
+
input.sourceSatoshis !== undefined &&
|
|
1242
|
+
(!Number.isSafeInteger(input.sourceSatoshis) || input.sourceSatoshis < 0)
|
|
1243
|
+
) {
|
|
1244
|
+
throw new VaultError('no-transaction', 'Vault action history has an invalid source value')
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1184
1247
|
if (
|
|
1185
|
-
|
|
1186
|
-
|
|
1248
|
+
repairUnsigned &&
|
|
1249
|
+
action.status === 'unsigned' &&
|
|
1250
|
+
!action.txid &&
|
|
1251
|
+
action.reference &&
|
|
1252
|
+
r1cInputs.length === inputs.length
|
|
1187
1253
|
) {
|
|
1188
|
-
|
|
1254
|
+
abortable.push(action.reference)
|
|
1255
|
+
return
|
|
1189
1256
|
}
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
if (BROADCAST_ACTION_STATUSES.has(action.status)) return
|
|
1202
|
-
throw new VaultError('action-pending', 'A pending or failed action is holding a Vault output')
|
|
1203
|
-
}, scopeToken)
|
|
1257
|
+
// A spend already posted to the network is not holding its source back: it
|
|
1258
|
+
// has SPENT it, listOutputs is right to omit it, and whatever the spend
|
|
1259
|
+
// created is listed in its place. Blocking here would fail every balance
|
|
1260
|
+
// read, coverage check and transfer for as long as the spend sits at
|
|
1261
|
+
// 'unproven' — and, because the transfer screen waits for the balance to
|
|
1262
|
+
// settle, would report a completed withdrawal as a failure.
|
|
1263
|
+
if (BROADCAST_ACTION_STATUSES.has(action.status)) return
|
|
1264
|
+
throw new VaultError('action-pending', 'A pending or failed action is holding a Vault output')
|
|
1265
|
+
},
|
|
1266
|
+
scopeToken
|
|
1267
|
+
)
|
|
1204
1268
|
for (const reference of abortable) {
|
|
1205
1269
|
assertVaultScope(scopeToken)
|
|
1206
1270
|
await w.abortAction({ reference }, adminOriginator)
|
|
@@ -1235,12 +1299,14 @@ function vaultActionOutputId(action: VaultActionRow, outputIndex: number): strin
|
|
|
1235
1299
|
if (!Number.isSafeInteger(outputIndex) || outputIndex < 0) {
|
|
1236
1300
|
throw new VaultError('template-invalid', 'Vault action history contains an invalid output index')
|
|
1237
1301
|
}
|
|
1238
|
-
const actionId =
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1302
|
+
const actionId =
|
|
1303
|
+
typeof action.txid === 'string' && /^[0-9a-fA-F]{64}$/.test(action.txid)
|
|
1304
|
+
? action.txid.toLowerCase()
|
|
1305
|
+
: action.reference
|
|
1306
|
+
? `ref:${action.reference}`
|
|
1307
|
+
: undefined
|
|
1308
|
+
if (!actionId)
|
|
1309
|
+
throw new VaultError('template-invalid', 'Vault action history output has no stable transaction identity')
|
|
1244
1310
|
return `${actionId}.${outputIndex}`
|
|
1245
1311
|
}
|
|
1246
1312
|
|
|
@@ -1281,10 +1347,9 @@ function rememberVaultSalt(
|
|
|
1281
1347
|
throw new VaultError('template-invalid', 'Vault output belongs to a different network')
|
|
1282
1348
|
}
|
|
1283
1349
|
const scriptHash = Utils.toHex(Hash.sha256(lock.toBinary()))
|
|
1284
|
-
const outputFingerprint = Utils.toHex(
|
|
1285
|
-
`${scriptHash}\u0000${encodeVaultInstructionsAny(ci)}`,
|
|
1286
|
-
|
|
1287
|
-
) as number[]))
|
|
1350
|
+
const outputFingerprint = Utils.toHex(
|
|
1351
|
+
Hash.sha256(Utils.toArray(`${scriptHash}\u0000${encodeVaultInstructionsAny(ci)}`, 'utf8') as number[])
|
|
1352
|
+
)
|
|
1288
1353
|
const knownOutput = inventory.outputFingerprints.get(outputId)
|
|
1289
1354
|
if (knownOutput !== undefined && knownOutput !== outputFingerprint) {
|
|
1290
1355
|
throw new VaultError('template-invalid', 'One Vault output has conflicting authenticated representations')
|
|
@@ -1310,16 +1375,20 @@ export async function deriveVaultSalt(
|
|
|
1310
1375
|
serials: readonly string[]
|
|
1311
1376
|
): Promise<string> {
|
|
1312
1377
|
try {
|
|
1313
|
-
const derived = await w.createHmac(
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1378
|
+
const derived = await w.createHmac(
|
|
1379
|
+
{
|
|
1380
|
+
protocolID: [...VAULT_SALT_PROTOCOL],
|
|
1381
|
+
keyID: saltKeyId,
|
|
1382
|
+
counterparty: 'self',
|
|
1383
|
+
data: vaultSaltHmacData(serials),
|
|
1384
|
+
seekPermission: false
|
|
1385
|
+
},
|
|
1386
|
+
adminOriginator
|
|
1387
|
+
)
|
|
1320
1388
|
const hmac = derived.hmac
|
|
1321
1389
|
if (
|
|
1322
|
-
!Array.isArray(hmac) ||
|
|
1390
|
+
!Array.isArray(hmac) ||
|
|
1391
|
+
hmac.length !== SALT_BYTES ||
|
|
1323
1392
|
hmac.some(byte => !Number.isInteger(byte) || byte < 0 || byte > 0xff)
|
|
1324
1393
|
) {
|
|
1325
1394
|
throw new Error('non-canonical HMAC')
|
|
@@ -1341,7 +1410,10 @@ async function verifyVaultSaltDerivations(
|
|
|
1341
1410
|
for (const claim of inventory.derivationClaims.values()) {
|
|
1342
1411
|
const actualSalt = await deriveVaultSalt(w, adminOriginator, claim.saltKeyId, claim.serials)
|
|
1343
1412
|
if (actualSalt !== claim.salt) {
|
|
1344
|
-
throw new VaultError(
|
|
1413
|
+
throw new VaultError(
|
|
1414
|
+
'template-invalid',
|
|
1415
|
+
`Vault salt derivation ${claim.saltKeyId} does not belong to this wallet`
|
|
1416
|
+
)
|
|
1345
1417
|
}
|
|
1346
1418
|
}
|
|
1347
1419
|
}
|
|
@@ -1355,43 +1427,48 @@ async function addHistoricalVaultSaltInventory(
|
|
|
1355
1427
|
): Promise<void> {
|
|
1356
1428
|
const expectedChain = scopeToken?.chain ?? vaultStore.getScope()?.chain
|
|
1357
1429
|
if (!expectedChain) throw new VaultError('not-enrolled', 'Wallet vault scope is not configured')
|
|
1358
|
-
await scanVaultActions(
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1430
|
+
await scanVaultActions(
|
|
1431
|
+
w,
|
|
1432
|
+
adminOriginator,
|
|
1433
|
+
{
|
|
1434
|
+
labels: [],
|
|
1435
|
+
includeOutputs: true,
|
|
1436
|
+
includeOutputLockingScripts: true
|
|
1437
|
+
},
|
|
1438
|
+
async action => {
|
|
1439
|
+
for (const output of action.outputs ?? []) {
|
|
1440
|
+
if (output.basket !== VAULT_BASKET) continue
|
|
1441
|
+
const ci = decodeVaultInstructions(output.customInstructions)
|
|
1442
|
+
if (!ci || !output.lockingScript) {
|
|
1443
|
+
throw new VaultError('template-invalid', 'Vault history contains unreadable recovery metadata or script')
|
|
1444
|
+
}
|
|
1445
|
+
let lock: LockingScript
|
|
1446
|
+
try {
|
|
1447
|
+
lock = LockingScript.fromHex(output.lockingScript)
|
|
1448
|
+
} catch {
|
|
1449
|
+
throw new VaultError('template-invalid', 'Vault history contains a malformed locking script')
|
|
1450
|
+
}
|
|
1451
|
+
const salt = await resolveVaultSalt(w, adminOriginator, ci)
|
|
1452
|
+
verifyInstructionsAgainstLock(ci, lock, 'Vault history output', salt)
|
|
1453
|
+
// Salt/script reuse is wallet-global, so retain valid history from prior
|
|
1454
|
+
// enrollments after a drained Vault is disabled and set up again. Only a
|
|
1455
|
+
// record claiming the current enrollment ID must agree with its immutable
|
|
1456
|
+
// creation time, may not be newer than the local active revision, and
|
|
1457
|
+
// must carry the active ordered key set when revisions are equal.
|
|
1458
|
+
if (
|
|
1459
|
+
ci.vaultId === meta.vaultId &&
|
|
1460
|
+
(ci.createdAt !== meta.createdAt ||
|
|
1461
|
+
ci.revision > meta.revision ||
|
|
1462
|
+
(ci.revision === meta.revision && !sameInstructionKeys(meta.keys, ci.keys)))
|
|
1463
|
+
) {
|
|
1464
|
+
throw new VaultError('template-invalid', 'Vault history conflicts with the active vault enrollment')
|
|
1465
|
+
}
|
|
1466
|
+
rememberVaultSalt(inventory, ci, lock, vaultActionOutputId(action, output.outputIndex), expectedChain, salt)
|
|
1391
1467
|
}
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1468
|
+
},
|
|
1469
|
+
scopeToken,
|
|
1470
|
+
true
|
|
1471
|
+
)
|
|
1395
1472
|
assertVaultScope(scopeToken)
|
|
1396
1473
|
await verifyVaultSaltDerivations(w, adminOriginator, inventory)
|
|
1397
1474
|
assertVaultScope(scopeToken)
|
|
@@ -1506,7 +1583,8 @@ async function abortActions(
|
|
|
1506
1583
|
for (let i = 0; i < actions.length; i++) {
|
|
1507
1584
|
const a = actions[i]
|
|
1508
1585
|
const rowId = a.reference ?? a.txid
|
|
1509
|
-
if (!rowId || seenRows.has(rowId))
|
|
1586
|
+
if (!rowId || seenRows.has(rowId))
|
|
1587
|
+
throw new Error(`action listing repeated or omitted identity at ${offset + i}`)
|
|
1510
1588
|
seenRows.add(rowId)
|
|
1511
1589
|
if (!matches(a)) continue
|
|
1512
1590
|
seen.push(`${a.status}${a.reference ? '' : '/no-ref'}`)
|
|
@@ -1551,7 +1629,13 @@ async function abortReservingTxids(
|
|
|
1551
1629
|
): Promise<number> {
|
|
1552
1630
|
if (txids.length === 0) return 0
|
|
1553
1631
|
const want = new Set(txids)
|
|
1554
|
-
return await abortActions(
|
|
1632
|
+
return await abortActions(
|
|
1633
|
+
w,
|
|
1634
|
+
adminOriginator,
|
|
1635
|
+
a => a.txid != null && want.has(a.txid),
|
|
1636
|
+
{ stopAfter: want.size },
|
|
1637
|
+
scopeToken
|
|
1638
|
+
)
|
|
1555
1639
|
}
|
|
1556
1640
|
|
|
1557
1641
|
/** Abort the orphaned transactions holding these outpoints, matched on each
|
|
@@ -1723,10 +1807,7 @@ function recoveredSupersedesExisting(existing: VaultMeta, recovered: VaultMeta):
|
|
|
1723
1807
|
* always runs; the existing record is kept only when it is not superseded
|
|
1724
1808
|
* (recoveredSupersedesExisting), and restored the same way — through
|
|
1725
1809
|
* vaultStore.restoreVerifiedMeta — when it is. */
|
|
1726
|
-
export async function recoverVaultMetaFromOutputs(
|
|
1727
|
-
w: VaultWallet,
|
|
1728
|
-
adminOriginator: string
|
|
1729
|
-
): Promise<VaultMeta | null> {
|
|
1810
|
+
export async function recoverVaultMetaFromOutputs(w: VaultWallet, adminOriginator: string): Promise<VaultMeta | null> {
|
|
1730
1811
|
const scopeToken = vaultStore.captureScopeToken()
|
|
1731
1812
|
return await withVaultMutation(async () => {
|
|
1732
1813
|
assertVaultScope(scopeToken)
|
|
@@ -1735,10 +1816,11 @@ export async function recoverVaultMetaFromOutputs(
|
|
|
1735
1816
|
w,
|
|
1736
1817
|
adminOriginator,
|
|
1737
1818
|
() => [] as { instructions: VaultInstructions; txid: string }[],
|
|
1738
|
-
(records, output) =>
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1819
|
+
(records, output) =>
|
|
1820
|
+
records.push({
|
|
1821
|
+
instructions: output.ci,
|
|
1822
|
+
txid: output.outpoint.slice(0, 64).toLowerCase()
|
|
1823
|
+
}),
|
|
1742
1824
|
scopeToken
|
|
1743
1825
|
)
|
|
1744
1826
|
if (scan.outputs === 0) return existing
|
|
@@ -1859,88 +1941,88 @@ export async function disableVaultWhenSafe(
|
|
|
1859
1941
|
if (await vaultHasOutputs(w, adminOriginator, scopeToken)) return false
|
|
1860
1942
|
|
|
1861
1943
|
let blocked = false
|
|
1862
|
-
await scanVaultActions(
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
const ci = decodeVaultInstructions(output.customInstructions)
|
|
1898
|
-
if (!ci || !output.lockingScript) {
|
|
1899
|
-
throw new VaultError('template-invalid', 'Vault action history has unreadable output metadata or script')
|
|
1900
|
-
}
|
|
1901
|
-
if (ci.chain !== scopeToken.chain) {
|
|
1902
|
-
throw new VaultError('template-invalid', 'Vault action history output belongs to a different network')
|
|
1944
|
+
await scanVaultActions(
|
|
1945
|
+
w,
|
|
1946
|
+
adminOriginator,
|
|
1947
|
+
{
|
|
1948
|
+
labels: [],
|
|
1949
|
+
includeLabels: true,
|
|
1950
|
+
includeInputs: true,
|
|
1951
|
+
includeInputSourceLockingScripts: true,
|
|
1952
|
+
includeOutputs: true,
|
|
1953
|
+
includeOutputLockingScripts: true
|
|
1954
|
+
},
|
|
1955
|
+
async action => {
|
|
1956
|
+
const labels = new Set(action.labels ?? [])
|
|
1957
|
+
const inputs = action.inputs ?? []
|
|
1958
|
+
const r1cInputs = inputs.filter(input => isR1CSourceScript(input.sourceLockingScript))
|
|
1959
|
+
const claimsSpend = actionClaimsVaultSpend(action)
|
|
1960
|
+
// A posted transaction (BROADCAST_ACTION_STATUSES) reserves nothing this
|
|
1961
|
+
// check cannot already see: it has a txid and its outputs are ordinary
|
|
1962
|
+
// unconfirmed UTXOs, which is why reconcileHeldVaultDeposits skips the
|
|
1963
|
+
// same statuses. Only a signed-but-unposted action can hold a Vault
|
|
1964
|
+
// source back invisibly, and `unproven` can stand for hours — or never
|
|
1965
|
+
// resolve locally — so treating it as live refuses a drained Vault its
|
|
1966
|
+
// disablement for as long as the merkle proof is missing.
|
|
1967
|
+
const reserving = PENDING_ACTION_STATUSES.has(action.status) && !BROADCAST_ACTION_STATUSES.has(action.status)
|
|
1968
|
+
// Only a live action can still be holding a Vault source back, and its
|
|
1969
|
+
// source scripts are evidence for that decision only while it is live.
|
|
1970
|
+
// Settled history cannot satisfy this rule and must not be asked to:
|
|
1971
|
+
// storage RESTORES the sources of a terminally failed action and clears
|
|
1972
|
+
// their `spentBy`, which is exactly what empties `inputs` here, and
|
|
1973
|
+
// completed pre-release spends predate this template (see
|
|
1974
|
+
// inspectHiddenVaultReservations). Demanding exact R1C sources from
|
|
1975
|
+
// either shape reads an ordinary drained history as malformed and
|
|
1976
|
+
// refuses to disable an empty Vault for good.
|
|
1977
|
+
if (reserving && claimsSpend && (inputs.length === 0 || r1cInputs.length !== inputs.length)) {
|
|
1978
|
+
throw new VaultError('template-invalid', 'Vault action history has missing or malformed R1C source scripts')
|
|
1903
1979
|
}
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1980
|
+
const vaultOutputs = (action.outputs ?? []).filter(output => output.basket === VAULT_BASKET)
|
|
1981
|
+
for (const output of vaultOutputs) {
|
|
1982
|
+
const ci = decodeVaultInstructions(output.customInstructions)
|
|
1983
|
+
if (!ci || !output.lockingScript) {
|
|
1984
|
+
throw new VaultError('template-invalid', 'Vault action history has unreadable output metadata or script')
|
|
1985
|
+
}
|
|
1986
|
+
if (ci.chain !== scopeToken.chain) {
|
|
1987
|
+
throw new VaultError('template-invalid', 'Vault action history output belongs to a different network')
|
|
1988
|
+
}
|
|
1989
|
+
let lock: LockingScript
|
|
1990
|
+
try {
|
|
1991
|
+
lock = LockingScript.fromHex(output.lockingScript)
|
|
1992
|
+
} catch {
|
|
1993
|
+
throw new VaultError('template-invalid', 'Vault action history has a malformed output script')
|
|
1994
|
+
}
|
|
1995
|
+
const salt = await resolveVaultSalt(w, adminOriginator, ci)
|
|
1996
|
+
verifyInstructionsAgainstLock(ci, lock, 'Vault action history output', salt)
|
|
1909
1997
|
}
|
|
1910
|
-
const
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
if (r1cInputs.length > 0 || (claimsSpend && inputs.length > 0)) {
|
|
1929
|
-
blocked = true
|
|
1998
|
+
const touchesVault =
|
|
1999
|
+
r1cInputs.length > 0 ||
|
|
2000
|
+
vaultOutputs.length > 0 ||
|
|
2001
|
+
labels.has('vault-withdraw') ||
|
|
2002
|
+
labels.has('vault-relock') ||
|
|
2003
|
+
labels.has('vault-deposit')
|
|
2004
|
+
if (reserving && touchesVault) blocked = true
|
|
2005
|
+
if (action.status === 'failed') {
|
|
2006
|
+
// A failed deposit has no Vault source to hide. A failed spend still
|
|
2007
|
+
// listing inputs has NOT been unwound — storage drops `spentBy` in the
|
|
2008
|
+
// same write that makes the source spendable again — so it may still
|
|
2009
|
+
// be hiding a Vault output from listOutputs, and labels/scripts that
|
|
2010
|
+
// cannot classify it fail closed the same way. A failed spend listing
|
|
2011
|
+
// no inputs at all is holding nothing back, and the authoritative
|
|
2012
|
+
// empty check is then the only word that counts.
|
|
2013
|
+
if (r1cInputs.length > 0 || (claimsSpend && inputs.length > 0)) {
|
|
2014
|
+
blocked = true
|
|
2015
|
+
}
|
|
1930
2016
|
}
|
|
1931
|
-
}
|
|
1932
|
-
|
|
2017
|
+
},
|
|
2018
|
+
scopeToken,
|
|
2019
|
+
true
|
|
2020
|
+
)
|
|
1933
2021
|
if (blocked) return false
|
|
1934
2022
|
// A completed output can be absent from listOutputs but still be the only
|
|
1935
2023
|
// durable recovery record for this enrollment. Authenticate every history
|
|
1936
2024
|
// HMAC before allowing its local descriptor to be cleared.
|
|
1937
|
-
await addHistoricalVaultSaltInventory(
|
|
1938
|
-
w,
|
|
1939
|
-
adminOriginator,
|
|
1940
|
-
emptyVaultSaltInventory(),
|
|
1941
|
-
meta,
|
|
1942
|
-
scopeToken
|
|
1943
|
-
)
|
|
2025
|
+
await addHistoricalVaultSaltInventory(w, adminOriginator, emptyVaultSaltInventory(), meta, scopeToken)
|
|
1944
2026
|
// The monitor is outside the JS mutex. Re-read immediately before the
|
|
1945
2027
|
// destructive metadata clear so an output that became visible during the
|
|
1946
2028
|
// action scan blocks the operation.
|
|
@@ -2024,7 +2106,12 @@ async function newVaultOutput(
|
|
|
2024
2106
|
throw new VaultError('template-invalid', 'Vault salt key index is exhausted')
|
|
2025
2107
|
}
|
|
2026
2108
|
const saltKeyId = String(keyIndex)
|
|
2027
|
-
const salt = await deriveVaultSalt(
|
|
2109
|
+
const salt = await deriveVaultSalt(
|
|
2110
|
+
w,
|
|
2111
|
+
adminOriginator,
|
|
2112
|
+
saltKeyId,
|
|
2113
|
+
meta.keys.map(key => key.serial)
|
|
2114
|
+
)
|
|
2028
2115
|
const lockingScript = buildLock({ commitments: pubkeys.map(pk => commitment(pk, salt)), saltHex64: salt })
|
|
2029
2116
|
const v7: VaultInstructionsV7 = {
|
|
2030
2117
|
v: 7,
|
|
@@ -2053,7 +2140,13 @@ async function newVaultOutput(
|
|
|
2053
2140
|
outputDescription: 'Vault marker'
|
|
2054
2141
|
}
|
|
2055
2142
|
|
|
2056
|
-
const descriptorCiphertext = await encryptVaultDescriptorPlaintext(
|
|
2143
|
+
const descriptorCiphertext = await encryptVaultDescriptorPlaintext(
|
|
2144
|
+
w,
|
|
2145
|
+
adminOriginator,
|
|
2146
|
+
chain,
|
|
2147
|
+
saltKeyId,
|
|
2148
|
+
customInstructions
|
|
2149
|
+
)
|
|
2057
2150
|
const descriptorOutput: VaultOutputSpec = {
|
|
2058
2151
|
satoshis: 0,
|
|
2059
2152
|
lockingScript: buildVaultDescriptorScript(descriptorCiphertext).toHex(),
|
|
@@ -2100,7 +2193,7 @@ function requireReleased(opts: VaultTransferOptions | undefined, scopeToken: Vau
|
|
|
2100
2193
|
* evidence that any backup service exists. */
|
|
2101
2194
|
async function defaultPrivateBackupEnabled(): Promise<boolean> {
|
|
2102
2195
|
try {
|
|
2103
|
-
return getBackupUrl() !== '' && await isBackupPushEnabled()
|
|
2196
|
+
return getBackupUrl() !== '' && (await isBackupPushEnabled())
|
|
2104
2197
|
} catch {
|
|
2105
2198
|
return false
|
|
2106
2199
|
}
|
|
@@ -2120,7 +2213,10 @@ async function requireMeta(scopeToken?: VaultScopeToken): Promise<VaultMeta> {
|
|
|
2120
2213
|
const meta = await vaultStore.getMeta(scopeToken)
|
|
2121
2214
|
if (!meta) throw new VaultError('not-enrolled', 'Vault is not set up')
|
|
2122
2215
|
if (meta.keys.length < VAULT_MIN_KEYS) {
|
|
2123
|
-
throw new VaultError(
|
|
2216
|
+
throw new VaultError(
|
|
2217
|
+
'not-enough-keys',
|
|
2218
|
+
`A vault needs at least ${VAULT_MIN_KEYS} keys; ${meta.keys.length} enrolled`
|
|
2219
|
+
)
|
|
2124
2220
|
}
|
|
2125
2221
|
return meta
|
|
2126
2222
|
}
|
|
@@ -2131,11 +2227,7 @@ async function requireMeta(scopeToken?: VaultScopeToken): Promise<VaultMeta> {
|
|
|
2131
2227
|
* accepted or queued for retry. Missing, duplicate, unrelated, failed, and
|
|
2132
2228
|
* future/unknown statuses are all ambiguous and therefore remain held.
|
|
2133
2229
|
*/
|
|
2134
|
-
function requireReleasedHeldTransaction(
|
|
2135
|
-
released: CreateActionResult,
|
|
2136
|
-
txid: string,
|
|
2137
|
-
description: string
|
|
2138
|
-
): void {
|
|
2230
|
+
function requireReleasedHeldTransaction(released: CreateActionResult, txid: string, description: string): void {
|
|
2139
2231
|
const results = released.sendWithResults
|
|
2140
2232
|
if (
|
|
2141
2233
|
!Array.isArray(results) ||
|
|
@@ -2181,7 +2273,8 @@ function validateDepositPlan(tx: Transaction, expected: VaultOutputPlan): void {
|
|
|
2181
2273
|
throw new VaultError('no-transaction', 'Vault deposit has an invalid wallet funding outpoint')
|
|
2182
2274
|
}
|
|
2183
2275
|
const outpoint = `${txid}.${vout}`
|
|
2184
|
-
if (seenInputs.has(outpoint))
|
|
2276
|
+
if (seenInputs.has(outpoint))
|
|
2277
|
+
throw new VaultError('no-transaction', `Vault deposit repeats funding input ${outpoint}`)
|
|
2185
2278
|
seenInputs.add(outpoint)
|
|
2186
2279
|
const source = input.sourceTransaction?.outputs[vout]
|
|
2187
2280
|
const sourceSatoshis = source?.satoshis
|
|
@@ -2221,7 +2314,8 @@ function validateDepositPlan(tx: Transaction, expected: VaultOutputPlan): void {
|
|
|
2221
2314
|
throw new VaultError('no-transaction', 'Vault deposit has invalid implicit wallet change')
|
|
2222
2315
|
}
|
|
2223
2316
|
outputValue += changeSatoshis
|
|
2224
|
-
if (!Number.isSafeInteger(outputValue))
|
|
2317
|
+
if (!Number.isSafeInteger(outputValue))
|
|
2318
|
+
throw new VaultError('no-transaction', 'Vault deposit output value overflow')
|
|
2225
2319
|
}
|
|
2226
2320
|
const fee = inputValue - outputValue
|
|
2227
2321
|
// Add a conservative P2PKH witness allowance when checking the unsigned
|
|
@@ -2260,110 +2354,107 @@ export async function depositToVault(
|
|
|
2260
2354
|
): Promise<{ txid: string }> {
|
|
2261
2355
|
const scopeToken = vaultStore.captureScopeToken()
|
|
2262
2356
|
return await withVaultMutation(async () => {
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
}
|
|
2268
|
-
await requireOnline(opts)
|
|
2269
|
-
assertVaultScope(scopeToken)
|
|
2270
|
-
await requirePrivateBackup(opts, 'Vault deposit')
|
|
2271
|
-
assertVaultScope(scopeToken)
|
|
2272
|
-
// XR-002: authenticate meta BEFORE it is used to build a new output.
|
|
2273
|
-
const meta = await requireAuthenticatedMeta(w, adminOriginator, scopeToken, await requireMeta(scopeToken))
|
|
2274
|
-
if (meta.pendingRemoval) throw new VaultError('relock-required', 'Finish the pending key removal before depositing')
|
|
2275
|
-
if (meta.recovery?.required) {
|
|
2276
|
-
const adopted = new Set(meta.recovery.adoptedSerials)
|
|
2277
|
-
if (meta.keys.filter(key => adopted.has(key.serial)).length < VAULT_MIN_KEYS) {
|
|
2278
|
-
throw new VaultError('key-not-adopted', 'Verify at least two recovered YubiKeys before depositing new funds')
|
|
2357
|
+
assertVaultScope(scopeToken)
|
|
2358
|
+
requireReleased(opts, scopeToken, 'Vault deposit')
|
|
2359
|
+
if (!Number.isSafeInteger(satoshis) || satoshis < VAULT_DEPOSIT_MIN) {
|
|
2360
|
+
throw new VaultError('below-dust', `Vault deposits must be at least ${VAULT_DEPOSIT_MIN} satoshis`)
|
|
2279
2361
|
}
|
|
2280
|
-
|
|
2281
|
-
await reconcileHeldVaultDeposits(w, adminOriginator, meta, scopeToken)
|
|
2282
|
-
const existing = await reduceVerifiedVaultOutputs(
|
|
2283
|
-
w,
|
|
2284
|
-
adminOriginator,
|
|
2285
|
-
() => undefined,
|
|
2286
|
-
(_state, output) => requireOutputMetaConsistency([output], meta),
|
|
2287
|
-
scopeToken,
|
|
2288
|
-
'repair-unsigned'
|
|
2289
|
-
)
|
|
2290
|
-
const saltInventory = existing.inventory
|
|
2291
|
-
await addHistoricalVaultSaltInventory(w, adminOriginator, saltInventory, meta, scopeToken)
|
|
2292
|
-
|
|
2293
|
-
const output = await newVaultOutput(
|
|
2294
|
-
w,
|
|
2295
|
-
adminOriginator,
|
|
2296
|
-
meta,
|
|
2297
|
-
satoshis,
|
|
2298
|
-
'Vault deposit',
|
|
2299
|
-
saltInventory,
|
|
2300
|
-
scopeToken.chain
|
|
2301
|
-
)
|
|
2302
|
-
let reference: string | undefined
|
|
2303
|
-
let heldTxid: string | undefined
|
|
2304
|
-
let broadcastAttempted = false
|
|
2305
|
-
try {
|
|
2306
|
-
// Build and sign into `noSend` first. This gives this module a real
|
|
2307
|
-
// transaction to inspect before any deposit can leave the device.
|
|
2362
|
+
await requireOnline(opts)
|
|
2308
2363
|
assertVaultScope(scopeToken)
|
|
2309
|
-
|
|
2310
|
-
{
|
|
2311
|
-
description: 'Vault deposit',
|
|
2312
|
-
version: 1,
|
|
2313
|
-
outputs: [...output],
|
|
2314
|
-
labels: ['vault', 'vault-deposit'],
|
|
2315
|
-
options: { randomizeOutputs: false, noSend: true, signAndProcess: false }
|
|
2316
|
-
},
|
|
2317
|
-
adminOriginator
|
|
2318
|
-
)
|
|
2364
|
+
await requirePrivateBackup(opts, 'Vault deposit')
|
|
2319
2365
|
assertVaultScope(scopeToken)
|
|
2366
|
+
// XR-002: authenticate meta BEFORE it is used to build a new output.
|
|
2367
|
+
const meta = await requireAuthenticatedMeta(w, adminOriginator, scopeToken, await requireMeta(scopeToken))
|
|
2368
|
+
if (meta.pendingRemoval) throw new VaultError('relock-required', 'Finish the pending key removal before depositing')
|
|
2369
|
+
if (meta.recovery?.required) {
|
|
2370
|
+
const adopted = new Set(meta.recovery.adoptedSerials)
|
|
2371
|
+
if (meta.keys.filter(key => adopted.has(key.serial)).length < VAULT_MIN_KEYS) {
|
|
2372
|
+
throw new VaultError('key-not-adopted', 'Verify at least two recovered YubiKeys before depositing new funds')
|
|
2373
|
+
}
|
|
2374
|
+
}
|
|
2375
|
+
await reconcileHeldVaultDeposits(w, adminOriginator, meta, scopeToken)
|
|
2376
|
+
const existing = await reduceVerifiedVaultOutputs(
|
|
2377
|
+
w,
|
|
2378
|
+
adminOriginator,
|
|
2379
|
+
() => undefined,
|
|
2380
|
+
(_state, output) => requireOutputMetaConsistency([output], meta),
|
|
2381
|
+
scopeToken,
|
|
2382
|
+
'repair-unsigned'
|
|
2383
|
+
)
|
|
2384
|
+
const saltInventory = existing.inventory
|
|
2385
|
+
await addHistoricalVaultSaltInventory(w, adminOriginator, saltInventory, meta, scopeToken)
|
|
2320
2386
|
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2387
|
+
const output = await newVaultOutput(
|
|
2388
|
+
w,
|
|
2389
|
+
adminOriginator,
|
|
2390
|
+
meta,
|
|
2391
|
+
satoshis,
|
|
2392
|
+
'Vault deposit',
|
|
2393
|
+
saltInventory,
|
|
2394
|
+
scopeToken.chain
|
|
2395
|
+
)
|
|
2396
|
+
let reference: string | undefined
|
|
2397
|
+
let heldTxid: string | undefined
|
|
2398
|
+
let broadcastAttempted = false
|
|
2399
|
+
try {
|
|
2400
|
+
// Build and sign into `noSend` first. This gives this module a real
|
|
2401
|
+
// transaction to inspect before any deposit can leave the device.
|
|
2327
2402
|
assertVaultScope(scopeToken)
|
|
2328
|
-
const
|
|
2329
|
-
{
|
|
2403
|
+
const created = await w.createAction(
|
|
2404
|
+
{
|
|
2405
|
+
description: 'Vault deposit',
|
|
2406
|
+
version: 1,
|
|
2407
|
+
outputs: [...output],
|
|
2408
|
+
labels: ['vault', 'vault-deposit'],
|
|
2409
|
+
options: { randomizeOutputs: false, noSend: true, signAndProcess: false }
|
|
2410
|
+
},
|
|
2330
2411
|
adminOriginator
|
|
2331
2412
|
)
|
|
2332
2413
|
assertVaultScope(scopeToken)
|
|
2333
|
-
signedBytes = signed.tx
|
|
2334
|
-
reportedTxid = signed.txid ?? reportedTxid
|
|
2335
|
-
} else {
|
|
2336
|
-
signedBytes = created.tx
|
|
2337
|
-
}
|
|
2338
|
-
if (!signedBytes) throw new VaultError('no-transaction', 'Vault deposit produced no inspectable transaction')
|
|
2339
2414
|
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2415
|
+
let signedBytes: number[] | undefined
|
|
2416
|
+
let reportedTxid = created.txid
|
|
2417
|
+
if (created.signableTransaction) {
|
|
2418
|
+
reference = created.signableTransaction.reference
|
|
2419
|
+
const unsigned = Transaction.fromAtomicBEEF(created.signableTransaction.tx)
|
|
2420
|
+
validateDepositPlan(unsigned, output)
|
|
2421
|
+
assertVaultScope(scopeToken)
|
|
2422
|
+
const signed = await w.signAction({ reference, spends: {}, options: { noSend: true } }, adminOriginator)
|
|
2423
|
+
assertVaultScope(scopeToken)
|
|
2424
|
+
signedBytes = signed.tx
|
|
2425
|
+
reportedTxid = signed.txid ?? reportedTxid
|
|
2426
|
+
} else {
|
|
2427
|
+
signedBytes = created.tx
|
|
2428
|
+
}
|
|
2429
|
+
if (!signedBytes) throw new VaultError('no-transaction', 'Vault deposit produced no inspectable transaction')
|
|
2430
|
+
|
|
2431
|
+
const signedTx = Transaction.fromAtomicBEEF(signedBytes)
|
|
2432
|
+
validateDepositPlan(signedTx, output)
|
|
2433
|
+
heldTxid = signedTx.id('hex')
|
|
2434
|
+
if (reportedTxid && reportedTxid.toLowerCase() !== heldTxid) {
|
|
2435
|
+
throw new VaultError('no-transaction', 'Vault deposit transaction ID disagrees with its signed transaction')
|
|
2436
|
+
}
|
|
2437
|
+
// `sendWith` releases this already-inspected noSend action. Once the call
|
|
2438
|
+
// begins its broadcast outcome can be ambiguous, so cleanup below must no
|
|
2439
|
+
// longer free its inputs.
|
|
2440
|
+
assertVaultScope(scopeToken)
|
|
2441
|
+
broadcastAttempted = true
|
|
2442
|
+
const released = await w.createAction(
|
|
2443
|
+
{ description: 'Broadcast Vault deposit', options: { sendWith: [heldTxid] } },
|
|
2444
|
+
adminOriginator
|
|
2445
|
+
)
|
|
2446
|
+
assertVaultScope(scopeToken)
|
|
2447
|
+
requireReleasedHeldTransaction(released, heldTxid, 'Vault deposit')
|
|
2448
|
+
return { txid: heldTxid }
|
|
2449
|
+
} catch (e) {
|
|
2450
|
+
// noSend guarantees the transaction cannot leave before sendWith. A
|
|
2451
|
+
// locally detected plan/signing failure before that call may therefore
|
|
2452
|
+
// release its reservation, even if signAction already produced bytes.
|
|
2453
|
+
if (!broadcastAttempted && reference) {
|
|
2454
|
+
await w.abortAction({ reference }, adminOriginator).catch(() => {})
|
|
2455
|
+
}
|
|
2456
|
+
throw e
|
|
2364
2457
|
}
|
|
2365
|
-
throw e
|
|
2366
|
-
}
|
|
2367
2458
|
})
|
|
2368
2459
|
}
|
|
2369
2460
|
|
|
@@ -2474,7 +2565,8 @@ async function selectVaultInputs(
|
|
|
2474
2565
|
}
|
|
2475
2566
|
const add = (current: number, value: number, where: string): number => {
|
|
2476
2567
|
const next = current + value
|
|
2477
|
-
if (!Number.isSafeInteger(next))
|
|
2568
|
+
if (!Number.isSafeInteger(next))
|
|
2569
|
+
throw new VaultError('no-transaction', `${where} value exceeds the safe integer range`)
|
|
2478
2570
|
return next
|
|
2479
2571
|
}
|
|
2480
2572
|
const verified = await reduceVerifiedVaultOutputs(
|
|
@@ -2665,7 +2757,10 @@ function validateSignableVaultPlan(tx: Transaction, sel: VaultSelection, plan: V
|
|
|
2665
2757
|
const outpoint = `${txid}.${vout}`
|
|
2666
2758
|
const selected = selectedByOutpoint.get(outpoint)
|
|
2667
2759
|
if (!selected || seen.has(outpoint)) {
|
|
2668
|
-
throw new VaultError(
|
|
2760
|
+
throw new VaultError(
|
|
2761
|
+
'no-transaction',
|
|
2762
|
+
`Signable transaction contains an unapproved or duplicate input ${outpoint}`
|
|
2763
|
+
)
|
|
2669
2764
|
}
|
|
2670
2765
|
const source = input.sourceTransaction?.outputs[vout]
|
|
2671
2766
|
if (
|
|
@@ -2856,7 +2951,7 @@ async function createSignableVaultTx(
|
|
|
2856
2951
|
)
|
|
2857
2952
|
}
|
|
2858
2953
|
const bad = sel.selected[badAt]
|
|
2859
|
-
const current = sequences.get(bad.outpoint) ??
|
|
2954
|
+
const current = sequences.get(bad.outpoint) ?? tx.inputs[prepared[badAt].inputIndex].sequence ?? 0xffffffff
|
|
2860
2955
|
sequences.set(bad.outpoint, current - 1)
|
|
2861
2956
|
} catch (e) {
|
|
2862
2957
|
await w.abortAction({ reference }, adminOriginator).catch(() => {})
|
|
@@ -2896,8 +2991,20 @@ async function spendVaultOutputs(
|
|
|
2896
2991
|
opts?: VaultTransferOptions,
|
|
2897
2992
|
scopeToken?: VaultScopeToken
|
|
2898
2993
|
): Promise<VaultSpendResult> {
|
|
2899
|
-
const { tx, reference, prepared } = await createSignableVaultTx(
|
|
2900
|
-
|
|
2994
|
+
const { tx, reference, prepared } = await createSignableVaultTx(
|
|
2995
|
+
w,
|
|
2996
|
+
adminOriginator,
|
|
2997
|
+
sel,
|
|
2998
|
+
reason,
|
|
2999
|
+
plan,
|
|
3000
|
+
opts,
|
|
3001
|
+
scopeToken
|
|
3002
|
+
)
|
|
3003
|
+
const result = (txid: string): VaultSpendResult => ({
|
|
3004
|
+
txid,
|
|
3005
|
+
cappedInputs: sel.cappedInputs,
|
|
3006
|
+
unreachable: sel.unreachable
|
|
3007
|
+
})
|
|
2901
3008
|
const { chosen, selected } = sel
|
|
2902
3009
|
const total = selected.length
|
|
2903
3010
|
|
|
@@ -2913,12 +3020,10 @@ async function spendVaultOutputs(
|
|
|
2913
3020
|
// finally has not been entered yet.
|
|
2914
3021
|
if (signer.pubkey !== chosen.pubkey) {
|
|
2915
3022
|
signer.release()
|
|
2916
|
-
throw new VaultError(
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
{ tapped: signer.serial, chosen: chosen.serial }
|
|
2921
|
-
)
|
|
3023
|
+
throw new VaultError('serial-mismatch', 'Signer public key does not match the chosen key', undefined, {
|
|
3024
|
+
tapped: signer.serial,
|
|
3025
|
+
chosen: chosen.serial
|
|
3026
|
+
})
|
|
2922
3027
|
}
|
|
2923
3028
|
try {
|
|
2924
3029
|
for (let i = 0; i < total; i++) {
|
|
@@ -2994,7 +3099,8 @@ async function spendVaultOutputs(
|
|
|
2994
3099
|
txid = txid.toLowerCase()
|
|
2995
3100
|
|
|
2996
3101
|
if (createsVaultOutput) {
|
|
2997
|
-
if (!signed.tx)
|
|
3102
|
+
if (!signed.tx)
|
|
3103
|
+
throw new VaultError('no-transaction', 'Held Vault spend returned no inspectable signed transaction')
|
|
2998
3104
|
const signedTx = Transaction.fromAtomicBEEF(signed.tx)
|
|
2999
3105
|
if (signedTx.id('hex') !== txid) {
|
|
3000
3106
|
throw new VaultError('no-transaction', 'Held Vault spend transaction ID disagrees with its signed transaction')
|
|
@@ -3094,56 +3200,63 @@ export async function withdrawFromVault(
|
|
|
3094
3200
|
): Promise<VaultSpendResult> {
|
|
3095
3201
|
const scopeToken = vaultStore.captureScopeToken()
|
|
3096
3202
|
return await withVaultMutation(async () => {
|
|
3097
|
-
assertVaultScope(scopeToken)
|
|
3098
|
-
// Cheapest refusal first, as in depositToVault — see requireWithdrawalAmount.
|
|
3099
|
-
requireWithdrawalAmount(amount)
|
|
3100
|
-
// An offline user is never asked to present a key for a transfer that
|
|
3101
|
-
// cannot proceed.
|
|
3102
|
-
await requireOnline(opts)
|
|
3103
|
-
assertVaultScope(scopeToken)
|
|
3104
|
-
const meta = await requireMeta(scopeToken)
|
|
3105
|
-
const sel = await selectVaultInputs(w, adminOriginator, chosenSerial, amount, undefined, scopeToken)
|
|
3106
|
-
const want = amount === 'all' ? sel.acc : amount
|
|
3107
|
-
const remainder = sel.acc - want
|
|
3108
|
-
const outputs: VaultSpendPlan['outputs'] = []
|
|
3109
|
-
if (remainder >= VAULT_DEPOSIT_MIN) {
|
|
3110
|
-
// P2-774: reconcileHeldVaultDeposits' gate exists only to protect
|
|
3111
|
-
// salt-index-allocation integrity for a call that is about to allocate a
|
|
3112
|
-
// NEW one (spec's high-water-mark check) — a full-balance withdrawal
|
|
3113
|
-
// (no remainder here) creates no vault output and does not need it, so
|
|
3114
|
-
// only THIS branch, which re-vaults the remainder, still requires it.
|
|
3115
|
-
await reconcileHeldVaultDeposits(w, adminOriginator, meta, scopeToken)
|
|
3116
|
-
// Re-vaulting CREATES a vault output, which the release flag gates (spec
|
|
3117
|
-
// §5.5). Withdrawing pre-existing outputs — 'all' — never is.
|
|
3118
|
-
requireReleased(opts, scopeToken, 'Re-vaulting a remainder')
|
|
3119
|
-
await requirePrivateBackup(opts, 'Re-vaulting a remainder')
|
|
3120
3203
|
assertVaultScope(scopeToken)
|
|
3121
|
-
//
|
|
3122
|
-
|
|
3123
|
-
//
|
|
3124
|
-
//
|
|
3125
|
-
|
|
3126
|
-
|
|
3127
|
-
|
|
3128
|
-
|
|
3204
|
+
// Cheapest refusal first, as in depositToVault — see requireWithdrawalAmount.
|
|
3205
|
+
requireWithdrawalAmount(amount)
|
|
3206
|
+
// An offline user is never asked to present a key for a transfer that
|
|
3207
|
+
// cannot proceed.
|
|
3208
|
+
await requireOnline(opts)
|
|
3209
|
+
assertVaultScope(scopeToken)
|
|
3210
|
+
const meta = await requireMeta(scopeToken)
|
|
3211
|
+
const sel = await selectVaultInputs(w, adminOriginator, chosenSerial, amount, undefined, scopeToken)
|
|
3212
|
+
const want = amount === 'all' ? sel.acc : amount
|
|
3213
|
+
const remainder = sel.acc - want
|
|
3214
|
+
const outputs: VaultSpendPlan['outputs'] = []
|
|
3215
|
+
if (remainder >= VAULT_DEPOSIT_MIN) {
|
|
3216
|
+
// P2-774: reconcileHeldVaultDeposits' gate exists only to protect
|
|
3217
|
+
// salt-index-allocation integrity for a call that is about to allocate a
|
|
3218
|
+
// NEW one (spec's high-water-mark check) — a full-balance withdrawal
|
|
3219
|
+
// (no remainder here) creates no vault output and does not need it, so
|
|
3220
|
+
// only THIS branch, which re-vaults the remainder, still requires it.
|
|
3221
|
+
await reconcileHeldVaultDeposits(w, adminOriginator, meta, scopeToken)
|
|
3222
|
+
// Re-vaulting CREATES a vault output, which the release flag gates (spec
|
|
3223
|
+
// §5.5). Withdrawing pre-existing outputs — 'all' — never is.
|
|
3224
|
+
requireReleased(opts, scopeToken, 'Re-vaulting a remainder')
|
|
3225
|
+
await requirePrivateBackup(opts, 'Re-vaulting a remainder')
|
|
3226
|
+
assertVaultScope(scopeToken)
|
|
3227
|
+
// XR-002: authenticate meta BEFORE it is used to build the re-vaulted
|
|
3228
|
+
// remainder's new output. Scoped to exactly this branch — the only one
|
|
3229
|
+
// that creates an output — so an otherwise-unauthenticated meta never
|
|
3230
|
+
// blocks a plain 'all' withdrawal, whose authority is its spent outputs'
|
|
3231
|
+
// own real locks, never local meta (see requireAuthenticatedMeta).
|
|
3232
|
+
const authenticatedMeta = await requireAuthenticatedMeta(w, adminOriginator, scopeToken, sel.meta)
|
|
3233
|
+
await addHistoricalVaultSaltInventory(w, adminOriginator, sel.saltInventory, authenticatedMeta, scopeToken)
|
|
3234
|
+
outputs.push(
|
|
3235
|
+
...(await newVaultOutput(
|
|
3236
|
+
w,
|
|
3237
|
+
adminOriginator,
|
|
3238
|
+
authenticatedMeta,
|
|
3239
|
+
remainder,
|
|
3240
|
+
'Vault change',
|
|
3241
|
+
sel.saltInventory,
|
|
3242
|
+
scopeToken.chain
|
|
3243
|
+
))
|
|
3244
|
+
)
|
|
3245
|
+
}
|
|
3246
|
+
return await spendVaultOutputs(
|
|
3129
3247
|
w,
|
|
3130
3248
|
adminOriginator,
|
|
3131
|
-
|
|
3132
|
-
|
|
3133
|
-
|
|
3134
|
-
|
|
3135
|
-
|
|
3136
|
-
|
|
3137
|
-
|
|
3138
|
-
|
|
3139
|
-
|
|
3140
|
-
|
|
3141
|
-
|
|
3142
|
-
reason,
|
|
3143
|
-
{ outputs, labels: ['vault', 'vault-withdraw'], inputDescription: 'Vault withdrawal', description: 'Vault withdrawal' },
|
|
3144
|
-
opts,
|
|
3145
|
-
scopeToken
|
|
3146
|
-
)
|
|
3249
|
+
sel,
|
|
3250
|
+
reason,
|
|
3251
|
+
{
|
|
3252
|
+
outputs,
|
|
3253
|
+
labels: ['vault', 'vault-withdraw'],
|
|
3254
|
+
inputDescription: 'Vault withdrawal',
|
|
3255
|
+
description: 'Vault withdrawal'
|
|
3256
|
+
},
|
|
3257
|
+
opts,
|
|
3258
|
+
scopeToken
|
|
3259
|
+
)
|
|
3147
3260
|
})
|
|
3148
3261
|
}
|
|
3149
3262
|
|
|
@@ -3183,8 +3296,12 @@ const VAULT_MAX_DESCRIPTOR_OUTPUT_BYTES =
|
|
|
3183
3296
|
* when below dust).
|
|
3184
3297
|
*/
|
|
3185
3298
|
export function estimateRelockFee(inputCount: number, lockLen: number, satPerKb = 100): number {
|
|
3186
|
-
const size =
|
|
3187
|
-
|
|
3299
|
+
const size =
|
|
3300
|
+
10 +
|
|
3301
|
+
inputCount * (41 + 3 + R1C_UNLOCK_LEN) +
|
|
3302
|
+
(3 + lockLen + 8) +
|
|
3303
|
+
VAULT_MARKER_OUTPUT_BYTES +
|
|
3304
|
+
VAULT_MAX_DESCRIPTOR_OUTPUT_BYTES
|
|
3188
3305
|
const kb = Math.ceil(size / 1000)
|
|
3189
3306
|
return Math.ceil((kb * satPerKb * 11) / 10)
|
|
3190
3307
|
}
|
|
@@ -3211,78 +3328,78 @@ export async function relockVault(
|
|
|
3211
3328
|
): Promise<VaultSpendResult> {
|
|
3212
3329
|
const scopeToken = vaultStore.captureScopeToken()
|
|
3213
3330
|
return await withVaultMutation(async () => {
|
|
3214
|
-
assertVaultScope(scopeToken)
|
|
3215
|
-
requireReleased(opts, scopeToken, 'Re-locking the vault')
|
|
3216
|
-
await requireOnline(opts)
|
|
3217
|
-
assertVaultScope(scopeToken)
|
|
3218
|
-
await requirePrivateBackup(opts, 'Re-locking the vault')
|
|
3219
|
-
assertVaultScope(scopeToken)
|
|
3220
|
-
// XR-002: authenticate meta BEFORE anything below uses it — relockVault
|
|
3221
|
-
// always builds a new output, so unlike withdrawFromVault this runs
|
|
3222
|
-
// unconditionally, and early enough that a reconciled/retagged meta is
|
|
3223
|
-
// what selectVaultInputs (re-reading vaultStore.getMeta itself) and the
|
|
3224
|
-
// fee estimate below both see.
|
|
3225
|
-
const meta = await requireAuthenticatedMeta(w, adminOriginator, scopeToken, await requireMeta(scopeToken))
|
|
3226
|
-
await reconcileHeldVaultDeposits(w, adminOriginator, meta, scopeToken)
|
|
3227
|
-
const revokePubkey = opts?.revokePubkey
|
|
3228
|
-
const sel = await selectVaultInputs(w, adminOriginator, chosenSerial, 'all', revokePubkey, scopeToken)
|
|
3229
|
-
if (sel.meta.pendingRemoval && !revokePubkey) {
|
|
3230
|
-
throw new VaultError('relock-required', 'Finish the pending key removal before another re-lock')
|
|
3231
|
-
}
|
|
3232
|
-
if (revokePubkey && sel.chosen.pubkey === revokePubkey) {
|
|
3233
|
-
throw new VaultError('wrong-key', 'The key being revoked cannot authorize its own replacement')
|
|
3234
|
-
}
|
|
3235
|
-
if (revokePubkey && sel.meta.pendingRemoval?.key.pubkey !== revokePubkey) {
|
|
3236
|
-
throw new VaultError('not-enrolled', 'The key being revoked does not match the pending removal')
|
|
3237
|
-
}
|
|
3238
|
-
// Before the fee estimate below, which cannot size a lock outside 2..5.
|
|
3239
|
-
requireLockableKeyCount(sel.keys.length)
|
|
3240
|
-
await addHistoricalVaultSaltInventory(w, adminOriginator, sel.saltInventory, sel.meta, scopeToken)
|
|
3241
|
-
const fee = estimateRelockFee(sel.selected.length, R1C_LOCK_LEN(sel.keys.length))
|
|
3242
|
-
const relocked = sel.acc - fee
|
|
3243
|
-
if (relocked < VAULT_DEPOSIT_MIN) {
|
|
3244
|
-
throw new VaultError(
|
|
3245
|
-
'too-small-to-relock',
|
|
3246
|
-
`Re-locking ${sel.acc} satoshis would leave ${relocked} after fees, below the ${VAULT_DEPOSIT_MIN} floor`
|
|
3247
|
-
)
|
|
3248
|
-
}
|
|
3249
|
-
const result = await spendVaultOutputs(
|
|
3250
|
-
w,
|
|
3251
|
-
adminOriginator,
|
|
3252
|
-
sel,
|
|
3253
|
-
reason,
|
|
3254
|
-
{
|
|
3255
|
-
outputs: await newVaultOutput(
|
|
3256
|
-
w,
|
|
3257
|
-
adminOriginator,
|
|
3258
|
-
sel.meta,
|
|
3259
|
-
relocked,
|
|
3260
|
-
'Vault re-lock',
|
|
3261
|
-
sel.saltInventory,
|
|
3262
|
-
scopeToken.chain
|
|
3263
|
-
),
|
|
3264
|
-
labels: ['vault', 'vault-relock'],
|
|
3265
|
-
inputDescription: 'Vault re-lock',
|
|
3266
|
-
// Fixed, like the deposit and withdrawal descriptions (see
|
|
3267
|
-
// VaultSpendPlan): the caller's `reason` is NFC-prompt copy that names
|
|
3268
|
-
// the keys to tap, which read as nonsense in the activity list.
|
|
3269
|
-
description: 'Vault relock'
|
|
3270
|
-
},
|
|
3271
|
-
opts,
|
|
3272
|
-
scopeToken
|
|
3273
|
-
)
|
|
3274
|
-
if (revokePubkey) {
|
|
3275
|
-
// The tag covers pendingRemoval.state, so the 'broadcast' write must be
|
|
3276
|
-
// re-tagged like every other meta write, or the stored meta stops
|
|
3277
|
-
// verifying and every later check falls back to a chain re-scan.
|
|
3278
3331
|
assertVaultScope(scopeToken)
|
|
3279
|
-
|
|
3280
|
-
|
|
3281
|
-
|
|
3282
|
-
|
|
3332
|
+
requireReleased(opts, scopeToken, 'Re-locking the vault')
|
|
3333
|
+
await requireOnline(opts)
|
|
3334
|
+
assertVaultScope(scopeToken)
|
|
3335
|
+
await requirePrivateBackup(opts, 'Re-locking the vault')
|
|
3336
|
+
assertVaultScope(scopeToken)
|
|
3337
|
+
// XR-002: authenticate meta BEFORE anything below uses it — relockVault
|
|
3338
|
+
// always builds a new output, so unlike withdrawFromVault this runs
|
|
3339
|
+
// unconditionally, and early enough that a reconciled/retagged meta is
|
|
3340
|
+
// what selectVaultInputs (re-reading vaultStore.getMeta itself) and the
|
|
3341
|
+
// fee estimate below both see.
|
|
3342
|
+
const meta = await requireAuthenticatedMeta(w, adminOriginator, scopeToken, await requireMeta(scopeToken))
|
|
3343
|
+
await reconcileHeldVaultDeposits(w, adminOriginator, meta, scopeToken)
|
|
3344
|
+
const revokePubkey = opts?.revokePubkey
|
|
3345
|
+
const sel = await selectVaultInputs(w, adminOriginator, chosenSerial, 'all', revokePubkey, scopeToken)
|
|
3346
|
+
if (sel.meta.pendingRemoval && !revokePubkey) {
|
|
3347
|
+
throw new VaultError('relock-required', 'Finish the pending key removal before another re-lock')
|
|
3348
|
+
}
|
|
3349
|
+
if (revokePubkey && sel.chosen.pubkey === revokePubkey) {
|
|
3350
|
+
throw new VaultError('wrong-key', 'The key being revoked cannot authorize its own replacement')
|
|
3351
|
+
}
|
|
3352
|
+
if (revokePubkey && sel.meta.pendingRemoval?.key.pubkey !== revokePubkey) {
|
|
3353
|
+
throw new VaultError('not-enrolled', 'The key being revoked does not match the pending removal')
|
|
3354
|
+
}
|
|
3355
|
+
// Before the fee estimate below, which cannot size a lock outside 2..5.
|
|
3356
|
+
requireLockableKeyCount(sel.keys.length)
|
|
3357
|
+
await addHistoricalVaultSaltInventory(w, adminOriginator, sel.saltInventory, sel.meta, scopeToken)
|
|
3358
|
+
const fee = estimateRelockFee(sel.selected.length, R1C_LOCK_LEN(sel.keys.length))
|
|
3359
|
+
const relocked = sel.acc - fee
|
|
3360
|
+
if (relocked < VAULT_DEPOSIT_MIN) {
|
|
3361
|
+
throw new VaultError(
|
|
3362
|
+
'too-small-to-relock',
|
|
3363
|
+
`Re-locking ${sel.acc} satoshis would leave ${relocked} after fees, below the ${VAULT_DEPOSIT_MIN} floor`
|
|
3364
|
+
)
|
|
3365
|
+
}
|
|
3366
|
+
const result = await spendVaultOutputs(
|
|
3367
|
+
w,
|
|
3368
|
+
adminOriginator,
|
|
3369
|
+
sel,
|
|
3370
|
+
reason,
|
|
3371
|
+
{
|
|
3372
|
+
outputs: await newVaultOutput(
|
|
3373
|
+
w,
|
|
3374
|
+
adminOriginator,
|
|
3375
|
+
sel.meta,
|
|
3376
|
+
relocked,
|
|
3377
|
+
'Vault re-lock',
|
|
3378
|
+
sel.saltInventory,
|
|
3379
|
+
scopeToken.chain
|
|
3380
|
+
),
|
|
3381
|
+
labels: ['vault', 'vault-relock'],
|
|
3382
|
+
inputDescription: 'Vault re-lock',
|
|
3383
|
+
// Fixed, like the deposit and withdrawal descriptions (see
|
|
3384
|
+
// VaultSpendPlan): the caller's `reason` is NFC-prompt copy that names
|
|
3385
|
+
// the keys to tap, which read as nonsense in the activity list.
|
|
3386
|
+
description: 'Vault relock'
|
|
3387
|
+
},
|
|
3388
|
+
opts,
|
|
3389
|
+
scopeToken
|
|
3283
3390
|
)
|
|
3284
|
-
|
|
3285
|
-
|
|
3391
|
+
if (revokePubkey) {
|
|
3392
|
+
// The tag covers pendingRemoval.state, so the 'broadcast' write must be
|
|
3393
|
+
// re-tagged like every other meta write, or the stored meta stops
|
|
3394
|
+
// verifying and every later check falls back to a chain re-scan.
|
|
3395
|
+
assertVaultScope(scopeToken)
|
|
3396
|
+
const scope = vaultStore.getScope()
|
|
3397
|
+
if (!scope) throw new VaultError('not-enrolled', 'Wallet vault scope is not configured')
|
|
3398
|
+
await vaultStore.markKeyRemovalBroadcast(scopeToken, next =>
|
|
3399
|
+
computeVaultMetaAuthorityTag(asHmacWallet(w), adminOriginator, next, scope)
|
|
3400
|
+
)
|
|
3401
|
+
}
|
|
3402
|
+
return result
|
|
3286
3403
|
})
|
|
3287
3404
|
}
|
|
3288
3405
|
|
|
@@ -3553,21 +3670,13 @@ async function actionCarriesCurrentRelock(
|
|
|
3553
3670
|
}
|
|
3554
3671
|
const salt = await resolveVaultSalt(w, adminOriginator, ci)
|
|
3555
3672
|
verifyInstructionsAgainstLock(ci, lock, `Vault re-lock ${action.txid ?? action.reference ?? ''}`, salt)
|
|
3556
|
-
const carriesCurrentRelock =
|
|
3673
|
+
const carriesCurrentRelock =
|
|
3557
3674
|
ci.vaultId === meta.vaultId &&
|
|
3558
3675
|
ci.createdAt === meta.createdAt &&
|
|
3559
3676
|
ci.revision === meta.revision &&
|
|
3560
3677
|
sameInstructionKeys(meta.keys, ci.keys)
|
|
3561
|
-
)
|
|
3562
3678
|
if (carriesCurrentRelock && saltInventory) {
|
|
3563
|
-
rememberVaultSalt(
|
|
3564
|
-
saltInventory,
|
|
3565
|
-
ci,
|
|
3566
|
-
lock,
|
|
3567
|
-
vaultActionOutputId(action, output.outputIndex),
|
|
3568
|
-
expectedChain,
|
|
3569
|
-
salt
|
|
3570
|
-
)
|
|
3679
|
+
rememberVaultSalt(saltInventory, ci, lock, vaultActionOutputId(action, output.outputIndex), expectedChain, salt)
|
|
3571
3680
|
}
|
|
3572
3681
|
return carriesCurrentRelock
|
|
3573
3682
|
}
|
|
@@ -3589,9 +3698,7 @@ function actionSpendsPendingRemovalKey(
|
|
|
3589
3698
|
const lock = LockingScript.fromHex(input.sourceLockingScript)
|
|
3590
3699
|
bakedCommitments(lock)
|
|
3591
3700
|
const record = inventory.outputRecords.get(input.sourceOutpoint.toLowerCase())
|
|
3592
|
-
return record !== undefined &&
|
|
3593
|
-
record.lockingScript === lock.toHex() &&
|
|
3594
|
-
record.pubkeys.has(pending.key.pubkey)
|
|
3701
|
+
return record !== undefined && record.lockingScript === lock.toHex() && record.pubkeys.has(pending.key.pubkey)
|
|
3595
3702
|
} catch {
|
|
3596
3703
|
return false
|
|
3597
3704
|
}
|
|
@@ -3622,10 +3729,7 @@ async function isUnbroadcastPendingRemovalRelock(
|
|
|
3622
3729
|
* tombstone is cleared only when every Vault action is accepted by the network
|
|
3623
3730
|
* (ACCEPTED_ACTION_STATUSES) or is a failed deposit, a matching re-lock is
|
|
3624
3731
|
* accepted, and two authenticated scans find no lock authorizing the key. */
|
|
3625
|
-
export async function finalizeVaultKeyRemoval(
|
|
3626
|
-
w: VaultWallet,
|
|
3627
|
-
adminOriginator: string
|
|
3628
|
-
): Promise<boolean> {
|
|
3732
|
+
export async function finalizeVaultKeyRemoval(w: VaultWallet, adminOriginator: string): Promise<boolean> {
|
|
3629
3733
|
const scopeToken = vaultStore.captureScopeToken()
|
|
3630
3734
|
return await withVaultMutation(async () => {
|
|
3631
3735
|
assertVaultScope(scopeToken)
|
|
@@ -3660,36 +3764,55 @@ export async function finalizeVaultKeyRemoval(
|
|
|
3660
3764
|
let actionStateBlocks = false
|
|
3661
3765
|
const actionSaltInventory = emptyVaultSaltInventory()
|
|
3662
3766
|
await addHistoricalVaultSaltInventory(w, adminOriginator, actionSaltInventory, meta, scopeToken)
|
|
3663
|
-
await scanVaultActions(
|
|
3664
|
-
|
|
3665
|
-
|
|
3666
|
-
|
|
3667
|
-
|
|
3668
|
-
|
|
3669
|
-
|
|
3670
|
-
|
|
3671
|
-
|
|
3672
|
-
|
|
3673
|
-
}
|
|
3674
|
-
|
|
3675
|
-
if (
|
|
3676
|
-
|
|
3677
|
-
|
|
3678
|
-
|
|
3767
|
+
await scanVaultActions(
|
|
3768
|
+
w,
|
|
3769
|
+
adminOriginator,
|
|
3770
|
+
{
|
|
3771
|
+
labels: [],
|
|
3772
|
+
includeLabels: true,
|
|
3773
|
+
includeInputs: true,
|
|
3774
|
+
includeInputSourceLockingScripts: true,
|
|
3775
|
+
includeOutputs: true,
|
|
3776
|
+
includeOutputLockingScripts: true
|
|
3777
|
+
},
|
|
3778
|
+
async action => {
|
|
3779
|
+
if (
|
|
3780
|
+
await isUnbroadcastPendingRemovalRelock(
|
|
3781
|
+
w,
|
|
3782
|
+
adminOriginator,
|
|
3783
|
+
action,
|
|
3784
|
+
meta,
|
|
3785
|
+
scopeToken.chain,
|
|
3786
|
+
actionSaltInventory
|
|
3787
|
+
)
|
|
3788
|
+
) {
|
|
3789
|
+
unsignedRelockReferences.push(action.reference!)
|
|
3679
3790
|
}
|
|
3680
|
-
|
|
3681
|
-
|
|
3682
|
-
|
|
3683
|
-
|
|
3684
|
-
|
|
3685
|
-
|
|
3791
|
+
if (
|
|
3792
|
+
action.txid &&
|
|
3793
|
+
(await actionCarriesCurrentRelock(w, adminOriginator, action, meta, scopeToken.chain, actionSaltInventory))
|
|
3794
|
+
) {
|
|
3795
|
+
if (!actionSpendsPendingRemovalKey(action, meta, actionSaltInventory)) invalidCurrentRelock = true
|
|
3796
|
+
else {
|
|
3797
|
+
matchingRelock = true
|
|
3798
|
+
if (ACCEPTED_ACTION_STATUSES.has(action.status)) acceptedMatchingRelock = true
|
|
3799
|
+
}
|
|
3800
|
+
}
|
|
3801
|
+
if (!actionTouchesVault(action)) return
|
|
3802
|
+
if (ACCEPTED_ACTION_STATUSES.has(action.status)) return
|
|
3803
|
+
if (action.status === 'failed') {
|
|
3804
|
+
const labels = new Set(action.labels ?? [])
|
|
3805
|
+
if (!(labels.has('vault-deposit') && !labels.has('vault-withdraw') && !labels.has('vault-relock'))) {
|
|
3806
|
+
actionStateBlocks = true
|
|
3807
|
+
}
|
|
3808
|
+
} else {
|
|
3809
|
+
// Not yet accepted ('sending' included) or an unknown status.
|
|
3686
3810
|
actionStateBlocks = true
|
|
3687
3811
|
}
|
|
3688
|
-
}
|
|
3689
|
-
|
|
3690
|
-
|
|
3691
|
-
|
|
3692
|
-
}, scopeToken, true)
|
|
3812
|
+
},
|
|
3813
|
+
scopeToken,
|
|
3814
|
+
true
|
|
3815
|
+
)
|
|
3693
3816
|
|
|
3694
3817
|
assertVaultScope(scopeToken)
|
|
3695
3818
|
await verifyVaultSaltDerivations(w, adminOriginator, actionSaltInventory)
|