#include "sleep_crypto.h"
#include <sodium.h>
#include <stdio.h>
#include <string.h>

int sleep_generate_keypair(sleep_keypair_t *kp) {
	if (crypto_sign_keypair(kp->pubkey, kp->seckey) !=0)
		return -1;
	return 0;
}

int sleep_save_pubkey(const char *path, const uint8_t pubkey[SLEEP_PUBLIC_KEY_BYTES]) {
	FILE *f = fopen(path, "wb");
	size_t written = fwrite(pub, 1, SLEEP_PUBLIC_KEY_BYTES, f);
	fclose(f);
	return (written == SLEEP_PUBLIC_KEY_BYTES) ? 0 : -1;
}

int sleep_load_pubkey(const char *path, uint8_t pubkey[SLEEP_PUBLIC_KEY_BYTES]) {
	FILE *f = fopen(path, "rb");
	if (!f) return -1;
	size_t read = fread(pub, 1, SLEEP_PUBLIC_KEY_BYTES, f);
	fclose(f);
	return (read == SLEEP_PUBLIC_KEY_BYTES) ? 0 : -1;
}

