games_app/backend/dist/safety/networkGuard.js

88 lines
4.4 KiB
JavaScript

"use strict";
/**
* Blockchain network safety guards.
* - Non-production: blocks accidental mainnet usage when testnet is expected.
* - Production: blocks testnet endpoints/contracts so fake test USDT cannot be credited.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.MAINNET_USDT_BEP20_CONTRACT = exports.SHASTA_USDT_TRC20_CONTRACT = exports.MAINNET_USDT_TRC20_CONTRACT = void 0;
exports.assertProductionMainnetConfig = assertProductionMainnetConfig;
exports.validateTestnetSafetyOrThrow = validateTestnetSafetyOrThrow;
exports.validateProductionMainnetOrThrow = validateProductionMainnetOrThrow;
const config_1 = require("../config");
/** Official mainnet USDT TRC20 (Tether). */
exports.MAINNET_USDT_TRC20_CONTRACT = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t';
/** Shasta testnet USDT TRC20 — must never be used in production. */
exports.SHASTA_USDT_TRC20_CONTRACT = 'TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs';
/** Official mainnet USDT on BSC (BEP20). */
exports.MAINNET_USDT_BEP20_CONTRACT = '0x55d398326f99059fF775485246999027B3197955';
function assert(condition, message) {
if (!condition)
throw new Error(message);
}
function looksLikeShasta(url) {
return /shasta/i.test(url);
}
function looksLikeTronTestnet(url) {
return /shasta|nile/i.test(url);
}
function isShastaUsdtContract(contract) {
return contract.trim().toLowerCase() === exports.SHASTA_USDT_TRC20_CONTRACT.toLowerCase();
}
function looksLikeBscTestnet(url) {
return /testnet|chapel|amoy/i.test(url);
}
/**
* Validates production blockchain settings (testable with explicit config).
* @throws When production would use testnet TRON/BSC or Shasta USDT contract
*/
function assertProductionMainnetConfig(cfg) {
if (cfg.env !== 'production')
return;
assert(cfg.networkMode !== 'testnet', 'Safety check failed: NETWORK_MODE=testnet is not allowed when NODE_ENV=production.');
assert(!looksLikeTronTestnet(cfg.tronNodeUrl), `Safety check failed: TRON_NODE_URL must not point to a TRON testnet in production. Got: ${cfg.tronNodeUrl}`);
const contractOverride = cfg.usdtTrc20Contract?.trim() || '';
if (contractOverride) {
assert(!isShastaUsdtContract(contractOverride), `Safety check failed: USDT_TRC20_CONTRACT must not be the Shasta testnet token in production. ` +
`Use mainnet USDT (${exports.MAINNET_USDT_TRC20_CONTRACT}) or unset USDT_TRC20_CONTRACT to use the DB default.`);
}
const bscUrl = (cfg.bscRpcUrl || '').trim();
if (bscUrl) {
assert(!looksLikeBscTestnet(bscUrl), `Safety check failed: BSC_RPC_URL must not point to a BSC testnet in production. Got: ${bscUrl}`);
}
}
/**
* Validates that non-production runs use testnet endpoints and contracts.
*
* For the expected testnet setup:
* - TRON: Shasta (TRON_NODE_URL should include "shasta")
* - Asset: USDT TRC20 on Shasta
*
* Skipped in production or when NETWORK_MODE=mainnet.
* @throws When a configured URL or contract points at mainnet while testnet is required
*/
function validateTestnetSafetyOrThrow() {
const mode = (process.env.NETWORK_MODE || '').toLowerCase();
const isProd = config_1.config.env === 'production';
const requireTestnet = !isProd && mode !== 'mainnet';
if (!requireTestnet)
return;
assert(looksLikeShasta(config_1.config.blockchain.tron.nodeUrl), `Safety check failed: TRON_NODE_URL must point to Shasta (expected "shasta" in URL). Got: ${config_1.config.blockchain.tron.nodeUrl}`);
assert(!!config_1.config.blockchain.tron.usdtTrc20Contract && config_1.config.blockchain.tron.usdtTrc20Contract.trim().length > 0, 'Safety check failed: USDT_TRC20_CONTRACT must be set for Shasta (testnet) so we do not use the mainnet default from DB.');
}
/**
* Validates that production runs use mainnet TRON/BSC and never the Shasta USDT contract.
* Skipped when NODE_ENV is not production.
* @throws When TRON_NODE_URL, BSC_RPC_URL, or USDT_TRC20_CONTRACT targets testnet
*/
function validateProductionMainnetOrThrow() {
assertProductionMainnetConfig({
env: config_1.config.env,
networkMode: (process.env.NETWORK_MODE || '').toLowerCase(),
tronNodeUrl: config_1.config.blockchain.tron.nodeUrl,
usdtTrc20Contract: config_1.config.blockchain.tron.usdtTrc20Contract,
bscRpcUrl: config_1.config.blockchain.bsc.rpcUrl,
usdtBep20Contract: config_1.config.blockchain.bsc.usdtBep20Contract,
});
}
//# sourceMappingURL=networkGuard.js.map