150 lines
6.2 KiB
JavaScript
150 lines
6.2 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Express application setup.
|
|
* Configures security middleware, API routes, health checks, and global error handling.
|
|
*/
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|
var ownKeys = function(o) {
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
var ar = [];
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
return ar;
|
|
};
|
|
return ownKeys(o);
|
|
};
|
|
return function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
})();
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const express_1 = __importDefault(require("express"));
|
|
const cors_1 = __importDefault(require("cors"));
|
|
const helmet_1 = __importDefault(require("helmet"));
|
|
const config_1 = require("./config");
|
|
const routes_1 = __importDefault(require("./routes"));
|
|
const errorHandler_1 = require("./middleware/errorHandler");
|
|
const settingsRepository = __importStar(require("./repositories/settingsRepository"));
|
|
const withdrawalFee_1 = require("./utils/withdrawalFee");
|
|
const emailService_1 = require("./services/emailService");
|
|
function resolveCorsOrigin() {
|
|
const configured = config_1.config.app.corsOrigins;
|
|
if (configured.length > 0)
|
|
return configured;
|
|
if (config_1.config.env !== 'production')
|
|
return true;
|
|
if (config_1.config.app.publicUrl)
|
|
return [config_1.config.app.publicUrl];
|
|
console.error('[cors] CORS_ORIGINS is not set in production — denying cross-origin requests (fail-closed)');
|
|
return (_origin, callback) => callback(null, false);
|
|
}
|
|
const app = (0, express_1.default)();
|
|
// Behind nginx / Docker reverse proxy (required for rate-limit + req.ip).
|
|
if (process.env.TRUST_PROXY === 'true' || process.env.TRUST_PROXY === '1') {
|
|
app.set('trust proxy', 1);
|
|
}
|
|
else if (process.env.NODE_ENV === 'production') {
|
|
app.set('trust proxy', 1);
|
|
}
|
|
app.use((0, helmet_1.default)());
|
|
app.use((0, cors_1.default)({ origin: resolveCorsOrigin(), credentials: true }));
|
|
app.use(express_1.default.json());
|
|
app.use(config_1.config.apiPrefix, routes_1.default);
|
|
/**
|
|
* Infers TRON network label from the configured node URL for health reporting.
|
|
* @param url TRON node RPC URL
|
|
* @returns 'shasta', 'mainnet', or 'unknown'
|
|
*/
|
|
function labelTronNetwork(url) {
|
|
const u = (url || '').toLowerCase();
|
|
if (u.includes('shasta'))
|
|
return 'shasta';
|
|
if (u.includes('trongrid.io'))
|
|
return 'mainnet';
|
|
return 'unknown';
|
|
}
|
|
/**
|
|
* Shortens a blockchain address for safe display in health/debug output.
|
|
* @param addr Full address or null/undefined
|
|
* @returns Masked address (first 6 + last 4 chars) or null when empty
|
|
*/
|
|
function maskAddress(addr) {
|
|
if (!addr)
|
|
return null;
|
|
const a = addr.trim();
|
|
if (a.length <= 12)
|
|
return a;
|
|
return `${a.slice(0, 6)}…${a.slice(-4)}`;
|
|
}
|
|
app.get('/health', async (_req, res) => {
|
|
const settings = await settingsRepository.getSettings();
|
|
/** Fee in 6-decimal micro-USDT; clients scale to wallet decimals (e.g. BEP20 18). */
|
|
const feeRaw = (0, withdrawalFee_1.getWithdrawalNetworkFeeRaw)(settings, 6);
|
|
res.json({
|
|
status: 'ok',
|
|
timestamp: new Date().toISOString(),
|
|
env: config_1.config.env,
|
|
emailTransport: config_1.config.email.transport,
|
|
emailConsoleMode: (0, emailService_1.usesConsoleEmailTransport)(),
|
|
/** @deprecated Prefer `assets` — primary prediction ledger remains TRC20. */
|
|
asset: 'USDT_TRC20',
|
|
network: labelTronNetwork(config_1.config.blockchain.tron.nodeUrl),
|
|
assets: ['USDT_TRC20', 'USDT_BEP20'],
|
|
networks: {
|
|
USDT_TRC20: {
|
|
depositEnabled: settings.network_usdt_trc20_deposit_enabled,
|
|
withdrawEnabled: settings.network_usdt_trc20_withdraw_enabled,
|
|
label: labelTronNetwork(config_1.config.blockchain.tron.nodeUrl),
|
|
},
|
|
USDT_BEP20: {
|
|
depositEnabled: settings.network_usdt_bep20_deposit_enabled,
|
|
withdrawEnabled: settings.network_usdt_bep20_withdraw_enabled,
|
|
label: 'bsc',
|
|
},
|
|
},
|
|
withdrawalsEnabled: settings.allow_withdrawals,
|
|
requireWithdrawal2fa: settings.require_withdrawal_2fa,
|
|
withdrawalNetworkFeeRaw: feeRaw.toString(),
|
|
/** Min remaining after peer transfer (6-decimal micro-USDT; 0 = disabled). */
|
|
transferMinRemainingRaw: String(Math.max(0, Math.floor(settings.transfer_min_remaining_usdt_micro || 0))),
|
|
rpc: {
|
|
tronNodeUrl: config_1.config.blockchain.tron.nodeUrl,
|
|
bscRpcUrl: config_1.config.blockchain.bsc.rpcUrl,
|
|
},
|
|
contracts: {
|
|
usdtTrc20: maskAddress(config_1.config.blockchain.tron.usdtTrc20Contract),
|
|
usdtBep20: maskAddress(config_1.config.blockchain.bsc.usdtBep20Contract),
|
|
},
|
|
});
|
|
});
|
|
app.use((_req, res) => {
|
|
res.status(404).json({ error: 'Not found' });
|
|
});
|
|
app.use((err, req, res, next) => {
|
|
(0, errorHandler_1.errorHandler)(err, req, res, next);
|
|
});
|
|
/** Configured Express application instance (not yet listening). */
|
|
exports.default = app;
|
|
//# sourceMappingURL=app.js.map
|