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 remove_setting(key: &str) -> Result<(), String> {
26 let conn = super::get_write_connection_guard_static()?;
27 conn.execute("DELETE FROM settings WHERE key = ?1", rusqlite::params![key])
28 .map_err(|e| format!("Failed to remove setting: {}", e))?;
29 Ok(())
30}
31
32pub fn get_pkey() -> Result<Option<String>, String> {
34 let conn = super::get_db_connection_guard_static()?;
35 Ok(conn.query_row(
36 "SELECT value FROM settings WHERE key = 'pkey'",
37 [],
38 |row| row.get(0),
39 ).ok())
40}
41
42pub fn set_pkey(pkey: &str) -> Result<(), String> {
44 let conn = super::get_write_connection_guard_static()?;
45 conn.execute(
46 "INSERT OR REPLACE INTO settings (key, value) VALUES ('pkey', ?1)",
47 rusqlite::params![pkey],
48 ).map_err(|e| format!("Failed to set pkey: {}", e))?;
49 Ok(())
50}
51
52pub fn get_seed() -> Result<Option<String>, String> {
54 let conn = super::get_db_connection_guard_static()?;
55 Ok(conn.query_row(
56 "SELECT value FROM settings WHERE key = 'seed'",
57 [],
58 |row| row.get(0),
59 ).ok())
60}
61
62pub fn set_seed(seed: &str) -> Result<(), String> {
64 let conn = super::get_write_connection_guard_static()?;
65 conn.execute(
66 "INSERT OR REPLACE INTO settings (key, value) VALUES ('seed', ?1)",
67 rusqlite::params![seed],
68 ).map_err(|e| format!("Failed to set seed: {}", e))?;
69 Ok(())
70}
71
72pub fn commit_account_setup(
87 pkey: &str,
88 encryption_enabled: bool,
89 security_type: Option<&str>,
90 encrypted_seed: Option<&str>,
91) -> Result<(), String> {
92 let mut conn = super::get_write_connection_guard_static()?;
93 let tx = conn.transaction()
94 .map_err(|e| format!("Failed to begin tx: {}", e))?;
95 tx.execute(
96 "INSERT OR REPLACE INTO settings (key, value) VALUES ('pkey', ?1)",
97 rusqlite::params![pkey],
98 ).map_err(|e| format!("Failed to set pkey: {}", e))?;
99 tx.execute(
100 "INSERT OR REPLACE INTO settings (key, value) VALUES ('encryption_enabled', ?1)",
101 rusqlite::params![if encryption_enabled { "true" } else { "false" }],
102 ).map_err(|e| format!("Failed to set encryption_enabled: {}", e))?;
103 if let Some(st) = security_type {
104 tx.execute(
105 "INSERT OR REPLACE INTO settings (key, value) VALUES ('security_type', ?1)",
106 rusqlite::params![st],
107 ).map_err(|e| format!("Failed to set security_type: {}", e))?;
108 } else {
109 tx.execute(
112 "DELETE FROM settings WHERE key = 'security_type'",
113 [],
114 ).map_err(|e| format!("Failed to clear security_type: {}", e))?;
115 }
116 if let Some(seed) = encrypted_seed {
117 tx.execute(
118 "INSERT OR REPLACE INTO settings (key, value) VALUES ('seed', ?1)",
119 rusqlite::params![seed],
120 ).map_err(|e| format!("Failed to set seed: {}", e))?;
121 }
122 tx.execute(
127 "INSERT OR REPLACE INTO settings (key, value) VALUES ('signer_type', 'local')",
128 [],
129 ).map_err(|e| format!("Failed to set signer_type: {}", e))?;
130 tx.commit().map_err(|e| format!("Failed to commit tx: {}", e))?;
131 Ok(())
132}
133
134pub fn get_signer_type() -> Result<String, String> {
152 let conn = super::get_db_connection_guard_static()?;
153 Ok(conn.query_row(
154 "SELECT value FROM settings WHERE key = 'signer_type'",
155 [],
156 |row| row.get::<_, String>(0),
157 ).unwrap_or_else(|_| "local".to_string()))
158}
159
160pub fn set_signer_type(value: &str) -> Result<(), String> {
164 let conn = super::get_write_connection_guard_static()?;
165 conn.execute(
166 "INSERT OR REPLACE INTO settings (key, value) VALUES ('signer_type', ?1)",
167 rusqlite::params![value],
168 ).map_err(|e| format!("Failed to set signer_type: {}", e))?;
169 Ok(())
170}
171
172pub async fn get_bunker_url() -> Result<Option<String>, String> {
176 let raw: Option<String> = {
177 let conn = super::get_db_connection_guard_static()?;
178 conn.query_row(
179 "SELECT value FROM settings WHERE key = 'bunker_url'",
180 [],
181 |row| row.get::<_, String>(0),
182 ).ok()
183 };
184 match raw {
185 Some(s) => match crate::crypto::maybe_decrypt(s).await {
186 Ok(plain) => Ok(Some(plain)),
187 Err(_) => Err("bunker_url decryption failed (account locked?)".into()),
188 },
189 None => Ok(None),
190 }
191}
192
193pub async fn set_bunker_url(url: &str) -> Result<(), String> {
196 let stored = crate::crypto::maybe_encrypt(url.to_string()).await;
197 let conn = super::get_write_connection_guard_static()?;
198 conn.execute(
199 "INSERT OR REPLACE INTO settings (key, value) VALUES ('bunker_url', ?1)",
200 rusqlite::params![stored],
201 ).map_err(|e| format!("Failed to set bunker_url: {}", e))?;
202 Ok(())
203}
204
205pub fn get_bunker_remote_pubkey() -> Result<Option<String>, String> {
210 let conn = super::get_db_connection_guard_static()?;
211 Ok(conn.query_row(
212 "SELECT value FROM settings WHERE key = 'bunker_remote_pubkey'",
213 [],
214 |row| row.get::<_, String>(0),
215 ).ok())
216}
217
218pub fn set_bunker_remote_pubkey(pubkey_hex: &str) -> Result<(), String> {
222 let conn = super::get_write_connection_guard_static()?;
223 conn.execute(
224 "INSERT OR REPLACE INTO settings (key, value) VALUES ('bunker_remote_pubkey', ?1)",
225 rusqlite::params![pubkey_hex],
226 ).map_err(|e| format!("Failed to set bunker_remote_pubkey: {}", e))?;
227 Ok(())
228}
229
230pub fn commit_bunker_account_setup(
241 pkey: &str,
242 encryption_enabled: bool,
243 security_type: Option<&str>,
244 bunker_url_stored: &str,
245 bunker_remote_pubkey_hex: &str,
246) -> Result<(), String> {
247 let mut conn = super::get_write_connection_guard_static()?;
248 let tx = conn.transaction()
249 .map_err(|e| format!("Failed to begin tx: {}", e))?;
250 tx.execute(
251 "INSERT OR REPLACE INTO settings (key, value) VALUES ('pkey', ?1)",
252 rusqlite::params![pkey],
253 ).map_err(|e| format!("Failed to set pkey: {}", e))?;
254 tx.execute(
255 "INSERT OR REPLACE INTO settings (key, value) VALUES ('encryption_enabled', ?1)",
256 rusqlite::params![if encryption_enabled { "true" } else { "false" }],
257 ).map_err(|e| format!("Failed to set encryption_enabled: {}", e))?;
258 if let Some(st) = security_type {
259 tx.execute(
260 "INSERT OR REPLACE INTO settings (key, value) VALUES ('security_type', ?1)",
261 rusqlite::params![st],
262 ).map_err(|e| format!("Failed to set security_type: {}", e))?;
263 } else {
264 tx.execute(
265 "DELETE FROM settings WHERE key = 'security_type'",
266 [],
267 ).map_err(|e| format!("Failed to clear security_type: {}", e))?;
268 }
269 tx.execute(
270 "INSERT OR REPLACE INTO settings (key, value) VALUES ('signer_type', 'bunker')",
271 [],
272 ).map_err(|e| format!("Failed to set signer_type: {}", e))?;
273 tx.execute(
274 "INSERT OR REPLACE INTO settings (key, value) VALUES ('bunker_url', ?1)",
275 rusqlite::params![bunker_url_stored],
276 ).map_err(|e| format!("Failed to set bunker_url: {}", e))?;
277 tx.execute(
278 "INSERT OR REPLACE INTO settings (key, value) VALUES ('bunker_remote_pubkey', ?1)",
279 rusqlite::params![bunker_remote_pubkey_hex],
280 ).map_err(|e| format!("Failed to set bunker_remote_pubkey: {}", e))?;
281 tx.execute("DELETE FROM settings WHERE key = 'seed'", [])
283 .map_err(|e| format!("Failed to clear stale seed: {}", e))?;
284 tx.commit().map_err(|e| format!("Failed to commit tx: {}", e))?;
285 Ok(())
286}