Files
telegram-shop/src/utils/walletGenerator.js
NW ce1b6003cb feat(logging): replace 207 console.log/error/warn with pino structured logger (#58)
- Add pino + pino-pretty dependencies
- Create src/utils/logger.js with env-based LOG_LEVEL
- Replace all 207 console.log/error/warn calls across 46 source files
- Remove [DEBUG], [ERROR] string prefixes (levels convey this)
- Add pino redact for sensitive fields (mnemonic, privateKey, token, etc.)
- Structured logging with context objects instead of string interpolation
- NODE_ENV=production disables pino-pretty transport

49 files changed, 5601 insertions, 6056 deletions
2026-06-22 01:42:47 +01:00

95 lines
2.9 KiB
JavaScript

// walletGenerator.js
import bip39 from 'bip39';
import HDKey from 'hdkey';
import { publicToAddress } from 'ethereumjs-util';
import * as bitcoin from 'bitcoinjs-lib';
import * as ecc from 'tiny-secp256k1';
import { ECPairFactory } from 'ecpair';
import CryptoJS from 'crypto';
import logger from './logger.js';
const ECPair = ECPairFactory(ecc);
export default class WalletGenerator {
static async generateMnemonic() {
try {
return bip39.generateMnemonic(128); // 12 слов
} catch (error) {
logger.error({ err: error }, 'Error generating mnemonic');
throw new Error('Failed to generate mnemonic');
}
}
static async generateWallets(mnemonic) {
try {
const seed = await bip39.mnemonicToSeed(mnemonic);
const hdkey = HDKey.fromMasterSeed(Buffer.from(seed));
// Generate BTC wallet (BIP84 - Native SegWit)
const btcNode = hdkey.derive("m/84'/0'/0'/0/0");
const btcKeyPair = ECPair.fromPrivateKey(btcNode.privateKey);
const btcAddress = bitcoin.payments.p2wpkh({
pubkey: btcKeyPair.publicKey,
}).address;
// Generate ETH wallet (BIP44)
const ethNode = hdkey.derive("m/44'/60'/0'/0/0");
const ethAddress = '0x' + publicToAddress(ethNode.publicKey, true).toString('hex');
// Generate USDT wallet (BIP44, same as ETH but different index)
const usdtNode = hdkey.derive("m/44'/60'/0'/0/1");
const usdtAddress = '0x' + publicToAddress(usdtNode.publicKey, true).toString('hex');
// Generate USDC wallet (BIP44, same as ETH but different index)
const usdcNode = hdkey.derive("m/44'/60'/0'/0/2");
const usdcAddress = '0x' + publicToAddress(usdcNode.publicKey, true).toString('hex');
// Generate LTC wallet (BIP84 - Native SegWit)
const ltcNode = hdkey.derive("m/84'/2'/0'/0/0");
const ltcKeyPair = ECPair.fromPrivateKey(ltcNode.privateKey);
const ltcAddress = bitcoin.payments.p2wpkh({
pubkey: ltcKeyPair.publicKey,
network: {
messagePrefix: '\x19Litecoin Signed Message:\n',
bech32: 'ltc',
bip32: {
public: 0x019da462,
private: 0x019d9cfe,
},
pubKeyHash: 0x30,
scriptHash: 0x32,
wif: 0xb0,
},
}).address;
return {
BTC: {
address: btcAddress,
path: "m/84'/0'/0'/0/0",
},
ETH: {
address: ethAddress,
path: "m/44'/60'/0'/0/0",
},
USDT: {
address: usdtAddress,
path: "m/44'/60'/0'/0/1",
},
USDC: {
address: usdcAddress,
path: "m/44'/60'/0'/0/2",
},
LTC: {
address: ltcAddress,
path: "m/84'/2'/0'/0/0",
},
};
} catch (error) {
logger.error({ err: error }, 'Error in generateWallets');
throw new Error('Failed to generate cryptocurrency wallets: ' + error.message);
}
}
}