-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
218 lines (184 loc) · 7.85 KB
/
index.js
File metadata and controls
218 lines (184 loc) · 7.85 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
import fs from 'fs';
import path from 'path';
import os from 'os';
import { getStorePath } from './src/functions.js';
import { App } from './src/app.js';
import FileExchangeProtocol from "./contract/FileExchangeProtocol.js"; //
import FileExchangeContract from "./contract/FileExchangeContract.js";
import Migration from "./features/migration/index.js";
import readline from 'readline';
export * from 'trac-peer/src/functions.js';
function getSafePearConfigDir() {
if (typeof Pear !== 'undefined' && Pear.config && Pear.config.dir) {
return Pear.config.dir;
}
const storePath = getStorePath();
return storePath;
}
const RECEIPTS_DIR = path.join(getSafePearConfigDir(), 'receipts');
try {
if (!fs.existsSync(RECEIPTS_DIR)) {
fs.mkdirSync(RECEIPTS_DIR, { recursive: true });
if (typeof process !== "undefined" && (process.platform === 'linux' || process.platform === 'darwin')) {
try {
fs.chmodSync(RECEIPTS_DIR, 0o755);
} catch (chmodError) {
console.warn('Could not set directory permissions:', chmodError.message);
}
}
}
} catch (error) {
console.error('Error creating receipts directory:', error.message);
console.error('Attempting to create in alternative location...');
const fallbackReceiptsDir = path.join(os.homedir(), '.hypertokens-cli', 'receipts');
try {
fs.mkdirSync(fallbackReceiptsDir, { recursive: true });
console.log('Created receipts directory in:', fallbackReceiptsDir);
Object.defineProperty(globalThis, 'RECEIPTS_DIR', {
value: fallbackReceiptsDir,
writable: false
});
} catch (fallbackError) {
console.error('Failed to create fallback receipts directory:', fallbackError.message);
if (typeof process !== "undefined") {
process.exit(1);
} else {
console.error('No se pudo salir del proceso, "process" no definido. Por favor, cierre manualmente.');
}
}
}
console.log('Storage path:', getStorePath());
console.log('Receipts path:', RECEIPTS_DIR);
const msb_opts = {
bootstrap: '54c2623aa400b769b2837873653014587278fb83fd72e255428f78a4ff7bac87',
channel: '00000000000000000000000trac20msb',
store_name: getStorePath() + '/t20msb_2'
};
const peer_opts = {
protocol: FileExchangeProtocol,
contract: FileExchangeContract,
bootstrap: '0c2ece0c5e17fb8dc2bb53c5850d46a4d7b3eae170e4bd53c0c7d676e1194163',
channel: '0000000000000000000000104fracpnk',
store_name: getStorePath() + '/file-exchange-db',
enable_logs: true,
enable_txlogs: false,
receipts_path: globalThis.RECEIPTS_DIR || RECEIPTS_DIR
};
const old_path_v1 = getStorePath() + "/trac20";
const new_path_v1 = peer_opts.store_name;
if (false === fs.existsSync(new_path_v1 + '/db') &&
true === fs.existsSync(old_path_v1 + '/db/keypair.json')) {
fs.mkdirSync(new_path_v1, { recursive: true });
fs.mkdirSync(new_path_v1 + '/db', { recursive: true });
fs.copyFileSync(old_path_v1 + '/db/keypair.json', new_path_v1 + '/db/keypair.json');
fs.rmSync(old_path_v1, { recursive: true, force: true });
console.log(`Migrated keypair from ${old_path_v1} to ${new_path_v1}`);
}
const old_path_v2 = getStorePath() + "/trac20_2";
const new_path_v2 = peer_opts.store_name;
if (false === fs.existsSync(new_path_v2 + '/db') &&
true === fs.existsSync(old_path_v2 + '/db/keypair.json')) {
fs.mkdirSync(new_path_v2, { recursive: true });
fs.mkdirSync(new_path_v2 + '/db', { recursive: true });
fs.copyFileSync(old_path_v2 + '/db/keypair.json', new_path_v2 + '/db/keypair.json');
fs.rmSync(old_path_v2, { recursive: true, force: true });
console.log(`Migrated keypair from ${old_path_v2} to ${new_path_v2}`);
}
export const app = new App(msb_opts, peer_opts, [
{ name: 'migration', class: Migration }
]);
let rl;
let isShuttingDown = false;
async function shutdown() {
console.log('Iniciando apagado limpio...');
if (rl) {
rl.close();
if (typeof process !== "undefined" && process.stdin && process.stdin.isPaused()) {
process.stdin.resume();
}
}
console.log('Deteniendo nodo trac-peer...');
try {
if (app && app.peer && typeof app.peer.stop === 'function') {
await app.peer.stop();
console.log('Nodo trac-peer detenido.');
} else if (app && typeof app.stop === 'function') {
await app.stop();
console.log('Aplicación trac-peer detenida.');
} else {
console.warn('No se encontró instancia de trac-peer o método stop para detener.');
}
} catch (stopError) {
console.error('Error al detener trac-peer:', stopError.message);
}
console.log('Saliendo del proceso Node.js.');
if (typeof process !== "undefined") {
process.exit(0);
} else {
console.log('Running in Pear environment - manual shutdown required.');
}
}
try {
await app.start();
console.log("trac-peer node started successfully.");
console.log("Minter/Owner Address:", app.peer.wallet.publicKey);
console.log("\nNode is running in interactive mode.");
console.log("Type '/commands' to see available file exchange options.");
console.log("========================================================\n");
if (typeof process !== "undefined") {
rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', async (input) => {
if (input.trim().toLowerCase() === '/exit') {
console.log('Detectado comando /exit. Iniciando apagado...');
await shutdown();
}
});
console.log("Escribe '/exit' para apagar el nodo.");
} else {
console.log("Running in Pear environment - use Pear controls to exit.");
}
} catch (startError) {
console.error('Error starting application:', startError.message);
console.error('Stack trace:', startError.stack);
if (typeof process !== "undefined" && process.platform === 'linux') {
console.error('\nLinux diagnostic information:');
console.error('- Current user:', os.userInfo().username);
console.error('- Home directory:', os.homedir());
console.error('- Storage path exists:', fs.existsSync(getStorePath()));
console.error('- Receipts path exists:', fs.existsSync(globalThis.RECEIPTS_DIR || RECEIPTS_DIR));
console.error('- Node.js version:', process.version);
console.error('- Platform:', process.platform);
console.error('- Architecture:', process.arch);
}
if (typeof process !== "undefined") {
process.exit(1);
} else {
console.error('No se pudo salir del proceso, "process" no definido. Por favor, cierre manualmente.');
}
}
if (typeof process !== "undefined") {
process.on('SIGINT', async () => {
if (isShuttingDown) return;
isShuttingDown = true;
console.log('\nSeñal SIGINT (Control+C) recibida. Iniciando apagado limpio...');
await shutdown();
});
process.on('SIGTERM', async () => {
if (isShuttingDown) return;
isShuttingDown = true;
console.log('\nSeñal SIGTERM recibida. Iniciando apagado limpio...');
await shutdown();
});
process.on('unhandledRejection', (reason, promise) => {
console.error('ERROR CRÍTICO: Promesa no manejada:', reason);
shutdown();
});
process.on('uncaughtException', (err) => {
console.error('ERROR CRÍTICO: Excepción no capturada:', err);
shutdown();
setTimeout(() => process.exit(1), 1000);
});
}