wative 1.2.7 → 1.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,181 +0,0 @@
1
- const { spawn } = require('child_process');
2
- const fs = require('fs');
3
- const path = require('path');
4
- const crypto = require('crypto');
5
-
6
- // Get configuration from command line arguments
7
- const serviceName = process.argv[2];
8
- const logFile = process.argv[3];
9
- const pidFile = process.argv[4];
10
- const watcherPidFile = process.argv[5];
11
- const wativeRoot = process.argv[6];
12
-
13
- // Get encrypted passwords from environment variable and immediately delete it
14
- const encryptedPasswords = process.env.WATIVE_ENCRYPTED_PASSWORDS;
15
- delete process.env.WATIVE_ENCRYPTED_PASSWORDS;
16
-
17
- if (!encryptedPasswords) {
18
- console.error('Error: WATIVE_ENCRYPTED_PASSWORDS environment variable not found');
19
- process.exit(1);
20
- }
21
-
22
- let childProcess = null;
23
- let restartCount = 0;
24
- const maxRestarts = 10;
25
- const restartDelay = 5000; // 5 seconds
26
-
27
- // Hash verification for wative.umd.js
28
- let initialFileHash = null;
29
- const wativeUmdPath = path.join(wativeRoot, 'lib', 'wative.umd.js');
30
-
31
- /**
32
- * Calculate SHA256 hash of a file
33
- * @param {string} filePath - Path to the file
34
- * @returns {string|null} - Hash string or null if file doesn't exist
35
- */
36
- const calculateFileHash = (filePath) => {
37
- try {
38
- if (!fs.existsSync(filePath)) {
39
- return null;
40
- }
41
- const fileBuffer = fs.readFileSync(filePath);
42
- const hashSum = crypto.createHash('sha256');
43
- hashSum.update(fileBuffer);
44
- return hashSum.digest('hex');
45
- } catch (error) {
46
- logToFile(`Error calculating file hash: ${error.message}`);
47
- return null;
48
- }
49
- };
50
-
51
- /**
52
- * Verify if the current file hash matches the initial hash
53
- * @returns {boolean} - True if hashes match or initial hash is not set
54
- */
55
- const verifyFileHash = () => {
56
- if (!initialFileHash) {
57
- return true; // No initial hash to compare against
58
- }
59
-
60
- const currentHash = calculateFileHash(wativeUmdPath);
61
- if (!currentHash) {
62
- logToFile(`Warning: Could not calculate current hash for ${wativeUmdPath}`);
63
- return false;
64
- }
65
-
66
- const hashesMatch = currentHash === initialFileHash;
67
- if (!hashesMatch) {
68
- logToFile(`File hash mismatch detected!`);
69
- logToFile(`Initial hash: ${initialFileHash}`);
70
- logToFile(`Current hash: ${currentHash}`);
71
- }
72
-
73
- return hashesMatch;
74
- };
75
-
76
- /**
77
- * Log message to daemon log file with timestamp
78
- * @param {string} message - Message to log
79
- */
80
- const logToFile = (message) => {
81
- const timestamp = new Date().toISOString();
82
- const logMessage = `[${timestamp}] [WATCHER] ${message}\n`;
83
- fs.appendFileSync(logFile, logMessage);
84
- };
85
-
86
- /**
87
- * Start the SSH service process
88
- */
89
- const startService = () => {
90
- if (restartCount >= maxRestarts) {
91
- logToFile(`Maximum restart attempts (${maxRestarts}) reached. Stopping watcher.`);
92
- process.exit(1);
93
- }
94
-
95
- // Initialize or verify file hash
96
- if (initialFileHash === null) {
97
- // First startup - calculate and store the initial hash
98
- initialFileHash = calculateFileHash(wativeUmdPath);
99
- if (initialFileHash) {
100
- logToFile(`Initial file hash calculated: ${initialFileHash}`);
101
- } else {
102
- logToFile(`Warning: Could not calculate initial hash for ${wativeUmdPath}`);
103
- }
104
- } else {
105
- // Restart - verify hash before proceeding
106
- if (!verifyFileHash()) {
107
- logToFile(`File integrity check failed. Exiting daemon to prevent potential security issues.`);
108
- process.exit(1);
109
- }
110
- logToFile(`File integrity check passed.`);
111
- }
112
-
113
- logToFile(`Starting SSH service '${serviceName}' (attempt ${restartCount + 1})`);
114
-
115
- // Spawn the SSH service process
116
- childProcess = spawn('node', [path.join(wativeRoot, 'bin', 'wative.js'), 'ssh', 'start', '-c', serviceName], {
117
- stdio: ['ignore', fs.openSync(logFile, 'a'), fs.openSync(logFile, 'a')],
118
- env: {
119
- ...process.env,
120
- WATIVE_DAEMON_PASSWORDS: encryptedPasswords,
121
- WATIVE_WORKER_MODE: 'true'
122
- },
123
- cwd: wativeRoot
124
- });
125
-
126
- // Write service PID to file
127
- fs.writeFileSync(pidFile, childProcess.pid.toString());
128
-
129
- // Handle service process exit
130
- childProcess.on('exit', (code, signal) => {
131
- logToFile(`SSH service exited with code ${code}, signal ${signal}`);
132
-
133
- // Check if service crashed (non-zero exit code and not terminated by user)
134
- if (code !== 0 && signal !== 'SIGTERM' && signal !== 'SIGINT') {
135
- restartCount++;
136
- logToFile(`Service crashed. Restarting in ${restartDelay/1000} seconds...`);
137
- setTimeout(startService, restartDelay);
138
- } else {
139
- logToFile('Service stopped normally.');
140
- // Clean up PID files
141
- try {
142
- fs.unlinkSync(pidFile);
143
- fs.unlinkSync(watcherPidFile);
144
- } catch (e) {
145
- // Ignore cleanup errors
146
- }
147
- process.exit(0);
148
- }
149
- });
150
-
151
- // Handle service process errors
152
- childProcess.on('error', (error) => {
153
- logToFile(`Failed to start service: ${error.message}`);
154
- restartCount++;
155
- setTimeout(startService, restartDelay);
156
- });
157
- };
158
-
159
- /**
160
- * Handle graceful shutdown signals
161
- * @param {string} signal - Signal received
162
- */
163
- const gracefulShutdown = (signal) => {
164
- logToFile(`Watcher received ${signal}, stopping service...`);
165
- if (childProcess) {
166
- childProcess.kill('SIGTERM');
167
- // Force kill after 5 seconds if process doesn't terminate gracefully
168
- setTimeout(() => {
169
- if (childProcess && !childProcess.killed) {
170
- childProcess.kill('SIGKILL');
171
- }
172
- }, 5000);
173
- }
174
- };
175
-
176
- // Register signal handlers for graceful shutdown
177
- process.on('SIGTERM', () => gracefulShutdown('SIGTERM'));
178
- process.on('SIGINT', () => gracefulShutdown('SIGINT'));
179
-
180
- // Start the service
181
- startService();
package/src/home_page.ts DELETED
@@ -1,36 +0,0 @@
1
- import { selectSomething } from "./utils";
2
- import { showNetwork } from "./network";
3
- import { accountSetting } from "./account";
4
- import { showTools } from "./tools";
5
- const { WativeCore } = require("wative-core");
6
-
7
- export const showHomePage = async (keystore_path: string, wative_core: typeof WativeCore) => {
8
- const home_page_list = [
9
- '> System & Settings',
10
- '> Accounts',
11
- '> Tools',
12
- '> Exit'
13
- ];
14
-
15
- const selected_home_page = await selectSomething(home_page_list);
16
-
17
- switch (selected_home_page) {
18
- case '> System & Settings': {
19
- await showNetwork(keystore_path);
20
- break;
21
- }
22
- case '> Accounts': {
23
- await accountSetting(keystore_path, wative_core);
24
- break;
25
- }
26
- case '> Tools': {
27
- await showTools(keystore_path, wative_core);
28
- break;
29
- }
30
- case '> Exit': {
31
- return;
32
- }
33
- }
34
-
35
- await showHomePage(keystore_path, wative_core);
36
- }
package/src/index.d.ts DELETED
@@ -1,12 +0,0 @@
1
- declare module 'wative-core' {
2
- export class WativeCore {
3
- constructor(...args: any[]);
4
- [key: string]: any;
5
- }
6
-
7
- export interface WativeCoreOptions {
8
- [key: string]: any;
9
- }
10
-
11
- export default WativeCore;
12
- }
package/src/index.ts DELETED
@@ -1,53 +0,0 @@
1
- import * as fs from "fs";
2
- import * as path from "path";
3
- import { showIntroduction, getKeystorePath, updateStorage, batchGetKeystoreSlugByAccountName } from "./utils";
4
- import { createNetwork } from "./network";
5
- import { showHomePage } from "./home_page";
6
-
7
- const { WativeCore } = require("wative-core");
8
- const packageJson = require('../package.json');
9
- const Event = require('events');
10
- Event.EventEmitter.defaultMaxListeners = 0;
11
-
12
- const main = async () => {
13
- console.log("version:", packageJson.version);
14
-
15
- showIntroduction();
16
-
17
- const keystore_path = await getKeystorePath();
18
- await createNetwork(keystore_path);
19
- updateStorage(keystore_path);
20
-
21
- const network_path = path.resolve(keystore_path, 'network.json');
22
- const networks = JSON.parse(fs.readFileSync(network_path, 'utf8'));
23
- let getKeystoreSlugByAccountName_list = batchGetKeystoreSlugByAccountName(networks.accounts);
24
- const wative_core = new WativeCore(keystore_path, getKeystoreSlugByAccountName_list, null, true);
25
- await showHomePage(keystore_path, wative_core);
26
- }
27
-
28
- process.on('unhandledRejection', (reason, p) => {
29
- console.log('Unhandled Rejection at:', p, 'reason:', reason);
30
- });
31
-
32
- // Only run main() if this file is executed directly, not when imported as a module
33
- if (require.main === module) {
34
- main()
35
- .then(() => process.exit(0))
36
- .catch((error: any) => {
37
- console.log(error.message);
38
- process.exit(1)
39
- })
40
- }
41
-
42
- // Export modules for library usage
43
- export * as SSH from './ssh';
44
- export * from './utils';
45
- export * from './network';
46
- export * from './account';
47
- export * from './assets';
48
- export * from './tools';
49
- export * from './web3';
50
- export * from './wative';
51
- export * from './chain';
52
- export * from './config';
53
- export * from './tx_gas_utils';