Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bring in ethereum-bloom-filters #3137

Merged
merged 7 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
110 changes: 85 additions & 25 deletions packages/web3-utils/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/web3-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"dependencies": {
"bn.js": "4.11.8",
"eth-lib": "0.2.7",
"ethereum-bloom-filters": "^1.0.6",
"ethjs-unit": "0.1.6",
"number-to-bn": "1.7.0",
"randombytes": "^2.1.0",
Expand Down
111 changes: 0 additions & 111 deletions packages/web3-utils/src/bloomFilter.js

This file was deleted.

79 changes: 61 additions & 18 deletions packages/web3-utils/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var BN = require('bn.js');
var numberToBN = require('number-to-bn');
var utf8 = require('utf8');
var Hash = require("eth-lib/lib/hash");
var ethereumBloomFilters = require('ethereum-bloom-filters');



/**
Expand Down Expand Up @@ -390,39 +392,76 @@ var isHex = function (hex) {
/**
* Returns true if given string is a valid Ethereum block header bloom.
*
* TODO UNDOCUMENTED
*
* @method isBloom
* @param {String} hex encoded bloom filter
* @return {Boolean}
*/
var isBloom = function (bloom) {
if (!/^(0x)?[0-9a-f]{512}$/i.test(bloom)) {
return false;
} else if (/^(0x)?[0-9a-f]{512}$/.test(bloom) || /^(0x)?[0-9A-F]{512}$/.test(bloom)) {
return true;
}
return false;
return ethereumBloomFilters.isBloom(bloom);
};

/**
* Returns true if given string is a valid log topic.
* Returns true if the ethereum users address is part of the given bloom
* note: false positives are possible.
*
* TODO UNDOCUMENTED
* @method isUserEthereumAddressInBloom
* @param {String} hex encoded bloom filter
* @param {String} hex ethereum addresss
* @return {Boolean}
*/
var isUserEthereumAddressInBloom = function (bloom, ethereumAddress) {
return ethereumBloomFilters.isUserEthereumAddressInBloom(bloom, ethereumAddress);
};

/**
* Returns true if the contract address is part of the given bloom
* note: false positives are possible.
*
* @method isUserEthereumAddressInBloom
* @param {String} hex encoded bloom filter
* @param {String} hex contract addresss
* @return {Boolean}
*/
var isContractAddressInBloom = function (bloom, contractAddress) {
return ethereumBloomFilters.isContractAddressInBloom(bloom, contractAddress);
};

/**
* Returns true if given string is a valid log topic.
*
* @method isTopic
* @param {String} hex encoded topic
* @return {Boolean}
*/
var isTopic = function (topic) {
if (!/^(0x)?[0-9a-f]{64}$/i.test(topic)) {
return false;
} else if (/^(0x)?[0-9a-f]{64}$/.test(topic) || /^(0x)?[0-9A-F]{64}$/.test(topic)) {
return true;
}
return false;
return ethereumBloomFilters.isTopic(topic);
};

/**
* Returns true if the topic is part of the given bloom
* note: false positives are possible.
*
* @method isTopicInBloom
* @param {String} hex encoded bloom filter
* @param {String} hex encoded topic
* @return {Boolean}
*/
var isTopicInBloom = function (bloom, topic) {
return ethereumBloomFilters.isTopicInBloom(bloom, topic);
};

/**
* Returns true if the value is part of the given bloom
* note: false positives are possible.
*
* @method isInBloom
* @param {String} hex encoded bloom filter
* @param {String | Uint8Array} topic encoded value
* @return {Boolean}
*/
var isInBloom = function (bloom, topic) {
return ethereumBloomFilters.isInBloom(bloom, topic);
};

/**
* Hashes values to a sha3 hash using keccak 256
Expand Down Expand Up @@ -461,8 +500,12 @@ module.exports = {
isBigNumber: isBigNumber,
toBN: toBN,
isAddress: isAddress,
isBloom: isBloom, // TODO UNDOCUMENTED
isTopic: isTopic, // TODO UNDOCUMENTED
isBloom: isBloom,
isUserEthereumAddressInBloom: isUserEthereumAddressInBloom,
isContractAddressInBloom: isContractAddressInBloom,
isTopic: isTopic,
isTopicInBloom: isTopicInBloom,
isInBloom: isInBloom,
checkAddressChecksum: checkAddressChecksum,
utf8ToHex: utf8ToHex,
hexToUtf8: hexToUtf8,
Expand Down
8 changes: 8 additions & 0 deletions packages/web3-utils/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ export function toUtf8(string: string): string;
export function toWei(val: BN, unit?: Unit): BN;
export function toWei(val: string, unit?: Unit): string;
export function isBloom(bloom: string): boolean;
export function isInBloom(bloom: string, value: string | Uint8Array): boolean;
export function isUserEthereumAddressInBloom(bloom: string, ethereumAddress: string): boolean;
export function isContractAddressInBloom(bloom: string, contractAddress: string): boolean;
export function isTopicInBloom(bloom: string, topic: string): boolean;
export function isTopic(topic: string): boolean;
export function jsonInterfaceMethodToString(abiItem: AbiItem): string;
export function soliditySha3(...val: Mixed[]): string;
Expand Down Expand Up @@ -156,6 +160,10 @@ export interface Utils {
toWei(val: BN, unit?: Unit): BN;
toWei(val: string, unit?: Unit): string;
isBloom(bloom: string): boolean;
isInBloom(bloom: string, value: string | Uint8Array): boolean;
isUserEthereumAddressInBloom(bloom: string, ethereumAddress: string): boolean;
isContractAddressInBloom(bloom: string, contractAddress: string): boolean;
isTopicInBloom(bloom: string, topic: string): boolean;
isTopic(topic: string): boolean;
jsonInterfaceMethodToString(abiItem: AbiItem): string;
soliditySha3(...val: Mixed[]): string;
Expand Down
4 changes: 2 additions & 2 deletions packages/web3-utils/types/tests/is-bloom-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
/**
* @file is-bloom-tests.ts
* @author Josh Stevens <joshstevens19@hotmail.co.uk>
* @date 2018
* @date 2019
*/

import BN = require('bn.js');
import {isBloom} from 'web3-utils';
import { isBloom } from 'web3-utils';

// $ExpectType boolean
isBloom('0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef');
Expand Down
26 changes: 26 additions & 0 deletions packages/web3-utils/types/tests/is-contract-address-in-bloom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
This file is part of web3.js.

web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file is-contract-address-in-bloom.ts
* @author Josh Stevens <joshstevens19@hotmail.co.uk>
* @date 2019
*/

import { isContractAddressInBloom } from 'web3-utils';

// $ExpectType boolean
isContractAddressInBloom('0x08200081a06415012858022200cc48143008908c0000824e5405b41520795989024800380a8d4b198910b422b231086c3a62cc402e2573070306f180446440ad401016c3e30781115844d028c89028008a12240c0a2c184c0425b90d7af0530002f981221aa565809132000818c82805023a132a25150400010530ba0080420a10a137054454021882505080a6b6841082d84151010400ba8100c8802d440d060388084052c1300105a0868410648a40540c0f0460e190400807008914361118000a5202e94445ccc088311050052c8002807205212a090d90ba428030266024a910644b1042011aaae05391cc2094c45226400000380880241282ce4e12518c', '0x494bfa3a4576ba6cfe835b0deb78834f0c3e3994');
26 changes: 26 additions & 0 deletions packages/web3-utils/types/tests/is-in-bloom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
This file is part of web3.js.

web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file is-in-bloom-tests.ts
* @author Josh Stevens <joshstevens19@hotmail.co.uk>
* @date 2019
*/

import { isInBloom } from 'web3-utils';

// $ExpectType boolean
isInBloom('0x08200081a06415012858022200cc48143008908c0000824e5405b41520795989024800380a8d4b198910b422b231086c3a62cc402e2573070306f180446440ad401016c3e30781115844d028c89028008a12240c0a2c184c0425b90d7af0530002f981221aa565809132000818c82805023a132a25150400010530ba0080420a10a137054454021882505080a6b6841082d84151010400ba8100c8802d440d060388084052c1300105a0868410648a40540c0f0460e190400807008914361118000a5202e94445ccc088311050052c8002807205212a090d90ba428030266024a910644b1042011aaae05391cc2094c45226400000380880241282ce4e12518c', '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef');
26 changes: 26 additions & 0 deletions packages/web3-utils/types/tests/is-topic-in-bloom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
This file is part of web3.js.

web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file is-topic-in-bloom.ts
* @author Josh Stevens <joshstevens19@hotmail.co.uk>
* @date 2019
*/

import { isTopicInBloom } from 'web3-utils';

// $ExpectType boolean
isTopicInBloom('0x08200081a06415012858022200cc48143008908c0000824e5405b41520795989024800380a8d4b198910b422b231086c3a62cc402e2573070306f180446440ad401016c3e30781115844d028c89028008a12240c0a2c184c0425b90d7af0530002f981221aa565809132000818c82805023a132a25150400010530ba0080420a10a137054454021882505080a6b6841082d84151010400ba8100c8802d440d060388084052c1300105a0868410648a40540c0f0460e190400807008914361118000a5202e94445ccc088311050052c8002807205212a090d90ba428030266024a910644b1042011aaae05391cc2094c45226400000380880241282ce4e12518c', '0x4d61726b65745061792e696f206973206465706c6f79696e6720536d61727420');
26 changes: 26 additions & 0 deletions packages/web3-utils/types/tests/is-topic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
This file is part of web3.js.

web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file is-user-ethereum-address-in-bloom.ts
* @author Josh Stevens <joshstevens19@hotmail.co.uk>
* @date 2019
*/

import { isTopic } from 'web3-utils';

// $ExpectType boolean
isTopic('0x000000000000000000000000b3bb037d2f2341a1c2775d51909a3d944597987d');