caracal 0.4.0

Nostr client for Gemini
use super::route_prelude::*;

pub async fn private_key_export(
    ctx: RouteContext,
    user: &'static mut CaracalUser,
) -> Response {
    let format = ctx.parameters.get("format").unwrap_or("bech32");

    let Some(name) = ctx.parameters.get("name") else {
        return resp_invalid_params();
    };

    let Ok(mut rtxn) = user.storage.get_read_txn() else {
        return Response::permanent_failure("Transaction error");
    };

    let Ok(keys) = user.storage.get_keys(&mut rtxn, name) else {
        return Response::permanent_failure("Keys not found");
    };

    match format {
        "hex" => Response::success(keys.secret_key().to_secret_hex()),
        "bech32" => match keys.secret_key().to_bech32() {
            Ok(bech) => Response::success(bech),
            Err(_) => {
                Response::temporary_failure("Failed to convert to bech32")
            }
        },
        _ => Response::temporary_failure("Invalid format"),
    }
}