1 #include "config.h"
2 #include <assert.h>
3 #include <bitcoin/pubkey.h>
4 #include <bitcoin/tx.h>
5 #include <tests/fuzz/libfuzz.h>
6 
7 #include <common/channel_id.h>
8 #include <wire/wire.h>
9 
init(int * argc,char *** argv)10 void init(int *argc, char ***argv)
11 {
12 }
13 
run(const uint8_t * data,size_t size)14 void run(const uint8_t *data, size_t size)
15 {
16 	struct channel_id chan_id;
17 	struct pubkey basepoint_1, basepoint_2;
18 	struct bitcoin_outpoint outpoint;
19 	const uint8_t **v1_chunks, **v2_chunks, **marshal_chunks;
20 	const uint8_t *wire_ptr;
21 	size_t wire_max;
22 	uint8_t *wire_buf;
23 
24 	/* 32 (txid) + 4 (vout) */
25 	if (size < 36)
26 		return;
27 
28 	v1_chunks = get_chunks(NULL, data, size, 36);
29 	for (size_t i = 0; i < tal_count(v1_chunks); i++) {
30 		wire_ptr = v1_chunks[i];
31 		wire_max = 36;
32 		fromwire_bitcoin_outpoint(&wire_ptr, &wire_max, &outpoint);
33 		assert(wire_ptr);
34 		derive_channel_id(&chan_id, &outpoint);
35 	}
36 	tal_free(v1_chunks);
37 
38 	v2_chunks = get_chunks(NULL, data, size, PUBKEY_CMPR_LEN * 2);
39 	for (size_t i = 0; i < tal_count(v2_chunks); i++) {
40 		wire_ptr = v2_chunks[i];
41 		wire_max = PUBKEY_CMPR_LEN;
42 		fromwire_pubkey(&wire_ptr, &wire_max, &basepoint_1);
43 		if (!wire_ptr)
44 			continue;
45 
46 		wire_max = PUBKEY_CMPR_LEN;
47 		fromwire_pubkey(&wire_ptr, &wire_max, &basepoint_2);
48 		if (!wire_ptr)
49 			continue;
50 
51 		derive_channel_id_v2(&chan_id, &basepoint_1, &basepoint_2);
52 	}
53 	tal_free(v2_chunks);
54 
55 	marshal_chunks = get_chunks(NULL, data, size, 32);
56 	for (size_t i = 0; i < tal_count(marshal_chunks); i++) {
57 		wire_ptr = marshal_chunks[i];
58 		wire_max = tal_count(marshal_chunks[i]);
59 		fromwire_channel_id(&wire_ptr, &wire_max, &chan_id);
60 		wire_buf = tal_arr(NULL, uint8_t, tal_count(marshal_chunks[i]));
61 		towire_channel_id(&wire_buf, &chan_id);
62 		tal_free(wire_buf);
63 	}
64 	tal_free(marshal_chunks);
65 }
66