Durable, beneficiary-bound subaddress index reservation

This commit is contained in:
2026-10-22 17:26:13 -04:00
parent 84e6e08209
commit 2ecb42c019
9 changed files with 416 additions and 42 deletions
+16 -21
View File
@@ -42,30 +42,25 @@ function identityFromRow(row: Record<string, unknown>): PilotCustodyIdentity {
return identity;
}
async function requireEmptyFinancialDatabase(client: PoolClient): Promise<void> {
const result = await client.query(`SELECT
EXISTS(SELECT 1 FROM wk_custody.custody_meta) OR
EXISTS(SELECT 1 FROM wk_custody.accounts) OR
EXISTS(SELECT 1 FROM wk_custody.journals) OR
EXISTS(SELECT 1 FROM wk_custody.entries) OR
EXISTS(SELECT 1 FROM wk_custody.listings) OR
EXISTS(SELECT 1 FROM wk_custody.orders) OR
EXISTS(SELECT 1 FROM wk_custody.withdrawals) OR
EXISTS(SELECT 1 FROM wk_custody.receipts) OR
EXISTS(SELECT 1 FROM wk_custody.outbox) OR
EXISTS(SELECT 1 FROM wk_custody.credits) OR
EXISTS(SELECT 1 FROM wk_custody.monero_observer_state) OR
EXISTS(SELECT 1 FROM wk_custody.monero_transfer_observations) OR
EXISTS(SELECT 1 FROM wk_custody.monero_observation_scans) OR
EXISTS(SELECT 1 FROM wk_custody.monero_deposit_allocations) OR
EXISTS(SELECT 1 FROM wk_custody.monero_deposit_credit_events) OR
EXISTS(SELECT 1 FROM wk_custody.monero_deposit_reorg_holds) OR
EXISTS(SELECT 1 FROM wk_custody.monero_deposit_release_events) OR
EXISTS(SELECT 1 FROM wk_custody.monero_deposit_reorg_resolutions) OR
EXISTS(SELECT 1 FROM wk_custody.monero_withdrawal_intents) AS populated`);
async function requireEmptyFinancialDatabase(client: PoolClient, allowPilotIdentity = false): Promise<void> {
const result = await client.query(`SELECT NOT wk_custody.pilot_reservation_database_isolated() AS populated,
(SELECT count(*) FROM wk_custody.pilot_custody_identity) AS pilot_identities`);
if (result.rows[0].populated) {
throw new Error('Pilot identity requires a separate empty custody database; existing mock or financial data cannot be reused');
}
const identities = Number(result.rows[0].pilot_identities);
if ((!allowPilotIdentity && identities !== 0) || (allowPilotIdentity && identities !== 1)) {
throw new Error('Pilot custody database identity state is inconsistent');
}
if (!allowPilotIdentity) {
const reservations = await client.query('SELECT EXISTS(SELECT 1 FROM wk_custody.pilot_monero_deposit_reservations) AS populated');
if (reservations.rows[0].populated) throw new Error('Pilot identity requires an empty pilot reservation table');
}
}
/** Recheck the dedicated database boundary before each reservation; only its single identity row is allowed. */
export async function assertPilotReservationDatabaseEmpty(client: PoolClient): Promise<void> {
await requireEmptyFinancialDatabase(client, true);
}
/** One-time identity binding only. It does not enable deposit allocation, observation, credit, or withdrawal roles. */