diff --git a/src/config/index.ts b/src/config/index.ts index 3ece9e38..c37114fe 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -7,7 +7,7 @@ import envPaths from 'env-paths' import allMigrations from './migrations' -const paths = envPaths('entropy-cryptography', { suffix: '' }) +const paths = envPaths('entropyxyz', { suffix: '' }) const CONFIG_PATH = join(paths.config, 'entropy-cli.json') const OLD_CONFIG_PATH = join(process.env.HOME, '.entropy-cli.config') diff --git a/src/flows/manage-accounts/import-key.ts b/src/flows/manage-accounts/helpers/import-key.ts similarity index 96% rename from src/flows/manage-accounts/import-key.ts rename to src/flows/manage-accounts/helpers/import-key.ts index f2f8fcd7..17e9f848 100644 --- a/src/flows/manage-accounts/import-key.ts +++ b/src/flows/manage-accounts/helpers/import-key.ts @@ -1,4 +1,4 @@ -import { debug } from '../../common/utils' +import { debug } from '../../../common/utils' // import { mnemonicValidate, mnemonicToMiniSecret } from '@polkadot/util-crypto' export const importQuestions = [ diff --git a/src/flows/manage-accounts/index.ts b/src/flows/manage-accounts/index.ts index 842ebd65..b911afa2 100644 --- a/src/flows/manage-accounts/index.ts +++ b/src/flows/manage-accounts/index.ts @@ -1,19 +1,20 @@ import inquirer from 'inquirer' +import { debug, print } from '../../common/utils' import { newKey } from './new-key' import { selectAccount } from './select-account' -import { debug, print } from '../../common/utils' +import { listAccounts } from './list' const actions = { 'Create/Import Account': newKey, 'Select Account': selectAccount, - 'List Accounts': async (config) => { - const accountsArray = Array.isArray(config.accounts) ? config.accounts : [config.accounts] - accountsArray.forEach((account) => print({ - name: account.name, - address: account.address, - verifyingKeys: account?.data?.admin?.verifyingKeys - })) - if (!accountsArray.length) console.error('There are currently no accounts available, please create or import your new account using the Manage Accounts feature') + 'List Accounts': (config) => { + try { + const accountsArray = listAccounts(config) + accountsArray?.forEach(account => print(account)) + return + } catch (error) { + console.error(error.message); + } }, } diff --git a/src/flows/manage-accounts/list.ts b/src/flows/manage-accounts/list.ts new file mode 100644 index 00000000..63ae5995 --- /dev/null +++ b/src/flows/manage-accounts/list.ts @@ -0,0 +1,12 @@ +export function listAccounts (config) { + const accountsArray = Array.isArray(config.accounts) ? config.accounts : [config.accounts] + if (!accountsArray.length) + throw new Error( + 'There are currently no accounts available, please create or import your new account using the Manage Accounts feature' + ) + return accountsArray.map((account) => ({ + name: account.name, + address: account.address, + verifyingKeys: account?.data?.admin?.verifyingKeys + })) +} \ No newline at end of file diff --git a/src/flows/manage-accounts/new-key.ts b/src/flows/manage-accounts/new-key.ts index a2d55d48..39161c19 100644 --- a/src/flows/manage-accounts/new-key.ts +++ b/src/flows/manage-accounts/new-key.ts @@ -2,7 +2,7 @@ import inquirer from 'inquirer' import { randomAsHex } from '@polkadot/util-crypto' // @ts-ignore import Keyring from '@entropyxyz/sdk/keys' -import { importQuestions } from './import-key' +import { importQuestions } from './helpers/import-key' // import * as passwordFlow from '../password' import { debug, print } from '../../common/utils' diff --git a/src/types/index.ts b/src/types/index.ts index 6811e13c..791cdb5c 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,3 +1,8 @@ +export interface EntropyConfig { + accounts: EntropyAccountConfig[] + endpoints: { dev: string; 'test-net': string } + 'migration-version': string +} export interface EntropyAccountConfig { name: string address: string diff --git a/tests/manage-accounts.test.ts b/tests/manage-accounts.test.ts new file mode 100644 index 00000000..a4540652 --- /dev/null +++ b/tests/manage-accounts.test.ts @@ -0,0 +1,47 @@ +import { EntropyAccountConfig, EntropyConfig } from 'src/types' +import test from 'tape' +import { charlieStashAddress, charlieStashSeed } from './testing-utils/constants' +import { listAccounts } from 'src/flows/manage-accounts/list' + +test('List Accounts', async t => { + const account: EntropyAccountConfig = { + name: 'Test Config', + address: charlieStashAddress, + data: { + seed: charlieStashSeed, + admin: { + verifyingKeys: ['this-is-a-verifying-key'], + seed: charlieStashSeed, + address: charlieStashAddress, + path: '//Charlie' + } + } + } + const config: EntropyConfig = { + accounts: [account], + endpoints: { + dev: 'ws://127.0.0.1:9944', + 'test-net': 'wss://testnet.entropy.xyz', + }, + 'migration-version': '0' + } + + const accountsArray = listAccounts(config) + + t.deepEqual(accountsArray, [{ + name: account.name, + address: account.address, + verifyingKeys: account.data.admin.verifyingKeys + }]) + + // Resetting accounts on config to test for empty list + config.accounts = [] + try { + listAccounts(config) + } catch (error) { + const msg = error.message + t.equal(msg, 'There are currently no accounts available, please create or import your new account using the Manage Accounts feature') + } + + t.end() +}) \ No newline at end of file diff --git a/tests/testing-utils/constants.ts b/tests/testing-utils/constants.ts new file mode 100644 index 00000000..9fb1b88c --- /dev/null +++ b/tests/testing-utils/constants.ts @@ -0,0 +1,5 @@ +export const charlieStashAddress = + '5Ck5SLSHYac6WFt5UZRSsdJjwmpSZq85fd5TRNAdZQVzEAPT' + +export const charlieStashSeed = + '0x66256c4e2f90e273bf387923a9a7860f2e9f47a1848d6263de512f7fb110fc08' \ No newline at end of file