games_app/backend/src/repositories/hotWalletRepository.ts
2026-06-29 15:01:23 +02:00

48 lines
1.8 KiB
TypeScript

import { query } from '../db/connection';
import { HotWallet } from '../models';
/**
* Repository for hot wallet management.
* Provides access to active signing wallets used for outbound blockchain transactions.
*/
/**
* Returns the active hot wallet for a given wallet type.
* @param walletTypeId - ID of the wallet type to look up.
* @returns The active hot wallet record, or null if none is configured.
*/
export async function getActiveHotWallet(walletTypeId: number): Promise<HotWallet | null> {
const rows = await query<HotWallet[]>(
'SELECT id, wallet_type_id, address, encrypted_private_key, is_active FROM hot_wallets WHERE wallet_type_id = $1 AND is_active = TRUE LIMIT 1',
[walletTypeId]
);
return rows[0] || null;
}
export async function listActiveHotWallets(): Promise<HotWallet[]> {
return query<HotWallet[]>(
'SELECT id, wallet_type_id, address, encrypted_private_key, is_active FROM hot_wallets WHERE is_active = TRUE ORDER BY wallet_type_id'
);
}
/**
* Inserts a new hot wallet record.
* @param data - Hot wallet fields to persist.
* @param data.walletTypeId - Wallet type this hot wallet serves.
* @param data.address - On-chain address of the hot wallet.
* @param data.encryptedPrivateKey - Encrypted private key for signing transactions.
* @param data.derivationIndex - Optional HD derivation index used to generate the wallet.
* @returns Resolves when the row has been inserted.
*/
export async function create(data: {
walletTypeId: number;
address: string;
encryptedPrivateKey: string;
derivationIndex?: number | null;
}): Promise<void> {
await query(
'INSERT INTO hot_wallets (wallet_type_id, address, encrypted_private_key, derivation_index) VALUES ($1, $2, $3, $4)',
[data.walletTypeId, data.address, data.encryptedPrivateKey, data.derivationIndex ?? null]
);
}