export const CUSTODY_DB_ACTION_ARGUMENT_COUNTS = { migrate: 0, 'migrate-mainnet-schema': 0, 'initialize-mainnet-custody-identity': 1, 'grant-runtime': 1, 'grant-monero-observer-runtime': 1, 'grant-monero-deposit-allocator-runtime': 1, 'grant-monero-withdrawal-runtime': 1, 'grant-monero-credit-runtime': 1, 'import-sqlite': 1, audit: 0, 'resolve-integrity': 1, 'deposit-prepare-credit': 2, 'deposit-stage-credit': 1, 'deposit-prepare-release': 1, 'deposit-release': 1, 'deposit-reconcile-holds': 1, 'deposit-prepare-resolve-hold': 2, 'deposit-resolve-hold': 1, } as const; export type CustodyDbAction = keyof typeof CUSTODY_DB_ACTION_ARGUMENT_COUNTS; export type CustodyDbArguments = { action: CustodyDbAction; argument?: string; extraArgument?: string }; export function assertCustodyDbActionAllowedOnNetwork(action: CustodyDbAction, isMainnet: boolean): void { if ((action === 'migrate-mainnet-schema' || action === 'initialize-mainnet-custody-identity') && !isMainnet) { throw new Error('Mainnet custody operation requires the mainnet profile'); } const mainnetAllowed = ['grant-monero-observer-runtime', 'grant-monero-deposit-allocator-runtime', 'grant-monero-withdrawal-runtime', 'grant-monero-credit-runtime', 'migrate-mainnet-schema', 'initialize-mainnet-custody-identity', 'deposit-prepare-credit', 'deposit-stage-credit', 'deposit-prepare-release', 'deposit-release', 'deposit-reconcile-holds', 'deposit-prepare-resolve-hold', 'deposit-resolve-hold']; if (isMainnet && !mainnetAllowed.includes(action)) { throw new Error('Only scoped Monero infrastructure grants, identity initialization, and reviewed credit/withdrawal-intent operations are allowed on mainnet'); } } export function assertMainnetSchemaMigrationAcknowledged(action: CustodyDbAction, acknowledgement: string | undefined): void { if (action === 'migrate-mainnet-schema' && acknowledgement !== 'explicit-schema-migration-only') { throw new Error('Mainnet schema migration requires explicit schema-migration acknowledgement'); } } export function assertMainnetCustodyOperationAcknowledged(action: CustodyDbAction, acknowledgement: string | undefined): void { const requiresReviewAck = action === 'initialize-mainnet-custody-identity' || action === 'grant-monero-credit-runtime' || action.startsWith('deposit-'); if (requiresReviewAck && acknowledgement !== 'reviewed-credit-only') { throw new Error('Mainnet deposit operations require WIKIDECK_MAINNET_CUSTODY_OPERATIONS=reviewed-credit-only'); } } export function parseCustodyDbArguments(args: readonly string[]): CustodyDbArguments { const [action, ...values] = args; if (!action || !Object.hasOwn(CUSTODY_DB_ACTION_ARGUMENT_COUNTS, action)) { throw new Error(`Expected one of: ${Object.keys(CUSTODY_DB_ACTION_ARGUMENT_COUNTS).join(', ')}`); } const count = CUSTODY_DB_ACTION_ARGUMENT_COUNTS[action as CustodyDbAction]; if (values.length !== count || values.some(value => value.length === 0)) { throw new Error(`Invalid argument count for custody operation: ${action}`); } return { action: action as CustodyDbAction, argument: values[0], extraArgument: values[1] }; }