1pub fn get_sql_setting(key: String) -> Result<Option<String>, String> {
5 let conn = super::get_db_connection_guard_static()?;
6 let result: Option<String> = conn.query_row(
7 "SELECT value FROM settings WHERE key = ?1",
8 rusqlite::params![key],
9 |row| row.get(0),
10 ).ok();
11 Ok(result)
12}
13
14pub fn set_sql_setting(key: String, value: String) -> Result<(), String> {
16 let conn = super::get_write_connection_guard_static()?;
17 conn.execute(
18 "INSERT OR REPLACE INTO settings (key, value) VALUES (?1, ?2)",
19 rusqlite::params![key, value],
20 ).map_err(|e| format!("Failed to set setting: {}", e))?;
21 Ok(())
22}
23
24pub fn advance_u64_setting(key: String, value: u64) -> Result<(), String> {
29 let conn = super::get_write_connection_guard_static()?;
30 conn.execute(
31 "INSERT INTO settings (key, value) VALUES (?1, ?2)
32 ON CONFLICT(key) DO UPDATE SET value = excluded.value
33 WHERE CAST(excluded.value AS INTEGER) > CAST(value AS INTEGER)",
34 rusqlite::params![key, value.to_string()],
35 ).map_err(|e| format!("Failed to advance setting: {}", e))?;
36 Ok(())
37}
38
39pub fn remove_setting(key: &str) -> Result<(), String> {
41 let conn = super::get_write_connection_guard_static()?;
42 conn.execute("DELETE FROM settings WHERE key = ?1", rusqlite::params![key])
43 .map_err(|e| format!("Failed to remove setting: {}", e))?;
44 Ok(())
45}
46
47pub fn get_pkey() -> Result<Option<String>, String> {
49 let conn = super::get_db_connection_guard_static()?;
50 Ok(conn.query_row(
51 "SELECT value FROM settings WHERE key = 'pkey'",
52 [],
53 |row| row.get(0),
54 ).ok())
55}
56
57pub fn set_pkey(pkey: &str) -> Result<(), String> {
59 let conn = super::get_write_connection_guard_static()?;
60 conn.execute(
61 "INSERT OR REPLACE INTO settings (key, value) VALUES ('pkey', ?1)",
62 rusqlite::params![pkey],
63 ).map_err(|e| format!("Failed to set pkey: {}", e))?;
64 Ok(())
65}
66
67pub fn get_seed() -> Result<Option<String>, String> {
69 let conn = super::get_db_connection_guard_static()?;
70 Ok(conn.query_row(
71 "SELECT value FROM settings WHERE key = 'seed'",
72 [],
73 |row| row.get(0),
74 ).ok())
75}
76
77pub fn set_seed(seed: &str) -> Result<(), String> {
79 let conn = super::get_write_connection_guard_static()?;
80 conn.execute(
81 "INSERT OR REPLACE INTO settings (key, value) VALUES ('seed', ?1)",
82 rusqlite::params![seed],
83 ).map_err(|e| format!("Failed to set seed: {}", e))?;
84 Ok(())
85}
86
87pub fn commit_account_setup(
102 pkey: &str,
103 encryption_enabled: bool,
104 security_type: Option<&str>,
105 encrypted_seed: Option<&str>,
106) -> Result<(), String> {
107 let mut conn = super::get_write_connection_guard_static()?;
108 let tx = conn.transaction()
109 .map_err(|e| format!("Failed to begin tx: {}", e))?;
110 tx.execute(
111 "INSERT OR REPLACE INTO settings (key, value) VALUES ('pkey', ?1)",
112 rusqlite::params![pkey],
113 ).map_err(|e| format!("Failed to set pkey: {}", e))?;
114 tx.execute(
115 "INSERT OR REPLACE INTO settings (key, value) VALUES ('encryption_enabled', ?1)",
116 rusqlite::params![if encryption_enabled { "true" } else { "false" }],
117 ).map_err(|e| format!("Failed to set encryption_enabled: {}", e))?;
118 if let Some(st) = security_type {
119 tx.execute(
120 "INSERT OR REPLACE INTO settings (key, value) VALUES ('security_type', ?1)",
121 rusqlite::params![st],
122 ).map_err(|e| format!("Failed to set security_type: {}", e))?;
123 } else {
124 tx.execute(
127 "DELETE FROM settings WHERE key = 'security_type'",
128 [],
129 ).map_err(|e| format!("Failed to clear security_type: {}", e))?;
130 }
131 if let Some(seed) = encrypted_seed {
132 tx.execute(
133 "INSERT OR REPLACE INTO settings (key, value) VALUES ('seed', ?1)",
134 rusqlite::params![seed],
135 ).map_err(|e| format!("Failed to set seed: {}", e))?;
136 }
137 tx.execute(
142 "INSERT OR REPLACE INTO settings (key, value) VALUES ('signer_type', 'local')",
143 [],
144 ).map_err(|e| format!("Failed to set signer_type: {}", e))?;
145 tx.commit().map_err(|e| format!("Failed to commit tx: {}", e))?;
146 Ok(())
147}
148
149pub fn get_signer_type() -> Result<String, String> {
167 let conn = super::get_db_connection_guard_static()?;
168 Ok(conn.query_row(
169 "SELECT value FROM settings WHERE key = 'signer_type'",
170 [],
171 |row| row.get::<_, String>(0),
172 ).unwrap_or_else(|_| "local".to_string()))
173}
174
175pub fn set_signer_type(value: &str) -> Result<(), String> {
179 let conn = super::get_write_connection_guard_static()?;
180 conn.execute(
181 "INSERT OR REPLACE INTO settings (key, value) VALUES ('signer_type', ?1)",
182 rusqlite::params![value],
183 ).map_err(|e| format!("Failed to set signer_type: {}", e))?;
184 Ok(())
185}
186
187pub async fn get_bunker_url() -> Result<Option<String>, String> {
191 let raw: Option<String> = {
192 let conn = super::get_db_connection_guard_static()?;
193 conn.query_row(
194 "SELECT value FROM settings WHERE key = 'bunker_url'",
195 [],
196 |row| row.get::<_, String>(0),
197 ).ok()
198 };
199 match raw {
200 Some(s) => match crate::crypto::maybe_decrypt(s).await {
201 Ok(plain) => Ok(Some(plain)),
202 Err(_) => Err("bunker_url decryption failed (account locked?)".into()),
203 },
204 None => Ok(None),
205 }
206}
207
208pub async fn set_bunker_url(url: &str) -> Result<(), String> {
211 let stored = crate::crypto::maybe_encrypt(url.to_string()).await;
212 let conn = super::get_write_connection_guard_static()?;
213 conn.execute(
214 "INSERT OR REPLACE INTO settings (key, value) VALUES ('bunker_url', ?1)",
215 rusqlite::params![stored],
216 ).map_err(|e| format!("Failed to set bunker_url: {}", e))?;
217 Ok(())
218}
219
220pub fn get_bunker_remote_pubkey() -> Result<Option<String>, String> {
225 let conn = super::get_db_connection_guard_static()?;
226 Ok(conn.query_row(
227 "SELECT value FROM settings WHERE key = 'bunker_remote_pubkey'",
228 [],
229 |row| row.get::<_, String>(0),
230 ).ok())
231}
232
233pub fn set_bunker_remote_pubkey(pubkey_hex: &str) -> Result<(), String> {
237 let conn = super::get_write_connection_guard_static()?;
238 conn.execute(
239 "INSERT OR REPLACE INTO settings (key, value) VALUES ('bunker_remote_pubkey', ?1)",
240 rusqlite::params![pubkey_hex],
241 ).map_err(|e| format!("Failed to set bunker_remote_pubkey: {}", e))?;
242 Ok(())
243}
244
245pub fn commit_bunker_account_setup(
256 pkey: &str,
257 encryption_enabled: bool,
258 security_type: Option<&str>,
259 bunker_url_stored: &str,
260 bunker_remote_pubkey_hex: &str,
261) -> Result<(), String> {
262 let mut conn = super::get_write_connection_guard_static()?;
263 let tx = conn.transaction()
264 .map_err(|e| format!("Failed to begin tx: {}", e))?;
265 tx.execute(
266 "INSERT OR REPLACE INTO settings (key, value) VALUES ('pkey', ?1)",
267 rusqlite::params![pkey],
268 ).map_err(|e| format!("Failed to set pkey: {}", e))?;
269 tx.execute(
270 "INSERT OR REPLACE INTO settings (key, value) VALUES ('encryption_enabled', ?1)",
271 rusqlite::params![if encryption_enabled { "true" } else { "false" }],
272 ).map_err(|e| format!("Failed to set encryption_enabled: {}", e))?;
273 if let Some(st) = security_type {
274 tx.execute(
275 "INSERT OR REPLACE INTO settings (key, value) VALUES ('security_type', ?1)",
276 rusqlite::params![st],
277 ).map_err(|e| format!("Failed to set security_type: {}", e))?;
278 } else {
279 tx.execute(
280 "DELETE FROM settings WHERE key = 'security_type'",
281 [],
282 ).map_err(|e| format!("Failed to clear security_type: {}", e))?;
283 }
284 tx.execute(
285 "INSERT OR REPLACE INTO settings (key, value) VALUES ('signer_type', 'bunker')",
286 [],
287 ).map_err(|e| format!("Failed to set signer_type: {}", e))?;
288 tx.execute(
289 "INSERT OR REPLACE INTO settings (key, value) VALUES ('bunker_url', ?1)",
290 rusqlite::params![bunker_url_stored],
291 ).map_err(|e| format!("Failed to set bunker_url: {}", e))?;
292 tx.execute(
293 "INSERT OR REPLACE INTO settings (key, value) VALUES ('bunker_remote_pubkey', ?1)",
294 rusqlite::params![bunker_remote_pubkey_hex],
295 ).map_err(|e| format!("Failed to set bunker_remote_pubkey: {}", e))?;
296 tx.execute("DELETE FROM settings WHERE key = 'seed'", [])
298 .map_err(|e| format!("Failed to clear stale seed: {}", e))?;
299 tx.commit().map_err(|e| format!("Failed to commit tx: {}", e))?;
300 Ok(())
301}
302
303pub fn get_nip55_user_pubkey() -> Result<Option<String>, String> {
323 let conn = super::get_db_connection_guard_static()?;
324 Ok(conn.query_row(
325 "SELECT value FROM settings WHERE key = 'nip55_user_pubkey'",
326 [],
327 |row| row.get::<_, String>(0),
328 ).ok())
329}
330
331pub fn set_nip55_user_pubkey(pubkey_hex: &str) -> Result<(), String> {
333 let conn = super::get_write_connection_guard_static()?;
334 conn.execute(
335 "INSERT OR REPLACE INTO settings (key, value) VALUES ('nip55_user_pubkey', ?1)",
336 rusqlite::params![pubkey_hex],
337 ).map_err(|e| format!("Failed to set nip55_user_pubkey: {}", e))?;
338 Ok(())
339}
340
341pub fn get_nip55_signer_package() -> Result<Option<String>, String> {
344 let conn = super::get_db_connection_guard_static()?;
345 Ok(conn.query_row(
346 "SELECT value FROM settings WHERE key = 'nip55_signer_package'",
347 [],
348 |row| row.get::<_, String>(0),
349 ).ok())
350}
351
352pub fn set_nip55_signer_package(package: &str) -> Result<(), String> {
354 let conn = super::get_write_connection_guard_static()?;
355 conn.execute(
356 "INSERT OR REPLACE INTO settings (key, value) VALUES ('nip55_signer_package', ?1)",
357 rusqlite::params![package],
358 ).map_err(|e| format!("Failed to set nip55_signer_package: {}", e))?;
359 Ok(())
360}
361
362pub fn commit_nip55_account_setup(
369 user_pubkey_hex: &str,
370 signer_package: &str,
371 encryption_enabled: bool,
372 security_type: Option<&str>,
373) -> Result<(), String> {
374 let mut conn = super::get_write_connection_guard_static()?;
375 let tx = conn.transaction()
376 .map_err(|e| format!("Failed to begin tx: {}", e))?;
377 tx.execute(
378 "INSERT OR REPLACE INTO settings (key, value) VALUES ('encryption_enabled', ?1)",
379 rusqlite::params![if encryption_enabled { "true" } else { "false" }],
380 ).map_err(|e| format!("Failed to set encryption_enabled: {}", e))?;
381 if let Some(st) = security_type {
382 tx.execute(
383 "INSERT OR REPLACE INTO settings (key, value) VALUES ('security_type', ?1)",
384 rusqlite::params![st],
385 ).map_err(|e| format!("Failed to set security_type: {}", e))?;
386 } else {
387 tx.execute(
388 "DELETE FROM settings WHERE key = 'security_type'",
389 [],
390 ).map_err(|e| format!("Failed to clear security_type: {}", e))?;
391 }
392 tx.execute(
393 "INSERT OR REPLACE INTO settings (key, value) VALUES ('signer_type', 'nip55')",
394 [],
395 ).map_err(|e| format!("Failed to set signer_type: {}", e))?;
396 tx.execute(
397 "INSERT OR REPLACE INTO settings (key, value) VALUES ('nip55_user_pubkey', ?1)",
398 rusqlite::params![user_pubkey_hex],
399 ).map_err(|e| format!("Failed to set nip55_user_pubkey: {}", e))?;
400 tx.execute(
401 "INSERT OR REPLACE INTO settings (key, value) VALUES ('nip55_signer_package', ?1)",
402 rusqlite::params![signer_package],
403 ).map_err(|e| format!("Failed to set nip55_signer_package: {}", e))?;
404 for stale in ["pkey", "seed", "bunker_url", "bunker_remote_pubkey"] {
407 tx.execute(
408 "DELETE FROM settings WHERE key = ?1",
409 rusqlite::params![stale],
410 ).map_err(|e| format!("Failed to clear stale {}: {}", stale, e))?;
411 }
412 tx.commit().map_err(|e| format!("Failed to commit tx: {}", e))?;
413 Ok(())
414}