"use strict"; /** * Verify that user_wallets rows match PLATFORM_MNEMONIC + derivation_index. * Supports USDT_TRC20 (TRON) and USDT_BEP20 (BSC). * * VPS: * sudo docker compose run --rm --no-deps --build backend \ * node dist/scripts/verifyDepositAddresses.js * * Optional: --wallet-type=USDT_TRC20 | USDT_BEP20 | ALL (default ALL) */ 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; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); require("./loadEnv"); const bip39 = __importStar(require("bip39")); const connection_1 = require("../db/connection"); const hdWallet_1 = require("../services/hdWallet"); const walletRepository = __importStar(require("../repositories/walletRepository")); const walletCapabilities_1 = require("../services/walletCapabilities"); function parseWalletTypes(argv) { const raw = argv.find((a) => a.startsWith('--wallet-type='))?.slice('--wallet-type='.length).trim().toUpperCase(); if (!raw || raw === 'ALL') return [walletCapabilities_1.USDT_TRC20_CODE, walletCapabilities_1.USDT_BEP20_CODE]; if (raw === walletCapabilities_1.USDT_TRC20_CODE || raw === walletCapabilities_1.USDT_BEP20_CODE) return [raw]; throw new Error(`--wallet-type must be ${walletCapabilities_1.USDT_TRC20_CODE}, ${walletCapabilities_1.USDT_BEP20_CODE}, or ALL`); } function deriveAddress(mnemonic, code, index) { if (code === walletCapabilities_1.USDT_BEP20_CODE) return (0, hdWallet_1.deriveEvmFromMnemonic)(mnemonic, index).address; return (0, hdWallet_1.deriveTronFromMnemonic)(mnemonic, index).address; } async function verifyOne(mnemonic, code) { const wt = await walletRepository.getWalletTypeByCode(code); if (!wt) { console.log(`\n[${code}] wallet type not found — skip (run migrations).`); return 0; } const counts = await (0, connection_1.query)(` SELECT 'user_wallets' AS source, COUNT(*)::text AS count FROM user_wallets WHERE wallet_type_id = $1 UNION ALL SELECT 'hot_wallets', COUNT(*)::text FROM hot_wallets WHERE wallet_type_id = $1 UNION ALL SELECT 'users_with_deposit', COUNT(DISTINCT uw.user_id)::text FROM user_wallets uw JOIN users u ON u.id = uw.user_id WHERE uw.wallet_type_id = $1 AND u.deleted_at IS NULL `, [wt.id]); console.log(`\n[${code}] counts:`); for (const row of counts) { console.log(` ${row.source}: ${row.count}`); } const rows = await (0, connection_1.query)(`SELECT uw.user_id, uw.address, uw.derivation_index, u.deleted_at FROM user_wallets uw JOIN users u ON u.id = uw.user_id WHERE uw.wallet_type_id = $1 ORDER BY uw.derivation_index ASC`, [wt.id]); console.log(`Verifying ${rows.length} ${code} user_wallets against PLATFORM_MNEMONIC…`); let ok = 0; let mismatch = 0; for (const row of rows) { const expected = deriveAddress(mnemonic, code, row.derivation_index); const match = code === walletCapabilities_1.USDT_BEP20_CODE ? expected.toLowerCase() === row.address.toLowerCase() : expected === row.address; if (match) { ok += 1; console.log(` OK index=${row.derivation_index} user=${row.user_id}` + `${row.deleted_at ? ' (deleted user)' : ''} ${row.address}`); } else { mismatch += 1; console.log(` FAIL index=${row.derivation_index} user=${row.user_id}` + `${row.deleted_at ? ' (deleted user)' : ''}`); console.log(` db=${row.address}`); console.log(` expected=${expected}`); } } const hot = await (0, connection_1.query)(`SELECT address, derivation_index, is_active FROM hot_wallets WHERE wallet_type_id = $1`, [wt.id]); console.log(`Hot wallets (${hot.length}):`); for (const h of hot) { const idx = h.derivation_index; if (idx == null) { console.log(` ${h.address} active=${h.is_active} (no derivation_index)`); continue; } const expected = deriveAddress(mnemonic, code, idx); const match = code === walletCapabilities_1.USDT_BEP20_CODE ? expected.toLowerCase() === h.address.toLowerCase() : expected === h.address; console.log(` ${match ? 'OK' : 'FAIL'} index=${idx} active=${h.is_active} ${h.address}`); } console.log(`Summary [${code}]: ${ok} match, ${mismatch} mismatch out of ${rows.length}.`); return mismatch; } async function main() { const mnemonic = (process.env.PLATFORM_MNEMONIC || '').trim(); if (!mnemonic || !bip39.validateMnemonic(mnemonic)) { throw new Error('PLATFORM_MNEMONIC missing or invalid BIP39 phrase.'); } const codes = parseWalletTypes(process.argv.slice(2)); let totalMismatch = 0; for (const code of codes) { totalMismatch += await verifyOne(mnemonic, code); } if (totalMismatch > 0) { throw new Error('Some deposit addresses do NOT match the current PLATFORM_MNEMONIC. ' + 'Do not send real funds to FAIL rows; run rotateDepositAddresses for those users.'); } } main() .then(() => process.exit(0)) .catch((e) => { console.error(e instanceof Error ? e.message : e); process.exit(1); }); //# sourceMappingURL=verifyDepositAddresses.js.map