nostr_core_lib/tests/simple_nip44_test.c

119 lines
3.6 KiB
C

/*
* Simple NIP-44 Test
* Basic functionality test for NIP-44 encryption/decryption
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "../nostr_core/nostr_core.h"
int main() {
printf("🧪 Simple NIP-44 Test\n");
printf("=====================\n\n");
// Initialize the library
if (nostr_init() != NOSTR_SUCCESS) {
printf("❌ Failed to initialize NOSTR library\n");
return 1;
}
// Test keys (from successful NIP-04 test)
const char* sender_key_hex = "91ba716fa9e7ea2fcbad360cf4f8e0d312f73984da63d90f524ad61a6a1e7dbe";
const char* recipient_key_hex = "96f6fa197aa07477ab88f6981118466ae3a982faab8ad5db9d5426870c73d220";
unsigned char sender_private_key[32];
unsigned char recipient_private_key[32];
unsigned char recipient_public_key[32];
// Parse keys
if (nostr_hex_to_bytes(sender_key_hex, sender_private_key, 32) != 0) {
printf("❌ Failed to parse sender private key\n");
nostr_cleanup();
return 1;
}
if (nostr_hex_to_bytes(recipient_key_hex, recipient_private_key, 32) != 0) {
printf("❌ Failed to parse recipient private key\n");
nostr_cleanup();
return 1;
}
// Generate recipient's public key
if (nostr_ec_public_key_from_private_key(recipient_private_key, recipient_public_key) != 0) {
printf("❌ Failed to generate recipient public key\n");
nostr_cleanup();
return 1;
}
printf("✅ Keys parsed successfully\n");
// Test message
const char* test_message = "Hello, NIP-44! This is a test message.";
printf("📝 Test message: \"%s\"\n", test_message);
// Test encryption
char encrypted[8192];
printf("🔐 Testing NIP-44 encryption...\n");
int encrypt_result = nostr_nip44_encrypt(
sender_private_key,
recipient_public_key,
test_message,
encrypted,
sizeof(encrypted)
);
if (encrypt_result != NOSTR_SUCCESS) {
printf("❌ NIP-44 encryption failed with error: %d\n", encrypt_result);
nostr_cleanup();
return 1;
}
printf("✅ NIP-44 encryption successful!\n");
printf("📦 Encrypted length: %zu bytes\n", strlen(encrypted));
printf("📦 First 80 chars: %.80s...\n", encrypted);
// Test decryption
char decrypted[8192];
unsigned char sender_public_key[32];
// Generate sender's public key for decryption
if (nostr_ec_public_key_from_private_key(sender_private_key, sender_public_key) != 0) {
printf("❌ Failed to generate sender public key\n");
nostr_cleanup();
return 1;
}
printf("🔓 Testing NIP-44 decryption...\n");
int decrypt_result = nostr_nip44_decrypt(
recipient_private_key,
sender_public_key,
encrypted,
decrypted,
sizeof(decrypted)
);
if (decrypt_result != NOSTR_SUCCESS) {
printf("❌ NIP-44 decryption failed with error: %d\n", decrypt_result);
nostr_cleanup();
return 1;
}
printf("✅ NIP-44 decryption successful!\n");
printf("📝 Decrypted: \"%s\"\n", decrypted);
// Verify round-trip
if (strcmp(test_message, decrypted) == 0) {
printf("✅ Round-trip successful! Messages match perfectly.\n");
printf("\n🎉 NIP-44 TEST PASSED! 🎉\n");
nostr_cleanup();
return 0;
} else {
printf("❌ Round-trip failed! Messages don't match.\n");
printf("📝 Original: \"%s\"\n", test_message);
printf("📝 Decrypted: \"%s\"\n", decrypted);
nostr_cleanup();
return 1;
}
}