Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Cardano.Wallet.Jormungandr.Network.Api #325

Merged
merged 1 commit into from
May 27, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions lib/jormungandr/src/Cardano/Wallet/Network/Jormungandr/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}

{-# OPTIONS_GHC -fno-warn-orphans #-} -- for content types

-- |
-- Copyright: © 2018-2019 IOHK
-- License: MIT
Expand Down Expand Up @@ -39,6 +38,7 @@ import Data.Text.Encoding
import Servant.API
( (:<|>)
, (:>)
, Accept (..)
, Capture
, Get
, MimeUnrender (..)
Expand All @@ -48,10 +48,9 @@ import Servant.API
, ReqBody
, ToHttpApiData (..)
)
import Servant.API.ContentTypes
( OctetStream, PlainText )

import qualified Data.ByteString.Lazy as BL
import qualified Servant.API.ContentTypes as Servant


api :: Proxy Api
Expand All @@ -67,32 +66,38 @@ type GetBlock
:> "v0"
:> "block"
:> Capture "blockHeaderHash" BlockId
:> Get '[OctetStream] Block
:> Get '[JormungandrBinary] Block

-- | Retrieve a list of 'n' block decendant ids, sorted from closest to
-- | Retrieve 'n' decendants of a given block, sorted from closest to
-- farthest.
--
-- There might also exist fewer than 'n' decendants.
--
-- For n=3 we might have:
--
-- > [genesis] ... -- [b] -- [b+1] -- [b+2] -- [b+3] -- ... -- [tip]
-- > \ \ \
-- > parent +--- decendants ---+
type GetBlockDecendantIds
= "api"
:> "v0"
:> "block"
:> Capture "blockId" BlockId
:> QueryParam "count" Int
:> Get '[OctetStream] [BlockId]
:> Get '[JormungandrBinary] [BlockId]

-- | Retrieve the header of the latest known block.
type GetTipId
= "api"
:> "v0"
:> "tip"
:> Get '[PlainText] BlockId
:> Get '[Hex] BlockId

type PostSignedTx
= "api"
:> "v0"
:> "transaction"
:> ReqBody '[OctetStream] SignedTx
:> ReqBody '[JormungandrBinary] SignedTx
:> Post '[NoContent] NoContent

-- TODO: Replace SignedTx with something real
Expand All @@ -107,10 +112,27 @@ instance ToHttpApiData BlockId where
instance FromBinary BlockId where
get = BlockId . Hash <$> getByteString 32

instance MimeUnrender PlainText BlockId where
instance MimeUnrender Hex BlockId where
mimeUnrender _ bs =
BlockId . Hash <$> convertFromBase Base16 (BL.toStrict bs)

-- Orphan instance
instance FromBinary a => MimeUnrender OctetStream a where

{-------------------------------------------------------------------------------
Content Types
-------------------------------------------------------------------------------}

-- | Represents the binary format of Jörmungandr.
data JormungandrBinary

instance Accept JormungandrBinary where
contentType _ = contentType $ Proxy @Servant.OctetStream

instance FromBinary a => MimeUnrender JormungandrBinary a where
mimeUnrender _ bs = Right $ runGet get bs


data Hex

-- | Represents data rendered to hexadecimal text.
instance Accept Hex where
contentType _ = contentType $ Proxy @Servant.PlainText