121 lines
5.3 KiB
JavaScript
121 lines
5.3 KiB
JavaScript
"use strict";
|
|
/**
|
|
* User wallet and deposit address service.
|
|
* Derives TRON deposit addresses for USDT (TRC20) only.
|
|
*/
|
|
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 });
|
|
exports.getOrCreateDepositAddress = getOrCreateDepositAddress;
|
|
exports.getWalletTypes = getWalletTypes;
|
|
exports.getUserBalances = getUserBalances;
|
|
exports.listUserTransactions = listUserTransactions;
|
|
exports.listUserTransactionsPaginated = listUserTransactionsPaginated;
|
|
const bip39 = __importStar(require("bip39"));
|
|
const hdWallet_1 = require("./hdWallet");
|
|
const connection_1 = require("../db/connection");
|
|
const walletRepository = __importStar(require("../repositories/walletRepository"));
|
|
const transactionRepository = __importStar(require("../repositories/transactionRepository"));
|
|
const walletCapabilities_1 = require("./walletCapabilities");
|
|
const PLATFORM_MNEMONIC = process.env.PLATFORM_MNEMONIC || '';
|
|
function getMnemonic() {
|
|
const raw = PLATFORM_MNEMONIC.trim();
|
|
if (!raw || raw.split(/\s+/).length < 12) {
|
|
throw new Error('PLATFORM_MNEMONIC is not set or too short. Set a valid BIP39 mnemonic (12 or 24 words) in .env.');
|
|
}
|
|
if (!bip39.validateMnemonic(raw)) {
|
|
throw new Error('PLATFORM_MNEMONIC is not a valid BIP39 mnemonic.');
|
|
}
|
|
return raw;
|
|
}
|
|
/**
|
|
* Gets or creates a USDT TRC20 deposit address for a user.
|
|
*/
|
|
async function getOrCreateDepositAddress(userId, walletTypeCode) {
|
|
(0, walletCapabilities_1.assertUsdtWalletType)(walletTypeCode);
|
|
const walletType = await walletRepository.getWalletTypeByCode(walletCapabilities_1.USDT_WALLET_CODE);
|
|
if (!walletType)
|
|
throw new Error('USDT wallet type is not configured');
|
|
const existing = await walletRepository.getUserWallet(userId, walletType.id);
|
|
if (existing) {
|
|
return { address: existing.address, walletType: (0, walletCapabilities_1.enrichWalletType)(walletType) };
|
|
}
|
|
const maxIndex = await getNextDerivationIndex(walletType.id);
|
|
const derived = (0, hdWallet_1.deriveTronFromMnemonic)(getMnemonic(), maxIndex);
|
|
await walletRepository.createUserWallet({
|
|
userId,
|
|
walletTypeId: walletType.id,
|
|
address: derived.address,
|
|
derivationIndex: maxIndex,
|
|
});
|
|
return { address: derived.address, walletType: (0, walletCapabilities_1.enrichWalletType)(walletType) };
|
|
}
|
|
async function getNextDerivationIndex(walletTypeId) {
|
|
const rows = await (0, connection_1.query)('SELECT MAX(derivation_index) as max FROM user_wallets WHERE wallet_type_id = $1', [walletTypeId]);
|
|
const max = rows[0]?.max ?? -1;
|
|
return max + 1;
|
|
}
|
|
async function getWalletTypes() {
|
|
const types = await walletRepository.getWalletTypes();
|
|
return types.map(walletCapabilities_1.enrichWalletType);
|
|
}
|
|
async function getUserBalances(userId) {
|
|
const types = await walletRepository.getWalletTypes();
|
|
const { getOrCreateAccount, getAccount } = await Promise.resolve().then(() => __importStar(require('../repositories/ledgerRepository')));
|
|
const result = [];
|
|
for (const wt of types) {
|
|
const account = await getOrCreateAccount(userId, wt.id, 'main');
|
|
const bonus = await getAccount(userId, wt.id, 'bonus');
|
|
const test = await getAccount(userId, wt.id, 'test');
|
|
result.push({
|
|
walletType: (0, walletCapabilities_1.enrichWalletType)(wt),
|
|
available: account.available_balance,
|
|
locked: account.locked_balance,
|
|
bonusAvailable: bonus?.available_balance ?? '0',
|
|
testAvailable: test?.available_balance ?? '0',
|
|
});
|
|
}
|
|
return result;
|
|
}
|
|
async function listUserTransactions(userId, limit, offset) {
|
|
return transactionRepository.listByUserId(userId, limit, offset);
|
|
}
|
|
async function listUserTransactionsPaginated(userId, limit, offset) {
|
|
const [items, total] = await Promise.all([
|
|
transactionRepository.listByUserId(userId, limit, offset),
|
|
transactionRepository.countByUserId(userId),
|
|
]);
|
|
return { items, total, limit, offset };
|
|
}
|
|
//# sourceMappingURL=walletService.js.map
|