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

mixmix/custom-config #264

Merged
merged 46 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
fb4691b
fix cli opts bug with multiple accountOptions
mixmix Oct 3, 2024
6eccbba
drop async argParser function! (unsupported)
mixmix Oct 3, 2024
ea23881
add setSelectedAccount
mixmix Oct 3, 2024
ebaf352
Merge branch 'dev' into mixmix/fix-cli-opts
mixmix Oct 14, 2024
ef005b3
make custom config possible for CLI usage
mixmix Oct 14, 2024
3e1df5c
Merge branch 'dev' of github.com:entropyxyz/cli into mixmix/custom-co…
mixmix Oct 22, 2024
8236b29
cli descriptions refactor
mixmix Oct 23, 2024
61df302
wip
mixmix Oct 23, 2024
5bc32b4
Merge branch 'dev' of github.com:entropyxyz/cli into mixmix/custom-co…
mixmix Nov 5, 2024
8ce3d4c
fixups
mixmix Nov 5, 2024
0e607b8
fixup e2e.cli.sh, rm test.only
mixmix Nov 6, 2024
db201c6
Merge branch 'dev' of github.com:entropyxyz/cli into mixmix/custom-co…
mixmix Nov 6, 2024
94ff8b8
Merge branch 'dev' of github.com:entropyxyz/cli into mixmix/custom-co…
mixmix Nov 6, 2024
30867e3
remove default config path from all config functions, fixups
mixmix Nov 6, 2024
2f6ea9e
Merge branch 'dev' into mixmix/custom-config
mixmix Nov 7, 2024
3cabc31
[NayNay] Testing Mix's Custom Config (#290)
rh0delta Nov 13, 2024
4da3244
make print.error, print.success etc
mixmix Nov 13, 2024
40af3d5
Merge branch 'dev' of github.com:entropyxyz/cli into mixmix/custom-co…
mixmix Nov 13, 2024
7cd297f
fix tui saving accounts
mixmix Nov 13, 2024
c5e1254
very WIP refactor
mixmix Nov 14, 2024
840b236
wire it up
mixmix Nov 14, 2024
1801b3d
fixups
mixmix Nov 14, 2024
02f6022
remove .gitignore change
mixmix Nov 14, 2024
2d7291c
extract loadEntropy
mixmix Nov 14, 2024
b4f7168
arg... wip notes
mixmix Nov 14, 2024
ed7a37d
Merge branch 'dev' of github.com:entropyxyz/cli into mixmix/load-entropy
mixmix Nov 20, 2024
0177428
wip
mixmix Nov 20, 2024
f43452c
fixups
mixmix Nov 21, 2024
f03ceae
refactor package.json scripts
mixmix Nov 21, 2024
b76e2cf
replace makeSeed with polkadot randomAsHex
mixmix Nov 21, 2024
009b33a
Merge branch 'mixmix/refactor-scripts' into mixmix/load-entropy
mixmix Nov 21, 2024
23e6d46
Merge branch 'dev' of github.com:entropyxyz/cli into mixmix/load-entropy
mixmix Nov 21, 2024
427109c
extract config persistence from loadEntropy
mixmix Nov 27, 2024
c66d76b
fix typo
mixmix Nov 27, 2024
a420884
rename config types
mixmix Nov 27, 2024
12fcf22
Merge branch 'mixmix/config-types' into mixmix/load-entropy
mixmix Nov 27, 2024
f23942e
add config types to test
mixmix Nov 27, 2024
d7d8f1f
Merge branch 'mixmix/config-types' into mixmix/load-entropy
mixmix Nov 27, 2024
3aff2f0
Merge branch 'mixmix/load-entropy' into mixmix/custom-config
mixmix Nov 28, 2024
0b85ec5
fix tests
mixmix Nov 28, 2024
892cfc6
fix "account register" command bug
mixmix Nov 28, 2024
ad27918
Merge remote-tracking branch 'origin/dev' into mixmix/load-entropy
mixmix Dec 2, 2024
2f06bd6
Merge branch 'mixmix/load-entropy' into mixmix/custom-config
mixmix Dec 2, 2024
4c2c00a
Merge remote-tracking branch 'origin/dev' into mixmix/custom-config
mixmix Dec 10, 2024
d121d72
change config.set signature, rm config.getSync
mixmix Dec 10, 2024
94929bd
fix tests
mixmix Dec 10, 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
23 changes: 14 additions & 9 deletions src/account/command.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Entropy from "@entropyxyz/sdk"
import { Command, Option } from 'commander'

import { EntropyAccount } from "./main";
import { selectAndPersistNewAccount, persistVerifyingKeyToAccount, generateAccountDataForPrint } from "./utils";
import { ACCOUNTS_CONTENT } from './constants'
import * as config from '../config'
import { accountOption, endpointOption, cliWrite } from "../common/utils-cli";
import { accountOption, configOption, endpointOption, cliWrite } from "../common/utils-cli";
import { loadEntropyCli } from "../common/load-entropy"

export function entropyAccountCommand () {
Expand All @@ -24,17 +25,18 @@ function entropyAccountCreate () {
.alias('new')
.description('Create a new entropy account from scratch. Output is JSON of form {name, address}')
.argument('<name>', 'A user friendly name for your new account.')
.addOption(configOption())
.addOption(
new Option(
'--path',
'Derivation path'
).default(ACCOUNTS_CONTENT.path.default)
)
.action(async (name, opts) => {
const { path } = opts
const { config: configPath, path } = opts
const newAccount = await EntropyAccount.create({ name, path })

await selectAndPersistNewAccount(newAccount)
await selectAndPersistNewAccount(configPath, newAccount)

cliWrite(generateAccountDataForPrint(newAccount))

Expand All @@ -47,17 +49,18 @@ function entropyAccountImport () {
.description('Import an existing entropy account from seed. Output is JSON of form {name, address}')
.argument('<name>', 'A user friendly name for your new account.')
.argument('<seed>', 'The seed for the account you are importing')
.addOption(configOption())
.addOption(
new Option(
'--path',
'Derivation path'
).default(ACCOUNTS_CONTENT.path.default)
)
.action(async (name, seed, opts) => {
const { path } = opts
const { config: configPath, path } = opts
const newAccount = await EntropyAccount.import({ name, seed, path })

await selectAndPersistNewAccount(newAccount)
await selectAndPersistNewAccount(configPath, newAccount)

cliWrite({
name: newAccount.name,
Expand All @@ -72,9 +75,9 @@ function entropyAccountList () {
return 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 accounts = await config.get()
.addOption(configOption())
.action(async (opts) => {
const accounts = await config.get(opts.config)
.then(storedConfig => EntropyAccount.list(storedConfig))
.catch((err) => {
if (err.message.includes('currently no accounts')) return []
Expand All @@ -92,6 +95,7 @@ function entropyAccountRegister () {
return new Command('register')
.description('Register an entropy account with a program')
.addOption(accountOption())
.addOption(configOption())
.addOption(endpointOption())
// Removing these options for now until we update the design to accept program configs
// .addOption(
Expand All @@ -111,7 +115,8 @@ function entropyAccountRegister () {
const accountService = new EntropyAccount(entropy, opts.endpoint)

const verifyingKey = await accountService.register()
await persistVerifyingKeyToAccount(verifyingKey, opts.account)
// TODO: handle error
await persistVerifyingKeyToAccount(opts.config, verifyingKey, opts.account)

cliWrite(verifyingKey)
process.exit(0)
Expand Down
24 changes: 13 additions & 11 deletions src/account/interaction.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import inquirer from "inquirer";
import Entropy from "@entropyxyz/sdk";
import yoctoSpinner from "yocto-spinner";

import { EntropyAccount } from './main'
import { EntropyTuiOptions } from '../types'
import { selectAndPersistNewAccount, persistVerifyingKeyToAccount, generateAccountDataForPrint } from "./utils";
import { findAccountByAddressOrName, print } from "../common/utils"
import { EntropyConfig } from "../config/types";
Expand All @@ -12,13 +14,11 @@ import {
accountNewQuestions,
accountSelectQuestions
} from "./utils"
import { ERROR_RED } from "src/common/constants";
import yoctoSpinner from "yocto-spinner";

/*
* @returns partialConfigUpdate | "exit" | undefined
*/
export async function entropyAccount (endpoint: string, storedConfig: EntropyConfig) {
export async function entropyAccount (opts: EntropyTuiOptions, storedConfig: EntropyConfig) {
const { accounts } = storedConfig
const { interactionChoice } = await inquirer.prompt(accountManageQuestions)

Expand All @@ -37,7 +37,7 @@ export async function entropyAccount (endpoint: string, storedConfig: EntropyCon
? await EntropyAccount.import({ seed, name, path })
: await EntropyAccount.create({ name, path })

await selectAndPersistNewAccount(newAccount)
await selectAndPersistNewAccount(opts.config, newAccount)
print(generateAccountDataForPrint(newAccount))
return
}
Expand All @@ -48,8 +48,8 @@ export async function entropyAccount (endpoint: string, storedConfig: EntropyCon
return
}
const { selectedAccount } = await inquirer.prompt(accountSelectQuestions(accounts))
await config.setSelectedAccount(selectedAccount)

await config.setSelectedAccount(opts.config, selectedAccount)

print('Current selected account is:')
print({ name: selectedAccount.name, address: selectedAccount.address })
Expand Down Expand Up @@ -77,8 +77,9 @@ export async function entropyAccount (endpoint: string, storedConfig: EntropyCon

const registrationSpinner = yoctoSpinner()
const SPINNER_TEXT = 'Registering account…'
export async function entropyRegister (entropy: Entropy, endpoint: string, storedConfig: EntropyConfig): Promise<Partial<EntropyConfig>> {
const accountService = new EntropyAccount(entropy, endpoint)

export async function entropyRegister (entropy: Entropy, opts: EntropyTuiOptions, storedConfig: EntropyConfig): Promise<Partial<EntropyConfig>> {
const accountService = new EntropyAccount(entropy, opts.endpoint)

const { accounts, selectedAccount } = storedConfig
const account = findAccountByAddressOrName(accounts, selectedAccount)
Expand All @@ -94,17 +95,18 @@ export async function entropyRegister (entropy: Entropy, endpoint: string, store
try {
if (!registrationSpinner.isSpinning) registrationSpinner.start()
const verifyingKey = await accountService.register()
await persistVerifyingKeyToAccount(verifyingKey, account.address)
await persistVerifyingKeyToAccount(opts.config, verifyingKey, account.address)

if (registrationSpinner.isSpinning) registrationSpinner.stop()
print("Your address", account.address, "has been successfully registered.")
} catch (error) {
const endpointErrorMessageToMatch = 'Extrinsic registry.register expects 3 arguments, got 2'
registrationSpinner.text = 'Registration has failed...'
if (registrationSpinner.isSpinning) registrationSpinner.stop()
if (error.message.includes(endpointErrorMessageToMatch)) {
console.error(ERROR_RED + 'GenericError: Incompatible endpoint, expected core version 0.3.0, got 0.2.0')
print.error('GenericError: Incompatible endpoint, expected core version 0.3.0, got 0.2.0')
return
}
console.error(ERROR_RED + 'RegisterError:', error.message);
print.error('RegisterError:', error.message)
}
}
17 changes: 10 additions & 7 deletions src/account/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { EntropyConfigAccount } from "../config/types";
import * as config from "../config";
import { generateAccountChoices, findAccountByAddressOrName } from '../common/utils';

export async function selectAndPersistNewAccount (newAccount: EntropyConfigAccount) {
const storedConfig = await config.get()
export async function selectAndPersistNewAccount (configPath: string, newAccount: EntropyConfigAccount) {
const storedConfig = await config.get(configPath)
const { accounts } = storedConfig

const isExistingName = accounts.find(account => account.name === newAccount.name)
Expand All @@ -22,22 +22,25 @@ export async function selectAndPersistNewAccount (newAccount: EntropyConfigAccou

// persist to config, set selectedAccount
accounts.push(newAccount)
await config.set({
await config.set(configPath, {
...storedConfig,
selectedAccount: newAccount.name
selectedAccount: newAccount.address
})
}

export async function persistVerifyingKeyToAccount (verifyingKey: string, accountNameOrAddress: string) {
const storedConfig = await config.get()
export async function persistVerifyingKeyToAccount (configPath: string, verifyingKey: string, accountNameOrAddress: string) {
const storedConfig = await config.get(configPath)
const { accounts } = storedConfig

const account = findAccountByAddressOrName(accounts, accountNameOrAddress)
if (!account) throw Error(`Unable to persist verifyingKey "${verifyingKey}" to unknown account "${accountNameOrAddress}"`)

// persist to config, set selectedAccount
account.data.registration.verifyingKeys.push(verifyingKey)
await config.set(storedConfig)
await config.set(configPath, {
...storedConfig,
selectedAccount: account.name
})
}

function validateSeedInput (seed) {
Expand Down
7 changes: 4 additions & 3 deletions src/balance/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Command } from "commander";
import Entropy from "@entropyxyz/sdk";

import { EntropyBalance } from "./main";
import { endpointOption, cliWrite } from "../common/utils-cli";
import { configOption, endpointOption, cliWrite } from "../common/utils-cli";
import { findAccountByAddressOrName, getTokenDetails, nanoBitsToBits, round } from "../common/utils";
import { loadEntropyCli } from "../common/load-entropy"
import * as config from "../config";
Expand All @@ -11,17 +11,18 @@ export function entropyBalanceCommand () {
const balanceCommand = new Command('balance')
balanceCommand
.description('Command to retrieive the balance of an account on the Entropy Network')
.argument('<account>', [
.argument('account <address|name>', [
'The address an account address whose balance you want to query.',
'Can also be the human-readable name of one of your accounts'
].join(' '))
.addOption(configOption())
.addOption(endpointOption())
.action(async (account, opts) => {
const entropy: Entropy = await loadEntropyCli({ account, ...opts })
const balanceService = new EntropyBalance(entropy, opts.endpoint)
const { decimals, symbol } = await getTokenDetails(entropy)

const { accounts } = await config.get()
const { accounts } = await config.get(opts.config)
const address = findAccountByAddressOrName(accounts, account)?.address

const nanoBalance = await balanceService.getBalance(address)
Expand Down
8 changes: 5 additions & 3 deletions src/balance/interaction.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { findAccountByAddressOrName, getTokenDetails, print, round, nanoBitsToBits } from "src/common/utils"
import { EntropyBalance } from "./main"
import { findAccountByAddressOrName, getTokenDetails, print, round, nanoBitsToBits } from "../common/utils"

export async function entropyBalance (entropy, endpoint, storedConfig) {
import { EntropyTuiOptions } from '../types'

export async function entropyBalance (entropy, opts: EntropyTuiOptions, storedConfig) {
try {
// grabbing decimals from chain spec as that is the source of truth for the value
const { decimals, symbol } = await getTokenDetails(entropy)
const balanceService = new EntropyBalance(entropy, endpoint)
const balanceService = new EntropyBalance(entropy, opts.endpoint)
const address = findAccountByAddressOrName(storedConfig.accounts, storedConfig.selectedAccount)?.address
const nanoBalance = await balanceService.getBalance(address)
const balance = round(nanoBitsToBits(nanoBalance, decimals))
Expand Down
10 changes: 4 additions & 6 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ cli

// print entropy help and exit
cli.help()

// tuiAction(opts)
// NOTE: this doesn't quite work, because -a, -e are not defined as options
// and if we do put them in here it gets a bit confusing
})

// set up config file, run migrations
.hook('preAction', async () => {
return config.init()
.hook('preAction', async (thisCommand, actionCommand) => {
const { config: configPath } = actionCommand.opts()

if (configPath) await config.init(configPath)
})

cli.parseAsync()
4 changes: 3 additions & 1 deletion src/common/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const ENTROPY_ENDPOINT_DEFAULT = 'wss://testnet.entropy.xyz/'

// ASCII Colors for Logging to Console
export const ERROR_RED = '\u001b[31m'
export const SUCCESS_GREEN = '\u001b[32m'
export const INFO_BLUE = '\u001b[34m'
export const INFO_BLUE = '\u001b[34m'
Loading
Loading