Skip to content

Commit

Permalink
chore: update dependencies & rename passPhrase to passphrase
Browse files Browse the repository at this point in the history
  • Loading branch information
martiliones committed Jan 26, 2024
1 parent 92e9aa0 commit 204e846
Show file tree
Hide file tree
Showing 29 changed files with 2,697 additions and 1,869 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ module.exports = {
'no-console': 'off',
'no-underscore-dangle': 'off',
'default-param-last': 'off',
'import/extensions': 'always',
},
};
80 changes: 29 additions & 51 deletions bin/adamant.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
#!/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 })) {
if (!satisfies(process.version, wanted, { includePrerelease: true })) {
console.log(
chalk.red(
`You are using Node ${process.version}, but this version of ${id}` +
Expand Down Expand Up @@ -53,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 @@ -76,14 +82,14 @@ installVoteCommands(program);
const client = program.command('client');

client.command('version').action(() => {
log.log({
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 @@ -106,35 +112,7 @@ program.on('--help', () => {
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
4 changes: 2 additions & 2 deletions config.default.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
/**
/**
The Console will use this ADM passPhrase by default.
See 'Installing and configuring' in Readme for details.
**/
"passPhrase": "distance expect praise frequent..",
"passphrase": "distance expect praise frequent..",

/** Choose 'mainnet' or 'testnet' **/
"network": "testnet",
Expand Down
6 changes: 3 additions & 3 deletions lib/account.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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
Expand Down
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
16 changes: 8 additions & 8 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 (
export async function createDelegate(
username = requiredParam('username'),
passPhrase,
) => {
const res = await api.newDelegate(passPhrase || config.passPhrase, username);
passphrase,
) {
const res = await api.newDelegate(passphrase || config.passphrase, username);

return res.data || res;
};
}
89 changes: 48 additions & 41 deletions lib/api/get.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,49 @@
const api = require('../../utils/api');
const config = require('../../utils/config');
import {
createKeypairFromPassphrase,
createAddressFromPublicKey,
decodeMessage,
TransactionType,
} from 'adamant-api';

const keys = require('../../utils/keys');
const decrypter = require('../../utils/decryptor');
const { transactionTypes } = require('../../utils/constants');
import api from '../../utils/api.js';
import config from '../../utils/config.js';

const { requiredParam } = require('../../utils/validate');
import { requiredParam } from '../../utils/validate.js';

exports.getAddress = async (address = requiredParam('address')) => {
const res = await api.get('accounts', { address });
export async function getAddress(address = requiredParam('address')) {
const res = await api.getAccountInfo({ address });

return res.data || res;
};
}

exports.getBlock = async (id = requiredParam('id')) => {
const res = await api.get('blocks/get', { id });
export async function getBlock(id = requiredParam('id')) {
const res = await api.getBlock(id);

return res.data || res;
};
}

exports.getBlocks = async (...queries) => {
export async function getBlocks(...queries) {
const urlQuery = queries.join(',');

const parsedQuery = new URLSearchParams(urlQuery.replace(/,/g, '&'));

const res = await api.get('blocks', parsedQuery);
const res = await api.getBlocks(parsedQuery); // todo

return res.data || res;
};
}

exports.getDelegate = async (username = requiredParam('username')) => {
const res = await api.get('delegates/get', { username });
export async function getDelegate(username = requiredParam('username')) {
const res = await api.getDelegate({ username });

return res.data || res;
};
}

exports.getMessage = async (id = requiredParam('id'), customPassPhrase) => {
const res = await api.get('transactions/get', { returnAsset: 1, id });
export async function getMessage(id = requiredParam('id'), customPassphrase) {
const res = await api.getTransaction(id, { returnAsset: 1 });

const passPhrase = customPassPhrase || config.passPhrase;
const passphrase = customPassphrase || config.passphrase;

if (!passPhrase) {
if (!passphrase) {
return res.data || res;
}

Expand All @@ -52,16 +55,16 @@ exports.getMessage = async (id = requiredParam('id'), customPassPhrase) => {

const { transaction } = data;

if (transaction?.type !== transactionTypes.CHAT_MESSAGE) {
if (transaction?.type !== TransactionType.CHAT_MESSAGE) {
return {
success: false,
error: 'Not a message transaction',
};
}

if (transaction.asset?.chat.own_message) {
const keypair = keys.createKeypairFromPassPhrase(passPhrase);
const readerAddress = keys.createAddressFromPublicKey(keypair.publicKey);
const keypair = createKeypairFromPassphrase(passphrase);
const readerAddress = createAddressFromPublicKey(keypair.publicKey);

if (
![transaction.senderId, transaction.recipientId].includes(readerAddress)
Expand All @@ -77,7 +80,7 @@ exports.getMessage = async (id = requiredParam('id'), customPassPhrase) => {
const publicKey = await api.getPublicKey(recipientName);

if (publicKey) {
const decoded = decrypter.decodeMessage(
const decoded = decodeMessage(
transaction.asset.chat.message,
transaction.asset.chat.own_message,
keypair.privateKey,
Expand All @@ -91,42 +94,46 @@ exports.getMessage = async (id = requiredParam('id'), customPassPhrase) => {
}

return data;
};
}

exports.getTransaction = async (id = requiredParam('id')) => {
const res = await api.get('transactions/get', { id });
export async function getTransaction(id = requiredParam('id')) {
const res = await api.getTransaction(id);

return res.data || res;
};
}

exports.getTransactions = async (...queries) => {
export async function getTransactions(...queries) {
const urlQuery = queries.join(',');

const parsedQuery = new URLSearchParams(urlQuery.replace(/,/g, '&'));

const res = await api.get('transactions', parsedQuery);
const res = await api.getTransactions(parsedQuery); // todo

return res.data || res;
};
}

exports.getTransactionsInBlockById = async (
export async function getTransactionsInBlockById(
blockId = requiredParam('blockId'),
) => exports.getTransactions(`blockId=${blockId}`, 'orderBy=timestamp:desc');
) {
return getTransactions(`blockId=${blockId}`, 'orderBy=timestamp:desc');
}

exports.getTransactionsInBlockByHeight = async (
export async function getTransactionsInBlockByHeight(
height = requiredParam('height'),
) =>
exports.getTransactions(
) {
return getTransactions(
`fromHeight=${height}`,
`and:toHeight=${height}`,
'orderBy=timestamp:desc',
);
}

exports.getTransactionsReceivedByAddress = async (
export async function getTransactionsReceivedByAddress(
address = requiredParam('address'),
) =>
exports.getTransactions(
) {
return getTransactions(
`recipientId=${address}`,
'and:minAmount=1',
'orderBy=timestamp:desc',
);
}
21 changes: 6 additions & 15 deletions lib/api/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
const account = require('./account');
const get = require('./get');
const node = require('./node');
const send = require('./send');
const delegate = require('./delegate');
const vote = require('./vote');

module.exports = {
...account,
...get,
...node,
...send,
...delegate,
...vote,
};
export * from './account.js';
export * from './get.js';
export * from './node.js';
export * from './send.js';
export * from './delegate.js';
export * from './vote.js';
Loading

0 comments on commit 204e846

Please sign in to comment.