Skip to content

Commit

Permalink
refactor entropy register so CLI/TUI are similar
Browse files Browse the repository at this point in the history
  • Loading branch information
mixmix committed Sep 26, 2024
1 parent 7cd5072 commit cc63c72
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 58 deletions.
28 changes: 6 additions & 22 deletions src/account/command.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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)
})
Expand Down
19 changes: 9 additions & 10 deletions src/account/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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.")
}
23 changes: 1 addition & 22 deletions src/account/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -75,28 +76,6 @@ export class EntropyAccount extends EntropyBase {
})
}

// WATCH: should this be extracted to interaction.ts?
async registerAccount (account: EntropyAccountConfig, registerParams?: AccountRegisterParams): Promise<EntropyAccountConfig> {
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 () {
Expand Down
19 changes: 15 additions & 4 deletions src/account/utils.ts
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down

0 comments on commit cc63c72

Please sign in to comment.