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

Commit 8ee4ddc

Browse files
committed
[CO-319] Fix account index swagger example
1 parent 15f27e3 commit 8ee4ddc

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

wallet-new/src/Cardano/Wallet/API/V1/Migration/Types.hs

+6-2
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ instance Migrate V0.CAccount V1.Account where
178178
-- in old API 'V0.AccountId' supposed to carry both wallet id and derivation index
179179
instance Migrate (V1.WalletId, V1.AccountIndex) V0.AccountId where
180180
eitherMigrate (walId, accIdx) =
181-
V0.AccountId <$> eitherMigrate walId <*> pure accIdx
181+
V0.AccountId <$> eitherMigrate walId <*> pure (V1.getAccIndex accIdx)
182182

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

193193
instance Migrate V0.AccountId (V1.WalletId, V1.AccountIndex) where
194194
eitherMigrate accId =
195-
(,) <$> eitherMigrate (V0.aiWId accId) <*> pure (V0.aiIndex accId)
195+
(,)
196+
<$> eitherMigrate (V0.aiWId accId)
197+
<*> first
198+
Errors.MigrationFailed
199+
(V1.mkAccountIndex $ V0.aiIndex accId)
196200

197201
instance Migrate V0.CAccountId V0.AccountId where
198202
eitherMigrate = first Errors.MigrationFailed . V0.decodeCType

wallet-new/src/Cardano/Wallet/API/V1/Swagger.hs

-4
Original file line numberDiff line numberDiff line change
@@ -186,17 +186,13 @@ instance (HasSwagger subApi) => HasSwagger (WalletRequestParams :> subApi) where
186186

187187
instance ToParamSchema WalletId
188188

189-
instance ToSchema Core.Address where
190-
declareNamedSchema = pure . paramSchemaToNamedSchema defaultSchemaOptions
191-
192189
instance ToParamSchema Core.Address where
193190
toParamSchema _ = mempty
194191
& type_ .~ SwaggerString
195192

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

199-
200196
--
201197
-- Descriptions
202198
--

wallet-new/src/Cardano/Wallet/API/V1/Types.hs

+50-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ module Cardano.Wallet.API.V1.Types (
3939
, Account (..)
4040
, accountsHaveSameId
4141
, AccountIndex
42+
, getAccIndex
43+
, mkAccountIndex
4244
-- * Addresses
4345
, WalletAddress (..)
4446
, NewAddress (..)
@@ -128,8 +130,9 @@ import qualified Pos.Core as Core
128130
import Pos.Crypto (decodeHash, hashHexF)
129131
import qualified Pos.Crypto.Signing as Core
130132
import Pos.Infra.Diffusion.Subscription.Status (SubscriptionStatus (..))
131-
import Pos.Infra.Util.LogSafe (BuildableSafeGen (..), SecureLog (..), buildSafe, buildSafeList,
132-
buildSafeMaybe, deriveSafeBuildable, plainOrSecureF)
133+
import Pos.Infra.Util.LogSafe (BuildableSafeGen (..), SecureLog (..), buildSafe,
134+
buildSafeList, buildSafeMaybe, deriveSafeBuildable,
135+
plainOrSecureF)
133136
import qualified Pos.Wallet.Web.State.Storage as OldStorage
134137

135138

@@ -849,7 +852,51 @@ instance Arbitrary WalletAddress where
849852
<*> arbitrary
850853
<*> arbitrary
851854

852-
type AccountIndex = Word32
855+
newtype AccountIndex = AccountIndex { getAccIndex :: Word32 }
856+
deriving (Show, Eq, Ord, Generic)
857+
858+
instance Bounded AccountIndex where
859+
-- NOTE: minimum for hardened key. See https://iohk.myjetbrains.com/youtrack/issue/CO-309
860+
minBound = AccountIndex 2147483648
861+
maxBound = AccountIndex maxBound
862+
863+
mkAccountIndex :: Word32 -> Either Text AccountIndex
864+
mkAccountIndex index | index >= getAccIndex minBound = Right $ AccountIndex index
865+
| otherwise = Left $ "mkAccountIndex: Account index should be in range ["
866+
<> show (getAccIndex minBound)
867+
<> ".."
868+
<> show (getAccIndex maxBound)
869+
<> "]"
870+
871+
instance ToJSON AccountIndex where
872+
toJSON = toJSON . getAccIndex
873+
874+
instance FromJSON AccountIndex where
875+
parseJSON =
876+
either (fail . toString) pure . mkAccountIndex <=< parseJSON
877+
878+
instance Arbitrary AccountIndex where
879+
arbitrary = AccountIndex <$> choose (getAccIndex minBound, getAccIndex maxBound)
880+
881+
deriveSafeBuildable ''AccountIndex
882+
-- Nothing secret to redact for a AccountIndex.
883+
instance BuildableSafeGen AccountIndex where
884+
buildSafeGen _ = bprint build
885+
886+
instance ToParamSchema AccountIndex where
887+
toParamSchema _ = mempty
888+
& type_ .~ SwaggerNumber
889+
& minimum_ .~ Just (fromIntegral $ getAccIndex minBound)
890+
& maximum_ .~ Just (fromIntegral $ getAccIndex maxBound)
891+
892+
instance ToSchema AccountIndex where
893+
declareNamedSchema = pure . paramSchemaToNamedSchema defaultSchemaOptions
894+
895+
instance FromHttpApiData AccountIndex where
896+
parseQueryParam = mkAccountIndex <=< parseQueryParam
897+
898+
instance ToHttpApiData AccountIndex where
899+
toQueryParam = fromString . show . getAccIndex
853900

854901
-- | A wallet 'Account'.
855902
data Account = Account

0 commit comments

Comments
 (0)