damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

node_id.c (1729B)


      1 #include "config.h"
      2 #include <assert.h>
      3 #include "array_size.h"
      4 #include "mem.h"
      5 #include "hex.h"
      6 #include "talstr.h"
      7 #include "node_id.h"
      8 
      9 /* Convert from pubkey to compressed pubkey. */
     10 /*
     11 void node_id_from_pubkey(struct node_id *id, const struct pubkey *key)
     12 {
     13     size_t outlen = ARRAY_SIZE(id->k);
     14     if (!secp256k1_ec_pubkey_serialize(secp256k1_ctx, id->k, &outlen,
     15                        &key->pubkey,
     16                        SECP256K1_EC_COMPRESSED))
     17         abort();
     18 }
     19 
     20 WARN_UNUSED_RESULT
     21 bool pubkey_from_node_id(struct pubkey *key, const struct node_id *id)
     22 {
     23     return secp256k1_ec_pubkey_parse(secp256k1_ctx, &key->pubkey,
     24                      memcheck(id->k, sizeof(id->k)),
     25                      sizeof(id->k));
     26 }
     27 
     28 WARN_UNUSED_RESULT
     29 bool point32_from_node_id(struct point32 *key, const struct node_id *id)
     30 {
     31     struct pubkey k;
     32     if (!pubkey_from_node_id(&k, id))
     33         return false;
     34     return secp256k1_xonly_pubkey_from_pubkey(secp256k1_ctx, &key->pubkey,
     35                           NULL, &k.pubkey) == 1;
     36 }
     37 */
     38 
     39 char *tal_hexstr(const tal_t *ctx, const void *data, size_t len)
     40 {
     41     char *str = tal_arr(ctx, char, hex_str_size(len));
     42     hex_encode(data, len, str, hex_str_size(len));
     43     return str;
     44 }
     45 
     46 
     47 /* Convert to hex string of SEC1 encoding */
     48 char *node_id_to_hexstr(const tal_t *ctx, const struct node_id *id)
     49 {
     50     return tal_hexstr(ctx, id->k, sizeof(id->k));
     51 }
     52 
     53 /* Convert from hex string of SEC1 encoding */
     54 
     55 bool node_id_from_hexstr(const char *str, size_t slen, struct node_id *id)
     56 {
     57     return hex_decode(str, slen, id->k, sizeof(id->k));
     58     /*    && node_id_valid(id);*/
     59 }
     60 
     61 int node_id_cmp(const struct node_id *a, const struct node_id *b)
     62 {
     63     return memcmp(a->k, b->k, sizeof(a->k));
     64 }