2024-02-09 22:18:48 +00:00
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
2024-02-12 21:09:36 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2024-02-09 22:18:48 +00:00
|
|
|
|
2024-02-08 19:26:54 +00:00
|
|
|
module Zenith.CLI where
|
|
|
|
|
2024-02-09 22:18:48 +00:00
|
|
|
import Control.Monad (void)
|
2024-02-28 21:12:57 +00:00
|
|
|
import Control.Monad.IO.Class (liftIO)
|
2024-02-09 22:18:48 +00:00
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Graphics.Vty as V
|
2024-02-11 16:33:22 +00:00
|
|
|
import Lens.Micro ((&), (.~), (^.), set)
|
2024-02-09 22:18:48 +00:00
|
|
|
import Lens.Micro.Mtl
|
|
|
|
import Lens.Micro.TH
|
|
|
|
|
|
|
|
import qualified Brick.AttrMap as A
|
2024-02-14 18:03:18 +00:00
|
|
|
import qualified Brick.Focus as F
|
2024-02-19 20:05:32 +00:00
|
|
|
import Brick.Forms
|
|
|
|
( Form(..)
|
|
|
|
, (@@=)
|
|
|
|
, editTextField
|
|
|
|
, focusedFormInputAttr
|
|
|
|
, handleFormEvent
|
|
|
|
, newForm
|
|
|
|
, renderForm
|
2024-02-27 17:17:36 +00:00
|
|
|
, updateFormState
|
2024-02-19 20:05:32 +00:00
|
|
|
)
|
2024-02-09 22:18:48 +00:00
|
|
|
import qualified Brick.Main as M
|
|
|
|
import qualified Brick.Types as BT
|
|
|
|
import Brick.Types (Widget)
|
2024-02-14 18:03:18 +00:00
|
|
|
import Brick.Util (fg, on, style)
|
2024-02-09 22:18:48 +00:00
|
|
|
import qualified Brick.Widgets.Border as B
|
2024-02-14 18:03:18 +00:00
|
|
|
import Brick.Widgets.Border.Style (unicode, unicodeBold)
|
2024-02-09 22:18:48 +00:00
|
|
|
import qualified Brick.Widgets.Center as C
|
|
|
|
import Brick.Widgets.Core
|
2024-02-11 16:33:22 +00:00
|
|
|
( Padding(..)
|
|
|
|
, (<+>)
|
|
|
|
, (<=>)
|
2024-02-13 20:19:05 +00:00
|
|
|
, emptyWidget
|
2024-02-14 18:03:18 +00:00
|
|
|
, fill
|
2024-02-09 22:18:48 +00:00
|
|
|
, hLimit
|
|
|
|
, joinBorders
|
2024-02-13 20:19:05 +00:00
|
|
|
, padAll
|
2024-02-14 18:03:18 +00:00
|
|
|
, padBottom
|
2024-02-11 16:33:22 +00:00
|
|
|
, padRight
|
2024-02-09 22:18:48 +00:00
|
|
|
, str
|
2024-02-28 21:12:57 +00:00
|
|
|
, txt
|
2024-02-09 22:18:48 +00:00
|
|
|
, vBox
|
|
|
|
, vLimit
|
|
|
|
, withAttr
|
|
|
|
, withBorderStyle
|
|
|
|
)
|
2024-02-13 20:19:05 +00:00
|
|
|
import qualified Brick.Widgets.Dialog as D
|
2024-02-09 22:18:48 +00:00
|
|
|
import qualified Brick.Widgets.List as L
|
|
|
|
import qualified Data.Vector as Vec
|
2024-02-19 20:05:32 +00:00
|
|
|
import Database.Persist
|
2024-02-28 21:12:57 +00:00
|
|
|
import ZcashHaskell.Keys (generateWalletSeedPhrase)
|
2024-02-12 21:09:36 +00:00
|
|
|
import ZcashHaskell.Types
|
2024-02-09 22:18:48 +00:00
|
|
|
import Zenith.Core
|
2024-02-19 20:05:32 +00:00
|
|
|
import Zenith.DB
|
2024-02-09 22:18:48 +00:00
|
|
|
|
|
|
|
data Name
|
|
|
|
= WList
|
|
|
|
| AList
|
2024-02-29 21:02:58 +00:00
|
|
|
| AcList
|
2024-02-09 22:18:48 +00:00
|
|
|
| TList
|
2024-02-13 20:19:05 +00:00
|
|
|
| HelpDialog
|
2024-02-27 15:44:17 +00:00
|
|
|
| DialogInputField
|
2024-02-09 22:18:48 +00:00
|
|
|
deriving (Eq, Show, Ord)
|
|
|
|
|
2024-02-27 15:44:17 +00:00
|
|
|
data DialogInput = DialogInput
|
|
|
|
{ _dialogInput :: !T.Text
|
2024-02-14 18:03:18 +00:00
|
|
|
} deriving (Show)
|
|
|
|
|
2024-02-27 15:44:17 +00:00
|
|
|
makeLenses ''DialogInput
|
|
|
|
|
|
|
|
data DialogType
|
|
|
|
= WName
|
|
|
|
| AName
|
|
|
|
| Blank
|
2024-02-14 18:03:18 +00:00
|
|
|
|
2024-02-09 22:18:48 +00:00
|
|
|
data State = State
|
2024-02-11 16:33:22 +00:00
|
|
|
{ _network :: !String
|
2024-02-19 20:05:32 +00:00
|
|
|
, _wallets :: !(L.List Name (Entity ZcashWallet))
|
2024-02-29 21:02:58 +00:00
|
|
|
, _accounts :: !(L.List Name (Entity ZcashAccount))
|
2024-02-11 16:33:22 +00:00
|
|
|
, _addresses :: !(L.List Name String)
|
|
|
|
, _transactions :: !(L.List Name String)
|
|
|
|
, _msg :: !String
|
2024-02-13 20:19:05 +00:00
|
|
|
, _helpBox :: !Bool
|
2024-02-27 15:44:17 +00:00
|
|
|
, _dialogBox :: !DialogType
|
2024-02-14 18:03:18 +00:00
|
|
|
, _splashBox :: !Bool
|
2024-02-27 15:44:17 +00:00
|
|
|
, _inputForm :: !(Form DialogInput () Name)
|
2024-02-14 18:03:18 +00:00
|
|
|
, _focusRing :: !(F.FocusRing Name)
|
2024-02-28 21:12:57 +00:00
|
|
|
, _startBlock :: !Int
|
|
|
|
, _dbPath :: !T.Text
|
2024-02-14 18:03:18 +00:00
|
|
|
}
|
2024-02-09 22:18:48 +00:00
|
|
|
|
|
|
|
makeLenses ''State
|
|
|
|
|
|
|
|
drawUI :: State -> [Widget Name]
|
2024-02-27 15:44:17 +00:00
|
|
|
drawUI s = [splashDialog s, helpDialog s, inputDialog s, ui s]
|
2024-02-09 22:18:48 +00:00
|
|
|
where
|
|
|
|
ui :: State -> Widget Name
|
2024-02-27 14:41:43 +00:00
|
|
|
ui st =
|
2024-02-09 22:18:48 +00:00
|
|
|
joinBorders $
|
|
|
|
withBorderStyle unicode $
|
2024-02-28 21:12:57 +00:00
|
|
|
B.borderWithLabel
|
|
|
|
(str
|
|
|
|
("Zenith - " <>
|
|
|
|
st ^. network <>
|
|
|
|
" - " <>
|
|
|
|
T.unpack
|
|
|
|
(maybe
|
|
|
|
"(None)"
|
|
|
|
(\(_, w) -> zcashWalletName $ entityVal w)
|
|
|
|
(L.listSelectedElement (st ^. wallets))))) $
|
2024-02-27 14:41:43 +00:00
|
|
|
(C.center (listBox "Addresses" (st ^. addresses)) <+>
|
|
|
|
B.vBorder <+> C.center (listBox "Transactions" (st ^. transactions))) <=>
|
|
|
|
msgBox (st ^. msg)
|
2024-02-09 22:18:48 +00:00
|
|
|
listBox :: String -> L.List Name String -> Widget Name
|
|
|
|
listBox titleLabel l =
|
|
|
|
C.vCenter $
|
|
|
|
vBox
|
|
|
|
[ C.hCenter
|
|
|
|
(B.borderWithLabel (str titleLabel) $
|
|
|
|
hLimit 25 $ vLimit 15 $ L.renderList listDrawElement True l)
|
|
|
|
, str " "
|
|
|
|
, C.hCenter $ str "Select "
|
|
|
|
]
|
2024-02-11 16:33:22 +00:00
|
|
|
msgBox :: String -> Widget Name
|
|
|
|
msgBox m =
|
|
|
|
vBox
|
|
|
|
[B.hBorderWithLabel (str "Messages"), hLimit 70 $ padRight Max $ str m]
|
2024-02-13 20:19:05 +00:00
|
|
|
helpDialog :: State -> Widget Name
|
2024-02-27 14:41:43 +00:00
|
|
|
helpDialog st =
|
|
|
|
if st ^. helpBox
|
2024-02-13 20:19:05 +00:00
|
|
|
then D.renderDialog
|
2024-02-14 18:03:18 +00:00
|
|
|
(D.dialog (Just (str "Commands")) Nothing 55)
|
2024-02-13 20:19:05 +00:00
|
|
|
(vBox ([C.hCenter $ str "Key", B.hBorder] <> keyList) <+>
|
|
|
|
vBox ([str "Actions", B.hBorder] <> actionList))
|
|
|
|
else emptyWidget
|
|
|
|
where
|
2024-02-14 18:03:18 +00:00
|
|
|
keyList = map (C.hCenter . str) ["?", "Esc", "c", "q"]
|
|
|
|
actionList =
|
|
|
|
map
|
|
|
|
(hLimit 40 . str)
|
|
|
|
["Open help", "Close dialog", "Create Wallet", "Quit"]
|
2024-02-27 15:44:17 +00:00
|
|
|
inputDialog :: State -> Widget Name
|
|
|
|
inputDialog st =
|
|
|
|
case st ^. dialogBox of
|
|
|
|
WName ->
|
|
|
|
D.renderDialog
|
|
|
|
(D.dialog (Just (str "Create Wallet")) Nothing 50)
|
|
|
|
(renderForm $ st ^. inputForm)
|
|
|
|
AName ->
|
|
|
|
D.renderDialog
|
|
|
|
(D.dialog (Just (str "Create Account")) Nothing 50)
|
|
|
|
(renderForm $ st ^. inputForm)
|
|
|
|
Blank -> emptyWidget
|
2024-02-14 18:03:18 +00:00
|
|
|
splashDialog :: State -> Widget Name
|
2024-02-27 14:41:43 +00:00
|
|
|
splashDialog st =
|
|
|
|
if st ^. splashBox
|
2024-02-14 18:03:18 +00:00
|
|
|
then withBorderStyle unicodeBold $
|
|
|
|
D.renderDialog
|
|
|
|
(D.dialog Nothing Nothing 30)
|
|
|
|
(withAttr
|
|
|
|
titleAttr
|
|
|
|
(str
|
|
|
|
" _____ _ _ _ \n|__ /___ _ __ (_) |_| |__\n / // _ \\ '_ \\| | __| '_ \\\n / /| __/ | | | | |_| | | |\n/____\\___|_| |_|_|\\__|_| |_|") <=>
|
2024-02-28 21:12:57 +00:00
|
|
|
C.hCenter (withAttr titleAttr (str "Zcash Wallet v0.4.3.0")) <=>
|
2024-02-14 18:03:18 +00:00
|
|
|
C.hCenter (withAttr blinkAttr $ str "Press any key..."))
|
|
|
|
else emptyWidget
|
|
|
|
|
2024-02-27 15:44:17 +00:00
|
|
|
mkInputForm :: DialogInput -> Form DialogInput e Name
|
|
|
|
mkInputForm =
|
|
|
|
newForm
|
|
|
|
[label "Name: " @@= editTextField dialogInput DialogInputField (Just 1)]
|
2024-02-14 18:03:18 +00:00
|
|
|
where
|
|
|
|
label s w =
|
|
|
|
padBottom (Pad 1) $ vLimit 1 (hLimit 15 $ str s <+> fill ' ') <+> w
|
2024-02-09 22:18:48 +00:00
|
|
|
|
|
|
|
listDrawElement :: (Show a) => Bool -> a -> Widget Name
|
|
|
|
listDrawElement sel a =
|
|
|
|
let selStr s =
|
|
|
|
if sel
|
|
|
|
then withAttr customAttr (str $ "<" <> s <> ">")
|
|
|
|
else str s
|
|
|
|
in C.hCenter $ selStr $ show a
|
|
|
|
|
|
|
|
customAttr :: A.AttrName
|
|
|
|
customAttr = L.listSelectedAttr <> A.attrName "custom"
|
|
|
|
|
2024-02-14 18:03:18 +00:00
|
|
|
titleAttr :: A.AttrName
|
|
|
|
titleAttr = A.attrName "title"
|
|
|
|
|
|
|
|
blinkAttr :: A.AttrName
|
|
|
|
blinkAttr = A.attrName "blink"
|
|
|
|
|
2024-02-09 22:18:48 +00:00
|
|
|
appEvent :: BT.BrickEvent Name e -> BT.EventM Name State ()
|
2024-02-14 18:03:18 +00:00
|
|
|
appEvent (BT.VtyEvent e) = do
|
|
|
|
r <- F.focusGetCurrent <$> use focusRing
|
|
|
|
s <- BT.get
|
|
|
|
if s ^. splashBox
|
|
|
|
then BT.modify $ set splashBox False
|
|
|
|
else if s ^. helpBox
|
|
|
|
then do
|
|
|
|
case e of
|
|
|
|
V.EvKey V.KEsc [] -> do
|
|
|
|
BT.modify $ set helpBox False
|
2024-02-27 14:41:43 +00:00
|
|
|
_ev -> return ()
|
2024-02-14 18:03:18 +00:00
|
|
|
else do
|
2024-02-27 15:44:17 +00:00
|
|
|
case s ^. dialogBox of
|
|
|
|
WName -> do
|
|
|
|
case e of
|
|
|
|
V.EvKey V.KEsc [] -> BT.modify $ set dialogBox Blank
|
|
|
|
V.EvKey V.KEnter [] -> do
|
|
|
|
fs <- BT.zoom inputForm $ BT.gets formState
|
2024-02-28 21:12:57 +00:00
|
|
|
nw <- liftIO $ addNewWallet (fs ^. dialogInput) s
|
2024-02-28 22:37:43 +00:00
|
|
|
BT.put nw
|
2024-03-01 13:33:30 +00:00
|
|
|
aL <- use accounts
|
|
|
|
BT.modify $
|
|
|
|
set dialogBox $
|
|
|
|
if not (null $ L.listElements aL)
|
|
|
|
then Blank
|
|
|
|
else AName
|
2024-02-27 15:44:17 +00:00
|
|
|
ev -> BT.zoom inputForm $ handleFormEvent (BT.VtyEvent ev)
|
|
|
|
AName -> do
|
2024-02-14 18:03:18 +00:00
|
|
|
case e of
|
2024-02-27 15:44:17 +00:00
|
|
|
V.EvKey V.KEsc [] -> BT.modify $ set dialogBox Blank
|
2024-02-19 20:05:32 +00:00
|
|
|
V.EvKey V.KEnter [] -> do
|
2024-02-27 15:44:17 +00:00
|
|
|
BT.modify $ set dialogBox Blank
|
|
|
|
fs <- BT.zoom inputForm $ BT.gets formState
|
2024-02-19 20:05:32 +00:00
|
|
|
printMsg $
|
2024-03-01 13:33:30 +00:00
|
|
|
"Creating new account " <> T.unpack (fs ^. dialogInput)
|
2024-02-27 15:44:17 +00:00
|
|
|
ev -> BT.zoom inputForm $ handleFormEvent (BT.VtyEvent ev)
|
|
|
|
Blank -> do
|
2024-02-14 18:03:18 +00:00
|
|
|
case e of
|
2024-02-19 20:05:32 +00:00
|
|
|
V.EvKey (V.KChar '\t') [] -> focusRing %= F.focusNext
|
2024-02-14 18:03:18 +00:00
|
|
|
V.EvKey (V.KChar 'q') [] -> M.halt
|
|
|
|
V.EvKey (V.KChar '?') [] -> BT.modify $ set helpBox True
|
2024-02-27 17:17:36 +00:00
|
|
|
V.EvKey (V.KChar 'w') [] -> do
|
|
|
|
BT.modify $
|
|
|
|
set inputForm $
|
|
|
|
updateFormState (DialogInput "New Wallet") $
|
|
|
|
s ^. inputForm
|
|
|
|
BT.modify $ set dialogBox WName
|
|
|
|
V.EvKey (V.KChar 'a') [] -> do
|
|
|
|
BT.modify $
|
|
|
|
set inputForm $
|
|
|
|
updateFormState (DialogInput "New Account") $
|
|
|
|
s ^. inputForm
|
|
|
|
BT.modify $ set dialogBox AName
|
2024-02-14 18:03:18 +00:00
|
|
|
ev ->
|
|
|
|
case r of
|
|
|
|
Just AList -> BT.zoom addresses $ L.handleListEvent ev
|
|
|
|
Just TList -> BT.zoom transactions $ L.handleListEvent ev
|
2024-02-27 14:41:43 +00:00
|
|
|
_anyName -> return ()
|
2024-02-11 16:33:22 +00:00
|
|
|
where
|
|
|
|
printMsg :: String -> BT.EventM Name State ()
|
|
|
|
printMsg s = BT.modify $ updateMsg s
|
|
|
|
updateMsg :: String -> State -> State
|
|
|
|
updateMsg = set msg
|
2024-02-27 14:41:43 +00:00
|
|
|
appEvent _ = return ()
|
2024-02-09 22:18:48 +00:00
|
|
|
|
|
|
|
theMap :: A.AttrMap
|
|
|
|
theMap =
|
|
|
|
A.attrMap
|
|
|
|
V.defAttr
|
|
|
|
[ (L.listAttr, V.white `on` V.blue)
|
|
|
|
, (L.listSelectedAttr, V.blue `on` V.white)
|
2024-02-14 18:03:18 +00:00
|
|
|
, (customAttr, fg V.black)
|
|
|
|
, (titleAttr, V.withStyle (fg V.brightGreen) V.bold)
|
|
|
|
, (blinkAttr, style V.blink)
|
2024-02-19 20:05:32 +00:00
|
|
|
, (focusedFormInputAttr, V.white `on` V.blue)
|
2024-02-09 22:18:48 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
theApp :: M.App State e Name
|
|
|
|
theApp =
|
|
|
|
M.App
|
|
|
|
{ M.appDraw = drawUI
|
|
|
|
, M.appChooseCursor = M.showFirstCursor
|
|
|
|
, M.appHandleEvent = appEvent
|
|
|
|
, M.appStartEvent = return ()
|
|
|
|
, M.appAttrMap = const theMap
|
|
|
|
}
|
|
|
|
|
2024-02-14 18:03:18 +00:00
|
|
|
runZenithCLI :: T.Text -> Int -> T.Text -> IO ()
|
2024-02-27 14:41:43 +00:00
|
|
|
runZenithCLI host port dbFilePath = do
|
2024-02-14 18:03:18 +00:00
|
|
|
w <- checkZebra host port
|
2024-02-12 21:09:36 +00:00
|
|
|
case (w :: Maybe ZebraGetInfo) of
|
|
|
|
Just zebra -> do
|
2024-02-14 18:03:18 +00:00
|
|
|
bc <- checkBlockChain host port
|
2024-02-12 21:09:36 +00:00
|
|
|
case (bc :: Maybe ZebraGetBlockChainInfo) of
|
|
|
|
Nothing -> print "Unable to determine blockchain status"
|
|
|
|
Just chainInfo -> do
|
2024-02-28 21:12:57 +00:00
|
|
|
initDb dbFilePath
|
2024-02-27 14:41:43 +00:00
|
|
|
walList <- getWallets dbFilePath $ zgb_net chainInfo
|
2024-02-29 21:02:58 +00:00
|
|
|
accList <-
|
|
|
|
if not (null walList)
|
|
|
|
then getAccounts dbFilePath $ entityKey $ head walList
|
|
|
|
else return []
|
2024-02-12 21:09:36 +00:00
|
|
|
void $
|
|
|
|
M.defaultMain theApp $
|
|
|
|
State
|
|
|
|
((show . zgb_net) chainInfo)
|
2024-02-19 20:05:32 +00:00
|
|
|
(L.list WList (Vec.fromList walList) 1)
|
2024-02-29 21:02:58 +00:00
|
|
|
(L.list AcList (Vec.fromList accList) 0)
|
2024-02-12 21:09:36 +00:00
|
|
|
(L.list AList (Vec.fromList ["addr1", "addr2"]) 1)
|
|
|
|
(L.list TList (Vec.fromList ["tx1", "tx2", "tx3"]) 1)
|
|
|
|
("Start up Ok! Connected to Zebra " ++
|
|
|
|
(T.unpack . zgi_build) zebra ++ " on port " ++ show port ++ ".")
|
2024-02-13 20:19:05 +00:00
|
|
|
False
|
2024-02-27 15:44:17 +00:00
|
|
|
(if null walList
|
|
|
|
then WName
|
|
|
|
else Blank)
|
2024-02-14 18:03:18 +00:00
|
|
|
True
|
2024-02-27 15:44:17 +00:00
|
|
|
(mkInputForm $ DialogInput "Main")
|
2024-02-14 18:03:18 +00:00
|
|
|
(F.focusRing [AList, TList])
|
2024-02-28 21:12:57 +00:00
|
|
|
(zgb_blocks chainInfo)
|
|
|
|
dbFilePath
|
2024-02-12 21:09:36 +00:00
|
|
|
Nothing -> do
|
|
|
|
print $
|
|
|
|
"No Zebra node available on port " <>
|
|
|
|
show port <> ". Check your configuration"
|
2024-02-28 21:12:57 +00:00
|
|
|
|
2024-02-28 22:37:43 +00:00
|
|
|
addNewWallet :: T.Text -> State -> IO State
|
2024-02-28 21:12:57 +00:00
|
|
|
addNewWallet n s = do
|
|
|
|
sP <- generateWalletSeedPhrase
|
|
|
|
let bH = s ^. startBlock
|
|
|
|
let netName = read $ s ^. network
|
2024-02-28 22:37:43 +00:00
|
|
|
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
|
2024-03-01 13:33:30 +00:00
|
|
|
let aL =
|
|
|
|
L.listFindBy (\x -> zcashWalletName (entityVal x) == n) $
|
|
|
|
L.listReplace (Vec.fromList wL) (Just 0) (s ^. wallets)
|
|
|
|
return $ (s & wallets .~ aL) & msg .~ "Created new wallet: " ++ T.unpack n
|
|
|
|
|
|
|
|
addNewAccount :: T.Text -> State -> IO State
|
|
|
|
addNewAccount n s = undefined
|