Skip to content

Commit

Permalink
Factor out REST module in a separate sublibrary together with code fr…
Browse files Browse the repository at this point in the history
…om the ui to be reused in json http
  • Loading branch information
paolino committed Sep 19, 2024
1 parent 7afb798 commit 24a6b6f
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 133 deletions.
63 changes: 38 additions & 25 deletions lib/customer-deposit-wallet/customer-deposit-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ common language
common opts-lib
ghc-options:
-Wall -Wcompat -Wredundant-constraints -Wincomplete-uni-patterns
-Wincomplete-record-updates
-Wincomplete-record-updates -Wunused-imports -Wunused-packages

if flag(release)
ghc-options: -O2 -Werror
Expand All @@ -35,58 +35,44 @@ common opts-exe
import: opts-lib
ghc-options: -threaded -rtsopts


common no-delta-table-on-windows
if !os(windows)
build-depends:
, delta-table
, sqlite-simple
other-modules:
Cardano.Wallet.Deposit.IO.DB.Real
build-depends: delta-table
other-modules: Cardano.Wallet.Deposit.IO.DB.Real

else
other-modules:
Cardano.Wallet.Deposit.IO.DB.Stub
other-modules: Cardano.Wallet.Deposit.IO.DB.Stub

flag release
description: Enable optimization and `-Werror`
default: False
manual: True

library
import: language, opts-lib, no-delta-table-on-windows
hs-source-dirs: src
import: language, opts-lib, no-delta-table-on-windows
hs-source-dirs: src
build-depends:
, async
, base
, bytestring
, cardano-addresses
, cardano-binary
, cardano-crypto
, cardano-ledger-api
, cardano-ledger-binary
, cardano-ledger-byron
, cardano-strict-containers
, cardano-wallet
, cardano-wallet-network-layer
, cardano-wallet-read ==0.2024.8.27
, containers
, contra-tracer
, crypto-primitives
, customer-deposit-wallet-pure
, delta-store
, delta-types
, directory
, filepath
, io-classes
, iohk-monitoring-extra
, memory
, microlens
, OddWord
, serialise
, text
, time
, transformers

reexported-modules: Cardano.Wallet.Address.BIP32
exposed-modules:
Cardano.Wallet.Deposit.IO
Cardano.Wallet.Deposit.IO.DB
Expand All @@ -99,7 +85,6 @@ library
Cardano.Wallet.Deposit.Pure.UTxO
Cardano.Wallet.Deposit.Pure.UTxOHistory
Cardano.Wallet.Deposit.Read
Cardano.Wallet.Deposit.REST
Cardano.Wallet.Deposit.Write

test-suite scenario
Expand All @@ -125,7 +110,7 @@ test-suite scenario
Test.Scenario.Wallet.Deposit.Exchanges
Test.Scenario.Wallet.Deposit.Run

library customer-deposit-wallet-http
library http
import: language, opts-lib
hs-source-dirs: http
build-depends:
Expand Down Expand Up @@ -155,6 +140,33 @@ library customer-deposit-wallet-http
Cardano.Wallet.Deposit.HTTP.Types.JSON.Encoding
Cardano.Wallet.Deposit.HTTP.Types.OpenAPI

library rest
import: language, opts-lib
visibility: public
hs-source-dirs: rest
build-depends:
, base
, bytestring
, cardano-addresses
, cardano-crypto
, containers
, contra-tracer
, crypto-primitives
, customer-deposit-wallet
, delta-store
, directory
, filepath
, memory
, serialise
, servant-server
, text
, transformers

exposed-modules:
Cardano.Wallet.Deposit.REST
Cardano.Wallet.Deposit.REST.Catch
Cardano.Wallet.Deposit.REST.Wallet.Create

test-suite unit
import: language, opts-exe
type: exitcode-stdio-1.0
Expand All @@ -169,7 +181,8 @@ test-suite unit
, cardano-wallet-test-utils
, contra-tracer
, customer-deposit-wallet
, customer-deposit-wallet:customer-deposit-wallet-http
, customer-deposit-wallet:http
, customer-deposit-wallet:rest
, directory
, hspec
, hspec-golden
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Cardano.Wallet.Deposit.REST.Catch
( catchRunWalletResourceM
)
where

import Prelude

import Cardano.Wallet.Deposit.REST
( WalletResource
, WalletResourceM
, runWalletResourceM
)
import Control.Monad.IO.Class
( MonadIO (..)
)
import Control.Monad.Trans.Except
( throwE
)
import Servant
( Handler (..)
, ServerError (..)
)

import qualified Data.ByteString.Lazy.Char8 as BL

-- | Catch and run a 'WalletResourceM' action, converting any exceptions to
-- 'ServerError'.
catchRunWalletResourceM
:: WalletResource
-> ServerError
-> WalletResourceM a
-> Handler a
catchRunWalletResourceM s se f = do
r <- liftIO $ runWalletResourceM f s
case r of
Right a -> pure a
Left e -> Handler $ throwE $ se{errBody = BL.pack $ show e}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}

module Cardano.Wallet.Deposit.REST.Wallet.Create
( PostWalletViaMenmonic (..)
, PostWalletViaXPub (..)
, decodeXPub
, xpubFromMnemonics
, encodeXPub
)
where

import Prelude

import Cardano.Address.Derivation
( XPub
, generate
, toXPub
, xpubFromBytes
, xpubToBytes
)
import Data.ByteArray.Encoding
( Base (Base64)
, convertFromBase
, convertToBase
)
import Data.ByteString.Char8
( ByteString
)
import Data.Text
( Text
)
import GHC.Generics
( Generic
)

import qualified Data.Text.Encoding as T

-- | Data fpr a request to create a wallet via a mnemonic.
data PostWalletViaMenmonic = PostWalletViaMenmonic
{ mnemonics :: Text
, users :: Int
}
deriving (Generic)

-- | Data for a request to create a wallet via an extended public key.
data PostWalletViaXPub = PostWalletViaXPub
{ xpub :: Text
, users :: Int
}
deriving (Generic)

unBase64 :: ByteString -> Either String ByteString
unBase64 = convertFromBase Base64

-- | Decode an extended public key from a base64-encoded text.
decodeXPub :: Text -> Either String (Maybe XPub)
decodeXPub = fmap xpubFromBytes . unBase64 . T.encodeUtf8

-- | Encode an extended public key to a base64-encoded text.
encodeXPub :: XPub -> Text
encodeXPub = T.decodeUtf8 . convertToBase Base64 . xpubToBytes

-- | Generate an extended public key from a mnemonic.
-- this is not what one wants to use in production
xpubFromMnemonics :: Text -> XPub
xpubFromMnemonics = toXPub . generate . T.encodeUtf8
1 change: 1 addition & 0 deletions lib/exe/cardano-wallet-exe.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ library
, cardano-wallet-ui
, contra-tracer
, customer-deposit-wallet
, customer-deposit-wallet:rest
, data-default
, directory
, extra
Expand Down
10 changes: 6 additions & 4 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 Down Expand Up @@ -96,6 +98,7 @@ library
, contra-tracer
, cookie
, customer-deposit-wallet
, customer-deposit-wallet:rest
, exceptions
, generic-lens
, http-api-data
Expand All @@ -114,7 +117,6 @@ library
, text
, text-class
, time
, transformers
, unliftio

hs-source-dirs: src
Expand Down
23 changes: 4 additions & 19 deletions lib/ui/src/Cardano/Wallet/UI/Deposit/API.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
Expand All @@ -17,6 +16,10 @@ module Cardano.Wallet.UI.Deposit.API where

import Prelude

import Cardano.Wallet.Deposit.REST.Wallet.Create
( PostWalletViaMenmonic
, PostWalletViaXPub
)
import Cardano.Wallet.UI.Common.API
( Image
, SessionedHtml
Expand All @@ -29,12 +32,6 @@ import Cardano.Wallet.UI.Common.Handlers.SSE
import Cardano.Wallet.UI.Cookies
( CookieRequest
)
import Data.Text
( Text
)
import GHC.Generics
( Generic
)
import Servant
( Delete
, FormUrlEncoded
Expand All @@ -54,20 +51,8 @@ import Web.FormUrlEncoded

import qualified Data.ByteString.Lazy as BL

data PostWalletViaMenmonic = PostWalletViaMenmonic
{ mnemonics :: Text
, users :: Int
}
deriving (Generic)

instance FromForm PostWalletViaMenmonic

data PostWalletViaXPub = PostWalletViaXPub
{ xpub :: Text
, users :: Int
}
deriving (Generic)

instance FromForm PostWalletViaXPub

-- | Pages endpoints
Expand Down
13 changes: 3 additions & 10 deletions lib/ui/src/Cardano/Wallet/UI/Deposit/Handlers/Lib.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,21 @@ import Control.Monad.Reader
import Control.Monad.Trans
( MonadIO (..)
)
import Control.Monad.Trans.Except
( throwE
)
import Servant
( Handler (..)
, ServerError (..)
, err500
)

import qualified Cardano.Wallet.Deposit.REST.Catch as REST
import qualified Data.ByteString.Lazy.Char8 as BL

catchRunWalletResourceM
:: SessionLayer WalletResource
-> WalletResourceM a
-> Handler a
catchRunWalletResourceM layer f = do
r <- liftIO $ do
s <- view stateL <$> state layer
runWalletResourceM f s
case r of
Right a -> pure a
Left e -> Handler $ throwE $ err500{errBody = BL.pack $ show e}
r <- liftIO $ view stateL <$> state layer
REST.catchRunWalletResourceM r err500 f

catchRunWalletResourceHtml
:: SessionLayer WalletResource
Expand Down
Loading

0 comments on commit 24a6b6f

Please sign in to comment.