-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out REST module in a separate sublibrary together with code fr…
…om the ui to be reused in json http
- Loading branch information
Showing
9 changed files
with
185 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
37 changes: 37 additions & 0 deletions
37
lib/customer-deposit-wallet/rest/Cardano/Wallet/Deposit/REST/Catch.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
67 changes: 67 additions & 0 deletions
67
lib/customer-deposit-wallet/rest/Cardano/Wallet/Deposit/REST/Wallet/Create.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.