1 /* Encapsulation for pubkeys used as node ids: more compact, more dangerous. */
2 #ifndef LIGHTNING_COMMON_NODE_ID_H
3 #define LIGHTNING_COMMON_NODE_ID_H
4 #include "config.h"
5 #include <bitcoin/pubkey.h>
6 
7 struct node_id {
8 	u8 k[PUBKEY_CMPR_LEN];
9 };
10 
node_id_eq(const struct node_id * a,const struct node_id * b)11 static inline bool node_id_eq(const struct node_id *a,
12 			      const struct node_id *b)
13 {
14 	return memcmp(a->k, b->k, sizeof(a->k)) == 0;
15 }
16 
17 /* Is this actually a valid pubkey?  Relatively expensive. */
18 bool node_id_valid(const struct node_id *id);
19 
20 /* Convert from pubkey to compressed pubkey. */
21 void node_id_from_pubkey(struct node_id *id, const struct pubkey *key);
22 
23 /* Returns false if not a valid pubkey: relatively expensive */
24 WARN_UNUSED_RESULT
25 bool pubkey_from_node_id(struct pubkey *key, const struct node_id *id);
26 
27 /* Returns false if not a valid pubkey: relatively expensive */
28 WARN_UNUSED_RESULT
29 bool point32_from_node_id(struct point32 *key, const struct node_id *id);
30 
31 /* Convert to hex string of SEC1 encoding. */
32 char *node_id_to_hexstr(const tal_t *ctx, const struct node_id *id);
33 
34 /* Convert from hex string of SEC1 encoding: checks validity! */
35 bool node_id_from_hexstr(const char *str, size_t slen, struct node_id *id);
36 
37 /* Compare the keys `a` and `b`. Return <0 if `a`<`b`, 0 if equal and >0 otherwise */
38 int node_id_cmp(const struct node_id *a, const struct node_id *b);
39 
40 /* If the two nodes[] are id1 and id2, which index would id1 be? */
node_id_idx(const struct node_id * id1,const struct node_id * id2)41 static inline int node_id_idx(const struct node_id *id1,
42 			      const struct node_id *id2)
43 {
44 	return node_id_cmp(id1, id2) > 0;
45 }
46 
47 /* marshal/unmarshal functions */
48 void towire_node_id(u8 **pptr, const struct node_id *id);
49 void fromwire_node_id(const u8 **cursor, size_t *max, struct node_id *id);
50 #endif /* LIGHTNING_COMMON_NODE_ID_H */
51