Skip to content

Commit

Permalink
Merge #790
Browse files Browse the repository at this point in the history
790: Provide skeletal API endpoints for functions on Byron-style wallets.   r=piotr-iohk a=jonathanknowles

# Issue Number

#774 
#775 
#776 
#777 

# Overview

This PR provides skeletal API endpoints for a subset of the functions on Byron-style wallets.

The goal of this PR is to establish the general direction, including types and module organization, and not to provide a complete implementation. (This will be provided in further PRs.)

- [x] Separated the API into a core API (non-legacy) and a compatibility API (includes operations on Byron wallets).
- [x] Provided API types for a subset of functions on Byron-style wallets.
- [x] Provided compatibility API server implementation that returns HTTP 501 (Not Implemented) for each of the functions on Byron-style wallets.
- [x] Added appropriate definitions to the Swagger API specification.

Co-authored-by: Jonathan Knowles <[email protected]>
  • Loading branch information
iohk-bors[bot] and jonathanknowles authored Oct 3, 2019
2 parents ba623d6 + 16f395f commit 6ae6c2c
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/cli/src/Cardano/CLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ import Cardano.BM.Setup
import Cardano.BM.Trace
( Trace, appendName, logAlert, logInfo )
import Cardano.Wallet.Api
( Api )
( CoreApi )
import Cardano.Wallet.Api.Server
( Listen (..) )
import Cardano.Wallet.Api.Types
Expand Down Expand Up @@ -934,7 +934,7 @@ walletClient :: forall t. (DecodeAddress t, EncodeAddress t) => WalletClient t
walletClient =
let
addresses :<|> wallets :<|> transactions :<|> pools =
client (Proxy @("v2" :> Api t))
client (Proxy @("v2" :> CoreApi t))

_listAddresses =
addresses
Expand Down
56 changes: 53 additions & 3 deletions lib/core/src/Cardano/Wallet/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,29 @@
{-# LANGUAGE TypeOperators #-}

module Cardano.Wallet.Api
( -- * Types
( -- * API
Api

-- * Core API
, CoreApi
, Addresses
, Wallets
, Transactions
, StakePools
, Any

-- * Compatibility API
, CompatibilityApi

-- * Api Layer
, ApiLayer (..)
, HasWorkerRegistry
, workerRegistry
, HasDBFactory
, dbFactory

-- * Miscellaneous Types
, Any

) where

import Prelude
Expand All @@ -36,13 +45,15 @@ import Cardano.Wallet
( WalletLayer (..) )
import Cardano.Wallet.Api.Types
( ApiAddress
, ApiByronWallet
, ApiFee
, ApiStakePool
, ApiT
, ApiTransaction
, ApiTxId
, ApiUtxoStatistics
, ApiWallet
, ByronWalletPostData
, Iso8601Time
, PostExternalTransactionData
, PostTransactionData
Expand Down Expand Up @@ -96,7 +107,13 @@ import Servant.API
, ReqBody
)

type Api t = Addresses t :<|> Wallets :<|> Transactions t :<|> StakePools
type Api t = CoreApi t :<|> CompatibilityApi t

{-==============================================================================
Core API
==============================================================================-}

type CoreApi t = Addresses t :<|> Wallets :<|> Transactions t :<|> StakePools

{-------------------------------------------------------------------------------
Addresses
Expand Down Expand Up @@ -221,6 +238,39 @@ type StakePools = ListStakePools
type ListStakePools = "stake-pools"
:> Get '[JSON] [ApiStakePool]

{-==============================================================================
Compatibility API
==============================================================================-}

type CompatibilityApi t =
DeleteByronWallet
:<|> GetByronWallet
:<|> ListByronWallets
:<|> PostByronWallet

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/deleteByronWallet
type DeleteByronWallet = "byron"
:> "wallets"
:> Capture "walletId" (ApiT WalletId)
:> DeleteNoContent '[Any] NoContent

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/getByronWallet
type GetByronWallet = "byron"
:> "wallets"
:> Capture "walletId" (ApiT WalletId)
:> Get '[JSON] ApiByronWallet

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/listByronWallets
type ListByronWallets = "byron"
:> "wallets"
:> Get '[JSON] [ApiByronWallet]

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/postByronWallet
type PostByronWallet = "byron"
:> "wallets"
:> ReqBody '[JSON] ByronWalletPostData
:> PostAccepted '[JSON] ApiByronWallet

{-------------------------------------------------------------------------------
Internals
-------------------------------------------------------------------------------}
Expand Down
60 changes: 59 additions & 1 deletion lib/core/src/Cardano/Wallet/Api/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ import Cardano.Wallet.Api
( Addresses
, Api
, ApiLayer (..)
, CompatibilityApi
, CoreApi
, HasDBFactory
, HasWorkerRegistry
, StakePools
Expand All @@ -72,6 +74,7 @@ import Cardano.Wallet.Api.Types
( AddressAmount (..)
, ApiAddress (..)
, ApiBlockData (..)
, ApiByronWallet (..)
, ApiErrorCode (..)
, ApiFee (..)
, ApiStakePool
Expand All @@ -81,6 +84,7 @@ import Cardano.Wallet.Api.Types
, ApiTxInput (..)
, ApiUtxoStatistics (..)
, ApiWallet (..)
, ByronWalletPostData (..)
, Iso8601Time (..)
, PostExternalTransactionData (..)
, PostTransactionData
Expand Down Expand Up @@ -253,7 +257,7 @@ start settings trace socket ctx = do
where
-- | A Servant server for our wallet API
server :: Server (Api t)
server = addresses ctx :<|> wallets ctx :<|> transactions ctx :<|> pools ctx
server = coreApiServer ctx :<|> compatibilityApiServer ctx

application :: Application
application = serve (Proxy @("v2" :> Api t)) server
Expand Down Expand Up @@ -284,6 +288,24 @@ withListeningSocket portOpt = bracket acquire release
-- TODO: make configurable, default to secure for now.
hostPreference = "127.0.0.1"

{-==============================================================================
Core API
==============================================================================-}

coreApiServer
:: forall ctx s t k.
( DefineTx t
, KeyToAddress t k
, Buildable (ErrValidateSelection t)
, k ~ SeqKey
, s ~ SeqState t
, ctx ~ ApiLayer s t k
)
=> ctx
-> Server (CoreApi t)
coreApiServer ctx =
addresses ctx :<|> wallets ctx :<|> transactions ctx :<|> pools ctx

{-------------------------------------------------------------------------------
Wallets
-------------------------------------------------------------------------------}
Expand Down Expand Up @@ -602,6 +624,42 @@ listPools
-> Handler [ApiStakePool]
listPools _ctx = throwError err501

{-==============================================================================
Compatibility API
==============================================================================-}

compatibilityApiServer
:: ctx
-> Server (CompatibilityApi t)
compatibilityApiServer ctx =
deleteByronWallet ctx
:<|> getByronWallet ctx
:<|> listByronWallets ctx
:<|> postByronWallet ctx

deleteByronWallet
:: ctx
-> ApiT WalletId
-> Handler NoContent
deleteByronWallet _ _ = throwError err501

getByronWallet
:: ctx
-> ApiT WalletId
-> Handler ApiByronWallet
getByronWallet _ _ = throwError err501

listByronWallets
:: ctx
-> Handler [ApiByronWallet]
listByronWallets _ = throwError err501

postByronWallet
:: ctx
-> ByronWalletPostData
-> Handler ApiByronWallet
postByronWallet _ _ = throwError err501

{-------------------------------------------------------------------------------
Helpers
-------------------------------------------------------------------------------}
Expand Down
16 changes: 16 additions & 0 deletions lib/core/src/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module Cardano.Wallet.Api.Types

-- * API Types (Byron)
, ApiByronWallet (..)
, ByronWalletPostData (..)

-- * Polymorphic Types
, ApiT (..)
Expand Down Expand Up @@ -203,6 +204,16 @@ data WalletPostData = WalletPostData
, passphrase :: !(ApiT (Passphrase "encryption"))
} deriving (Eq, Generic, Show)

data ByronWalletPostData = ByronWalletPostData
{ mnemonicSentence :: !(ApiMnemonicT '[12] "seed")
-- It was theoretically possible to create a Byron wallet with a mnemonic
-- sentence longer than 12 words. However, in practice, previous wallets
-- only ever supported mnemonic sentences of exactly 12 words. Hence, we
-- restrict ourselves to this length.
, name :: !(ApiT WalletName)
, passphrase :: !(ApiT (Passphrase "encryption"))
} deriving (Eq, Generic, Show)

newtype WalletPutData = WalletPutData
{ name :: (Maybe (ApiT WalletName))
} deriving (Eq, Generic, Show)
Expand Down Expand Up @@ -400,6 +411,11 @@ instance FromJSON WalletPostData where
instance ToJSON WalletPostData where
toJSON = genericToJSON defaultRecordTypeOptions

instance FromJSON ByronWalletPostData where
parseJSON = genericParseJSON defaultRecordTypeOptions
instance ToJSON ByronWalletPostData where
toJSON = genericToJSON defaultRecordTypeOptions

instance FromJSON WalletPutData where
parseJSON = genericParseJSON defaultRecordTypeOptions
instance ToJSON WalletPutData where
Expand Down
9 changes: 9 additions & 0 deletions lib/core/test/unit/Cardano/Wallet/Api/TypesSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import Cardano.Wallet.Api.Types
, ApiTxInput (..)
, ApiUtxoStatistics (..)
, ApiWallet (..)
, ByronWalletPostData (..)
, Iso8601Time (..)
, PostExternalTransactionData (..)
, PostTransactionData (..)
Expand Down Expand Up @@ -218,6 +219,7 @@ spec = do
jsonRoundtripAndGolden $ Proxy @(PostTransactionData DummyTarget)
jsonRoundtripAndGolden $ Proxy @(PostTransactionFeeData DummyTarget)
jsonRoundtripAndGolden $ Proxy @WalletPostData
jsonRoundtripAndGolden $ Proxy @ByronWalletPostData
jsonRoundtripAndGolden $ Proxy @WalletPutData
jsonRoundtripAndGolden $ Proxy @WalletPutPassphraseData
jsonRoundtripAndGolden $ Proxy @(ApiT (Hash "Tx"))
Expand Down Expand Up @@ -645,6 +647,10 @@ instance Arbitrary WalletPostData where
arbitrary = genericArbitrary
shrink = genericShrink

instance Arbitrary ByronWalletPostData where
arbitrary = genericArbitrary
shrink = genericShrink

instance Arbitrary WalletPutData where
arbitrary = genericArbitrary
shrink = genericShrink
Expand Down Expand Up @@ -954,6 +960,9 @@ instance ToSchema ApiTxId where
instance ToSchema WalletPostData where
declareNamedSchema _ = declareSchemaForDefinition "ApiWalletPostData"

instance ToSchema ByronWalletPostData where
declareNamedSchema _ = declareSchemaForDefinition "ApiByronWalletPostData"

instance ToSchema WalletPutData where
declareNamedSchema _ = declareSchemaForDefinition "ApiWalletPutData"

Expand Down
Loading

0 comments on commit 6ae6c2c

Please sign in to comment.