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
|
|
|
|
-- | Returns the list of wallets available in the given database
|
|
|
|
checkWallets ::
|
|
|
|
T.Text -- ^ The database name to check
|
2024-02-13 20:19:05 +00:00
|
|
|
-> ZcashNet -- ^ The network the wallet is running
|
2024-02-12 21:09:36 +00:00
|
|
|
-> IO [Entity ZcashWallet]
|
2024-02-13 20:19:05 +00:00
|
|
|
checkWallets dbName znet = do
|
2024-02-09 22:18:48 +00:00
|
|
|
runSqlite dbName $ do runMigration migrateAll
|
2024-02-13 20:19:05 +00:00
|
|
|
runSqlite dbName $ selectList [ZcashWalletNetwork ==. znet] []
|
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-14 18:03:18 +00:00
|
|
|
checkZebra host port = do
|
|
|
|
res <- makeZebraCall host port "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-14 18:03:18 +00:00
|
|
|
checkBlockChain host port = do
|
|
|
|
let f = makeZebraCall host port
|
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)
|
|
|
|
connectZebra host port m params = do
|
|
|
|
res <- makeZebraCall host port m params
|
2024-02-12 21:09:36 +00:00
|
|
|
let body = responseBody res
|
|
|
|
return $ result body
|