PHP WebShell

Текущая директория: /usr/lib/node_modules/bitgo/node_modules/@bitgo/sdk-coin-hbar/node_modules/@hashgraph/sdk/lib

Просмотр файла: util.cjs

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.REQUIRE_UINT8ARRAY_ERROR = exports.REQUIRE_TYPE_ERROR = exports.REQUIRE_STRING_OR_UINT8ARRAY_ERROR = exports.REQUIRE_STRING_ERROR = exports.REQUIRE_NUMBER_ERROR = exports.REQUIRE_NON_NULL_ERROR = exports.REQUIRE_LONG_ERROR = exports.REQUIRE_BIGNUMBER_ERROR = exports.REQUIRE_ARRAY_ERROR = exports.FUNCTION_CONVERT_TO_NUMBER_PARSE_ERROR = exports.FUNCTION_CONVERT_TO_NUMBER_ERROR = exports.FUNCTION_CONVERT_TO_BIGNUMBER_ERROR = void 0;
exports.arrayEqual = arrayEqual;
exports.compare = compare;
exports.convertToBigNumber = convertToBigNumber;
exports.convertToBigNumberArray = convertToBigNumberArray;
exports.convertToNumber = convertToNumber;
exports.isBigNumber = isBigNumber;
exports.isLong = isLong;
exports.isNonNull = isNonNull;
exports.isNumber = isNumber;
exports.isString = isString;
exports.isStringOrUint8Array = isStringOrUint8Array;
exports.isType = isType;
exports.isUint8Array = isUint8Array;
exports.requireBigNumber = requireBigNumber;
exports.requireLong = requireLong;
exports.requireNonNull = requireNonNull;
exports.requireNotNegative = requireNotNegative;
exports.requireNumber = requireNumber;
exports.requireString = requireString;
exports.requireStringOrUint8Array = requireStringOrUint8Array;
exports.requireType = requireType;
exports.requireUint8Array = requireUint8Array;
exports.safeView = safeView;
exports.shuffle = shuffle;
var _bignumber = _interopRequireDefault(require("bignumber.js"));
var _long = _interopRequireDefault(require("long"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/*-
 * ‌
 * Hedera JavaScript SDK
 * ​
 * Copyright (C) 2020 - 2022 Hedera Hashgraph, LLC
 * ​
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ‍
 */

/**
 * @typedef {import("./Hbar.js").default} Hbar
 */

/**
 * Utility Error Messages
 */
const REQUIRE_NON_NULL_ERROR = "This value cannot be null | undefined.";
exports.REQUIRE_NON_NULL_ERROR = REQUIRE_NON_NULL_ERROR;
const REQUIRE_STRING_ERROR = "This value must be a string.";
exports.REQUIRE_STRING_ERROR = REQUIRE_STRING_ERROR;
const REQUIRE_UINT8ARRAY_ERROR = "This value must be a Uint8Array.";
exports.REQUIRE_UINT8ARRAY_ERROR = REQUIRE_UINT8ARRAY_ERROR;
const REQUIRE_STRING_OR_UINT8ARRAY_ERROR = "This value must be a string or Uint8Array.";
exports.REQUIRE_STRING_OR_UINT8ARRAY_ERROR = REQUIRE_STRING_OR_UINT8ARRAY_ERROR;
const REQUIRE_NUMBER_ERROR = "This value must be a Number.";
exports.REQUIRE_NUMBER_ERROR = REQUIRE_NUMBER_ERROR;
const REQUIRE_BIGNUMBER_ERROR = "This value must be a BigNumber.";
exports.REQUIRE_BIGNUMBER_ERROR = REQUIRE_BIGNUMBER_ERROR;
const REQUIRE_ARRAY_ERROR = "The provided variable must be an Array.";
exports.REQUIRE_ARRAY_ERROR = REQUIRE_ARRAY_ERROR;
const REQUIRE_LONG_ERROR = "This value must be a Long.";
exports.REQUIRE_LONG_ERROR = REQUIRE_LONG_ERROR;
const REQUIRE_TYPE_ERROR = "The provided variables are not matching types.";
exports.REQUIRE_TYPE_ERROR = REQUIRE_TYPE_ERROR;
const FUNCTION_CONVERT_TO_BIGNUMBER_ERROR = "This value must be a String, Number, or BigNumber to be converted.";
exports.FUNCTION_CONVERT_TO_BIGNUMBER_ERROR = FUNCTION_CONVERT_TO_BIGNUMBER_ERROR;
const FUNCTION_CONVERT_TO_NUMBER_ERROR = "This value must be a String, Number, or BigNumber to be converted.";
exports.FUNCTION_CONVERT_TO_NUMBER_ERROR = FUNCTION_CONVERT_TO_NUMBER_ERROR;
const FUNCTION_CONVERT_TO_NUMBER_PARSE_ERROR = "Unable to parse given variable. Returns NaN.";

//Soft Checks

/**
 * Takes any param and returns false if null or undefined.
 *
 * @param {any | null | undefined} variable
 * @returns {boolean}
 */
exports.FUNCTION_CONVERT_TO_NUMBER_PARSE_ERROR = FUNCTION_CONVERT_TO_NUMBER_PARSE_ERROR;
function isNonNull(variable) {
  return variable != null;
}

/**
 * Takes any param and returns true if param variable and type are the same.
 *
 * @param {any | null | undefined} variable
 * @param {any | null | undefined} type
 * @returns {boolean}
 */
function isType(variable, type) {
  return typeof variable == typeof type;
}

/**
 * Takes any param and returns true if param is not null and of type Uint8Array.
 *
 * @param {any | null | undefined} variable
 * @returns {boolean}
 */
function isUint8Array(variable) {
  return isNonNull(variable) && variable instanceof Uint8Array;
}

/**
 * Takes any param and returns true if param is not null and of type Number.
 *
 * @param {any | null | undefined} variable
 * @returns {boolean}
 */
function isNumber(variable) {
  return isNonNull(variable) && (typeof variable == "number" || variable instanceof Number);
}

/**
 * Takes any param and returns true if param is not null and of type BigNumber.
 *
 * @param {any | null | undefined} variable
 * @returns {boolean}
 */
function isBigNumber(variable) {
  return isNonNull(variable) && variable instanceof _bignumber.default;
}

/**
 * Takes any param and returns true if param is not null and of type BigNumber.
 *
 * @param {any | null | undefined} variable
 * @returns {boolean}
 */
function isLong(variable) {
  return isNonNull(variable) && variable instanceof _long.default;
}

/**
 * Takes any param and returns true if param is not null and of type string.
 *
 * @param {any | null | undefined} variable
 * @returns {boolean}
 */
function isString(variable) {
  return isNonNull(variable) && typeof variable == "string";
}

/**
 * Takes any param and returns true if param is not null and type string or Uint8Array.
 *
 * @param {any | null | undefined} variable
 * @returns {boolean}
 */
function isStringOrUint8Array(variable) {
  return isNonNull(variable) && (isString(variable) || isUint8Array(variable));
}

/**
 * Takes any param and returns false if null or undefined.
 *
 * @template {Long | Hbar} T
 * @param {T} variable
 * @returns {T}
 */
function requireNotNegative(variable) {
  if (variable.isNegative()) {
    throw new Error("negative value not allowed");
  }
  return variable;
}

/**
 * Takes any param and throws custom error if null or undefined.
 *
 * @param {any} variable
 * @returns {object}
 */
function requireNonNull(variable) {
  if (!isNonNull(variable)) {
    throw new Error(REQUIRE_NON_NULL_ERROR);
  } else {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-return
    return variable;
  }
}

/**
 * Takes any param and throws custom error if params are not same type.
 *
 * @param {any | null | undefined} variable
 * @param {any | null | undefined} type
 * @returns {object}
 */
function requireType(variable, type) {
  if (!isType(variable, type)) {
    throw new Error(REQUIRE_TYPE_ERROR);
  } else {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-return
    return variable;
  }
}

/**
 * Takes any param and throws custom error if non BigNumber.
 *
 * @param {any | null | undefined} variable
 * @returns {BigNumber}
 */
function requireBigNumber(variable) {
  if (!isBigNumber(requireNonNull(variable))) {
    throw new Error(REQUIRE_BIGNUMBER_ERROR);
  } else {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-return
    return (/** @type {BigNumber} */variable
    );
  }
}

/**
 * Takes any param and throws custom error if non BigNumber.
 *
 * @param {any | null | undefined} variable
 * @returns {Long}
 */
function requireLong(variable) {
  if (!isLong(requireNonNull(variable))) {
    throw new Error(REQUIRE_LONG_ERROR);
  } else {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-return
    return (/** @type {Long} */variable
    );
  }
}

/**
 * Takes any param and throws custom error if non string.
 *
 * @param {any | null | undefined} variable
 * @returns {string}
 */
function requireString(variable) {
  if (!isString(requireNonNull(variable))) {
    throw new Error(REQUIRE_STRING_ERROR);
  } else {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-return
    return (/** @type {string} */variable
    );
  }
}

/**
 * Takes any param and throws custom error if non Uint8Array.
 *
 * @param {any | null | undefined} variable
 * @returns {Uint8Array}
 */
function requireUint8Array(variable) {
  if (!isUint8Array(requireNonNull(variable))) {
    throw new Error(REQUIRE_UINT8ARRAY_ERROR);
  } else {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-return
    return (/** @type {Uint8Array} */variable
    );
  }
}

/**
 * Takes any param and throws custom error if non Uint8Array.
 *
 * @param {any | null | undefined} variable
 * @returns {number}
 */
function requireNumber(variable) {
  if (!isNumber(requireNonNull(variable))) {
    throw new Error(REQUIRE_NUMBER_ERROR);
  } else {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-return
    return (/** @type {number} */variable
    );
  }
}

/**
 * Takes any param and throws custom error if null or undefined and not a string or Uint8Array.
 *
 * @param {any | null | undefined} variable
 * @returns {string | Uint8Array}
 */
function requireStringOrUint8Array(variable) {
  if (isStringOrUint8Array(requireNonNull(variable))) {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-return
    return (/** @type {string | Uint8Array} */variable
    );
  } else {
    throw new Error(REQUIRE_STRING_OR_UINT8ARRAY_ERROR);
  }
}

//Conversions

/**
 * Converts number or string to BigNumber.
 *
 * @param {any | null | undefined} variable
 * @returns {BigNumber}
 */
function convertToBigNumber(variable) {
  requireNonNull(variable);
  if (isBigNumber(variable) || isString(variable) || isNumber(variable) || isLong(variable)) {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
    return new _bignumber.default(variable);
  }
  throw new Error(FUNCTION_CONVERT_TO_BIGNUMBER_ERROR);
}

/**
 * Converts Array of Numbers or Strings to Array of BigNumbers.
 *
 * @param {any | null | undefined} variable
 * @returns {Array<BigNumber>}
 */
function convertToBigNumberArray(variable) {
  if (variable instanceof Array) {
    return (/** @type {Array<BigNumber>} */
      variable.map(convertToBigNumber)
    );
  } else {
    throw new Error(REQUIRE_ARRAY_ERROR);
  }
}

/**
 * @param {*} variable
 * @returns {number}
 */
function convertToNumber(variable) {
  requireNonNull(variable);
  if (isBigNumber(variable) || isString(variable) || isNumber(variable) || isLong(variable)) {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
    const num = parseInt(variable);
    if (isNaN(num)) {
      throw new Error(FUNCTION_CONVERT_TO_NUMBER_PARSE_ERROR);
    } else {
      return num;
    }
  } else {
    throw new Error(FUNCTION_CONVERT_TO_NUMBER_ERROR);
  }
}

/**
 * Creates a DataView on top of an Uint8Array that could be or not be pooled, ensuring that we don't get out of bounds.
 *
 * @param {Uint8Array | Int8Array} arr
 * @param {number | undefined} offset
 * @param {number | undefined} length
 * @returns {DataView}
 */
function safeView(arr, offset = 0, length = arr.byteLength) {
  if (!(Number.isInteger(offset) && offset >= 0)) throw new Error("Invalid offset!");
  if (!(Number.isInteger(length) && length >= 0)) throw new Error("Invalid length!");
  return new DataView(arr.buffer, arr.byteOffset + offset, Math.min(length, arr.byteLength - offset));
}

/**
 * @param {any} a
 * @param {any} b
 * @param {Set<string>=} ignore
 * @returns {boolean}
 */
function compare(a, b, ignore = new Set()) {
  if (typeof a === "object" && typeof b === "object") {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
    const aKeys = Object.keys(a);
    // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
    const bKeys = Object.keys(b);
    if (aKeys.length !== bKeys.length) {
      return false;
    }
    for (let i = 0; i < aKeys.length; i++) {
      if (aKeys[i] !== bKeys[i]) {
        return false;
      }
      if (ignore.has(aKeys[i])) {
        continue;
      }

      // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
      if (!compare(a[aKeys[i]], b[bKeys[i]], ignore)) {
        return false;
      }
    }
    return true;
  } else if (typeof a === "number" && typeof b === "number") {
    return a === b;
  } else if (typeof a === "string" && typeof b === "string") {
    return a === b;
  } else if (typeof a === "boolean" && typeof b === "boolean") {
    return a === b;
  } else {
    return false;
  }
}

/**
 * https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
 *
 * @template T
 * @param {Array<T>} array
 */
function shuffle(array) {
  var currentIndex = array.length,
    temporaryValue,
    randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {
    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }
}

/**
 * @param {Uint8Array} array1
 * @param {Uint8Array} array2
 * @returns {boolean}
 */
function arrayEqual(array1, array2) {
  if (array1 === array2) {
    return true;
  }
  if (array1.byteLength !== array2.byteLength) {
    return false;
  }
  const view1 = new DataView(array1.buffer, array1.byteOffset, array1.byteLength);
  const view2 = new DataView(array2.buffer, array2.byteOffset, array2.byteLength);
  let i = array1.byteLength;
  while (i--) {
    if (view1.getUint8(i) !== view2.getUint8(i)) {
      return false;
    }
  }
  return true;
}

Выполнить команду


Для локальной разработки. Не используйте в интернете!