Skip to content

Commit c87261a

Browse files
committed
Add postWallet deposit UI endpoint
1 parent 9951cc6 commit c87261a

File tree

13 files changed

+294
-68
lines changed

13 files changed

+294
-68
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,5 @@ package-lock.json
9090
package.json
9191

9292
# buildkite artifacts testing
93-
artifacts
93+
artifacts
94+
deposit-*

lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/IO.hs

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ data WalletPublicIdentity = WalletPublicIdentity
199199
{ pubXpub :: XPub
200200
, pubNextUser :: Word31
201201
}
202+
deriving Show
202203

203204
walletPublicIdentity :: WalletInstance -> IO WalletPublicIdentity
204205
walletPublicIdentity w = do

lib/exe/lib/Cardano/Wallet/Application.hs

+12
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ import Cardano.Wallet.DB.Layer
9595
import Cardano.Wallet.DB.Sqlite.Migration.Old
9696
( DefaultFieldValues (..)
9797
)
98+
import Cardano.Wallet.Deposit.IO
99+
( WalletBootEnv (..)
100+
)
98101
import Cardano.Wallet.Deposit.IO.Resource
99102
( withResource
100103
)
@@ -201,6 +204,9 @@ import Control.Tracer
201204
import Data.Function
202205
( (&)
203206
)
207+
import Data.Functor.Contravariant
208+
( (>$<)
209+
)
204210
import Data.Generics.Internal.VL
205211
( view
206212
)
@@ -551,7 +557,13 @@ serveWallet
551557
application =
552558
Server.serve api
553559
$ DepositUi.serveUI
560+
(UiApplicationLog >$< applicationTracer)
554561
ui
562+
( WalletBootEnv
563+
(error "Not defined")
564+
(error "Not defined")
565+
(error "Not defined")
566+
)
555567
databaseDir'
556568
(PageConfig "" "Deposit Cardano Wallet")
557569
_proxy

lib/exe/lib/Cardano/Wallet/Application/Logging.hs

+11-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ module Cardano.Wallet.Application.Logging
77

88
import Prelude
99

10+
import Cardano.BM.Data.Severity
11+
( Severity (..)
12+
)
1013
import Cardano.BM.Data.Tracer
1114
( HasPrivacyAnnotation
12-
, HasSeverityAnnotation
15+
, HasSeverityAnnotation (..)
1316
)
1417
import Cardano.Wallet.Api.Http.Logging
1518
( ApiApplicationLog
@@ -27,13 +30,19 @@ import GHC.Generics
2730
data ApplicationLog
2831
= ApiApplicationLog ApiApplicationLog
2932
| MsgServerStartupError ListenError
33+
| UiApplicationLog String
3034
deriving (Generic, Show, Eq)
3135

3236
instance ToText ApplicationLog where
3337
toText = \case
3438
ApiApplicationLog msg -> toText msg
3539
MsgServerStartupError err -> toText err
40+
UiApplicationLog msg -> toText msg
3641

3742
instance HasPrivacyAnnotation ApplicationLog
3843

39-
instance HasSeverityAnnotation ApplicationLog
44+
instance HasSeverityAnnotation ApplicationLog where
45+
getSeverityAnnotation = \case
46+
ApiApplicationLog msg -> getSeverityAnnotation msg
47+
MsgServerStartupError _ -> Error
48+
UiApplicationLog _ -> Warning

lib/ui/src/Cardano/Wallet/UI/Common/Html/Pages/Lib.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ import Servant
8383
import qualified Data.Text as T
8484

8585
-- | A simple alert message around any html content.
86-
alertH :: ToHtml a => a -> Html ()
86+
alertH :: (ToHtml a, Monad m) => a -> HtmlT m ()
8787
alertH =
8888
div_
8989
[ id_ "result"

lib/ui/src/Cardano/Wallet/UI/Common/Html/Pages/Wallet.hs

+54-31
Original file line numberDiff line numberDiff line change
@@ -80,53 +80,76 @@ newWalletH walletMnemonicLink config = do
8080

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

83-
postWalletForm config
83+
postWalletMnemonicForm config
8484

8585
div_
8686
[ id_ "new_wallet"
8787
]
8888
mempty
8989

90+
newWalletFromXPubH :: PostWalletConfig -> WHtml ()
91+
newWalletFromXPubH config = do
92+
useHtmxExtension "json-enc"
93+
94+
postWalletMnemonicForm config
95+
96+
div_
97+
[ id_ "new_wallet"
98+
]
99+
mempty
90100
data PostWalletConfig = PostWalletConfig
91101
{ passwordVisibility :: Maybe Visible
92102
, walletDataLink :: Link
93103
}
94104

95-
postWalletForm :: PostWalletConfig -> WHtml ()
96-
postWalletForm PostWalletConfig{..} = form_
97-
[ hxPost_ $ linkText walletDataLink
98-
, hxExt_ "json-enc"
99-
, hxTarget_ "#new_wallet"
100-
, autocomplete_ "off"
101-
]
102-
$ do
103-
input_
105+
formHeaderH :: PostWalletConfig -> WHtml () -> WHtml ()
106+
formHeaderH PostWalletConfig{..} =
107+
form_
108+
[ hxPost_ $ linkText walletDataLink
109+
, hxExt_ "json-enc"
110+
, hxTarget_ "#new_wallet"
111+
, autocomplete_ "off"
112+
]
113+
114+
mnemonicSetupFieldsH :: PostWalletConfig -> WHtml ()
115+
mnemonicSetupFieldsH PostWalletConfig{..} = do
116+
input_
117+
[ class_ "form-control form-control-lg mb-3"
118+
, visibility
119+
, name_ "mnemonicSentence"
120+
, placeholder_ "Mnemonic Sentence"
121+
]
122+
onShelley
123+
$ input_
104124
[ class_ "form-control form-control-lg mb-3"
105-
, visibility
106-
, name_ "mnemonicSentence"
107-
, placeholder_ "Mnemonic Sentence"
125+
, type_ "text"
126+
, name_ "name"
127+
, placeholder_ "Wallet Name"
108128
]
109-
onShelley
110-
$ input_
111-
[ class_ "form-control form-control-lg mb-3"
112-
, type_ "text"
113-
, name_ "name"
114-
, placeholder_ "Wallet Name"
115-
]
116-
onShelley
117-
$ input_
118-
[ class_ "form-control form-control-lg mb-3"
119-
, visibility
120-
, name_ "passphrase"
121-
, placeholder_ "Passphrase"
122-
]
123-
button_
124-
[ class_ "btn btn-primary btn-block mb-3"
125-
, type_ "submit"
129+
onShelley
130+
$ input_
131+
[ class_ "form-control form-control-lg mb-3"
132+
, visibility
133+
, name_ "passphrase"
134+
, placeholder_ "Passphrase"
126135
]
127-
"Restore wallet"
128136
where
129137
visibility = type_ $ case passwordVisibility of
130138
Just Visible -> "text"
131139
Just Hidden -> "password"
132140
Nothing -> "password"
141+
142+
postWalletMnemonicForm :: PostWalletConfig -> WHtml ()
143+
postWalletMnemonicForm cfg = formHeaderH cfg $ mnemonicSetupFieldsH cfg
144+
145+
xpubSetupFieldsH :: WHtml ()
146+
xpubSetupFieldsH = do
147+
input_
148+
[ class_ "form-control form-control-lg mb-3"
149+
, type_ "text"
150+
, name_ "xpub"
151+
, placeholder_ "Extended Public Key"
152+
]
153+
154+
postWalletXPubForm :: PostWalletConfig -> WHtml ()
155+
postWalletXPubForm cfg = formHeaderH cfg xpubSetupFieldsH

lib/ui/src/Cardano/Wallet/UI/Deposit/API.hs

+18-1
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,17 @@ import Cardano.Wallet.UI.Common.Handlers.SSE
2727
import Cardano.Wallet.UI.Cookies
2828
( CookieRequest
2929
)
30+
import Data.Aeson
31+
( Value
32+
)
3033
import Servant
3134
( Get
35+
, JSON
3236
, Link
3337
, Post
3438
, Proxy (..)
3539
, QueryParam
40+
, ReqBody
3641
, allLinks
3742
, (:<|>) (..)
3843
, (:>)
@@ -59,6 +64,14 @@ type Data =
5964
:> QueryParam "clean" Bool
6065
:> SessionedHtml Get
6166
:<|> "wallet" :> SessionedHtml Get
67+
:<|> "wallet"
68+
:> "mnemonic"
69+
:> ReqBody '[JSON] Value
70+
:> SessionedHtml Post
71+
:<|> "wallet"
72+
:> "xpub"
73+
:> ReqBody '[JSON] Value
74+
:> SessionedHtml Post
6275

6376
type Home = SessionedHtml Get
6477

@@ -82,6 +95,8 @@ faviconLink :: Link
8295
walletMnemonicLink :: Maybe Bool -> Link
8396
walletPageLink :: Link
8497
walletLink :: Link
98+
walletPostMnemonicLink :: Link
99+
walletPostXPubLink :: Link
85100
homePageLink
86101
:<|> aboutPageLink
87102
:<|> networkPageLink
@@ -93,5 +108,7 @@ homePageLink
93108
:<|> sseLink
94109
:<|> faviconLink
95110
:<|> walletMnemonicLink
96-
:<|> walletLink =
111+
:<|> walletLink
112+
:<|> walletPostMnemonicLink
113+
:<|> walletPostXPubLink =
97114
allLinks (Proxy @UI)

lib/ui/src/Cardano/Wallet/UI/Deposit/Handlers/Page.hs

+51-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1+
{-# OPTIONS_GHC -Wno-type-defaults #-}
2+
13
module Cardano.Wallet.UI.Deposit.Handlers.Page
24
where
35

46
import Prelude
57

8+
import Cardano.Wallet.Deposit.IO
9+
( WalletBootEnv
10+
)
11+
import Cardano.Wallet.Deposit.IO.Resource
12+
( ResourceStatus (..)
13+
, readStatus
14+
)
615
import Cardano.Wallet.Deposit.REST
716
( WalletResource
17+
, loadWallet
818
, walletExists
919
, walletPublicIdentity
1020
)
@@ -34,28 +44,61 @@ import Cardano.Wallet.UI.Deposit.Html.Pages.Page
3444
import Cardano.Wallet.UI.Deposit.Html.Pages.Wallet
3545
( WalletPresent (..)
3646
)
47+
import Cardano.Wallet.UI.Type
48+
( WHtml
49+
)
50+
import Control.Monad.IO.Class
51+
( MonadIO (..)
52+
)
53+
import Control.Monad.Reader.Class
54+
( ask
55+
)
56+
import Control.Tracer
57+
( Tracer
58+
, traceWith
59+
)
3760
import Servant
3861
( Handler
3962
)
4063

64+
import qualified Data.ByteString.Lazy.Char8 as BL
65+
66+
lg :: (MonadIO m, Show a) => Tracer IO String -> String -> a -> m ()
67+
lg tr p x = liftIO $ traceWith tr $ p <> ": " <> show x
68+
4169
pageHandler
42-
:: UILayer WalletResource
70+
:: Tracer IO String
71+
-> UILayer WalletResource
72+
-> WalletBootEnv IO
4373
-- ^ The deposit UI layer
4474
-> FilePath
4575
-- ^ The directory where the wallet data is stored
4676
-> PageConfig
4777
-- ^ The page configuration
4878
-> Page
4979
-- ^ The page to render
80+
-> (BL.ByteString -> WHtml ())
5081
-> Maybe RequestCookies
5182
-- ^ The request cookies
5283
-> Handler (CookieResponse RawHtml)
53-
pageHandler layer dir config x =
84+
pageHandler tr layer env dir config x alert =
5485
withSessionLayer layer $ \session -> do
5586
w <- catchRunWalletResourceM session $ do
56-
test <- walletExists dir
57-
if test
58-
then do
59-
WalletPresent <$> walletPublicIdentity
60-
else pure WalletAbsent
61-
pure $ page config x w
87+
s <- ask >>= liftIO . readStatus
88+
case s of
89+
NotInitialized -> do
90+
test <- walletExists dir
91+
if test
92+
then do
93+
lg tr "Loading wallet from" dir
94+
loadWallet env dir
95+
lg tr "Wallet loaded from" dir
96+
WalletPresent <$> walletPublicIdentity
97+
else
98+
pure WalletAbsent
99+
Initialized _ -> WalletPresent <$> walletPublicIdentity
100+
Vanished e -> pure $ WalletVanished e
101+
FailedToInitialize e -> pure $ WalletFailedToInitialize e
102+
Initializing -> pure WalletInitializing
103+
lg tr "Rendering page" w
104+
pure $ page config x alert w

0 commit comments

Comments
 (0)