Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #178 from nearprotocol/generate-key
Browse files Browse the repository at this point in the history
Add `near generate-key` command
  • Loading branch information
vgrichina authored Nov 5, 2019
2 parents d4961f2 + 415f4c1 commit 5dd2f32
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 21 deletions.
35 changes: 14 additions & 21 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
const yargs = require('yargs');
const main = require('../');

const exitOnError = async function(promise) {
try {
await promise;
} catch (e) {
console.log('Error: ', e);
process.exit(1);
}
};
const exitOnError = require('../utils/exit-on-error');

// For account:
const createAccount = {
Expand All @@ -35,14 +27,14 @@ const createAccount = {
type: 'string',
default: '1000000000000000000'
}),
handler: (argv) => exitOnError(main.createAccount(argv))
handler: exitOnError(main.createAccount)
};

const login = {
command: 'login',
desc: 'create a developer account',
builder: (yargs) => yargs,
handler: (argv) => exitOnError(main.login(argv))
handler: exitOnError(main.login)
};

const viewAccount = {
Expand All @@ -54,7 +46,7 @@ const viewAccount = {
type: 'string',
required: true
}),
handler: (argv) => exitOnError(main.viewAccount(argv))
handler: exitOnError(main.viewAccount)
};

const deleteAccount = {
Expand All @@ -71,7 +63,7 @@ const deleteAccount = {
type: 'string',
required: true
}),
handler: (argv) => exitOnError(main.deleteAccount(argv))
handler: exitOnError(main.deleteAccount)
};

const keys = {
Expand All @@ -83,14 +75,14 @@ const keys = {
type: 'string',
required: true
}),
handler: (argv) => exitOnError(main.keys(argv))
handler: exitOnError(main.keys)
};

const sendMoney = {
command: 'send <sender> <receiver> <amount>',
desc: 'send tokens to given receiver',
builder: (yargs) => yargs,
handler: (argv) => exitOnError(main.sendMoney(argv))
handler: exitOnError(main.sendMoney)
};

const stake = {
Expand All @@ -112,7 +104,7 @@ const stake = {
type: 'string',
required: true,
}),
handler: (argv) => exitOnError(main.stake(argv))
handler: exitOnError(main.stake)
};

// For contract:
Expand All @@ -128,7 +120,7 @@ const deploy = {
.alias({
'accountId': ['account_id', 'contractName', 'contract_name'],
}),
handler: (argv) => exitOnError(main.deploy(argv))
handler: exitOnError(main.deploy)
};

const scheduleFunctionCall = {
Expand All @@ -140,14 +132,14 @@ const scheduleFunctionCall = {
type: 'string',
default: '1000000000'
}),
handler: (argv) => exitOnError(main.scheduleFunctionCall(argv))
handler: exitOnError(main.scheduleFunctionCall)
};

const callViewFunction = {
command: 'view <contractName> <methodName> [args]',
desc: 'make smart contract call which can view state',
builder: (yargs) => yargs,
handler: (argv) => exitOnError(main.callViewFunction(argv))
handler: exitOnError(main.callViewFunction)
};

const { spawn } = require('child_process');
Expand Down Expand Up @@ -178,7 +170,7 @@ const txStatus = {
type: 'string',
required: true
}),
handler: (argv) => exitOnError(main.txStatus(argv))
handler: exitOnError(main.txStatus)
};

const clean = {
Expand All @@ -190,7 +182,7 @@ const clean = {
type: 'string',
default: './out'
}),
handler: (argv) => exitOnError(main.clean(argv))
handler: exitOnError(main.clean)
};

let config = require('../get-config')();
Expand Down Expand Up @@ -238,6 +230,7 @@ yargs // eslint-disable-line
.command(stake)
.command(login)
.command(require('../commands/repl'))
.command(require('../commands/generate-key'))
.config(config)
.alias({
'accountId': ['account_id'],
Expand Down
22 changes: 22 additions & 0 deletions commands/generate-key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const KeyPair = require('nearlib').KeyPair;
const exitOnError = require('../utils/exit-on-error');

module.exports = {
command: 'generate-key <account-id>',
desc: 'generate key ',
builder: (yargs) => yargs,
handler: exitOnError(async (argv) => {
let near = await require('../utils/connect')(argv);
if (argv.accountId) {
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`);
} else {
const keyPair = KeyPair.fromRandom('ed25519');
await keyStore.setKey(argv.networkId, argv.accountId, keyPair);
console.log(`Generated key pair with ${keyPair.publicKey} public key`);
}
}
})
};
28 changes: 28 additions & 0 deletions test/test_generate_key.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

#!/bin/bash
set -ex
KEY_FILE=neardev/default/generate-key-test.json
rm -f "$KEY_FILE"

RESULT=$(./bin/near generate-key generate-key-test --networkId default)
echo $RESULT

if [[ ! -f "${KEY_FILE}" ]]; then
echo "FAILURE Key file doesn't exist"
exit 1
fi

EXPECTED=".*Generated key pair with ed25519:.+ public key.*"
if [[ ! "$RESULT" =~ $EXPECTED ]]; then
echo FAILURE Unexpected output from near generate-key
exit 1
fi

RESULT2=$(./bin/near generate-key generate-key-test --networkId default)
echo $RESULT2

EXPECTED2=".*Account has existing key pair with ed25519:.+ public key.*"
if [[ ! "$RESULT2" =~ $EXPECTED2 ]]; then
echo FAILURE Unexpected output from near generate-key when key already exists
exit 1
fi
9 changes: 9 additions & 0 deletions utils/exit-on-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = (promiseFn) => async (...args) => {
const promise = promiseFn.apply(null, args);
try {
await promise;
} catch (e) {
console.log('Error: ', e);
process.exit(1);
}
};

0 comments on commit 5dd2f32

Please sign in to comment.