Skip to content

Commit

Permalink
Implement add-key option
Browse files Browse the repository at this point in the history
  • Loading branch information
ilblackdragon committed Sep 7, 2020
1 parent 1712b50 commit 266e492
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions bin/near-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
44 changes: 44 additions & 0 deletions commands/add-key.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 266e492

Please sign in to comment.