pub fn get_sql_setting(key: String) -> Result<Option<String>, String> {
let conn = super::get_db_connection_guard_static()?;
let result: Option<String> = conn.query_row(
"SELECT value FROM settings WHERE key = ?1",
rusqlite::params![key],
|row| row.get(0),
).ok();
Ok(result)
}
pub fn set_sql_setting(key: String, value: String) -> Result<(), String> {
let conn = super::get_write_connection_guard_static()?;
conn.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES (?1, ?2)",
rusqlite::params![key, value],
).map_err(|e| format!("Failed to set setting: {}", e))?;
Ok(())
}
pub fn remove_setting(key: &str) -> Result<(), String> {
let conn = super::get_write_connection_guard_static()?;
conn.execute("DELETE FROM settings WHERE key = ?1", rusqlite::params![key])
.map_err(|e| format!("Failed to remove setting: {}", e))?;
Ok(())
}
pub fn get_pkey() -> Result<Option<String>, String> {
let conn = super::get_db_connection_guard_static()?;
Ok(conn.query_row(
"SELECT value FROM settings WHERE key = 'pkey'",
[],
|row| row.get(0),
).ok())
}
pub fn set_pkey(pkey: &str) -> Result<(), String> {
let conn = super::get_write_connection_guard_static()?;
conn.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES ('pkey', ?1)",
rusqlite::params![pkey],
).map_err(|e| format!("Failed to set pkey: {}", e))?;
Ok(())
}
pub fn get_seed() -> Result<Option<String>, String> {
let conn = super::get_db_connection_guard_static()?;
Ok(conn.query_row(
"SELECT value FROM settings WHERE key = 'seed'",
[],
|row| row.get(0),
).ok())
}
pub fn set_seed(seed: &str) -> Result<(), String> {
let conn = super::get_write_connection_guard_static()?;
conn.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES ('seed', ?1)",
rusqlite::params![seed],
).map_err(|e| format!("Failed to set seed: {}", e))?;
Ok(())
}
pub fn commit_account_setup(
pkey: &str,
encryption_enabled: bool,
security_type: Option<&str>,
encrypted_seed: Option<&str>,
) -> Result<(), String> {
let mut conn = super::get_write_connection_guard_static()?;
let tx = conn.transaction()
.map_err(|e| format!("Failed to begin tx: {}", e))?;
tx.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES ('pkey', ?1)",
rusqlite::params![pkey],
).map_err(|e| format!("Failed to set pkey: {}", e))?;
tx.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES ('encryption_enabled', ?1)",
rusqlite::params![if encryption_enabled { "true" } else { "false" }],
).map_err(|e| format!("Failed to set encryption_enabled: {}", e))?;
if let Some(st) = security_type {
tx.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES ('security_type', ?1)",
rusqlite::params![st],
).map_err(|e| format!("Failed to set security_type: {}", e))?;
} else {
tx.execute(
"DELETE FROM settings WHERE key = 'security_type'",
[],
).map_err(|e| format!("Failed to clear security_type: {}", e))?;
}
if let Some(seed) = encrypted_seed {
tx.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES ('seed', ?1)",
rusqlite::params![seed],
).map_err(|e| format!("Failed to set seed: {}", e))?;
}
tx.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES ('signer_type', 'local')",
[],
).map_err(|e| format!("Failed to set signer_type: {}", e))?;
tx.commit().map_err(|e| format!("Failed to commit tx: {}", e))?;
Ok(())
}
pub fn get_signer_type() -> Result<String, String> {
let conn = super::get_db_connection_guard_static()?;
Ok(conn.query_row(
"SELECT value FROM settings WHERE key = 'signer_type'",
[],
|row| row.get::<_, String>(0),
).unwrap_or_else(|_| "local".to_string()))
}
pub fn set_signer_type(value: &str) -> Result<(), String> {
let conn = super::get_write_connection_guard_static()?;
conn.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES ('signer_type', ?1)",
rusqlite::params![value],
).map_err(|e| format!("Failed to set signer_type: {}", e))?;
Ok(())
}
pub async fn get_bunker_url() -> Result<Option<String>, String> {
let raw: Option<String> = {
let conn = super::get_db_connection_guard_static()?;
conn.query_row(
"SELECT value FROM settings WHERE key = 'bunker_url'",
[],
|row| row.get::<_, String>(0),
).ok()
};
match raw {
Some(s) => match crate::crypto::maybe_decrypt(s).await {
Ok(plain) => Ok(Some(plain)),
Err(_) => Err("bunker_url decryption failed (account locked?)".into()),
},
None => Ok(None),
}
}
pub async fn set_bunker_url(url: &str) -> Result<(), String> {
let stored = crate::crypto::maybe_encrypt(url.to_string()).await;
let conn = super::get_write_connection_guard_static()?;
conn.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES ('bunker_url', ?1)",
rusqlite::params![stored],
).map_err(|e| format!("Failed to set bunker_url: {}", e))?;
Ok(())
}
pub fn get_bunker_remote_pubkey() -> Result<Option<String>, String> {
let conn = super::get_db_connection_guard_static()?;
Ok(conn.query_row(
"SELECT value FROM settings WHERE key = 'bunker_remote_pubkey'",
[],
|row| row.get::<_, String>(0),
).ok())
}
pub fn set_bunker_remote_pubkey(pubkey_hex: &str) -> Result<(), String> {
let conn = super::get_write_connection_guard_static()?;
conn.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES ('bunker_remote_pubkey', ?1)",
rusqlite::params![pubkey_hex],
).map_err(|e| format!("Failed to set bunker_remote_pubkey: {}", e))?;
Ok(())
}
pub fn commit_bunker_account_setup(
pkey: &str,
encryption_enabled: bool,
security_type: Option<&str>,
bunker_url_stored: &str,
bunker_remote_pubkey_hex: &str,
) -> Result<(), String> {
let mut conn = super::get_write_connection_guard_static()?;
let tx = conn.transaction()
.map_err(|e| format!("Failed to begin tx: {}", e))?;
tx.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES ('pkey', ?1)",
rusqlite::params![pkey],
).map_err(|e| format!("Failed to set pkey: {}", e))?;
tx.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES ('encryption_enabled', ?1)",
rusqlite::params![if encryption_enabled { "true" } else { "false" }],
).map_err(|e| format!("Failed to set encryption_enabled: {}", e))?;
if let Some(st) = security_type {
tx.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES ('security_type', ?1)",
rusqlite::params![st],
).map_err(|e| format!("Failed to set security_type: {}", e))?;
} else {
tx.execute(
"DELETE FROM settings WHERE key = 'security_type'",
[],
).map_err(|e| format!("Failed to clear security_type: {}", e))?;
}
tx.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES ('signer_type', 'bunker')",
[],
).map_err(|e| format!("Failed to set signer_type: {}", e))?;
tx.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES ('bunker_url', ?1)",
rusqlite::params![bunker_url_stored],
).map_err(|e| format!("Failed to set bunker_url: {}", e))?;
tx.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES ('bunker_remote_pubkey', ?1)",
rusqlite::params![bunker_remote_pubkey_hex],
).map_err(|e| format!("Failed to set bunker_remote_pubkey: {}", e))?;
tx.execute("DELETE FROM settings WHERE key = 'seed'", [])
.map_err(|e| format!("Failed to clear stale seed: {}", e))?;
tx.commit().map_err(|e| format!("Failed to commit tx: {}", e))?;
Ok(())
}