Skip to content

Commit

Permalink
[NayNay] Entropy Register + Unit Tests
Browse files Browse the repository at this point in the history
- added new register pure function
- imported new types from sdk
- added unit test for default register
  • Loading branch information
rh0delta committed Jul 11, 2024
1 parent 614f9ec commit bce1135
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 39 deletions.
4 changes: 1 addition & 3 deletions src/common/masking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])
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 './UserPrograms'
export { devPrograms } from './DeployPrograms'
export { sign } from './sign'
Expand Down
48 changes: 14 additions & 34 deletions src/flows/register/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
}
}
29 changes: 29 additions & 0 deletions src/flows/register/register.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
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
}
}
}
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
17 changes: 17 additions & 0 deletions tests/register.test.ts
Original file line number Diff line number Diff line change
@@ -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()
})

0 comments on commit bce1135

Please sign in to comment.