-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathconfig.js
More file actions
670 lines (634 loc) · 23.3 KB
/
config.js
File metadata and controls
670 lines (634 loc) · 23.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
const path = require('path');
const Datastore = require('nedb');
const fs = require('fs'); // 加入文件系统模块
let IS_BUILD = true;
if (process.env["IS_BUILD"]?.toLowerCase() === "false") {
IS_BUILD = false;
}
class Config {
static instance = null;
#walletDb;
#taskDb;
#fingerPrintDb;
// 内存维护所有路径
_paths = {};
constructor() {
if (!Config.instance) {
Config.instance = this;
this.isBuild = IS_BUILD;
this.assetsPath = this.isBuild ? path.resolve(__dirname, '../assets') : path.resolve(__dirname, './assets');
this.defaultScriptPath = path.join(this.assetsPath, '/scripts');
this.defaultAgentPath = path.join(this.assetsPath, '/agents');
if (process.platform === "win32") {
this.platform = "win32";
this.defaultExecPath = this.isBuild
? path.join(this.assetsPath, '/node_for_win/node-v22.22.0-win/node.exe')
: process.execPath;
} else if (process.platform === "darwin") {
this.platform = "darwin";
this.defaultExecPath = this.isBuild
? path.join(this.assetsPath, '/node_for_mac/node-v21.6.2-mac/bin/node')
: process.execPath;
} else {
console.log("当前平台不是 Windows 也不是 macOS");
}
this.ip2LocationDbPath = path.join(this.assetsPath, '/ip2location/IP2LOCATION-LITE-DB11.BIN');
this.initWalletScriptPath = '';
this.openWalletScriptPath = '';
// 初始化时加载所有路径到内存
this._loadAllPathsFromJson();
// Apply default savePath if not configured
if (!this._paths.path) {
this._applyDefaultSavePath();
}
let cacheInfo = this.getSavePath();
if (!cacheInfo.path) {
console.error('[Config] savePath still empty after default — DB not initialized');
return;
}
this.#walletDb = new Datastore({
filename: path.join(cacheInfo.path, "db/walletData.db"),
autoload: true,
});
this.#taskDb = new Datastore({
filename: path.join(cacheInfo.path, "/db/task.db"),
autoload: true,
});
this.#fingerPrintDb = new Datastore({
filename: path.join(cacheInfo.path, "/db/fingerPrint.db"),
autoload: true,
});
}
return Config.instance;
}
// Set default savePath when none configured
_applyDefaultSavePath() {
const home = process.env.USERPROFILE || process.env.HOME || require('os').homedir();
const defaultPath = path.join(home, 'Documents', 'Web3ToolBox');
try {
fs.mkdirSync(defaultPath, { recursive: true });
fs.mkdirSync(path.join(defaultPath, 'db'), { recursive: true });
this._paths.path = defaultPath;
this._saveAllPathsToJson();
console.log(`[Config] Default savePath set: ${defaultPath}`);
} catch (e) {
console.error(`[Config] Failed to create default savePath: ${e.message}`);
}
}
// 内存优先加载所有路径
getConfigDir() {
const userDataDir = process.env.APP_USER_DATA;
if (userDataDir) {
return userDataDir;
}
return this.getAssetsPath();
}
_loadAllPathsFromJson() {
const configDir = this.getConfigDir();
const savePathFile = path.join(configDir, "savePath.json");
const legacySavePathFile = path.join(this.getAssetsPath(), "savePath.json");
if (!fs.existsSync(configDir)) {
try {
fs.mkdirSync(configDir, { recursive: true });
} catch (e) {
// fallback to assets path if userData dir creation fails
}
}
if (fs.existsSync(savePathFile)) {
try {
this._paths = JSON.parse(fs.readFileSync(savePathFile));
return;
} catch (e) {
this._paths = {};
}
}
if (fs.existsSync(legacySavePathFile)) {
try {
this._paths = JSON.parse(fs.readFileSync(legacySavePathFile));
// migrate legacy config to userData dir
try {
fs.writeFileSync(savePathFile, JSON.stringify(this._paths));
} catch (e) {
// ignore migration failures
}
} catch (e) {
this._paths = {};
}
}
}
_saveAllPathsToJson() {
const configDir = this.getConfigDir();
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true });
}
const savePathFile = path.join(configDir, "savePath.json");
fs.writeFileSync(savePathFile, JSON.stringify(this._paths));
}
static getInstance() {
if (!Config.instance) {
console.log('new Config');
Config.instance = new Config();
}
return Config.instance;
}
getDefaultScriptPath(relativePath) {
return path.join(this.defaultScriptPath, relativePath);
}
getIp2LocationDbPath() {
return this.ip2LocationDbPath;
}
getIsBuild() {
return this.isBuild;
}
getPlatform() {
return this.platform;
}
getAssetsPath() {
return this.assetsPath;
}
getDefaultExecPath() {
return this.defaultExecPath;
}
getWalletDb() {
return this.#walletDb;
}
getTaskDb() {
return this.#taskDb;
}
getFingerPrintDb() {
return this.#fingerPrintDb;
}
loadDefaultTask() {
const defaultTaskConfig = require(path.join(this.assetsPath, "/defaultTaskConfig.json"));
//根据taskName加载默认任务,如果不存在则插入
const taskNames = defaultTaskConfig.map((el) => el.taskName);
taskNames.forEach((taskName) => {
this.#taskDb.findOne({ taskName: taskName }, (err, doc) => {
if (err) {
console.error("加载默认任务时出错:", err);
return;
}
let taskObj = defaultTaskConfig.find((el) => el.taskName === taskName);
if (taskObj.scriptPath && !path.isAbsolute(taskObj.scriptPath)) {
taskObj.scriptPath = path.join(this.defaultScriptPath, taskObj.scriptPath);
}
//如果不存在则插入,存在替换
if (!doc) {
this.#taskDb.insert(taskObj);
} else {
this.#taskDb.update({ taskName: taskName }, taskObj);
}
});
});
// 扫描 assets/agents/*/taskConfig.json,自动加载每个 agent 自带的任务配置
this._loadAgentTasks();
}
_loadAgentTasks() {
try {
if (!fs.existsSync(this.defaultAgentPath)) return;
const dirs = fs.readdirSync(this.defaultAgentPath, { withFileTypes: true })
.filter((d) => d.isDirectory())
.map((d) => d.name);
dirs.forEach((dirName) => {
const agentDir = path.join(this.defaultAgentPath, dirName);
const configFile = path.join(agentDir, 'taskConfig.json');
if (!fs.existsSync(configFile)) return;
let taskArr;
try {
taskArr = JSON.parse(fs.readFileSync(configFile, 'utf-8'));
} catch (e) {
console.error(`[_loadAgentTasks] ${configFile} 解析失败:`, e.message);
return;
}
if (!Array.isArray(taskArr)) taskArr = [taskArr];
taskArr.forEach((taskObj) => {
if (!taskObj || !taskObj.taskName) return;
// scriptPath 相对于 agent 自身目录解析
if (taskObj.scriptPath && !path.isAbsolute(taskObj.scriptPath)) {
taskObj.scriptPath = path.join(agentDir, taskObj.scriptPath);
}
if (typeof taskObj.defaultTask === 'undefined') {
taskObj.defaultTask = true;
}
this.#taskDb.findOne({ taskName: taskObj.taskName }, (err, doc) => {
if (err) {
console.error(`[_loadAgentTasks] 查询 ${taskObj.taskName} 失败:`, err);
return;
}
if (!doc) {
this.#taskDb.insert(taskObj);
} else {
this.#taskDb.update({ taskName: taskObj.taskName }, taskObj);
}
});
});
});
} catch (e) {
console.error('[_loadAgentTasks] 扫描 agent 目录失败:', e.message);
}
}
// 从指定目录加载并 upsert 任务(taskConfig.json)
async _loadTasksFromDirectory(directory) {
try {
const taskConfigPath = path.join(directory, 'taskConfig.json');
if (!fs.existsSync(taskConfigPath)) {
return { success: false, message: '未检测到 taskConfig.json,跳过任务加载' };
}
const raw = fs.readFileSync(taskConfigPath, 'utf-8');
let taskArr = [];
try {
taskArr = JSON.parse(raw);
} catch (e) {
return { success: false, message: 'taskConfig.json 解析失败: ' + e.message };
}
if (!Array.isArray(taskArr)) {
return { success: false, message: 'taskConfig.json 格式错误,应为数组' };
}
if (!this.#taskDb) {
// 可能尚未设置 savePath,先不报错,后续可再次设置或调用
return { success: true, message: '任务数据库未初始化,已跳过任务导入' };
}
const normalizeTask = (task) => {
const cloned = { ...task };
if (cloned.scriptPath && !path.isAbsolute(cloned.scriptPath)) {
cloned.scriptPath = path.join(directory, cloned.scriptPath);
}
if (!cloned.configSchema && cloned.taskSchema) {
cloned.configSchema = cloned.taskSchema;
}
if (typeof cloned.defaultTask === 'undefined') {
cloned.defaultTask = false;
}
return cloned;
};
const isValidTask = (task) => {
return task && typeof task === 'object' && typeof task.taskName === 'string' && typeof task.scriptPath === 'string';
};
const validTasks = taskArr.filter(isValidTask).map(normalizeTask);
const results = await Promise.all(validTasks.map((task) => new Promise((resolve) => {
this.#taskDb.findOne({ taskName: task.taskName }, (err, doc) => {
if (err) {
console.error('查询任务失败:', err);
return resolve({ task: task.taskName, success: false, message: err.message });
}
if (!doc) {
this.#taskDb.insert(task, (insertErr) => {
if (insertErr) {
console.error('插入任务失败:', insertErr);
return resolve({ task: task.taskName, success: false, message: insertErr.message });
}
return resolve({ task: task.taskName, success: true });
});
} else {
const mergedTask = { ...doc, ...task };
if (typeof task.config === 'undefined' && typeof doc.config !== 'undefined') {
mergedTask.config = doc.config;
}
if (typeof task.configSchema === 'undefined' && typeof doc.configSchema !== 'undefined') {
mergedTask.configSchema = doc.configSchema;
}
this.#taskDb.update({ taskName: task.taskName }, mergedTask, {}, (updateErr) => {
if (updateErr) {
console.error('更新任务失败:', updateErr);
return resolve({ task: task.taskName, success: false, message: updateErr.message });
}
return resolve({ task: task.taskName, success: true });
});
}
});
})));
const failed = results.filter(item => !item.success);
if (failed.length > 0) {
const reason = failed.map(f => `${f.task}:${f.message || 'unknown'}`).join('; ');
return { success: false, message: `部分任务导入失败: ${reason}` };
}
return { success: true, message: `已加载 ${validTasks.length} 个任务` };
} catch (e) {
console.error('从目录加载任务失败:', e);
return { success: false, message: e.message };
}
}
// 对外公开目录加载方法
async loadTasksFromDirectory(directory) {
return this._loadTasksFromDirectory(directory);
}
async setSavePath(savePath) {
// 合并 path 到 savePath.json,保留其它字段
console.log("设置保存路径:", savePath);
this._paths.path = savePath;
this._saveAllPathsToJson();
this.#walletDb = new Datastore({
filename: path.join(savePath, "db/walletData.db"),
autoload: true,
});
this.#taskDb = new Datastore({
filename: path.join(savePath, "/db/task.db"),
autoload: true,
});
this.#fingerPrintDb = new Datastore({
filename: path.join(savePath, "/db/fingerPrint.db"),
autoload: true,
});
this.loadDefaultTask();
// 如果之前已设置过钱包脚本目录/同步脚本目录,则在初始化 DB 后加载其中的任务
if (this._paths.walletScriptDirectory) {
await this._loadTasksFromDirectory(this._paths.walletScriptDirectory);
}
if (this._paths.syncScriptDirectory) {
await this._loadTasksFromDirectory(this._paths.syncScriptDirectory);
}
return await this.refreshData();
}
getSavePath() {
// 优先从内存获取
if (this._paths.path) {
return { success: true, path: this._paths.path };
}
// 回退从文件获取
this._loadAllPathsFromJson();
return { success: !!this._paths.path, path: this._paths.path };
}
setChromePath(chromePath) {
console.log("设置Chrome路径:", chromePath);
this._paths.chromePath = chromePath;
this._saveAllPathsToJson();
return { success: true };
}
getChromePath() {
if (this._paths.chromePath) {
return { success: true, path: this._paths.chromePath };
}
this._loadAllPathsFromJson();
return { success: !!this._paths.chromePath, path: this._paths.chromePath };
}
// ── mini_installer 管理 ──
setInstallerPath(installerPath) {
console.log("设置 Installer 路径:", installerPath);
this._paths.installerPath = installerPath;
this._saveAllPathsToJson();
return { success: true };
}
getInstallerPath() {
if (this._paths.installerPath) {
return { success: true, path: this._paths.installerPath };
}
// 检查 assets 目录下是否存在 mini_installer.exe
const assetsInstaller = path.join(this.assetsPath, 'mini_installer.exe');
if (fs.existsSync(assetsInstaller)) {
return { success: true, path: assetsInstaller };
}
this._loadAllPathsFromJson();
return { success: !!this._paths.installerPath, path: this._paths.installerPath };
}
/**
* 运行 mini_installer.exe 安装浏览器,安装完成后自动更新 chromePath
* @returns {Promise<{success: boolean, message: string, chromePath?: string}>}
*/
async runInstaller() {
const { execFile } = require('child_process');
const installerRes = this.getInstallerPath();
if (!installerRes.success || !installerRes.path) {
return { success: false, message: 'Installer path not configured' };
}
const installerPath = installerRes.path;
if (!fs.existsSync(installerPath)) {
return { success: false, message: `Installer not found: ${installerPath}` };
}
return new Promise((resolve) => {
console.log(`Running installer: ${installerPath}`);
execFile(installerPath, [], { windowsHide: false }, (error) => {
if (error) {
console.error('Installer error:', error);
return resolve({ success: false, message: error.message });
}
// mini_installer 默认安装路径
const defaultChromePath = path.join(
process.env.LOCALAPPDATA || '',
'Chromium', 'Application', 'chrome.exe'
);
if (fs.existsSync(defaultChromePath)) {
this.setChromePath(defaultChromePath);
resolve({ success: true, message: 'Installation completed', chromePath: defaultChromePath });
} else {
resolve({ success: true, message: 'Installer ran but chrome.exe not found at default path. Please set chromePath manually.' });
}
});
});
}
// 获取 initWallet/openWallet 脚本路径(优先用户设置,其次默认 assets/scripts)
getInitWalletScriptPath() {
const p = this._paths.initWalletScriptPath;
if (p && fs.existsSync(p)) {
return p;
}
return path.join(this.defaultScriptPath, 'initWallet.js');
}
getOpenWalletScriptPath() {
const p = this._paths.openWalletScriptPath;
if (p && fs.existsSync(p)) {
return p;
}
return path.join(this.defaultScriptPath, 'openWallet.js');
}
// 获取钱包脚本目录:若用户已设置且有效则返回目录路径,否则返回 'default'
getWalletScriptDirectory() {
const dir = this._paths.walletScriptDirectory;
if (
dir &&
fs.existsSync(dir) &&
fs.existsSync(path.join(dir, 'initWallet.js')) &&
fs.existsSync(path.join(dir, 'openWallet.js'))
) {
return { success: true, code: 0, directory: dir };
}
return { success: true, code: 0, directory: 'default' };
}
// 获取同步脚本目录:若用户已设置且有效则返回目录路径,否则返回 'default'
getSyncScriptDirectory() {
const dir = this._paths.syncScriptDirectory;
if (dir && fs.existsSync(dir)) {
return { success: true, code: 0, directory: dir };
}
return { success: true, code: 0, directory: 'default' };
}
async setWalletScriptDirectory(directory) {
console.log("设置钱包脚本目录:", directory);
const initWalletPath = path.join(directory, 'initWallet.js');
const openWalletPath = path.join(directory, 'openWallet.js');
if (!fs.existsSync(initWalletPath) || !fs.existsSync(openWalletPath)) {
return { success: false, code: 1, message: '目录中缺少initWallet.js或openWallet.js' };
}
this.initWalletScriptPath = initWalletPath;
this.openWalletScriptPath = openWalletPath;
this._paths.walletScriptDirectory = directory;
this._paths.initWalletScriptPath = initWalletPath;
this._paths.openWalletScriptPath = openWalletPath;
this._saveAllPathsToJson();
// 加载并 upsert 自定义目录下的任务
const loadRes = await this._loadTasksFromDirectory(directory);
const ignoreMissingTaskConfig =
loadRes && !loadRes.success && String(loadRes.message || '').includes('未检测到 taskConfig.json');
if (!loadRes.success && !ignoreMissingTaskConfig) {
return { success: false, code: 2, message: loadRes.message || '加载任务失败' };
}
// 确保 initWallet/openWallet 指向新目录脚本(即使没有 taskConfig.json)
try {
if (this.#taskDb) {
const defaultTaskConfig = require(path.join(this.assetsPath, '/defaultTaskConfig.json'));
const taskMap = {
initWallet: initWalletPath,
openWallet: openWalletPath,
};
Object.entries(taskMap).forEach(([taskName, scriptPath]) => {
const base = Array.isArray(defaultTaskConfig)
? defaultTaskConfig.find((t) => t.taskName === taskName)
: null;
const taskObj = {
...(base || { taskName, taskType: 'execByOrder', defaultTask: true }),
scriptPath,
};
if (typeof taskObj.defaultTask === 'undefined') {
taskObj.defaultTask = true;
}
this.#taskDb.update({ taskName }, taskObj, { upsert: true });
});
}
} catch (e) {
console.error('[setWalletScriptDirectory] update task paths failed:', e);
}
return {
success: true,
code: 0,
...(loadRes?.message ? { message: loadRes.message } : {})
};
}
resetWalletScriptDirectory() {
console.log("重置钱包脚本目录到默认");
delete this._paths.walletScriptDirectory;
delete this._paths.initWalletScriptPath;
delete this._paths.openWalletScriptPath;
this._saveAllPathsToJson();
try {
const defaultTaskConfig = require(path.join(this.assetsPath, "/defaultTaskConfig.json"));
if (!this.#taskDb) {
return { success: false, code: 1, message: '任务数据库未初始化' };
}
const tasks = Array.isArray(defaultTaskConfig) ? defaultTaskConfig : [];
['initWallet', 'openWallet'].forEach((taskName) => {
const base = tasks.find((t) => t.taskName === taskName);
if (!base) return;
const taskObj = { ...base };
if (taskObj.scriptPath && !path.isAbsolute(taskObj.scriptPath)) {
taskObj.scriptPath = path.join(this.defaultScriptPath, taskObj.scriptPath);
}
if (typeof taskObj.defaultTask === 'undefined') {
taskObj.defaultTask = true;
}
this.#taskDb.update({ taskName: taskObj.taskName }, taskObj, { upsert: true });
});
return { success: true, code: 0 };
} catch (e) {
console.error('[resetWalletScriptDirectory] failed:', e);
return { success: false, code: 2, message: e.message };
}
}
async setSyncScriptDirectory(directory) {
console.log("设置同步脚本目录:", directory);
if (!fs.existsSync(directory)) {
return { success: false, code: 1, message: '目录不存在' };
}
this._paths.syncScriptDirectory = directory;
this._saveAllPathsToJson();
// 加载并 upsert 自定义目录下的任务
const loadRes = await this._loadTasksFromDirectory(directory);
if (!loadRes.success) {
return { success: false, code: 2, message: loadRes.message || '加载任务失败' };
}
return { success: true, code: 0, ...(loadRes?.message ? { message: loadRes.message } : {}) };
}
resetSyncScriptDirectory() {
console.log("重置同步脚本目录到默认");
delete this._paths.syncScriptDirectory;
this._saveAllPathsToJson();
try {
// 仅重新加载默认的 syncFunction 任务
const defaultTaskConfig = require(path.join(this.assetsPath, "/defaultTaskConfig.json"));
const syncTask = Array.isArray(defaultTaskConfig) ? defaultTaskConfig.find(t => t.taskName === 'syncFunction') : null;
if (!this.#taskDb) {
return { success: false, code: 1, message: '任务数据库未初始化' };
}
if (syncTask) {
const taskObj = { ...syncTask };
if (taskObj.scriptPath && !path.isAbsolute(taskObj.scriptPath)) {
taskObj.scriptPath = path.join(this.defaultScriptPath, taskObj.scriptPath);
}
if (typeof taskObj.defaultTask === 'undefined') {
taskObj.defaultTask = true;
}
// upsert 同名任务
this.#taskDb.update({ taskName: taskObj.taskName }, taskObj, { upsert: true });
} else {
console.warn('[resetSyncScriptDirectory] defaultTaskConfig 未找到 syncFunction 配置');
}
return { success: true, code: 0 };
} catch (e) {
console.error('[resetSyncScriptDirectory] 重新加载默认 syncFunction 失败:', e);
return { success: false, code: 2, message: e.message };
}
}
// 重新加载保存路径下的 DB 数据(用于二次安装后的同步)
async refreshData() {
try {
const savePathRes = this.getSavePath();
const savePath = savePathRes?.path;
if (!savePath) {
return { success: false, code: 5001, message: 'Save path not configured' };
}
const dbDir = path.join(savePath, 'db');
const hasWalletDb = fs.existsSync(path.join(dbDir, 'walletData.db'));
const hasTaskDb = fs.existsSync(path.join(dbDir, 'task.db'));
const hasFingerPrintDb = fs.existsSync(path.join(dbDir, 'fingerPrint.db'));
let wallets = [];
let tasks = [];
let fingerprints = {};
if (hasWalletDb) {
const walletService = require('./server/services/walletService');
await walletService.reinitializeWalletDatabase();
try {
const walletDocs = await walletService.getAllWallets();
wallets = Array.isArray(walletDocs) ? walletDocs : [];
} catch (e) {
wallets = [];
}
}
if (hasFingerPrintDb) {
const fingerPrintService = require('./server/services/fingerPrintService');
await fingerPrintService.reinitializeDatabase();
try {
const fpRes = await fingerPrintService.getFingerPrints();
if (fpRes && fpRes.success && fpRes.data) {
fingerprints = fpRes.data;
}
} catch (e) {
fingerprints = {};
}
}
if (hasTaskDb) {
const taskService = require('./server/services/taskService').getInstance();
try {
tasks = await taskService.getAllTasks(false);
} catch (e) {
tasks = [];
}
}
return {
success: true,
hasDb: { wallet: hasWalletDb, task: hasTaskDb, fingerprint: hasFingerPrintDb },
data: { wallets, tasks, fingerprints }
};
} catch (e) {
return { success: false, code: 5002, message: e.message || 'Refresh data failed' };
}
}
}
module.exports = Config;