All files / src/utils utils.js

85.56% Statements 166/194
77.35% Branches 82/106
91.17% Functions 31/34
85.93% Lines 165/192

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511                          26x                         3x 3x 3x 3x 3x   3x 3x     3x 1x 1x     3x 1x   2x       3x                   296x           19x                 8x                 6x   1x 1x   6x 5x                         20x 7x   13x 13x 13x 13x 5x   8x 8x 8x 8x       1x 1x   13x 13x       16x 6x   10x 9x   1x                           10x 1x   9x 1x   8x   8x 14x 14x 13x   13x 4x   9x   1x 1x     8x   1x 1x   8x       2x     2x 2x 2x     2x                           8x 8x   8x 8x       8x 8x 4x 4x   4x 1x   3x 2x   1x                 14x 14x 14x 4x       10x   14x               22x     26x                                     2x         3x                 2x 2x 2x         2x 2x     2x 2x                                   2x                 4x 3x 3x 3x         3x 3x       1x 1x           1x 1x   2x 2x 2x 2x 2x       2x                                                       2x   2x       3x       4x       13x 13x 13x 13x       4x 13x 13x                 13x           4x 4x 4x                 5x 1x     4x   4x         4x 4x 4x 4x 4x 4x 4x 3x   4x   1x   1x 1x               3x 3x     4x     26x 2x 2x 2x 2x 2x 4x 4x 4x 4x     2x   2x     26x 1x 1x 1x 1x 1x                                            
import AElf from 'aelf-sdk';
import moment from 'moment';
import chalk from 'chalk';
import path from 'path';
import { v4 as uuid } from 'uuid';
import fs from 'fs';
import { fileURLToPath } from 'url';
import _camelCase from 'camelcase';
import inquirer from 'inquirer';
import { plainLogger } from './myLogger.js';
import protobuf from '@aelfqueen/protobufjs';
import { createReadStream } from 'fs';
import csv from 'csv-parser';
const { load } = protobuf;
 
/**
 * @typedef {import('ora').Ora} Ora
 * @typedef {import('inquirer').DistinctQuestion} DistinctQuestion
 */
/**
 * Promisifies a function.
 * @param {Function} fn - The function to promisify.
 * @param {boolean} [firstData] - Whether to pass first data.
 * @returns {Function} A promisified function.
 */
function promisify(fn, firstData) {
  return (...args) =>
    new Promise((resolve, reject) => {
      args.push((err, ...result) => {
        let res = result;
        let error = err;
 
        Eif (result.length <= 1) {
          res = result[0];
        }
 
        if (firstData) {
          res = error;
          error = null;
        }
 
        if (error) {
          reject(error);
        } else {
          resolve(res);
        }
      });
 
      fn.call(null, ...args);
    });
}
 
/**
 * Converts a string to camelCase.
 * @param {string} str - The input string.
 * @returns {string} The camelCase version of the string.
 */
function camelCase(str) {
  return _camelCase(str);
}
 
// todo: repository aelf-sdk, add a method that return all contract's name
// so that we can develop a better method to help us identify the aelf's contract
function isAElfContract(str) {
  return str.trim().toLowerCase().startsWith('aelf.');
}
 
/**
 * @description judge if the input is regular expression
 * @param {*} o
 * @returns boolean flag
 */
function isRegExp(o) {
  return o && Object.prototype.toString.call(o) === '[object RegExp]';
}
 
/**
 * Retrieves the list of methods of a contract.
 * @param {Object.<string, any>} [contract] - The contract object.
 * @returns {string[]} An array of method names.
 */
function getContractMethods(contract = {}) {
  if (!contract) {
    // @ts-ignore
    plainLogger.fatal('There is no such contract');
    process.exit(1);
  }
  return Object.keys(contract)
    .filter(v => /^[A-Z]/.test(v))
    .sort();
}
 
/**
 * Retrieves an instance of a contract.
 * @param {string} contractAddress - The address of the contract.
 * @param {any} aelf - The AElf instance.
 * @param {any} wallet - The wallet instance.
 * @param {Ora} oraInstance - The ora instance for logging.
 * @returns {Promise<any>} A promise that resolves to the contract instance.
 */
async function getContractInstance(contractAddress, aelf, wallet, oraInstance) {
  if (typeof contractAddress !== 'string') {
    return contractAddress;
  }
  oraInstance.start('Fetching contract');
  let contract = null;
  try {
    if (!isAElfContract(contractAddress)) {
      contract = await aelf.chain.contractAt(contractAddress, wallet);
    } else {
      const { GenesisContractAddress } = await aelf.chain.getChainStatus();
      const genesisContract = await aelf.chain.contractAt(GenesisContractAddress, wallet);
      const address = await genesisContract.GetContractAddressByName.call(AElf.utils.sha256(contractAddress));
      contract = await aelf.chain.contractAt(address, wallet);
    }
  } catch (e) {
    // @ts-ignore
    oraInstance.fail(plainLogger.error('Failed to find the contract, please enter the correct contract name!'));
    process.exit(1);
  }
  oraInstance.succeed('Fetching contract successfully!');
  return contract;
}
 
function getMethod(method, contract) {
  if (typeof method !== 'string') {
    return method;
  }
  if (contract[method]) {
    return contract[method];
  }
  throw new Error(`Not exist method ${method}`);
}
 
/**
 * Prompts with tolerance for multiple attempts.
 * @param {Object} options - Prompt options.
 * @param {Function} options.processAfterPrompt - Function to process after prompt.
 * @param {string | RegExp} options.pattern - Pattern for the prompt.
 * @param {number} options.times - Number of times to prompt.
 * @param {DistinctQuestion} options.prompt - prompt.
 * @param {Ora} oraInstance - The ora instance for logging.
 * @returns {Promise<Object.<string, any>>} The result of the prompt.
 */
async function promptTolerateSeveralTimes({ processAfterPrompt = () => {}, pattern, times = 3, prompt = [] }, oraInstance) {
  if (pattern && !isRegExp(pattern)) {
    throw new Error("param 'pattern' must be a regular expression!");
  }
  if (processAfterPrompt && typeof processAfterPrompt !== 'function') {
    throw new Error("Param 'processAfterPrompt' must be a function!");
  }
  let askTimes = 0;
  let answerInput;
  while (askTimes < times) {
    try {
      answerInput = await inquirer.prompt(prompt);
      answerInput = await processAfterPrompt(answerInput);
      // @ts-ignore
      if (!pattern || pattern.test(answerInput)) {
        break;
      }
      askTimes++;
    } catch (e) {
      oraInstance.fail('Failed');
      break;
    }
  }
  if (askTimes >= times && answerInput === null) {
    // @ts-ignore
    oraInstance.fail(plainLogger.fatal(`You has entered wrong message ${times} times!`));
    process.exit(1);
  }
  return answerInput;
}
 
function isFilePath(val) {
  Iif (!val) {
    return false;
  }
  const filePath = path.resolve(process.cwd(), val);
  try {
    const stat = fs.statSync(filePath);
    return stat.isFile();
  } catch (e) {
    return false;
  }
}
 
/**
 * Retrieves the result of a transaction.
 * @param {*} aelf - The AElf instance.
 * @param {string} txId - The transaction ID.
 * @param {number} [times] - Number of times to retry.
 * @param {number} [delay] - Delay between retries.
 * @param {number} [timeLimit] - Time limit for retries.
 * @returns {Promise<any>} The transaction result.
 */
async function getTxResult(aelf, txId, times = 0, delay = 3000, timeLimit = 3) {
  const currentTime = times + 1;
  await /** @type {Promise<void>} */ (
    new Promise(resolve => {
      setTimeout(() => {
        resolve();
      }, delay);
    })
  );
  const tx = await aelf.chain.getTxResult(txId);
  if (tx.Status === 'PENDING' && currentTime <= timeLimit) {
    const result = await getTxResult(aelf, txId, currentTime, delay, timeLimit);
    return result;
  }
  if (tx.Status === 'PENDING' && currentTime > timeLimit) {
    return tx;
  }
  if (tx.Status === 'MINED') {
    return tx;
  }
  throw tx;
}
 
/**
 * Parses a JSON string.
 * @param {string} [str] - The JSON string to parse.
 * @returns {*} The parsed JSON object.
 */
function parseJSON(str = '') {
  let result = null;
  try {
    result = JSON.parse(str);
    Iif (typeof result === 'number' && /^-?\d+(\.\d+)?[eE][+-]?\d+$/.test(String(result))) {
      result = str;
    }
  } catch (e) {
    result = str;
  }
  return result;
}
 
/**
 * Generates a random ID.
 * @returns {string} The random ID.
 */
function randomId() {
  return uuid().replace(/-/g, '');
}
 
const PROTO_TYPE_PROMPT_TYPE = {
  '.google.protobuf.Timestamp': {
    type: 'datetime',
    format: ['yyyy', '/', 'mm', '/', 'dd', ' ', 'HH', ':', 'MM'],
    initial: moment()
      .add({
        hours: 1,
        minutes: 5
      })
      .toDate(),
    transformFunc(time) {
      return {
        seconds: moment(time).unix(),
        nanos: moment(time).milliseconds() * 1000
      };
    }
  },
  default: {
    type: 'input',
    transformFunc: v => v
  }
};
 
function isSpecialParameters(inputType) {
  return (
    inputType.fieldsArray &&
    inputType.fieldsArray.length === 1 &&
    ['Hash', 'Address'].includes(inputType.name) &&
    inputType.fieldsArray[0].type === 'bytes'
  );
}
 
async function getParamValue(type, fieldName, rule) {
  let prompts = PROTO_TYPE_PROMPT_TYPE[type] || PROTO_TYPE_PROMPT_TYPE.default;
  const fieldNameWithoutDot = fieldName.replace('.', '');
  prompts = {
    ...prompts,
    name: fieldNameWithoutDot,
    message: `Enter the required param <${fieldName}>:`
  };
  const promptValue = (await inquirer.prompt(prompts))[fieldNameWithoutDot];
  Iif (rule === 'repeated') {
    prompts.transformFunc = v => JSON.parse(v.replace(/'/g, '"'));
  }
  let value = parseJSON(await prompts.transformFunc(promptValue));
  Iif (typeof value === 'string' && isFilePath(value)) {
    const filePath = path.resolve(process.cwd(), value);
 
    const { read } = await inquirer.prompt({
      type: 'confirm',
      name: 'read',
      // eslint-disable-next-line max-len
      message: `It seems that you have entered a file path, do you want to read the file content and take it as the value of <${fieldName}>`
    });
    if (read) {
      try {
        fs.accessSync(filePath, fs.constants.R_OK);
      } catch (err) {
        throw new Error(`permission denied, no read access to file ${filePath}!`);
      }
      value = fs.readFileSync(filePath).toString('base64');
    }
  }
  return value;
}
 
/**
 * Retrieves parameters of a method.
 * @param {*} method - The method.
 * @returns {Promise<Object.<string, any>>} A promise that resolves to the parameters object.
 */
async function getParams(method) {
  const fields = Object.entries(method.inputTypeInfo.fields || {});
  let result = {};
  Eif (fields.length > 0) {
    console.log(
      chalk.yellow(
        '\nIf you need to pass file contents as a parameter, you can enter the relative or absolute path of the file\n'
      )
    );
    console.log('Enter the params one by one, type `Enter` to skip optional param:');
    if (isSpecialParameters(method.inputType)) {
      /**
       * @type {Object.<string, any>}
       */
      let prompts = PROTO_TYPE_PROMPT_TYPE.default;
      prompts = {
        ...prompts,
        name: 'value',
        message: 'Enter the required param <value>:'
      };
 
      const promptValue = (await inquirer.prompt(prompts)).value;
      result = parseJSON(promptValue);
    } else {
      for (const [fieldName, fieldType] of fields) {
        const { type, rule } = fieldType;
        let innerType = null;
        try {
          innerType = method.inputType.lookupType(type);
        } catch (e) {}
        let paramValue;
        // todo: use recursion
        Iif (
          rule !== 'repeated' &&
          innerType &&
          !isSpecialParameters(innerType) &&
          (type || '').indexOf('google.protobuf.Timestamp') === -1
        ) {
          let innerResult = {};
          const innerInputTypeInfo = innerType.toJSON();
          const innerFields = Object.entries(innerInputTypeInfo.fields || {});
          if (isSpecialParameters(innerFields)) {
            /**
             * @type {Object.<string, any>}
             */
            let prompts = PROTO_TYPE_PROMPT_TYPE.default;
            prompts = {
              ...prompts,
              name: 'value',
              message: `Enter the required param <${fieldName}.value>:`
            };
 
            innerResult = (await inquirer.prompt(prompts)).value;
          } else {
            for (const [innerFieldName, innerFieldType] of innerFields) {
              innerResult[innerFieldName] = parseJSON(await getParamValue(innerFieldType.type, `${fieldName}.${innerFieldName}`));
            }
          }
          paramValue = innerResult;
        } else {
          paramValue = await getParamValue(type, fieldName, rule);
        }
        result[fieldName] = parseJSON(paramValue);
      }
    }
  }
  return result;
}
 
async function getProto(aelf, address) {
  return AElf.pbjs.Root.fromDescriptor(await aelf.chain.getContractFileDescriptorSet(address));
}
 
function decodeBase64(str) {
  const { util } = AElf.pbjs;
  const buffer = util.newBuffer(util.base64.length(str));
  util.base64.decode(str, buffer, 0);
  return buffer;
}
 
function getDeserializeLogResult(serializedData, dataType) {
  let deserializeLogResult = serializedData.reduce((acc, v) => {
    let deserialize = dataType.decode(decodeBase64(v));
    deserialize = dataType.toObject(deserialize, {
      enums: String, // enums as string names
      longs: String, // longs as strings (requires long.js)
      bytes: String, // bytes as base64 encoded strings
      defaults: false, // includes default values
      arrays: true, // populates empty arrays (repeated fields) even if defaults=false
      objects: true, // populates empty objects (map fields) even if defaults=false
      oneofs: true // includes virtual oneof fields set to the present field's name
    });
    return {
      ...acc,
      ...deserialize
    };
  }, {});
 
  deserializeLogResult = AElf.utils.transform.transform(dataType, deserializeLogResult, AElf.utils.transform.OUTPUT_TRANSFORMERS);
  deserializeLogResult = AElf.utils.transform.transformArrayToMap(dataType, deserializeLogResult);
  return deserializeLogResult;
}
/**
 * Deserializes logs from AElf.
 * @param {*} aelf - The AElf instance.
 * @param {Array} [logs] - The logs array to deserialize.
 * @returns {Promise<any>} A promise that resolves to the deserialized logs.
 */
async function deserializeLogs(aelf, logs = []) {
  if (!logs || logs.length === 0) {
    return null;
  }
  let dirname;
  try {
    // for test as we cannot use import.meta.url in Jest
    dirname = __dirname;
  } catch {
    const __filename = fileURLToPath(import.meta.url);
    dirname = path.dirname(__filename);
  }
  const filePath = path.resolve(dirname, '../package.json');
  const Root = await load(path.resolve(dirname, '../protobuf/virtual_transaction.proto'));
  let results = await Promise.all(logs.map(v => getProto(aelf, v.Address)));
  results = results.map((proto, index) => {
    const { Name, NonIndexed, Indexed = [] } = logs[index];
    const serializedData = [...Indexed];
    if (NonIndexed) {
      serializedData.push(NonIndexed);
    }
    if (Name === 'VirtualTransactionCreated') {
      // VirtualTransactionCreated is system-default
      try {
        // @ts-ignore
        const dataType = Root.VirtualTransactionCreated;
        return getDeserializeLogResult(serializedData, dataType);
      } catch (e) {
        // if normal contract has a method called VirtualTransactionCreated
        const dataType = proto.lookupType(Name);
        return getDeserializeLogResult(serializedData, dataType);
      }
    } else {
      // other method
      const dataType = proto.lookupType(Name);
      return getDeserializeLogResult(serializedData, dataType);
    }
  });
  return results;
}
 
const parseCSV = async address => {
  let results = [];
  const stream = createReadStream(address).pipe(csv());
  for await (const data of stream) {
    const cleanData = {};
    for (const key in data) {
      const cleanKey = key.replace(/\n/g, '').trim();
      const cleanValue = typeof data[key] === 'string' ? data[key].replace(/\n/g, '').trim() : data[key];
      Eif (cleanValue !== '') {
        cleanData[cleanKey] = cleanValue;
      }
    }
    Object.keys(cleanData).length && results.push(cleanData);
  }
  return results;
};
 
const parseJSONFile = async address => {
  try {
    const absolutePath = path.resolve(address);
    const data = await fs.readFileSync(absolutePath);
    const jsonObject = JSON.parse(data.toString('utf8'));
    return jsonObject;
  } catch (error) {
    throw new Error(`An error occurred while reading or parsing the JSON file: ${error.message}`);
  }
};
 
export {
  promisify,
  camelCase,
  getContractMethods,
  getContractInstance,
  getMethod,
  promptTolerateSeveralTimes,
  isAElfContract,
  getTxResult,
  parseJSON,
  randomId,
  getParams,
  deserializeLogs,
  parseCSV,
  parseJSONFile
};