From 649239013b1aa0e9c7ce18beab5836ed64541e87 Mon Sep 17 00:00:00 2001 From: Rigidity Date: Thu, 28 Nov 2024 13:15:49 -0500 Subject: [PATCH] Make resync not delete offers by default --- crates/sage-api/src/requests/keys.rs | 2 + crates/sage/src/endpoints/keys.rs | 21 +++++++++- crates/sage/src/sage.rs | 32 ++++++++------- src/bindings.ts | 2 +- src/pages/Login.tsx | 61 +++++++++++++++++++++------- 5 files changed, 86 insertions(+), 32 deletions(-) diff --git a/crates/sage-api/src/requests/keys.rs b/crates/sage-api/src/requests/keys.rs index e381bfd1..f4cdc01a 100644 --- a/crates/sage-api/src/requests/keys.rs +++ b/crates/sage-api/src/requests/keys.rs @@ -20,6 +20,8 @@ pub struct LogoutResponse {} #[derive(Debug, Clone, Copy, Serialize, Deserialize, Type)] pub struct Resync { pub fingerprint: u32, + #[serde(default)] + pub delete_offer_files: bool, } #[derive(Debug, Clone, Copy, Serialize, Deserialize, Type)] diff --git a/crates/sage/src/endpoints/keys.rs b/crates/sage/src/endpoints/keys.rs index 44c5ca48..2e9bf8d9 100644 --- a/crates/sage/src/endpoints/keys.rs +++ b/crates/sage/src/endpoints/keys.rs @@ -37,7 +37,26 @@ impl Sage { self.switch_wallet().await?; } - self.delete_wallet_db(req.fingerprint)?; + let pool = self.connect_to_database(req.fingerprint).await?; + + sqlx::query!( + " + DELETE FROM `coin_states`; + DELETE FROM `transactions`; + DELETE FROM `peaks`; + DELETE FROM `cats`; + DELETE FROM `future_did_names`; + DELETE FROM `collections`; + DELETE FROM `nft_data`; + DELETE FROM `nft_uris`; + " + ) + .execute(&pool) + .await?; + + if req.delete_offer_files { + sqlx::query!("DELETE FROM `offers`").execute(&pool).await?; + } if login { self.config.app.active_fingerprint = Some(req.fingerprint); diff --git a/crates/sage/src/sage.rs b/crates/sage/src/sage.rs index 426c6eba..661aa12b 100644 --- a/crates/sage/src/sage.rs +++ b/crates/sage/src/sage.rs @@ -16,7 +16,7 @@ use sage_keychain::Keychain; use sage_wallet::{PeerState, SyncCommand, SyncEvent, SyncManager, SyncOptions, Timeouts, Wallet}; use sqlx::{ sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions}, - ConnectOptions, + ConnectOptions, SqlitePool, }; use tokio::sync::{mpsc, Mutex}; use tracing::{error, info, Level}; @@ -231,19 +231,10 @@ impl Sage { }; let intermediate_pk = master_to_wallet_unhardened_intermediate(&master_pk); - let path = self.wallet_db_path(fingerprint)?; - - let pool = SqlitePoolOptions::new() - .connect_with( - SqliteConnectOptions::from_str(&format!("sqlite://{}?mode=rwc", path.display()))? - .journal_mode(SqliteJournalMode::Wal) - .log_statements(log::LevelFilter::Trace), - ) - .await?; - - sqlx::migrate!("../../migrations").run(&pool).await?; + let pool = self.connect_to_database(fingerprint).await?; let db = Database::new(pool); + let wallet = Arc::new(Wallet::new( db.clone(), fingerprint, @@ -363,12 +354,23 @@ impl Sage { Ok(amount) } - pub fn delete_wallet_db(&self, fingerprint: u32) -> Result<()> { + pub async fn connect_to_database(&self, fingerprint: u32) -> Result { let path = self.wallet_db_path(fingerprint)?; - Ok(fs::remove_file(path)?) + + let pool = SqlitePoolOptions::new() + .connect_with( + SqliteConnectOptions::from_str(&format!("sqlite://{}?mode=rwc", path.display()))? + .journal_mode(SqliteJournalMode::Wal) + .log_statements(log::LevelFilter::Trace), + ) + .await?; + + sqlx::migrate!("../../migrations").run(&pool).await?; + + Ok(pool) } - pub fn wallet_db_path(&self, fingerprint: u32) -> Result { + fn wallet_db_path(&self, fingerprint: u32) -> Result { let path = self.path.join("wallets").join(fingerprint.to_string()); fs::create_dir_all(&path)?; let network_id = &self.config.network.network_id; diff --git a/src/bindings.ts b/src/bindings.ts index eb18c299..d7022a11 100644 --- a/src/bindings.ts +++ b/src/bindings.ts @@ -618,7 +618,7 @@ export type RemovePeer = { ip: string; ban: boolean } export type RemovePeerResponse = Record export type RenameKey = { fingerprint: number; name: string } export type RenameKeyResponse = Record -export type Resync = { fingerprint: number } +export type Resync = { fingerprint: number; delete_offer_files?: boolean } export type ResyncResponse = Record export type SecretKeyInfo = { mnemonic: string | null; secret_key: string } export type SendCat = { asset_id: string; address: string; amount: Amount; fee: Amount; auto_submit?: boolean } diff --git a/src/pages/Login.tsx b/src/pages/Login.tsx index 0348450d..a9b863e2 100644 --- a/src/pages/Login.tsx +++ b/src/pages/Login.tsx @@ -33,9 +33,11 @@ import { useNavigate } from 'react-router-dom'; import { commands, KeyInfo, SecretKeyInfo } from '../bindings'; import Container from '../components/Container'; import { loginAndUpdateState } from '../state'; +import { Switch } from '@/components/ui/switch'; export default function Login() { const [keys, setKeys] = useState(null); + const [network, setNetwork] = useState(null); const navigate = useNavigate(); @@ -45,6 +47,12 @@ export default function Login() { setKeys(res.data.keys); } }); + + commands.networkConfig().then((res) => { + if (res.status === 'ok') { + setNetwork(res.data.network_id); + } + }); }, []); useEffect(() => { @@ -68,12 +76,18 @@ export default function Login() { )} - {' '} + {keys !== null ? ( keys.length ? (
{keys.map((key, i) => ( - + ))}
) : ( @@ -99,30 +113,39 @@ function SkeletonWalletList() { } interface WalletItemProps { + network: string | null; info: KeyInfo; keys: KeyInfo[]; setKeys: (keys: KeyInfo[]) => void; } -function WalletItem({ info, keys, setKeys }: WalletItemProps) { +function WalletItem({ network, info, keys, setKeys }: WalletItemProps) { const navigate = useNavigate(); - const [anchorEl, setAnchorEl] = useState(null); + const [anchorEl, _setAnchorEl] = useState(null); const isMenuOpen = Boolean(anchorEl); const [isDeleteOpen, setDeleteOpen] = useState(false); - const [isRenameOpen, setRenameOpen] = useState(false); + const [isDetailsOpen, setDetailsOpen] = useState(false); - const [isResyncOpen, setResyncOpen] = useState(false); + const [secrets, setSecrets] = useState(null); + + const [isRenameOpen, setRenameOpen] = useState(false); const [newName, setNewName] = useState(''); - const [secrets, setSecrets] = useState(null); + const [isResyncOpen, setResyncOpen] = useState(false); + const [deleteOffers, setDeleteOffers] = useState(false); const resyncSelf = () => { - commands.resync({ fingerprint: info.fingerprint }).then((res) => { - if (res.status === 'error') return; - setResyncOpen(false); - }); + commands + .resync({ + fingerprint: info.fingerprint, + delete_offer_files: deleteOffers, + }) + .then((res) => { + if (res.status === 'error') return; + setResyncOpen(false); + }); }; const deleteSelf = () => { @@ -224,7 +247,7 @@ function WalletItem({ info, keys, setKeys }: WalletItemProps) { }} > - Resync + Resync ({network}) - Resync Wallet + Resync on {network} Are you sure you want to resync this wallet's data? This will - remove custom names for tokens and profiles and redownload all of - the data from the network. + redownload data from the network which can take a while depending + on the size of the wallet. +
+ + setDeleteOffers(value)} + /> +