feat: enable VK fields

This commit is contained in:
Rene Vergara 2025-02-14 09:22:07 -06:00
parent e9995791f3
commit edb793ffa1
Signed by: pitmutt
SSH key fingerprint: SHA256:vNa8FIqbBZjV9hOCkXyOzd7gqWCMCfkcfiPH2zaGfQ0
3 changed files with 167 additions and 52 deletions

View file

@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Changed
- Database schema for wallets and accounts
## [0.8.0.0-beta]
### Added

View file

@ -13,3 +13,8 @@ source-repository-package
type: git
location: https://code.vergara.tech/Vergara_Tech/haskell-foreign-rust.git
tag: 335e804454cd30da2c526457be37e477f71e4665
source-repository-package
type: git
location: https://code.vergara.tech/Vergara_Tech/persistent-sqlite.git
tag: 85093ef51cb2bd245ac9a85925770fdb55afce9e

View file

@ -27,6 +27,7 @@ import Control.Monad.Logger
, NoLoggingT
, logDebugN
, logErrorN
, logInfoN
, runNoLoggingT
, runStderrLoggingT
)
@ -109,6 +110,16 @@ import Zenith.Types
, ZenithUuid(..)
)
share
[mkPersist sqlSettings, mkMigrate "schemaMigration"]
[persistLowerCase|
ZenithSchema
version Int
action T.Text
UniqueAction version action
deriving Show Eq
|]
share
[mkPersist sqlSettings, mkMigrate "migrateAll"]
[persistLowerCase|
@ -118,19 +129,19 @@ share
seedPhrase PhraseDB
birthdayHeight Int
lastSync Int default=0
local Bool default=true
local Bool default=TRUE
UniqueWallet name network
deriving Show Eq
ZcashAccount
index Int
walletId ZcashWalletId
name T.Text
orchSpendKey OrchardSpendingKeyDB Maybe
sapSpendKey SaplingSpendingKeyDB Maybe
tPrivateKey TransparentSpendingKeyDB Maybe
orchSpendKey OrchardSpendingKeyDB Maybe default=NULL
sapSpendKey SaplingSpendingKeyDB Maybe default=NULL
tPrivateKey TransparentSpendingKeyDB Maybe default=NULL
fvk UnifiedFvkDB Maybe default=NULL
ivk UnifiedIvkDB Maybe default=NULL
type AccountType default=Local
type AccountType default='Local'
UniqueAccount index walletId
UniqueAccName walletId name
deriving Show Eq
@ -434,61 +445,154 @@ orchToZcashNoteAPI pool n = do
-- | Initializes the database
initDb ::
T.Text -- ^ The database path to check
-> IO (Either String Bool)
-> LoggingT IO (Either String Bool)
initDb dbName = do
j <-
try $ PS.runSqlite dbName $ runMigrationQuiet migrateAll :: IO
(Either SomeException [T.Text])
case j of
Left _e1 -> do
pool <- runNoLoggingT $ initPool dbName
wallets <-
runNoLoggingT $
PS.retryOnBusy $
flip PS.runSqlPool pool $ do select . from $ table @ZcashWallet
accounts <-
runNoLoggingT $
PS.retryOnBusy $
flip PS.runSqlPool pool $ do select . from $ table @ZcashAccount
abook <-
runNoLoggingT $
PS.retryOnBusy $
flip PS.runSqlPool pool $ do select . from $ table @AddressBook
hDir <- getHomeDirectory
let backupDb = hDir </> "Zenith/.backup.db"
checkDbFile <- doesFileExist backupDb
when checkDbFile $ removeFile backupDb
_ <- PS.runSqlite (T.pack backupDb) $ runMigrationQuiet migrateAll
backupPool <- runNoLoggingT $ initPool $ T.pack backupDb
_ <-
runNoLoggingT $
PS.retryOnBusy $
flip PS.runSqlPool backupPool $ insertMany_ $ entityVal <$> wallets
_ <-
runNoLoggingT $
PS.retryOnBusy $
flip PS.runSqlPool backupPool $ insertMany_ $ entityVal <$> accounts
_ <-
runNoLoggingT $
PS.retryOnBusy $
flip PS.runSqlPool backupPool $ insertMany_ $ entityVal <$> abook
clearWalletTransactions pool
clearWalletData pool
m <-
try $ PS.runSqlite dbName $ runMigrationUnsafeQuiet migrateAll :: IO
(Either SomeException [T.Text])
case m of
Left e2 -> return $ Left $ "Failed to migrate data tables" ++ show e2
Right _ -> do
return $ Right True
x <-
liftIO
(try $ PS.runSqlite dbName $ runMigrationUnsafeQuiet schemaMigration :: IO
(Either SomeException [T.Text]))
case x of
Left _ -> do
logErrorN "Failed to initiate schema table"
return $ Left "Failed to initiate schema table"
Right _ -> do
return $ Right False
j <-
liftIO
(try $ PS.runSqlite dbName $ runMigrationQuiet migrateAll :: IO
(Either SomeException [T.Text]))
case j of
Left e1 -> do
logDebugN "Automatic migration failed, starting manual"
pool <- liftIO $ runNoLoggingT $ initPool dbName
versions <- liftIO $ getVersions pool
migrateTables pool versions
PS.runSqlite dbName $ printMigration migrateAll
m <-
liftIO
(try $ PS.runSqlite dbName $ runMigration migrateAll :: IO
(Either SomeException ()))
case m of
Left e2 -> do
logErrorN $ "Failed to migrate data tables " <> T.pack (show e2)
return $ Left $ "Failed to migrate data tables" ++ show e2
Right _ -> do
logInfoN "Migration of tables successful"
wallets <-
runNoLoggingT $
PS.retryOnBusy $
flip PS.runSqlPool pool $ do select . from $ table @ZcashWallet
accounts <-
runNoLoggingT $
PS.retryOnBusy $
flip PS.runSqlPool pool $ do select . from $ table @ZcashAccount
abook <-
runNoLoggingT $
PS.retryOnBusy $
flip PS.runSqlPool pool $ do select . from $ table @AddressBook
hDir <- liftIO getHomeDirectory
let backupDb = hDir </> "Zenith/.backup.db"
checkDbFile <- liftIO $ doesFileExist backupDb
when checkDbFile $ liftIO $ removeFile backupDb
_ <- PS.runSqlite (T.pack backupDb) $ runMigrationQuiet migrateAll
backupPool <- liftIO $ runNoLoggingT $ initPool $ T.pack backupDb
_ <-
runNoLoggingT $
PS.retryOnBusy $
flip PS.runSqlPool backupPool $
insertMany_ $ entityVal <$> wallets
_ <-
runNoLoggingT $
PS.retryOnBusy $
flip PS.runSqlPool backupPool $
insertMany_ $ entityVal <$> accounts
_ <-
runNoLoggingT $
PS.retryOnBusy $
flip PS.runSqlPool backupPool $
insertMany_ $ entityVal <$> abook
return $ Right False
Right _ -> do
pool <- liftIO $ runNoLoggingT $ initPool dbName
_ <-
runNoLoggingT $
PS.retryOnBusy $
flip PS.runSqlPool pool $ do
upsert (ZenithSchema 1 "Viewing Keys") []
return $ Right False
migrateTables :: ConnectionPool -> [Int] -> LoggingT IO ()
migrateTables pool versions = do
unless (1 `elem` versions) $ do
logDebugN "Making version 1 changes"
_ <-
runNoLoggingT $
PS.retryOnBusy $
flip PS.runSqlPool pool $ do
rawExecute
"ALTER TABLE \"zcash_wallet\" ADD COLUMN \"local\" BOOLEAN NOT NULL DEFAULT TRUE;"
[]
rawExecute
"ALTER TABLE \"zcash_account\" RENAME COLUMN \"orch_spend_key\" TO \"orch_spend_key_old\";"
[]
rawExecute
"ALTER TABLE \"zcash_account\" RENAME COLUMN \"sap_spend_key\" TO \"sap_spend_key_old\";"
[]
rawExecute
"ALTER TABLE \"zcash_account\" RENAME COLUMN \"t_private_key\" TO \"t_private_key_old\";"
[]
rawExecute
"ALTER TABLE \"zcash_account\" ADD COLUMN \"orch_spend_key\" VARCHAR NULL DEFAULT NULL;"
[]
rawExecute
"ALTER TABLE \"zcash_account\" ADD COLUMN \"sap_spend_key\" VARCHAR NULL DEFAULT NULL;"
[]
rawExecute
"ALTER TABLE \"zcash_account\" ADD COLUMN \"t_private_key\" VARCHAR NULL DEFAULT NULL;"
[]
rawExecute
"UPDATE \"zcash_account\" SET \"orch_spend_key\" = \"orch_spend_key_old\", \"sap_spend_key\" = \"sap_spend_key_old\", \"t_private_key\" = \"t_private_key_old\" WHERE 1=1;"
[]
rawExecute
"ALTER TABLE \"zcash_account\" DROP COLUMN \"orch_spend_key_old\";"
[]
rawExecute
"ALTER TABLE \"zcash_account\" DROP COLUMN \"sap_spend_key_old\";"
[]
rawExecute
"ALTER TABLE \"zcash_account\" DROP COLUMN \"t_private_key_old\";"
[]
rawExecute
"ALTER TABLE \"zcash_account\" ADD COLUMN \"fvk\" VARCHAR NULL DEFAULT NULL;"
[]
rawExecute
"ALTER TABLE \"zcash_account\" ADD COLUMN \"ivk\" VARCHAR NULL DEFAULT NULL;"
[]
rawExecute
"ALTER TABLE \"zcash_account\" ADD COLUMN \"type\" VARCHAR NOT NULL DEFAULT 'Local';"
[]
_ <-
runNoLoggingT $
PS.retryOnBusy $
flip PS.runSqlPool pool $ do upsert (ZenithSchema 1 "Viewing Keys") []
logDebugN "Version 1 changes complete"
initPool :: T.Text -> NoLoggingT IO ConnectionPool
initPool dbPath = do
let dbInfo = PS.mkSqliteConnectionInfo dbPath
PS.createSqlitePoolFromInfo dbInfo 5
getVersions :: ConnectionPool -> IO [Int]
getVersions pool = do
versions <-
runNoLoggingT $
PS.retryOnBusy $
flip PS.runSqlPool pool $ do
select $ do
v <- from $ table @ZenithSchema
orderBy [asc $ v ^. ZenithSchemaVersion]
pure (v ^. ZenithSchemaVersion)
return $ map (\(Value x) -> x) versions
-- | Upgrade the database
upgradeDb ::
T.Text -- ^ database path