beeline-cli
Version:
A terminal wallet for the Hive blockchain - type, sign, rule the chain
258 lines • 13.6 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@oclif/core");
const neon_js_1 = require("../utils/neon.js");
const crypto_js_1 = require("../utils/crypto.js");
const hive_js_1 = require("../utils/hive.js");
const inquirer_1 = __importDefault(require("inquirer"));
class Accounts extends core_1.Command {
async run() {
const { args, flags } = await this.parse(Accounts);
const keyManager = new crypto_js_1.KeyManager();
await keyManager.initialize();
console.log(neon_js_1.neonChalk.glow(`${neon_js_1.neonSymbols.diamond} Accessing account management...`));
console.log('');
switch (args.action) {
case 'list':
await this.listAccounts(keyManager, flags.format);
break;
case 'switch':
await this.switchAccount(keyManager, args.account);
break;
case 'info':
await this.showAccountInfo(keyManager, args.account);
break;
case 'remove':
await this.removeAccount(keyManager, args.account, flags.force);
break;
}
}
async listAccounts(keyManager, format) {
const summaries = await keyManager.getAllAccountSummaries();
if (summaries.length === 0) {
console.log((0, neon_js_1.createNeonBox)(`${neon_js_1.neonChalk.warning('No accounts found in wallet')}\n\n` +
`Add your first account with:\n` +
`${neon_js_1.neonChalk.highlight('beeline login <account>')}`, `${neon_js_1.neonSymbols.star} ACCOUNT WALLET ${neon_js_1.neonSymbols.star}`));
return;
}
if (format === 'json') {
console.log(JSON.stringify(summaries, null, 2));
return;
}
let accountDisplay = '';
for (const summary of summaries) {
const defaultIndicator = summary.isDefault ? neon_js_1.neonChalk.yellow(' (default)') : '';
const roleColors = summary.roles.map(role => this.getRoleColor(role)(role));
accountDisplay += `${neon_js_1.neonChalk.glow('@' + summary.account)}${defaultIndicator}\n`;
accountDisplay += `${neon_js_1.neonChalk.darkCyan('├─')} ${neon_js_1.neonChalk.cyan('Keys:')} ${summary.keyCount} ${neon_js_1.neonSymbols.arrow} ${roleColors.join(', ')}\n`;
accountDisplay += `${neon_js_1.neonChalk.darkCyan('└─')} ${neon_js_1.neonChalk.magenta('Status:')} ${neon_js_1.neonChalk.success('Ready')}\n\n`;
}
console.log((0, neon_js_1.createNeonBox)(accountDisplay.trim(), `${neon_js_1.neonSymbols.star} ACCOUNT WALLET ${neon_js_1.neonSymbols.star}`));
// Account management commands
console.log('');
console.log(neon_js_1.neonChalk.info('Account commands:'));
console.log(neon_js_1.neonChalk.darkCyan(`${neon_js_1.neonSymbols.bullet} Switch default: ${neon_js_1.neonChalk.highlight('beeline accounts switch <account>')}`));
console.log(neon_js_1.neonChalk.darkCyan(`${neon_js_1.neonSymbols.bullet} View details: ${neon_js_1.neonChalk.highlight('beeline accounts info <account>')}`));
console.log(neon_js_1.neonChalk.darkCyan(`${neon_js_1.neonSymbols.bullet} Add account: ${neon_js_1.neonChalk.highlight('beeline login <account>')}`));
}
async switchAccount(keyManager, account) {
if (!account) {
const accounts = await keyManager.listAccounts();
if (accounts.length === 0) {
console.log(neon_js_1.neonChalk.warning('No accounts found in wallet'));
return;
}
const accountPrompt = await inquirer_1.default.prompt([{
type: 'list',
name: 'account',
message: neon_js_1.neonChalk.cyan('Select default account:'),
choices: accounts.map(acc => {
const isDefault = keyManager.getDefaultAccount() === acc;
return {
name: `@${acc}${isDefault ? ' (current default)' : ''}`,
value: acc
};
})
}]);
account = accountPrompt.account;
}
// Clean @ prefix if provided
if (account.startsWith('@')) {
account = account.substring(1);
}
try {
await keyManager.setDefaultAccount(account);
console.log(neon_js_1.neonChalk.success(`${neon_js_1.neonSymbols.check} Default account switched to ${neon_js_1.neonChalk.highlight('@' + account)}`));
const summary = await keyManager.getAccountSummary(account);
if (summary) {
const switchMessage = [
`${neon_js_1.neonChalk.glow('Account switch successful')}`,
``,
`${neon_js_1.neonChalk.cyan('New default:')} @${account}`,
`${neon_js_1.neonChalk.magenta('Available keys:')} ${summary.roles.join(', ')}`,
``,
`${neon_js_1.neonChalk.info('All commands will now use this account by default')}`
].join('\n');
console.log((0, neon_js_1.createNeonBox)(switchMessage, `${neon_js_1.neonSymbols.star} ACCOUNT SWITCHED ${neon_js_1.neonSymbols.star}`));
}
}
catch (error) {
console.log(neon_js_1.neonChalk.error(`${neon_js_1.neonSymbols.cross} Switch failed: ${error instanceof Error ? error.message : 'Unknown error'}`));
}
}
async showAccountInfo(keyManager, account) {
if (!account) {
account = keyManager.getDefaultAccount();
if (!account) {
console.log(neon_js_1.neonChalk.warning('No account specified and no default account set'));
return;
}
}
// Clean @ prefix if provided
if (account.startsWith('@')) {
account = account.substring(1);
}
const summary = await keyManager.getAccountSummary(account);
if (!summary) {
console.log(neon_js_1.neonChalk.error(`${neon_js_1.neonSymbols.cross} Account @${account} not found in wallet`));
return;
}
console.log(neon_js_1.neonChalk.glow(`${neon_js_1.neonSymbols.diamond} Fetching account details...`));
const spinner = (0, neon_js_1.neonSpinner)('Connecting to Hive blockchain');
try {
const hiveClient = new hive_js_1.HiveClient(keyManager);
const accountData = await hiveClient.getAccount(account);
clearInterval(spinner);
process.stdout.write('\r' + ' '.repeat(80) + '\r');
if (!accountData) {
console.log(neon_js_1.neonChalk.warning(`${neon_js_1.neonSymbols.warning} Account exists in wallet but not found on blockchain`));
}
console.log('');
const roleColors = summary.roles.map(role => this.getRoleColor(role)(role));
const infoDisplay = [
`${neon_js_1.neonChalk.cyan('ACCOUNT')} ${neon_js_1.neonSymbols.arrow} ${neon_js_1.neonChalk.highlight('@' + account)}`,
`${neon_js_1.neonChalk.magenta('STATUS')} ${neon_js_1.neonSymbols.arrow} ${summary.isDefault ? neon_js_1.neonChalk.success('Default') : neon_js_1.neonChalk.white('Available')}`,
`${neon_js_1.neonChalk.electric('KEYS')} ${neon_js_1.neonSymbols.arrow} ${roleColors.join(', ')}`,
accountData ? `${neon_js_1.neonChalk.orange('BLOCKCHAIN')} ${neon_js_1.neonSymbols.arrow} ${neon_js_1.neonChalk.success('Verified')}` : `${neon_js_1.neonChalk.orange('BLOCKCHAIN')} ${neon_js_1.neonSymbols.arrow} ${neon_js_1.neonChalk.warning('Not found')}`,
``,
`${neon_js_1.neonChalk.darkCyan('Local wallet contains ' + summary.keyCount + ' key(s) for this account')}`
].join('\n');
console.log((0, neon_js_1.createNeonBox)(infoDisplay, `${neon_js_1.neonSymbols.star} @${account.toUpperCase()} INFO ${neon_js_1.neonSymbols.star}`));
}
catch (error) {
clearInterval(spinner);
process.stdout.write('\r' + ' '.repeat(80) + '\r');
console.log(neon_js_1.neonChalk.warning(`${neon_js_1.neonSymbols.warning} Could not verify on blockchain: ${error instanceof Error ? error.message : 'Unknown error'}`));
const roleColors = summary.roles.map(role => this.getRoleColor(role)(role));
const infoDisplay = [
`${neon_js_1.neonChalk.cyan('ACCOUNT')} ${neon_js_1.neonSymbols.arrow} ${neon_js_1.neonChalk.highlight('@' + account)}`,
`${neon_js_1.neonChalk.magenta('STATUS')} ${neon_js_1.neonSymbols.arrow} ${summary.isDefault ? neon_js_1.neonChalk.success('Default') : neon_js_1.neonChalk.white('Available')}`,
`${neon_js_1.neonChalk.electric('KEYS')} ${neon_js_1.neonSymbols.arrow} ${roleColors.join(', ')}`,
`${neon_js_1.neonChalk.orange('BLOCKCHAIN')} ${neon_js_1.neonSymbols.arrow} ${neon_js_1.neonChalk.warning('Connection failed')}`,
``,
`${neon_js_1.neonChalk.darkCyan('Local wallet contains ' + summary.keyCount + ' key(s) for this account')}`
].join('\n');
console.log((0, neon_js_1.createNeonBox)(infoDisplay, `${neon_js_1.neonSymbols.star} @${account.toUpperCase()} INFO ${neon_js_1.neonSymbols.star}`));
}
}
async removeAccount(keyManager, account, force = false) {
if (!account) {
const accounts = await keyManager.listAccounts();
if (accounts.length === 0) {
console.log(neon_js_1.neonChalk.warning('No accounts found in wallet'));
return;
}
const accountPrompt = await inquirer_1.default.prompt([{
type: 'list',
name: 'account',
message: neon_js_1.neonChalk.warning('Select account to remove:'),
choices: accounts.map(acc => `@${acc}`)
}]);
account = accountPrompt.account;
}
// Clean @ prefix if provided
if (account.startsWith('@')) {
account = account.substring(1);
}
const summary = await keyManager.getAccountSummary(account);
if (!summary) {
console.log(neon_js_1.neonChalk.error(`${neon_js_1.neonSymbols.cross} Account @${account} not found in wallet`));
return;
}
if (!force) {
const confirmPrompt = await inquirer_1.default.prompt([{
type: 'confirm',
name: 'confirm',
message: neon_js_1.neonChalk.warning(`Remove account @${account} and all its keys? This cannot be undone.`),
default: false
}]);
if (!confirmPrompt.confirm) {
console.log(neon_js_1.neonChalk.info('Operation cancelled'));
return;
}
}
try {
// Remove all keys for this account
for (const role of summary.roles) {
await keyManager.removeKey(account, role);
}
console.log(neon_js_1.neonChalk.success(`${neon_js_1.neonSymbols.check} Account @${account} removed from wallet`));
const remaining = await keyManager.listAccounts();
if (remaining.length > 0) {
console.log(neon_js_1.neonChalk.info(`Remaining accounts: ${remaining.map(a => '@' + a).join(', ')}`));
}
else {
console.log(neon_js_1.neonChalk.info('No accounts remaining in wallet'));
console.log(neon_js_1.neonChalk.info('Add account with: ') + neon_js_1.neonChalk.highlight('beeline login <account>'));
}
}
catch (error) {
console.log(neon_js_1.neonChalk.error(`${neon_js_1.neonSymbols.cross} Removal failed: ${error instanceof Error ? error.message : 'Unknown error'}`));
}
}
getRoleColor(role) {
switch (role) {
case 'owner': return neon_js_1.neonChalk.warning;
case 'active': return neon_js_1.neonChalk.electric;
case 'posting': return neon_js_1.neonChalk.orange;
case 'memo': return neon_js_1.neonChalk.pink;
default: return neon_js_1.neonChalk.white;
}
}
}
Accounts.description = 'Manage multiple Hive accounts in your wallet';
Accounts.examples = [
`$ beeline accounts list`,
`$ beeline accounts switch alice`,
`$ beeline accounts info alice`,
`$ beeline accounts remove bob`
];
Accounts.flags = {
format: core_1.Flags.string({
char: 'f',
description: 'output format',
options: ['table', 'json'],
default: 'table'
}),
force: core_1.Flags.boolean({
description: 'force operation without confirmation',
default: false
})
};
Accounts.args = {
action: core_1.Args.string({
description: 'action to perform',
required: false,
default: 'list',
options: ['list', 'switch', 'info', 'remove']
}),
account: core_1.Args.string({
description: 'account name',
required: false
})
};
exports.default = Accounts;
//# sourceMappingURL=accounts.js.map