Skip to content

Commit 4a5ad61

Browse files
committed
Add users parameter to wallet creation forms
1 parent 144d9a8 commit 4a5ad61

File tree

8 files changed

+98
-67
lines changed

8 files changed

+98
-67
lines changed

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

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{-# LANGUAGE DataKinds #-}
22
{-# LANGUAGE GADTs #-}
33
{-# LANGUAGE LambdaCase #-}
4+
{-# LANGUAGE ScopedTypeVariables #-}
45
{-# LANGUAGE TypeApplications #-}
56

67
-- |
@@ -237,7 +238,9 @@ findTheDepositWalletOnDisk fp action = do
237238

238239
-- | Try to create a new wallet
239240
createTheDepositWalletOnDisk
240-
:: FilePath
241+
:: Tracer IO String
242+
-- ^ Tracer for logging
243+
-> FilePath
241244
-- ^ Path to the wallet database directory
242245
-> XPub
243246
-- ^ Id of the wallet
@@ -246,7 +249,7 @@ createTheDepositWalletOnDisk
246249
-> (Maybe WalletIO.WalletStore -> IO a)
247250
-- ^ Action to run if the wallet is created
248251
-> IO a
249-
createTheDepositWalletOnDisk fp identity users action = do
252+
createTheDepositWalletOnDisk _tr fp identity users action = do
250253
ds <- scanDirectoryForDepositPrefix fp
251254
case ds of
252255
[] -> do
@@ -260,7 +263,7 @@ createTheDepositWalletOnDisk fp identity users action = do
260263
hashWalletId :: XPub -> String
261264
hashWalletId =
262265
B8.unpack
263-
. convertToBase Base64
266+
. convertToBase Base16
264267
. blake2b160
265268
. xpubPublicKey
266269

@@ -289,7 +292,9 @@ loadWallet bootEnv fp trs = do
289292

290293
-- | Initialize a new wallet from an 'XPub'.
291294
initXPubWallet
292-
:: WalletIO.WalletBootEnv IO
295+
:: Tracer IO String
296+
-- ^ Tracer for logging
297+
-> WalletIO.WalletBootEnv IO
293298
-- ^ Environment for the wallet
294299
-> FilePath
295300
-- ^ Path to the wallet database directory
@@ -299,9 +304,9 @@ initXPubWallet
299304
-> Word31
300305
-- ^ Max number of users ?
301306
-> WalletResourceM ()
302-
initXPubWallet bootEnv fp trs xpub users = do
307+
initXPubWallet tr bootEnv fp trs xpub users = do
303308
let action :: (WalletIO.WalletInstance -> IO b) -> IO (Either ErrDatabase b)
304-
action f = createTheDepositWalletOnDisk fp xpub users $ \case
309+
action f = createTheDepositWalletOnDisk tr fp xpub users $ \case
305310
Just wallet -> do
306311
fmap Right
307312
$ WalletIO.withWalletInit

lib/customer-deposit-wallet/test/unit/Cardano/Wallet/Deposit/RESTSpec.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ withInitializedWallet
7777
-> WalletResourceM a
7878
-> IO (Either ErrWalletResource a)
7979
withInitializedWallet dir f = withWallet $ do
80-
initXPubWallet fakeBootEnv dir nullTracer xpub 0
80+
initXPubWallet nullTracer fakeBootEnv dir nullTracer xpub 0
8181
letItInitialize
8282
f
8383

lib/ui/cardano-wallet-ui.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ library
9797
, customer-deposit-wallet
9898
, exceptions
9999
, generic-lens
100+
, http-api-data
100101
, http-media
101102
, lens
102103
, lucid

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

+17-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import Cardano.Wallet.UI.Common.API
99
( Visible (..)
1010
)
1111
import Cardano.Wallet.UI.Common.Html.Htmx
12-
( hxExt_
13-
, hxGet_
12+
( hxGet_
1413
, hxPost_
1514
, hxTarget_
1615
)
@@ -22,6 +21,7 @@ import Cardano.Wallet.UI.Common.Html.Pages.Lib
2221
)
2322
import Cardano.Wallet.UI.Type
2423
( WHtml
24+
, onDeposit
2525
, onShelley
2626
)
2727
import Data.Text
@@ -70,7 +70,6 @@ postWalletFormTagH :: PostWalletConfig -> WHtml () -> WHtml ()
7070
postWalletFormTagH PostWalletConfig{..} =
7171
form_
7272
[ hxPost_ $ linkText walletDataLink
73-
, hxExt_ "json-enc"
7473
, hxTarget_ responseTarget
7574
, autocomplete_ "off"
7675
]
@@ -119,9 +118,16 @@ mnemonicSetupFieldsH PostWalletConfig{..} = do
119118
input_
120119
[ class_ "form-control form-control-lg mb-3"
121120
, visibility
122-
, name_ "mnemonicSentence"
121+
, name_ "mnemonics"
123122
, placeholder_ "Mnemonic Sentence"
124123
]
124+
onDeposit
125+
$ input_
126+
[ class_ "form-control form-control-lg mb-3"
127+
, type_ "number"
128+
, name_ "users"
129+
, placeholder_ "Customer Discovery"
130+
]
125131
onShelley
126132
$ input_
127133
[ class_ "form-control form-control-lg mb-3"
@@ -160,6 +166,13 @@ newWalletFromXPubH config = do
160166
, name_ "xpub"
161167
, placeholder_ "Extended Public Key"
162168
]
169+
onDeposit
170+
$ input_
171+
[ class_ "form-control form-control-lg mb-3"
172+
, type_ "number"
173+
, name_ "users"
174+
, placeholder_ "Customer Discovery"
175+
]
163176
button_
164177
[ class_ "btn btn-primary btn-block mb-3"
165178
, type_ "submit"

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

+31-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{-# LANGUAGE BlockArguments #-}
22
{-# LANGUAGE DataKinds #-}
3+
{-# LANGUAGE DeriveGeneric #-}
4+
{-# LANGUAGE DuplicateRecordFields #-}
35
{-# LANGUAGE FlexibleContexts #-}
46
{-# LANGUAGE MultiParamTypeClasses #-}
57
{-# LANGUAGE NoMonomorphismRestriction #-}
@@ -27,12 +29,15 @@ import Cardano.Wallet.UI.Common.Handlers.SSE
2729
import Cardano.Wallet.UI.Cookies
2830
( CookieRequest
2931
)
30-
import Data.Aeson
31-
( Value
32+
import Data.Text
33+
( Text
34+
)
35+
import GHC.Generics
36+
( Generic
3237
)
3338
import Servant
34-
( Get
35-
, JSON
39+
( FormUrlEncoded
40+
, Get
3641
, Link
3742
, Post
3843
, Proxy (..)
@@ -42,9 +47,28 @@ import Servant
4247
, (:<|>) (..)
4348
, (:>)
4449
)
50+
import Web.FormUrlEncoded
51+
( FromForm
52+
)
4553

4654
import qualified Data.ByteString.Lazy as BL
4755

56+
data PostWalletViaMenmonic = PostWalletViaMenmonic
57+
{ mnemonics :: Text
58+
, users :: Int
59+
}
60+
deriving (Generic)
61+
62+
instance FromForm PostWalletViaMenmonic
63+
64+
data PostWalletViaXPub = PostWalletViaXPub
65+
{ xpub :: Text
66+
, users :: Int
67+
}
68+
deriving (Generic)
69+
70+
instance FromForm PostWalletViaXPub
71+
4872
-- | Pages endpoints
4973
type Pages =
5074
"about" :> SessionedHtml Get
@@ -66,11 +90,11 @@ type Data =
6690
:<|> "wallet" :> SessionedHtml Get
6791
:<|> "wallet"
6892
:> "mnemonic"
69-
:> ReqBody '[JSON] Value
93+
:> ReqBody '[FormUrlEncoded] PostWalletViaMenmonic
7094
:> SessionedHtml Post
7195
:<|> "wallet"
7296
:> "xpub"
73-
:> ReqBody '[JSON] Value
97+
:> ReqBody '[FormUrlEncoded] PostWalletViaXPub
7498
:> SessionedHtml Post
7599

76100
type Home = SessionedHtml Get
@@ -110,6 +134,5 @@ homePageLink
110134
:<|> walletMnemonicLink
111135
:<|> walletLink
112136
:<|> walletPostMnemonicLink
113-
:<|> walletPostXPubLink
114-
=
137+
:<|> walletPostXPubLink =
115138
allLinks (Proxy @UI)

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

+29-45
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,24 @@ import Prelude
88
import Cardano.Address.Derivation
99
( XPub
1010
)
11+
import Cardano.Wallet.Deposit.Pure
12+
( Customer
13+
)
1114
import Cardano.Wallet.Deposit.REST
1215
( ErrWalletResource
1316
, WalletResource
1417
, WalletResourceM
1518
, runWalletResourceM
1619
)
17-
import Cardano.Wallet.UI.Common.Handlers.Lib
18-
( handleParseRequestError
19-
)
2020
import Cardano.Wallet.UI.Common.Layer
2121
( Push (Push)
2222
, SessionLayer (..)
2323
, stateL
2424
)
25+
import Cardano.Wallet.UI.Deposit.API
26+
( PostWalletViaMenmonic (..)
27+
, PostWalletViaXPub (..)
28+
)
2529
import Cardano.Wallet.UI.Deposit.Handlers.Lib
2630
( walletPresent
2731
)
@@ -34,24 +38,13 @@ import Control.Lens
3438
import Control.Monad.Trans
3539
( MonadIO (..)
3640
)
37-
import Data.Aeson
38-
( Value
39-
, withObject
40-
, (.:)
41-
)
42-
import Data.Aeson.Types
43-
( parseEither
44-
)
4541
import Data.ByteArray.Encoding
4642
( Base (..)
4743
, convertFromBase
4844
)
4945
import Data.ByteString
5046
( ByteString
5147
)
52-
import Data.Text
53-
( Text
54-
)
5548
import Servant
5649
( Handler
5750
)
@@ -91,12 +84,11 @@ initWalletWithXPub
9184
:: SessionLayer WalletResource
9285
-> (BL.ByteString -> html)
9386
-> (() -> html)
94-
-> (XPub -> WalletResourceM a)
95-
-> XPub
87+
-> (WalletResourceM a)
9688
-> Handler html
97-
initWalletWithXPub l@SessionLayer{sendSSE} alert render initWallet xpub = do
89+
initWalletWithXPub l@SessionLayer{sendSSE} alert render initWallet = do
9890
liftIO $ sendSSE $ Push "wallet"
99-
r <- liftIO $ catchRunWalletResourceM l (initWallet xpub)
91+
r <- liftIO $ catchRunWalletResourceM l initWallet
10092
case r of
10193
Left e -> pure $ alert $ BL.pack $ show e
10294
Right _ -> do
@@ -105,39 +97,33 @@ initWalletWithXPub l@SessionLayer{sendSSE} alert render initWallet xpub = do
10597

10698
postMnemonicWallet
10799
:: SessionLayer WalletResource
108-
-> (XPub -> WalletResourceM a)
100+
-> (XPub -> Customer -> WalletResourceM a)
109101
-> (BL.ByteString -> html)
110102
-> (() -> html)
111-
-> Value
103+
-> PostWalletViaMenmonic
112104
-> Handler html
113-
postMnemonicWallet l initWallet alert render v = do
114-
mnemonic <-
115-
handleParseRequestError
116-
$ parsePostWalletRequest v
117-
let xpub =
118-
Addresses.toXPub
119-
$ Addresses.generate (T.encodeUtf8 mnemonic)
120-
initWalletWithXPub l alert render initWallet xpub
121-
122-
parsePostWalletRequest :: Value -> Either String Text
123-
parsePostWalletRequest = parseEither
124-
. withObject "create wallet request"
125-
$ \o -> o .: "mnemonicSentence"
105+
postMnemonicWallet
106+
l
107+
initWallet
108+
alert
109+
render
110+
(PostWalletViaMenmonic mnemonic users) = do
111+
let xpub =
112+
Addresses.toXPub
113+
$ Addresses.generate (T.encodeUtf8 mnemonic)
114+
initWalletWithXPub l alert render $ initWallet xpub $ fromIntegral users
126115

127116
unBase64 :: ByteString -> Either String ByteString
128117
unBase64 = convertFromBase Base64
129118

130119
postXPubWallet
131120
:: SessionLayer WalletResource
132-
-> (XPub -> WalletResourceM a)
121+
-> (XPub -> Customer -> WalletResourceM a)
133122
-> (BL.ByteString -> html)
134123
-> (() -> html)
135-
-> Value
124+
-> PostWalletViaXPub
136125
-> Handler html
137-
postXPubWallet l initWallet alert render v = do
138-
xpubText <-
139-
handleParseRequestError
140-
$ parsePostXPubRequest v
126+
postXPubWallet l initWallet alert render (PostWalletViaXPub xpubText users) = do
141127
case T.encodeUtf8 xpubText of
142128
xpubByteString -> case unBase64 xpubByteString of
143129
Left e -> pure $ alert $ BL.pack $ "Invalid base64: " <> e
@@ -147,12 +133,10 @@ postXPubWallet l initWallet alert render v = do
147133
$ alert
148134
$ BL.pack
149135
$ "Invalid xpub: " <> show xpubText
150-
Just xpub -> initWalletWithXPub l alert render initWallet xpub
151-
152-
parsePostXPubRequest :: Value -> Either String Text
153-
parsePostXPubRequest = parseEither
154-
. withObject "create wallet from xpub request"
155-
$ \o -> o .: "xpub"
136+
Just xpub ->
137+
initWalletWithXPub l alert render
138+
$ initWallet xpub
139+
$ fromIntegral users
156140

157141
walletIsLoading
158142
:: SessionLayer WalletResource

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ import Lucid
6565
, class_
6666
, div_
6767
, hidden_
68+
, hr_
6869
, id_
70+
, section_
6971
)
7072

7173
import qualified Data.ByteString.Char8 as B8
@@ -110,13 +112,16 @@ walletElementH alert = \case
110112
simpleField "Public Key" $ pubKeyH xpub
111113
simpleField "Customer Discovery" $ toHtml $ toText customers
112114
WalletAbsent -> runWHtml Deposit $ do
113-
newWalletFromMnemonicH walletMnemonicLink
115+
section_
116+
$ newWalletFromMnemonicH walletMnemonicLink
114117
$ PostWalletConfig
115118
{ walletDataLink = walletPostMnemonicLink
116119
, passwordVisibility = Just Hidden
117120
, responseTarget = "#post-response"
118121
}
119-
newWalletFromXPubH
122+
hr_ mempty
123+
section_
124+
$ newWalletFromXPubH
120125
$ PostWalletConfig
121126
{ walletDataLink = walletPostXPubLink
122127
, passwordVisibility = Just Hidden

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ serveUI tr ul env dbDir config _ nl bs =
172172
NodeSource{} -> Node
173173
_ = networkInfoH
174174
wsl f = withSessionLayer ul $ \l -> f l
175-
initWallet l xpub = initXPubWallet env dbDir trs xpub 500000
175+
initWallet l = initXPubWallet tr env dbDir trs
176176
where
177177
trs :: Tracer IO (ResourceStatus ErrDatabase WalletInstance)
178178
trs = Tracer $ \_e -> do

0 commit comments

Comments
 (0)