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] File Restructure: Accounts Restructure #215

Merged
merged 33 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4e0532a
[NayNay] File Restructure: Accounts Restructure
rh0delta Aug 5, 2024
af58213
Merge branch 'naynay/file-restructure' into naynay/accounts-restructure
rh0delta Aug 5, 2024
e9411ce
updated new account, list account and selected account with new file …
rh0delta Aug 6, 2024
6d46831
finished updated manage accoutns stuff, updated tests too
rh0delta Aug 6, 2024
8063677
updated register flow and moved methods to accounts namespace
rh0delta Aug 6, 2024
118c5a7
updated register tests
rh0delta Aug 6, 2024
6dfd8b2
cleanup from smoke test
rh0delta Aug 6, 2024
9d22f6a
updated changelog
rh0delta Aug 7, 2024
b59a656
Merge branch 'naynay/file-restructure' into naynay/accounts-restructure
rh0delta Aug 19, 2024
1b7aa6f
start refactor
mixmix Aug 27, 2024
ec0609f
WIP: part way refactored account stuff
mixmix Aug 28, 2024
3aa419b
cleaning up the cleanup for accounts restructure;
rh0delta Aug 28, 2024
9cc1c67
forgot something
rh0delta Aug 28, 2024
249ca3e
updated accounts restructure, continuing from mixs changes
rh0delta Aug 28, 2024
6cd2e9f
Update main.ts
rh0delta Aug 28, 2024
b5af231
Update manage-accounts.test.ts
rh0delta Aug 28, 2024
036936b
Merge branch 'naynay/file-restructure' of github.com:entropyxyz/cli i…
mixmix Aug 29, 2024
9cb1b81
fixups
mixmix Aug 29, 2024
dbbb777
WIP
mixmix Aug 29, 2024
378fffa
fixups
mixmix Aug 29, 2024
d971d4d
compleeeete
mixmix Aug 29, 2024
50ab1e1
get working for fresh install. see WIP
mixmix Aug 29, 2024
b013ce3
fixed fresh install and using tui, might have fixed cli not sure
rh0delta Aug 29, 2024
2272689
updated initialization of entropy and formt of how we instantiate com…
rh0delta Sep 9, 2024
35eecee
Merge branch 'naynay/file-restructure' into naynay/accounts-restructure
rh0delta Sep 16, 2024
6951167
skipping faucet test for now, shenanigans occurring
rh0delta Sep 16, 2024
96d273c
fixed faucet test
rh0delta Sep 16, 2024
930d149
updated tests;
rh0delta Sep 16, 2024
6d0ea12
Update command.ts
rh0delta Sep 17, 2024
c941e17
pr review updates
rh0delta Sep 17, 2024
40774cc
Merge branch 'naynay/accounts-restructure' of https://github.com/entr…
rh0delta Sep 17, 2024
3e091f9
Update src/cli.ts
mixmix Sep 17, 2024
183e200
account-restructure tweaks (#226)
mixmix Sep 18, 2024
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ Version header format: `[version] Name - year-month-day (entropy-core compatibil
- new: './src/transfer' - new file structure for our CLI/TUI flows
- new: './src/transfer/command.ts' - main entry file for transfer command for tui/cli
- new: './src/transfer/utils.ts' - utilities and helper methods for all things transfer
- new: './src/accounts' - new file structure for our CLI/TUI flows
- new: './src/accounts/command.ts' - main entry file for accounts command for tui/cli
- new: './src/accounts/utils.ts' - utilities and helper methods for all things accounts

### Changed

Expand All @@ -43,6 +46,8 @@ Version header format: `[version] Name - year-month-day (entropy-core compatibil
- merged user + dev program folders + tests
- removed flows/balance/*.ts directory with file restructure
- removed flows/entropyTransfer/*.ts directory with file restructure
- removed flows/manage-accounts/*/*.ts directory with file restructure
- removed flows/register/*.ts directory with file restructure


### Broke
Expand Down
142 changes: 142 additions & 0 deletions src/account/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import Entropy from "@entropyxyz/sdk"
import { Command, Option } from 'commander'
import { EntropyAccount } from "./main";
import { ACCOUNTS_CONTENT } from './constants'
import * as config from '../config'
import { cliWrite, currentAccountAddressOption, endpointOption, loadEntropy, passwordOption } from "../common/utils-cli";
import { findAccountNameByAddress, updateConfig } from "src/common/utils";

export function entropyAccountCommand () {
const accountCommand = new Command('account')
.description('Commands to work with accounts on the Entropy Network')
.addCommand(entropyAccountCreate())
.addCommand(entropyAccountImport())
.addCommand(entropyAccountList())
.addCommand(entropyAccountRegister())

return accountCommand
}

function entropyAccountCreate () {
const accountCreateCommand = new Command('create')
.alias('new')
.description('Create a new entropy account from scratch. Output is JSON of form {name, address}')
.addOption(passwordOption())
.argument('<name>', 'A user friendly name for your new account.')
.addOption(
new Option(
'--path',
'Derivation path'
).default(ACCOUNTS_CONTENT.path.default)
)
.action(async (name, opts) => {
const { path } = opts
const newAccount = await EntropyAccount.create({ name, path })

await persistAndSelectNewAccount(newAccount)

cliWrite({
name: newAccount.name,
address: newAccount.address
})
process.exit(0)
})
return accountCreateCommand
}

function entropyAccountImport () {
const accountImportCommand = new Command('import')
.description('Import an existing entropy account from seed. Output is JSON of form {name, address}')
.addOption(passwordOption())
.argument('<name>', 'A user friendly name for your new account.')
.argument('<seed>', 'The seed for the account you are importing')
.addOption(
new Option(
'--path',
'Derivation path'
).default(ACCOUNTS_CONTENT.path.default)
)
.action(async (name, seed, opts) => {
const { path } = opts
const newAccount = await EntropyAccount.import({ name, seed, path })

await persistAndSelectNewAccount(newAccount)

cliWrite({
name: newAccount.name,
address: newAccount.address
})
process.exit(0)
})
return accountImportCommand
}

async function persistAndSelectNewAccount (newAccount) {
const storedConfig = await config.get()
const { accounts } = storedConfig

const isExistingName = accounts.find(account => account.name === newAccount.name)
if (isExistingName) {
throw Error(`An account with name "${newAccount.name}" already exists. Choose a different name`)
}

accounts.push(newAccount)
await updateConfig(storedConfig, {
accounts,
selectedAccount: newAccount.address
})
}

function entropyAccountList () {
const accountListCommand = new Command('list')
.alias('ls')
.description('List all accounts. Output is JSON of form [{ name, address, verifyingKeys }]')
.action(async () => {
// TODO: test if it's an encrypted account, if no password provided, throw because later on there's no protection from a prompt coming up
const storedConfig = await config.get()
const accounts = EntropyAccount.list(storedConfig)
cliWrite(accounts)
process.exit(0)
})
return accountListCommand
}

/* register */
function entropyAccountRegister () {
const accountRegisterCommand = new Command('register')
accountRegisterCommand
.description('Register an entropy account with a program')
.addOption(passwordOption())
.addOption(endpointOption())
.addOption(currentAccountAddressOption())
// Removing these options for now until we update the design to accept program configs
// .addOption(
// new Option(
// '-pointer, --pointer',
// 'Program pointer of program to be used for registering'
// )
// )
// .addOption(
// new Option(
// '-data, --program-data',
// 'Path to file containing program data in JSON format'
// )
// )
.action(async (opts) => {
const storedConfig = await config.get()
const { accounts } = storedConfig
const entropy: Entropy = await loadEntropy(opts.account, opts.endpoint)
const AccountsService = new EntropyAccount(entropy, opts.endpoint)
const accountToRegister = findAccountNameByAddress(accounts, opts.account)
if (!accountToRegister) {
throw new Error('AccountError: Unable to register non-existent account')
}
const updatedAccount = await AccountsService.registerAccount(accountToRegister)
const arrIdx = accounts.indexOf(accountToRegister)
accounts.splice(arrIdx, 1, updatedAccount)
await updateConfig(storedConfig, { accounts, selectedAccount: updatedAccount.address })
cliWrite("Your address" + updatedAccount.address + "has been successfully registered.")
mixmix marked this conversation as resolved.
Show resolved Hide resolved
process.exit(0)
})
return accountRegisterCommand
}
36 changes: 36 additions & 0 deletions src/account/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export const FLOW_CONTEXT = 'ENTROPY_ACCOUNTS'

export const ACCOUNTS_CONTENT = {
seed: {
name: 'seed',
message: 'Enter seed:',
invalidSeed: 'Seed provided is not valid'
},
path: {
name: 'path',
message: 'derivation path:',
default: 'none',
},
importKey: {
name: 'importKey',
message: 'Would you like to import your own seed?',
default: false
},
name: {
name: 'name',
default: 'My Key',
},
selectAccount: {
name: "selectedAccount",
message: "Choose account:",
},
interactionChoice: {
name: 'interactionChoice',
choices: [
{ name: 'Create/Import Account', value: 'create-import' },
{ name: 'Select Account', value: 'select-account' },
{ name: 'List Accounts', value: 'list-account' },
{ name: 'Exit to Main Menu', value: 'exit' }
]
}
}
89 changes: 89 additions & 0 deletions src/account/interaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import inquirer from "inquirer";
import Entropy from "@entropyxyz/sdk";

import { findAccountNameByAddress, print } from "../common/utils"
import { EntropyConfig } from "../config/types";
import { EntropyAccount } from './main'

import {
manageAccountsQuestions,
newAccountQuestions,
selectAccountQuestions
} from "./utils"


export async function entropyManageAccounts (endpoint: string, storedConfig: EntropyConfig) {
mixmix marked this conversation as resolved.
Show resolved Hide resolved
const { accounts } = storedConfig
const { interactionChoice } = await inquirer.prompt(manageAccountsQuestions)

switch (interactionChoice) {

case 'create-import': {
const answers = await inquirer.prompt(newAccountQuestions)
const { name, path, importKey } = answers
let { seed } = answers
if (importKey && seed.includes('#debug')) {
// isDebugMode = true
seed = seed.split('#debug')[0]
}
const newAccount = seed
? await EntropyAccount.import({ seed, name, path })
: await EntropyAccount.create({ name, path })
accounts.push(newAccount)
mixmix marked this conversation as resolved.
Show resolved Hide resolved

return {
accounts,
selectedAccount: newAccount.address
}
}

case 'select-account': {
if (!accounts.length) {
console.error('There are currently no accounts available, please create or import a new account using the Manage Accounts feature')
return
}
const { selectedAccount } = await inquirer.prompt(selectAccountQuestions(accounts))
print('Current selected account is ' + selectedAccount)

return {
accounts: storedConfig.accounts,
selectedAccount: selectedAccount.address
}
}

case 'list-account': {
try {
EntropyAccount.list({ accounts })
.forEach((account) => print(account))
} catch (error) {
console.error(error.message.split('AccountsError: ')[1])
}
return
}

case 'exit': {
return 'exit'
}

default:
throw new Error('AccountsError: Unknown interaction action')
}
}

export async function entropyRegister (entropy: Entropy, endpoint: string, storedConfig: EntropyConfig): Promise<Partial<EntropyConfig>> {
const AccountService = new EntropyAccount(entropy, endpoint)

const { accounts, selectedAccount } = storedConfig
const currentAccount = findAccountNameByAddress(accounts, selectedAccount)
if (!currentAccount) {
print("No account selected to register")
return;
}
print("Attempting to register the address:", currentAccount.address)
const updatedAccount = await AccountService.registerAccount(currentAccount)
const arrIdx = accounts.indexOf(currentAccount)
accounts.splice(arrIdx, 1, updatedAccount)
print("Your address", updatedAccount.address, "has been successfully registered.")

return { accounts, selectedAccount }
}
Loading
Loading