Skip to content

Commit

Permalink
align Api.Types and unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Apr 6, 2021
1 parent 3aef97a commit 34f7f33
Show file tree
Hide file tree
Showing 13 changed files with 9,024 additions and 5,334 deletions.
2 changes: 0 additions & 2 deletions lib/core/src/Cardano/SharedWallet/DB.hs
Expand Up @@ -30,8 +30,6 @@ import Cardano.SharedWallet.SharedState
( SharedWalletInfo )
import Cardano.Wallet.Primitive.Types
( GenesisParameters, WalletId, WalletMetadata )
import Control.Monad.Fail
( MonadFail )
import Control.Monad.IO.Class
( MonadIO )
import Control.Monad.Trans.Except
Expand Down
5 changes: 3 additions & 2 deletions lib/core/src/Cardano/SharedWallet/DB/Sqlite/TH.hs
@@ -1,9 +1,12 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
Expand Down Expand Up @@ -34,8 +37,6 @@ import Data.Time.Clock
( UTCTime )
import Data.Word
( Word32, Word8 )
import Database.Persist.Class
( AtLeastOneUniqueKey (..), OnlyOneUniqueKey (..) )
import Database.Persist.TH
( mkDeleteCascade, mkMigrate, mkPersist, persistLowerCase, share )
import GHC.Generics
Expand Down
53 changes: 46 additions & 7 deletions lib/core/src/Cardano/Wallet/Api/Types.hs
Expand Up @@ -154,6 +154,8 @@ module Cardano.Wallet.Api.Types
, ApiPendingSharedWallet (..)
, ApiActiveSharedWallet (..)
, ApiSharedWalletPostData (..)
, ApiSharedWalletPostDataFromMnemonics (..)
, ApiSharedWalletPostDataFromAccountPubX (..)
, ApiSharedWalletPatchData (..)

-- * Polymorphic Types
Expand Down Expand Up @@ -1047,13 +1049,28 @@ data ApiAccountKey = ApiAccountKey
} deriving (Eq, Generic, Show)
deriving anyclass NFData

data ApiSharedWalletPostData = ApiSharedWalletPostData
{ retrieveMethod :: WalletOrAccountPostData
data ApiSharedWalletPostDataFromMnemonics = ApiSharedWalletPostDataFromMnemonics
{ name :: !(ApiT WalletName)
, mnemonicSentence :: !(ApiMnemonicT (AllowedMnemonics 'Shelley))
, mnemonicSecondFactor :: !(Maybe (ApiMnemonicT (AllowedMnemonics 'SndFactor)))
, passphrase :: !(ApiT (Passphrase "raw"))
, accountIndex :: !(ApiT DerivationIndex)
, paymentScriptTemplate :: !ScriptTemplate
, delegationScriptTemplate :: !(Maybe ScriptTemplate)
} deriving (Eq, Generic, Show)

data ApiSharedWalletPostDataFromAccountPubX = ApiSharedWalletPostDataFromAccountPubX
{ name :: !(ApiT WalletName)
, accountPublicKey :: !ApiAccountPublicKey
, accountIndex :: !(ApiT DerivationIndex)
, paymentScriptTemplate :: !ScriptTemplate
, delegationScriptTemplate :: !(Maybe ScriptTemplate)
} deriving (Eq, Generic, Show)

newtype ApiSharedWalletPostData = ApiSharedWalletPostData
{ wallet :: Either ApiSharedWalletPostDataFromMnemonics ApiSharedWalletPostDataFromAccountPubX
} deriving (Eq, Generic, Show)

data ApiActiveSharedWallet = ApiActiveSharedWallet
{ id :: !(ApiT WalletId)
, name :: !(ApiT WalletName)
Expand Down Expand Up @@ -2276,11 +2293,33 @@ instance {-# OVERLAPS #-} EncodeStakeAddress n
where
toJSON (acct, _) = toJSON . encodeStakeAddress @n . getApiT $ acct

instance FromJSON ApiSharedWalletPostData where
instance FromJSON ApiSharedWalletPostDataFromAccountPubX where
parseJSON = genericParseJSON defaultRecordTypeOptions
instance ToJSON ApiSharedWalletPostData where
instance ToJSON ApiSharedWalletPostDataFromAccountPubX where
toJSON = genericToJSON defaultRecordTypeOptions

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

instance FromJSON ApiSharedWalletPostData where
parseJSON obj = do
mnemonic <-
(withObject "postData" $
\o -> o .:? "mnemonic_sentence" :: Aeson.Parser (Maybe [Text])) obj
case mnemonic of
Nothing -> do
xs <- parseJSON obj :: Aeson.Parser ApiSharedWalletPostDataFromAccountPubX
pure $ ApiSharedWalletPostData $ Right xs
_ -> do
xs <- parseJSON obj :: Aeson.Parser ApiSharedWalletPostDataFromMnemonics
pure $ ApiSharedWalletPostData $ Left xs

instance ToJSON ApiSharedWalletPostData where
toJSON (ApiSharedWalletPostData (Left c))= toJSON c
toJSON (ApiSharedWalletPostData (Right c))= toJSON c

instance FromJSON (ApiT Cosigner) where
parseJSON =
parseJSON >=> eitherToParser . first ShowFmt . fromText
Expand Down Expand Up @@ -2430,10 +2469,10 @@ instance FromText (ApiT Cosigner) where
["",numTxt] -> case T.decimal numTxt of
Right (num,"") -> do
when (num < minBound @Word8 || num > maxBound @Word8) $
fail "Cosigner number should be between '0' and '255'"
Left $ TextDecodingError "Cosigner number should be between '0' and '255'"
pure $ ApiT $ Cosigner num
_ -> fail "Cosigner should be enumerated with number"
_ -> fail "Cosigner should be of form: cosigner#num"
_ -> Left $ TextDecodingError "Cosigner should be enumerated with number"
_ -> Left $ TextDecodingError "Cosigner should be of form: cosigner#num"

{-------------------------------------------------------------------------------
HTTPApiData instances
Expand Down
2 changes: 0 additions & 2 deletions lib/core/src/Cardano/Wallet/DB/Sqlite/TH.hs
Expand Up @@ -39,8 +39,6 @@ import Data.Time.Clock
( UTCTime )
import Data.Word
( Word16, Word32, Word64 )
import Database.Persist.Class
( AtLeastOneUniqueKey (..), OnlyOneUniqueKey (..) )
import Database.Persist.TH
( mkDeleteCascade, mkMigrate, mkPersist, persistLowerCase, share )
import GHC.Generics
Expand Down

0 comments on commit 34f7f33

Please sign in to comment.