nip 17, and 59
This commit is contained in:
@@ -85,6 +85,14 @@ typedef struct relay_connection {
|
||||
subscription_timing_t pending_subscriptions[NOSTR_POOL_MAX_PENDING_SUBSCRIPTIONS];
|
||||
int pending_subscription_count;
|
||||
|
||||
// Error reporting for publish operations
|
||||
char last_publish_error[512]; // Last error message from relay publish response
|
||||
time_t last_publish_error_time; // When the error occurred
|
||||
|
||||
// Error reporting for connection operations
|
||||
char last_connection_error[512]; // Last error message from relay connection
|
||||
time_t last_connection_error_time; // When the connection error occurred
|
||||
|
||||
// Statistics
|
||||
nostr_relay_stats_t stats;
|
||||
} relay_connection_t;
|
||||
@@ -236,6 +244,10 @@ static int ensure_relay_connection(relay_connection_t* relay) {
|
||||
relay->status = NOSTR_POOL_RELAY_ERROR;
|
||||
relay->reconnect_attempts++;
|
||||
relay->stats.connection_failures++;
|
||||
// Set connection error message
|
||||
snprintf(relay->last_connection_error, sizeof(relay->last_connection_error),
|
||||
"Failed to create WebSocket client for relay %s", relay->url);
|
||||
relay->last_connection_error_time = time(NULL);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -257,6 +269,11 @@ static int ensure_relay_connection(relay_connection_t* relay) {
|
||||
relay->reconnect_attempts++;
|
||||
relay->stats.connection_failures++;
|
||||
|
||||
// Set connection error message
|
||||
snprintf(relay->last_connection_error, sizeof(relay->last_connection_error),
|
||||
"WebSocket connection failed for relay %s (state: %d)", relay->url, state);
|
||||
relay->last_connection_error_time = time(NULL);
|
||||
|
||||
// Close the failed connection
|
||||
nostr_ws_close(relay->ws_client);
|
||||
relay->ws_client = NULL;
|
||||
@@ -448,6 +465,11 @@ nostr_relay_pool_t* nostr_relay_pool_create(nostr_pool_reconnect_config_t* confi
|
||||
return pool;
|
||||
}
|
||||
|
||||
// Compatibility wrapper for old API
|
||||
nostr_relay_pool_t* nostr_relay_pool_create_compat(void) {
|
||||
return nostr_relay_pool_create(NULL);
|
||||
}
|
||||
|
||||
int nostr_relay_pool_add_relay(nostr_relay_pool_t* pool, const char* relay_url) {
|
||||
if (!pool || !relay_url || pool->relay_count >= NOSTR_POOL_MAX_RELAYS) {
|
||||
return NOSTR_ERROR_INVALID_INPUT;
|
||||
@@ -755,6 +777,9 @@ static void process_relay_message(nostr_relay_pool_t* pool, relay_connection_t*
|
||||
|
||||
relay->stats.last_event_time = time(NULL);
|
||||
|
||||
// Debug: Print all message types received
|
||||
printf("📨 DEBUG: Received %s message from %s\n", msg_type, relay->url);
|
||||
|
||||
if (strcmp(msg_type, "EVENT") == 0) {
|
||||
// Handle EVENT message: ["EVENT", subscription_id, event]
|
||||
if (cJSON_IsArray(parsed) && cJSON_GetArraySize(parsed) >= 3) {
|
||||
@@ -887,6 +912,19 @@ static void process_relay_message(nostr_relay_pool_t* pool, relay_connection_t*
|
||||
relay->stats.events_published_ok++;
|
||||
} else {
|
||||
relay->stats.events_published_failed++;
|
||||
// Store error message if available
|
||||
if (cJSON_GetArraySize(parsed) >= 4) {
|
||||
cJSON* error_msg = cJSON_GetArrayItem(parsed, 3);
|
||||
if (cJSON_IsString(error_msg)) {
|
||||
const char* msg = cJSON_GetStringValue(error_msg);
|
||||
if (msg) {
|
||||
strncpy(relay->last_publish_error, msg,
|
||||
sizeof(relay->last_publish_error) - 1);
|
||||
relay->last_publish_error[sizeof(relay->last_publish_error) - 1] = '\0';
|
||||
relay->last_publish_error_time = time(NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1258,21 +1296,65 @@ double nostr_relay_pool_get_relay_ping_latency(
|
||||
}
|
||||
|
||||
double nostr_relay_pool_get_relay_query_latency(
|
||||
nostr_relay_pool_t* pool,
|
||||
nostr_relay_pool_t* pool,
|
||||
const char* relay_url) {
|
||||
|
||||
|
||||
if (!pool || !relay_url) {
|
||||
return -1.0;
|
||||
}
|
||||
|
||||
|
||||
relay_connection_t* relay = find_relay_by_url(pool, relay_url);
|
||||
if (!relay) {
|
||||
return -1.0;
|
||||
}
|
||||
|
||||
|
||||
return relay->stats.query_latency_avg;
|
||||
}
|
||||
|
||||
const char* nostr_relay_pool_get_relay_last_publish_error(
|
||||
nostr_relay_pool_t* pool,
|
||||
const char* relay_url) {
|
||||
|
||||
if (!pool || !relay_url) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
relay_connection_t* relay = find_relay_by_url(pool, relay_url);
|
||||
if (!relay) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Return error message only if it's recent (within last 60 seconds)
|
||||
if (relay->last_publish_error_time > 0 &&
|
||||
time(NULL) - relay->last_publish_error_time < 60) {
|
||||
return relay->last_publish_error[0] != '\0' ? relay->last_publish_error : NULL;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* nostr_relay_pool_get_relay_last_connection_error(
|
||||
nostr_relay_pool_t* pool,
|
||||
const char* relay_url) {
|
||||
|
||||
if (!pool || !relay_url) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
relay_connection_t* relay = find_relay_by_url(pool, relay_url);
|
||||
if (!relay) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Return error message only if it's recent (within last 60 seconds)
|
||||
if (relay->last_connection_error_time > 0 &&
|
||||
time(NULL) - relay->last_connection_error_time < 60) {
|
||||
return relay->last_connection_error[0] != '\0' ? relay->last_connection_error : NULL;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int nostr_relay_pool_ping_relay(
|
||||
nostr_relay_pool_t* pool,
|
||||
const char* relay_url) {
|
||||
|
||||
406
nostr_core/nip017.c
Normal file
406
nostr_core/nip017.c
Normal file
@@ -0,0 +1,406 @@
|
||||
/*
|
||||
* NIP-17: Private Direct Messages Implementation
|
||||
* https://github.com/nostr-protocol/nips/blob/master/17.md
|
||||
*/
|
||||
|
||||
#define _GNU_SOURCE
|
||||
#include "nip017.h"
|
||||
#include "nip059.h"
|
||||
#include "nip001.h"
|
||||
#include "utils.h"
|
||||
#include "nostr_common.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
// Forward declarations for crypto functions
|
||||
int nostr_ec_public_key_from_private_key(const unsigned char* private_key, unsigned char* public_key);
|
||||
|
||||
/**
|
||||
* Create tags array for DM events
|
||||
*/
|
||||
static cJSON* create_dm_tags(const char** recipient_pubkeys,
|
||||
int num_recipients,
|
||||
const char* subject,
|
||||
const char* reply_to_event_id,
|
||||
const char* reply_relay_url) {
|
||||
cJSON* tags = cJSON_CreateArray();
|
||||
if (!tags) return NULL;
|
||||
|
||||
// Add "p" tags for each recipient
|
||||
for (int i = 0; i < num_recipients; i++) {
|
||||
cJSON* p_tag = cJSON_CreateArray();
|
||||
if (!p_tag) {
|
||||
cJSON_Delete(tags);
|
||||
return NULL;
|
||||
}
|
||||
cJSON_AddItemToArray(p_tag, cJSON_CreateString("p"));
|
||||
cJSON_AddItemToArray(p_tag, cJSON_CreateString(recipient_pubkeys[i]));
|
||||
// Add relay URL if provided (recommended)
|
||||
if (reply_relay_url) {
|
||||
cJSON_AddItemToArray(p_tag, cJSON_CreateString(reply_relay_url));
|
||||
}
|
||||
cJSON_AddItemToArray(tags, p_tag);
|
||||
}
|
||||
|
||||
// Add subject tag if provided
|
||||
if (subject && strlen(subject) > 0) {
|
||||
cJSON* subject_tag = cJSON_CreateArray();
|
||||
if (!subject_tag) {
|
||||
cJSON_Delete(tags);
|
||||
return NULL;
|
||||
}
|
||||
cJSON_AddItemToArray(subject_tag, cJSON_CreateString("subject"));
|
||||
cJSON_AddItemToArray(subject_tag, cJSON_CreateString(subject));
|
||||
cJSON_AddItemToArray(tags, subject_tag);
|
||||
}
|
||||
|
||||
// Add reply reference if provided
|
||||
if (reply_to_event_id && strlen(reply_to_event_id) > 0) {
|
||||
cJSON* e_tag = cJSON_CreateArray();
|
||||
if (!e_tag) {
|
||||
cJSON_Delete(tags);
|
||||
return NULL;
|
||||
}
|
||||
cJSON_AddItemToArray(e_tag, cJSON_CreateString("e"));
|
||||
cJSON_AddItemToArray(e_tag, cJSON_CreateString(reply_to_event_id));
|
||||
if (reply_relay_url) {
|
||||
cJSON_AddItemToArray(e_tag, cJSON_CreateString(reply_relay_url));
|
||||
}
|
||||
// For replies, add "reply" marker as per NIP-17
|
||||
cJSON_AddItemToArray(e_tag, cJSON_CreateString("reply"));
|
||||
cJSON_AddItemToArray(tags, e_tag);
|
||||
}
|
||||
|
||||
return tags;
|
||||
}
|
||||
|
||||
/**
|
||||
* NIP-17: Create a chat message event (kind 14)
|
||||
*/
|
||||
cJSON* nostr_nip17_create_chat_event(const char* message,
|
||||
const char** recipient_pubkeys,
|
||||
int num_recipients,
|
||||
const char* subject,
|
||||
const char* reply_to_event_id,
|
||||
const char* reply_relay_url,
|
||||
const char* sender_pubkey_hex) {
|
||||
if (!message || !recipient_pubkeys || num_recipients <= 0 || !sender_pubkey_hex) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Create tags
|
||||
cJSON* tags = create_dm_tags(recipient_pubkeys, num_recipients, subject,
|
||||
reply_to_event_id, reply_relay_url);
|
||||
if (!tags) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Create the chat rumor (kind 14, unsigned)
|
||||
cJSON* chat_event = nostr_nip59_create_rumor(14, message, tags, sender_pubkey_hex, 0);
|
||||
|
||||
cJSON_Delete(tags); // Tags are duplicated in create_rumor
|
||||
|
||||
return chat_event;
|
||||
}
|
||||
|
||||
/**
|
||||
* NIP-17: Create a file message event (kind 15)
|
||||
*/
|
||||
cJSON* nostr_nip17_create_file_event(const char* file_url,
|
||||
const char* file_type,
|
||||
const char* encryption_algorithm,
|
||||
const char* decryption_key,
|
||||
const char* decryption_nonce,
|
||||
const char* file_hash,
|
||||
const char* original_file_hash,
|
||||
size_t file_size,
|
||||
const char* dimensions,
|
||||
const char* blurhash,
|
||||
const char* thumbnail_url,
|
||||
const char** recipient_pubkeys,
|
||||
int num_recipients,
|
||||
const char* subject,
|
||||
const char* reply_to_event_id,
|
||||
const char* reply_relay_url,
|
||||
const char* sender_pubkey_hex) {
|
||||
if (!file_url || !file_type || !encryption_algorithm || !decryption_key ||
|
||||
!decryption_nonce || !file_hash || !recipient_pubkeys ||
|
||||
num_recipients <= 0 || !sender_pubkey_hex) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Create base tags
|
||||
cJSON* tags = create_dm_tags(recipient_pubkeys, num_recipients, subject,
|
||||
reply_to_event_id, reply_relay_url);
|
||||
if (!tags) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Add file-specific tags
|
||||
cJSON* file_type_tag = cJSON_CreateArray();
|
||||
cJSON_AddItemToArray(file_type_tag, cJSON_CreateString("file-type"));
|
||||
cJSON_AddItemToArray(file_type_tag, cJSON_CreateString(file_type));
|
||||
cJSON_AddItemToArray(tags, file_type_tag);
|
||||
|
||||
cJSON* encryption_tag = cJSON_CreateArray();
|
||||
cJSON_AddItemToArray(encryption_tag, cJSON_CreateString("encryption-algorithm"));
|
||||
cJSON_AddItemToArray(encryption_tag, cJSON_CreateString(encryption_algorithm));
|
||||
cJSON_AddItemToArray(tags, encryption_tag);
|
||||
|
||||
cJSON* key_tag = cJSON_CreateArray();
|
||||
cJSON_AddItemToArray(key_tag, cJSON_CreateString("decryption-key"));
|
||||
cJSON_AddItemToArray(key_tag, cJSON_CreateString(decryption_key));
|
||||
cJSON_AddItemToArray(tags, key_tag);
|
||||
|
||||
cJSON* nonce_tag = cJSON_CreateArray();
|
||||
cJSON_AddItemToArray(nonce_tag, cJSON_CreateString("decryption-nonce"));
|
||||
cJSON_AddItemToArray(nonce_tag, cJSON_CreateString(decryption_nonce));
|
||||
cJSON_AddItemToArray(tags, nonce_tag);
|
||||
|
||||
cJSON* x_tag = cJSON_CreateArray();
|
||||
cJSON_AddItemToArray(x_tag, cJSON_CreateString("x"));
|
||||
cJSON_AddItemToArray(x_tag, cJSON_CreateString(file_hash));
|
||||
cJSON_AddItemToArray(tags, x_tag);
|
||||
|
||||
// Optional tags
|
||||
if (original_file_hash && strlen(original_file_hash) > 0) {
|
||||
cJSON* ox_tag = cJSON_CreateArray();
|
||||
cJSON_AddItemToArray(ox_tag, cJSON_CreateString("ox"));
|
||||
cJSON_AddItemToArray(ox_tag, cJSON_CreateString(original_file_hash));
|
||||
cJSON_AddItemToArray(tags, ox_tag);
|
||||
}
|
||||
|
||||
if (file_size > 0) {
|
||||
char size_str[32];
|
||||
snprintf(size_str, sizeof(size_str), "%zu", file_size);
|
||||
cJSON* size_tag = cJSON_CreateArray();
|
||||
cJSON_AddItemToArray(size_tag, cJSON_CreateString("size"));
|
||||
cJSON_AddItemToArray(size_tag, cJSON_CreateString(size_str));
|
||||
cJSON_AddItemToArray(tags, size_tag);
|
||||
}
|
||||
|
||||
if (dimensions && strlen(dimensions) > 0) {
|
||||
cJSON* dim_tag = cJSON_CreateArray();
|
||||
cJSON_AddItemToArray(dim_tag, cJSON_CreateString("dim"));
|
||||
cJSON_AddItemToArray(dim_tag, cJSON_CreateString(dimensions));
|
||||
cJSON_AddItemToArray(tags, dim_tag);
|
||||
}
|
||||
|
||||
if (blurhash && strlen(blurhash) > 0) {
|
||||
cJSON* blurhash_tag = cJSON_CreateArray();
|
||||
cJSON_AddItemToArray(blurhash_tag, cJSON_CreateString("blurhash"));
|
||||
cJSON_AddItemToArray(blurhash_tag, cJSON_CreateString(blurhash));
|
||||
cJSON_AddItemToArray(tags, blurhash_tag);
|
||||
}
|
||||
|
||||
if (thumbnail_url && strlen(thumbnail_url) > 0) {
|
||||
cJSON* thumb_tag = cJSON_CreateArray();
|
||||
cJSON_AddItemToArray(thumb_tag, cJSON_CreateString("thumb"));
|
||||
cJSON_AddItemToArray(thumb_tag, cJSON_CreateString(thumbnail_url));
|
||||
cJSON_AddItemToArray(tags, thumb_tag);
|
||||
}
|
||||
|
||||
// Create the file rumor (kind 15, unsigned)
|
||||
cJSON* file_event = nostr_nip59_create_rumor(15, file_url, tags, sender_pubkey_hex, 0);
|
||||
|
||||
cJSON_Delete(tags); // Tags are duplicated in create_rumor
|
||||
|
||||
return file_event;
|
||||
}
|
||||
|
||||
/**
|
||||
* NIP-17: Create a relay list event (kind 10050)
|
||||
*/
|
||||
cJSON* nostr_nip17_create_relay_list_event(const char** relay_urls,
|
||||
int num_relays,
|
||||
const unsigned char* private_key) {
|
||||
if (!relay_urls || num_relays <= 0 || !private_key) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Get public key
|
||||
unsigned char public_key[32];
|
||||
if (nostr_ec_public_key_from_private_key(private_key, public_key) != 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char pubkey_hex[65];
|
||||
nostr_bytes_to_hex(public_key, 32, pubkey_hex);
|
||||
|
||||
// Create tags with relay URLs
|
||||
cJSON* tags = cJSON_CreateArray();
|
||||
if (!tags) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (int i = 0; i < num_relays; i++) {
|
||||
cJSON* relay_tag = cJSON_CreateArray();
|
||||
if (!relay_tag) {
|
||||
cJSON_Delete(tags);
|
||||
return NULL;
|
||||
}
|
||||
cJSON_AddItemToArray(relay_tag, cJSON_CreateString("relay"));
|
||||
cJSON_AddItemToArray(relay_tag, cJSON_CreateString(relay_urls[i]));
|
||||
cJSON_AddItemToArray(tags, relay_tag);
|
||||
}
|
||||
|
||||
// Create and sign the event
|
||||
cJSON* relay_event = nostr_create_and_sign_event(10050, "", tags, private_key, time(NULL));
|
||||
|
||||
cJSON_Delete(tags); // Tags are duplicated in create_and_sign_event
|
||||
|
||||
return relay_event;
|
||||
}
|
||||
|
||||
/**
|
||||
* NIP-17: Send a direct message to recipients
|
||||
*/
|
||||
int nostr_nip17_send_dm(cJSON* dm_event,
|
||||
const char** recipient_pubkeys,
|
||||
int num_recipients,
|
||||
const unsigned char* sender_private_key,
|
||||
cJSON** gift_wraps_out,
|
||||
int max_gift_wraps) {
|
||||
if (!dm_event || !recipient_pubkeys || num_recipients <= 0 ||
|
||||
!sender_private_key || !gift_wraps_out || max_gift_wraps <= 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int created_wraps = 0;
|
||||
|
||||
for (int i = 0; i < num_recipients && created_wraps < max_gift_wraps; i++) {
|
||||
// Convert recipient pubkey hex to bytes
|
||||
unsigned char recipient_public_key[32];
|
||||
if (nostr_hex_to_bytes(recipient_pubkeys[i], recipient_public_key, 32) != 0) {
|
||||
continue; // Skip invalid pubkeys
|
||||
}
|
||||
|
||||
// Create seal for this recipient
|
||||
cJSON* seal = nostr_nip59_create_seal(dm_event, sender_private_key, recipient_public_key);
|
||||
if (!seal) {
|
||||
continue; // Skip if sealing fails
|
||||
}
|
||||
|
||||
// Create gift wrap for this recipient
|
||||
cJSON* gift_wrap = nostr_nip59_create_gift_wrap(seal, recipient_pubkeys[i]);
|
||||
cJSON_Delete(seal); // Seal is now wrapped
|
||||
|
||||
if (!gift_wrap) {
|
||||
continue; // Skip if wrapping fails
|
||||
}
|
||||
|
||||
gift_wraps_out[created_wraps++] = gift_wrap;
|
||||
}
|
||||
|
||||
// Also create a gift wrap for the sender (so they can see their own messages)
|
||||
if (created_wraps < max_gift_wraps) {
|
||||
// Get sender's public key
|
||||
unsigned char sender_public_key[32];
|
||||
if (nostr_ec_public_key_from_private_key(sender_private_key, sender_public_key) == 0) {
|
||||
char sender_pubkey_hex[65];
|
||||
nostr_bytes_to_hex(sender_public_key, 32, sender_pubkey_hex);
|
||||
|
||||
// Create seal for sender
|
||||
cJSON* sender_seal = nostr_nip59_create_seal(dm_event, sender_private_key, sender_public_key);
|
||||
if (sender_seal) {
|
||||
// Create gift wrap for sender
|
||||
cJSON* sender_gift_wrap = nostr_nip59_create_gift_wrap(sender_seal, sender_pubkey_hex);
|
||||
cJSON_Delete(sender_seal);
|
||||
|
||||
if (sender_gift_wrap) {
|
||||
gift_wraps_out[created_wraps++] = sender_gift_wrap;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return created_wraps;
|
||||
}
|
||||
|
||||
/**
|
||||
* NIP-17: Receive and decrypt a direct message
|
||||
*/
|
||||
cJSON* nostr_nip17_receive_dm(cJSON* gift_wrap,
|
||||
const unsigned char* recipient_private_key) {
|
||||
if (!gift_wrap || !recipient_private_key) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Unwrap the gift wrap to get the seal
|
||||
cJSON* seal = nostr_nip59_unwrap_gift(gift_wrap, recipient_private_key);
|
||||
if (!seal) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Get sender's public key from the seal
|
||||
cJSON* seal_pubkey_item = cJSON_GetObjectItem(seal, "pubkey");
|
||||
if (!seal_pubkey_item || !cJSON_IsString(seal_pubkey_item)) {
|
||||
cJSON_Delete(seal);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* sender_pubkey_hex = cJSON_GetStringValue(seal_pubkey_item);
|
||||
|
||||
// Convert sender pubkey hex to bytes
|
||||
unsigned char sender_public_key[32];
|
||||
if (nostr_hex_to_bytes(sender_pubkey_hex, sender_public_key, 32) != 0) {
|
||||
cJSON_Delete(seal);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Unseal the rumor
|
||||
cJSON* rumor = nostr_nip59_unseal_rumor(seal, sender_public_key, recipient_private_key);
|
||||
cJSON_Delete(seal); // Seal is no longer needed
|
||||
|
||||
return rumor;
|
||||
}
|
||||
|
||||
/**
|
||||
* NIP-17: Extract DM relay URLs from a user's kind 10050 event
|
||||
*/
|
||||
int nostr_nip17_extract_dm_relays(cJSON* relay_list_event,
|
||||
char** relay_urls_out,
|
||||
int max_relays) {
|
||||
if (!relay_list_event || !relay_urls_out || max_relays <= 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Check if this is a kind 10050 event
|
||||
cJSON* kind_item = cJSON_GetObjectItem(relay_list_event, "kind");
|
||||
if (!kind_item || !cJSON_IsNumber(kind_item) || cJSON_GetNumberValue(kind_item) != 10050) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Get tags array
|
||||
cJSON* tags_item = cJSON_GetObjectItem(relay_list_event, "tags");
|
||||
if (!tags_item || !cJSON_IsArray(tags_item)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int extracted = 0;
|
||||
cJSON* tag_item;
|
||||
cJSON_ArrayForEach(tag_item, tags_item) {
|
||||
if (!cJSON_IsArray(tag_item) || cJSON_GetArraySize(tag_item) < 2) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if this is a "relay" tag
|
||||
cJSON* tag_name = cJSON_GetArrayItem(tag_item, 0);
|
||||
cJSON* relay_url = cJSON_GetArrayItem(tag_item, 1);
|
||||
|
||||
if (cJSON_IsString(tag_name) && cJSON_IsString(relay_url) &&
|
||||
strcmp(cJSON_GetStringValue(tag_name), "relay") == 0) {
|
||||
|
||||
if (extracted < max_relays) {
|
||||
relay_urls_out[extracted] = strdup(cJSON_GetStringValue(relay_url));
|
||||
if (relay_urls_out[extracted]) {
|
||||
extracted++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return extracted;
|
||||
}
|
||||
137
nostr_core/nip017.h
Normal file
137
nostr_core/nip017.h
Normal file
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* NIP-17: Private Direct Messages
|
||||
* https://github.com/nostr-protocol/nips/blob/master/17.md
|
||||
*/
|
||||
|
||||
#ifndef NOSTR_NIP017_H
|
||||
#define NOSTR_NIP017_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include "../cjson/cJSON.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* NIP-17: Create a chat message event (kind 14)
|
||||
*
|
||||
* @param message Plain text message content
|
||||
* @param recipient_pubkeys Array of recipient public keys (hex strings)
|
||||
* @param num_recipients Number of recipients
|
||||
* @param subject Optional conversation subject/title (can be NULL)
|
||||
* @param reply_to_event_id Optional event ID this message replies to (can be NULL)
|
||||
* @param reply_relay_url Optional relay URL for reply reference (can be NULL)
|
||||
* @param sender_pubkey_hex Sender's public key in hex format
|
||||
* @return cJSON object representing the unsigned chat event, or NULL on error
|
||||
*/
|
||||
cJSON* nostr_nip17_create_chat_event(const char* message,
|
||||
const char** recipient_pubkeys,
|
||||
int num_recipients,
|
||||
const char* subject,
|
||||
const char* reply_to_event_id,
|
||||
const char* reply_relay_url,
|
||||
const char* sender_pubkey_hex);
|
||||
|
||||
/**
|
||||
* NIP-17: Create a file message event (kind 15)
|
||||
*
|
||||
* @param file_url URL of the encrypted file
|
||||
* @param file_type MIME type of the original file (e.g., "image/jpeg")
|
||||
* @param encryption_algorithm Encryption algorithm used ("aes-gcm")
|
||||
* @param decryption_key Base64-encoded decryption key
|
||||
* @param decryption_nonce Base64-encoded decryption nonce
|
||||
* @param file_hash SHA-256 hash of the encrypted file (hex)
|
||||
* @param original_file_hash SHA-256 hash of the original file before encryption (hex, optional)
|
||||
* @param file_size Size of encrypted file in bytes (optional, 0 to skip)
|
||||
* @param dimensions Image dimensions in "WxH" format (optional, NULL to skip)
|
||||
* @param blurhash Blurhash for preview (optional, NULL to skip)
|
||||
* @param thumbnail_url URL of encrypted thumbnail (optional, NULL to skip)
|
||||
* @param recipient_pubkeys Array of recipient public keys (hex strings)
|
||||
* @param num_recipients Number of recipients
|
||||
* @param subject Optional conversation subject/title (can be NULL)
|
||||
* @param reply_to_event_id Optional event ID this message replies to (can be NULL)
|
||||
* @param reply_relay_url Optional relay URL for reply reference (can be NULL)
|
||||
* @param sender_pubkey_hex Sender's public key in hex format
|
||||
* @return cJSON object representing the unsigned file event, or NULL on error
|
||||
*/
|
||||
cJSON* nostr_nip17_create_file_event(const char* file_url,
|
||||
const char* file_type,
|
||||
const char* encryption_algorithm,
|
||||
const char* decryption_key,
|
||||
const char* decryption_nonce,
|
||||
const char* file_hash,
|
||||
const char* original_file_hash,
|
||||
size_t file_size,
|
||||
const char* dimensions,
|
||||
const char* blurhash,
|
||||
const char* thumbnail_url,
|
||||
const char** recipient_pubkeys,
|
||||
int num_recipients,
|
||||
const char* subject,
|
||||
const char* reply_to_event_id,
|
||||
const char* reply_relay_url,
|
||||
const char* sender_pubkey_hex);
|
||||
|
||||
/**
|
||||
* NIP-17: Create a relay list event (kind 10050)
|
||||
*
|
||||
* @param relay_urls Array of relay URLs for DM delivery
|
||||
* @param num_relays Number of relay URLs
|
||||
* @param private_key Sender's private key for signing
|
||||
* @return cJSON object representing the signed relay list event, or NULL on error
|
||||
*/
|
||||
cJSON* nostr_nip17_create_relay_list_event(const char** relay_urls,
|
||||
int num_relays,
|
||||
const unsigned char* private_key);
|
||||
|
||||
/**
|
||||
* NIP-17: Send a direct message to recipients
|
||||
*
|
||||
* This function creates the appropriate rumor, seals it, gift wraps it,
|
||||
* and returns the final gift wrap events ready for publishing.
|
||||
*
|
||||
* @param dm_event The unsigned DM event (kind 14 or 15)
|
||||
* @param recipient_pubkeys Array of recipient public keys (hex strings)
|
||||
* @param num_recipients Number of recipients
|
||||
* @param sender_private_key 32-byte sender private key
|
||||
* @param gift_wraps_out Array to store resulting gift wrap events (caller must free)
|
||||
* @param max_gift_wraps Maximum number of gift wraps to create
|
||||
* @return Number of gift wrap events created, or -1 on error
|
||||
*/
|
||||
int nostr_nip17_send_dm(cJSON* dm_event,
|
||||
const char** recipient_pubkeys,
|
||||
int num_recipients,
|
||||
const unsigned char* sender_private_key,
|
||||
cJSON** gift_wraps_out,
|
||||
int max_gift_wraps);
|
||||
|
||||
/**
|
||||
* NIP-17: Receive and decrypt a direct message
|
||||
*
|
||||
* This function unwraps a gift wrap, unseals the rumor, and returns the original DM event.
|
||||
*
|
||||
* @param gift_wrap The received gift wrap event (kind 1059)
|
||||
* @param recipient_private_key 32-byte recipient private key
|
||||
* @return cJSON object representing the decrypted DM event, or NULL on error
|
||||
*/
|
||||
cJSON* nostr_nip17_receive_dm(cJSON* gift_wrap,
|
||||
const unsigned char* recipient_private_key);
|
||||
|
||||
/**
|
||||
* NIP-17: Extract DM relay URLs from a user's kind 10050 event
|
||||
*
|
||||
* @param relay_list_event The kind 10050 event
|
||||
* @param relay_urls_out Array to store extracted relay URLs (caller must free)
|
||||
* @param max_relays Maximum number of relays to extract
|
||||
* @return Number of relay URLs extracted, or -1 on error
|
||||
*/
|
||||
int nostr_nip17_extract_dm_relays(cJSON* relay_list_event,
|
||||
char** relay_urls_out,
|
||||
int max_relays);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // NOSTR_NIP017_H
|
||||
406
nostr_core/nip059.c
Normal file
406
nostr_core/nip059.c
Normal file
@@ -0,0 +1,406 @@
|
||||
/*
|
||||
* NIP-59: Gift Wrap Implementation
|
||||
* https://github.com/nostr-protocol/nips/blob/master/59.md
|
||||
*/
|
||||
|
||||
#include "nip059.h"
|
||||
#include "nip044.h"
|
||||
#include "nip001.h"
|
||||
#include "utils.h"
|
||||
#include "nostr_common.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
// Forward declarations for crypto functions
|
||||
int nostr_secp256k1_get_random_bytes(unsigned char* buf, size_t len);
|
||||
int nostr_ec_public_key_from_private_key(const unsigned char* private_key, unsigned char* public_key);
|
||||
int nostr_ec_sign(const unsigned char* private_key, const unsigned char* hash, unsigned char* signature);
|
||||
|
||||
// Memory clearing utility
|
||||
static void memory_clear(const void *p, size_t len) {
|
||||
if (p && len) {
|
||||
memset((void *)p, 0, len);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a random timestamp within 2 days in the past (as per NIP-59 spec)
|
||||
*/
|
||||
static time_t random_past_timestamp(void) {
|
||||
time_t now = time(NULL);
|
||||
// Random time up to 2 days (172800 seconds) in the past
|
||||
long random_offset = (long)(rand() % 172800);
|
||||
return now - random_offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a random private key for gift wrap
|
||||
*/
|
||||
static int generate_random_private_key(unsigned char* private_key) {
|
||||
return nostr_secp256k1_get_random_bytes(private_key, 32);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create event ID from event data (without signature)
|
||||
*/
|
||||
static int create_event_id(cJSON* event, char* event_id_hex) {
|
||||
if (!event || !event_id_hex) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Get event fields for serialization
|
||||
cJSON* pubkey_item = cJSON_GetObjectItem(event, "pubkey");
|
||||
cJSON* created_at_item = cJSON_GetObjectItem(event, "created_at");
|
||||
cJSON* kind_item = cJSON_GetObjectItem(event, "kind");
|
||||
cJSON* tags_item = cJSON_GetObjectItem(event, "tags");
|
||||
cJSON* content_item = cJSON_GetObjectItem(event, "content");
|
||||
|
||||
if (!pubkey_item || !created_at_item || !kind_item || !tags_item || !content_item) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Create serialization array: [0, pubkey, created_at, kind, tags, content]
|
||||
cJSON* serialize_array = cJSON_CreateArray();
|
||||
if (!serialize_array) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
cJSON_AddItemToArray(serialize_array, cJSON_CreateNumber(0));
|
||||
cJSON_AddItemToArray(serialize_array, cJSON_Duplicate(pubkey_item, 1));
|
||||
cJSON_AddItemToArray(serialize_array, cJSON_Duplicate(created_at_item, 1));
|
||||
cJSON_AddItemToArray(serialize_array, cJSON_Duplicate(kind_item, 1));
|
||||
cJSON_AddItemToArray(serialize_array, cJSON_Duplicate(tags_item, 1));
|
||||
cJSON_AddItemToArray(serialize_array, cJSON_Duplicate(content_item, 1));
|
||||
|
||||
char* serialize_string = cJSON_PrintUnformatted(serialize_array);
|
||||
cJSON_Delete(serialize_array);
|
||||
|
||||
if (!serialize_string) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Hash the serialized event
|
||||
unsigned char event_hash[32];
|
||||
if (nostr_sha256((const unsigned char*)serialize_string, strlen(serialize_string), event_hash) != 0) {
|
||||
free(serialize_string);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Convert hash to hex
|
||||
nostr_bytes_to_hex(event_hash, 32, event_id_hex);
|
||||
|
||||
free(serialize_string);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* NIP-59: Create a rumor (unsigned event)
|
||||
*/
|
||||
cJSON* nostr_nip59_create_rumor(int kind, const char* content, cJSON* tags,
|
||||
const char* pubkey_hex, time_t created_at) {
|
||||
if (!pubkey_hex || !content) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Use provided timestamp or random past timestamp
|
||||
time_t event_time = (created_at == 0) ? random_past_timestamp() : created_at;
|
||||
|
||||
// Create event structure (without id and sig - that's what makes it a rumor)
|
||||
cJSON* rumor = cJSON_CreateObject();
|
||||
if (!rumor) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cJSON_AddStringToObject(rumor, "pubkey", pubkey_hex);
|
||||
cJSON_AddNumberToObject(rumor, "created_at", (double)event_time);
|
||||
cJSON_AddNumberToObject(rumor, "kind", kind);
|
||||
|
||||
// Add tags (copy provided tags or create empty array)
|
||||
if (tags) {
|
||||
cJSON_AddItemToObject(rumor, "tags", cJSON_Duplicate(tags, 1));
|
||||
} else {
|
||||
cJSON_AddItemToObject(rumor, "tags", cJSON_CreateArray());
|
||||
}
|
||||
|
||||
cJSON_AddStringToObject(rumor, "content", content);
|
||||
|
||||
// Calculate and add event ID
|
||||
char event_id[65];
|
||||
if (create_event_id(rumor, event_id) != 0) {
|
||||
cJSON_Delete(rumor);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cJSON_AddStringToObject(rumor, "id", event_id);
|
||||
|
||||
return rumor;
|
||||
}
|
||||
|
||||
/**
|
||||
* NIP-59: Create a seal (kind 13) wrapping a rumor
|
||||
*/
|
||||
cJSON* nostr_nip59_create_seal(cJSON* rumor, const unsigned char* sender_private_key,
|
||||
const unsigned char* recipient_public_key) {
|
||||
if (!rumor || !sender_private_key || !recipient_public_key) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Serialize the rumor to JSON
|
||||
char* rumor_json = cJSON_PrintUnformatted(rumor);
|
||||
if (!rumor_json) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Encrypt the rumor using NIP-44
|
||||
char encrypted_content[4096]; // Should be large enough for most events
|
||||
int encrypt_result = nostr_nip44_encrypt(sender_private_key, recipient_public_key,
|
||||
rumor_json, encrypted_content, sizeof(encrypted_content));
|
||||
free(rumor_json);
|
||||
|
||||
if (encrypt_result != NOSTR_SUCCESS) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Get sender's public key
|
||||
unsigned char sender_public_key[32];
|
||||
if (nostr_ec_public_key_from_private_key(sender_private_key, sender_public_key) != 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char sender_pubkey_hex[65];
|
||||
nostr_bytes_to_hex(sender_public_key, 32, sender_pubkey_hex);
|
||||
|
||||
// Create seal event (kind 13)
|
||||
cJSON* seal = cJSON_CreateObject();
|
||||
if (!seal) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
time_t seal_time = random_past_timestamp();
|
||||
|
||||
cJSON_AddStringToObject(seal, "pubkey", sender_pubkey_hex);
|
||||
cJSON_AddNumberToObject(seal, "created_at", (double)seal_time);
|
||||
cJSON_AddNumberToObject(seal, "kind", 13);
|
||||
cJSON_AddItemToObject(seal, "tags", cJSON_CreateArray()); // Empty tags array
|
||||
cJSON_AddStringToObject(seal, "content", encrypted_content);
|
||||
|
||||
// Calculate event ID
|
||||
char event_id[65];
|
||||
if (create_event_id(seal, event_id) != 0) {
|
||||
cJSON_Delete(seal);
|
||||
return NULL;
|
||||
}
|
||||
cJSON_AddStringToObject(seal, "id", event_id);
|
||||
|
||||
// Sign the seal
|
||||
unsigned char event_hash[32];
|
||||
if (nostr_hex_to_bytes(event_id, event_hash, 32) != 0) {
|
||||
cJSON_Delete(seal);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
unsigned char signature[64];
|
||||
if (nostr_ec_sign(sender_private_key, event_hash, signature) != 0) {
|
||||
cJSON_Delete(seal);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char sig_hex[129];
|
||||
nostr_bytes_to_hex(signature, 64, sig_hex);
|
||||
cJSON_AddStringToObject(seal, "sig", sig_hex);
|
||||
|
||||
return seal;
|
||||
}
|
||||
|
||||
/**
|
||||
* NIP-59: Create a gift wrap (kind 1059) wrapping a seal
|
||||
*/
|
||||
cJSON* nostr_nip59_create_gift_wrap(cJSON* seal, const char* recipient_public_key_hex) {
|
||||
if (!seal || !recipient_public_key_hex) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Serialize the seal to JSON
|
||||
char* seal_json = cJSON_PrintUnformatted(seal);
|
||||
if (!seal_json) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Generate random private key for gift wrap
|
||||
unsigned char random_private_key[32];
|
||||
if (generate_random_private_key(random_private_key) != 1) {
|
||||
free(seal_json);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Get random public key
|
||||
unsigned char random_public_key[32];
|
||||
if (nostr_ec_public_key_from_private_key(random_private_key, random_public_key) != 0) {
|
||||
memory_clear(random_private_key, 32);
|
||||
free(seal_json);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char random_pubkey_hex[65];
|
||||
nostr_bytes_to_hex(random_public_key, 32, random_pubkey_hex);
|
||||
|
||||
// Convert recipient pubkey hex to bytes
|
||||
unsigned char recipient_public_key[32];
|
||||
if (nostr_hex_to_bytes(recipient_public_key_hex, recipient_public_key, 32) != 0) {
|
||||
memory_clear(random_private_key, 32);
|
||||
free(seal_json);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Encrypt the seal using NIP-44
|
||||
char encrypted_content[8192]; // Larger buffer for nested encryption
|
||||
int encrypt_result = nostr_nip44_encrypt(random_private_key, recipient_public_key,
|
||||
seal_json, encrypted_content, sizeof(encrypted_content));
|
||||
free(seal_json);
|
||||
|
||||
if (encrypt_result != NOSTR_SUCCESS) {
|
||||
memory_clear(random_private_key, 32);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Create gift wrap event (kind 1059)
|
||||
cJSON* gift_wrap = cJSON_CreateObject();
|
||||
if (!gift_wrap) {
|
||||
memory_clear(random_private_key, 32);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
time_t wrap_time = random_past_timestamp();
|
||||
|
||||
cJSON_AddStringToObject(gift_wrap, "pubkey", random_pubkey_hex);
|
||||
cJSON_AddNumberToObject(gift_wrap, "created_at", (double)wrap_time);
|
||||
cJSON_AddNumberToObject(gift_wrap, "kind", 1059);
|
||||
|
||||
// Add p tag for recipient
|
||||
cJSON* tags = cJSON_CreateArray();
|
||||
cJSON* p_tag = cJSON_CreateArray();
|
||||
cJSON_AddItemToArray(p_tag, cJSON_CreateString("p"));
|
||||
cJSON_AddItemToArray(p_tag, cJSON_CreateString(recipient_public_key_hex));
|
||||
cJSON_AddItemToArray(tags, p_tag);
|
||||
cJSON_AddItemToObject(gift_wrap, "tags", tags);
|
||||
|
||||
cJSON_AddStringToObject(gift_wrap, "content", encrypted_content);
|
||||
|
||||
// Calculate event ID
|
||||
char event_id[65];
|
||||
if (create_event_id(gift_wrap, event_id) != 0) {
|
||||
memory_clear(random_private_key, 32);
|
||||
cJSON_Delete(gift_wrap);
|
||||
return NULL;
|
||||
}
|
||||
cJSON_AddStringToObject(gift_wrap, "id", event_id);
|
||||
|
||||
// Sign the gift wrap
|
||||
unsigned char event_hash[32];
|
||||
if (nostr_hex_to_bytes(event_id, event_hash, 32) != 0) {
|
||||
memory_clear(random_private_key, 32);
|
||||
cJSON_Delete(gift_wrap);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
unsigned char signature[64];
|
||||
if (nostr_ec_sign(random_private_key, event_hash, signature) != 0) {
|
||||
memory_clear(random_private_key, 32);
|
||||
cJSON_Delete(gift_wrap);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char sig_hex[129];
|
||||
nostr_bytes_to_hex(signature, 64, sig_hex);
|
||||
cJSON_AddStringToObject(gift_wrap, "sig", sig_hex);
|
||||
|
||||
// Clear the random private key from memory
|
||||
memory_clear(random_private_key, 32);
|
||||
|
||||
return gift_wrap;
|
||||
}
|
||||
|
||||
/**
|
||||
* NIP-59: Unwrap a gift wrap to get the seal
|
||||
*/
|
||||
cJSON* nostr_nip59_unwrap_gift(cJSON* gift_wrap, const unsigned char* recipient_private_key) {
|
||||
if (!gift_wrap || !recipient_private_key) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Get the encrypted content
|
||||
cJSON* content_item = cJSON_GetObjectItem(gift_wrap, "content");
|
||||
if (!content_item || !cJSON_IsString(content_item)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* encrypted_content = cJSON_GetStringValue(content_item);
|
||||
|
||||
// Get the sender's public key (gift wrap pubkey)
|
||||
cJSON* pubkey_item = cJSON_GetObjectItem(gift_wrap, "pubkey");
|
||||
if (!pubkey_item || !cJSON_IsString(pubkey_item)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* sender_pubkey_hex = cJSON_GetStringValue(pubkey_item);
|
||||
|
||||
// Convert sender pubkey hex to bytes
|
||||
unsigned char sender_public_key[32];
|
||||
if (nostr_hex_to_bytes(sender_pubkey_hex, sender_public_key, 32) != 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Decrypt the content using NIP-44
|
||||
char decrypted_json[8192];
|
||||
int decrypt_result = nostr_nip44_decrypt(recipient_private_key, sender_public_key,
|
||||
encrypted_content, decrypted_json, sizeof(decrypted_json));
|
||||
|
||||
if (decrypt_result != NOSTR_SUCCESS) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Parse the decrypted JSON as the seal event
|
||||
cJSON* seal = cJSON_Parse(decrypted_json);
|
||||
if (!seal) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return seal;
|
||||
}
|
||||
|
||||
/**
|
||||
* NIP-59: Unseal a seal to get the rumor
|
||||
*/
|
||||
cJSON* nostr_nip59_unseal_rumor(cJSON* seal, const unsigned char* sender_public_key,
|
||||
const unsigned char* recipient_private_key) {
|
||||
if (!seal || !sender_public_key || !recipient_private_key) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Get the encrypted content
|
||||
cJSON* content_item = cJSON_GetObjectItem(seal, "content");
|
||||
if (!content_item || !cJSON_IsString(content_item)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* encrypted_content = cJSON_GetStringValue(content_item);
|
||||
|
||||
// Decrypt the content using NIP-44
|
||||
char decrypted_json[4096];
|
||||
int decrypt_result = nostr_nip44_decrypt(recipient_private_key, sender_public_key,
|
||||
encrypted_content, decrypted_json, sizeof(decrypted_json));
|
||||
|
||||
if (decrypt_result != NOSTR_SUCCESS) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Parse the decrypted JSON as the rumor event
|
||||
cJSON* rumor = cJSON_Parse(decrypted_json);
|
||||
if (!rumor) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return rumor;
|
||||
}
|
||||
74
nostr_core/nip059.h
Normal file
74
nostr_core/nip059.h
Normal file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* NIP-59: Gift Wrap
|
||||
* https://github.com/nostr-protocol/nips/blob/master/59.md
|
||||
*/
|
||||
|
||||
#ifndef NOSTR_NIP059_H
|
||||
#define NOSTR_NIP059_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <time.h>
|
||||
#include "../cjson/cJSON.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* NIP-59: Create a rumor (unsigned event)
|
||||
*
|
||||
* @param kind Event kind
|
||||
* @param content Event content
|
||||
* @param tags Event tags (cJSON array, can be NULL)
|
||||
* @param pubkey_hex Sender's public key in hex format
|
||||
* @param created_at Event timestamp (0 for current time)
|
||||
* @return cJSON object representing the rumor, or NULL on error
|
||||
*/
|
||||
cJSON* nostr_nip59_create_rumor(int kind, const char* content, cJSON* tags,
|
||||
const char* pubkey_hex, time_t created_at);
|
||||
|
||||
/**
|
||||
* NIP-59: Create a seal (kind 13) wrapping a rumor
|
||||
*
|
||||
* @param rumor The rumor event to seal (cJSON object)
|
||||
* @param sender_private_key 32-byte sender private key
|
||||
* @param recipient_public_key 32-byte recipient public key (x-only)
|
||||
* @return cJSON object representing the seal event, or NULL on error
|
||||
*/
|
||||
cJSON* nostr_nip59_create_seal(cJSON* rumor, const unsigned char* sender_private_key,
|
||||
const unsigned char* recipient_public_key);
|
||||
|
||||
/**
|
||||
* NIP-59: Create a gift wrap (kind 1059) wrapping a seal
|
||||
*
|
||||
* @param seal The seal event to wrap (cJSON object)
|
||||
* @param recipient_public_key_hex Recipient's public key in hex format
|
||||
* @return cJSON object representing the gift wrap event, or NULL on error
|
||||
*/
|
||||
cJSON* nostr_nip59_create_gift_wrap(cJSON* seal, const char* recipient_public_key_hex);
|
||||
|
||||
/**
|
||||
* NIP-59: Unwrap a gift wrap to get the seal
|
||||
*
|
||||
* @param gift_wrap The gift wrap event (cJSON object)
|
||||
* @param recipient_private_key 32-byte recipient private key
|
||||
* @return cJSON object representing the seal event, or NULL on error
|
||||
*/
|
||||
cJSON* nostr_nip59_unwrap_gift(cJSON* gift_wrap, const unsigned char* recipient_private_key);
|
||||
|
||||
/**
|
||||
* NIP-59: Unseal a seal to get the rumor
|
||||
*
|
||||
* @param seal The seal event (cJSON object)
|
||||
* @param sender_public_key 32-byte sender public key (x-only)
|
||||
* @param recipient_private_key 32-byte recipient private key
|
||||
* @return cJSON object representing the rumor event, or NULL on error
|
||||
*/
|
||||
cJSON* nostr_nip59_unseal_rumor(cJSON* seal, const unsigned char* sender_public_key,
|
||||
const unsigned char* recipient_private_key);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // NOSTR_NIP059_H
|
||||
@@ -2,10 +2,10 @@
|
||||
#define NOSTR_CORE_H
|
||||
|
||||
// Version information (auto-updated by increment_and_push.sh)
|
||||
#define VERSION "v0.4.3"
|
||||
#define VERSION "v0.4.4"
|
||||
#define VERSION_MAJOR 0
|
||||
#define VERSION_MINOR 4
|
||||
#define VERSION_PATCH 3
|
||||
#define VERSION_PATCH 4
|
||||
|
||||
/*
|
||||
* NOSTR Core Library - Complete API Reference
|
||||
@@ -49,6 +49,21 @@
|
||||
* - nostr_nip44_encrypt_with_nonce() -> Encrypt with specific nonce (testing)
|
||||
* - nostr_nip44_decrypt() -> Decrypt ChaCha20 + HMAC messages
|
||||
*
|
||||
* NIP-59 GIFT WRAP:
|
||||
* - nostr_nip59_create_rumor() -> Create unsigned event (rumor)
|
||||
* - nostr_nip59_create_seal() -> Seal rumor with sender's key (kind 13)
|
||||
* - nostr_nip59_create_gift_wrap() -> Wrap seal with random key (kind 1059)
|
||||
* - nostr_nip59_unwrap_gift() -> Unwrap gift wrap to get seal
|
||||
* - nostr_nip59_unseal_rumor() -> Unseal to get original rumor
|
||||
*
|
||||
* NIP-17 PRIVATE DIRECT MESSAGES:
|
||||
* - nostr_nip17_create_chat_event() -> Create chat message (kind 14)
|
||||
* - nostr_nip17_create_file_event() -> Create file message (kind 15)
|
||||
* - nostr_nip17_create_relay_list_event() -> Create DM relay list (kind 10050)
|
||||
* - nostr_nip17_send_dm() -> Send DM to multiple recipients
|
||||
* - nostr_nip17_receive_dm() -> Receive and decrypt DM
|
||||
* - nostr_nip17_extract_dm_relays() -> Extract relay URLs from kind 10050
|
||||
*
|
||||
* NIP-42 AUTHENTICATION:
|
||||
* - nostr_nip42_create_auth_event() -> Create authentication event (kind 22242)
|
||||
* - nostr_nip42_verify_auth_event() -> Verify authentication event (relay-side)
|
||||
@@ -132,6 +147,16 @@
|
||||
* cJSON* auth_event = nostr_nip42_create_auth_event(challenge, relay_url, private_key, 0);
|
||||
* nostr_ws_authenticate(client, private_key, 600); // Auto-authenticate WebSocket
|
||||
*
|
||||
* Private Direct Messages (NIP-17):
|
||||
* // Create and send a DM
|
||||
* cJSON* dm_event = nostr_nip17_create_chat_event("Hello!", &recipient_pubkey, 1, NULL, NULL, NULL, sender_pubkey);
|
||||
* cJSON* gift_wraps[10];
|
||||
* int count = nostr_nip17_send_dm(dm_event, &recipient_pubkey, 1, sender_privkey, gift_wraps, 10);
|
||||
* // Publish gift_wraps[0] to recipient's relays
|
||||
*
|
||||
* // Receive a DM
|
||||
* cJSON* decrypted_dm = nostr_nip17_receive_dm(received_gift_wrap, recipient_privkey);
|
||||
*
|
||||
* ============================================================================
|
||||
*/
|
||||
|
||||
@@ -150,9 +175,11 @@ extern "C" {
|
||||
#include "nip006.h" // Key derivation from mnemonic
|
||||
#include "nip011.h" // Relay information document
|
||||
#include "nip013.h" // Proof of Work
|
||||
#include "nip017.h" // Private Direct Messages
|
||||
#include "nip019.h" // Bech32 encoding (nsec/npub)
|
||||
#include "nip042.h" // Authentication of clients to relays
|
||||
#include "nip044.h" // Encryption (modern)
|
||||
#include "nip059.h" // Gift Wrap
|
||||
|
||||
// Authentication and request validation system
|
||||
#include "request_validator.h" // Request validation and authentication rules
|
||||
@@ -283,6 +310,12 @@ int nostr_relay_pool_reset_relay_stats(
|
||||
double nostr_relay_pool_get_relay_query_latency(
|
||||
nostr_relay_pool_t* pool,
|
||||
const char* relay_url);
|
||||
const char* nostr_relay_pool_get_relay_last_publish_error(
|
||||
nostr_relay_pool_t* pool,
|
||||
const char* relay_url);
|
||||
const char* nostr_relay_pool_get_relay_last_connection_error(
|
||||
nostr_relay_pool_t* pool,
|
||||
const char* relay_url);
|
||||
double nostr_relay_pool_get_relay_ping_latency(
|
||||
nostr_relay_pool_t* pool,
|
||||
const char* relay_url);
|
||||
|
||||
Reference in New Issue
Block a user