v0.0.21 - Add C reference client library, examples, and integration test migration

This commit is contained in:
Laan Tungir
2026-05-05 11:44:43 -04:00
parent cc797a16df
commit cc5638b6e7
11 changed files with 706 additions and 47 deletions

View File

@@ -464,6 +464,8 @@ int socket_name_random(char *out, size_t out_len);
#include <time.h>
#include <unistd.h>
#include "nsigner_client.h"
#define SOCKET_NAME_A "nsigner_test_run_a"
#define SOCKET_NAME_B "nsigner_test_run_b"
#define MAX_MSG_SIZE 65536
@@ -630,6 +632,8 @@ int main(void) {
char *resp = NULL;
int status;
(void)signal(SIGPIPE, SIG_IGN);
if (pipe(stdin_pipe) != 0) {
perror("pipe");
return 1;
@@ -704,42 +708,57 @@ int main(void) {
sleep_ms(1000);
if (request_roundtrip_to(SOCKET_NAME_A, "{\"id\":\"1\",\"method\":\"get_public_key\",\"params\":[\"\"]}", &resp) == 0) {
check_condition("get_public_key response id", strstr(resp, "\"id\":\"1\"") != NULL);
check_condition("get_public_key has result", strstr(resp, "\"result\":") != NULL);
} else {
check_condition("get_public_key request roundtrip", 0);
}
free(resp);
resp = NULL;
{
nsigner_client_t client;
nsigner_client_init(&client);
if (nsigner_client_connect_unix(&client, SOCKET_NAME_A, 5000) == 0) {
if (nsigner_client_get_public_key(&client, "1", "", &resp) == 0) {
check_condition("get_public_key response id", strstr(resp, "\"id\":\"1\"") != NULL);
check_condition("get_public_key has result", strstr(resp, "\"result\":") != NULL);
} else {
check_condition("get_public_key request roundtrip", 0);
}
free(resp);
resp = NULL;
nsigner_client_close(&client);
} else {
check_condition("connect signer socket for client library", 0);
}
if (request_roundtrip_to(SOCKET_NAME_A, "{\"id\":\"2\",\"method\":\"sign_event\",\"params\":[\"{\\\"kind\\\":1,\\\"content\\\":\\\"hello\\\",\\\"tags\\\":[],\\\"created_at\\\":1700000000}\",{\"role\":\"main\"}]}", &resp) == 0) {
check_condition("sign_event response id", strstr(resp, "\"id\":\"2\"") != NULL);
check_condition("sign_event has result", strstr(resp, "\"result\":") != NULL);
} else {
check_condition("sign_event request roundtrip", 0);
nsigner_client_init(&client);
if (nsigner_client_connect_unix(&client, SOCKET_NAME_A, 5000) == 0) {
if (nsigner_client_sign_event(&client,
"2",
"{\"kind\":1,\"content\":\"hello\",\"tags\":[],\"created_at\":1700000000}",
"main",
&resp) == 0) {
check_condition("sign_event response id", strstr(resp, "\"id\":\"2\"") != NULL);
check_condition("sign_event has result", strstr(resp, "\"result\":") != NULL);
} else {
check_condition("sign_event request roundtrip", 0);
}
free(resp);
resp = NULL;
} else {
check_condition("connect signer socket for sign_event", 0);
}
nsigner_client_close(&client);
}
free(resp);
{
char *resp_a = NULL;
char *resp_b = NULL;
char pub_a[65] = {0};
char pub_b[65] = {0};
char cipher[2048] = {0};
char req[4096];
const char *p;
if (request_roundtrip_to(SOCKET_NAME_A, "{\"id\":\"3\",\"method\":\"get_public_key\",\"params\":[\"\"]}", &resp_a) == 0 &&
request_roundtrip_to(SOCKET_NAME_B, "{\"id\":\"4\",\"method\":\"get_public_key\",\"params\":[\"\"]}", &resp_b) == 0) {
if (request_roundtrip_to(SOCKET_NAME_A, "{\"id\":\"3\",\"method\":\"get_public_key\",\"params\":[\"\"]}", &resp_a) == 0) {
p = strstr(resp_a, "\"result\":\"");
if (p) { p += 10; strncpy(pub_a, p, 64); pub_a[64]='\0'; }
p = strstr(resp_b, "\"result\":\"");
if (p) { p += 10; strncpy(pub_b, p, 64); pub_b[64]='\0'; }
check_condition("multi-instance distinct sockets respond", pub_a[0] && pub_b[0]);
check_condition("single-instance public key request", pub_a[0] != '\0');
snprintf(req, sizeof(req), "{\"id\":\"5\",\"method\":\"nip04_encrypt\",\"params\":[\"%s\",\"hello_nip04\"]}", pub_b);
snprintf(req, sizeof(req), "{\"id\":\"5\",\"method\":\"nip04_encrypt\",\"params\":[\"%s\",\"hello_nip04\"]}", pub_a);
free(resp_a); resp_a = NULL;
if (request_roundtrip_to(SOCKET_NAME_A, req, &resp_a) == 0) {
p = strstr(resp_a, "\"result\":\"");
@@ -750,9 +769,9 @@ int main(void) {
cipher[i]='\0';
}
snprintf(req, sizeof(req), "{\"id\":\"6\",\"method\":\"nip04_decrypt\",\"params\":[\"%s\",\"%s\"]}", pub_a, cipher);
free(resp_b); resp_b = NULL;
if (request_roundtrip_to(SOCKET_NAME_B, req, &resp_b) == 0) {
check_condition("nip04 round-trip plaintext recovered", strstr(resp_b, "hello_nip04") != NULL);
free(resp_a); resp_a = NULL;
if (request_roundtrip_to(SOCKET_NAME_A, req, &resp_a) == 0) {
check_condition("nip04 round-trip plaintext recovered", strstr(resp_a, "hello_nip04") != NULL);
} else {
check_condition("nip04 decrypt request roundtrip", 0);
}
@@ -761,7 +780,7 @@ int main(void) {
}
memset(cipher, 0, sizeof(cipher));
snprintf(req, sizeof(req), "{\"id\":\"7\",\"method\":\"nip44_encrypt\",\"params\":[\"%s\",\"hello_nip44\"]}", pub_b);
snprintf(req, sizeof(req), "{\"id\":\"7\",\"method\":\"nip44_encrypt\",\"params\":[\"%s\",\"hello_nip44\"]}", pub_a);
free(resp_a); resp_a = NULL;
if (request_roundtrip_to(SOCKET_NAME_A, req, &resp_a) == 0) {
p = strstr(resp_a, "\"result\":\"");
@@ -772,9 +791,9 @@ int main(void) {
cipher[i]='\0';
}
snprintf(req, sizeof(req), "{\"id\":\"8\",\"method\":\"nip44_decrypt\",\"params\":[\"%s\",\"%s\"]}", pub_a, cipher);
free(resp_b); resp_b = NULL;
if (request_roundtrip_to(SOCKET_NAME_B, req, &resp_b) == 0) {
check_condition("nip44 round-trip plaintext recovered", strstr(resp_b, "hello_nip44") != NULL);
free(resp_a); resp_a = NULL;
if (request_roundtrip_to(SOCKET_NAME_A, req, &resp_a) == 0) {
check_condition("nip44 round-trip plaintext recovered", strstr(resp_a, "hello_nip44") != NULL);
} else {
check_condition("nip44 decrypt request roundtrip", 0);
}
@@ -782,10 +801,9 @@ int main(void) {
check_condition("nip44 encrypt request roundtrip", 0);
}
} else {
check_condition("multi-instance public key requests", 0);
check_condition("single-instance public key request", 0);
}
free(resp_a);
free(resp_b);
}
if (kill(child, SIGTERM) == 0) {