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

[NayNay] Entropy Register + Unit Tests #167

Merged
merged 7 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/flows/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
45 changes: 12 additions & 33 deletions src/flows/register/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
28 changes: 28 additions & 0 deletions src/flows/register/register.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
let verifyingKey: string
try {
const registerParams = params?.programModAddress && params?.programData ? { programDeployer: params.programModAddress, programData: params.programData } : undefined
rh0delta marked this conversation as resolved.
Show resolved Hide resolved

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
}
}
5 changes: 5 additions & 0 deletions src/flows/register/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface RegsiterParams {
programModAddress?: string
// TODO: Export ProgramInstance type from sdk
programData?: any
}
2 changes: 1 addition & 1 deletion src/tui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
44 changes: 44 additions & 0 deletions tests/register.test.ts
Original file line number Diff line number Diff line change
@@ -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()
})