Skip to content

Commit

Permalink
feat(add-account): refactor update add-from-wallet command utils
Browse files Browse the repository at this point in the history
  • Loading branch information
realdreamer committed Jan 25, 2024
1 parent 0f628a0 commit 5c5bcb2
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 311 deletions.
32 changes: 7 additions & 25 deletions packages/tools/kadena-cli/src/account/commands/accountAddManual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,10 @@ import { updateAccountDetailsPrompt } from '../../prompts/account.js';
import { createCommand } from '../../utils/createCommand.js';
import { globalOptions } from '../../utils/globalOptions.js';
import { sanitizeFilename } from '../../utils/helpers.js';
import type {
IAccountDetailsResult,
IAddAccountManualConfig,
} from '../types.js';
import { getUpdatedConfig } from '../utils/addHelpers.js';
import { validateAccountDetails } from '../utils/validateAccountDetails.js';
import { writeConfigInFile } from '../utils/writeConfigInFile.js';

export const getUpdatedConfig = (
config: IAddAccountManualConfig,
accountDetails: IAccountDetailsResult,
updateOption: string,
) => {
if (updateOption === 'userInput') {
return config;
} else {
const updatedConfig = {
...config,
publicKeys: accountDetails.publicKeys.join(','),
publicKeysConfig: accountDetails.publicKeys,
predicate: accountDetails.predicate,
};
return updatedConfig;
}
};

export const addAccountManualCommand: (
program: Command,
version: string,
Expand All @@ -56,8 +35,11 @@ export const addAccountManualCommand: (
const filePath = path.join(defaultAccountPath, `${sanitizedAlias}.yaml`);

try {
const [{ config: newConfig, accountDetails }, isConfigAreSame] =
await validateAccountDetails(config);
const {
config: newConfig,
accountDetails,
isConfigAreSame,
} = await validateAccountDetails(config);

if (isConfigAreSame) {
await writeConfigInFile(filePath, newConfig);
Expand All @@ -81,7 +63,7 @@ export const addAccountManualCommand: (
if (error.message.includes('row not found')) {
console.log(
chalk.red(
`The account ${config.accountName} is not on chain yet. To create it on-chain, transfer funds to it from ${config.networkConfig.network} and use "fund" command.`,
`The account is not on chain yet. To create it on-chain, transfer funds to it from ${config.networkConfig.network} and use "fund" command.`,
),
);
return;
Expand Down
85 changes: 63 additions & 22 deletions packages/tools/kadena-cli/src/account/commands/accountAddWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,30 @@ import type { IKeyPair } from '@kadena/types';
import { defaultAccountPath } from '../../constants/account.js';
import { WALLET_DIR } from '../../constants/config.js';
import { printWalletKeys } from '../../keys/utils/keysDisplay.js';
import { IWallet, getWallet } from '../../keys/utils/keysHelpers.js';
import { updateAccountDetailsPrompt } from '../../prompts/account.js';
import { services } from '../../services/index.js';
import { createCommand } from '../../utils/createCommand.js';
import { globalOptions } from '../../utils/globalOptions.js';
import { sanitizeFilename } from '../../utils/helpers.js';
import { IAddAccountManualConfig } from '../types.js';
import {
validateAccountDetails,
writeConfigInFile,
} from '../utils/addHelpers.js';
import type { IAddAccountManualConfig } from '../types.js';
import { getUpdatedConfig } from '../utils/addHelpers.js';
import { validateAccountDetails } from '../utils/validateAccountDetails.js';
import { writeConfigInFile } from '../utils/writeConfigInFile.js';

async function getAllPublicKeysFromWallet(
keyWalletConfig: IWallet,
): Promise<Array<string>> {
const publicKeysList: Array<string> = [];
for (const key of keyWalletConfig.keys) {
const content = await services.filesystem.readFile(
path.join(WALLET_DIR, keyWalletConfig?.folder, key),
);
const parsed = content !== null ? (yaml.load(content) as IKeyPair) : null;
publicKeysList.push(parsed?.publicKey || '');
}
return publicKeysList.filter((key) => !!key);
}

export const addAccountWalletCommand: (
program: Command,
Expand All @@ -43,32 +58,58 @@ export const addAccountWalletCommand: (
return;
}

const publicKeysList: Array<string | undefined> = [];
for (const key of keyWalletConfig.keys) {
const content = await services.filesystem.readFile(
path.join(WALLET_DIR, keyWalletConfig?.folder, key),
);
const parsed = content !== null ? (yaml.load(content) as IKeyPair) : null;
publicKeysList.push(parsed?.publicKey);
}

publicKeysList.filter((key) => !!key);
const publicKeys = await getAllPublicKeysFromWallet(keyWalletConfig);

const selectPublicKeys = await checkbox({
message: 'Select public keys to add to account',
choices: publicKeysList.map((key) => ({ value: key })),
choices: publicKeys.map((key) => ({ value: key })),
});

(config as unknown as IAddAccountManualConfig).publicKeysConfig =
selectPublicKeys as string[];
const updatedConfig = {
...config,
publicKeys: selectPublicKeys.join(','),
publicKeysConfig: selectPublicKeys,
};

const sanitizedAlias = sanitizeFilename(config.accountAlias).toLowerCase();
const filePath = path.join(defaultAccountPath, `${sanitizedAlias}.yaml`);

const newConfig = await validateAccountDetails(
config as unknown as IAddAccountManualConfig,
);
try {
const {
config: newConfig,
accountDetails,
isConfigAreSame,
} = await validateAccountDetails(updatedConfig);

if (isConfigAreSame) {
await writeConfigInFile(filePath, newConfig);
} else {
const updateOption = await updateAccountDetailsPrompt();

const updatedConfig = getUpdatedConfig(
newConfig,
accountDetails,
updateOption,
);

await writeConfigInFile(filePath, newConfig);
await writeConfigInFile(filePath, updatedConfig);
}
console.log(
chalk.green(
`\nThe account configuration "${updatedConfig.accountAlias}" has been saved.\n`,
),
);
} catch (error) {
if (error.message.includes('row not found')) {
console.log(
chalk.red(
`The account is not on chain yet. To create it on-chain, transfer funds to it from ${updatedConfig.networkConfig.network} and use "fund" command.`,
),
);
return;
}
console.log(chalk.red(`${error.message}`));
process.exit(1);
}
},
);
23 changes: 19 additions & 4 deletions packages/tools/kadena-cli/src/account/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,27 @@ import type { INetworkCreateOptions } from '../networks/utils/networkHelpers.js'

export type Predicate = 'keys-all' | 'keys-2' | 'keys-any';

export interface IAddAccountManualConfig extends INetworkCreateOptions {
export interface IAccountConfig {
accountAlias: string;
accountName?: string;
fungible: string;
predicate: Predicate;
predicate: string;
network: string;
chainId: ChainId;
publicKeysConfig: string[];
networkConfig: INetworkCreateOptions;
quiet?: boolean;
publicKeys: string;
publicKeysConfig: string[];
}

export interface IAddAccountWalletConfig extends IAccountConfig {
keyWallet: string;
}

export interface IAddAccountManualConfig extends IAccountConfig {
accountName?: string;
}

export interface IAccountDetailsResult {
publicKeys: string[];
predicate: string;
}
Loading

0 comments on commit 5c5bcb2

Please sign in to comment.