/** * Withdrawal controller — HTTP handler for submitting on-chain withdrawal requests. */ import { Response } from 'express'; import * as withdrawalService from '../services/withdrawalService'; import { SecurityBlockedError } from '../services/withdrawalService'; import { AuthRequest } from '../middleware/auth'; /** * Submits a withdrawal request to send funds from the user's wallet to an external address. * May require a TOTP code when 2FA is enabled for the user. * @param req - Authenticated request. Body: `walletType`, `toAddress`, `amount`, optional `totpCode`. * @param res - JSON `{ transactionId, status }` on success, 400 on validation or withdrawal failure. */ export async function withdraw(req: AuthRequest, res: Response): Promise { try { const userId = req.user!.userId; const { walletType, toAddress, amount } = req.body; if (!walletType || !toAddress || amount == null) { res.status(400).json({ error: 'walletType, toAddress and amount required' }); return; } const amountRaw = BigInt(amount); const result = await withdrawalService.requestWithdrawal( userId, walletType, toAddress, amountRaw, req.body.totpCode ); res.json(result); } catch (e) { const msg = e instanceof Error ? e.message : 'Withdrawal failed'; const status = e instanceof SecurityBlockedError ? 403 : 400; res.status(status).json({ error: msg }); } }