Skip to content

Commit

Permalink
add AnyAddress to swagger and and core unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Oct 26, 2020
1 parent f264b25 commit 6b90e68
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
16 changes: 12 additions & 4 deletions lib/core/src/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ data ApiCredentials = ApiCredentials
data AnyAddressType =
EnterpriseDelegating
| RewardAccount
deriving (Eq, Show)
deriving (Eq, Show, Bounded, Enum)

data AnyAddress = AnyAddress
{ payload :: ByteString
Expand Down Expand Up @@ -1411,16 +1411,24 @@ instance ToJSON ApiCredentials where
object [ "spending" .= toJSON spending', "staking" .= toJSON staking']

instance FromJSON AnyAddress where
parseJSON = parseJSON >=> eitherToParser . first ShowFmt . fromText
parseJSON = withObject "ApiCredentials" $ \obj -> do
addr <- obj .:? "address"
case addr of
Nothing ->
fail "AnyAddress expects 'address' key"
Just addr' -> case fromText addr' of
Right anyAddr -> pure anyAddr
Left (TextDecodingError err) -> fail err

instance ToJSON AnyAddress where
toJSON (AnyAddress p addrType net) = do
let hrp = case (addrType, net) of
(EnterpriseDelegating, 1) -> [Bech32.humanReadablePart|addr|]
(RewardAccount, 1) -> [Bech32.humanReadablePart|stake|]
(EnterpriseDelegating, 0) -> [Bech32.humanReadablePart|addr_test|]
(RewardAccount, 0) -> [Bech32.humanReadablePart|stake_test|]
_ -> error "Wrong network tag"
String $ T.decodeUtf8 $ encode (EBech32 hrp) p
_ -> error "Wrong network tag in AnyAddress"
object [ "address" .= (String $ T.decodeUtf8 $ encode (EBech32 hrp) p)]

instance MkSomeMnemonic sizes => FromJSON (ApiMnemonicT sizes)
where
Expand Down
35 changes: 35 additions & 0 deletions lib/core/test/data/Cardano/Wallet/Api/AnyAddress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"seed": 406081777260831026,
"samples": [
{
"address": "addr1kn0hst6smm5jwsmyw0ewj3r0w7rszpudfpz7gpcv2q3h2m494qksva58aw"
},
{
"address": "addr_test1zzf0zlnnp95ly399hqetfvpgreldacs0jm4qxn65ykjdfk4v3ansxlwmc0"
},
{
"address": "addr_test1zl8d4etheywevjgsd5p5mpqfge8xjskr6cd209kmrevzzx6nlmnsqh6gcz"
},
{
"address": "stake_test1gp9nl0zgw8h3a226z7ygpv6l06vxtuh0nfey4kf099mhy7lcdg0qtzfx8q"
},
{
"address": "addr_test1pu44lev3w28fm25g0mlmng2s5wwh4jk8u93ead5ll66cezjqv6dq8w0p0d"
},
{
"address": "addr_test1lklyyd7ewrc6pt4jlg38xkaf0vc98q50uhdpagdv7jk4w0nvkq8s26pn3z"
},
{
"address": "addr17j606vkpz89v7zsuvedrnnrkv3djyykrndjvh3cqedpjwkrseg2qkckem3"
},
{
"address": "stake_test1d9fzzrmemtnr8jyd4mcwy0mz7lmf0pnlvuskmgrxwfujpfpe3jdqvlyyml"
},
{
"address": "stake_test1alxs246symxwaektgww8933cje93j9at0srzwepcqnlp33usg2cs5tj9j5"
},
{
"address": "stake_test18f3ktc9g2aj297paj0vd0hl8n02p7893gdzctq00rzcrmse7yr4qg79txs"
}
]
}
12 changes: 12 additions & 0 deletions lib/core/test/unit/Cardano/Wallet/Api/TypesSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import Cardano.Wallet.Api
import Cardano.Wallet.Api.Types
( AccountPostData (..)
, AddressAmount (..)
, AnyAddress (..)
, ApiAccountPublicKey (..)
, ApiAddress (..)
, ApiAddressInspect (..)
Expand Down Expand Up @@ -333,6 +334,7 @@ spec = do
jsonRoundtripAndGolden $ Proxy @(ApiAddress ('Testnet 0))
jsonRoundtripAndGolden $ Proxy @ApiScript
jsonRoundtripAndGolden $ Proxy @ApiPubKey
jsonRoundtripAndGolden $ Proxy @AnyAddress
jsonRoundtripAndGolden $ Proxy @Credential
jsonRoundtripAndGolden $ Proxy @ApiCredentials
jsonRoundtripAndGolden $ Proxy @(ApiT DerivationIndex)
Expand Down Expand Up @@ -1012,6 +1014,13 @@ instance Arbitrary ApiCredentials where
, pure $ ApiCredentials (Just credential1) (Just credential2)
]

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

instance Arbitrary (ApiSelectCoinsPayments n) where
arbitrary = genericArbitrary
shrink = genericShrink
Expand Down Expand Up @@ -1886,6 +1895,9 @@ instance ToSchema Credential where
, script' ^. properties
]

instance ToSchema AnyAddress where
declareNamedSchema _ = declareSchemaForDefinition "AnyAddress"

instance ToSchema ApiNetworkParameters where
declareNamedSchema _ = declareSchemaForDefinition "ApiNetworkParameters"

Expand Down
18 changes: 17 additions & 1 deletion specifications/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,15 @@ x-credentialPubKey: &credentialPubKey
0: addr_vk13pfp60uf4ru7cmj5d57xjdzj6lagn328ah67h35urqrl92jn094z0s07fcy2pvfw3xfhna6fa24na8nk6lraymjcrl5wwj6chsjqtjqlfw44z
1: stake_vk13pfp60uf4ru7cmj5d57xjdzj6lagn328ah67h35urqrl92jn094z0s07fcy2pvfw3xfhna6fa24na8nk6lraymjcrl5wwj6chsjqtjqlfw44z

x-anyAddress: &anyAddress
description: A Shelley address representing either enterprise, reward account or delegating address
type: string
format: bech32
pattern: "^((addr)|(stake)|(addr_test)|(stake_test))1[0-9a-z]*$"
example:
0: addr13pfp60uf4ru7cmj5d57xjdzj6lagn328ah67h35urqrl92jn094z0s07fcy2pvfw3xfhna6fa24na8nk6lraymjcrl5wwj6chsjqtjqlfw44z
1: stake_test13pfp60uf4ru7cmj5d57xjdzj6lagn328ah67h35urqrl92jn094z0s07fcy2pvfw3xfhna6fa24na8nk6lraymjcrl5wwj6chsjqtjqlfw44z

x-credential: &credential
nullable: false
additionalProperties:
Expand Down Expand Up @@ -1530,6 +1539,13 @@ components:
properties:
pub_key: *credentialPubKey

AnyAddress: &AnyAddress
type: object
required:
- address
properties:
address: *anyAddress

ApiCredential: &ApiCredential
<<: *credential

Expand Down Expand Up @@ -2290,7 +2306,7 @@ x-responsesPostAnyAddress: &responsesPostAnyAddress
description: Accepted
content:
application/json:
schema: *ApiAddress
schema: *AnyAddress

x-responsesPostTransactionFee: &responsesPostTransactionFee
<<: *responsesErr400
Expand Down

0 comments on commit 6b90e68

Please sign in to comment.