From 266e4929d03efbbcdb457b8fa84549e97fb5ac4e Mon Sep 17 00:00:00 2001 From: Illia Polosukhin Date: Mon, 7 Sep 2020 02:32:45 -0700 Subject: [PATCH] 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); +}