v0.4.1 - Fixed startup bug
This commit is contained in:
84
src/config.c
84
src/config.c
@@ -917,10 +917,11 @@ cJSON* create_default_config_event(const unsigned char* admin_privkey_bytes,
|
||||
|
||||
int first_time_startup_sequence(const cli_options_t* cli_options) {
|
||||
log_info("Starting first-time startup sequence...");
|
||||
|
||||
|
||||
// 1. Generate or use provided admin keypair
|
||||
unsigned char admin_privkey_bytes[32];
|
||||
char admin_privkey[65], admin_pubkey[65];
|
||||
int generated_admin_key = 0; // Track if we generated a new admin key
|
||||
|
||||
if (cli_options && strlen(cli_options->admin_pubkey_override) == 64) {
|
||||
// Use provided admin public key directly - skip private key generation entirely
|
||||
@@ -943,6 +944,7 @@ int first_time_startup_sequence(const cli_options_t* cli_options) {
|
||||
// Set a dummy private key that will never be used (not displayed or stored)
|
||||
memset(admin_privkey_bytes, 0, 32); // Zero out for security
|
||||
memset(admin_privkey, 0, sizeof(admin_privkey)); // Zero out the hex string
|
||||
generated_admin_key = 0; // Did not generate a new key
|
||||
} else {
|
||||
// Generate random admin keypair using /dev/urandom + nostr_core_lib
|
||||
log_info("Generating random admin keypair");
|
||||
@@ -959,6 +961,7 @@ int first_time_startup_sequence(const cli_options_t* cli_options) {
|
||||
return -1;
|
||||
}
|
||||
nostr_bytes_to_hex(admin_pubkey_bytes, 32, admin_pubkey);
|
||||
generated_admin_key = 1; // Generated a new key
|
||||
}
|
||||
|
||||
// 2. Generate or use provided relay keypair
|
||||
@@ -1017,57 +1020,40 @@ int first_time_startup_sequence(const cli_options_t* cli_options) {
|
||||
g_temp_relay_privkey[sizeof(g_temp_relay_privkey) - 1] = '\0';
|
||||
log_info("Relay private key cached for secure storage after database initialization");
|
||||
|
||||
// 6. Handle configuration setup based on admin key availability
|
||||
if (cli_options && strlen(cli_options->admin_pubkey_override) == 64) {
|
||||
// Admin pubkey provided - will populate config table after database initialization
|
||||
log_info("Admin pubkey provided - config table will be populated after database initialization");
|
||||
// 6. Handle configuration setup - defaults will be populated after database initialization
|
||||
log_info("Configuration setup prepared - defaults will be populated after database initialization");
|
||||
|
||||
|
||||
// CLI overrides will be applied after database initialization in main.c
|
||||
// This prevents "g_db is NULL" errors during first-time startup
|
||||
|
||||
// 10. Print admin private key for user to save (only if we generated a new key)
|
||||
if (generated_admin_key) {
|
||||
printf("\n");
|
||||
printf("=================================================================\n");
|
||||
printf("IMPORTANT: SAVE THIS ADMIN PRIVATE KEY SECURELY!\n");
|
||||
printf("=================================================================\n");
|
||||
printf("Admin Private Key: %s\n", admin_privkey);
|
||||
printf("Admin Public Key: %s\n", admin_pubkey);
|
||||
printf("Relay Public Key: %s\n", relay_pubkey);
|
||||
printf("\nDatabase: %s\n", g_database_path);
|
||||
printf("\nThis admin private key is needed to update configuration!\n");
|
||||
printf("Store it safely - it will not be displayed again.\n");
|
||||
printf("=================================================================\n");
|
||||
printf("\n");
|
||||
} else {
|
||||
// Admin private key available - create signed configuration event
|
||||
log_info("Admin private key available - creating signed configuration event");
|
||||
|
||||
// Create initial configuration event using defaults
|
||||
cJSON* config_event = create_default_config_event(admin_privkey_bytes, relay_privkey, relay_pubkey, cli_options);
|
||||
if (!config_event) {
|
||||
log_error("Failed to create default configuration event");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Process configuration through admin API instead of storing in events table
|
||||
if (process_startup_config_event_with_fallback(config_event) == 0) {
|
||||
log_success("Initial configuration processed successfully through admin API");
|
||||
} else {
|
||||
log_warning("Failed to process initial configuration - will retry after database init");
|
||||
// Cache the event for later processing
|
||||
if (g_pending_config_event) {
|
||||
cJSON_Delete(g_pending_config_event);
|
||||
}
|
||||
g_pending_config_event = cJSON_Duplicate(config_event, 1);
|
||||
}
|
||||
|
||||
// Cache the current config
|
||||
if (g_current_config) {
|
||||
cJSON_Delete(g_current_config);
|
||||
}
|
||||
g_current_config = cJSON_Duplicate(config_event, 1);
|
||||
|
||||
// Clean up
|
||||
cJSON_Delete(config_event);
|
||||
printf("\n");
|
||||
printf("=================================================================\n");
|
||||
printf("RELAY STARTUP COMPLETE\n");
|
||||
printf("=================================================================\n");
|
||||
printf("Using provided admin public key for authentication\n");
|
||||
printf("Admin Public Key: %s\n", admin_pubkey);
|
||||
printf("Relay Public Key: %s\n", relay_pubkey);
|
||||
printf("\nDatabase: %s\n", g_database_path);
|
||||
printf("=================================================================\n");
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// 10. Print admin private key for user to save
|
||||
printf("\n");
|
||||
printf("=================================================================\n");
|
||||
printf("IMPORTANT: SAVE THIS ADMIN PRIVATE KEY SECURELY!\n");
|
||||
printf("=================================================================\n");
|
||||
printf("Admin Private Key: %s\n", admin_privkey);
|
||||
printf("Admin Public Key: %s\n", admin_pubkey);
|
||||
printf("Relay Public Key: %s\n", relay_pubkey);
|
||||
printf("\nDatabase: %s\n", g_database_path);
|
||||
printf("\nThis admin private key is needed to update configuration!\n");
|
||||
printf("Store it safely - it will not be displayed again.\n");
|
||||
printf("=================================================================\n");
|
||||
printf("\n");
|
||||
|
||||
log_success("First-time startup sequence completed");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user