Skip to content

Commit

Permalink
Add query customer address form
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Sep 18, 2024
1 parent 0656caa commit dc93546
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 20 deletions.
11 changes: 8 additions & 3 deletions lib/ui/cardano-wallet-ui.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ common language
OverloadedStrings

common opts-lib
ghc-options: -Wall -Wcompat -Wredundant-constraints -Wunused-packages
-Wunused-imports -Wincomplete-uni-patterns -Wincomplete-record-updates
ghc-options:
-Wall -Wcompat -Wredundant-constraints -Wunused-packages
-Wunused-imports -Wincomplete-uni-patterns
-Wincomplete-record-updates

if flag(release)
ghc-options: -O2 -Werror
Expand All @@ -36,8 +38,8 @@ library
exposed-modules:
Cardano.Wallet.UI.Common.API
Cardano.Wallet.UI.Common.Handlers.Lib
Cardano.Wallet.UI.Common.Handlers.Settings
Cardano.Wallet.UI.Common.Handlers.Session
Cardano.Wallet.UI.Common.Handlers.Settings
Cardano.Wallet.UI.Common.Handlers.SSE
Cardano.Wallet.UI.Common.Handlers.State
Cardano.Wallet.UI.Common.Handlers.Wallet
Expand All @@ -63,6 +65,7 @@ library
Cardano.Wallet.UI.Deposit.Html.Pages.Page
Cardano.Wallet.UI.Deposit.Html.Pages.Wallet
Cardano.Wallet.UI.Deposit.Server
Cardano.Wallet.UI.Lib.Address
Cardano.Wallet.UI.Lib.ListOf
Cardano.Wallet.UI.Shelley.API
Cardano.Wallet.UI.Shelley.Handlers.Addresses
Expand All @@ -84,6 +87,8 @@ library
, aeson
, aeson-pretty
, base
, bech32
, bech32-th
, bytestring
, cardano-addresses
, cardano-ledger-core
Expand Down
57 changes: 40 additions & 17 deletions lib/ui/src/Cardano/Wallet/UI/Deposit/Html/Pages/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import Cardano.Address.Derivation
( XPub
, xpubToBytes
)
import Cardano.Ledger.Address
( unCompactAddr
)
import Cardano.Wallet.Deposit.IO
( WalletPublicIdentity (..)
)
Expand Down Expand Up @@ -62,6 +59,9 @@ import Cardano.Wallet.UI.Deposit.API
, walletPostMnemonicLink
, walletPostXPubLink
)
import Cardano.Wallet.UI.Lib.Address
( encodeMainnetAddress
)
import Cardano.Wallet.UI.Type
( WHtml
, WalletType (..)
Expand Down Expand Up @@ -108,7 +108,7 @@ import Lucid.Html5

import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy.Char8 as BL
import qualified Data.ByteString.Short as B
import qualified Data.Text as T

data WalletPresent
= WalletPresent WalletPublicIdentity
Expand All @@ -134,23 +134,27 @@ base64 = convertToBase Base64

customerAddressH :: Monad m => Address -> HtmlT m ()
customerAddressH addr = div_ [class_ "row"] $ do
div_ [id_ "address", hidden_ "false"] $ toHtml addr'
div_ [class_ "col-6"] $ toHtml $ headAndTail 4 $ B8.dropEnd 1 addr'
div_ [class_ "col-6"]
$ copyButton "address"
where addr' = base64 $ B.fromShort $ unCompactAddr addr
div_ [id_ "address", hidden_ "true"] $ toHtml encodedAddr
div_ [class_ "col-8"] $ toHtml addrShortened
div_ [class_ "col-4"] $ copyButton "address"
where
encodedAddr = encodeMainnetAddress addr
addrShortened =
T.take 10 (T.drop 5 encodedAddr)
<> " .. "
<> T.takeEnd 10 encodedAddr

pubKeyH :: Monad m => XPub -> HtmlT m ()
pubKeyH xpub = div_ [class_ "row"] $ do
div_ [id_ "public_key", hidden_ "false"] $ toHtml xpubByteString
div_ [class_ "col-6"] $ toHtml $ headAndTail 4 $ B8.dropEnd 1 xpubByteString
div_ [class_ "col-6"]
div_ [id_ "public_key", hidden_ "true"] $ toHtml xpubByteString
div_ [class_ "col-8"] $ toHtml $ headAndTail 4 $ B8.dropEnd 1 xpubByteString
div_ [class_ "col-4"]
$ copyButton "public_key"
where
xpubByteString = base64 $ xpubToBytes xpub

headAndTail :: Int -> ByteString -> ByteString
headAndTail n t = B8.take n t <> ".." <> B8.takeEnd n t
headAndTail n t = B8.take n t <> " .. " <> B8.takeEnd n t

deleteWalletButtonH :: Html ()
deleteWalletButtonH =
Expand Down Expand Up @@ -183,10 +187,29 @@ deleteWalletModalH =
walletElementH :: (BL.ByteString -> Html ()) -> WalletPresent -> Html ()
walletElementH alert = \case
WalletPresent (WalletPublicIdentity xpub customers) -> do
record $ do
simpleField "Public Key" $ pubKeyH xpub
simpleField "Customer Discovery" $ toHtml $ toText customers
div_ [class_ "row"] $ do
div_ [class_ "row mt-5 "] $ do
h5_ [class_ "text-center"] "Wallet Details"
div_ [class_ "col"] $ record $ do
simpleField "Public Key" $ pubKeyH xpub
simpleField "Customer Discovery" $ toHtml $ toText customers
div_ [class_ "row mt-5"] $ do
h5_ [class_ "text-center"] "Query Address"
div_ [class_ "col"] $ record $ do
simpleField "Customer number"
$ input_
[ type_ "number"
, hxTarget_ "#customer-address"
, class_ "form-control"
, hxTrigger_ "change"
, hxPost_ $ linkText customerAddressLink
, min_ "0"
, max_ $ toText $ customers - 1
, step_ "1"
, name_ "customer"
]
simpleField "Address" $ div_ [id_ "customer-address"] mempty
div_ [class_ "row mt-5"] $ do
h5_ [class_ "text-center"] "Actions"
div_ [class_ "col"] $ do
deleteWalletButtonH
div_ [id_ "delete-result"] mempty
Expand Down
29 changes: 29 additions & 0 deletions lib/ui/src/Cardano/Wallet/UI/Lib/Address.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{-# LANGUAGE QuasiQuotes #-}
module Cardano.Wallet.UI.Lib.Address
where

import Prelude

import Cardano.Ledger.Address
( unCompactAddr
)
import Cardano.Wallet.Deposit.Read
( Address
)
import Codec.Binary.Bech32
( dataPartFromBytes
)
import Data.Text
( Text
)

import qualified Codec.Binary.Bech32 as Bech32
import qualified Codec.Binary.Bech32.TH as Bech32
import qualified Data.ByteString.Short as B8

encodeMainnetAddress :: Address -> Text
encodeMainnetAddress addr = bech32
where
bytes = B8.fromShort $ unCompactAddr addr
bech32 = Bech32.encodeLenient hrp (dataPartFromBytes bytes)
hrp = [Bech32.humanReadablePart|addr|]

0 comments on commit dc93546

Please sign in to comment.