Skip to content
This repository was archived by the owner on Oct 4, 2024. It is now read-only.

delete-key prompts #890

Merged
merged 3 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,8 @@ With NEAR REPL you have complete access to [`near-api-js`](https://github.com/ne
| `--masterAccount` | Master account used when creating new accounts [string] |
| `--helperAccount` | Expected top-level account for a network [string] |
| `-v, --verbose` | Prints out verbose output [boolean] [default: false] |
|`-f, --force` | Forcefully execute the desired action even if it is unsafe to do so [boolean] [default: false]|


> Got a question? <a href="https://stackoverflow.com/questions/tagged/nearprotocol"> <h8>Ask it on StackOverflow!</h8></a>

Expand Down
31 changes: 28 additions & 3 deletions commands/delete-key.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const exitOnError = require('../utils/exit-on-error');
const chalk = require('chalk');
const connect = require('../utils/connect');
const inspectResponse = require('../utils/inspect-response');
const checkCredentials = require('../utils/check-credentials');
const { askYesNoQuestion } = require('../utils/readline');

module.exports = {
command: 'delete-key <account-id> <access-key>',
Expand All @@ -17,9 +19,32 @@ module.exports = {

async function deleteAccessKey(options) {
await checkCredentials(options.accountId, options.networkId, options.keyStore);
console.log(`Deleting key = ${options.accessKey} on ${options.accountId}.`);
const near = await connect(options);
const account = await near.account(options.accountId);
const result = await account.deleteKey(options.accessKey);
inspectResponse.prettyPrintResponse(result, options);
if (options.force || await isAllApprovalsGranted(account, options.accessKey)) {
console.log(chalk`{bold.white Deleting key {bold.blue ${options.accessKey}} on {bold.blue ${options.accountId}.}}`);
const result = await account.deleteKey(options.accessKey);
inspectResponse.prettyPrintResponse(result, options);
} else {
console.log(chalk`{bold.white Deletion of {bold.blue ${options.accessKey} } key on {bold.blue ${options.accountId}}. was {bold.red canceled}}`);
}
}

async function isAllApprovalsGranted(account, accessKey) {
let accessKeys = await account.getAccessKeys();
let fullAccessKeys = accessKeys.filter(accessKey => accessKey.access_key.permission === 'FullAccess');

// aks for approvals if user is deleting Full Access Key
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// aks for approvals if user is deleting Full Access Key
// asks for approval if user is deleting Full Access Key

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merged together with other changes, thanks!

if (fullAccessKeys.find(key => key.public_key === accessKey)) {
if (!await askYesNoQuestion(
chalk`{bold.white Key {bold.blue ${accessKey}} is a Full Access key. Make sure it's not your recovery method. Do you want to proceed? {bold.green (y/n) }}`,
false)
) { return false; }
// ask additional questions if it's the last Full Access Key
if (fullAccessKeys.length === 1 && !await askYesNoQuestion(
chalk`{bold.white Key {bold.blue ${accessKey}} is the last Full Access key. In case of deletion, you will not be able to restore access to the account {bold.blue ${account.accountId}}. Do you want to proceed? {bold.green (y/n) }}`,
false)
) { return false; }
}
return true;
}