Skip to content

Commit

Permalink
Add postWallet deposit UI endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Sep 12, 2024
1 parent aef4c43 commit 70dfa74
Show file tree
Hide file tree
Showing 13 changed files with 294 additions and 68 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,5 @@ package-lock.json
package.json

# buildkite artifacts testing
artifacts
artifacts
deposit-*
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ data WalletPublicIdentity = WalletPublicIdentity
{ pubXpub :: XPub
, pubNextUser :: Word31
}
deriving Show

walletPublicIdentity :: WalletInstance -> IO WalletPublicIdentity
walletPublicIdentity w = do
Expand Down
12 changes: 12 additions & 0 deletions lib/exe/lib/Cardano/Wallet/Application.hs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ import Cardano.Wallet.DB.Layer
import Cardano.Wallet.DB.Sqlite.Migration.Old
( DefaultFieldValues (..)
)
import Cardano.Wallet.Deposit.IO
( WalletBootEnv (..)
)
import Cardano.Wallet.Deposit.IO.Resource
( withResource
)
Expand Down Expand Up @@ -201,6 +204,9 @@ import Control.Tracer
import Data.Function
( (&)
)
import Data.Functor.Contravariant
( (>$<)
)
import Data.Generics.Internal.VL
( view
)
Expand Down Expand Up @@ -551,7 +557,13 @@ serveWallet
application =
Server.serve api
$ DepositUi.serveUI
(UiApplicationLog >$< applicationTracer)
ui
( WalletBootEnv
(error "Not defined")
(error "Not defined")
(error "Not defined")
)
databaseDir'
(PageConfig "" "Deposit Cardano Wallet")
_proxy
Expand Down
13 changes: 11 additions & 2 deletions lib/exe/lib/Cardano/Wallet/Application/Logging.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ module Cardano.Wallet.Application.Logging

import Prelude

import Cardano.BM.Data.Severity
( Severity (..)
)
import Cardano.BM.Data.Tracer
( HasPrivacyAnnotation
, HasSeverityAnnotation
, HasSeverityAnnotation (..)
)
import Cardano.Wallet.Api.Http.Logging
( ApiApplicationLog
Expand All @@ -27,13 +30,19 @@ import GHC.Generics
data ApplicationLog
= ApiApplicationLog ApiApplicationLog
| MsgServerStartupError ListenError
| UiApplicationLog String
deriving (Generic, Show, Eq)

instance ToText ApplicationLog where
toText = \case
ApiApplicationLog msg -> toText msg
MsgServerStartupError err -> toText err
UiApplicationLog msg -> toText msg

instance HasPrivacyAnnotation ApplicationLog

instance HasSeverityAnnotation ApplicationLog
instance HasSeverityAnnotation ApplicationLog where
getSeverityAnnotation = \case
ApiApplicationLog msg -> getSeverityAnnotation msg
MsgServerStartupError _ -> Error
UiApplicationLog _ -> Warning
2 changes: 1 addition & 1 deletion lib/ui/src/Cardano/Wallet/UI/Common/Html/Pages/Lib.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ import Servant
import qualified Data.Text as T

-- | A simple alert message around any html content.
alertH :: ToHtml a => a -> Html ()
alertH :: (ToHtml a, Monad m) => a -> HtmlT m ()
alertH =
div_
[ id_ "result"
Expand Down
85 changes: 54 additions & 31 deletions lib/ui/src/Cardano/Wallet/UI/Common/Html/Pages/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,53 +80,76 @@ newWalletH walletMnemonicLink config = do

div_ [id_ "menmonic", class_ "mb-3"] ""

postWalletForm config
postWalletMnemonicForm config

div_
[ id_ "new_wallet"
]
mempty

newWalletFromXPubH :: PostWalletConfig -> WHtml ()
newWalletFromXPubH config = do
useHtmxExtension "json-enc"

postWalletMnemonicForm config

div_
[ id_ "new_wallet"
]
mempty
data PostWalletConfig = PostWalletConfig
{ passwordVisibility :: Maybe Visible
, walletDataLink :: Link
}

postWalletForm :: PostWalletConfig -> WHtml ()
postWalletForm PostWalletConfig{..} = form_
[ hxPost_ $ linkText walletDataLink
, hxExt_ "json-enc"
, hxTarget_ "#new_wallet"
, autocomplete_ "off"
]
$ do
input_
formHeaderH :: PostWalletConfig -> WHtml () -> WHtml ()
formHeaderH PostWalletConfig{..} =
form_
[ hxPost_ $ linkText walletDataLink
, hxExt_ "json-enc"
, hxTarget_ "#new_wallet"
, autocomplete_ "off"
]

mnemonicSetupFieldsH :: PostWalletConfig -> WHtml ()
mnemonicSetupFieldsH PostWalletConfig{..} = do
input_
[ class_ "form-control form-control-lg mb-3"
, visibility
, name_ "mnemonicSentence"
, placeholder_ "Mnemonic Sentence"
]
onShelley
$ input_
[ class_ "form-control form-control-lg mb-3"
, visibility
, name_ "mnemonicSentence"
, placeholder_ "Mnemonic Sentence"
, type_ "text"
, name_ "name"
, placeholder_ "Wallet Name"
]
onShelley
$ input_
[ class_ "form-control form-control-lg mb-3"
, type_ "text"
, name_ "name"
, placeholder_ "Wallet Name"
]
onShelley
$ input_
[ class_ "form-control form-control-lg mb-3"
, visibility
, name_ "passphrase"
, placeholder_ "Passphrase"
]
button_
[ class_ "btn btn-primary btn-block mb-3"
, type_ "submit"
onShelley
$ input_
[ class_ "form-control form-control-lg mb-3"
, visibility
, name_ "passphrase"
, placeholder_ "Passphrase"
]
"Restore wallet"
where
visibility = type_ $ case passwordVisibility of
Just Visible -> "text"
Just Hidden -> "password"
Nothing -> "password"

postWalletMnemonicForm :: PostWalletConfig -> WHtml ()
postWalletMnemonicForm cfg = formHeaderH cfg $ mnemonicSetupFieldsH cfg

xpubSetupFieldsH :: WHtml ()
xpubSetupFieldsH = do
input_
[ class_ "form-control form-control-lg mb-3"
, type_ "text"
, name_ "xpub"
, placeholder_ "Extended Public Key"
]

postWalletXPubForm :: PostWalletConfig -> WHtml ()
postWalletXPubForm cfg = formHeaderH cfg xpubSetupFieldsH
19 changes: 18 additions & 1 deletion lib/ui/src/Cardano/Wallet/UI/Deposit/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ import Cardano.Wallet.UI.Common.Handlers.SSE
import Cardano.Wallet.UI.Cookies
( CookieRequest
)
import Data.Aeson
( Value
)
import Servant
( Get
, JSON
, Link
, Post
, Proxy (..)
, QueryParam
, ReqBody
, allLinks
, (:<|>) (..)
, (:>)
Expand All @@ -59,6 +64,14 @@ type Data =
:> QueryParam "clean" Bool
:> SessionedHtml Get
:<|> "wallet" :> SessionedHtml Get
:<|> "wallet"
:> "mnemonic"
:> ReqBody '[JSON] Value
:> SessionedHtml Post
:<|> "wallet"
:> "xpub"
:> ReqBody '[JSON] Value
:> SessionedHtml Post

type Home = SessionedHtml Get

Expand All @@ -82,6 +95,8 @@ faviconLink :: Link
walletMnemonicLink :: Maybe Bool -> Link
walletPageLink :: Link
walletLink :: Link
walletPostMnemonicLink :: Link
walletPostXPubLink :: Link
homePageLink
:<|> aboutPageLink
:<|> networkPageLink
Expand All @@ -93,5 +108,7 @@ homePageLink
:<|> sseLink
:<|> faviconLink
:<|> walletMnemonicLink
:<|> walletLink =
:<|> walletLink
:<|> walletPostMnemonicLink
:<|> walletPostXPubLink =
allLinks (Proxy @UI)
59 changes: 51 additions & 8 deletions lib/ui/src/Cardano/Wallet/UI/Deposit/Handlers/Page.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
{-# OPTIONS_GHC -Wno-type-defaults #-}

module Cardano.Wallet.UI.Deposit.Handlers.Page
where

import Prelude

import Cardano.Wallet.Deposit.IO
( WalletBootEnv
)
import Cardano.Wallet.Deposit.IO.Resource
( ResourceStatus (..)
, readStatus
)
import Cardano.Wallet.Deposit.REST
( WalletResource
, loadWallet
, walletExists
, walletPublicIdentity
)
Expand Down Expand Up @@ -34,28 +44,61 @@ import Cardano.Wallet.UI.Deposit.Html.Pages.Page
import Cardano.Wallet.UI.Deposit.Html.Pages.Wallet
( WalletPresent (..)
)
import Cardano.Wallet.UI.Type
( WHtml
)
import Control.Monad.IO.Class
( MonadIO (..)
)
import Control.Monad.Reader.Class
( ask
)
import Control.Tracer
( Tracer
, traceWith
)
import Servant
( Handler
)

import qualified Data.ByteString.Lazy.Char8 as BL

lg :: (MonadIO m, Show a) => Tracer IO String -> String -> a -> m ()
lg tr p x = liftIO $ traceWith tr $ p <> ": " <> show x

pageHandler
:: UILayer WalletResource
:: Tracer IO String
-> UILayer WalletResource
-> WalletBootEnv IO
-- ^ The deposit UI layer
-> FilePath
-- ^ The directory where the wallet data is stored
-> PageConfig
-- ^ The page configuration
-> Page
-- ^ The page to render
-> (BL.ByteString -> WHtml ())
-> Maybe RequestCookies
-- ^ The request cookies
-> Handler (CookieResponse RawHtml)
pageHandler layer dir config x =
pageHandler tr layer env dir config x alert =
withSessionLayer layer $ \session -> do
w <- catchRunWalletResourceM session $ do
test <- walletExists dir
if test
then do
WalletPresent <$> walletPublicIdentity
else pure WalletAbsent
pure $ page config x w
s <- ask >>= liftIO . readStatus
case s of
NotInitialized -> do
test <- walletExists dir
if test
then do
lg tr "Loading wallet from" dir
loadWallet env dir
lg tr "Wallet loaded from" dir
WalletPresent <$> walletPublicIdentity
else
pure WalletAbsent
Initialized _ -> WalletPresent <$> walletPublicIdentity
Vanished e -> pure $ WalletVanished e
FailedToInitialize e -> pure $ WalletFailedToInitialize e
Initializing -> pure WalletInitializing
lg tr "Rendering page" w
pure $ page config x alert w
Loading

0 comments on commit 70dfa74

Please sign in to comment.