Skip to content

Commit

Permalink
Add wallet creation to json http api and use REST interface
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Sep 19, 2024
1 parent fca51e6 commit 0ea8c95
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 19 deletions.
2 changes: 2 additions & 0 deletions lib/customer-deposit-wallet/customer-deposit-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,150 @@
-- 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
------------------------------------------------------------------------------}
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
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
, (:<|>)
Expand All @@ -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)

0 comments on commit 0ea8c95

Please sign in to comment.