string.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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.validateNoLeadingZeroes = exports.isHexPrefixed = exports.isHexString32Bytes = exports.isHexString8Bytes = exports.isHex = exports.isHexString = exports.isHexStrict = exports.isString = void 0;
  17. /**
  18. * checks input if typeof data is valid string input
  19. */
  20. const isString = (value) => typeof value === 'string';
  21. exports.isString = isString;
  22. const isHexStrict = (hex) => typeof hex === 'string' && /^((-)?0x[0-9a-f]+|(0x))$/i.test(hex);
  23. exports.isHexStrict = isHexStrict;
  24. /**
  25. * Is the string a hex string.
  26. *
  27. * @param value
  28. * @param length
  29. * @returns output the string is a hex string
  30. */
  31. function isHexString(value, length) {
  32. if (typeof value !== 'string' || !value.match(/^0x[0-9A-Fa-f]*$/))
  33. return false;
  34. if (typeof length !== 'undefined' && length > 0 && value.length !== 2 + 2 * length)
  35. return false;
  36. return true;
  37. }
  38. exports.isHexString = isHexString;
  39. const isHex = (hex) => typeof hex === 'number' ||
  40. typeof hex === 'bigint' ||
  41. (typeof hex === 'string' && /^((-0x|0x|-)?[0-9a-f]+|(0x))$/i.test(hex));
  42. exports.isHex = isHex;
  43. const isHexString8Bytes = (value, prefixed = true) => prefixed ? (0, exports.isHexStrict)(value) && value.length === 18 : (0, exports.isHex)(value) && value.length === 16;
  44. exports.isHexString8Bytes = isHexString8Bytes;
  45. const isHexString32Bytes = (value, prefixed = true) => prefixed ? (0, exports.isHexStrict)(value) && value.length === 66 : (0, exports.isHex)(value) && value.length === 64;
  46. exports.isHexString32Bytes = isHexString32Bytes;
  47. /**
  48. * Returns a `Boolean` on whether or not the a `String` starts with '0x'
  49. * @param str the string input value
  50. * @return a boolean if it is or is not hex prefixed
  51. * @throws if the str input is not a string
  52. */
  53. function isHexPrefixed(str) {
  54. if (typeof str !== 'string') {
  55. throw new Error(`[isHexPrefixed] input must be type 'string', received type ${typeof str}`);
  56. }
  57. return str.startsWith('0x');
  58. }
  59. exports.isHexPrefixed = isHexPrefixed;
  60. /**
  61. * Checks provided Uint8Array for leading zeroes and throws if found.
  62. *
  63. * Examples:
  64. *
  65. * Valid values: 0x1, 0x, 0x01, 0x1234
  66. * Invalid values: 0x0, 0x00, 0x001, 0x0001
  67. *
  68. * Note: This method is useful for validating that RLP encoded integers comply with the rule that all
  69. * integer values encoded to RLP must be in the most compact form and contain no leading zero bytes
  70. * @param values An object containing string keys and Uint8Array values
  71. * @throws if any provided value is found to have leading zero bytes
  72. */
  73. const validateNoLeadingZeroes = function (values) {
  74. for (const [k, v] of Object.entries(values)) {
  75. if (v !== undefined && v.length > 0 && v[0] === 0) {
  76. throw new Error(`${k} cannot have leading zeroes, received: ${v.toString()}`);
  77. }
  78. }
  79. };
  80. exports.validateNoLeadingZeroes = validateNoLeadingZeroes;
  81. //# sourceMappingURL=string.js.map