Skip to content

Commit

Permalink
Merge pull request #28 from Adamant-im/chore/renovation
Browse files Browse the repository at this point in the history
Chore/renovation
  • Loading branch information
adamant-al authored Feb 16, 2024
2 parents 525de6a + f37e6b8 commit 5e36901
Show file tree
Hide file tree
Showing 32 changed files with 2,961 additions and 2,028 deletions.
5 changes: 2 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ module.exports = {
node: true,
jest: true,
},
extends: [
'airbnb-base',
],
extends: ['airbnb-base', 'prettier'],
rules: {
'no-restricted-syntax': 'off',
'no-console': 'off',
'no-underscore-dangle': 'off',
'default-param-last': 'off',
'import/extensions': 'always',
},
};
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
package-lock.json
config.default.jsonc
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Adamant-console is Command-line utilities to interact with ADAMANT blockchain. I

## Understanding interaction with ADAMANT blockchain

ADAMANT has *only secure API*, and you cannot transfer passphrase to node to make any action with wallet. Instead, node *requires signed transaction* to make any action.
ADAMANT has _only secure API_, and you cannot transfer passphrase to node to make any action with wallet. Instead, node _requires signed transaction_ to make any action.

Adamant-console connects to ADAMANT nodes on your choice (set in configuration file), it can be any node, locally installed on your machine, or from other side of the Earth. As Console doesn’t transfer passphrases to nodes, it's safe to connect to any node. Node you connect should have [API enabled](https://medium.com/adamant-im/how-to-run-your-adamant-node-on-ubuntu-990e391e8fcc#fe7e).

Expand Down
101 changes: 44 additions & 57 deletions bin/adamant.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
#!/usr/bin/env node

const { program, CommanderError } = require('commander');
const chalk = require('chalk');
const parseShell = require('shell-quote').parse;
import { program, CommanderError } from 'commander';
import chalk from 'chalk';
import { parse as parseShell } from 'shell-quote';

const semver = require('semver');
const leven = require('leven');
import { satisfies } from 'semver';
import leven from 'leven';

const prompt = require('../prompt/index');
import prompt from '../prompt/index.js';

const log = require('../utils/log');
const config = require('../utils/config');
import { log } from '../utils/log.js';
import config from '../utils/config.js';

const packageInfo = require('../package.json');
const enhanceErrorMessages = require('../utils/enhanceErrorMessages');
import packageInfo from '../package.json' assert { type: 'json' };

import installAccountCommands from '../lib/account.js';
import installGetCommands from '../lib/get.js';
import installNodeCommands from '../lib/node.js';
import installSendCommands from '../lib/send.js';
import installRpcServerCommands from '../lib/rpc.js';
import installDelegateCommands from '../lib/delegate.js';
import installVoteCommands from '../lib/vote.js';

const INTERACTIVE_MODE = process.argv.length < 3;

if (INTERACTIVE_MODE) {
program.exitOverride();
}

const requiredVersion = packageInfo.engines.node;

const checkNodeVersion = (wanted, id) => {
if (!semver.satisfies(process.version, wanted, { includePrerelease: true })) {
console.log(chalk.red(
`You are using Node ${process.version}, but this version of ${id}`
+ ` requires Node ${wanted}.\nPlease upgrade your Node version.`,
));
if (!satisfies(process.version, wanted, { includePrerelease: true })) {
console.log(
chalk.red(
`You are using Node ${process.version}, but this version of ${id}` +
` requires Node ${wanted}.\nPlease upgrade your Node version.`,
),
);

process.exit(1);
}
Expand All @@ -36,7 +51,8 @@ const suggestCommands = (unknownCommand) => {
let suggestion;

availableCommands.forEach((cmd) => {
const isBestMatch = leven(cmd, unknownCommand) < leven(suggestion || '', unknownCommand);
const isBestMatch =
leven(cmd, unknownCommand) < leven(suggestion || '', unknownCommand);

if (leven(cmd, unknownCommand) < 3 && isBestMatch) {
suggestion = cmd;
Expand All @@ -50,17 +66,10 @@ const suggestCommands = (unknownCommand) => {
};

program
.name('adm')
.version(`adm ${packageInfo.version}`)
.usage('<type> <command> [options]')
.option('-p, --passPhrase <phrase>', 'account pass phrase');

const installAccountCommands = require('../lib/account');
const installGetCommands = require('../lib/get');
const installNodeCommands = require('../lib/node');
const installSendCommands = require('../lib/send');
const installRpcServerCommands = require('../lib/rpc');
const installDelegateCommands = require('../lib/delegate');
const installVoteCommands = require('../lib/vote');
.option('-p, --passphrase <phrase>', 'account passphrase');

installAccountCommands(program);
installGetCommands(program);
Expand All @@ -72,17 +81,15 @@ installVoteCommands(program);

const client = program.command('client');

client
.command('version')
.action(() => {
log.log({
success: true,
version: packageInfo.version,
});
client.command('version').action(() => {
log({
success: true,
version: packageInfo.version,
});
});

program.on('option:passPhrase', () => {
config.passPhrase = program.opts().passPhrase;
program.on('option:passphrase', () => {
config.passphrase = program.opts().passphrase;
});

// output help information on unknown commands
Expand All @@ -99,33 +106,13 @@ program.on('command:*', ([cmd]) => {
// add some useful info on help
program.on('--help', () => {
console.log();
console.log(` Run ${chalk.cyan('adm <command> --help')} for detailed usage of given command.`);
console.log(
` Run ${chalk.cyan('adm <command> --help')} for detailed usage of given command.`,
);
console.log();
});

program.commands.forEach((command) => command.on('--help', () => console.log()));

enhanceErrorMessages('missingArgument', (argName) => (
`Missing required argument ${chalk.yellow(`<${argName}>`)}.`
));

enhanceErrorMessages('unknownCommand', () => `Unknown command.`);

enhanceErrorMessages('unknownOption', (optionName) => (
`Unknown option ${chalk.yellow(optionName)}.`
));

enhanceErrorMessages('optionMissingArgument', (option, flag) => (
`Missing required argument for option ${chalk.yellow(option.flags)}${flag ? `, got ${chalk.yellow(flag)}` : ''}`
));

const INTERACTIVE_MODE = process.argv.length < 3;

if (INTERACTIVE_MODE) {
// enhance common error messages

program.exitOverride();

prompt(async (command) => {
try {
await program.parseAsync(parseShell(command), { from: 'user' });
Expand Down
18 changes: 9 additions & 9 deletions config.default.json → config.default.jsonc
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
/**
The Console will use this ADM passPhrase by default.
See 'Installing and configuring' in Readme for details.
**/
"passPhrase": "distance expect praise frequent..",
/**
* The Console will use this ADM passphrase by default.
* See 'Installing and configuring' in Readme for details.
*/
"passphrase": "distance expect praise frequent..",

/** Choose 'mainnet' or 'testnet' **/
/* Choose 'mainnet' or 'testnet' */
"network": "testnet",

/** Port used to run RPC server **/
/* Port used to run RPC server */
"rpc": {
"port": 5080
},

/** Additionally, you can specify what ADM nodes to use **/
/* Additionally, you can specify what ADM nodes to use */
"networks": {
"testnet": {
"nodes": [
Expand Down Expand Up @@ -44,4 +44,4 @@
]
}
}
}
}
10 changes: 6 additions & 4 deletions lib/account.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const api = require('./api/index');
const log = require('../utils/log');
import * as api from './api/index.js';
import * as log from '../utils/log.js';

module.exports = (program) => {
export default (program) => {
const account = program.command('account');

account
.command('new')
.description('creates new ADAMANT account and provide account data in JSON format')
.description(
'creates new ADAMANT account and provide account data in JSON format',
)
.action(log.call(api.createAccount));
};
16 changes: 10 additions & 6 deletions lib/api/account.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
const keys = require('../../utils/keys');
import {
createNewPassphrase,
createKeypairFromPassphrase,
createAddressFromPublicKey,
} from 'adamant-api';

exports.createAccount = () => {
const passPhrase = keys.createNewPassPhrase();
const keypair = keys.createKeypairFromPassPhrase(passPhrase);
export const createAccount = () => {
const passphrase = createNewPassphrase();
const keypair = createKeypairFromPassphrase(passphrase);

const answer = {
success: true,
account: {
passPhrase,
address: keys.createAddressFromPublicKey(keypair.publicKey),
passphrase,
address: createAddressFromPublicKey(keypair.publicKey),
publicKey: keypair.publicKey.toString('hex'),
privateKey: keypair.privateKey.toString('hex'),
},
Expand Down
18 changes: 9 additions & 9 deletions lib/api/delegate.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const api = require('../../utils/api');
const config = require('../../utils/config');
const { requiredParam } = require('../../utils/validate');
import api from '../../utils/api.js';
import config from '../../utils/config.js';
import { requiredParam } from '../../utils/validate.js';

exports.createDelegate = async (username = requiredParam('username'), passPhrase) => {
const res = await api.newDelegate(
passPhrase || config.passPhrase,
username,
);
export async function createDelegate(
username = requiredParam('username'),
passphrase,
) {
const res = await api.newDelegate(passphrase || config.passphrase, username);

return res.data || res;
};
}
Loading

0 comments on commit 5e36901

Please sign in to comment.