module Test.Cardano.Chain.Elaboration.Keys (
  elaborateKeyPair,
  elaborateVKey,
  elaborateVKeyGenesis,
  elaborateVKeyGenesisHash,

  -- * Abstract verification-key elaboration functions
  vKeyPair,
  vKeyToSKey,
  vKeyToSafeSigner,
) where

import Byron.Spec.Ledger.Core (
  KeyPair,
  Owner (Owner),
  VKey (VKey),
  VKeyGenesis (VKeyGenesis),
  keyPair,
  owner,
  sKey,
 )
import Cardano.Chain.Common (KeyHash, hashKey)
import Cardano.Crypto.Signing (
  SafeSigner,
  SigningKey,
  VerificationKey,
  deterministicKeyGen,
  noPassSafeSigner,
 )
import Cardano.Prelude
import qualified Data.ByteString as BS
import Data.ByteString.Builder (integerDec, toLazyByteString)
import qualified Data.ByteString.Lazy as BSL

elaborateKeyPair :: KeyPair -> (VerificationKey, SigningKey)
elaborateKeyPair :: KeyPair -> (VerificationKey, SigningKey)
elaborateKeyPair KeyPair
kp = ByteString -> (VerificationKey, SigningKey)
deterministicKeyGen (ByteString -> (VerificationKey, SigningKey))
-> ByteString -> (VerificationKey, SigningKey)
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString
padSeed ByteString
seed
  where
    Owner Natural
o = SKey -> Owner
forall a. HasOwner a => a -> Owner
owner (SKey -> Owner) -> SKey -> Owner
forall a b. (a -> b) -> a -> b
$ KeyPair -> SKey
sKey KeyPair
kp
    padSeed :: ByteString -> ByteString
padSeed ByteString
s =
      let padLength :: Int
padLength = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int
32 Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
BS.length ByteString
s) in Int -> Word8 -> ByteString
BS.replicate Int
padLength Word8
0 ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
s
    seed :: ByteString
seed = ByteString -> ByteString
BSL.toStrict (ByteString -> ByteString)
-> (Integer -> ByteString) -> Integer -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Builder -> ByteString
toLazyByteString (Builder -> ByteString)
-> (Integer -> Builder) -> Integer -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Integer -> Builder
integerDec (Integer -> ByteString) -> Integer -> ByteString
forall a b. (a -> b) -> a -> b
$ Natural -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Natural
o

vKeyPair :: VKey -> KeyPair
vKeyPair :: VKey -> KeyPair
vKeyPair (VKey Owner
o) = Owner -> KeyPair
keyPair Owner
o

elaborateVKey :: VKey -> VerificationKey
elaborateVKey :: VKey -> VerificationKey
elaborateVKey = (VerificationKey, SigningKey) -> VerificationKey
forall a b. (a, b) -> a
fst ((VerificationKey, SigningKey) -> VerificationKey)
-> (VKey -> (VerificationKey, SigningKey))
-> VKey
-> VerificationKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. KeyPair -> (VerificationKey, SigningKey)
elaborateKeyPair (KeyPair -> (VerificationKey, SigningKey))
-> (VKey -> KeyPair) -> VKey -> (VerificationKey, SigningKey)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. VKey -> KeyPair
vKeyPair

vKeyToSKey :: VKey -> SigningKey
vKeyToSKey :: VKey -> SigningKey
vKeyToSKey = (VerificationKey, SigningKey) -> SigningKey
forall a b. (a, b) -> b
snd ((VerificationKey, SigningKey) -> SigningKey)
-> (VKey -> (VerificationKey, SigningKey)) -> VKey -> SigningKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. KeyPair -> (VerificationKey, SigningKey)
elaborateKeyPair (KeyPair -> (VerificationKey, SigningKey))
-> (VKey -> KeyPair) -> VKey -> (VerificationKey, SigningKey)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. VKey -> KeyPair
vKeyPair

vKeyToSafeSigner :: VKey -> SafeSigner
vKeyToSafeSigner :: VKey -> SafeSigner
vKeyToSafeSigner = SigningKey -> SafeSigner
noPassSafeSigner (SigningKey -> SafeSigner)
-> (VKey -> SigningKey) -> VKey -> SafeSigner
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. VKey -> SigningKey
vKeyToSKey

elaborateVKeyGenesis :: VKeyGenesis -> VerificationKey
elaborateVKeyGenesis :: VKeyGenesis -> VerificationKey
elaborateVKeyGenesis (VKeyGenesis VKey
vk) = VKey -> VerificationKey
elaborateVKey VKey
vk

elaborateVKeyGenesisHash :: VKeyGenesis -> KeyHash
elaborateVKeyGenesisHash :: VKeyGenesis -> KeyHash
elaborateVKeyGenesisHash = VerificationKey -> KeyHash
hashKey (VerificationKey -> KeyHash)
-> (VKeyGenesis -> VerificationKey) -> VKeyGenesis -> KeyHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. VKeyGenesis -> VerificationKey
elaborateVKeyGenesis