Skip to content

Commit

Permalink
ApiCredentials -> ApiAddressData
Browse files Browse the repository at this point in the history
fix malformed and change heap size

regenerate ApiAddressData
  • Loading branch information
paweljakubas committed Oct 30, 2020
1 parent 0144291 commit 1071e78
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 141 deletions.
4 changes: 2 additions & 2 deletions lib/core/src/Cardano/Wallet/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ import Cardano.Wallet
( WalletLayer (..), WalletLog )
import Cardano.Wallet.Api.Types
( AnyAddress
, ApiAddressData
, ApiAddressIdT
, ApiAddressInspect
, ApiAddressInspectData
, ApiAddressT
, ApiByronWallet
, ApiCoinSelectionT
, ApiCredentials
, ApiFee
, ApiNetworkClock
, ApiNetworkInformation
Expand Down Expand Up @@ -336,7 +336,7 @@ type InspectAddress = "addresses"

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/postAnyAddress
type PostAnyAddress n = "addresses"
:> ReqBody '[JSON] ApiCredentials
:> ReqBody '[JSON] ApiAddressData
:> PostCreated '[JSON] AnyAddress

{-------------------------------------------------------------------------------
Expand Down
35 changes: 17 additions & 18 deletions lib/core/src/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module Cardano.Wallet.Api.Types
, ApiScript (..)
, ApiPubKey (..)
, ApiCredential (..)
, ApiCredentials (..)
, ApiAddressData (..)
, AnyAddress (..)
, AnyAddressType (..)
, ApiCertificate (..)
Expand Down Expand Up @@ -411,10 +411,11 @@ data ApiCredential =
| CredentialScript ApiScript
deriving (Eq, Generic, Show)

data ApiCredentials = ApiCredentials
{ spending :: !(Maybe ApiCredential)
, staking :: !(Maybe ApiCredential)
} deriving (Eq, Generic, Show)
data ApiAddressData =
AddrEnterprise ApiCredential
| AddrRewardAccount ApiCredential
| AddrBase ApiCredential ApiCredential
deriving (Eq, Generic, Show)

data AnyAddressType =
EnterpriseDelegating
Expand Down Expand Up @@ -1386,33 +1387,31 @@ instance ToJSON ApiCredential where
toJSON (CredentialPubKey c)= object ["pub_key" .= toJSON c]
toJSON (CredentialScript c)= toJSON c

instance FromJSON ApiCredentials where
parseJSON = withObject "ApiCredentials" $ \obj -> do
instance FromJSON ApiAddressData where
parseJSON = withObject "ApiAddressData" $ \obj -> do
choice <- (,) <$> obj .:? "spending" <*> obj .:? "staking"
case choice of
(Nothing, Nothing) -> fail "ApiCredentials must have at least one credential."
(Nothing, Nothing) -> fail "ApiAddressData must have at least one credential."
(Just c, Nothing) -> do
spending' <- parseJSON c
pure $ ApiCredentials (Just spending') Nothing
pure $ AddrEnterprise spending'
(Nothing, Just c) -> do
staking' <- parseJSON c
pure $ ApiCredentials Nothing (Just staking')
pure $ AddrRewardAccount staking'
(Just c1, Just c2) -> do
spending' <- parseJSON c1
staking' <- parseJSON c2
pure $ ApiCredentials (Just spending') (Just staking')
instance ToJSON ApiCredentials where
toJSON (ApiCredentials Nothing Nothing) =
error "ApiCredentials must have at least one credential."
toJSON (ApiCredentials (Just spending') Nothing) =
pure $ AddrBase spending' staking'
instance ToJSON ApiAddressData where
toJSON (AddrEnterprise spending') =
object [ "spending" .= toJSON spending']
toJSON (ApiCredentials Nothing (Just staking')) =
toJSON (AddrRewardAccount staking') =
object [ "staking" .= toJSON staking']
toJSON (ApiCredentials (Just spending') (Just staking')) =
toJSON (AddrBase spending' staking') =
object [ "spending" .= toJSON spending', "staking" .= toJSON staking']

instance FromJSON AnyAddress where
parseJSON = withObject "ApiCredentials" $ \obj -> do
parseJSON = withObject "AnyAddress" $ \obj -> do
addr <- obj .:? "address"
case addr of
Nothing ->
Expand Down
73 changes: 73 additions & 0 deletions lib/core/test/data/Cardano/Wallet/Api/ApiAddressData.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"seed": 3464589065996029052,
"samples": [
{
"staking": {
"script": "script_vkh1vpq4jwk2zue8gttfaqkc6zdxxt67r3j0mg9az9tj4tnp6zewcpz"
}
},
{
"staking": {
"script": "script_vkh1l686qpfelyz057dn0yacxsr6da6z775pk7nmwgqp8a0dghzlnre"
}
},
{
"staking": {
"pub_key": "addr_vk1jg5gupuznm0t6py32xfmxw3aths7v8583wd6rrg23nfe94nzhgwsl69n85"
}
},
{
"spending": {
"script": "script_vkh1w0lc9k9uxn66qr9galkc026yay2uapk02mhlu0jsenx0z83vkyw"
},
"staking": {
"pub_key": "addr_vk1zejnq8l778fsry7rm56cnndm0n847lg4vq5xrt27xw762d97cyjsgy5vs4"
}
},
{
"staking": {
"pub_key": "addr_vk1ykxae7euz42zh33nmrjt8plcv0xudsxn63fsyx7q5n7nwdqfnzeqk33qq2"
}
},
{
"spending": {
"script": "script_vkh1z8sje05asrq33mjnggh7qel50ngv3x0h4detewew2903x2s0frk"
},
"staking": {
"pub_key": "addr_vk1fmz6fehcw8u8gu4hvy8p3vp6zznmp6cualqpc3xv4zjmm875etdquvu9td"
}
},
{
"staking": {
"script": "script_vkh1g6nyj4v2hpwu7l35tttr775y98ktah8tn2ynjxmuxdh0gkzd4su"
}
},
{
"spending": {
"pub_key": "addr_vk1pgunxulhzrwzs8gk68xh5me0y8k9hvg6crs3fr098tztl4ynjaaq06lu6s"
},
"staking": {
"pub_key": "addr_vk1kp4hdttm4n3tzsq2jk9958lzwyd8lp7azhs3w5jlk0fu687t5ydspdlak6"
}
},
{
"spending": {
"script": {
"all": [
"script_vkh15gqdujp76avesry8pst70c7etd3dpa9qyfwmngcvmddazkr0szk",
"script_vkh1r6jl4ehjenluwlejmshm0lvche2eteyzng5sm5vag9mzc4h0vlw",
"script_vkh1lnunew99ncml4nfecre58ek55xnhrrnvyl7wta0w2ljz553jpw2"
]
}
},
"staking": {
"pub_key": "addr_vk13t3hzrma0wf4663yxfrepzh4pghpwlvmherzvpadx3s6y08umxts2jsv09"
}
},
{
"spending": {
"pub_key": "addr_vk1jjt99zk9778l6c5zchw9vdu2rrzfs6l60lgxcyv8sv7sjgkpueesx9d99m"
}
}
]
}
58 changes: 0 additions & 58 deletions lib/core/test/data/Cardano/Wallet/Api/ApiCredentials.json

This file was deleted.

10 changes: 5 additions & 5 deletions lib/core/test/unit/Cardano/Wallet/Api/Malformed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ module Cardano.Wallet.Api.Malformed
import Prelude

import Cardano.Wallet.Api.Types
( ApiAddressInspectData
, ApiCredentials
( ApiAddressData
, ApiAddressInspectData
, ApiPoolId
, ApiPostRandomAddressData
, ApiPutAddressesData
Expand Down Expand Up @@ -290,16 +290,16 @@ instance Malformed (BodyParam ApiScript) where
)
]

instance Malformed (BodyParam ApiCredentials) where
instance Malformed (BodyParam ApiAddressData) where
malformed = first BodyParam <$>
[ ( Aeson.encode [aesonQQ|
{}|]
, "Error in $: ApiCredentials must have at least one credential."
, "Error in $: ApiAddressData must have at least one credential."
)
, ( Aeson.encode [aesonQQ|
{ "script": {}
}|]
, "Error in $: ApiCredentials must have at least one credential."
, "Error in $: ApiAddressData must have at least one credential."
)
, ( Aeson.encode [aesonQQ|
{ "staking": {}
Expand Down
28 changes: 14 additions & 14 deletions lib/core/test/unit/Cardano/Wallet/Api/TypesSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import Cardano.Wallet.Api.Types
, AnyAddress (..)
, ApiAccountPublicKey (..)
, ApiAddress (..)
, ApiAddressData (..)
, ApiAddressInspect (..)
, ApiBlockInfo (..)
, ApiBlockReference (..)
Expand All @@ -57,7 +58,6 @@ import Cardano.Wallet.Api.Types
, ApiCoinSelectionInput (..)
, ApiCoinSelectionOutput (..)
, ApiCredential (..)
, ApiCredentials (..)
, ApiEpochInfo (..)
, ApiFee (..)
, ApiMnemonicT (..)
Expand Down Expand Up @@ -334,7 +334,7 @@ spec = do
jsonRoundtripAndGolden $ Proxy @ApiPubKey
jsonRoundtripAndGolden $ Proxy @AnyAddress
jsonRoundtripAndGolden $ Proxy @ApiCredential
jsonRoundtripAndGolden $ Proxy @ApiCredentials
jsonRoundtripAndGolden $ Proxy @ApiAddressData
jsonRoundtripAndGolden $ Proxy @(ApiT DerivationIndex)
jsonRoundtripAndGolden $ Proxy @ApiEpochInfo
jsonRoundtripAndGolden $ Proxy @(ApiSelectCoinsData ('Testnet 0))
Expand Down Expand Up @@ -987,14 +987,14 @@ instance Arbitrary ApiScript where
instance Arbitrary Script where
arbitrary = do
reqAllGen <- do
n <- choose (1,10)
n <- choose (1,4)
pure $ RequireAllOf <$> vector n
reqAnyGen <- do
n <- choose (1,10)
n <- choose (1,4)
pure $ RequireAnyOf <$> vector n
reqMofNGen <- do
m <- choose (2,5)
n <- choose ((fromInteger $ toInteger m),10)
n <- choose ((fromInteger $ toInteger m),7)
pure $ RequireSomeOf m <$> vector n
let reqSig =
(RequireSignatureOf . KeyHash . BS.pack) <$> replicateM 28 arbitrary
Expand All @@ -1016,22 +1016,22 @@ instance Arbitrary ApiCredential where
oneof [ CredentialPubKey <$> pubKeyGen
, CredentialScript <$> scriptGen ]

instance Arbitrary ApiCredentials where
instance Arbitrary ApiAddressData where
arbitrary = do
credential1 <- arbitrary
credential2 <- arbitrary
oneof
[ pure $ ApiCredentials (Just credential1) Nothing
, pure $ ApiCredentials Nothing (Just credential1)
, pure $ ApiCredentials (Just credential1) (Just credential2)
[ pure $ AddrEnterprise credential1
, pure $ AddrRewardAccount credential2
, pure $ AddrBase credential1 credential2
]

instance Arbitrary AnyAddress where
arbitrary = do
payload <- BS.pack <$> replicateM 32 arbitrary
network <- choose (0,1)
payload' <- BS.pack <$> replicateM 32 arbitrary
network' <- choose (0,1)
addrType <- arbitraryBoundedEnum
pure $ AnyAddress payload addrType network
pure $ AnyAddress payload' addrType network'

instance Arbitrary (ApiSelectCoinsPayments n) where
arbitrary = genericArbitrary
Expand Down Expand Up @@ -1880,11 +1880,11 @@ instance ToSchema ApiScript where
instance ToSchema ApiPubKey where
declareNamedSchema _ = declareSchemaForDefinition "ApiPubKey"

instance ToSchema ApiCredentials where
instance ToSchema ApiAddressData where
declareNamedSchema _ = do
addDefinition scriptValueSchema
addDefinition credentialValueSchema
declareSchemaForDefinition "ApiCredentials"
declareSchemaForDefinition "ApiAddressData"

instance ToSchema ApiCredential where
declareNamedSchema _ = do
Expand Down
Loading

0 comments on commit 1071e78

Please sign in to comment.