Skip to content

Commit f10d7eb

Browse files
committed
Add postWallet deposit UI endpoint
1 parent fcd9fe8 commit f10d7eb

File tree

17 files changed

+378
-106
lines changed

17 files changed

+378
-106
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/customer-deposit-wallet/src/Cardano/Wallet/Deposit/IO/Resource.hs

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Cardano.Wallet.Deposit.IO.Resource
1919
, putResource
2020
, ResourceStatus (..)
2121
, readStatus
22+
, reInitialize
2223
) where
2324

2425
import Prelude
@@ -126,6 +127,10 @@ onResource action resource = do
126127
Vanished e -> pure $ Left $ ErrVanished e
127128
FailedToInitialize e -> pure $ Left $ ErrFailedToInitialize e
128129

130+
-- | Reinitialize a 'Resource'.
131+
reInitialize :: Resource e a -> IO ()
132+
reInitialize resource = atomically $ writeTVar (content resource) NotInitialized
133+
129134
-- | Error condition for 'putResource'.
130135
data ErrResourceExists e a
131136
= -- | The resource 'a' is currently being initialized.

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/cardano-wallet-ui.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ library
100100
, http-media
101101
, lens
102102
, lucid
103+
, memory
103104
, mmorph
104105
, mtl
105106
, ntp-client

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/Template/Head.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ where
1010
import Prelude
1111

1212
import Cardano.Wallet.UI.Common.Html.Htmx
13-
( useHtmxVersion
13+
( useHtmxVersion, useHtmxExtension
1414
)
1515
import Cardano.Wallet.UI.Common.Html.Lib
1616
( linkText
@@ -107,6 +107,7 @@ pageFromBodyH faviconLink PageConfig{..} body =
107107
popperScript
108108
favicon faviconLink
109109
useHtmxVersion (1, 9, 12)
110+
useHtmxExtension "json-enc"
110111
body_ body
111112

112113
data PageConfig = PageConfig

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

+91-55
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Cardano.Wallet.UI.Common.Html.Htmx
1313
, hxGet_
1414
, hxPost_
1515
, hxTarget_
16-
, useHtmxExtension
16+
1717
)
1818
import Cardano.Wallet.UI.Common.Html.Lib
1919
( linkText
@@ -49,25 +49,44 @@ import Servant
4949

5050
import qualified Data.Text as T
5151

52-
mnemonicH :: Maybe [Text] -> Html ()
53-
mnemonicH Nothing = ""
54-
mnemonicH (Just mnemonic) = do
55-
div_ [class_ "card"] $ do
56-
div_
57-
[ class_ "card-body text-muted small"
58-
, id_ "copy-mnemonic"
59-
]
60-
$ toHtml
61-
$ T.intercalate " " mnemonic
62-
copyButton "copy-mnemonic"
52+
--------------------------------------------------------------------------------
53+
-- Configuration
54+
--------------------------------------------------------------------------------
55+
56+
-- | Configuration for the wallet creation form
57+
data PostWalletConfig = PostWalletConfig
58+
{ passwordVisibility :: Maybe Visible
59+
-- ^ Whether the password should be visible
60+
, walletDataLink :: Link
61+
-- ^ Link to post the form data to
62+
, responseTarget :: Text
63+
}
64+
65+
--------------------------------------------------------------------------------
66+
-- Library
67+
--------------------------------------------------------------------------------
68+
69+
-- | Add a form tag with the appropriate attributes for a POST request
70+
postWalletFormTagH :: PostWalletConfig -> WHtml () -> WHtml ()
71+
postWalletFormTagH PostWalletConfig{..} =
72+
form_
73+
[ hxPost_ $ linkText walletDataLink
74+
, hxExt_ "json-enc"
75+
, hxTarget_ responseTarget
76+
, autocomplete_ "off"
77+
]
78+
79+
--------------------------------------------------------------------------------
80+
-- Wallet creation forms
81+
--------------------------------------------------------------------------------
6382

64-
newWalletH :: (Maybe Bool -> Link) -> PostWalletConfig -> WHtml ()
65-
newWalletH walletMnemonicLink config = do
66-
useHtmxExtension "json-enc"
83+
-- | Widget to create a new wallet from a mnemonic
84+
newWalletFromMnemonicH :: (Maybe Bool -> Link) -> PostWalletConfig -> WHtml ()
85+
newWalletFromMnemonicH walletMnemonicLink config = do
6786
div_ [class_ "btn-group mb-3", role_ "group"] $ do
6887
button_
6988
[ class_ "btn btn-outline-secondary"
70-
, hxGet_ $ linkText $ walletMnemonicLink Nothing
89+
, hxGet_ $ linkText $ walletMnemonicLink $ Just False
7190
, hxTarget_ "#menmonic"
7291
]
7392
"Hint a mnemonic"
@@ -80,53 +99,70 @@ newWalletH walletMnemonicLink config = do
8099

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

83-
postWalletForm config
102+
postWalletFormTagH config $ mnemonicSetupFieldsH config
84103

85-
div_
86-
[ id_ "new_wallet"
104+
-- | Display a mnemonic
105+
mnemonicH :: Maybe [Text] -> Html ()
106+
mnemonicH Nothing = ""
107+
mnemonicH (Just mnemonic) = do
108+
div_ [class_ "card"] $ do
109+
div_
110+
[ class_ "card-body text-muted small"
111+
, id_ "copy-mnemonic"
112+
]
113+
$ toHtml
114+
$ T.intercalate " " mnemonic
115+
copyButton "copy-mnemonic"
116+
117+
-- | Form fields for restoring a wallet from a mnemonic
118+
mnemonicSetupFieldsH :: PostWalletConfig -> WHtml ()
119+
mnemonicSetupFieldsH PostWalletConfig{..} = do
120+
input_
121+
[ class_ "form-control form-control-lg mb-3"
122+
, visibility
123+
, name_ "mnemonicSentence"
124+
, placeholder_ "Mnemonic Sentence"
87125
]
88-
mempty
126+
onShelley
127+
$ input_
128+
[ class_ "form-control form-control-lg mb-3"
129+
, type_ "text"
130+
, name_ "name"
131+
, placeholder_ "Wallet Name"
132+
]
133+
onShelley
134+
$ input_
135+
[ class_ "form-control form-control-lg mb-3"
136+
, type_ "password"
137+
, name_ "password"
138+
, placeholder_ "Wallet Password"
139+
]
140+
button_
141+
[ class_ "btn btn-primary btn-block mb-3"
142+
, type_ "submit"
143+
]
144+
"Restore wallet from mnemonic"
145+
where
146+
visibility = type_ $ case passwordVisibility of
147+
Just Visible -> "text"
148+
Just Hidden -> "password"
149+
Nothing -> "password"
89150

90-
data PostWalletConfig = PostWalletConfig
91-
{ passwordVisibility :: Maybe Visible
92-
, walletDataLink :: Link
93-
}
151+
--------------------------------------------------------------------------------
152+
-- Wallet restoration from public key
153+
--------------------------------------------------------------------------------
94154

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
155+
newWalletFromXPubH :: PostWalletConfig -> WHtml ()
156+
newWalletFromXPubH config = do
157+
postWalletFormTagH config $ do
103158
input_
104159
[ class_ "form-control form-control-lg mb-3"
105-
, visibility
106-
, name_ "mnemonicSentence"
107-
, placeholder_ "Mnemonic Sentence"
160+
, type_ "text"
161+
, name_ "xpub"
162+
, placeholder_ "Extended Public Key"
108163
]
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-
]
123164
button_
124165
[ class_ "btn btn-primary btn-block mb-3"
125166
, type_ "submit"
126167
]
127-
"Restore wallet"
128-
where
129-
visibility = type_ $ case passwordVisibility of
130-
Just Visible -> "text"
131-
Just Hidden -> "password"
132-
Nothing -> "password"
168+
"Restore wallet from public key"

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)

0 commit comments

Comments
 (0)