Files
wikideck/server/market/custody-db-args.ts
T

48 lines
2.1 KiB
TypeScript
Executable File

export const CUSTODY_DB_ACTION_ARGUMENT_COUNTS = {
migrate: 0,
'migrate-mainnet-schema': 0,
'grant-runtime': 1,
'grant-monero-observer-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' && !isMainnet) {
throw new Error('Mainnet schema migration action requires the mainnet profile');
}
if (isMainnet && action !== 'grant-monero-observer-runtime' && action !== 'migrate-mainnet-schema') {
throw new Error('Only the separately scoped Monero observation-role grant is 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 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] };
}