93 lines
3.2 KiB
C
93 lines
3.2 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "../nostr_core/nip004.h"
|
|
#include "../nostr_core/nostr_common.h"
|
|
#include "../nostr_core/utils.h"
|
|
|
|
int main(void) {
|
|
printf("=== NIP-04 DEBUG COMPARISON (C) ===\n");
|
|
|
|
// Initialize NOSTR library - REQUIRED for secp256k1 operations
|
|
if (nostr_init() != NOSTR_SUCCESS) {
|
|
printf("❌ Failed to initialize NOSTR library\n");
|
|
return 1;
|
|
}
|
|
printf("✓ NOSTR library initialized successfully\n");
|
|
|
|
// Test vectors matching JavaScript
|
|
const char* sk1_hex = "91ba716fa9e7ea2fcbad360cf4f8e0d312f73984da63d90f524ad61a6a1e7dbe";
|
|
const char* pk1_hex = "b38ce15d3d9874ee710dfabb7ff9801b1e0e20aace6e9a1a05fa7482a04387d1";
|
|
const char* sk2_hex = "96f6fa197aa07477ab88f6981118466ae3a982faab8ad5db9d5426870c73d220";
|
|
const char* pk2_hex = "dcb33a629560280a0ee3b6b99b68c044fe8914ad8a984001ebf6099a9b474dc3";
|
|
const char* plaintext = "nanana";
|
|
const char* expectedCiphertext = "d6Joav5EciPI9hdHw31vmQ==?iv=fWs5rfv2+532arG/k83kcA==";
|
|
|
|
// Convert hex keys to bytes
|
|
unsigned char sk1[32], pk1[32], sk2[32], pk2[32];
|
|
nostr_hex_to_bytes(sk1_hex, sk1, 32);
|
|
nostr_hex_to_bytes(pk1_hex, pk1, 32);
|
|
nostr_hex_to_bytes(sk2_hex, sk2, 32);
|
|
nostr_hex_to_bytes(pk2_hex, pk2, 32);
|
|
|
|
// Print keys for comparison
|
|
printf("[C] Private Key sk1: %s\n", sk1_hex);
|
|
printf("[C] Public Key pk2: %s\n", pk2_hex);
|
|
|
|
// Allocate output buffer for encryption
|
|
char* encrypted = malloc(NOSTR_NIP04_MAX_ENCRYPTED_SIZE);
|
|
if (!encrypted) {
|
|
printf("Memory allocation failed\n");
|
|
return 1;
|
|
}
|
|
|
|
printf("\n--- ENCRYPTION TEST ---\n");
|
|
printf("[C] Encrypting \"%s\" using sk1 -> pk2\n", plaintext);
|
|
|
|
// Call the encryption function
|
|
int result = nostr_nip04_encrypt(sk1, pk2, plaintext, encrypted, NOSTR_NIP04_MAX_ENCRYPTED_SIZE);
|
|
|
|
if (result == NOSTR_SUCCESS) {
|
|
printf("[C] Encrypted Result: %s\n", encrypted);
|
|
} else {
|
|
printf("Encryption Error: %s\n", nostr_strerror(result));
|
|
free(encrypted);
|
|
return 1;
|
|
}
|
|
|
|
printf("\n--- DECRYPTION TEST ---\n");
|
|
printf("[C] Decrypting \"%s\" using sk2 + pk1\n", expectedCiphertext);
|
|
printf("[C] Private Key sk2: %s\n", sk2_hex);
|
|
printf("[C] Public Key pk1: %s\n", pk1_hex);
|
|
|
|
// Allocate output buffer for decryption
|
|
char* decrypted = malloc(1000);
|
|
if (!decrypted) {
|
|
printf("Memory allocation failed\n");
|
|
free(encrypted);
|
|
return 1;
|
|
}
|
|
|
|
// Call the decryption function
|
|
result = nostr_nip04_decrypt(sk2, pk1, expectedCiphertext, decrypted, 1000);
|
|
|
|
if (result == NOSTR_SUCCESS) {
|
|
printf("[C] UTF-8 Decoded: \"%s\"\n", decrypted);
|
|
|
|
printf("\n--- RESULTS ---\n");
|
|
printf("Encryption Success: Generated ciphertext\n");
|
|
printf("Decryption Success: %s\n", strcmp(decrypted, plaintext) == 0 ? "true" : "false");
|
|
printf("Expected: \"%s\"\n", plaintext);
|
|
printf("Got: \"%s\"\n", decrypted);
|
|
} else {
|
|
printf("Decryption Error: %s\n", nostr_strerror(result));
|
|
}
|
|
|
|
free(encrypted);
|
|
free(decrypted);
|
|
|
|
// Cleanup NOSTR library
|
|
nostr_cleanup();
|
|
return 0;
|
|
}
|