Milestone 3: RPC server, ZIP-320 #104

Merged
pitmutt merged 152 commits from milestone3 into master 2024-11-21 15:39:19 +00:00
2 changed files with 68 additions and 19 deletions
Showing only changes of commit 67d334a60b - Show all commits

View file

@ -59,6 +59,7 @@ import Zenith.Utils
, showAddress , showAddress
, validBarValue , validBarValue
, validateAddressBool , validateAddressBool
, isValidString
) )
data AppEvent data AppEvent
@ -104,6 +105,7 @@ data AppEvent
| CheckRecipient !T.Text | CheckRecipient !T.Text
| CheckAmount !Float | CheckAmount !Float
| ShowTxId !T.Text | ShowTxId !T.Text
| LoadAbList ![Entity AddressBook]
| ShowAdrBook | ShowAdrBook
| CloseAdrBook | CloseAdrBook
| NewAdrBkEntry | NewAdrBkEntry
@ -111,6 +113,7 @@ data AppEvent
| NotImplemented | NotImplemented
| CloseMsgAB | CloseMsgAB
| CheckValidAddress !T.Text | CheckValidAddress !T.Text
| CheckValidDescrip !T.Text
deriving (Eq, Show) deriving (Eq, Show)
data AppModel = AppModel data AppModel = AppModel
@ -155,9 +158,11 @@ data AppModel = AppModel
, _home :: !FilePath , _home :: !FilePath
, _showAdrBook :: !Bool , _showAdrBook :: !Bool
, _newAdrBkEntry :: !Bool , _newAdrBkEntry :: !Bool
, _abdescrip :: !(Maybe T.Text) , _abdescrip :: !T.Text
, _abaddress :: !T.Text , _abaddress :: !T.Text
, _abAddressValid :: !Bool , _abAddressValid :: !Bool
, _abDescripValid :: !Bool
, _abaddressList :: ![Entity AddressBook]
, _msgAB :: !(Maybe T.Text) , _msgAB :: !(Maybe T.Text)
} deriving (Eq, Show) } deriving (Eq, Show)
@ -778,6 +783,13 @@ buildUI wenv model = widgetTree
(label "Address Book" `styleBasic` (label "Address Book" `styleBasic`
[textFont "Bold", textSize 12, textColor white]) `styleBasic` [textFont "Bold", textSize 12, textColor white]) `styleBasic`
[bgColor btnColor, radius 2, padding 3] [bgColor btnColor, radius 2, padding 3]
, boxShadow $
box_
[alignMiddle]
(vstack
[ vscroll (vstack (zipWith abookRow [0 ..] (model ^. abaddressList))) `nodeKey` "txScroll"
]) `styleBasic`
[radius 2, padding 3, bgColor white]
, spacer , spacer
, hstack [ , hstack [
button "New" NewAdrBkEntry button "New" NewAdrBkEntry
@ -785,9 +797,21 @@ buildUI wenv model = widgetTree
, button "Edit" notImplemented , button "Edit" notImplemented
, spacer , spacer
, button "Copy" notImplemented , button "Copy" notImplemented
] ]
] ]
abookRow :: Int -> Entity AddressBook -> WidgetNode AppModel AppEvent
abookRow idx ab =
box_
[onClick $ ShowTx idx, alignLeft]
(hstack
[
(label "Descr: ") `styleBasic` [textFont "Bold"]
, spacer
, label
(T.pack $
show (addressBookAbdescrip $ entityVal ab))
]) `styleBasic`
[padding 2, borderB 1 gray]
newAdrBkOverlay = newAdrBkOverlay =
alert CloseNewAdrBook $ alert CloseNewAdrBook $
vstack vstack
@ -800,11 +824,11 @@ buildUI wenv model = widgetTree
, hstack , hstack
[ label "Description: " `styleBasic` [width 80] [ label "Description: " `styleBasic` [width 80]
, spacer , spacer
, textField_ sendRecipient [onChange CheckRecipient] `styleBasic` , textField_ abdescrip [onChange CheckValidDescrip] `styleBasic`
[ width 320 [ width 320
-- , styleIf , styleIf
-- (not $ model ^. recipientValid) (not $ model ^. abDescripValid)
-- (textColor red) (textColor red)
] ]
] ]
, spacer , spacer
@ -821,7 +845,7 @@ buildUI wenv model = widgetTree
, spacer , spacer
, hstack [ , hstack [
button "Save" NotImplemented `nodeEnabled` button "Save" NotImplemented `nodeEnabled`
(model ^. abAddressValid) ((model ^. abAddressValid) && (model ^. abDescripValid))
] ]
] ]
msgAdrBookOverlay= msgAdrBookOverlay=
@ -1148,6 +1172,7 @@ handleEvent wenv node model evt =
] ]
ShowTxId tx -> [Model $ model & showId ?~ tx & modalMsg .~ Nothing] ShowTxId tx -> [Model $ model & showId ?~ tx & modalMsg .~ Nothing]
CheckValidAddress a -> [Model $ model & abAddressValid .~ isRecipientValid a] CheckValidAddress a -> [Model $ model & abAddressValid .~ isRecipientValid a]
CheckValidDescrip a -> [Model $ model & abDescripValid .~ isValidString a]
ShowAdrBook -> [Model $ model & showAdrBook .~ True & menuPopup .~ False] ShowAdrBook -> [Model $ model & showAdrBook .~ True & menuPopup .~ False]
CloseAdrBook -> [Model $ model & showAdrBook .~ False] CloseAdrBook -> [Model $ model & showAdrBook .~ False]
NewAdrBkEntry -> [Model $ model & newAdrBkEntry .~ True & menuPopup .~ False] NewAdrBkEntry -> [Model $ model & newAdrBkEntry .~ True & menuPopup .~ False]
@ -1391,6 +1416,7 @@ runZenithGUI config = do
if not (null accList) if not (null accList)
then getUnconfirmedBalance pool $ entityKey $ head accList then getUnconfirmedBalance pool $ entityKey $ head accList
else return 0 else return 0
abList <- getAdrBook pool (zgb_net chainInfo)
let model = let model =
AppModel AppModel
config config
@ -1438,9 +1464,11 @@ runZenithGUI config = do
"" ""
False False
False False
Nothing ""
"" ""
False False
False
abList
Nothing Nothing
-- hD -- hD
startApp model handleEvent buildUI (params hD) startApp model handleEvent buildUI (params hD)
@ -1491,9 +1519,11 @@ runZenithGUI config = do
"" ""
False False
False False
Nothing ""
"" ""
False False
False
[]
Nothing Nothing
-- hD -- hD
startApp model handleEvent buildUI (params hD) startApp model handleEvent buildUI (params hD)

View file

@ -9,6 +9,7 @@ import Data.Ord (clamp)
import Data.Scientific (Scientific(..), scientific) import Data.Scientific (Scientific(..), scientific)
import qualified Data.Text as T import qualified Data.Text as T
import qualified Data.Text.Encoding as E import qualified Data.Text.Encoding as E
import Data.Char (isAlphaNum, isSpace)
import System.Directory import System.Directory
import System.Process (createProcess_, shell) import System.Process (createProcess_, shell)
import Text.Regex.Posix import Text.Regex.Posix
@ -134,3 +135,21 @@ parseAddress a znet =
Just a3 -> Just a3 ->
Just $ UnifiedAddress znet Nothing Nothing (Just $ ta_receiver a3) Just $ UnifiedAddress znet Nothing Nothing (Just $ ta_receiver a3)
Nothing -> Nothing Nothing -> Nothing
isValidContent :: String -> Bool
isValidContent [] = False -- an empty string is invalid
isValidContent (x:xs)
| not (isAlphaNum x ) = False -- string must start with an alphanumeric character
| otherwise = allValidChars xs -- process the rest of the string
where
allValidChars :: String -> Bool
allValidChars [] = True -- if we got here, string is valid
allValidChars (y:ys)
| isAlphaNum y || isSpace y = allValidChars ys -- char is valid, continue
| otherwise = False -- found an invalid character, return false
isValidString :: T.Text -> Bool
isValidString c = do
let a = T.unpack c
isValidContent a