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 CHANGED
@@ -60,3 +60,8 @@ Signature: 4BLUt5uxutbEwVywBTbAoBnG4EKb6QgsHgk3JRfjy6uJCoNjxdyYodbAhsWPXquBBwVzu
60
60
  ```shell
61
61
  $ soluser import "mnemonic words" --alias someone
62
62
  ```
63
+
64
+ ## 导出私钥为base58编码
65
+ ```shell
66
+ $ soluser export someone
67
+ ```
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "soluser",
3
- "version": "1.0.14",
3
+ "version": "1.0.15",
4
4
  "description": "A CLI tool to manage Solana accounts (new, switch, list)",
5
5
  "main": "bin/index.js",
6
6
  "bin": {
@@ -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;
@@ -40,6 +40,9 @@ $ soluser airdrop 5 alice
40
40
 
41
41
  $ soluser import "mnemonic ... (12words or 24words)" --alias someone
42
42
 
43
+ ## export private key of alias
44
+ $ soluser export alice
45
+
43
46
  `;
44
47
 
45
48
  const chalk = require('chalk');