random.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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.randomHex = exports.randomBytes = void 0;
  17. const random_js_1 = require("ethereum-cryptography/random.js");
  18. const converters_js_1 = require("./converters.js");
  19. /**
  20. * Returns a random byte array by the given bytes size
  21. * @param size - The size of the random byte array returned
  22. * @returns - random byte array
  23. *
  24. * @example
  25. * ```ts
  26. * console.log(web3.utils.randomBytes(32));
  27. * > Uint8Array(32) [
  28. * 93, 172, 226, 32, 33, 176, 156, 156,
  29. * 182, 30, 240, 2, 69, 96, 174, 197,
  30. * 33, 136, 194, 241, 197, 156, 110, 111,
  31. * 66, 87, 17, 88, 67, 48, 245, 183
  32. * ]
  33. * ```
  34. */
  35. const randomBytes = (size) => (0, random_js_1.getRandomBytesSync)(size);
  36. exports.randomBytes = randomBytes;
  37. /**
  38. * Returns a random hex string by the given bytes size
  39. * @param byteSize - The size of the random hex string returned
  40. * @returns - random hex string
  41. *
  42. * ```ts
  43. * console.log(web3.utils.randomHex(32));
  44. * > 0x139f5b88b72a25eab053d3b57fe1f8a9dbc62a526b1cb1774d0d7db1c3e7ce9e
  45. * ```
  46. */
  47. const randomHex = (byteSize) => (0, converters_js_1.bytesToHex)((0, exports.randomBytes)(byteSize));
  48. exports.randomHex = randomHex;
  49. //# sourceMappingURL=random.js.map