From 266e4929d03efbbcdb457b8fa84549e97fb5ac4e Mon Sep 17 00:00:00 2001 From: Illia Polosukhin Date: Mon, 7 Sep 2020 02:32:45 -0700 Subject: [PATCH 1/3] Implement add-key option --- bin/near-cli.js | 1 + commands/add-key.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 commands/add-key.js diff --git a/bin/near-cli.js b/bin/near-cli.js index 208b2c3e..6300fbb7 100644 --- a/bin/near-cli.js +++ b/bin/near-cli.js @@ -228,6 +228,7 @@ yargs // eslint-disable-line .command(login) .command(require('../commands/repl')) .command(require('../commands/generate-key')) + .command(require('../commands/add-key')) .command(require('../commands/delete-key')) .command(require('../commands/validators')) .command(require('../commands/proposals')) diff --git a/commands/add-key.js b/commands/add-key.js new file mode 100644 index 00000000..0079e679 --- /dev/null +++ b/commands/add-key.js @@ -0,0 +1,44 @@ +const BN = require('bn.js'); +const exitOnError = require('../utils/exit-on-error'); +const connect = require('../utils/connect'); +const inspectResponse = require('../utils/inspect-response'); +const { utils } = require('near-api-js'); + +module.exports = { + command: 'add-key [access-key]', + desc: 'Add an access key to given account', + builder: (yargs) => yargs + .option('access-key', { + desc: 'Public key to add (base58 encoded)', + type: 'string', + required: true, + }) + .option('contract-id', { + desc: 'Limit access key to given contract (if not provided - will create full access key)', + type: 'string', + required: false, + }) + .option('method-names', { + desc: 'Method names to limit access key to (command separated)', + type: 'string', + required: false, + }) + .option('allowance', { + desc: 'Allowance on the key (default 0)', + type: 'string', + required: false, + }), + handler: exitOnError(addAccessKey) +}; + +async function addAccessKey(options) { + if (!options.accountId) { + return; + } + console.log(`Adding ${options.contractId ? 'limited access' : 'full access'} key = ${options.accessKey} to ${options.accountId}.`); + const near = await connect(options); + const account = await near.account(options.accountId); + const allowance = utils.format.parseNearAmount(options.allowance); + const result = await account.addKey(options.accessKey, options.contractId, options.methodNames, allowance); + inspectResponse.prettyPrintResponse(result, options); +} From 7aa17110620aaeec9b12ded176329f9c00afad65 Mon Sep 17 00:00:00 2001 From: Illia Polosukhin Date: Mon, 7 Sep 2020 02:37:36 -0700 Subject: [PATCH 2/3] Fix lint --- commands/add-key.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/commands/add-key.js b/commands/add-key.js index 0079e679..c92bde50 100644 --- a/commands/add-key.js +++ b/commands/add-key.js @@ -1,4 +1,3 @@ -const BN = require('bn.js'); const exitOnError = require('../utils/exit-on-error'); const connect = require('../utils/connect'); const inspectResponse = require('../utils/inspect-response'); @@ -24,7 +23,7 @@ module.exports = { required: false, }) .option('allowance', { - desc: 'Allowance on the key (default 0)', + desc: 'Allowance in $NEAR for the key (default 0)', type: 'string', required: false, }), @@ -35,7 +34,7 @@ async function addAccessKey(options) { if (!options.accountId) { return; } - console.log(`Adding ${options.contractId ? 'limited access' : 'full access'} key = ${options.accessKey} to ${options.accountId}.`); + console.log(`Adding ${options.contractId ? 'function call access' : 'full access'} key = ${options.accessKey} to ${options.accountId}.`); const near = await connect(options); const account = await near.account(options.accountId); const allowance = utils.format.parseNearAmount(options.allowance); From 3603f3207d18cba269db3c6f813d3b9c8f838b58 Mon Sep 17 00:00:00 2001 From: Illia Polosukhin Date: Tue, 15 Sep 2020 14:50:14 -0700 Subject: [PATCH 3/3] Redo the format of add/remove key to be --- commands/add-key.js | 5 +---- commands/delete-key.js | 4 ++-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/commands/add-key.js b/commands/add-key.js index c92bde50..3fb96dba 100644 --- a/commands/add-key.js +++ b/commands/add-key.js @@ -4,7 +4,7 @@ const inspectResponse = require('../utils/inspect-response'); const { utils } = require('near-api-js'); module.exports = { - command: 'add-key [access-key]', + command: 'add-key ', desc: 'Add an access key to given account', builder: (yargs) => yargs .option('access-key', { @@ -31,9 +31,6 @@ module.exports = { }; async function addAccessKey(options) { - if (!options.accountId) { - return; - } console.log(`Adding ${options.contractId ? 'function call access' : 'full access'} key = ${options.accessKey} to ${options.accountId}.`); const near = await connect(options); const account = await near.account(options.accountId); diff --git a/commands/delete-key.js b/commands/delete-key.js index 8d6f47fd..380c771d 100644 --- a/commands/delete-key.js +++ b/commands/delete-key.js @@ -3,10 +3,10 @@ const connect = require('../utils/connect'); const inspectResponse = require('../utils/inspect-response'); module.exports = { - command: 'delete-key [accessKey]', + command: 'delete-key ', desc: 'delete access key', builder: (yargs) => yargs - .option('accessKey', { + .option('access-key', { desc: 'Public key to delete (base58 encoded)', type: 'string', required: true,