You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
4.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env node
/**
* 链下服务配置助手
* 帮助用户配置 .env 文件
*/
const fs = require('fs')
const path = require('path')
const readline = require('readline')
const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
green: '\x1b[32m',
yellow: '\x1b[33m',
red: '\x1b[31m',
cyan: '\x1b[36m'
}
function log(message, color = 'reset') {
console.log(`${colors[color]}${message}${colors.reset}`)
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
function question(prompt) {
return new Promise((resolve) => {
rl.question(prompt, resolve)
})
}
async function main() {
log('\n' + '='.repeat(60), 'bright')
log(' 链下服务配置助手', 'bright')
log('='.repeat(60) + '\n', 'bright')
const envPath = path.join(__dirname, '.env')
// 检查 .env 文件是否存在
if (!fs.existsSync(envPath)) {
log('❌ 未找到 .env 文件', 'red')
log(' 正在创建 .env 文件...', 'yellow')
} else {
log('✅ 找到 .env 文件', 'green')
const currentContent = fs.readFileSync(envPath, 'utf-8')
if (currentContent.includes('PRIVATE_KEY=请在这里填入') ||
currentContent.match(/PRIVATE_KEY=\s*$/)) {
log('⚠️ 检测到 PRIVATE_KEY 未配置', 'yellow')
} else {
log('✅ PRIVATE_KEY 已配置', 'green')
const shouldContinue = await question('\n是否要重新配置(y/N): ')
if (shouldContinue.toLowerCase() !== 'y') {
log('\n✅ 配置保持不变', 'green')
rl.close()
return
}
}
}
log('\n📋 配置步骤:\n', 'cyan')
log('1. 打开 Ganache', 'bright')
log('2. 选择一个账户(通常是第一个账户,即部署合约的账户)', 'bright')
log('3. 点击账户右侧的钥匙图标,复制私钥', 'bright')
log('4. 确保该账户在 CampusRatingV2 合约中是管理员', 'bright')
log(' 可通过前端管理员页面查看http://127.0.0.1:5173 -> 管理员标签页)\n', 'bright')
// 获取 RPC URL
const rpcUrl = await question('RPC URL (默认: http://127.0.0.1:8545): ') || 'http://127.0.0.1:8545'
// 获取私钥
let privateKey = ''
while (!privateKey || !privateKey.startsWith('0x') || privateKey.length !== 66) {
privateKey = await question('管理员私钥 (0x开头64个字符): ')
if (!privateKey) {
log('❌ 私钥不能为空', 'red')
continue
}
if (!privateKey.startsWith('0x')) {
log('❌ 私钥必须以 0x 开头', 'red')
privateKey = ''
continue
}
if (privateKey.length !== 66) {
log('❌ 私钥长度不正确(应该是 66 个字符,包括 0x', 'red')
privateKey = ''
continue
}
}
// 获取合约地址
const contractAddress = await question('CampusRatingV2 合约地址 (默认: 0xdd9956F9e228bA1fa891105d9AC449f2614bd8A2): ') ||
'0xdd9956F9e228bA1fa891105d9AC449f2614bd8A2'
const mainContractAddress = await question('CampusRating 主合约地址 (默认: 0xd7fa5C79BeFf459dDD95E0F82c54A75a48eb88ae): ') ||
'0xd7fa5C79BeFf459dDD95E0F82c54A75a48eb88ae'
// 获取更新间隔
const updateInterval = await question('更新间隔(秒,默认: 3600: ') || '3600'
// 生成 .env 文件内容
const envContent = `# 链下服务配置文件
# 注意:不要将包含真实私钥的.env文件提交到版本控制
# 区块链RPC地址默认本地Ganache
RPC_URL=${rpcUrl}
# 管理员私钥(需要有管理员权限)
# 格式0x开头的64个字符的十六进制字符串
PRIVATE_KEY=${privateKey}
# CampusRatingV2合约地址
CONTRACT_ADDRESS=${contractAddress}
# CampusRating主合约地址
MAIN_CONTRACT_ADDRESS=${mainContractAddress}
# 更新间隔(秒)
# 排名服务默认3600秒1小时
# 合理性服务默认7200秒2小时
UPDATE_INTERVAL=${updateInterval}
`
// 写入文件
fs.writeFileSync(envPath, envContent, 'utf-8')
log('\n✅ 配置完成!', 'green')
log(`\n配置文件已保存到: ${envPath}`, 'cyan')
log('\n📝 下一步:', 'bright')
log('1. 确保 Ganache 正在运行', 'bright')
log('2. 确保该账户在合约中是管理员', 'bright')
log('3. 运行: cd .. && npm run dev', 'bright')
log(' 或单独运行: npm run dev:all', 'bright')
rl.close()
}
main().catch((error) => {
log(`\n❌ 配置失败: ${error.message}`, 'red')
rl.close()
process.exit(1)
})