forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
txfilter.c
54 lines (41 loc) · 1.26 KB
/
txfilter.c
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
#include "txfilter.h"
#include <bitcoin/script.h>
#include <ccan/crypto/ripemd160/ripemd160.h>
#include <common/utils.h>
struct txfilter {
u8 **scriptpubkeys;
};
struct txfilter *txfilter_new(const tal_t *ctx)
{
struct txfilter *filter = tal(ctx, struct txfilter);
filter->scriptpubkeys = tal_arr(filter, u8*, 0);
return filter;
}
void txfilter_add_scriptpubkey(struct txfilter *filter, u8 *script)
{
size_t count = tal_count(filter->scriptpubkeys);
tal_resize(&filter->scriptpubkeys, count + 1);
filter->scriptpubkeys[count] = tal_dup_arr(filter, u8, script, tal_len(script), 0);
}
void txfilter_add_derkey(struct txfilter *filter, u8 derkey[PUBKEY_DER_LEN])
{
tal_t *tmpctx = tal_tmpctx(filter);
u8 *skp, *p2sh;
skp = scriptpubkey_p2wpkh_derkey(tmpctx, derkey);
p2sh = scriptpubkey_p2sh(tmpctx, skp);
txfilter_add_scriptpubkey(filter, take(skp));
txfilter_add_scriptpubkey(filter, take(p2sh));
tal_free(tmpctx);
}
bool txfilter_match(const struct txfilter *filter, const struct bitcoin_tx *tx)
{
u8 *oscript;
for (size_t i = 0; i < tal_count(tx->output); i++) {
oscript = tx->output[i].script;
for (size_t j = 0; j < tal_count(filter->scriptpubkeys); j++) {
if (scripteq(oscript, filter->scriptpubkeys[j]))
return true;
}
}
return false;
}