|
@@ -1,139 +0,0 @@
|
|
|
-import { Not, Repository } from 'typeorm'
|
|
|
|
|
-import { Wallet } from '../entities/wallet.entity'
|
|
|
|
|
-import { EthWallet } from '../entities/eth-wallet.entity'
|
|
|
|
|
-import { TronWallet } from '../entities/tron-wallet.entity'
|
|
|
|
|
-import { BtcWallet } from '../entities/btc-wallet.entity'
|
|
|
|
|
-import { Web3 } from 'web3'
|
|
|
|
|
-import { ListWalletQuery, ReplaceWalletBody, ListReplacementQuery } from '../dto/wallet.dto'
|
|
|
|
|
-import { PaginationResponse } from '../dto/common.dto'
|
|
|
|
|
-import { FastifyInstance } from 'fastify'
|
|
|
|
|
-import { Replacement } from '../entities/replacement.entity'
|
|
|
|
|
-import { Chain } from '../misc/chain.enum'
|
|
|
|
|
-
|
|
|
|
|
-export class WalletService {
|
|
|
|
|
- private tronWalletRepo: Repository<TronWallet>
|
|
|
|
|
- private ethWalletRepo: Repository<EthWallet>
|
|
|
|
|
- private btcWalletRepo: Repository<BtcWallet>
|
|
|
|
|
- private replacementRepository: Repository<Replacement>
|
|
|
|
|
-
|
|
|
|
|
- constructor(app: FastifyInstance) {
|
|
|
|
|
- this.tronWalletRepo = app.dataSource.getRepository(TronWallet)
|
|
|
|
|
- this.ethWalletRepo = app.dataSource.getRepository(EthWallet)
|
|
|
|
|
- this.btcWalletRepo = app.dataSource.getRepository(BtcWallet)
|
|
|
|
|
- this.replacementRepository = app.dataSource.getRepository(Replacement)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- async list(page: number, size: number): Promise<PaginationResponse<Wallet>> {
|
|
|
|
|
- const [wallets, total] = await this.tronWalletRepo.findAndCount({
|
|
|
|
|
- skip: (Number(page) || 0) * (Number(size) || 20),
|
|
|
|
|
- take: Number(size) || 20,
|
|
|
|
|
- order: {
|
|
|
|
|
- id: 'DESC'
|
|
|
|
|
- }
|
|
|
|
|
- })
|
|
|
|
|
- return {
|
|
|
|
|
- content: wallets,
|
|
|
|
|
- metadata: {
|
|
|
|
|
- total: Number(total),
|
|
|
|
|
- page: Number(page),
|
|
|
|
|
- size: Number(size)
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- async findOne(id: number): Promise<Wallet | null> {
|
|
|
|
|
- return this.tronWalletRepo.findOne({
|
|
|
|
|
- where: { id }
|
|
|
|
|
- })
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- async update(id: number, data: Partial<Wallet>): Promise<Wallet | null> {
|
|
|
|
|
- await this.tronWalletRepo.update({ id }, data)
|
|
|
|
|
- return this.findOne(id)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- async delete(id: number): Promise<boolean> {
|
|
|
|
|
- const result = await this.tronWalletRepo.delete({
|
|
|
|
|
- id
|
|
|
|
|
- })
|
|
|
|
|
- return result.affected ? true : false
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- async replace(address: string): Promise<Replacement> {
|
|
|
|
|
- if (!address) {
|
|
|
|
|
- throw new Error('Address is required')
|
|
|
|
|
- }
|
|
|
|
|
- let chain: Chain
|
|
|
|
|
- // BTC地址格式:
|
|
|
|
|
- // 1. Legacy: 1开头, 26-35位
|
|
|
|
|
- // 2. SegWit: 3开头, 26-35位
|
|
|
|
|
- // 3. Native SegWit: bc1开头, 42位
|
|
|
|
|
- // 4. Taproot: bc1p开头, 62位
|
|
|
|
|
- if (
|
|
|
|
|
- /^(1[a-km-zA-HJ-NP-Z1-9]{25,34}|3[a-km-zA-HJ-NP-Z1-9]{25,34}|bc1[a-zA-HJ-NP-Z0-9]{39}|bc1p[a-zA-HJ-NP-Z0-9]{58})$/.test(
|
|
|
|
|
- address
|
|
|
|
|
- )
|
|
|
|
|
- ) {
|
|
|
|
|
- chain = Chain.BTC
|
|
|
|
|
- } else if (/^(0x)?[0-9a-fA-F]{40}$/.test(address)) {
|
|
|
|
|
- chain = Chain.ETH
|
|
|
|
|
- } else if (/^T[A-Za-z1-9]{33}$/.test(address)) {
|
|
|
|
|
- chain = Chain.TRON
|
|
|
|
|
- } else {
|
|
|
|
|
- throw new Error('Invalid address format')
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- let replacement = await this.replacementRepository.findOneBy({
|
|
|
|
|
- address
|
|
|
|
|
- })
|
|
|
|
|
-
|
|
|
|
|
- if (!replacement) {
|
|
|
|
|
- if (chain === Chain.BTC) {
|
|
|
|
|
- const replaceWallet = await this.btcWalletRepo.findOneOrFail({ where: {} })
|
|
|
|
|
- replacement = this.replacementRepository.create({
|
|
|
|
|
- address,
|
|
|
|
|
- replaceAddress: replaceWallet.address,
|
|
|
|
|
- walletId: replaceWallet.id,
|
|
|
|
|
- chain
|
|
|
|
|
- })
|
|
|
|
|
- } else if (chain === Chain.ETH) {
|
|
|
|
|
- const replaceWallet = await this.ethWalletRepo.findOneOrFail({ where: {} })
|
|
|
|
|
- replacement = this.replacementRepository.create({
|
|
|
|
|
- address,
|
|
|
|
|
- replaceAddress: replaceWallet.address,
|
|
|
|
|
- walletId: replaceWallet.id,
|
|
|
|
|
- chain
|
|
|
|
|
- })
|
|
|
|
|
- } else if (chain === Chain.TRON) {
|
|
|
|
|
- const replaceWallet = await this.tronWalletRepo.findOneOrFail({ where: {} })
|
|
|
|
|
- replacement = this.replacementRepository.create({
|
|
|
|
|
- address,
|
|
|
|
|
- replaceAddress: replaceWallet.address,
|
|
|
|
|
- walletId: replaceWallet.id,
|
|
|
|
|
- chain
|
|
|
|
|
- })
|
|
|
|
|
- }
|
|
|
|
|
- await this.replacementRepository.save(replacement!)
|
|
|
|
|
- }
|
|
|
|
|
- return replacement!
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- async listReplacement(body: ListReplacementQuery): Promise<PaginationResponse<Replacement>> {
|
|
|
|
|
- const { page, size } = body
|
|
|
|
|
- const [replacements, total] = await this.replacementRepository.findAndCount({
|
|
|
|
|
- skip: (Number(page) || 0) * (Number(size) || 20),
|
|
|
|
|
- take: Number(size) || 20,
|
|
|
|
|
- order: {
|
|
|
|
|
- id: 'DESC'
|
|
|
|
|
- }
|
|
|
|
|
- })
|
|
|
|
|
- return {
|
|
|
|
|
- content: replacements,
|
|
|
|
|
- metadata: {
|
|
|
|
|
- total: Number(total),
|
|
|
|
|
- page: Number(page),
|
|
|
|
|
- size: Number(size)
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|