v0.3.12 - Working through auth still
This commit is contained in:
116
src/config.c
116
src/config.c
@@ -2448,7 +2448,117 @@ int handle_kind_23456_unified(cJSON* event, char* error_message, size_t error_si
|
||||
|
||||
log_info("Processing Kind 23456 event through unified handler");
|
||||
|
||||
// Parse first tag to determine action type
|
||||
// Check if content is encrypted (NIP-44)
|
||||
cJSON* content_obj = cJSON_GetObjectItem(event, "content");
|
||||
if (!content_obj || !cJSON_IsString(content_obj)) {
|
||||
snprintf(error_message, error_size, "invalid: missing or invalid content");
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char* content = cJSON_GetStringValue(content_obj);
|
||||
cJSON* decrypted_content = NULL;
|
||||
|
||||
// Check if content looks like NIP-44 encrypted content (base64 string, not JSON)
|
||||
if (content && strlen(content) > 10 && content[0] != '[' && content[0] != '{') {
|
||||
log_info("Detected NIP-44 encrypted content, attempting decryption");
|
||||
|
||||
// Get relay private key for decryption
|
||||
char* relay_privkey = get_relay_private_key();
|
||||
if (!relay_privkey) {
|
||||
snprintf(error_message, error_size, "error: relay private key not available for decryption");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Get sender's pubkey from the event for NIP-44 decryption
|
||||
cJSON* pubkey_obj = cJSON_GetObjectItem(event, "pubkey");
|
||||
if (!pubkey_obj || !cJSON_IsString(pubkey_obj)) {
|
||||
free(relay_privkey);
|
||||
snprintf(error_message, error_size, "invalid: missing sender pubkey in event");
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char* sender_pubkey = cJSON_GetStringValue(pubkey_obj);
|
||||
if (!sender_pubkey || strlen(sender_pubkey) != 64) {
|
||||
free(relay_privkey);
|
||||
snprintf(error_message, error_size, "invalid: invalid sender pubkey format");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Convert relay private key from hex to bytes
|
||||
unsigned char relay_privkey_bytes[32];
|
||||
if (nostr_hex_to_bytes(relay_privkey, relay_privkey_bytes, 32) != NOSTR_SUCCESS) {
|
||||
free(relay_privkey);
|
||||
snprintf(error_message, error_size, "error: failed to convert relay private key");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Convert sender public key from hex to bytes
|
||||
unsigned char sender_pubkey_bytes[32];
|
||||
if (nostr_hex_to_bytes(sender_pubkey, sender_pubkey_bytes, 32) != NOSTR_SUCCESS) {
|
||||
free(relay_privkey);
|
||||
snprintf(error_message, error_size, "error: failed to convert sender public key");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Perform NIP-44 decryption (relay as recipient, admin as sender)
|
||||
char decrypted_text[4096]; // Buffer for decrypted content
|
||||
int decrypt_result = nostr_nip44_decrypt(relay_privkey_bytes, sender_pubkey_bytes, content, decrypted_text, sizeof(decrypted_text));
|
||||
|
||||
// Clean up private key immediately after use
|
||||
memset(relay_privkey_bytes, 0, 32);
|
||||
free(relay_privkey);
|
||||
|
||||
if (decrypt_result != NOSTR_SUCCESS) {
|
||||
snprintf(error_message, error_size, "error: NIP-44 decryption failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
log_info("NIP-44 decryption successful");
|
||||
printf(" Decrypted content: %s\n", decrypted_text);
|
||||
|
||||
// Parse decrypted content as JSON array
|
||||
decrypted_content = cJSON_Parse(decrypted_text);
|
||||
|
||||
if (!decrypted_content || !cJSON_IsArray(decrypted_content)) {
|
||||
snprintf(error_message, error_size, "error: decrypted content is not valid JSON array");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Replace event content with decrypted command array for processing
|
||||
cJSON_DeleteItemFromObject(event, "content");
|
||||
cJSON_AddStringToObject(event, "content", "decrypted");
|
||||
|
||||
// Create synthetic tags from decrypted command array
|
||||
cJSON* tags_obj = cJSON_GetObjectItem(event, "tags");
|
||||
if (!tags_obj) {
|
||||
tags_obj = cJSON_CreateArray();
|
||||
cJSON_AddItemToObject(event, "tags", tags_obj);
|
||||
}
|
||||
|
||||
// Add decrypted command as first tag
|
||||
if (cJSON_GetArraySize(decrypted_content) > 0) {
|
||||
cJSON* first_item = cJSON_GetArrayItem(decrypted_content, 0);
|
||||
if (cJSON_IsString(first_item)) {
|
||||
cJSON* command_tag = cJSON_CreateArray();
|
||||
cJSON_AddItemToArray(command_tag, cJSON_Duplicate(first_item, 1));
|
||||
|
||||
// Add remaining items as tag values
|
||||
for (int i = 1; i < cJSON_GetArraySize(decrypted_content); i++) {
|
||||
cJSON* item = cJSON_GetArrayItem(decrypted_content, i);
|
||||
if (item) {
|
||||
cJSON_AddItemToArray(command_tag, cJSON_Duplicate(item, 1));
|
||||
}
|
||||
}
|
||||
|
||||
// Insert at beginning of tags array
|
||||
cJSON_InsertItemInArray(tags_obj, 0, command_tag);
|
||||
}
|
||||
}
|
||||
|
||||
cJSON_Delete(decrypted_content);
|
||||
}
|
||||
|
||||
// Parse first tag to determine action type (now from decrypted content if applicable)
|
||||
const char* action_type = get_first_tag_name(event);
|
||||
if (!action_type) {
|
||||
snprintf(error_message, error_size, "invalid: missing or invalid first tag");
|
||||
@@ -2459,7 +2569,7 @@ int handle_kind_23456_unified(cJSON* event, char* error_message, size_t error_si
|
||||
|
||||
// Route to appropriate handler based on action type
|
||||
if (strcmp(action_type, "auth_query") == 0) {
|
||||
const char* query_type = get_tag_value(event, "auth_query", 1);
|
||||
const char* query_type = get_tag_value(event, action_type, 1);
|
||||
if (!query_type) {
|
||||
snprintf(error_message, error_size, "invalid: missing auth_query type");
|
||||
return -1;
|
||||
@@ -2467,7 +2577,7 @@ int handle_kind_23456_unified(cJSON* event, char* error_message, size_t error_si
|
||||
return handle_auth_query_unified(event, query_type, error_message, error_size, wsi);
|
||||
}
|
||||
else if (strcmp(action_type, "system_command") == 0) {
|
||||
const char* command = get_tag_value(event, "system_command", 1);
|
||||
const char* command = get_tag_value(event, action_type, 1);
|
||||
if (!command) {
|
||||
snprintf(error_message, error_size, "invalid: missing system_command type");
|
||||
return -1;
|
||||
|
||||
Reference in New Issue
Block a user