uint8array.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.uint8ArrayEquals = exports.uint8ArrayConcat = void 0;
  4. /*
  5. This file is part of web3.js.
  6. web3.js is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Lesser General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. web3.js is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with web3.js. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. function uint8ArrayConcat(...parts) {
  18. const length = parts.reduce((prev, part) => {
  19. const agg = prev + part.length;
  20. return agg;
  21. }, 0);
  22. const result = new Uint8Array(length);
  23. let offset = 0;
  24. for (const part of parts) {
  25. result.set(part, offset);
  26. offset += part.length;
  27. }
  28. return result;
  29. }
  30. exports.uint8ArrayConcat = uint8ArrayConcat;
  31. /**
  32. * Returns true if the two passed Uint8Arrays have the same content
  33. */
  34. function uint8ArrayEquals(a, b) {
  35. if (a === b) {
  36. return true;
  37. }
  38. if (a.byteLength !== b.byteLength) {
  39. return false;
  40. }
  41. for (let i = 0; i < a.byteLength; i += 1) {
  42. if (a[i] !== b[i]) {
  43. return false;
  44. }
  45. }
  46. return true;
  47. }
  48. exports.uint8ArrayEquals = uint8ArrayEquals;
  49. //# sourceMappingURL=uint8array.js.map