soluser 1.0.14 → 1.0.15
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.
- package/README.md +5 -0
- package/bin/index.js +10 -1
- package/package.json +1 -1
- package/src/commands/export.js +38 -0
- package/src/utils/example.js +3 -0
package/README.md
CHANGED
package/bin/index.js
CHANGED
|
@@ -19,6 +19,7 @@ const showAddress = require('../src/commands/address');
|
|
|
19
19
|
const checkBalance = require('../src/commands/balance');
|
|
20
20
|
// Import the airdrop command
|
|
21
21
|
const requestAirdrop = require('../src/commands/airdrop');
|
|
22
|
+
const exportPrivateKey = require('../src/commands/export');
|
|
22
23
|
|
|
23
24
|
// Get version from package.json
|
|
24
25
|
const packageJson = require('../package.json');
|
|
@@ -157,7 +158,15 @@ program
|
|
|
157
158
|
restoreAccount();
|
|
158
159
|
});
|
|
159
160
|
|
|
160
|
-
|
|
161
|
+
// 定义 export 命令
|
|
162
|
+
program
|
|
163
|
+
.command('export')
|
|
164
|
+
.alias('e')
|
|
165
|
+
.description('Output the base58 private key of a Solana account')
|
|
166
|
+
.argument('<alias>', 'Alias of the account to export private key')
|
|
167
|
+
.action((alias) => {
|
|
168
|
+
exportPrivateKey(alias);
|
|
169
|
+
});
|
|
161
170
|
|
|
162
171
|
|
|
163
172
|
program.on('--help', () => {
|
package/package.json
CHANGED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const { existsAccount, getKeyFilePath } = require('../utils/path');
|
|
2
|
+
const { Keypair } = require('@solana/web3.js');
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const bs58 = require('bs58').default;
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
function exportPrivateKey(alias) {
|
|
8
|
+
// Check if account exists
|
|
9
|
+
if (!existsAccount(alias)) {
|
|
10
|
+
console.error(`Error: Account "${alias}" does not exist.`);
|
|
11
|
+
console.error(` Use "soluser list" to view all available accounts.`);
|
|
12
|
+
process.exit(1);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
try {
|
|
16
|
+
// Read and parse the key file
|
|
17
|
+
const keyPath = getKeyFilePath(alias);
|
|
18
|
+
const keyData = fs.readFileSync(keyPath, 'utf8');
|
|
19
|
+
const secretKeyArray = JSON.parse(keyData);
|
|
20
|
+
|
|
21
|
+
// Convert to Uint8Array and create Keypair
|
|
22
|
+
const secretKey = Uint8Array.from(secretKeyArray);
|
|
23
|
+
const keypair = Keypair.fromSecretKey(secretKey);
|
|
24
|
+
|
|
25
|
+
// Encode to base58 and output
|
|
26
|
+
const base58PrivateKey = bs58.encode(secretKey );
|
|
27
|
+
process.stdout.write(`address for "${alias}" ${keypair.publicKey.toBase58()}:\n`);
|
|
28
|
+
process.stdout.write(`private key for "${alias}": ${base58PrivateKey}\n`);
|
|
29
|
+
} catch (err) {
|
|
30
|
+
console.error(`Error: Failed to export private key for "${alias}": ${err.message}`);
|
|
31
|
+
for( k in bs58) {
|
|
32
|
+
console.log(k,":", bs58[k]);
|
|
33
|
+
}
|
|
34
|
+
process.exit(1);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
module.exports = exportPrivateKey;
|