-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.ts
190 lines (159 loc) · 4.29 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { bytesToHex, keccak256, padLeft } from './utils';
/**
* Returns true if the bloom is a valid bloom
* @param bloom The bloom
*/
export function isBloom(bloom: string): boolean {
if (typeof bloom !== 'string') {
return false;
}
if (!/^(0x)?[0-9a-f]{512}$/i.test(bloom)) {
return false;
}
if (
/^(0x)?[0-9a-f]{512}$/.test(bloom) ||
/^(0x)?[0-9A-F]{512}$/.test(bloom)
) {
return true;
}
return false;
}
/**
* Returns true if the value is part of the given bloom
* note: false positives are possible.
* @param bloom encoded bloom
* @param value The value
*/
export function isInBloom(bloom: string, value: string | Uint8Array): boolean {
if (typeof value === 'object' && value.constructor === Uint8Array) {
value = bytesToHex(value);
}
const hash = keccak256(value).replace('0x', '');
for (let i = 0; i < 12; i += 4) {
// calculate bit position in bloom filter that must be active
const bitpos =
((parseInt(hash.substr(i, 2), 16) << 8) +
parseInt(hash.substr(i + 2, 2), 16)) &
2047;
// test if bitpos in bloom is active
const code = codePointToInt(
bloom.charCodeAt(bloom.length - 1 - Math.floor(bitpos / 4)),
);
const offset = 1 << bitpos % 4;
if ((code & offset) !== offset) {
return false;
}
}
return true;
}
/**
* Code points to int
* @param codePoint The code point
*/
function codePointToInt(codePoint: number): number {
if (codePoint >= 48 && codePoint <= 57) {
/* ['0'..'9'] -> [0..9] */
return codePoint - 48;
}
if (codePoint >= 65 && codePoint <= 70) {
/* ['A'..'F'] -> [10..15] */
return codePoint - 55;
}
if (codePoint >= 97 && codePoint <= 102) {
/* ['a'..'f'] -> [10..15] */
return codePoint - 87;
}
throw new Error('invalid bloom');
}
/**
* Returns true if the ethereum users address is part of the given bloom.
* note: false positives are possible.
* @param bloom encoded bloom
* @param address the address to test
*/
export function isUserEthereumAddressInBloom(
bloom: string,
ethereumAddress: string,
): boolean {
if (!isBloom(bloom)) {
throw new Error('Invalid bloom given');
}
if (!isAddress(ethereumAddress)) {
throw new Error(`Invalid ethereum address given: "${ethereumAddress}"`);
}
// you have to pad the ethereum address to 32 bytes
// else the bloom filter does not work
// this is only if your matching the USERS
// ethereum address. Contract address do not need this
// hence why we have 2 methods
// (0x is not in the 2nd parameter of padleft so 64 chars is fine)
const address = padLeft(ethereumAddress, 64);
return isInBloom(bloom, address);
}
/**
* Returns true if the contract address is part of the given bloom.
* note: false positives are possible.
* @param bloom encoded bloom
* @param contractAddress the contract address to test
*/
export function isContractAddressInBloom(
bloom: string,
contractAddress: string,
): boolean {
if (!isBloom(bloom)) {
throw new Error('Invalid bloom given');
}
if (!isAddress(contractAddress)) {
throw new Error(`Invalid contract address given: "${contractAddress}"`);
}
return isInBloom(bloom, contractAddress);
}
/**
* Returns true if the topic is part of the given bloom.
* note: false positives are possible.
* @param bloom encoded bloom
* @param topic the topic encoded hex
*/
export function isTopicInBloom(bloom: string, topic: string): boolean {
if (!isBloom(bloom)) {
throw new Error('Invalid bloom given');
}
if (!isTopic(topic)) {
throw new Error('Invalid topic');
}
return isInBloom(bloom, topic);
}
/**
* Checks if its a valid topic
* @param topic encoded hex topic
*/
export function isTopic(topic: string): boolean {
if (typeof topic !== 'string') {
return false;
}
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;
}
/**
* Is valid address
* @param address The address
*/
export function isAddress(address: string): boolean {
if (typeof address !== 'string') {
return false;
}
if (address.match(/^(0x)?[0-9a-fA-F]{40}$/)) {
return true;
}
if (address.match(/^XE[0-9]{2}[0-9A-Za-z]{30,31}$/)) {
return true;
}
return false;
}