module Cardano.Chain.Common.AddressHash
  ( AddressHash,
    addressHash,
    unsafeAddressHash,
  )
where

import Cardano.Binary (ToCBOR, serialize)
import Cardano.Crypto.Hashing (AbstractHash, abstractHashFromDigest)
import Cardano.Prelude
import Crypto.Hash (Blake2b_224, Digest, SHA3_256)
import qualified Crypto.Hash as CryptoHash

-- | Hash used to identify address.
type AddressHash = AbstractHash Blake2b_224

unsafeAddressHash :: ToCBOR a => a -> AddressHash b
unsafeAddressHash :: a -> AddressHash b
unsafeAddressHash = Digest Blake2b_224 -> AddressHash b
forall algo a. Digest algo -> AbstractHash algo a
abstractHashFromDigest (Digest Blake2b_224 -> AddressHash b)
-> (a -> Digest Blake2b_224) -> a -> AddressHash b
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Digest SHA3_256 -> Digest Blake2b_224
secondHash (Digest SHA3_256 -> Digest Blake2b_224)
-> (a -> Digest SHA3_256) -> a -> Digest Blake2b_224
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> Digest SHA3_256
forall a. ToCBOR a => a -> Digest SHA3_256
firstHash
  where
    firstHash :: ToCBOR a => a -> Digest SHA3_256
    firstHash :: a -> Digest SHA3_256
firstHash = ByteString -> Digest SHA3_256
forall a. HashAlgorithm a => ByteString -> Digest a
CryptoHash.hashlazy (ByteString -> Digest SHA3_256)
-> (a -> ByteString) -> a -> Digest SHA3_256
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> ByteString
forall a. ToCBOR a => a -> ByteString
serialize
    secondHash :: Digest SHA3_256 -> Digest Blake2b_224
    secondHash :: Digest SHA3_256 -> Digest Blake2b_224
secondHash = Digest SHA3_256 -> Digest Blake2b_224
forall ba a.
(ByteArrayAccess ba, HashAlgorithm a) =>
ba -> Digest a
CryptoHash.hash

addressHash :: ToCBOR a => a -> AddressHash a
addressHash :: a -> AddressHash a
addressHash = a -> AddressHash a
forall a b. ToCBOR a => a -> AddressHash b
unsafeAddressHash