Files
nostr_core_lib/tests/nip46_test.c

248 lines
10 KiB
C

/*
* NIP-46 Remote Signing Test Suite
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../nostr_core/nostr_core.h"
static int tests_run = 0;
static int tests_passed = 0;
static void expect_int(const char* name, int expected, int actual) {
tests_run++;
if (expected == actual) {
tests_passed++;
printf("✅ %s (expected=%d actual=%d)\n", name, expected, actual);
} else {
printf("❌ %s (expected=%d actual=%d)\n", name, expected, actual);
}
}
static void expect_true(const char* name, int cond) {
tests_run++;
if (cond) {
tests_passed++;
printf("✅ %s\n", name);
} else {
printf("❌ %s\n", name);
}
}
static int hex_to_bytes32(const char* hex, unsigned char out[32]) {
return nostr_hex_to_bytes(hex, out, 32) == 0 ? 0 : -1;
}
static void test_url_parsing_and_generation(void) {
printf("\n=== test_url_parsing_and_generation ===\n");
const char* bunker = "bunker://fa984bd7dbb282f07e16e7ae87b26a2a7b9b90b7246a44771f0cf5ae58018f52?relay=wss%3A%2F%2Frelay1.example.com&relay=wss%3A%2F%2Frelay2.example.com&secret=s3cr3t";
nostr_nip46_bunker_url_t bu;
int rc = nostr_nip46_parse_bunker_url(bunker, &bu);
expect_int("parse bunker url", NOSTR_SUCCESS, rc);
expect_true("bunker pubkey parsed", strcmp(bu.remote_signer_pubkey, "fa984bd7dbb282f07e16e7ae87b26a2a7b9b90b7246a44771f0cf5ae58018f52") == 0);
expect_int("bunker relay count", 2, bu.relay_count);
expect_true("bunker relay[0] decoded", strcmp(bu.relays[0], "wss://relay1.example.com") == 0);
expect_true("bunker secret parsed", strcmp(bu.secret, "s3cr3t") == 0);
char bunker_roundtrip[2048];
rc = nostr_nip46_create_bunker_url(&bu, bunker_roundtrip, sizeof(bunker_roundtrip));
expect_int("create bunker url", NOSTR_SUCCESS, rc);
expect_true("bunker roundtrip has scheme", strstr(bunker_roundtrip, "bunker://") == bunker_roundtrip);
const char* nc = "nostrconnect://83f3b2ae6aa368e8275397b9c26cf550101d63ebaab900d19dd4a4429f5ad8f5?relay=wss%3A%2F%2Frelay1.example.com&secret=0s8j2djs&perms=nip44_encrypt%2Csign_event%3A1&name=My+Client&url=https%3A%2F%2Fclient.example.com";
nostr_nip46_nostrconnect_url_t nu;
rc = nostr_nip46_parse_nostrconnect_url(nc, &nu);
expect_int("parse nostrconnect url", NOSTR_SUCCESS, rc);
expect_int("nostrconnect relay count", 1, nu.relay_count);
expect_true("nostrconnect secret parsed", strcmp(nu.secret, "0s8j2djs") == 0);
expect_true("nostrconnect name decoded (+ to space)", strcmp(nu.name, "My Client") == 0);
char nc_roundtrip[2048];
rc = nostr_nip46_create_nostrconnect_url(&nu, nc_roundtrip, sizeof(nc_roundtrip));
expect_int("create nostrconnect url", NOSTR_SUCCESS, rc);
expect_true("nostrconnect roundtrip has scheme", strstr(nc_roundtrip, "nostrconnect://") == nc_roundtrip);
}
static void test_request_response_roundtrip(void) {
printf("\n=== test_request_response_roundtrip ===\n");
char id[65];
int rc = nostr_nip46_generate_request_id(id, sizeof(id));
expect_int("generate request id", NOSTR_SUCCESS, rc);
expect_true("request id hex length", strlen(id) == 32);
const char* params[] = {"abc", "def"};
nostr_nip46_request_t req;
rc = nostr_nip46_create_request(id, NOSTR_NIP46_METHOD_PING, params, 2, &req);
expect_int("create request", NOSTR_SUCCESS, rc);
char* req_json = NULL;
rc = nostr_nip46_request_to_json(&req, &req_json);
expect_int("request to json", NOSTR_SUCCESS, rc);
nostr_nip46_request_t parsed_req;
rc = nostr_nip46_parse_request(req_json, &parsed_req);
expect_int("parse request", NOSTR_SUCCESS, rc);
expect_true("parsed request id matches", strcmp(parsed_req.id, id) == 0);
expect_true("parsed request method string", strcmp(parsed_req.method_str, "ping") == 0);
expect_int("parsed request param count", 2, parsed_req.param_count);
free(req_json);
nostr_nip46_free_request(&req);
nostr_nip46_free_request(&parsed_req);
nostr_nip46_response_t resp;
rc = nostr_nip46_create_response(id, "pong", NULL, &resp);
expect_int("create response", NOSTR_SUCCESS, rc);
char* resp_json = NULL;
rc = nostr_nip46_response_to_json(&resp, &resp_json);
expect_int("response to json", NOSTR_SUCCESS, rc);
nostr_nip46_response_t parsed_resp;
rc = nostr_nip46_parse_response(resp_json, &parsed_resp);
expect_int("parse response", NOSTR_SUCCESS, rc);
expect_true("parsed response id matches", strcmp(parsed_resp.id, id) == 0);
expect_true("parsed response result matches", strcmp(parsed_resp.result, "pong") == 0);
free(resp_json);
nostr_nip46_free_response(&resp);
nostr_nip46_free_response(&parsed_resp);
}
static void test_event_encryption_flow(void) {
printf("\n=== test_event_encryption_flow ===\n");
const char* client_sk_hex = "91ba716fa9e7ea2fcbad360cf4f8e0d312f73984da63d90f524ad61a6a1e7dbe";
const char* signer_sk_hex = "96f6fa197aa07477ab88f6981118466ae3a982faab8ad5db9d5426870c73d220";
unsigned char client_sk[32], signer_sk[32];
unsigned char signer_pk[32];
int rc = hex_to_bytes32(client_sk_hex, client_sk);
expect_int("client sk parse", 0, rc);
rc = hex_to_bytes32(signer_sk_hex, signer_sk);
expect_int("signer sk parse", 0, rc);
rc = nostr_ec_public_key_from_private_key(signer_sk, signer_pk);
expect_int("derive signer public key", 0, rc);
const char* params[] = {"hello"};
nostr_nip46_request_t req;
rc = nostr_nip46_create_request("abc123", NOSTR_NIP46_METHOD_PING, params, 1, &req);
expect_int("create ping request", NOSTR_SUCCESS, rc);
cJSON* evt = nostr_nip46_create_request_event(&req, client_sk, signer_pk, 0);
expect_true("create encrypted request event", evt != NULL);
char decrypted[65536];
rc = nostr_nip46_decrypt_event(evt, signer_sk, decrypted, sizeof(decrypted));
expect_int("decrypt request event", NOSTR_SUCCESS, rc);
expect_true("decrypted payload has method ping", strstr(decrypted, "\"method\":\"ping\"") != NULL);
cJSON_Delete(evt);
nostr_nip46_free_request(&req);
}
static void test_signer_handle_request(void) {
printf("\n=== test_signer_handle_request ===\n");
const char* signer_sk_hex = "96f6fa197aa07477ab88f6981118466ae3a982faab8ad5db9d5426870c73d220";
const char* user_sk_hex = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
unsigned char signer_sk[32], user_sk[32];
int rc = hex_to_bytes32(signer_sk_hex, signer_sk);
expect_int("parse signer sk", 0, rc);
rc = hex_to_bytes32(user_sk_hex, user_sk);
expect_int("parse user sk", 0, rc);
const char* relays[] = {"wss://relay.example.com"};
nostr_nip46_signer_session_t ss;
rc = nostr_nip46_signer_session_init(&ss, signer_sk, user_sk, relays, 1);
expect_int("signer session init", NOSTR_SUCCESS, rc);
const char* connect_params[] = { ss.signer_pubkey_hex, "secret123" };
nostr_nip46_request_t connect_req;
rc = nostr_nip46_create_request("req-connect", NOSTR_NIP46_METHOD_CONNECT, connect_params, 2, &connect_req);
expect_int("build connect request", NOSTR_SUCCESS, rc);
nostr_nip46_response_t connect_resp;
rc = nostr_nip46_signer_handle_request(&ss, &connect_req, &connect_resp);
expect_int("handle connect request", NOSTR_SUCCESS, rc);
expect_true("connect returns provided secret", connect_resp.result && strcmp(connect_resp.result, "secret123") == 0);
nostr_nip46_free_request(&connect_req);
nostr_nip46_free_response(&connect_resp);
nostr_nip46_request_t ping_req;
rc = nostr_nip46_create_request("req-ping", NOSTR_NIP46_METHOD_PING, NULL, 0, &ping_req);
expect_int("build ping request", NOSTR_SUCCESS, rc);
nostr_nip46_response_t ping_resp;
rc = nostr_nip46_signer_handle_request(&ss, &ping_req, &ping_resp);
expect_int("handle ping request", NOSTR_SUCCESS, rc);
expect_true("ping response is pong", ping_resp.result && strcmp(ping_resp.result, "pong") == 0);
nostr_nip46_free_request(&ping_req);
nostr_nip46_free_response(&ping_resp);
nostr_nip46_request_t gpk_req;
rc = nostr_nip46_create_request("req-gpk", NOSTR_NIP46_METHOD_GET_PUBLIC_KEY, NULL, 0, &gpk_req);
expect_int("build get_public_key request", NOSTR_SUCCESS, rc);
nostr_nip46_response_t gpk_resp;
rc = nostr_nip46_signer_handle_request(&ss, &gpk_req, &gpk_resp);
expect_int("handle get_public_key request", NOSTR_SUCCESS, rc);
expect_true("get_public_key returns hex", gpk_resp.result && strlen(gpk_resp.result) == 64);
nostr_nip46_free_request(&gpk_req);
nostr_nip46_free_response(&gpk_resp);
cJSON* tags = cJSON_CreateArray();
cJSON* unsigned_event = cJSON_CreateObject();
cJSON_AddNumberToObject(unsigned_event, "kind", 1);
cJSON_AddStringToObject(unsigned_event, "content", "hello signer");
cJSON_AddItemToObject(unsigned_event, "tags", tags);
cJSON_AddNumberToObject(unsigned_event, "created_at", (double)time(NULL));
char* unsigned_event_json = cJSON_PrintUnformatted(unsigned_event);
cJSON_Delete(unsigned_event);
const char* sign_params[] = { unsigned_event_json };
nostr_nip46_request_t sign_req;
rc = nostr_nip46_create_request("req-sign", NOSTR_NIP46_METHOD_SIGN_EVENT, sign_params, 1, &sign_req);
expect_int("build sign_event request", NOSTR_SUCCESS, rc);
nostr_nip46_response_t sign_resp;
rc = nostr_nip46_signer_handle_request(&ss, &sign_req, &sign_resp);
expect_int("handle sign_event request", NOSTR_SUCCESS, rc);
expect_true("sign_event response contains id field", sign_resp.result && strstr(sign_resp.result, "\"id\"") != NULL);
expect_true("sign_event response contains sig field", sign_resp.result && strstr(sign_resp.result, "\"sig\"") != NULL);
nostr_nip46_free_request(&sign_req);
nostr_nip46_free_response(&sign_resp);
free(unsigned_event_json);
nostr_nip46_signer_session_destroy(&ss);
}
int main(void) {
printf("🧪 NIP-46 Test Suite\n");
printf("===================\n");
int init_rc = nostr_init();
if (init_rc != NOSTR_SUCCESS) {
printf("❌ Failed to initialize nostr library: %s\n", nostr_strerror(init_rc));
return 1;
}
test_url_parsing_and_generation();
test_request_response_roundtrip();
test_event_encryption_flow();
test_signer_handle_request();
nostr_cleanup();
printf("\n=== RESULT ===\n");
printf("Passed %d / %d tests\n", tests_passed, tests_run);
return (tests_passed == tests_run) ? 0 : 1;
}