@@ -39,6 +39,8 @@ module Cardano.Wallet.API.V1.Types (
39
39
, Account (.. )
40
40
, accountsHaveSameId
41
41
, AccountIndex
42
+ , getAccIndex
43
+ , mkAccountIndex
42
44
-- * Addresses
43
45
, WalletAddress (.. )
44
46
, NewAddress (.. )
@@ -128,8 +130,9 @@ import qualified Pos.Core as Core
128
130
import Pos.Crypto (decodeHash , hashHexF )
129
131
import qualified Pos.Crypto.Signing as Core
130
132
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 )
133
136
import qualified Pos.Wallet.Web.State.Storage as OldStorage
134
137
135
138
@@ -849,7 +852,51 @@ instance Arbitrary WalletAddress where
849
852
<*> arbitrary
850
853
<*> arbitrary
851
854
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
853
900
854
901
-- | A wallet 'Account'.
855
902
data Account = Account
0 commit comments