nostr_core_lib/nostr_core/nip021.h

81 lines
2.7 KiB
C

/*
* NOSTR Core Library - NIP-021: nostr: URI scheme
*/
#ifndef NIP021_H
#define NIP021_H
#include <stdint.h>
#include <time.h>
#include "nip001.h"
// URI type enumeration
typedef enum {
NOSTR_URI_NPUB, // Simple 32-byte pubkey
NOSTR_URI_NSEC, // Simple 32-byte privkey
NOSTR_URI_NOTE, // Simple 32-byte event ID
NOSTR_URI_NPROFILE, // Structured: pubkey + relays
NOSTR_URI_NEVENT, // Structured: event ID + relays + metadata
NOSTR_URI_NADDR, // Structured: address + relays + metadata
NOSTR_URI_INVALID
} nostr_uri_type_t;
// Structured data types for complex URIs
typedef struct {
unsigned char pubkey[32];
char** relays;
int relay_count;
} nostr_nprofile_t;
typedef struct {
unsigned char event_id[32];
char** relays;
int relay_count;
unsigned char* author; // Optional, 32 bytes if present
int* kind; // Optional
time_t* created_at; // Optional
} nostr_nevent_t;
typedef struct {
char* identifier;
unsigned char pubkey[32];
int kind;
char** relays;
int relay_count;
} nostr_naddr_t;
// Unified URI result structure
typedef struct {
nostr_uri_type_t type;
union {
unsigned char pubkey[32]; // For NPUB
unsigned char privkey[32]; // For NSEC
unsigned char event_id[32]; // For NOTE
nostr_nprofile_t nprofile; // For NPROFILE
nostr_nevent_t nevent; // For NEVENT
nostr_naddr_t naddr; // For NADDR
} data;
} nostr_uri_result_t;
// Function declarations
// Main parsing function - unified entry point
int nostr_parse_uri(const char* uri, nostr_uri_result_t* result);
// URI construction functions
int nostr_build_uri_npub(const unsigned char* pubkey, char* output, size_t output_size);
int nostr_build_uri_nsec(const unsigned char* privkey, char* output, size_t output_size);
int nostr_build_uri_note(const unsigned char* event_id, char* output, size_t output_size);
int nostr_build_uri_nprofile(const unsigned char* pubkey, const char** relays, int relay_count,
char* output, size_t output_size);
int nostr_build_uri_nevent(const unsigned char* event_id, const char** relays, int relay_count,
const unsigned char* author, int kind, time_t created_at,
char* output, size_t output_size);
int nostr_build_uri_naddr(const char* identifier, const unsigned char* pubkey, int kind,
const char** relays, int relay_count, char* output, size_t output_size);
// Utility functions
void nostr_uri_result_free(nostr_uri_result_t* result);
nostr_uri_type_t nostr_detect_uri_type(const char* uri);
#endif // NIP021_H