Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Commit

Permalink
[CO-319] Fix account index swagger example
Browse files Browse the repository at this point in the history
  • Loading branch information
akegalj committed Jun 13, 2018
1 parent 15f27e3 commit 8ee4ddc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
8 changes: 6 additions & 2 deletions wallet-new/src/Cardano/Wallet/API/V1/Migration/Types.hs
Expand Up @@ -178,7 +178,7 @@ instance Migrate V0.CAccount V1.Account where
-- in old API 'V0.AccountId' supposed to carry both wallet id and derivation index
instance Migrate (V1.WalletId, V1.AccountIndex) V0.AccountId where
eitherMigrate (walId, accIdx) =
V0.AccountId <$> eitherMigrate walId <*> pure accIdx
V0.AccountId <$> eitherMigrate walId <*> pure (V1.getAccIndex accIdx)

instance Migrate V1.PaymentSource V0.AccountId where
eitherMigrate V1.PaymentSource{..} = eitherMigrate (psWalletId, psAccountIndex)
Expand All @@ -192,7 +192,11 @@ instance Migrate V1.PaymentSource V0.CAccountId where

instance Migrate V0.AccountId (V1.WalletId, V1.AccountIndex) where
eitherMigrate accId =
(,) <$> eitherMigrate (V0.aiWId accId) <*> pure (V0.aiIndex accId)
(,)
<$> eitherMigrate (V0.aiWId accId)
<*> first
Errors.MigrationFailed
(V1.mkAccountIndex $ V0.aiIndex accId)

instance Migrate V0.CAccountId V0.AccountId where
eitherMigrate = first Errors.MigrationFailed . V0.decodeCType
Expand Down
4 changes: 0 additions & 4 deletions wallet-new/src/Cardano/Wallet/API/V1/Swagger.hs
Expand Up @@ -186,17 +186,13 @@ instance (HasSwagger subApi) => HasSwagger (WalletRequestParams :> subApi) where

instance ToParamSchema WalletId

instance ToSchema Core.Address where
declareNamedSchema = pure . paramSchemaToNamedSchema defaultSchemaOptions

instance ToParamSchema Core.Address where
toParamSchema _ = mempty
& type_ .~ SwaggerString

instance ToParamSchema (V1 Core.Address) where
toParamSchema _ = toParamSchema (Proxy @Core.Address)


--
-- Descriptions
--
Expand Down
53 changes: 50 additions & 3 deletions wallet-new/src/Cardano/Wallet/API/V1/Types.hs
Expand Up @@ -39,6 +39,8 @@ module Cardano.Wallet.API.V1.Types (
, Account (..)
, accountsHaveSameId
, AccountIndex
, getAccIndex
, mkAccountIndex
-- * Addresses
, WalletAddress (..)
, NewAddress (..)
Expand Down Expand Up @@ -128,8 +130,9 @@ import qualified Pos.Core as Core
import Pos.Crypto (decodeHash, hashHexF)
import qualified Pos.Crypto.Signing as Core
import Pos.Infra.Diffusion.Subscription.Status (SubscriptionStatus (..))
import Pos.Infra.Util.LogSafe (BuildableSafeGen (..), SecureLog (..), buildSafe, buildSafeList,
buildSafeMaybe, deriveSafeBuildable, plainOrSecureF)
import Pos.Infra.Util.LogSafe (BuildableSafeGen (..), SecureLog (..), buildSafe,
buildSafeList, buildSafeMaybe, deriveSafeBuildable,
plainOrSecureF)
import qualified Pos.Wallet.Web.State.Storage as OldStorage


Expand Down Expand Up @@ -849,7 +852,51 @@ instance Arbitrary WalletAddress where
<*> arbitrary
<*> arbitrary

type AccountIndex = Word32
newtype AccountIndex = AccountIndex { getAccIndex :: Word32 }
deriving (Show, Eq, Ord, Generic)

instance Bounded AccountIndex where
-- NOTE: minimum for hardened key. See https://iohk.myjetbrains.com/youtrack/issue/CO-309
minBound = AccountIndex 2147483648
maxBound = AccountIndex maxBound

mkAccountIndex :: Word32 -> Either Text AccountIndex
mkAccountIndex index | index >= getAccIndex minBound = Right $ AccountIndex index
| otherwise = Left $ "mkAccountIndex: Account index should be in range ["
<> show (getAccIndex minBound)
<> ".."
<> show (getAccIndex maxBound)
<> "]"

instance ToJSON AccountIndex where
toJSON = toJSON . getAccIndex

instance FromJSON AccountIndex where
parseJSON =
either (fail . toString) pure . mkAccountIndex <=< parseJSON

instance Arbitrary AccountIndex where
arbitrary = AccountIndex <$> choose (getAccIndex minBound, getAccIndex maxBound)

deriveSafeBuildable ''AccountIndex
-- Nothing secret to redact for a AccountIndex.
instance BuildableSafeGen AccountIndex where
buildSafeGen _ = bprint build

instance ToParamSchema AccountIndex where
toParamSchema _ = mempty
& type_ .~ SwaggerNumber
& minimum_ .~ Just (fromIntegral $ getAccIndex minBound)
& maximum_ .~ Just (fromIntegral $ getAccIndex maxBound)

instance ToSchema AccountIndex where
declareNamedSchema = pure . paramSchemaToNamedSchema defaultSchemaOptions

instance FromHttpApiData AccountIndex where
parseQueryParam = mkAccountIndex <=< parseQueryParam

instance ToHttpApiData AccountIndex where
toQueryParam = fromString . show . getAccIndex

-- | A wallet 'Account'.
data Account = Account
Expand Down

0 comments on commit 8ee4ddc

Please sign in to comment.