Skip to content

Commit

Permalink
Merge pull request #529 from near/add-key
Browse files Browse the repository at this point in the history
Implement add-key option
  • Loading branch information
vgrichina authored Sep 15, 2020
2 parents c6870c5 + 3603f32 commit 93ee478
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 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
40 changes: 40 additions & 0 deletions commands/add-key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 <account-id> <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 in $NEAR for the key (default 0)',
type: 'string',
required: false,
}),
handler: exitOnError(addAccessKey)
};

async function addAccessKey(options) {
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);
const result = await account.addKey(options.accessKey, options.contractId, options.methodNames, allowance);
inspectResponse.prettyPrintResponse(result, options);
}
4 changes: 2 additions & 2 deletions commands/delete-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ const connect = require('../utils/connect');
const inspectResponse = require('../utils/inspect-response');

module.exports = {
command: 'delete-key [accessKey]',
command: 'delete-key <account-id> <access-key>',
desc: 'delete access key',
builder: (yargs) => yargs
.option('accessKey', {
.option('access-key', {
desc: 'Public key to delete (base58 encoded)',
type: 'string',
required: true,
Expand Down

0 comments on commit 93ee478

Please sign in to comment.