nostr_core_lib/examples/keypair_generation.c

57 lines
1.6 KiB
C

/*
* Example: Random Keypair Generation
* Demonstrates nostr_generate_keypair()
*/
#include <stdio.h>
#include <stdlib.h>
#include "nostr_core.h"
int main() {
printf("=== NOSTR Random Keypair Generation Example ===\n\n");
// Initialize the library
if (nostr_init() != NOSTR_SUCCESS) {
fprintf(stderr, "Failed to initialize NOSTR library\n");
return 1;
}
// Generate a random keypair
unsigned char private_key[NOSTR_PRIVATE_KEY_SIZE];
unsigned char public_key[NOSTR_PUBLIC_KEY_SIZE];
int result = nostr_generate_keypair(private_key, public_key);
if (result != NOSTR_SUCCESS) {
fprintf(stderr, "Failed to generate keypair: %s\n", nostr_strerror(result));
nostr_cleanup();
return 1;
}
// Convert to hex format
char private_hex[NOSTR_HEX_KEY_SIZE];
char public_hex[NOSTR_HEX_KEY_SIZE];
nostr_bytes_to_hex(private_key, NOSTR_PRIVATE_KEY_SIZE, private_hex);
nostr_bytes_to_hex(public_key, NOSTR_PUBLIC_KEY_SIZE, public_hex);
// Convert to bech32 format
char nsec[NOSTR_BECH32_KEY_SIZE];
char npub[NOSTR_BECH32_KEY_SIZE];
nostr_key_to_bech32(private_key, "nsec", nsec);
nostr_key_to_bech32(public_key, "npub", npub);
// Display results
printf("✓ Successfully generated random NOSTR keypair\n\n");
printf("Private Key (hex): %s\n", private_hex);
printf("Public Key (hex): %s\n", public_hex);
printf("nsec (bech32): %s\n", nsec);
printf("npub (bech32): %s\n", npub);
// Cleanup
nostr_cleanup();
printf("\n✓ Example completed successfully!\n");
return 0;
}