Skip to content

Commit

Permalink
asking with more commands
Browse files Browse the repository at this point in the history
  • Loading branch information
icerove committed Oct 21, 2020
1 parent fe5a618 commit 9c0d408
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 29 deletions.
3 changes: 3 additions & 0 deletions commands/add-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const exitOnError = require('../utils/exit-on-error');
const connect = require('../utils/connect');
const inspectResponse = require('../utils/inspect-response');
const { utils } = require('near-api-js');
const eventtracking = require('../utils/eventtracking');


module.exports = {
command: 'add-key <account-id> <access-key>',
Expand Down Expand Up @@ -31,6 +33,7 @@ module.exports = {
};

async function addAccessKey(options) {
await eventtracking.askForId(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);
Expand Down
2 changes: 2 additions & 0 deletions commands/delete-key.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const exitOnError = require('../utils/exit-on-error');
const connect = require('../utils/connect');
const inspectResponse = require('../utils/inspect-response');
const eventtracking = require('../utils/eventtracking');

module.exports = {
command: 'delete-key <account-id> <access-key>',
Expand All @@ -15,6 +16,7 @@ module.exports = {
};

async function deleteAccessKey(options) {
await eventtracking.askForId(options);
console.log(`Deleting key = ${options.accessKey} on ${options.accountId}.`);
const near = await connect(options);
const account = await near.account(options.accountId);
Expand Down
64 changes: 35 additions & 29 deletions commands/generate-key.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,48 @@
const KeyPair = require('near-api-js').KeyPair;
const exitOnError = require('../utils/exit-on-error');
const implicitAccountId = require('../utils/implicit-accountid');
const connect = require('../utils/connect');
const eventtracking = require('../utils/eventtracking');

module.exports = {
command: 'generate-key [account-id]',
desc: 'generate key or show key from Ledger',
builder: (yargs) => yargs,
handler: exitOnError(async (argv) => {
let near = await require('../utils/connect')(argv);
handler: exitOnError(generateKey)
};

if (argv.usingLedger) {
if (argv.accountId) {
console.log('WARN: Account id is provided but ignored in case of using Ledger.');
}
const publicKey = await argv.signer.getPublicKey();
// NOTE: Command above already prints public key.
console.log(`Implicit account: ${implicitAccountId(publicKey.toString())}`);
// TODO: query all accounts with this public key here.
// TODO: check if implicit account exist, and if the key doen't match already.
return;
}
async function generateKey(options) {
await eventtracking.askForId(options);
const near = await connect(options);

const { deps: { keyStore } } = near.config;
const existingKey = await keyStore.getKey(argv.networkId, argv.accountId);
if (existingKey) {
console.log(`Account has existing key pair with ${existingKey.publicKey} public key`);
return;
if (options.usingLedger) {
if (options.accountId) {
console.log('WARN: Account id is provided but ignored in case of using Ledger.');
}
const publicKey = await options.signer.getPublicKey();
// NOTE: Command above already prints public key.
console.log(`Implicit account: ${implicitAccountId(publicKey.toString())}`);
// TODO: query all accounts with this public key here.
// TODO: check if implicit account exist, and if the key doen't match already.
return;
}

// If key doesn't exist, create one and store in the keyStore.
// Otherwise, it's expected that both key and accountId are already provided in arguments.
if (!argv.publicKey) {
const keyPair = KeyPair.fromRandom('ed25519');
argv.publicKey = keyPair.publicKey.toString();
argv.accountId = argv.accountId || implicitAccountId(argv.publicKey);
await keyStore.setKey(argv.networkId, argv.accountId, keyPair);
}
const { deps: { keyStore } } = near.config;
const existingKey = await keyStore.getKey(options.networkId, options.accountId);
if (existingKey) {
console.log(`Account has existing key pair with ${existingKey.publicKey} public key`);
return;
}

// If key doesn't exist, create one and store in the keyStore.
// Otherwise, it's expected that both key and accountId are already provided in arguments.
if (!options.publicKey) {
const keyPair = KeyPair.fromRandom('ed25519');
options.publicKey = keyPair.publicKey.toString();
options.accountId = options.accountId || implicitAccountId(options.publicKey);
await keyStore.setKey(options.networkId, options.accountId, keyPair);
}

console.log(`Key pair with ${argv.publicKey} public key for an account "${argv.accountId}"`);
})
};
console.log(`Key pair with ${options.publicKey} public key for an account "${options.accountId}"`);

}

0 comments on commit 9c0d408

Please sign in to comment.