diff --git a/CHANGELOG.md b/CHANGELOG.md index cf66f1fd..b4ac5615 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ Version header format: `[version] Name - year-month-day (entropy-core compatibil ## [UNRELEASED] ### Added +- new: 'src/flows/register/register.ts' - service file for register pure function - new: './src/flows/manage-accounts/helpers/create-account.ts' - new helper file to house the pure function used to create a new entropy account - update: './tests/manage-accounts.test.ts' - added test for create account pure function - update: './src/common/utils.ts' - removed isValidSubstrateAddress and imported the method in from the sdk diff --git a/src/flows/index.ts b/src/flows/index.ts index d86504ca..2187a5eb 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 './user-program-management' export { devPrograms } from './DeployPrograms' export { sign } from './sign' diff --git a/src/flows/register/index.ts b/src/flows/register/index.ts index d15d6f88..05247578 100644 --- a/src/flows/register/index.ts +++ b/src/flows/register/index.ts @@ -2,8 +2,9 @@ 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 +23,19 @@ export async function register (storedConfig, options, logger: EntropyLogger) { // // Setting default to default key proxy program // default: '0x0000000000000000000000000000000000000000000000000000000000000000' // }]) - //@ts-ignore: + // @ts-expect-error: Expecting error here as method expects typeof ChildKey enum from sdk + // export from sdk is not working as intended currently logger.debug('about to register selectedAccount.address' + selectedAccount.address + 'keyring:' + entropy.keyring.getLazyLoadAccountProxy('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..8b2c3941 --- /dev/null +++ b/src/flows/register/register.ts @@ -0,0 +1,28 @@ +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) { + try { + const tx = 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 + } + } + 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..bbd21618 --- /dev/null +++ b/tests/register.test.ts @@ -0,0 +1,44 @@ +import test from 'tape' + +import { charlieStashSeed, setupTest } from './testing-utils' +import { register } from 'src/flows/register/register' +import { readFileSync } from 'node:fs' + +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', register(entropy)) + + const fullAccount = entropy.keyring.getAccount() + + t.equal(verifyingKey, fullAccount.registration.verifyingKeys[0], 'verifying key matches key added to regsitration account') + + t.end() +}) + +test('Register - Barebones Program', async t => { + const { run, entropy } = await setupTest(t, { networkType, seed: charlieStashSeed }) + const dummyProgram: any = readFileSync( + 'src/programs/template_barebones.wasm' + ) + const pointer = await run( + 'deploy program', + entropy.programs.dev.deploy(dummyProgram) + ) + + const verifyingKey = await run( + 'register', + entropy.register({ + programDeployer: entropy.keyring.accounts.registration.address, + programData: [{ program_pointer: pointer, program_config: '0x' }], + }) + ) + + const fullAccount = entropy.keyring.getAccount() + + t.equal(verifyingKey, fullAccount.registration.verifyingKeys[1], 'verifying key matches key added to regsitration account') + + t.end() +})