bloom.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.isContractAddressInBloom = exports.isUserEthereumAddressInBloom = exports.isInBloom = exports.isBloom = void 0;
  17. const keccak_js_1 = require("ethereum-cryptography/keccak.js");
  18. const utils_js_1 = require("../utils.js");
  19. const address_js_1 = require("./address.js");
  20. const string_js_1 = require("./string.js");
  21. /**
  22. * Returns true if the bloom is a valid bloom
  23. * https://github.com/joshstevens19/ethereum-bloom-filters/blob/fbeb47b70b46243c3963fe1c2988d7461ef17236/src/index.ts#L7
  24. */
  25. const isBloom = (bloom) => {
  26. if (typeof bloom !== 'string') {
  27. return false;
  28. }
  29. if (!/^(0x)?[0-9a-f]{512}$/i.test(bloom)) {
  30. return false;
  31. }
  32. if (/^(0x)?[0-9a-f]{512}$/.test(bloom) || /^(0x)?[0-9A-F]{512}$/.test(bloom)) {
  33. return true;
  34. }
  35. return false;
  36. };
  37. exports.isBloom = isBloom;
  38. /**
  39. * Returns true if the value is part of the given bloom
  40. * note: false positives are possible.
  41. */
  42. const isInBloom = (bloom, value) => {
  43. if (typeof value === 'string' && !(0, string_js_1.isHexStrict)(value)) {
  44. return false;
  45. }
  46. if (!(0, exports.isBloom)(bloom)) {
  47. return false;
  48. }
  49. const uint8Array = typeof value === 'string' ? (0, utils_js_1.hexToUint8Array)(value) : value;
  50. const hash = (0, utils_js_1.uint8ArrayToHexString)((0, keccak_js_1.keccak256)(uint8Array)).slice(2);
  51. for (let i = 0; i < 12; i += 4) {
  52. // calculate bit position in bloom filter that must be active
  53. const bitpos =
  54. // eslint-disable-next-line no-bitwise
  55. ((parseInt(hash.slice(i, i + 2), 16) << 8) + parseInt(hash.slice(i + 2, i + 4), 16)) &
  56. 2047;
  57. // test if bitpos in bloom is active
  58. const code = (0, utils_js_1.codePointToInt)(bloom.charCodeAt(bloom.length - 1 - Math.floor(bitpos / 4)));
  59. // eslint-disable-next-line no-bitwise
  60. const offset = 1 << bitpos % 4;
  61. // eslint-disable-next-line no-bitwise
  62. if ((code & offset) !== offset) {
  63. return false;
  64. }
  65. }
  66. return true;
  67. };
  68. exports.isInBloom = isInBloom;
  69. /**
  70. * Returns true if the ethereum users address is part of the given bloom note: false positives are possible.
  71. */
  72. const isUserEthereumAddressInBloom = (bloom, ethereumAddress) => {
  73. if (!(0, exports.isBloom)(bloom)) {
  74. return false;
  75. }
  76. if (!(0, address_js_1.isAddress)(ethereumAddress)) {
  77. return false;
  78. }
  79. // you have to pad the ethereum address to 32 bytes
  80. // else the bloom filter does not work
  81. // this is only if your matching the USERS
  82. // ethereum address. Contract address do not need this
  83. // hence why we have 2 methods
  84. // (0x is not in the 2nd parameter of padleft so 64 chars is fine)
  85. const address = (0, utils_js_1.padLeft)(ethereumAddress, 64);
  86. return (0, exports.isInBloom)(bloom, address);
  87. };
  88. exports.isUserEthereumAddressInBloom = isUserEthereumAddressInBloom;
  89. /**
  90. * Returns true if the contract address is part of the given bloom.
  91. * note: false positives are possible.
  92. */
  93. const isContractAddressInBloom = (bloom, contractAddress) => {
  94. if (!(0, exports.isBloom)(bloom)) {
  95. return false;
  96. }
  97. if (!(0, address_js_1.isAddress)(contractAddress)) {
  98. return false;
  99. }
  100. return (0, exports.isInBloom)(bloom, contractAddress);
  101. };
  102. exports.isContractAddressInBloom = isContractAddressInBloom;
  103. //# sourceMappingURL=bloom.js.map