-- | Test and example values used across hydra-node tests.
module Test.Hydra.Fixture where

import Hydra.Prelude

import Hydra.Cardano.Api (Key (..), SerialiseAsRawBytes (..), SigningKey, VerificationKey, getVerificationKey)
import Hydra.ContestationPeriod (ContestationPeriod (..))
import Hydra.Crypto (HydraKey, generateSigningKey)
import Hydra.Environment (Environment (..))
import Hydra.HeadId (HeadId (..), HeadSeed (..))
import Hydra.OnChainId (AsType (AsOnChainId), OnChainId)
import Hydra.Party (Party (..), deriveParty)

alice, bob, carol :: Party
alice :: Party
alice = SigningKey HydraKey -> Party
deriveParty SigningKey HydraKey
aliceSk
bob :: Party
bob = SigningKey HydraKey -> Party
deriveParty SigningKey HydraKey
bobSk
carol :: Party
carol = SigningKey HydraKey -> Party
deriveParty SigningKey HydraKey
carolSk

aliceSk, bobSk, carolSk :: SigningKey HydraKey
aliceSk :: SigningKey HydraKey
aliceSk = ByteString -> SigningKey HydraKey
generateSigningKey ByteString
"alice"
bobSk :: SigningKey HydraKey
bobSk = ByteString -> SigningKey HydraKey
generateSigningKey ByteString
"bob"
carolSk :: SigningKey HydraKey
carolSk = ByteString -> SigningKey HydraKey
generateSigningKey ByteString
"zcarol"

aliceVk, bobVk, carolVk :: VerificationKey HydraKey
aliceVk :: VerificationKey HydraKey
aliceVk = SigningKey HydraKey -> VerificationKey HydraKey
forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> VerificationKey keyrole
getVerificationKey SigningKey HydraKey
aliceSk
bobVk :: VerificationKey HydraKey
bobVk = SigningKey HydraKey -> VerificationKey HydraKey
forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> VerificationKey keyrole
getVerificationKey SigningKey HydraKey
bobSk
carolVk :: VerificationKey HydraKey
carolVk = SigningKey HydraKey -> VerificationKey HydraKey
forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> VerificationKey keyrole
getVerificationKey SigningKey HydraKey
carolSk

allVKeys :: [VerificationKey HydraKey]
allVKeys :: [VerificationKey HydraKey]
allVKeys = Party -> VerificationKey HydraKey
vkey (Party -> VerificationKey HydraKey)
-> [Party] -> [VerificationKey HydraKey]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Party
alice, Party
bob, Party
carol]

cperiod :: ContestationPeriod
cperiod :: ContestationPeriod
cperiod = Natural -> ContestationPeriod
UnsafeContestationPeriod Natural
42

testHeadId :: HeadId
testHeadId :: HeadId
testHeadId = ByteString -> HeadId
UnsafeHeadId ByteString
"1234"

testHeadSeed :: HeadSeed
testHeadSeed :: HeadSeed
testHeadSeed = ByteString -> HeadSeed
UnsafeHeadSeed ByteString
"000000000000000000#0"

-- | Derive some 'OnChainId' from a Hydra party. In the real protocol this is
-- currently not done, but in this simulated chain setting this is definitely
-- fine.
deriveOnChainId :: Party -> OnChainId
deriveOnChainId :: Party -> OnChainId
deriveOnChainId Party{VerificationKey HydraKey
$sel:vkey:Party :: Party -> VerificationKey HydraKey
vkey :: VerificationKey HydraKey
vkey} =
  case AsType OnChainId
-> ByteString -> Either SerialiseAsRawBytesError OnChainId
forall a.
SerialiseAsRawBytes a =>
AsType a -> ByteString -> Either SerialiseAsRawBytesError a
deserialiseFromRawBytes AsType OnChainId
AsOnChainId ByteString
bytes of
    Left SerialiseAsRawBytesError
_ -> Text -> OnChainId
forall a t. (HasCallStack, IsText t) => t -> a
error Text
"deriveOnChainId failed"
    Right OnChainId
oid -> OnChainId
oid
 where
  bytes :: ByteString
bytes = Hash HydraKey -> ByteString
forall a. SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytes (Hash HydraKey -> ByteString) -> Hash HydraKey -> ByteString
forall a b. (a -> b) -> a -> b
$ VerificationKey HydraKey -> Hash HydraKey
forall keyrole.
Key keyrole =>
VerificationKey keyrole -> Hash keyrole
verificationKeyHash VerificationKey HydraKey
vkey

-- | An environment fixture for testing.
testEnvironment :: Environment
testEnvironment :: Environment
testEnvironment =
  Environment
    { $sel:party:Environment :: Party
party = Party
alice
    , $sel:signingKey:Environment :: SigningKey HydraKey
signingKey = SigningKey HydraKey
aliceSk
    , $sel:otherParties:Environment :: [Party]
otherParties = [Party
bob, Party
carol]
    , $sel:contestationPeriod:Environment :: ContestationPeriod
contestationPeriod = ContestationPeriod
cperiod
    , $sel:participants:Environment :: [OnChainId]
participants = Party -> OnChainId
deriveOnChainId (Party -> OnChainId) -> [Party] -> [OnChainId]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Party
alice, Party
bob, Party
carol]
    }