Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NayNay] Account List Update + Testing #139

Merged
merged 2 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { debug } from '../../common/utils'
import { debug } from '../../../common/utils'
// import { mnemonicValidate, mnemonicToMiniSecret } from '@polkadot/util-crypto'

export const importQuestions = [
Expand Down
19 changes: 10 additions & 9 deletions src/flows/manage-accounts/index.ts
Original file line number Diff line number Diff line change
@@ -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);
}
},
}

Expand Down
12 changes: 12 additions & 0 deletions src/flows/manage-accounts/list.ts
Original file line number Diff line number Diff line change
@@ -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
}))
}
2 changes: 1 addition & 1 deletion src/flows/manage-accounts/new-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
5 changes: 5 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
47 changes: 47 additions & 0 deletions tests/manage-accounts.test.ts
Original file line number Diff line number Diff line change
@@ -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()
})
5 changes: 5 additions & 0 deletions tests/testing-utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const charlieStashAddress =
'5Ck5SLSHYac6WFt5UZRSsdJjwmpSZq85fd5TRNAdZQVzEAPT'

export const charlieStashSeed =
'0x66256c4e2f90e273bf387923a9a7860f2e9f47a1848d6263de512f7fb110fc08'