2024-02-09 22:18:48 +00:00
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
2024-03-22 20:39:37 +00:00
|
|
|
{-# LANGUAGE TypeApplications #-}
|
2024-02-12 21:09:36 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2024-03-22 20:39:37 +00:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
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 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-03-17 19:38:26 +00:00
|
|
|
, hBox
|
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-09 22:18:48 +00:00
|
|
|
, str
|
2024-03-07 20:20:06 +00:00
|
|
|
, strWrap
|
2024-02-28 21:12:57 +00:00
|
|
|
, txt
|
2024-03-17 19:38:26 +00:00
|
|
|
, txtWrap
|
2024-03-17 12:17:52 +00:00
|
|
|
, txtWrapWith
|
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
|
2024-03-22 20:39:37 +00:00
|
|
|
import Control.Exception (catch, throw, throwIO, try)
|
2024-03-17 12:17:52 +00:00
|
|
|
import Control.Monad (void)
|
|
|
|
import Control.Monad.IO.Class (liftIO)
|
|
|
|
import Data.Maybe
|
|
|
|
import qualified Data.Text as T
|
2024-03-17 19:38:26 +00:00
|
|
|
import qualified Data.Text.Encoding as E
|
2024-04-21 12:07:51 +00:00
|
|
|
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
|
2024-02-09 22:18:48 +00:00
|
|
|
import qualified Data.Vector as Vec
|
2024-02-19 20:05:32 +00:00
|
|
|
import Database.Persist
|
2024-03-17 12:17:52 +00:00
|
|
|
import qualified Graphics.Vty as V
|
|
|
|
import Lens.Micro ((&), (.~), (^.), set)
|
|
|
|
import Lens.Micro.Mtl
|
|
|
|
import Lens.Micro.TH
|
|
|
|
import Text.Wrap (FillScope(..), FillStrategy(..), WrapSettings(..), wrapText)
|
2024-03-07 14:01:29 +00:00
|
|
|
import ZcashHaskell.Keys (generateWalletSeedPhrase, getWalletSeed)
|
2024-04-18 01:28:47 +00:00
|
|
|
import ZcashHaskell.Orchard (getSaplingFromUA, isValidUnifiedAddress)
|
|
|
|
import ZcashHaskell.Transparent (encodeTransparentReceiver)
|
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-04-18 01:28:47 +00:00
|
|
|
import Zenith.Types
|
|
|
|
( Config(..)
|
|
|
|
, PhraseDB(..)
|
|
|
|
, UnifiedAddressDB(..)
|
2024-04-21 12:07:51 +00:00
|
|
|
, UserTx(..)
|
2024-04-18 01:28:47 +00:00
|
|
|
, ZcashNetDB(..)
|
|
|
|
)
|
2024-04-21 12:07:51 +00:00
|
|
|
import Zenith.Utils (displayTaz, displayZec, showAddress)
|
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
|
2024-03-05 18:34:30 +00:00
|
|
|
| AdName
|
2024-03-07 20:20:06 +00:00
|
|
|
| WSelect
|
|
|
|
| ASelect
|
2024-02-27 15:44:17 +00:00
|
|
|
| Blank
|
2024-02-14 18:03:18 +00:00
|
|
|
|
2024-03-07 20:20:06 +00:00
|
|
|
data DisplayType
|
|
|
|
= AddrDisplay
|
|
|
|
| MsgDisplay
|
2024-03-17 19:38:26 +00:00
|
|
|
| PhraseDisplay
|
2024-04-21 12:07:51 +00:00
|
|
|
| TxDisplay
|
2024-03-07 20:20:06 +00:00
|
|
|
| BlankDisplay
|
|
|
|
|
2024-02-09 22:18:48 +00:00
|
|
|
data State = State
|
2024-03-07 14:01:29 +00:00
|
|
|
{ _network :: !ZcashNet
|
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-03-05 18:34:30 +00:00
|
|
|
, _addresses :: !(L.List Name (Entity WalletAddress))
|
2024-04-21 12:07:51 +00:00
|
|
|
, _transactions :: !(L.List Name UserTx)
|
2024-02-11 16:33:22 +00:00
|
|
|
, _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-03-07 20:20:06 +00:00
|
|
|
, _displayBox :: !DisplayType
|
2024-04-21 12:07:51 +00:00
|
|
|
, _syncBlock :: !Int
|
2024-02-14 18:03:18 +00:00
|
|
|
}
|
2024-02-09 22:18:48 +00:00
|
|
|
|
|
|
|
makeLenses ''State
|
|
|
|
|
|
|
|
drawUI :: State -> [Widget Name]
|
2024-03-07 20:20:06 +00:00
|
|
|
drawUI s = [splashDialog s, helpDialog s, displayDialog 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 - " <>
|
2024-03-07 14:01:29 +00:00
|
|
|
show (st ^. network) <>
|
2024-02-28 21:12:57 +00:00
|
|
|
" - " <>
|
|
|
|
T.unpack
|
|
|
|
(maybe
|
|
|
|
"(None)"
|
|
|
|
(\(_, w) -> zcashWalletName $ entityVal w)
|
2024-03-07 20:20:06 +00:00
|
|
|
(L.listSelectedElement (st ^. wallets)))))
|
|
|
|
(C.hCenter
|
|
|
|
(str
|
|
|
|
("Account: " ++
|
|
|
|
T.unpack
|
|
|
|
(maybe
|
|
|
|
"(None)"
|
|
|
|
(\(_, a) -> zcashAccountName $ entityVal a)
|
|
|
|
(L.listSelectedElement (st ^. accounts))))) <=>
|
|
|
|
listAddressBox "Addresses" (st ^. addresses) <+>
|
2024-04-21 12:07:51 +00:00
|
|
|
B.vBorder <+>
|
|
|
|
(C.hCenter (str ("Last block seen: " ++ show (st ^. syncBlock))) <=>
|
|
|
|
listTxBox "Transactions" (st ^. transactions))) <=>
|
2024-03-17 19:38:26 +00:00
|
|
|
C.hCenter
|
|
|
|
(hBox
|
|
|
|
[ capCommand "W" "allets"
|
|
|
|
, capCommand "A" "ccounts"
|
|
|
|
, capCommand "V" "iew address"
|
|
|
|
, capCommand "Q" "uit"
|
|
|
|
])
|
2024-03-05 18:34:30 +00:00
|
|
|
listBox :: Show e => String -> L.List Name e -> Widget Name
|
2024-02-09 22:18:48 +00:00
|
|
|
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-03-07 20:20:06 +00:00
|
|
|
selectListBox ::
|
|
|
|
Show e
|
|
|
|
=> String
|
|
|
|
-> L.List Name e
|
|
|
|
-> (Bool -> e -> Widget Name)
|
|
|
|
-> Widget Name
|
|
|
|
selectListBox titleLabel l drawF =
|
|
|
|
vBox
|
|
|
|
[ C.hCenter
|
|
|
|
(B.borderWithLabel (str titleLabel) $
|
|
|
|
hLimit 25 $ vLimit 15 $ L.renderList drawF True l)
|
|
|
|
, str " "
|
|
|
|
]
|
2024-03-05 18:34:30 +00:00
|
|
|
listAddressBox ::
|
|
|
|
String -> L.List Name (Entity WalletAddress) -> Widget Name
|
|
|
|
listAddressBox titleLabel a =
|
|
|
|
C.vCenter $
|
|
|
|
vBox
|
|
|
|
[ C.hCenter
|
|
|
|
(B.borderWithLabel (str titleLabel) $
|
|
|
|
hLimit 40 $ vLimit 15 $ L.renderList listDrawAddress True a)
|
|
|
|
, str " "
|
|
|
|
, C.hCenter $ str "Use arrows to select"
|
|
|
|
]
|
2024-04-21 12:07:51 +00:00
|
|
|
listTxBox :: String -> L.List Name UserTx -> Widget Name
|
|
|
|
listTxBox titleLabel tx =
|
|
|
|
C.vCenter $
|
|
|
|
vBox
|
|
|
|
[ C.hCenter
|
|
|
|
(B.borderWithLabel (str titleLabel) $
|
|
|
|
hLimit 40 $ vLimit 15 $ L.renderList listDrawTx True tx)
|
|
|
|
, str " "
|
|
|
|
, C.hCenter $ str "Use arrows to select"
|
|
|
|
]
|
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-03-07 20:20:06 +00:00
|
|
|
keyList = map (C.hCenter . str) ["?", "Esc", "w", "a", "v", "q"]
|
2024-02-14 18:03:18 +00:00
|
|
|
actionList =
|
|
|
|
map
|
|
|
|
(hLimit 40 . str)
|
2024-03-07 20:20:06 +00:00
|
|
|
[ "Open help"
|
|
|
|
, "Close dialog"
|
|
|
|
, "Switch wallets"
|
|
|
|
, "Switch accounts"
|
|
|
|
, "View address"
|
|
|
|
, "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)
|
2024-03-05 18:34:30 +00:00
|
|
|
AdName ->
|
|
|
|
D.renderDialog
|
|
|
|
(D.dialog (Just (str "Create Address")) Nothing 50)
|
|
|
|
(renderForm $ st ^. inputForm)
|
2024-03-07 20:20:06 +00:00
|
|
|
WSelect ->
|
|
|
|
D.renderDialog
|
|
|
|
(D.dialog (Just (str "Select Wallet")) Nothing 50)
|
2024-03-17 19:38:26 +00:00
|
|
|
(selectListBox "Wallets" (st ^. wallets) listDrawWallet <=>
|
|
|
|
C.hCenter
|
|
|
|
(hBox
|
|
|
|
[ capCommand "↑↓ " "move"
|
|
|
|
, capCommand "↲ " "select"
|
|
|
|
, capCommand "N" "ew"
|
|
|
|
, capCommand "S" "how phrase"
|
|
|
|
, xCommand
|
|
|
|
]))
|
2024-03-07 20:20:06 +00:00
|
|
|
ASelect ->
|
|
|
|
D.renderDialog
|
|
|
|
(D.dialog (Just (str "Select Account")) Nothing 50)
|
2024-03-17 19:38:26 +00:00
|
|
|
(selectListBox "Accounts" (st ^. accounts) listDrawAccount <=>
|
|
|
|
C.hCenter
|
|
|
|
(hBox
|
|
|
|
[ capCommand "↑↓ " "move"
|
|
|
|
, capCommand "↲ " "select"
|
|
|
|
, capCommand "N" "ew"
|
|
|
|
, xCommand
|
|
|
|
]))
|
2024-02-27 15:44:17 +00:00
|
|
|
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-04-18 01:28:47 +00:00
|
|
|
C.hCenter (withAttr titleAttr (str "Zcash Wallet v0.4.5.0")) <=>
|
2024-02-14 18:03:18 +00:00
|
|
|
C.hCenter (withAttr blinkAttr $ str "Press any key..."))
|
|
|
|
else emptyWidget
|
2024-03-17 19:38:26 +00:00
|
|
|
capCommand :: String -> String -> Widget Name
|
|
|
|
capCommand k comm = hBox [withAttr titleAttr (str k), str comm, str " | "]
|
|
|
|
xCommand :: Widget Name
|
|
|
|
xCommand = hBox [str "E", withAttr titleAttr (str "x"), str "it"]
|
2024-03-07 20:20:06 +00:00
|
|
|
displayDialog :: State -> Widget Name
|
|
|
|
displayDialog st =
|
|
|
|
case st ^. displayBox of
|
|
|
|
AddrDisplay ->
|
|
|
|
case L.listSelectedElement $ st ^. addresses of
|
|
|
|
Just (_, a) ->
|
|
|
|
withBorderStyle unicodeBold $
|
|
|
|
D.renderDialog
|
|
|
|
(D.dialog
|
|
|
|
(Just $ txt ("Address: " <> walletAddressName (entityVal a)))
|
|
|
|
Nothing
|
|
|
|
60)
|
|
|
|
(padAll 1 $
|
2024-03-22 20:39:37 +00:00
|
|
|
B.borderWithLabel
|
|
|
|
(str "Unified")
|
|
|
|
(txtWrapWith (WrapSettings False True NoFill FillAfterFirst) $
|
|
|
|
getUA $ walletAddressUAddress $ entityVal a) <=>
|
|
|
|
B.borderWithLabel
|
|
|
|
(str "Legacy Shielded")
|
2024-04-18 01:28:47 +00:00
|
|
|
(txtWrapWith (WrapSettings False True NoFill FillAfterFirst) $
|
|
|
|
fromMaybe "None" $
|
|
|
|
(getSaplingFromUA .
|
|
|
|
E.encodeUtf8 . getUA . walletAddressUAddress)
|
|
|
|
(entityVal a)) <=>
|
2024-03-22 20:39:37 +00:00
|
|
|
B.borderWithLabel
|
|
|
|
(str "Transparent")
|
|
|
|
(txtWrapWith (WrapSettings False True NoFill FillAfterFirst) $
|
2024-04-18 01:28:47 +00:00
|
|
|
maybe "None" (encodeTransparentReceiver (st ^. network)) $
|
2024-03-22 20:39:37 +00:00
|
|
|
t_rec =<<
|
|
|
|
(isValidUnifiedAddress .
|
|
|
|
E.encodeUtf8 . getUA . walletAddressUAddress)
|
|
|
|
(entityVal a)))
|
2024-03-07 20:20:06 +00:00
|
|
|
Nothing -> emptyWidget
|
2024-03-17 19:38:26 +00:00
|
|
|
PhraseDisplay ->
|
|
|
|
case L.listSelectedElement $ st ^. wallets of
|
|
|
|
Just (_, w) ->
|
|
|
|
withBorderStyle unicodeBold $
|
|
|
|
D.renderDialog
|
|
|
|
(D.dialog (Just $ txt "Seed Phrase") Nothing 50)
|
|
|
|
(padAll 1 $
|
|
|
|
txtWrap $
|
|
|
|
E.decodeUtf8Lenient $
|
|
|
|
getBytes $ getPhrase $ zcashWalletSeedPhrase $ entityVal w)
|
|
|
|
Nothing -> emptyWidget
|
2024-03-07 20:20:06 +00:00
|
|
|
MsgDisplay ->
|
|
|
|
withBorderStyle unicodeBold $
|
|
|
|
D.renderDialog
|
|
|
|
(D.dialog (Just $ txt "Message") Nothing 50)
|
|
|
|
(padAll 1 $ strWrap $ st ^. msg)
|
2024-04-21 12:07:51 +00:00
|
|
|
TxDisplay ->
|
|
|
|
case L.listSelectedElement $ st ^. transactions of
|
|
|
|
Nothing -> emptyWidget
|
|
|
|
Just (_, tx) ->
|
|
|
|
withBorderStyle unicodeBold $
|
|
|
|
D.renderDialog
|
|
|
|
(D.dialog (Just $ txt "Transaction") Nothing 50)
|
|
|
|
(padAll
|
|
|
|
1
|
|
|
|
(str
|
|
|
|
("Date: " ++
|
|
|
|
show (posixSecondsToUTCTime (fromInteger (ut_time tx)))) <=>
|
|
|
|
str ("Tx ID: " ++ show (ut_txid tx)) <=>
|
|
|
|
str
|
|
|
|
("Amount: " ++
|
|
|
|
if st ^. network == MainNet
|
|
|
|
then displayZec (ut_value tx)
|
|
|
|
else displayTaz (ut_value tx)) <=>
|
|
|
|
txt ("Memo: " <> ut_memo tx)))
|
2024-03-07 20:20:06 +00:00
|
|
|
BlankDisplay -> emptyWidget
|
2024-02-14 18:03:18 +00:00
|
|
|
|
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
|
|
|
|
|
2024-03-05 18:34:30 +00:00
|
|
|
listDrawWallet :: Bool -> Entity ZcashWallet -> Widget Name
|
|
|
|
listDrawWallet sel w =
|
|
|
|
let selStr s =
|
|
|
|
if sel
|
|
|
|
then withAttr customAttr (txt $ "<" <> s <> ">")
|
|
|
|
else txt s
|
|
|
|
in C.hCenter $ selStr $ zcashWalletName (entityVal w)
|
|
|
|
|
|
|
|
listDrawAccount :: Bool -> Entity ZcashAccount -> Widget Name
|
|
|
|
listDrawAccount sel w =
|
|
|
|
let selStr s =
|
|
|
|
if sel
|
|
|
|
then withAttr customAttr (txt $ "<" <> s <> ">")
|
|
|
|
else txt s
|
|
|
|
in C.hCenter $ selStr $ zcashAccountName (entityVal w)
|
|
|
|
|
|
|
|
listDrawAddress :: Bool -> Entity WalletAddress -> Widget Name
|
|
|
|
listDrawAddress sel w =
|
|
|
|
let selStr s =
|
|
|
|
if sel
|
|
|
|
then withAttr customAttr (txt $ "<" <> s <> ">")
|
|
|
|
else txt s
|
|
|
|
in C.hCenter $
|
|
|
|
selStr $
|
|
|
|
walletAddressName (entityVal w) <>
|
|
|
|
": " <> showAddress (walletAddressUAddress (entityVal w))
|
|
|
|
|
2024-04-21 12:07:51 +00:00
|
|
|
listDrawTx :: Bool -> UserTx -> Widget Name
|
|
|
|
listDrawTx sel tx =
|
|
|
|
selStr $
|
|
|
|
T.pack (show $ posixSecondsToUTCTime (fromInteger (ut_time tx))) <>
|
|
|
|
" " <> fmtAmt
|
|
|
|
where
|
|
|
|
amt = fromIntegral (ut_value tx) / 100000000
|
|
|
|
fmtAmt =
|
|
|
|
if amt > 0
|
|
|
|
then "↘" <> T.pack (show amt) <> " "
|
|
|
|
else " " <> T.pack (show amt) <> "↗"
|
|
|
|
selStr s =
|
|
|
|
if sel
|
|
|
|
then withAttr customAttr (txt $ "> " <> s)
|
|
|
|
else txt $ " " <> s
|
|
|
|
|
2024-02-09 22:18:48 +00:00
|
|
|
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-03-07 20:20:06 +00:00
|
|
|
case s ^. displayBox of
|
|
|
|
AddrDisplay -> BT.modify $ set displayBox BlankDisplay
|
|
|
|
MsgDisplay -> BT.modify $ set displayBox BlankDisplay
|
2024-03-17 19:38:26 +00:00
|
|
|
PhraseDisplay -> BT.modify $ set displayBox BlankDisplay
|
2024-04-21 12:07:51 +00:00
|
|
|
TxDisplay -> BT.modify $ set displayBox BlankDisplay
|
2024-03-07 20:20:06 +00:00
|
|
|
BlankDisplay -> do
|
|
|
|
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
|
|
|
|
nw <- liftIO $ addNewWallet (fs ^. dialogInput) s
|
|
|
|
ns <- liftIO $ refreshWallet nw
|
|
|
|
BT.put ns
|
|
|
|
aL <- use accounts
|
|
|
|
BT.modify $ set displayBox MsgDisplay
|
|
|
|
BT.modify $
|
|
|
|
set dialogBox $
|
|
|
|
if not (null $ L.listElements aL)
|
|
|
|
then Blank
|
|
|
|
else AName
|
|
|
|
ev ->
|
|
|
|
BT.zoom inputForm $ handleFormEvent (BT.VtyEvent ev)
|
|
|
|
AName -> 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-03-17 12:17:52 +00:00
|
|
|
ns <-
|
|
|
|
liftIO $
|
|
|
|
refreshAccount =<<
|
|
|
|
addNewAddress "Change" Internal =<<
|
|
|
|
addNewAccount (fs ^. dialogInput) s
|
2024-03-07 20:20:06 +00:00
|
|
|
BT.put ns
|
|
|
|
addrL <- use addresses
|
|
|
|
BT.modify $ set displayBox MsgDisplay
|
|
|
|
BT.modify $
|
|
|
|
set dialogBox $
|
|
|
|
if not (null $ L.listElements addrL)
|
|
|
|
then Blank
|
|
|
|
else AdName
|
|
|
|
ev ->
|
|
|
|
BT.zoom inputForm $ handleFormEvent (BT.VtyEvent ev)
|
|
|
|
AdName -> 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-03-17 12:17:52 +00:00
|
|
|
nAddr <-
|
|
|
|
liftIO $ addNewAddress (fs ^. dialogInput) External s
|
2024-03-07 20:20:06 +00:00
|
|
|
BT.put nAddr
|
|
|
|
BT.modify $ set displayBox MsgDisplay
|
|
|
|
BT.modify $ set dialogBox Blank
|
|
|
|
ev ->
|
|
|
|
BT.zoom inputForm $ handleFormEvent (BT.VtyEvent ev)
|
|
|
|
WSelect -> do
|
|
|
|
case e of
|
2024-03-17 19:38:26 +00:00
|
|
|
V.EvKey (V.KChar 'x') [] ->
|
|
|
|
BT.modify $ set dialogBox Blank
|
2024-03-07 20:20:06 +00:00
|
|
|
V.EvKey V.KEnter [] -> do
|
|
|
|
ns <- liftIO $ refreshWallet s
|
|
|
|
BT.put ns
|
|
|
|
BT.modify $ set dialogBox Blank
|
2024-03-17 19:38:26 +00:00
|
|
|
V.EvKey (V.KChar 'n') [] -> do
|
2024-03-07 20:20:06 +00:00
|
|
|
BT.modify $
|
|
|
|
set inputForm $
|
|
|
|
updateFormState (DialogInput "New Wallet") $
|
|
|
|
s ^. inputForm
|
|
|
|
BT.modify $ set dialogBox WName
|
2024-03-17 19:38:26 +00:00
|
|
|
V.EvKey (V.KChar 's') [] ->
|
|
|
|
BT.modify $ set displayBox PhraseDisplay
|
2024-03-07 20:20:06 +00:00
|
|
|
ev -> BT.zoom wallets $ L.handleListEvent ev
|
|
|
|
ASelect -> do
|
|
|
|
case e of
|
2024-03-17 19:38:26 +00:00
|
|
|
V.EvKey (V.KChar 'x') [] ->
|
|
|
|
BT.modify $ set dialogBox Blank
|
2024-03-07 20:20:06 +00:00
|
|
|
V.EvKey V.KEnter [] -> do
|
|
|
|
ns <- liftIO $ refreshAccount s
|
|
|
|
BT.put ns
|
|
|
|
BT.modify $ set dialogBox Blank
|
2024-03-17 19:38:26 +00:00
|
|
|
V.EvKey (V.KChar 'n') [] -> do
|
2024-03-07 20:20:06 +00:00
|
|
|
BT.modify $
|
|
|
|
set inputForm $
|
|
|
|
updateFormState (DialogInput "New Account") $
|
|
|
|
s ^. inputForm
|
|
|
|
BT.modify $ set dialogBox AName
|
|
|
|
ev -> BT.zoom accounts $ L.handleListEvent ev
|
|
|
|
Blank -> do
|
|
|
|
case e of
|
|
|
|
V.EvKey (V.KChar '\t') [] -> focusRing %= F.focusNext
|
2024-04-21 12:07:51 +00:00
|
|
|
V.EvKey V.KEnter [] -> do
|
|
|
|
ns <- liftIO $ refreshTxs s
|
|
|
|
BT.put ns
|
2024-03-07 20:20:06 +00:00
|
|
|
V.EvKey (V.KChar 'q') [] -> M.halt
|
|
|
|
V.EvKey (V.KChar '?') [] -> BT.modify $ set helpBox True
|
|
|
|
V.EvKey (V.KChar 'n') [] ->
|
|
|
|
BT.modify $ set dialogBox AdName
|
|
|
|
V.EvKey (V.KChar 'v') [] ->
|
|
|
|
BT.modify $ set displayBox AddrDisplay
|
|
|
|
V.EvKey (V.KChar 'w') [] ->
|
|
|
|
BT.modify $ set dialogBox WSelect
|
2024-04-21 12:07:51 +00:00
|
|
|
V.EvKey (V.KChar 't') [] ->
|
|
|
|
BT.modify $ set displayBox TxDisplay
|
2024-03-07 20:20:06 +00:00
|
|
|
V.EvKey (V.KChar 'a') [] ->
|
|
|
|
BT.modify $ set dialogBox ASelect
|
|
|
|
ev ->
|
|
|
|
case r of
|
|
|
|
Just AList ->
|
|
|
|
BT.zoom addresses $ L.handleListEvent ev
|
|
|
|
Just TList ->
|
|
|
|
BT.zoom transactions $ L.handleListEvent ev
|
|
|
|
_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-04-18 01:28:47 +00:00
|
|
|
runZenithCLI :: Config -> IO ()
|
|
|
|
runZenithCLI config = do
|
|
|
|
let host = c_zebraHost config
|
|
|
|
let port = c_zebraPort config
|
|
|
|
let dbFilePath = c_dbPath config
|
2024-03-22 20:39:37 +00:00
|
|
|
w <- try $ checkZebra host port :: IO (Either IOError ZebraGetInfo)
|
|
|
|
case w of
|
|
|
|
Right zebra -> do
|
|
|
|
bc <-
|
|
|
|
try $ checkBlockChain host port :: IO
|
|
|
|
(Either IOError ZebraGetBlockChainInfo)
|
|
|
|
case bc of
|
|
|
|
Left e1 -> throwIO e1
|
|
|
|
Right 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-03-05 18:34:30 +00:00
|
|
|
addrList <-
|
|
|
|
if not (null accList)
|
|
|
|
then getAddresses dbFilePath $ entityKey $ head accList
|
|
|
|
else return []
|
2024-04-21 12:07:51 +00:00
|
|
|
txList <-
|
|
|
|
if not (null addrList)
|
|
|
|
then getUserTx dbFilePath =<<
|
|
|
|
getWalletTransactions dbFilePath (entityVal $ head addrList)
|
|
|
|
else return []
|
|
|
|
block <- getMaxWalletBlock dbFilePath
|
2024-02-12 21:09:36 +00:00
|
|
|
void $
|
|
|
|
M.defaultMain theApp $
|
|
|
|
State
|
2024-03-07 14:01:29 +00:00
|
|
|
(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-03-05 18:34:30 +00:00
|
|
|
(L.list AList (Vec.fromList addrList) 1)
|
2024-04-21 12:07:51 +00:00
|
|
|
(L.list TList (Vec.fromList txList) 1)
|
2024-02-12 21:09:36 +00:00
|
|
|
("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-03-07 20:20:06 +00:00
|
|
|
MsgDisplay
|
2024-04-21 12:07:51 +00:00
|
|
|
block
|
2024-03-22 20:39:37 +00:00
|
|
|
Left e -> do
|
2024-02-12 21:09:36 +00:00
|
|
|
print $
|
|
|
|
"No Zebra node available on port " <>
|
2024-03-22 20:39:37 +00:00
|
|
|
show port <> ". Check your configuration."
|
2024-02-28 21:12:57 +00:00
|
|
|
|
2024-03-07 20:20:06 +00:00
|
|
|
refreshWallet :: State -> IO State
|
|
|
|
refreshWallet s = do
|
|
|
|
selWallet <-
|
|
|
|
do case L.listSelectedElement $ s ^. wallets of
|
|
|
|
Nothing -> do
|
|
|
|
let fWall =
|
|
|
|
L.listSelectedElement $ L.listMoveToBeginning $ s ^. wallets
|
|
|
|
case fWall of
|
|
|
|
Nothing -> throw $ userError "Failed to select wallet"
|
|
|
|
Just (_j, w1) -> return w1
|
|
|
|
Just (_k, w) -> return w
|
|
|
|
aL <- getAccounts (s ^. dbPath) $ entityKey selWallet
|
|
|
|
addrL <-
|
|
|
|
if not (null aL)
|
|
|
|
then getAddresses (s ^. dbPath) $ entityKey $ head aL
|
|
|
|
else return []
|
2024-04-21 12:07:51 +00:00
|
|
|
txL <-
|
|
|
|
if not (null addrL)
|
|
|
|
then getUserTx (s ^. dbPath) =<<
|
|
|
|
getWalletTransactions (s ^. dbPath) (entityVal $ head addrL)
|
|
|
|
else return []
|
2024-03-07 20:20:06 +00:00
|
|
|
let aL' = L.listReplace (Vec.fromList aL) (Just 0) (s ^. accounts)
|
|
|
|
let addrL' = L.listReplace (Vec.fromList addrL) (Just 0) (s ^. addresses)
|
2024-04-21 12:07:51 +00:00
|
|
|
let txL' = L.listReplace (Vec.fromList txL) (Just 0) (s ^. transactions)
|
2024-03-07 20:20:06 +00:00
|
|
|
return $
|
2024-04-21 12:07:51 +00:00
|
|
|
(s & accounts .~ aL') & addresses .~ addrL' & transactions .~ txL' & msg .~
|
|
|
|
"Switched to wallet: " ++
|
2024-03-07 20:20:06 +00:00
|
|
|
T.unpack (zcashWalletName $ entityVal selWallet)
|
|
|
|
|
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
|
2024-03-07 14:01:29 +00:00
|
|
|
let netName = s ^. network
|
2024-03-17 12:17:52 +00:00
|
|
|
r <-
|
|
|
|
saveWallet (s ^. dbPath) $
|
|
|
|
ZcashWallet n (ZcashNetDB netName) (PhraseDB sP) bH
|
2024-02-28 22:37:43 +00:00
|
|
|
case r of
|
|
|
|
Nothing -> do
|
2024-03-01 20:57:13 +00:00
|
|
|
return $ s & msg .~ ("Wallet already exists: " ++ T.unpack n)
|
2024-02-28 22:37:43 +00:00
|
|
|
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
|
2024-03-01 20:57:13 +00:00
|
|
|
addNewAccount n s = do
|
|
|
|
selWallet <-
|
|
|
|
do case L.listSelectedElement $ s ^. wallets of
|
|
|
|
Nothing -> do
|
|
|
|
let fWall =
|
|
|
|
L.listSelectedElement $ L.listMoveToBeginning $ s ^. wallets
|
|
|
|
case fWall of
|
|
|
|
Nothing -> throw $ userError "Failed to select wallet"
|
|
|
|
Just (_j, w1) -> return w1
|
|
|
|
Just (_k, w) -> return w
|
|
|
|
aL' <- getMaxAccount (s ^. dbPath) (entityKey selWallet)
|
2024-03-07 14:01:29 +00:00
|
|
|
zA <-
|
|
|
|
try $ createZcashAccount n (aL' + 1) selWallet :: IO
|
|
|
|
(Either IOError ZcashAccount)
|
|
|
|
case zA of
|
|
|
|
Left e -> return $ s & msg .~ ("Error: " ++ show e)
|
|
|
|
Right zA' -> do
|
|
|
|
r <- saveAccount (s ^. dbPath) zA'
|
|
|
|
case r of
|
|
|
|
Nothing ->
|
|
|
|
return $ s & msg .~ ("Account already exists: " ++ T.unpack n)
|
|
|
|
Just x -> do
|
|
|
|
aL <- getAccounts (s ^. dbPath) (entityKey selWallet)
|
|
|
|
let nL =
|
|
|
|
L.listMoveToElement x $
|
|
|
|
L.listReplace (Vec.fromList aL) (Just 0) (s ^. accounts)
|
|
|
|
return $
|
|
|
|
(s & accounts .~ nL) & msg .~ "Created new account: " ++ T.unpack n
|
2024-03-05 18:34:30 +00:00
|
|
|
|
2024-03-07 20:20:06 +00:00
|
|
|
refreshAccount :: State -> IO State
|
|
|
|
refreshAccount s = do
|
|
|
|
selAccount <-
|
|
|
|
do case L.listSelectedElement $ s ^. accounts of
|
|
|
|
Nothing -> do
|
|
|
|
let fAcc =
|
|
|
|
L.listSelectedElement $ L.listMoveToBeginning $ s ^. accounts
|
|
|
|
case fAcc of
|
|
|
|
Nothing -> throw $ userError "Failed to select account"
|
|
|
|
Just (_j, w1) -> return w1
|
|
|
|
Just (_k, w) -> return w
|
|
|
|
aL <- getAddresses (s ^. dbPath) $ entityKey selAccount
|
|
|
|
let aL' = L.listReplace (Vec.fromList aL) (Just 0) (s ^. addresses)
|
2024-04-21 12:07:51 +00:00
|
|
|
selAddress <-
|
|
|
|
do case L.listSelectedElement aL' of
|
|
|
|
Nothing -> do
|
|
|
|
let fAdd = L.listSelectedElement $ L.listMoveToBeginning aL'
|
|
|
|
case fAdd of
|
|
|
|
Nothing -> throw $ userError "Failed to select address"
|
|
|
|
Just (_x, a1) -> return a1
|
|
|
|
Just (_y, a2) -> return a2
|
|
|
|
tList <-
|
|
|
|
getUserTx (s ^. dbPath) =<<
|
|
|
|
getWalletTransactions (s ^. dbPath) (entityVal selAddress)
|
|
|
|
let tL' = L.listReplace (Vec.fromList tList) (Just 0) (s ^. transactions)
|
2024-03-07 20:20:06 +00:00
|
|
|
return $
|
2024-04-21 12:07:51 +00:00
|
|
|
s & addresses .~ aL' & transactions .~ tL' & msg .~ "Switched to account: " ++
|
2024-03-07 20:20:06 +00:00
|
|
|
T.unpack (zcashAccountName $ entityVal selAccount)
|
|
|
|
|
2024-04-21 12:07:51 +00:00
|
|
|
refreshTxs :: State -> IO State
|
|
|
|
refreshTxs s = do
|
|
|
|
selAddress <-
|
|
|
|
do case L.listSelectedElement $ s ^. addresses of
|
|
|
|
Nothing -> do
|
|
|
|
let fAdd =
|
|
|
|
L.listSelectedElement $ L.listMoveToBeginning $ s ^. addresses
|
|
|
|
case fAdd of
|
|
|
|
Nothing -> throw $ userError "Failed to select address"
|
|
|
|
Just (_x, a1) -> return a1
|
|
|
|
Just (_y, a2) -> return a2
|
|
|
|
tList <-
|
|
|
|
getUserTx (s ^. dbPath) =<<
|
|
|
|
getWalletTransactions (s ^. dbPath) (entityVal selAddress)
|
|
|
|
let tL' = L.listReplace (Vec.fromList tList) (Just 0) (s ^. transactions)
|
|
|
|
return $ s & transactions .~ tL'
|
|
|
|
|
2024-03-17 12:17:52 +00:00
|
|
|
addNewAddress :: T.Text -> Scope -> State -> IO State
|
|
|
|
addNewAddress n scope s = do
|
2024-03-05 18:34:30 +00:00
|
|
|
selAccount <-
|
|
|
|
do case L.listSelectedElement $ s ^. accounts of
|
|
|
|
Nothing -> do
|
|
|
|
let fAcc =
|
|
|
|
L.listSelectedElement $ L.listMoveToBeginning $ s ^. accounts
|
|
|
|
case fAcc of
|
|
|
|
Nothing -> throw $ userError "Failed to select account"
|
|
|
|
Just (_j, a1) -> return a1
|
|
|
|
Just (_k, a) -> return a
|
2024-03-17 12:17:52 +00:00
|
|
|
maxAddr <- getMaxAddress (s ^. dbPath) (entityKey selAccount) scope
|
2024-03-07 20:20:06 +00:00
|
|
|
uA <-
|
2024-03-17 12:17:52 +00:00
|
|
|
try $ createWalletAddress n (maxAddr + 1) (s ^. network) scope selAccount :: IO
|
2024-03-07 20:20:06 +00:00
|
|
|
(Either IOError WalletAddress)
|
|
|
|
case uA of
|
|
|
|
Left e -> return $ s & msg .~ ("Error: " ++ show e)
|
|
|
|
Right uA' -> do
|
|
|
|
nAddr <- saveAddress (s ^. dbPath) uA'
|
|
|
|
case nAddr of
|
|
|
|
Nothing ->
|
|
|
|
return $ s & msg .~ ("Address already exists: " ++ T.unpack n)
|
|
|
|
Just x -> do
|
|
|
|
addrL <- getAddresses (s ^. dbPath) (entityKey selAccount)
|
|
|
|
let nL =
|
|
|
|
L.listMoveToElement x $
|
|
|
|
L.listReplace (Vec.fromList addrL) (Just 0) (s ^. addresses)
|
|
|
|
return $
|
|
|
|
(s & addresses .~ nL) & msg .~ "Created new address: " ++
|
|
|
|
T.unpack n ++
|
|
|
|
"(" ++
|
|
|
|
T.unpack (showAddress $ walletAddressUAddress $ entityVal x) ++ ")"
|