Skip to content

Commit

Permalink
CBOR Round trip for Byron signing key
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Jun 8, 2021
1 parent 8a2a984 commit 7eb78ae
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .hlint.yaml
Expand Up @@ -61,3 +61,7 @@

# To generate a suitable file for HLint do:
# $ hlint --default > .hlint.yaml

- ignore:
name: Use camelCase
within: Test.Cardano.Api.KeysByron
2 changes: 2 additions & 0 deletions cardano-api/cardano-api.cabal
Expand Up @@ -179,6 +179,7 @@ test-suite cardano-api-test
Test.Cardano.Api.Gen
Test.Cardano.Api.Genesis
Test.Cardano.Api.Json
Test.Cardano.Api.KeysByron
Test.Cardano.Api.Ledger
Test.Cardano.Api.Metadata
Test.Cardano.Api.Typed.Bech32
Expand All @@ -191,6 +192,7 @@ test-suite cardano-api-test
Test.Cardano.Api.Typed.RawBytes
Test.Cardano.Api.Typed.Script
Test.Cardano.Api.Typed.Value
Test.Cardano.Crypto.Seed.Gen
Test.Tasty.Hedgehog.Group

ghc-options: -threaded -rtsopts -with-rtsopts=-N -with-rtsopts=-T
33 changes: 33 additions & 0 deletions cardano-api/test/Test/Cardano/Api/KeysByron.hs
@@ -0,0 +1,33 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TemplateHaskell #-}

module Test.Cardano.Api.KeysByron
( tests
) where

import Cardano.Api
import Cardano.Prelude
import Hedgehog (Gen, Property, discover)
import Test.Cardano.Api.Typed.Orphans ()
import Test.Tasty (TestTree)
import Test.Tasty.Hedgehog.Group (fromGroup)

import qualified Hedgehog as H
import qualified Test.Cardano.Crypto.Seed.Gen as Gen

{- HLINT ignore "Use camelCase" -}

prop_roundtrip_byron_key_CBOR :: Property
prop_roundtrip_byron_key_CBOR =
roundtrip_CBOR (AsSigningKey AsByronKey) (deterministicSigningKey AsByronKey <$> Gen.genSeedForKey AsByronKey)

roundtrip_CBOR
:: (SerialiseAsCBOR a, Eq a, Show a)
=> AsType a -> Gen a -> Property
roundtrip_CBOR typeProxy gen =
H.property $ do
val <- H.forAll gen
H.tripping val serialiseToCBOR (deserialiseFromCBOR typeProxy)

tests :: TestTree
tests = fromGroup $$discover
22 changes: 22 additions & 0 deletions cardano-api/test/Test/Cardano/Crypto/Seed/Gen.hs
@@ -0,0 +1,22 @@
module Test.Cardano.Crypto.Seed.Gen
( genSeed
, genSeedForKey
) where

import Cardano.Api (AsType, Key)
import Cardano.Crypto.Seed (Seed)
import Data.Functor ((<$>))
import Data.Int (Int)
import Hedgehog (MonadGen, Range)
import Prelude (fromIntegral)

import qualified Cardano.Api as API
import qualified Cardano.Crypto.Seed as C
import qualified Hedgehog.Gen as G
import qualified Hedgehog.Range as R

genSeed :: MonadGen m => Range Int -> m Seed
genSeed r = C.mkSeedFromBytes <$> G.bytes r

genSeedForKey :: (Key key, MonadGen m) => AsType key -> m Seed
genSeedForKey keyRole = genSeed (R.singleton (fromIntegral (API.deterministicSigningKeySeedSize keyRole)))
8 changes: 5 additions & 3 deletions cardano-api/test/cardano-api-test.hs
Expand Up @@ -6,6 +6,7 @@ import Test.Tasty (TestTree, defaultMain, testGroup)

import qualified Test.Cardano.Api.Crypto
import qualified Test.Cardano.Api.Json
import qualified Test.Cardano.Api.KeysByron
import qualified Test.Cardano.Api.Ledger
import qualified Test.Cardano.Api.Metadata
import qualified Test.Cardano.Api.Typed.Bech32
Expand All @@ -26,16 +27,17 @@ main = do
tests :: TestTree
tests =
testGroup "Cardano.Api"
[ Test.Cardano.Api.Typed.Value.tests
, Test.Cardano.Api.Crypto.tests
[ Test.Cardano.Api.Crypto.tests
, Test.Cardano.Api.Json.tests
, Test.Cardano.Api.KeysByron.tests
, Test.Cardano.Api.Ledger.tests
, Test.Cardano.Api.Metadata.tests
, Test.Cardano.Api.Typed.Bech32.tests
, Test.Cardano.Api.Typed.CBOR.tests
, Test.Cardano.Api.Typed.Envelope.tests
, Test.Cardano.Api.Typed.JSON.tests
, Test.Cardano.Api.Typed.Ord.tests
, Test.Cardano.Api.Typed.Script.tests
, Test.Cardano.Api.Typed.RawBytes.tests
, Test.Cardano.Api.Typed.Script.tests
, Test.Cardano.Api.Typed.Value.tests
]

0 comments on commit 7eb78ae

Please sign in to comment.