validation.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. "use strict";
  2. /*
  3. This file is part of web3.js.
  4. web3.js is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. web3.js is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with web3.js. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. exports.isNullish = exports.compareBlockNumbers = exports.isTopicInBloom = exports.isTopic = exports.isContractAddressInBloom = exports.isUserEthereumAddressInBloom = exports.isInBloom = exports.isBloom = exports.isAddress = exports.checkAddressCheckSum = exports.isHex = exports.isHexStrict = void 0;
  17. const web3_errors_1 = require("web3-errors");
  18. const web3_validator_1 = require("web3-validator");
  19. const web3_types_1 = require("web3-types");
  20. /**
  21. * @deprecated Will be removed in next release. Please use `web3-validator` package instead.
  22. */
  23. exports.isHexStrict = web3_validator_1.isHexStrict;
  24. /**
  25. * returns true if input is a hexstring, number or bigint
  26. *
  27. * @deprecated Will be removed in next release. Please use `web3-validator` package instead.
  28. */
  29. exports.isHex = web3_validator_1.isHex;
  30. /**
  31. * Checks the checksum of a given address. Will also return false on non-checksum addresses.
  32. *
  33. * @deprecated Will be removed in next release. Please use `web3-validator` package instead.
  34. */
  35. exports.checkAddressCheckSum = web3_validator_1.checkAddressCheckSum;
  36. /**
  37. * Checks if a given string is a valid Ethereum address. It will also check the checksum, if the address has upper and lowercase letters.
  38. *
  39. * @deprecated Will be removed in next release. Please use `web3-validator` package instead.
  40. */
  41. exports.isAddress = web3_validator_1.isAddress;
  42. /**
  43. * Returns true if the bloom is a valid bloom
  44. * https://github.com/joshstevens19/ethereum-bloom-filters/blob/fbeb47b70b46243c3963fe1c2988d7461ef17236/src/index.ts#L7
  45. *
  46. * @deprecated Will be removed in next release. Please use `web3-validator` package instead.
  47. */
  48. exports.isBloom = web3_validator_1.isBloom;
  49. /**
  50. * Returns true if the value is part of the given bloom
  51. * note: false positives are possible.
  52. *
  53. * @deprecated Will be removed in next release. Please use `web3-validator` package instead.
  54. */
  55. exports.isInBloom = web3_validator_1.isInBloom;
  56. /**
  57. * Returns true if the ethereum users address is part of the given bloom note: false positives are possible.
  58. *
  59. * @deprecated Will be removed in next release. Please use `web3-validator` package instead.
  60. */
  61. exports.isUserEthereumAddressInBloom = web3_validator_1.isUserEthereumAddressInBloom;
  62. /**
  63. * Returns true if the contract address is part of the given bloom.
  64. * note: false positives are possible.
  65. *
  66. * @deprecated Will be removed in next release. Please use `web3-validator` package instead.
  67. */
  68. exports.isContractAddressInBloom = web3_validator_1.isContractAddressInBloom;
  69. /**
  70. * Checks if its a valid topic
  71. *
  72. * @deprecated Will be removed in next release. Please use `web3-validator` package instead.
  73. */
  74. exports.isTopic = web3_validator_1.isTopic;
  75. /**
  76. * Returns true if the topic is part of the given bloom.
  77. * note: false positives are possible.
  78. *
  79. * @deprecated Will be removed in next release. Please use `web3-validator` package instead.
  80. */
  81. exports.isTopicInBloom = web3_validator_1.isTopicInBloom;
  82. /**
  83. * Compares between block A and block B
  84. * @param blockA - Block number or string
  85. * @param blockB - Block number or string
  86. *
  87. * @returns - Returns -1 if a \< b, returns 1 if a \> b and returns 0 if a == b
  88. *
  89. * @example
  90. * ```ts
  91. * console.log(web3.utils.compareBlockNumbers('latest', 'pending'));
  92. * > -1
  93. *
  94. * console.log(web3.utils.compareBlockNumbers(12, 11));
  95. * > 1
  96. * ```
  97. */
  98. const compareBlockNumbers = (blockA, blockB) => {
  99. const isABlockTag = typeof blockA === 'string' && (0, web3_validator_1.isBlockTag)(blockA);
  100. const isBBlockTag = typeof blockB === 'string' && (0, web3_validator_1.isBlockTag)(blockB);
  101. if (blockA === blockB ||
  102. ((blockA === 'earliest' || blockA === 0) && (blockB === 'earliest' || blockB === 0)) // only exception compare blocktag with number
  103. ) {
  104. return 0;
  105. }
  106. if (blockA === 'earliest' && blockB > 0) {
  107. return -1;
  108. }
  109. if (blockB === 'earliest' && blockA > 0) {
  110. return 1;
  111. }
  112. if (isABlockTag && isBBlockTag) {
  113. // Increasing order: earliest, finalized , safe, latest, pending
  114. const tagsOrder = {
  115. [web3_types_1.BlockTags.EARLIEST]: 1,
  116. [web3_types_1.BlockTags.FINALIZED]: 2,
  117. [web3_types_1.BlockTags.SAFE]: 3,
  118. [web3_types_1.BlockTags.LATEST]: 4,
  119. [web3_types_1.BlockTags.PENDING]: 5,
  120. };
  121. if (tagsOrder[blockA] < tagsOrder[blockB]) {
  122. return -1;
  123. }
  124. return 1;
  125. }
  126. if ((isABlockTag && !isBBlockTag) || (!isABlockTag && isBBlockTag)) {
  127. throw new web3_errors_1.InvalidBlockError('Cannot compare blocktag with provided non-blocktag input.');
  128. }
  129. const bigIntA = BigInt(blockA);
  130. const bigIntB = BigInt(blockB);
  131. if (bigIntA < bigIntB) {
  132. return -1;
  133. }
  134. if (bigIntA === bigIntB) {
  135. return 0;
  136. }
  137. return 1;
  138. };
  139. exports.compareBlockNumbers = compareBlockNumbers;
  140. exports.isNullish = web3_validator_1.isNullish;
  141. //# sourceMappingURL=validation.js.map