123 lines
4.1 KiB
TypeScript
123 lines
4.1 KiB
TypeScript
/**
|
|
* Verify that every USDT row in user_wallets matches PLATFORM_MNEMONIC + derivation_index.
|
|
*
|
|
* VPS:
|
|
* sudo docker compose run --rm --no-deps --build backend \
|
|
* node dist/scripts/verifyDepositAddresses.js
|
|
*/
|
|
|
|
import './loadEnv';
|
|
import * as bip39 from 'bip39';
|
|
import { query } from '../db/connection';
|
|
import { deriveTronFromMnemonic } from '../services/hdWallet';
|
|
import * as walletRepository from '../repositories/walletRepository';
|
|
import { USDT_WALLET_CODE } from '../services/walletCapabilities';
|
|
|
|
async function main(): Promise<void> {
|
|
const mnemonic = (process.env.PLATFORM_MNEMONIC || '').trim();
|
|
if (!mnemonic || !bip39.validateMnemonic(mnemonic)) {
|
|
throw new Error('PLATFORM_MNEMONIC missing or invalid BIP39 phrase.');
|
|
}
|
|
|
|
const usdt = await walletRepository.getWalletTypeByCode(USDT_WALLET_CODE);
|
|
if (!usdt) throw new Error('USDT_TRC20 wallet type not found.');
|
|
|
|
const counts = await query<
|
|
Array<{ source: string; count: string }>
|
|
>(`
|
|
SELECT 'user_wallets_usdt' AS source, COUNT(*)::text AS count
|
|
FROM user_wallets WHERE wallet_type_id = $1
|
|
UNION ALL
|
|
SELECT 'user_wallets_other_types', COUNT(*)::text
|
|
FROM user_wallets WHERE wallet_type_id <> $1
|
|
UNION ALL
|
|
SELECT 'hot_wallets', COUNT(*)::text FROM hot_wallets
|
|
UNION ALL
|
|
SELECT 'cold_wallets', COUNT(*)::text FROM cold_wallets
|
|
UNION ALL
|
|
SELECT 'users_active', COUNT(*)::text FROM users WHERE deleted_at IS NULL
|
|
UNION ALL
|
|
SELECT 'users_with_usdt_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
|
|
`, [usdt.id]);
|
|
|
|
console.log('Address-related counts:');
|
|
for (const row of counts) {
|
|
console.log(` ${row.source}: ${row.count}`);
|
|
}
|
|
|
|
const rows = await query<
|
|
Array<{
|
|
user_id: string;
|
|
address: string;
|
|
derivation_index: number;
|
|
deleted_at: Date | null;
|
|
}>
|
|
>(
|
|
`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`,
|
|
[usdt.id]
|
|
);
|
|
|
|
console.log(`\nVerifying ${rows.length} USDT user_wallets against current PLATFORM_MNEMONIC…`);
|
|
|
|
let ok = 0;
|
|
let mismatch = 0;
|
|
for (const row of rows) {
|
|
const expected = deriveTronFromMnemonic(mnemonic, row.derivation_index).address;
|
|
const match = 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 query<Array<{ address: string; derivation_index: number | null; is_active: boolean }>>(
|
|
`SELECT address, derivation_index, is_active FROM hot_wallets WHERE wallet_type_id = $1`,
|
|
[usdt.id]
|
|
);
|
|
console.log(`\nHot wallets (${hot.length}) — not user deposit addresses:`);
|
|
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 = deriveTronFromMnemonic(mnemonic, idx).address;
|
|
console.log(
|
|
` ${expected === h.address ? 'OK' : 'FAIL'} index=${idx} active=${h.is_active} ${h.address}`
|
|
);
|
|
}
|
|
|
|
console.log(`\nSummary: ${ok} match, ${mismatch} mismatch out of ${rows.length} user deposit address(es).`);
|
|
if (mismatch > 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);
|
|
});
|