From bce1135d1b979b5cbf940faf6124596019ef95b2 Mon Sep 17 00:00:00 2001 From: Nayyir Jutha Date: Thu, 11 Jul 2024 15:54:40 -0400 Subject: [PATCH] [NayNay] Entropy Register + Unit Tests - added new register pure function - imported new types from sdk - added unit test for default register --- src/common/masking.ts | 4 +-- src/flows/index.ts | 2 +- src/flows/register/index.ts | 48 ++++++++++------------------------ src/flows/register/register.ts | 29 ++++++++++++++++++++ src/flows/register/types.ts | 5 ++++ src/tui.ts | 2 +- tests/register.test.ts | 17 ++++++++++++ 7 files changed, 68 insertions(+), 39 deletions(-) create mode 100644 src/flows/register/register.ts create mode 100644 src/flows/register/types.ts create mode 100644 tests/register.test.ts diff --git a/src/common/masking.ts b/src/common/masking.ts index 34ba7ac0..30f77af7 100644 --- a/src/common/masking.ts +++ b/src/common/masking.ts @@ -16,9 +16,7 @@ export function maskPayload (payload: any): any { // maskJSONFields doesn't handle nested objects very well so we'll // need to recursively walk to object and mask them one by one - for (const [property, value] of Object.entries(clonedPayload)) { - console.log(property, typeof value); - + for (const [property, value] of Object.entries(clonedPayload)) { if (value && typeof value === 'object') { if (Object.keys(clonedPayload[property]).filter(key => isNaN(parseInt(key))).length === 0) { const reconstructedUintArr: number[] = Object.values(clonedPayload[property]) diff --git a/src/flows/index.ts b/src/flows/index.ts index af41e7bf..df42a37a 100644 --- a/src/flows/index.ts +++ b/src/flows/index.ts @@ -1,6 +1,6 @@ export { entropyFaucet } from './entropyFaucet' export { checkBalance } from './balance' -export { register } from './register' +export { entropyRegister } from './register' export { userPrograms } from './UserPrograms' export { devPrograms } from './DeployPrograms' export { sign } from './sign' diff --git a/src/flows/register/index.ts b/src/flows/register/index.ts index d15d6f88..a5d71018 100644 --- a/src/flows/register/index.ts +++ b/src/flows/register/index.ts @@ -1,9 +1,12 @@ // import inquirer from "inquirer" +// @ts-ignore +import { ChildKey } from '@entropyxyz/sdk/keys' import { getSelectedAccount, print, /*accountChoices*/ } from "../../common/utils" import { initializeEntropy } from "../../common/initializeEntropy" import { EntropyLogger } from "src/common/logger"; +import { register } from "./register"; -export async function register (storedConfig, options, logger: EntropyLogger) { +export async function entropyRegister (storedConfig, options, logger: EntropyLogger) { const FLOW_CONTEXT = 'REGISTER' const { accounts, selectedAccount: selectedFromConfig } = storedConfig; const { endpoint } = options @@ -22,41 +25,18 @@ export async function register (storedConfig, options, logger: EntropyLogger) { // // Setting default to default key proxy program // default: '0x0000000000000000000000000000000000000000000000000000000000000000' // }]) - //@ts-ignore: - logger.debug('about to register selectedAccount.address' + selectedAccount.address + 'keyring:' + entropy.keyring.getLazyLoadAccountProxy('registration').pair.address, FLOW_CONTEXT) + + logger.debug('about to register selectedAccount.address' + selectedAccount.address + 'keyring:' + entropy.keyring.getLazyLoadAccountProxy(ChildKey.registration).pair.address, FLOW_CONTEXT) print("Attempting to register the address:", selectedAccount.address, ) - let verifyingKey: string + try { - // For now we are forcing users to only register with the default info before having to format the config for them - // verifyingKey = await entropy.register({ - // programDeployer: entropy.keyring.accounts.registration.address, - // programData: [{ - // program_pointer: programPointer, - // program_config: '0x', - // }] - // }) - verifyingKey = await entropy.register() - if (verifyingKey) { - print("Your address", selectedAccount.address, "has been successfully registered.") - selectedAccount?.data?.registration?.verifyingKeys?.push(verifyingKey) - const arrIdx = accounts.indexOf(selectedAccount) - accounts.splice(arrIdx, 1, selectedAccount) - return { accounts, selectedAccount: selectedAccount.address } - } + const verifyingKey = await register(entropy) + print("Your address", selectedAccount.address, "has been successfully registered.") + selectedAccount?.data?.registration?.verifyingKeys?.push(verifyingKey) + const arrIdx = accounts.indexOf(selectedAccount) + accounts.splice(arrIdx, 1, selectedAccount) + return { accounts, selectedAccount: selectedAccount.address } } catch (error) { - console.error('error', error); - if (!verifyingKey) { - logger.debug('Pruning Registration', FLOW_CONTEXT) - try { - const tx = await entropy.substrate.tx.registry.pruneRegistration() - await tx.signAndSend(entropy.keyring.accounts.registration.pair, ({ status }) => { - if (status.isFinalized) { - print('Successfully pruned registration'); - } - }) - } catch (error) { - console.error('Unable to prune registration due to:', error.message); - } - } + logger.error('There was a problem registering', error) } } diff --git a/src/flows/register/register.ts b/src/flows/register/register.ts new file mode 100644 index 00000000..8de98aa2 --- /dev/null +++ b/src/flows/register/register.ts @@ -0,0 +1,29 @@ +import Entropy from "@entropyxyz/sdk"; +import { RegsiterParams } from "./types"; +import { print } from "src/common/utils"; + +export async function register (entropy: Entropy, params?: RegsiterParams): Promise { + let verifyingKey: string + try { + const registerParams = params.programModAddress && params.programData ? { programDeployer: params.programModAddress, programData: params.programData } : undefined + verifyingKey = await entropy.register(registerParams) + return verifyingKey + } catch (error) { + if (!verifyingKey) { + // logger.debug('Pruning Registration', FLOW_CONTEXT) + try { + const tx = await entropy.substrate.tx.registry.pruneRegistration() + await tx.signAndSend(entropy.keyring.accounts.registration.pair, ({ status }) => { + if (status.isFinalized) { + print('Successfully pruned registration'); + } + }) + } catch (error) { + console.error('Unable to prune registration due to:', error.message); + throw error + } + } else { + throw error + } + } +} \ No newline at end of file diff --git a/src/flows/register/types.ts b/src/flows/register/types.ts new file mode 100644 index 00000000..df77d563 --- /dev/null +++ b/src/flows/register/types.ts @@ -0,0 +1,5 @@ +export interface RegsiterParams { + programModAddress?: string + // TODO: Export ProgramInstance type from sdk + programData?: any +} \ No newline at end of file diff --git a/src/tui.ts b/src/tui.ts index 872fd5ee..90553819 100644 --- a/src/tui.ts +++ b/src/tui.ts @@ -19,7 +19,7 @@ export default function tui (options: EntropyTuiOptions) { const choices = { 'Manage Accounts': flows.manageAccounts, 'Balance': flows.checkBalance, - 'Register': flows.register, + 'Register': flows.entropyRegister, 'Sign': flows.sign, 'Transfer': flows.entropyTransfer, 'Deploy Program': flows.devPrograms, diff --git a/tests/register.test.ts b/tests/register.test.ts new file mode 100644 index 00000000..c547a873 --- /dev/null +++ b/tests/register.test.ts @@ -0,0 +1,17 @@ +import test from 'tape' + +import { charlieStashSeed, setupTest } from './testing-utils' + +const networkType = 'two-nodes' + +test('Regsiter - Default Program', async (t) => { + const { run, entropy } = await setupTest(t, { networkType, seed: charlieStashSeed }) + + const verifyingKey = await run('register account', entropy.register()) + + const fullAccount = entropy.keyring.getAccount() + + t.equal(verifyingKey, fullAccount.registration.verifyingKeys[0], 'verifying key matches key added to regsitration account') + + t.end() +})