From cc63c728703495ce1ed1ba32adaf6a964e2937b5 Mon Sep 17 00:00:00 2001 From: mixmix Date: Thu, 26 Sep 2024 13:55:47 +1200 Subject: [PATCH 1/2] refactor entropy register so CLI/TUI are similar --- src/account/command.ts | 28 ++++++---------------------- src/account/interaction.ts | 19 +++++++++---------- src/account/main.ts | 23 +---------------------- src/account/utils.ts | 19 +++++++++++++++---- 4 files changed, 31 insertions(+), 58 deletions(-) diff --git a/src/account/command.ts b/src/account/command.ts index 06cf3a5..a502a85 100644 --- a/src/account/command.ts +++ b/src/account/command.ts @@ -1,11 +1,10 @@ import Entropy from "@entropyxyz/sdk" import { Command, Option } from 'commander' import { EntropyAccount } from "./main"; -import { selectAndPersistNewAccount } from "./utils"; +import { selectAndPersistNewAccount, addVerifyingKeyToAccountAndSelect } from "./utils"; import { ACCOUNTS_CONTENT } from './constants' import * as config from '../config' import { cliWrite, accountOption, endpointOption, loadEntropy, passwordOption } from "../common/utils-cli"; -import { findAccountByAddressOrName } from "src/common/utils"; export function entropyAccountCommand () { return new Command('account') @@ -102,28 +101,13 @@ function entropyAccountRegister () { // ) // ) .action(async (opts) => { - const { account, endpoint, /* password */ } = opts - const storedConfig = await config.get() - const { accounts } = storedConfig - const accountToRegister = findAccountByAddressOrName(accounts, account) - if (!accountToRegister) { - throw new Error('AccountError: Unable to register non-existent account') - } - - const entropy: Entropy = await loadEntropy(accountToRegister.address, endpoint) - const accountService = new EntropyAccount(entropy, endpoint) - const updatedAccount = await accountService.registerAccount(accountToRegister) + // NOTE: loadEntropy throws if it can't find opts.account + const entropy: Entropy = await loadEntropy(opts.account, opts.endpoint) + const accountService = new EntropyAccount(entropy, opts.endpoint) - const arrIdx = accounts.indexOf(accountToRegister) - accounts.splice(arrIdx, 1, updatedAccount) - await config.set({ - ...storedConfig, - accounts, - selectedAccount: updatedAccount.address - }) + const verifyingKey = await accountService.register() + await addVerifyingKeyToAccountAndSelect(verifyingKey, opts.account) - const verifyingKeys = updatedAccount?.data?.registration?.verifyingKeys - const verifyingKey = verifyingKeys[verifyingKeys.length - 1] cliWrite(verifyingKey) process.exit(0) }) diff --git a/src/account/interaction.ts b/src/account/interaction.ts index d3277c0..bf1c599 100644 --- a/src/account/interaction.ts +++ b/src/account/interaction.ts @@ -2,7 +2,7 @@ import inquirer from "inquirer"; import Entropy from "@entropyxyz/sdk"; import { EntropyAccount } from './main' -import { selectAndPersistNewAccount } from "./utils"; +import { selectAndPersistNewAccount, addVerifyingKeyToAccountAndSelect } from "./utils"; import { findAccountByAddressOrName, print } from "../common/utils" import { EntropyConfig } from "../config/types"; import * as config from "../config"; @@ -77,16 +77,15 @@ export async function entropyRegister (entropy: Entropy, endpoint: string, store const accountService = new EntropyAccount(entropy, endpoint) const { accounts, selectedAccount } = storedConfig - const currentAccount = findAccountByAddressOrName(accounts, selectedAccount) - if (!currentAccount) { + const account = findAccountByAddressOrName(accounts, selectedAccount) + if (!account) { print("No account selected to register") - return; + 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 } + print("Attempting to register the address:", account.address) + const verifyingKey = await accountService.register() + await addVerifyingKeyToAccountAndSelect(verifyingKey, account.address) + + print("Your address", account.address, "has been successfully registered.") } diff --git a/src/account/main.ts b/src/account/main.ts index 0c5d46d..c1395dc 100644 --- a/src/account/main.ts +++ b/src/account/main.ts @@ -66,6 +66,7 @@ export class EntropyAccount extends EntropyBase { } : undefined + this.logger.debug(`registering with params: ${registerParams}`, 'REGISTER') return this.entropy.register(registerParams) // NOTE: if "register" fails for any reason, core currently leaves the chain in a "polluted" // state. To fix this we manually "prune" the dirty registration transaction. @@ -75,28 +76,6 @@ export class EntropyAccount extends EntropyBase { }) } - // WATCH: should this be extracted to interaction.ts? - async registerAccount (account: EntropyAccountConfig, registerParams?: AccountRegisterParams): Promise { - this.logger.debug( - [ - `registering account: ${account.address}`, - // @ts-expect-error Type export of ChildKey still not available from SDK - `to keyring: ${this.entropy.keyring.getLazyLoadAccountProxy('registration').pair.address}` - ].join(', '), - 'REGISTER' - ) - // Register params to be defined from user input (arguments/options or inquirer prompts) - try { - const verifyingKey = await this.register(registerParams) - // NOTE: this mutation triggers events in Keyring - account.data.registration.verifyingKeys.push(verifyingKey) - return account - } catch (error) { - this.logger.error('There was a problem registering', error) - throw error - } - } - /* PRIVATE */ private async pruneRegistration () { diff --git a/src/account/utils.ts b/src/account/utils.ts index 86a595f..111f132 100644 --- a/src/account/utils.ts +++ b/src/account/utils.ts @@ -1,9 +1,9 @@ +import { ACCOUNTS_CONTENT } from './constants'; import { EntropyAccountConfig } from "../config/types"; import * as config from "../config"; -import { ACCOUNTS_CONTENT } from './constants'; -import { generateAccountChoices } from '../common/utils'; +import { generateAccountChoices, findAccountByAddressOrName } from '../common/utils'; -export async function selectAndPersistNewAccount (newAccount) { +export async function selectAndPersistNewAccount (newAccount: EntropyAccountConfig) { const storedConfig = await config.get() const { accounts } = storedConfig @@ -19,11 +19,22 @@ export async function selectAndPersistNewAccount (newAccount) { accounts.push(newAccount) await config.set({ ...storedConfig, - accounts, selectedAccount: newAccount.address }) } +export async function addVerifyingKeyToAccountAndSelect (verifyingKey: string, accountNameOrAddress: string) { + const storedConfig = await config.get() + const account = findAccountByAddressOrName(storedConfig.accounts, accountNameOrAddress) + account.data.registration.verifyingKeys.push(verifyingKey) + + // persist to config, set selectedAccount + await config.set({ + ...storedConfig, + selectedAccount: account.address + }) +} + function validateSeedInput (seed) { if (seed.includes('#debug')) return true if (seed.length === 66 && seed.startsWith('0x')) return true From 4c86915d6e35f76200addad67f3c1889cf8ddb4b Mon Sep 17 00:00:00 2001 From: mixmix Date: Thu, 26 Sep 2024 14:05:15 +1200 Subject: [PATCH 2/2] add detailed error message on failure to persist verifyingKey --- src/account/utils.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/account/utils.ts b/src/account/utils.ts index 111f132..cf51603 100644 --- a/src/account/utils.ts +++ b/src/account/utils.ts @@ -26,6 +26,8 @@ export async function selectAndPersistNewAccount (newAccount: EntropyAccountConf export async function addVerifyingKeyToAccountAndSelect (verifyingKey: string, accountNameOrAddress: string) { const storedConfig = await config.get() const account = findAccountByAddressOrName(storedConfig.accounts, accountNameOrAddress) + if (!account) throw Error(`Unable to persist verifyingKey "${verifyingKey}" to unknown account "${accountNameOrAddress}"`) + account.data.registration.verifyingKeys.push(verifyingKey) // persist to config, set selectedAccount