#!/usr/bin/env node import { composeAPI } from '@helixnetwork/core'; import { program } from 'commander'; import mongoose from 'mongoose'; import { getNodeInfoWrapper } from './modules/commands/helix-command'; import { pinFileToIpfsWrapper } from './modules/commands/ipfs-command'; import { publishNewRecord, extractPayloadFromHelixTx, giveLatestAddress, getFundBalance, fetchRecordById, } from './modules/commands/ajix-lab-command'; import init from './modules/config'; import { version } from './package.json'; const pinataSDK = require('@pinata/sdk'); async function runAjixCLI() { try { // Loading CLI Config const config = await init(); // Initialiseing Helix Provider const helix = composeAPI({ provider: config.HELIX_PROVIDER_URL, }); // Pinata Ipfs cloud SDK Initialise const pinata = pinataSDK(config.PINATA_API_KEY, config.PINATA_SECRET_KEY); // Initialise mongoDB // MongoDB Connection Url const url = config.MONGO_DB_URL + config.MONGO_DB_NAME; mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, useFindAndModify: false, autoIndex: false, serverSelectionTimeoutMS: 5000, socketTimeoutMS: 45000, family: 4, }); // Establishing MongoDB Connection const { connection } = mongoose; connection.on('error', console.error.bind(console, 'connection error:')); connection.once('open', async () => { // we're connected! // Initialising CLI with current version program.version(`Ajix Server v${version}`, '-v', 'Outputs the current CLI version'); // gets the current helix nodeInfo program.command('getNodeInfo') .description('get helix node info') .action(async () => { await getNodeInfoWrapper(helix); process.exit(0); }); // get ipfs data from pinata program.command('pinFileToIpfs ') .description('fetch data from Ipfs') .action(async (filePath:string) => { await pinFileToIpfsWrapper(pinata, filePath); process.exit(0); }); // publish record to ipfs and helix program.command('publishRecord ') .description('publish record to ipfs and helix') .action(async (filePath:string) => { await publishNewRecord( helix, pinata, filePath, config.HELIX_SEED, config.HELIX_RECIPIENT_ADDR, ); process.exit(0); }); // Fetch publsihed record details from helix network program.command('fetchPayload ') .description('Fetches record details published in helixnetwork transaction') .action(async (helixTransactionHash:string) => { await extractPayloadFromHelixTx(helix, helixTransactionHash); process.exit(0); }); // Fetch record details from database program.command('fetchRecord ') .description('Fetches record from database by record id eg: ajix#1') .action(async (reportId:string) => { await fetchRecordById(reportId); process.exit(0); }); // Fetch publsihed record details from helix network program.command('addMoreFunds') .description('gives an address which needs to be funded for the given seed') .action(async () => { await giveLatestAddress(config.HELIX_SEED); process.exit(0); }); // Fetch publsihed record details from helix network program.command('getFundBalance') .description('gives back the current account balance for the seed') .action(async () => { await getFundBalance(helix, config.HELIX_SEED); process.exit(0); }); await program.parse(process.argv); }); } catch (err) { // eslint-disable-next-line no-console console.error('Check your cli secrets configuration. Error Stack :', err); } } runAjixCLI();