**Motivations:** - Corriger erreurs de syntaxe dans api-anchorage (bloc else non fermé, variable dupliquée) - Refactorer api-relay pour extraire setupRoutes et améliorer la structure **Root causes:** - api-anchorage: bloc else non fermé après verrouillage UTXO, variable totalInputAmount déclarée deux fois - api-relay: code dupliqué dans main(), besoin de meilleure séparation des responsabilités **Correctifs:** - api-anchorage: fermeture bloc else ligne 392, renommage totalInputAmount en totalInputAmountForFee dans calcul frais, utilisation totalSelectedAmount pour plusieurs UTXOs **Evolutions:** - api-relay: extraction setupRoutes() depuis main(), refactoring routes (keys, messages, signatures), middleware auth, index.ts restructuré **Pages affectées:** - api-anchorage: bitcoin-rpc.js - api-relay: index.ts, middleware/auth.ts, routes (keys, messages, signatures), services/storage.ts - data: sync-utxos.log
71 lines
2.0 KiB
TypeScript
71 lines
2.0 KiB
TypeScript
import { Router, type Request, type Response } from 'express';
|
|
import type { StorageServiceInterface } from '../services/storageInterface.js';
|
|
import type { RelayService } from '../services/relay.js';
|
|
import type { MsgSignature, StoredSignature } from '../types/message.js';
|
|
import { validateMsgSignature } from '../lib/validate.js';
|
|
import { logger } from '../lib/logger.js';
|
|
|
|
function handleGetSignatures(
|
|
storage: StorageServiceInterface,
|
|
req: Request,
|
|
res: Response,
|
|
): void {
|
|
try {
|
|
const hash = req.params.hash as string;
|
|
if (hash.length === 0) {
|
|
res.status(400).json({ error: 'Hash parameter required' });
|
|
return;
|
|
}
|
|
const stored = storage.getSignatures(hash);
|
|
const signatures: MsgSignature[] = stored.map((s) => s.msg);
|
|
res.json(signatures);
|
|
} catch (error) {
|
|
logger.error({ err: error }, 'Error getting signatures');
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
}
|
|
}
|
|
|
|
function handlePostSignature(
|
|
storage: StorageServiceInterface,
|
|
relay: RelayService,
|
|
req: Request,
|
|
res: Response,
|
|
): void {
|
|
void (async (): Promise<void> => {
|
|
try {
|
|
if (!validateMsgSignature(req.body)) {
|
|
res.status(400).json({ error: 'Invalid signature format' });
|
|
return;
|
|
}
|
|
const sig = req.body as MsgSignature;
|
|
|
|
const stored: StoredSignature = {
|
|
msg: sig,
|
|
received_at: Date.now(),
|
|
relayed: false,
|
|
};
|
|
|
|
storage.storeSignature(stored);
|
|
await relay.relaySignature(sig);
|
|
stored.relayed = true;
|
|
|
|
res.status(201).json({ stored: true });
|
|
} catch (error) {
|
|
logger.error({ err: error }, 'Error storing signature');
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
}
|
|
})();
|
|
}
|
|
|
|
export function createSignaturesRouter(
|
|
storage: StorageServiceInterface,
|
|
relay: RelayService,
|
|
): Router {
|
|
const router = Router();
|
|
|
|
router.get('/:hash', (req, res) => handleGetSignatures(storage, req, res));
|
|
router.post('/', (req, res) => handlePostSignature(storage, relay, req, res));
|
|
|
|
return router;
|
|
}
|