{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}

module Test.Cardano.Crypto.Example (
  exampleProtocolMagic0,
  exampleProtocolMagic1,
  exampleProtocolMagic2,
  exampleProtocolMagic3,
  exampleProtocolMagic4,
  exampleProtocolMagicId0,
  exampleVerificationKey,
  exampleVerificationKeys,
  exampleRedeemVerificationKey,
  exampleSigningKey,
  exampleSigningKeys,
  exampleSafeSigner,
  staticSafeSigners,
)
where

import Cardano.Crypto (
  AProtocolMagic (..),
  ProtocolMagic,
  ProtocolMagicId (..),
  RedeemVerificationKey,
  RequiresNetworkMagic (..),
  SafeSigner,
  SigningKey (..),
  VerificationKey (..),
  noPassSafeSigner,
  redeemDeterministicKeyGen,
 )
import qualified Cardano.Crypto.Wallet as CC
import Cardano.Ledger.Binary (Annotated (..))
import Cardano.Prelude
import Data.List ((!!))
import Data.Maybe (fromJust)
import Test.Cardano.Crypto.CBOR (getBytes)

exampleProtocolMagicId0 :: ProtocolMagicId
exampleProtocolMagicId0 :: ProtocolMagicId
exampleProtocolMagicId0 = Word32 -> ProtocolMagicId
ProtocolMagicId Word32
31337

exampleProtocolMagic0 :: ProtocolMagic
exampleProtocolMagic0 :: ProtocolMagic
exampleProtocolMagic0 =
  forall a.
Annotated ProtocolMagicId a
-> RequiresNetworkMagic -> AProtocolMagic a
AProtocolMagic (forall b a. b -> a -> Annotated b a
Annotated ProtocolMagicId
exampleProtocolMagicId0 ()) RequiresNetworkMagic
RequiresMagic

exampleProtocolMagic1 :: ProtocolMagic
exampleProtocolMagic1 :: ProtocolMagic
exampleProtocolMagic1 =
  forall a.
Annotated ProtocolMagicId a
-> RequiresNetworkMagic -> AProtocolMagic a
AProtocolMagic (forall b a. b -> a -> Annotated b a
Annotated (Word32 -> ProtocolMagicId
ProtocolMagicId Word32
2147000001) ()) RequiresNetworkMagic
RequiresMagic

exampleProtocolMagic2 :: ProtocolMagic
exampleProtocolMagic2 :: ProtocolMagic
exampleProtocolMagic2 =
  forall a.
Annotated ProtocolMagicId a
-> RequiresNetworkMagic -> AProtocolMagic a
AProtocolMagic (forall b a. b -> a -> Annotated b a
Annotated (Word32 -> ProtocolMagicId
ProtocolMagicId Word32
58952) ()) RequiresNetworkMagic
RequiresMagic

exampleProtocolMagic3 :: ProtocolMagic
exampleProtocolMagic3 :: ProtocolMagic
exampleProtocolMagic3 =
  forall a.
Annotated ProtocolMagicId a
-> RequiresNetworkMagic -> AProtocolMagic a
AProtocolMagic (forall b a. b -> a -> Annotated b a
Annotated (Word32 -> ProtocolMagicId
ProtocolMagicId Word32
31337) ()) RequiresNetworkMagic
RequiresMagic

exampleProtocolMagic4 :: ProtocolMagic
exampleProtocolMagic4 :: ProtocolMagic
exampleProtocolMagic4 =
  forall a.
Annotated ProtocolMagicId a
-> RequiresNetworkMagic -> AProtocolMagic a
AProtocolMagic (forall b a. b -> a -> Annotated b a
Annotated (Word32 -> ProtocolMagicId
ProtocolMagicId Word32
500) ()) RequiresNetworkMagic
RequiresNoMagic

exampleVerificationKey :: VerificationKey
exampleVerificationKey :: VerificationKey
exampleVerificationKey = VerificationKey
vk where [VerificationKey
vk] = Int -> Int -> [VerificationKey]
exampleVerificationKeys Int
16 Int
1 -- 16 could be any number, as we take the first key

exampleVerificationKeys :: Int -> Int -> [VerificationKey]
exampleVerificationKeys :: Int -> Int -> [VerificationKey]
exampleVerificationKeys Int
offset Int
count = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
map (Int -> VerificationKey
toKey forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (forall a. Num a => a -> a -> a
* Int
offset)) [Int
0 .. Int
count forall a. Num a => a -> a -> a
- Int
1]
  where
    toKey :: Int -> VerificationKey
toKey Int
start =
      let Right VerificationKey
vk = XPub -> VerificationKey
VerificationKey forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either String XPub
CC.xpub (Int -> Int -> ByteString
getBytes Int
start Int
64) in VerificationKey
vk

exampleRedeemVerificationKey :: RedeemVerificationKey
exampleRedeemVerificationKey :: RedeemVerificationKey
exampleRedeemVerificationKey =
  forall a. HasCallStack => Maybe a -> a
fromJust (forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Maybe (RedeemVerificationKey, RedeemSigningKey)
redeemDeterministicKeyGen (Int -> Int -> ByteString
getBytes Int
0 Int
32))

-- In order to get the key starting at byte 10, we generate two with offsets of 10
-- between them and take the second.
exampleSigningKey :: SigningKey
exampleSigningKey :: SigningKey
exampleSigningKey = Int -> Int -> [SigningKey]
exampleSigningKeys Int
10 Int
2 forall a. [a] -> Int -> a
!! Int
1

exampleSigningKeys :: Int -> Int -> [SigningKey]
exampleSigningKeys :: Int -> Int -> [SigningKey]
exampleSigningKeys Int
offset Int
count = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
map (Int -> SigningKey
toKey forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (forall a. Num a => a -> a -> a
* Int
offset)) [Int
0 .. Int
count forall a. Num a => a -> a -> a
- Int
1]
  where
    toKey :: Int -> SigningKey
toKey Int
start =
      let Right SigningKey
sk = XPrv -> SigningKey
SigningKey forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall bin. ByteArrayAccess bin => bin -> Either String XPrv
CC.xprv (Int -> Int -> ByteString
getBytes Int
start Int
128) in SigningKey
sk

exampleSafeSigner :: Int -> SafeSigner
exampleSafeSigner :: Int -> SafeSigner
exampleSafeSigner Int
offset = [SafeSigner]
staticSafeSigners forall a. [a] -> Int -> a
!! Int
offset

staticSafeSigners :: [SafeSigner]
staticSafeSigners :: [SafeSigner]
staticSafeSigners = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
map SigningKey -> SafeSigner
noPassSafeSigner (Int -> Int -> [SigningKey]
exampleSigningKeys Int
1 Int
6)