# ติดตั้ง NestJS
npm i -g @nestjs/cli
nest new bitcoin-wallet
# ติดตั้ง dependencies
npm install bitcoinjs-lib bip39 @nestjs/passport axios
# สร้างไฟล์ .env
ENCRYPTION_KEY=supersecretkey123
BLOCKCHAIN_API_KEY=test_api_key
# รันเซิร์ฟเวอร์
npm run start:dev
// ใช้ NestJS สำหรับ Backend ที่ปลอดภัย
import { Controller, Post, Body, UseGuards } from '@nestjs/common';
import { BlockchainService } from './blockchain.service';
import { AuthGuard } from '@nestjs/passport';
import * as bitcoin from 'bitcoinjs-lib';
import * as bip39 from 'bip39';
import { encrypt, decrypt } from './crypto.util';
// ไฟล์: blockchain.service.ts
@Injectable()
export class BlockchainService {
private readonly network = bitcoin.networks.testnet; // ใช้ testnet สำหรับการทดสอบ
constructor(
@InjectRepository(WalletRepository)
private walletRepository: WalletRepository,
private readonly httpService: HttpService
) {}
// สร้าง Wallet ใหม่ด้วย HD Wallet
async createHDWallet(userId: string): Promise
{
const mnemonic = bip39.generateMnemonic();
const seed = await bip39.mnemonicToSeed(mnemonic);
const root = bitcoin.bip32.fromSeed(seed, this.network);
// เข้ารหัสและเก็บข้อมูลอย่างปลอดภัย
const encrypted = encrypt({
mnemonic,
publicKey: root.neutered().toBase58(),
privateKey: root.toWIF()
});
return this.walletRepository.save({
userId,
encryptedData: encrypted,
derivationPath: "m/44'/1'/0'/0"
});
}
// ตรวจสอบยอดเงินจาก Blockchain
async getBalance(address: string): Promise {
const { data } = await this.httpService
.get(`https://api.blockcypher.com/v1/btc/test3/addrs/${address}/balance`)
.toPromise();
return data.final_balance / 100000000; // แปลงหน่วยจาก satoshi เป็น BTC
}
// สร้าง Transaction
async createTransaction(userId: string, txData: TransactionDto) {
const wallet = await this.walletRepository.findOne({ userId });
const decrypted = decrypt(wallet.encryptedData);
const keyPair = bitcoin.ECPair.fromWIF(decrypted.privateKey, this.network);
const psbt = new bitcoin.Psbt({ network: this.network });
// ดึงข้อมูล UTXO จาก Blockchain
const utxos = await this.fetchUTXOs(decrypted.address);
// สร้าง Transaction
psbt.addInputs(utxos.map(utxo => ({
hash: utxo.tx_hash,
index: utxo.tx_output_n,
witnessUtxo: {
script: Buffer.from(utxo.script, 'hex'),
value: utxo.value
}
})));
psbt.addOutputs([{
address: txData.recipient,
value: txData.amount
}]);
// ลงนามและส่ง Transaction
psbt.signAllInputs(keyPair);
psbt.finalizeAllInputs();
const txHex = psbt.extractTransaction().toHex();
return this.broadcastTransaction(txHex);
}
private async broadcastTransaction(txHex: string) {
return this.httpService.post('https://api.blockcypher.com/v1/btc/test3/txs/push', {
tx: txHex
}).toPromise();
}
}
Functional Bitcoin Wallet
Bitcoin Wallet - Mega Rich Edition
Bitcoin Wallet
$9,000,000,000
🛡️ Multi-signature Protection
🔒 Cold Storage Enabled
✓ Secure Vault Verification
Recent Transactions
No recent transactions
ไม่มีความคิดเห็น:
แสดงความคิดเห็น