2024-02-12 21:09:36 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
-- Core wallet functionality for Zenith
|
2024-02-09 22:18:48 +00:00
|
|
|
module Zenith.Core where
|
|
|
|
|
2024-02-12 21:09:36 +00:00
|
|
|
import Data.Aeson
|
2024-02-09 22:18:48 +00:00
|
|
|
import qualified Data.Text as T
|
|
|
|
import Database.Persist
|
|
|
|
import Database.Persist.Sqlite
|
2024-02-12 21:09:36 +00:00
|
|
|
import Network.HTTP.Client
|
|
|
|
import ZcashHaskell.Types
|
|
|
|
import ZcashHaskell.Utils
|
2024-02-09 22:18:48 +00:00
|
|
|
import Zenith.DB
|
|
|
|
|
2024-02-12 21:09:36 +00:00
|
|
|
-- * Database functions
|
2024-02-28 21:12:57 +00:00
|
|
|
-- | Initializes the database
|
|
|
|
initDb ::
|
|
|
|
T.Text -- ^ The database path to check
|
|
|
|
-> IO ()
|
|
|
|
initDb dbName = do
|
2024-02-09 22:18:48 +00:00
|
|
|
runSqlite dbName $ do runMigration migrateAll
|
2024-02-28 21:12:57 +00:00
|
|
|
|
|
|
|
-- | Save a new wallet to the database
|
|
|
|
saveWallet ::
|
|
|
|
T.Text -- ^ The database path to use
|
|
|
|
-> ZcashWallet -- ^ The wallet to add to the database
|
2024-02-28 22:37:43 +00:00
|
|
|
-> IO (Maybe (Entity ZcashWallet))
|
|
|
|
saveWallet dbFp w = runSqlite dbFp $ insertUniqueEntity w
|
2024-02-28 21:12:57 +00:00
|
|
|
|
|
|
|
-- | Returns a list of accounts associated with the given wallet
|
|
|
|
getAccounts ::
|
|
|
|
T.Text -- ^ The database path
|
|
|
|
-> ZcashWalletId -- ^ The wallet ID to check
|
|
|
|
-> IO [Entity ZcashAccount]
|
|
|
|
getAccounts dbFp w = runSqlite dbFp $ selectList [ZcashAccountWalletId ==. w] []
|
|
|
|
|
2024-03-01 20:57:13 +00:00
|
|
|
-- | Returns the largest account index for the given wallet
|
|
|
|
getMaxAccount ::
|
|
|
|
T.Text -- ^ The database path
|
|
|
|
-> ZcashWalletId -- ^ The wallet ID to check
|
|
|
|
-> IO Int
|
|
|
|
getMaxAccount dbFp w = do
|
|
|
|
a <-
|
|
|
|
runSqlite dbFp $
|
|
|
|
selectFirst [ZcashAccountWalletId ==. w] [Desc ZcashAccountIndex]
|
|
|
|
case a of
|
|
|
|
Nothing -> return $ -1
|
|
|
|
Just x -> return $ zcashAccountIndex $ entityVal x
|
|
|
|
|
2024-02-28 21:12:57 +00:00
|
|
|
-- | Save a new account to the database
|
|
|
|
saveAccount ::
|
|
|
|
T.Text -- ^ The database path
|
|
|
|
-> ZcashAccount -- ^ The account to add to the database
|
2024-03-01 20:57:13 +00:00
|
|
|
-> IO (Maybe (Entity ZcashAccount))
|
|
|
|
saveAccount dbFp a = runSqlite dbFp $ insertUniqueEntity a
|
2024-02-28 21:12:57 +00:00
|
|
|
|
|
|
|
-- | Returns a list of addresses associated with the given account
|
|
|
|
getAddresses ::
|
|
|
|
T.Text -- ^ The database path
|
|
|
|
-> ZcashAccountId -- ^ The account ID to check
|
|
|
|
-> IO [Entity WalletAddress]
|
|
|
|
getAddresses dbFp a = runSqlite dbFp $ selectList [WalletAddressAccId ==. a] []
|
2024-02-12 21:09:36 +00:00
|
|
|
|
|
|
|
-- * Zebra Node interaction
|
|
|
|
-- | Checks the status of the `zebrad` node
|
|
|
|
checkZebra ::
|
2024-02-14 18:03:18 +00:00
|
|
|
T.Text -- ^ Host where `zebrad` is available
|
|
|
|
-> Int -- ^ Port where `zebrad` is available
|
2024-02-12 21:09:36 +00:00
|
|
|
-> IO (Maybe ZebraGetInfo)
|
2024-02-28 21:12:57 +00:00
|
|
|
checkZebra nodeHost nodePort = do
|
|
|
|
res <- makeZebraCall nodeHost nodePort "getinfo" []
|
2024-02-12 21:09:36 +00:00
|
|
|
let body = responseBody (res :: Response (RpcResponse ZebraGetInfo))
|
|
|
|
return $ result body
|
|
|
|
|
|
|
|
-- | Checks the status of the Zcash blockchain
|
|
|
|
checkBlockChain ::
|
2024-02-14 18:03:18 +00:00
|
|
|
T.Text -- ^ Host where `zebrad` is available
|
|
|
|
-> Int -- ^ Port where `zebrad` is available
|
2024-02-12 21:09:36 +00:00
|
|
|
-> IO (Maybe ZebraGetBlockChainInfo)
|
2024-02-28 21:12:57 +00:00
|
|
|
checkBlockChain nodeHost nodePort = do
|
|
|
|
let f = makeZebraCall nodeHost nodePort
|
2024-02-12 21:09:36 +00:00
|
|
|
result <$> (responseBody <$> f "getblockchaininfo" [])
|
|
|
|
|
|
|
|
-- | Generic RPC call function
|
|
|
|
connectZebra ::
|
2024-02-14 18:03:18 +00:00
|
|
|
FromJSON a => T.Text -> Int -> T.Text -> [Data.Aeson.Value] -> IO (Maybe a)
|
2024-02-28 21:12:57 +00:00
|
|
|
connectZebra nodeHost nodePort m params = do
|
|
|
|
res <- makeZebraCall nodeHost nodePort m params
|
2024-02-12 21:09:36 +00:00
|
|
|
let body = responseBody res
|
|
|
|
return $ result body
|