nostr_core_lib/examples/mnemonic_derivation.c

58 lines
1.9 KiB
C

/*
* Example: Key Derivation from Existing Mnemonic
* Demonstrates nostr_derive_keys_from_mnemonic()
*/
#include <stdio.h>
#include <stdlib.h>
#include "nostr_core.h"
int main() {
printf("=== NOSTR Key Derivation from Mnemonic Example ===\n\n");
// Initialize the library
if (nostr_init() != NOSTR_SUCCESS) {
fprintf(stderr, "Failed to initialize NOSTR library\n");
return 1;
}
// Use a well-known test mnemonic
const char* test_mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
printf("Using test mnemonic: %s\n\n", test_mnemonic);
// Derive keys for multiple accounts
for (int account = 0; account < 3; account++) {
unsigned char private_key[NOSTR_PRIVATE_KEY_SIZE];
unsigned char public_key[NOSTR_PUBLIC_KEY_SIZE];
int result = nostr_derive_keys_from_mnemonic(test_mnemonic, account,
private_key, public_key);
if (result != NOSTR_SUCCESS) {
fprintf(stderr, "Failed to derive keys for account %d: %s\n",
account, nostr_strerror(result));
continue;
}
// 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 for this account
printf("Account %d (m/44'/1237'/%d'/0/0):\n", account, account);
printf(" nsec: %s\n", nsec);
printf(" npub: %s\n", npub);
printf("\n");
}
// Cleanup
nostr_cleanup();
printf("✓ Example completed successfully!\n");
printf("💡 The same mnemonic always produces the same keys (deterministic)\n");
return 0;
}