forked from BlackBeard085/x1console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
excheckaccounts.js
239 lines (211 loc) · 9.7 KB
/
excheckaccounts.js
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
const { exec, execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const homeDir = process.env.HOME || process.env.HOMEPATH;
const stakePath = path.join(homeDir, '.config/solana/stake.json');
const votePath = path.join(homeDir, '.config/solana/vote.json');
const identityPath = path.join(homeDir, '.config/solana/identity.json');
const withdrawerPath = path.join(homeDir, '.config/solana/id.json');
const archivePath = path.join(homeDir, '.config/solana/archive');
// Paths for wallets.json
const walletsDir = path.join(homeDir, 'x1console');
const solanalabsDir = path.join(homeDir, 'x1/solanalabs');
const walletsFilePath = path.join(walletsDir, 'wallets.json');
if (!fs.existsSync(walletsDir)) {
fs.mkdirSync(walletsDir, { recursive: true }); // Create the x1console directory if it doesn't exist
}
if (!fs.existsSync(solanalabsDir)) {
fs.mkdirSync(solanalabsDir, { recursive: true }); // Create the solanalabs directory if it doesn't exist
}
let newWalletsCreated = false; // Track if any new wallets are created
// Function to capitalize the first letter of a string
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
// Function to update wallets.json with public keys from existing files
function updateWallets() {
const files = [withdrawerPath, identityPath, stakePath, votePath];
const wallets = [];
try {
// Collect public keys from the specified files
for (const file of files) {
if (fs.existsSync(file)) {
const publicKey = execSync(`solana-keygen pubkey ${file}`).toString().trim();
const walletName = capitalizeFirstLetter(path.basename(file, '.json'));
wallets.push({ name: walletName, address: publicKey });
}
}
// Write the wallets to wallets.json
fs.writeFileSync(walletsFilePath, JSON.stringify(wallets, null, 2));
console.log('wallets.json created/updated.');
// Copy the wallets.json to the solanalabs directory
fs.copyFileSync(walletsFilePath, path.join(solanalabsDir, 'wallets.json'));
console.log(`Copied wallets.json to: ${solanalabsDir}`);
} catch (error) {
console.error(`Error updating wallets.json: ${error}`);
}
}
// Function to move and create new stake account
function moveAndCreateStakeAccount() {
return new Promise((resolve, reject) => {
if (!fs.existsSync(archivePath)) {
fs.mkdirSync(archivePath);
}
const newStakePath = path.join(archivePath, 'stake.json');
fs.rename(stakePath, newStakePath, (err) => {
if (err) {
reject(`Error moving stake account to archive: ${err}`);
return;
}
console.log(`Moved stake.json to archive: ${newStakePath}`);
exec(`solana-keygen new --no-passphrase -o ${stakePath}`, (keygenError) => {
if (keygenError) {
reject(`Error creating new stake account: ${keygenError}`);
return;
}
console.log(`Created new stake account: ${stakePath}`);
newWalletsCreated = true; // A new wallet was created for the stake account
exec(`solana create-stake-account ${stakePath} 2`, (createError) => {
if (createError) {
reject(`Error creating stake account: ${createError}`);
return;
}
exec(`solana stake-account ${stakePath}`, (checkError, checkStdout) => {
if (checkError) {
reject(`Error checking new stake account: ${checkError}`);
return;
}
const outputLines = checkStdout.split('\n').slice(0, 10).join('\n');
resolve(`New stake account exists:\n${outputLines}`);
});
});
});
});
});
}
// Function to move and create new vote account
function moveAndCreateVoteAccount() {
return new Promise((resolve, reject) => {
if (!fs.existsSync(archivePath)) {
fs.mkdirSync(archivePath);
}
const newVotePath = path.join(archivePath, 'vote.json');
fs.rename(votePath, newVotePath, (err) => {
if (err) {
reject(`Error moving vote account to archive: ${err}`);
return;
}
console.log(`Moved vote.json to archive: ${newVotePath}`);
exec(`solana-keygen new --no-passphrase -o ${votePath}`, (keygenError) => {
if (keygenError) {
reject(`Error creating new vote account: ${keygenError}`);
return;
}
console.log(`Created new vote account: ${votePath}`);
newWalletsCreated = true; // A new wallet was created for the vote account
exec(`solana create-vote-account ${votePath} ${identityPath} ${withdrawerPath} --commission 10`, (createError) => {
if (createError) {
reject(`Error creating vote account: ${createError}`);
return;
}
exec(`solana vote-account ${votePath}`, (checkError, checkStdout) => {
if (checkError) {
reject(`Error checking new vote account: ${checkError}`);
return;
}
const outputLines = checkStdout.split('\n').slice(0, 10).join('\n');
resolve(`New vote account exists:\n${outputLines}`);
});
});
});
});
});
}
// Function to check stake account
function checkStakeAccount() {
return new Promise((resolve, reject) => {
exec(`solana stake-account ${stakePath}`, (error, stdout, stderr) => {
if (stderr.includes("AccountNotFound")) {
exec(`solana create-stake-account ${stakePath} 2`, (createErr, createStdout, createStderr) => {
if (createErr) {
reject(`Error creating stake account: ${createStderr}`);
} else {
resolve(`Stake account created: ${createStdout}`);
}
});
} else if (stderr.includes("is not a stake account")) {
moveAndCreateStakeAccount()
.then(message => resolve(message))
.catch(err => reject(err));
} else if (error) {
reject(`Error checking stake account: ${stderr}`);
return;
} else {
const outputLines = stdout.split('\n').slice(0, 10).join('\n');
resolve(`Stake account exists:\n${outputLines}`);
}
});
});
}
// Function to check vote account
function checkVoteAccount() {
return new Promise((resolve, reject) => {
exec(`solana vote-account ${votePath}`, (error, stdout, stderr) => {
if (stderr.includes("account does not exist")) {
exec(`solana create-vote-account ${votePath} ${identityPath} ${withdrawerPath} --commission 10`, (createErr, createStdout, createStderr) => {
if (createErr) {
reject(`Error creating vote account: ${createStderr}`);
} else {
resolve(`Vote account created: ${createStdout}`);
}
});
} else if (stderr.includes("is not a vote account")) {
moveAndCreateVoteAccount()
.then(message => resolve(message))
.catch(err => reject(err));
} else if (error) {
reject(`Error checking vote account: ${stderr}`);
return;
} else {
const outputLines = stdout.split('\n').slice(0, 10).join('\n');
resolve(`Vote account exists:\n${outputLines}`);
}
});
});
}
// Function to create wallets.json file, if new wallets were created.
function createWalletsJSON() {
if (!newWalletsCreated) {
console.log('No new wallets were created; wallets.json will not be generated.');
return;
}
const wallets = [
{ name: capitalizeFirstLetter("withdrawer"), address: execSync(`solana-keygen pubkey ${withdrawerPath}`).toString().trim() },
{ name: capitalizeFirstLetter("identity"), address: execSync(`solana-keygen pubkey ${identityPath}`).toString().trim() },
{ name: capitalizeFirstLetter("stake"), address: execSync(`solana-keygen pubkey ${stakePath}`).toString().trim() },
{ name: capitalizeFirstLetter("vote"), address: execSync(`solana-keygen pubkey ${votePath}`).toString().trim() },
];
fs.writeFileSync(walletsFilePath, JSON.stringify(wallets, null, 2));
console.log('wallets.json created/updated.');
// Copy the wallets.json to the solanalabs directory
fs.copyFileSync(walletsFilePath, path.join(solanalabsDir, 'wallets.json'));
console.log(`Copied wallets.json to: ${solanalabsDir}`);
}
// Main function to execute the checks
async function main() {
updateWallets(); // Update wallets.json with existing public keys
try {
const [stakeResult, voteResult] = await Promise.all([
checkStakeAccount(),
checkVoteAccount(),
]);
console.log(stakeResult);
console.log(voteResult);
// Create wallets.json after checking accounts
createWalletsJSON();
} catch (error) {
console.error(`Error occurred: ${error}`);
}
}
// Execute the main function
main();