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 forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString
padSeed ByteString
seed
  where
    Owner Natural
o = forall a. HasOwner a => a -> Owner
owner forall a b. (a -> b) -> a -> b
$ KeyPair -> SKey
sKey KeyPair
kp
    padSeed :: ByteString -> ByteString
padSeed ByteString
s =
      let padLength :: Int
padLength = forall a. Ord a => a -> a -> a
max Int
0 (Int
32 forall a. Num a => a -> a -> a
- ByteString -> Int
BS.length ByteString
s) in Int -> Word8 -> ByteString
BS.replicate Int
padLength Word8
0 forall a. Semigroup a => a -> a -> a
<> ByteString
s
    seed :: ByteString
seed = ByteString -> ByteString
BSL.toStrict 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 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 forall a b. (a -> b) -> a -> b
$ 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 = forall a b. (a, b) -> a
fst 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 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 = forall a b. (a, b) -> b
snd 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 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 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 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