From 0de5dc4f9c7814e102499907965391e4202b947e Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Wed, 28 Feb 2024 16:37:43 -0600 Subject: [PATCH] Avoid creating wallets with the same name --- src/Zenith/CLI.hs | 26 ++++++++++++++++++-------- src/Zenith/Core.hs | 4 ++-- src/Zenith/DB.hs | 1 + 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/Zenith/CLI.hs b/src/Zenith/CLI.hs index 106b1ae..1a8f8ed 100644 --- a/src/Zenith/CLI.hs +++ b/src/Zenith/CLI.hs @@ -212,9 +212,7 @@ appEvent (BT.VtyEvent e) = do V.EvKey V.KEnter [] -> do fs <- BT.zoom inputForm $ BT.gets formState nw <- liftIO $ addNewWallet (fs ^. dialogInput) s - BT.modify $ set wallets nw - printMsg $ - "Creating new wallet " <> T.unpack (fs ^. dialogInput) + BT.put nw BT.modify $ set dialogBox Blank ev -> BT.zoom inputForm $ handleFormEvent (BT.VtyEvent ev) AName -> do @@ -311,12 +309,24 @@ runZenithCLI host port dbFilePath = do "No Zebra node available on port " <> show port <> ". Check your configuration" -addNewWallet :: - T.Text -> State -> IO (L.GenericList Name Vec.Vector (Entity ZcashWallet)) +addNewWallet :: T.Text -> State -> IO State addNewWallet n s = do sP <- generateWalletSeedPhrase let bH = s ^. startBlock let netName = read $ s ^. network - _ <- saveWallet (s ^. dbPath) $ ZcashWallet sP bH n netName - wL <- getWallets (s ^. dbPath) netName - return $ L.listReplace (Vec.fromList wL) (Just 0) $ s ^. wallets + r <- saveWallet (s ^. dbPath) $ ZcashWallet sP bH n netName + case r of + Nothing -> do + wL <- getWallets (s ^. dbPath) netName + return $ + (s & wallets .~ L.listReplace (Vec.fromList wL) (Just 0) (s ^. wallets)) & + msg .~ + "Wallet already exists: " ++ + T.unpack n + Just _ -> do + wL <- getWallets (s ^. dbPath) netName + return $ + (s & wallets .~ L.listReplace (Vec.fromList wL) (Just 0) (s ^. wallets)) & + msg .~ + "Created new wallet: " ++ + T.unpack n diff --git a/src/Zenith/Core.hs b/src/Zenith/Core.hs index 40836f5..f261373 100644 --- a/src/Zenith/Core.hs +++ b/src/Zenith/Core.hs @@ -24,8 +24,8 @@ initDb dbName = do saveWallet :: T.Text -- ^ The database path to use -> ZcashWallet -- ^ The wallet to add to the database - -> IO ZcashWalletId -saveWallet dbFp w = runSqlite dbFp $ insert w + -> IO (Maybe (Entity ZcashWallet)) +saveWallet dbFp w = runSqlite dbFp $ insertUniqueEntity w -- | Returns a list of accounts associated with the given wallet getAccounts :: diff --git a/src/Zenith/DB.hs b/src/Zenith/DB.hs index 8af8832..260e8a2 100644 --- a/src/Zenith/DB.hs +++ b/src/Zenith/DB.hs @@ -34,6 +34,7 @@ share birthdayHeight Int name T.Text network ZcashNet + UniqueWallet name network deriving Show ZcashAccount walletId ZcashWalletId