diff --git a/lib/customer-deposit-wallet/customer-deposit-wallet.cabal b/lib/customer-deposit-wallet/customer-deposit-wallet.cabal index 186c00f73de..c4061aefa72 100644 --- a/lib/customer-deposit-wallet/customer-deposit-wallet.cabal +++ b/lib/customer-deposit-wallet/customer-deposit-wallet.cabal @@ -119,7 +119,9 @@ library http , base , bytestring , cardano-wallet-read + , contra-tracer , customer-deposit-wallet + , customer-deposit-wallet:rest , http-media , insert-ordered-containers , lens diff --git a/lib/customer-deposit-wallet/http/Cardano/Wallet/Deposit/HTTP/Implementation.hs b/lib/customer-deposit-wallet/http/Cardano/Wallet/Deposit/HTTP/Implementation.hs index a944af7e8bd..f9e3d80903d 100644 --- a/lib/customer-deposit-wallet/http/Cardano/Wallet/Deposit/HTTP/Implementation.hs +++ b/lib/customer-deposit-wallet/http/Cardano/Wallet/Deposit/HTTP/Implementation.hs @@ -3,28 +3,61 @@ -- License: Apache-2.0 -- -- Implementation of our HTTP API. --- module Cardano.Wallet.Deposit.HTTP.Implementation ( api - , implementation + , server ) - where +where + +import Prelude import Cardano.Wallet.Deposit.HTTP.Types.API ( CustomerAPI ) +import Cardano.Wallet.Deposit.HTTP.Types.JSON + ( Address + , ApiT (..) + , Customer + ) +import Cardano.Wallet.Deposit.IO + ( WalletBootEnv + ) +import Cardano.Wallet.Deposit.REST + ( WalletResource + , WalletResourceM + , createAddress + , listCustomers + ) +import Cardano.Wallet.Deposit.REST.Catch + ( catchRunWalletResourceM + ) +import Cardano.Wallet.Deposit.REST.Wallet.Create + ( PostWalletViaMenmonic (..) + , PostWalletViaXPub (..) + , decodeXPub + , xpubFromMnemonics + ) +import Control.Tracer + ( Tracer + , nullTracer + ) +import Data.Functor + ( ($>) + ) import Data.Proxy ( Proxy (..) ) import Servant - ( (:<|>) (..) + ( Handler + , NoContent (..) + , err404 + , (:<|>) (..) ) import Servant.Server ( Server ) -import qualified Cardano.Wallet.Deposit.HTTP.Endpoints as HTTP -import qualified Cardano.Wallet.Deposit.IO as Wallet +import qualified Cardano.Wallet.Deposit.REST as REST {----------------------------------------------------------------------------- Types @@ -32,7 +65,88 @@ import qualified Cardano.Wallet.Deposit.IO as Wallet api :: Proxy CustomerAPI api = Proxy -implementation :: Wallet.WalletInstance -> Server CustomerAPI -implementation w = - HTTP.listCustomers w - :<|> HTTP.createAddress w +server + :: Tracer IO String + -> FilePath + -> WalletBootEnv IO + -> WalletResource + -> Server CustomerAPI +server tr dbDir wb r = + listCustomerH r + :<|> createAddressH r + :<|> createWalletViaMnemonic tr dbDir wb r + :<|> createWalletViaXPub tr dbDir wb r + +createWalletViaMnemonic + :: Tracer IO String + -> FilePath + -> WalletBootEnv IO + -> WalletResource + -> PostWalletViaMenmonic + -> Handler NoContent +createWalletViaMnemonic + tracer + dir + boot + resource + (PostWalletViaMenmonic mnemonics' users') = + onlyOnWalletIntance resource initWallet $> NoContent + where + initWallet :: WalletResourceM () + initWallet = + REST.initXPubWallet + tracer + boot + dir + nullTracer + (xpubFromMnemonics mnemonics') + (fromIntegral users') + +createWalletViaXPub + :: Tracer IO String + -> FilePath + -> WalletBootEnv IO + -> WalletResource + -> PostWalletViaXPub + -> Handler NoContent +createWalletViaXPub + tracer + dir + boot + resource + (PostWalletViaXPub xpubText users') = do + result <- onlyOnWalletIntance resource initWallet + case result of + Left e -> fail e + Right () -> pure NoContent + where + initWallet :: WalletResourceM (Either String ()) + initWallet = case decodeXPub xpubText of + Left e -> pure $ Left e + Right (Just xpub') -> + Right + <$> REST.initXPubWallet + tracer + boot + dir + nullTracer + xpub' + (fromIntegral users') + Right Nothing -> pure $ Left "Invalid XPub" + +listCustomerH + :: WalletResource + -> Handler (ApiT [(Customer, Address)]) +listCustomerH r = ApiT <$> onlyOnWalletIntance r listCustomers + +createAddressH + :: WalletResource + -> ApiT Customer + -> Handler (ApiT Address) +createAddressH r (ApiT x) = fmap ApiT $ onlyOnWalletIntance r $ createAddress x + +onlyOnWalletIntance + :: WalletResource + -> WalletResourceM a + -> Handler a +onlyOnWalletIntance wr = catchRunWalletResourceM wr err404 diff --git a/lib/customer-deposit-wallet/http/Cardano/Wallet/Deposit/HTTP/Types/API.hs b/lib/customer-deposit-wallet/http/Cardano/Wallet/Deposit/HTTP/Types/API.hs index 8420dcfab0b..45d4576cfd4 100644 --- a/lib/customer-deposit-wallet/http/Cardano/Wallet/Deposit/HTTP/Types/API.hs +++ b/lib/customer-deposit-wallet/http/Cardano/Wallet/Deposit/HTTP/Types/API.hs @@ -1,16 +1,16 @@ {-# LANGUAGE DataKinds #-} {-# LANGUAGE TypeOperators #-} + -- | -- Copyright: © 2024 Cardano Foundation -- License: Apache-2.0 -- -- Servant Type for our HTTP API. --- module Cardano.Wallet.Deposit.HTTP.Types.API ( CustomerAPI , NetworkAPI ) - where +where import Cardano.Wallet.Deposit.HTTP.Types.JSON ( Address @@ -19,9 +19,18 @@ import Cardano.Wallet.Deposit.HTTP.Types.JSON , Customer , CustomerList ) +import Cardano.Wallet.Deposit.REST.Wallet.Create + ( PostWalletViaMenmonic + , PostWalletViaXPub + ) import Servant.API ( Capture + , Get , JSON + , NoContent + , Post + , Put + , ReqBody , StdMethod (..) , Verb , (:<|>) @@ -33,13 +42,18 @@ import Servant.API ------------------------------------------------------------------------------} type CustomerAPI = - "customers" - :> Verb 'GET 200 '[JSON] (ApiT CustomerList) - :<|> - "customers" + "customers" + :> Get '[JSON] (ApiT CustomerList) + :<|> "customers" :> Capture "customerId" (ApiT Customer) - :> Verb 'PUT 200 '[JSON] (ApiT Address) + :> Put '[JSON] (ApiT Address) + :<|> "menmonic" + :> ReqBody '[JSON] PostWalletViaMenmonic + :> Post '[JSON] NoContent + :<|> ReqBody '[JSON] PostWalletViaXPub + :> Post '[JSON] NoContent type NetworkAPI = - "network" :> "local-tip" - :> Verb 'GET 200 '[JSON] (ApiT ChainPoint) + "network" + :> "local-tip" + :> Verb 'GET 200 '[JSON] (ApiT ChainPoint)