soluser 1.0.3 → 1.0.5
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 +10 -0
- package/bin/index.js +24 -0
- package/package.json +1 -1
- package/src/commands/address.js +22 -0
- package/src/commands/balance.js +33 -0
- package/src/commands/list.js +2 -2
- package/src/utils/solana.js +1 -1
package/README.md
CHANGED
package/bin/index.js
CHANGED
|
@@ -6,6 +6,30 @@ const switchAccount = require('../src/commands/switch');
|
|
|
6
6
|
const listAccounts = require('../src/commands/list');
|
|
7
7
|
const removeAccount = require('../src/commands/remove');
|
|
8
8
|
|
|
9
|
+
// 导入地址查询命令
|
|
10
|
+
const showAddress = require('../src/commands/address');
|
|
11
|
+
|
|
12
|
+
// 定义地址查询命令
|
|
13
|
+
program
|
|
14
|
+
.command('address')
|
|
15
|
+
.description('Output the base58 address of a Solana account')
|
|
16
|
+
.argument('<alias>', 'Alias of the account to get address') // 接收别名作为位置参数
|
|
17
|
+
.action((alias) => {
|
|
18
|
+
showAddress(alias);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
// 导入余额查询命令
|
|
22
|
+
const checkBalance = require('../src/commands/balance');
|
|
23
|
+
|
|
24
|
+
// 定义余额查询命令
|
|
25
|
+
program
|
|
26
|
+
.command('balance')
|
|
27
|
+
.description('Check the SOL balance of a Solana account')
|
|
28
|
+
.argument('<alias>', 'Alias of the account to check balance') // 接收别名作为位置参数
|
|
29
|
+
.action((alias) => {
|
|
30
|
+
checkBalance(alias);
|
|
31
|
+
});
|
|
32
|
+
|
|
9
33
|
|
|
10
34
|
// 定义新建账号命令
|
|
11
35
|
// 定义新建账号命令(修改后)
|
package/package.json
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const { existsAccount, getKeyFilePath } = require('../utils/path');
|
|
2
|
+
const { getAddress } = require('../utils/solana');
|
|
3
|
+
|
|
4
|
+
function showAddress(alias) {
|
|
5
|
+
// 1. 检查账号是否存在
|
|
6
|
+
if (!existsAccount(alias)) {
|
|
7
|
+
console.error(`Error: Account "${alias}" does not exist.`);
|
|
8
|
+
console.error(` Use "soluser list" to view all available accounts.`);
|
|
9
|
+
process.exit(1);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// 2. 解析并输出 base58 地址
|
|
13
|
+
try {
|
|
14
|
+
const address = getAddress(alias); // 复用之前的 getAddress 函数(基于 solana-keygen)
|
|
15
|
+
console.log(address); // 直接输出地址(方便脚本调用)
|
|
16
|
+
} catch (err) {
|
|
17
|
+
console.error(`Error: Failed to parse address for "${alias}": ${err.message}`);
|
|
18
|
+
process.exit(1);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = showAddress;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const { existsAccount } = require('../utils/path');
|
|
2
|
+
const { getAddress, execCommand } = require('../utils/solana');
|
|
3
|
+
|
|
4
|
+
function checkBalance(alias) {
|
|
5
|
+
// 1. 检查账号是否存在
|
|
6
|
+
if (!existsAccount(alias)) {
|
|
7
|
+
console.error(`Error: Account "${alias}" does not exist.`);
|
|
8
|
+
console.error(` Use "soluser list" to view all available accounts.`);
|
|
9
|
+
process.exit(1);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// 2. 获取账号对应的公钥(address)
|
|
13
|
+
let address;
|
|
14
|
+
try {
|
|
15
|
+
address = getAddress(alias);
|
|
16
|
+
} catch (err) {
|
|
17
|
+
console.error(`Error: Failed to get address for "${alias}": ${err.message}`);
|
|
18
|
+
process.exit(1);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// 3. 调用 solana balance 命令查询余额
|
|
22
|
+
try {
|
|
23
|
+
// solana balance 命令会返回类似 "1.2345 SOL" 的结果
|
|
24
|
+
const balance = execCommand(`solana balance ${address}`);
|
|
25
|
+
console.log(`${alias}: ${balance}`); // 输出格式:别名 + 余额
|
|
26
|
+
} catch (err) {
|
|
27
|
+
console.error(`Error: Failed to check balance for "${alias}": ${err.message}`);
|
|
28
|
+
console.error(` Ensure Solana CLI is configured with a valid network (e.g., "solana config set --url https://api.devnet.solana.com")`);
|
|
29
|
+
process.exit(1);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
module.exports = checkBalance;
|
package/src/commands/list.js
CHANGED
|
@@ -26,7 +26,7 @@ function listAccounts() {
|
|
|
26
26
|
// 3. 构建表格
|
|
27
27
|
const table = new Table({
|
|
28
28
|
head: ['alias', 'address', 'active'],
|
|
29
|
-
colWidths: [15,
|
|
29
|
+
colWidths: [15, 50, 8],
|
|
30
30
|
});
|
|
31
31
|
|
|
32
32
|
// 4. 填充表格数据
|
|
@@ -34,7 +34,7 @@ function listAccounts() {
|
|
|
34
34
|
const alias = path.basename(file, '.json');
|
|
35
35
|
const address = getAddress(alias);
|
|
36
36
|
const isActive = alias === activeAlias ? '*' : '';
|
|
37
|
-
console.log("alias: ", alias, "address: ", address, "isActive: ", isActive,'activeAlias',activeAlias )
|
|
37
|
+
//console.log("alias: ", alias, "address: ", address, "isActive: ", isActive,'activeAlias',activeAlias )
|
|
38
38
|
table.push([alias, address, isActive]);
|
|
39
39
|
});
|
|
40
40
|
|
package/src/utils/solana.js
CHANGED
|
@@ -21,7 +21,7 @@ function getActiveKeyPath() {
|
|
|
21
21
|
const config = execCommand('solana config get keypair');
|
|
22
22
|
// 从输出中提取路径(例如:"Keypair Path: /home/user/.config/solana/keys/alice.json")
|
|
23
23
|
const match = config.match(/Key Path: (.*)/);
|
|
24
|
-
console.log("config:",config,"config match :",match)
|
|
24
|
+
//console.log("config:",config,"config match :",match)
|
|
25
25
|
return match ? match[1] : null;
|
|
26
26
|
}
|
|
27
27
|
|