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] Complete Import #307

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
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
21 changes: 15 additions & 6 deletions src/account/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import inquirer from "inquirer";
import Entropy from "@entropyxyz/sdk";

import { EntropyAccount } from './main'
import { selectAndPersistNewAccount, addVerifyingKeyToAccountAndSelect } from "./utils";
import { selectAndPersistNewAccount, addVerifyingKeyToAccountAndSelect, completeImport } from "./utils";
import { findAccountByAddressOrName, print } from "../common/utils"
import { EntropyConfig } from "../config/types";
import { EntropyAccountConfig, EntropyConfig } from "../config/types";
import * as config from "../config";

import {
Expand All @@ -17,7 +17,7 @@ import { ERROR_RED } from "src/common/constants";
/*
* @returns partialConfigUpdate | "exit" | undefined
*/
export async function entropyAccount (endpoint: string, storedConfig: EntropyConfig) {
export async function entropyAccount (storedConfig: EntropyConfig, endpoint?: string) {
const { accounts } = storedConfig
const { interactionChoice } = await inquirer.prompt(accountManageQuestions)

Expand All @@ -32,9 +32,13 @@ export async function entropyAccount (endpoint: string, storedConfig: EntropyCon
seed = seed.split('#debug')[0]
}

const newAccount = seed
? await EntropyAccount.import({ seed, name, path })
: await EntropyAccount.create({ name, path })
let newAccount: EntropyAccountConfig
if (seed) {
const partiallyImportedAccount = await EntropyAccount.import({ seed, name, path })
newAccount = await completeImport(partiallyImportedAccount, endpoint)
} else {
newAccount = await EntropyAccount.create({ name, path })
}

await selectAndPersistNewAccount(newAccount)
return
Expand Down Expand Up @@ -87,6 +91,11 @@ export async function entropyRegister (entropy: Entropy, endpoint: string, store
try {
const verifyingKey = await accountService.register()
await addVerifyingKeyToAccountAndSelect(verifyingKey, account.address)
const fullAccount = await entropy.keyring.getAccount()
const newverifyingkeys = await accountService.getVerifyingKeys(fullAccount.registration.address)

console.log({ regkeys: fullAccount.registration.verifyingKeys, devkeys: fullAccount.deviceKey.verifyingKeys, adminkeys: fullAccount.admin.verifyingKeys });
console.log({ newverifyingkeys })

print("Your address", account.address, "has been successfully registered.")
} catch (error) {
Expand Down
5 changes: 5 additions & 0 deletions src/account/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ export class EntropyAccount extends EntropyBase {
}))
}

async getVerifyingKeys (address: string) {
return this.entropy.substrate.query.registry.modifiableKeys(address)
.then(result => result.toJSON())
}

async register (params?: AccountRegisterParams): Promise<string> {
let programModAddress: string
let programData: any
Expand Down
13 changes: 13 additions & 0 deletions src/account/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ACCOUNTS_CONTENT } from './constants';
import { EntropyAccountConfig } from "../config/types";
import * as config from "../config";
import { generateAccountChoices, findAccountByAddressOrName } from '../common/utils';
import { EntropyAccount } from './main';
import { initializeEntropy } from 'src/common/initializeEntropy';

export async function selectAndPersistNewAccount (newAccount: EntropyAccountConfig) {
const storedConfig = await config.get()
Expand Down Expand Up @@ -93,3 +95,14 @@ export const accountManageQuestions = [
choices: ACCOUNTS_CONTENT.interactionChoice.choices
}
]

export async function completeImport (newAccount: EntropyAccountConfig, endpoint: string) {
const entropy = await initializeEntropy({ keyMaterial: newAccount.data, endpoint })
const accountService = new EntropyAccount(entropy, endpoint)

const verifyingKeys = await accountService.getVerifyingKeys(newAccount.address)
newAccount.data.admin.verifyingKeys = verifyingKeys as string[]
newAccount.data.registration.verifyingKeys = verifyingKeys as string[]

return newAccount
}
5 changes: 3 additions & 2 deletions src/faucet/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { EntropyProgram } from "src/program/main";
import FaucetSigner from "./helpers/signer";
import { SendMoneyParams } from "./types";
import { formatDispatchError } from "src/common/utils";
import { EntropyAccount } from "src/account/main";

const FLOW_CONTEXT = 'ENTROPY-FAUCET'

Expand Down Expand Up @@ -43,8 +44,8 @@ export class EntropyFaucet extends EntropyBase {
}

async getAllFaucetVerifyingKeys (programModKey = FAUCET_PROGRAM_MOD_KEY) {
return this.entropy.substrate.query.registry.modifiableKeys(programModKey)
.then(res => res.toJSON())
const accountService = new EntropyAccount(this.entropy, this.endpoint)
return accountService.getVerifyingKeys(programModKey)
}

// To handle overloading the individual faucet, multiple faucet accounts have been generated, and here is
Expand Down
2 changes: 1 addition & 1 deletion src/tui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ async function main (entropy: Entropy, choices: string[], options: EntropyTuiOpt

switch (answers.choice) {
case 'Manage Accounts': {
const response = await entropyAccount(options.endpoint, storedConfig)
const response = await entropyAccount(storedConfig, options.endpoint)
if (response === 'exit') { returnToMain = true }
break
}
Expand Down
Loading