{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

module Shelley.Spec.Ledger.Address
  ( mkVKeyRwdAcnt,
    mkRwdAcnt,
    scriptsToAddr,
    scriptToCred,
    toAddr,
    toCred,
    serialiseAddr,
    deserialiseAddr,
    deserialiseAddrStakeRef,
    Addr (..),
    BootstrapAddress (..),
    bootstrapAddressAttrsSize,
    isBootstrapRedeemer,
    getNetwork,
    RewardAcnt (..),
    serialiseRewardAcnt,
    deserialiseRewardAcnt,
    --  Bits
    byron,
    notBaseAddr,
    isEnterpriseAddr,
    stakeCredIsScript,
    -- internals exported for testing
    getAddr,
    getKeyHash,
    bootstrapKeyHash,
    getPtr,
    getRewardAcnt,
    getScriptHash,
    getVariableLengthNat,
    payCredIsScript,
    putAddr,
    putCredential,
    putPtr,
    putRewardAcnt,
    putVariableLengthNat,
    -- TODO: these should live somewhere else
    natToWord7s,
    word7sToNat,
    Word7 (..),
    toWord7,
  )
where

import Cardano.Binary
  ( Decoder,
    DecoderError (..),
    FromCBOR (..),
    ToCBOR (..),
    decodeFull,
    serialize,
  )
import qualified Cardano.Chain.Common as Byron
import qualified Cardano.Crypto.Hash.Class as Hash
import qualified Cardano.Crypto.Hashing as Byron
import Cardano.Ledger.Crypto (ADDRHASH)
import Cardano.Ledger.Era
import Cardano.Prelude (cborError, panic, parseBase16)
import Control.DeepSeq (NFData)
import Data.Aeson (FromJSON (..), FromJSONKey (..), ToJSON (..), ToJSONKey (..), (.:), (.=))
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.Encoding as Aeson
import qualified Data.Aeson.Types as Aeson
import Data.Binary (Get, Put, Word8)
import qualified Data.Binary as B
import qualified Data.Binary.Get as B
import qualified Data.Binary.Put as B
import Data.Bits (setBit, shiftL, shiftR, testBit, (.&.), (.|.))
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Base16 as Base16
import qualified Data.ByteString.Lazy as BSL
import Data.Foldable (foldl')
import Data.Maybe (fromMaybe)
import Data.String (fromString)
import Data.Text (Text)
import qualified Data.Text.Encoding as Text
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks (..))
import Numeric.Natural (Natural)
import Quiet
import Shelley.Spec.Ledger.BaseTypes (Network (..), networkToWord8, word8ToNetwork)
import Shelley.Spec.Ledger.Credential
  ( Credential (..),
    PaymentCredential,
    Ptr (..),
    StakeReference (..),
  )
import Shelley.Spec.Ledger.Keys
  ( KeyHash (..),
    KeyPair (..),
    KeyRole (..),
    hashKey,
  )
import Shelley.Spec.Ledger.Scripts
import Shelley.Spec.Ledger.Slot (SlotNo (..))

mkVKeyRwdAcnt ::
  Era era =>
  Network ->
  KeyPair 'Staking (Crypto era) ->
  RewardAcnt era
mkVKeyRwdAcnt :: Network -> KeyPair 'Staking (Crypto era) -> RewardAcnt era
mkVKeyRwdAcnt Network
network KeyPair 'Staking (Crypto era)
keys = Network -> Credential 'Staking era -> RewardAcnt era
forall era. Network -> Credential 'Staking era -> RewardAcnt era
RewardAcnt Network
network (Credential 'Staking era -> RewardAcnt era)
-> Credential 'Staking era -> RewardAcnt era
forall a b. (a -> b) -> a -> b
$ KeyHash 'Staking (Crypto era) -> Credential 'Staking era
forall (kr :: KeyRole) era.
KeyHash kr (Crypto era) -> Credential kr era
KeyHashObj (VKey 'Staking (Crypto era) -> KeyHash 'Staking (Crypto era)
forall crypto (kd :: KeyRole).
Crypto crypto =>
VKey kd crypto -> KeyHash kd crypto
hashKey (VKey 'Staking (Crypto era) -> KeyHash 'Staking (Crypto era))
-> VKey 'Staking (Crypto era) -> KeyHash 'Staking (Crypto era)
forall a b. (a -> b) -> a -> b
$ KeyPair 'Staking (Crypto era) -> VKey 'Staking (Crypto era)
forall (kd :: KeyRole) crypto. KeyPair kd crypto -> VKey kd crypto
vKey KeyPair 'Staking (Crypto era)
keys)

mkRwdAcnt ::
  Network ->
  Credential 'Staking era ->
  RewardAcnt era
mkRwdAcnt :: Network -> Credential 'Staking era -> RewardAcnt era
mkRwdAcnt Network
network script :: Credential 'Staking era
script@(ScriptHashObj ScriptHash era
_) = Network -> Credential 'Staking era -> RewardAcnt era
forall era. Network -> Credential 'Staking era -> RewardAcnt era
RewardAcnt Network
network Credential 'Staking era
script
mkRwdAcnt Network
network key :: Credential 'Staking era
key@(KeyHashObj KeyHash 'Staking (Crypto era)
_) = Network -> Credential 'Staking era -> RewardAcnt era
forall era. Network -> Credential 'Staking era -> RewardAcnt era
RewardAcnt Network
network Credential 'Staking era
key

toAddr ::
  Era era =>
  Network ->
  (KeyPair 'Payment (Crypto era), KeyPair 'Staking (Crypto era)) ->
  Addr era
toAddr :: Network
-> (KeyPair 'Payment (Crypto era), KeyPair 'Staking (Crypto era))
-> Addr era
toAddr Network
n (KeyPair 'Payment (Crypto era)
payKey, KeyPair 'Staking (Crypto era)
stakeKey) = Network -> PaymentCredential era -> StakeReference era -> Addr era
forall era.
Network -> PaymentCredential era -> StakeReference era -> Addr era
Addr Network
n (KeyPair 'Payment (Crypto era) -> PaymentCredential era
forall era (kr :: KeyRole).
Era era =>
KeyPair kr (Crypto era) -> Credential kr era
toCred KeyPair 'Payment (Crypto era)
payKey) (StakeCredential era -> StakeReference era
forall era. StakeCredential era -> StakeReference era
StakeRefBase (StakeCredential era -> StakeReference era)
-> StakeCredential era -> StakeReference era
forall a b. (a -> b) -> a -> b
$ KeyPair 'Staking (Crypto era) -> StakeCredential era
forall era (kr :: KeyRole).
Era era =>
KeyPair kr (Crypto era) -> Credential kr era
toCred KeyPair 'Staking (Crypto era)
stakeKey)

toCred ::
  (Era era) =>
  KeyPair kr (Crypto era) ->
  Credential kr era
toCred :: KeyPair kr (Crypto era) -> Credential kr era
toCred KeyPair kr (Crypto era)
k = KeyHash kr (Crypto era) -> Credential kr era
forall (kr :: KeyRole) era.
KeyHash kr (Crypto era) -> Credential kr era
KeyHashObj (KeyHash kr (Crypto era) -> Credential kr era)
-> (VKey kr (Crypto era) -> KeyHash kr (Crypto era))
-> VKey kr (Crypto era)
-> Credential kr era
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VKey kr (Crypto era) -> KeyHash kr (Crypto era)
forall crypto (kd :: KeyRole).
Crypto crypto =>
VKey kd crypto -> KeyHash kd crypto
hashKey (VKey kr (Crypto era) -> Credential kr era)
-> VKey kr (Crypto era) -> Credential kr era
forall a b. (a -> b) -> a -> b
$ KeyPair kr (Crypto era) -> VKey kr (Crypto era)
forall (kd :: KeyRole) crypto. KeyPair kd crypto -> VKey kd crypto
vKey KeyPair kr (Crypto era)
k

-- | Convert a given multi-sig script to a credential by hashing it and wrapping
-- into the `Credential` data type.
--
-- TODO nc what is the role of this credential?
scriptToCred :: Era era => MultiSig era -> Credential kr era
scriptToCred :: MultiSig era -> Credential kr era
scriptToCred = ScriptHash era -> Credential kr era
forall (kr :: KeyRole) era. ScriptHash era -> Credential kr era
ScriptHashObj (ScriptHash era -> Credential kr era)
-> (MultiSig era -> ScriptHash era)
-> MultiSig era
-> Credential kr era
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MultiSig era -> ScriptHash era
forall era. Era era => MultiSig era -> ScriptHash era
hashMultiSigScript

-- | Create a base address from a pair of multi-sig scripts (pay and stake)
scriptsToAddr :: Era era => Network -> (MultiSig era, MultiSig era) -> Addr era
scriptsToAddr :: Network -> (MultiSig era, MultiSig era) -> Addr era
scriptsToAddr Network
n (MultiSig era
payScript, MultiSig era
stakeScript) =
  Network -> PaymentCredential era -> StakeReference era -> Addr era
forall era.
Network -> PaymentCredential era -> StakeReference era -> Addr era
Addr Network
n (MultiSig era -> PaymentCredential era
forall era (kr :: KeyRole).
Era era =>
MultiSig era -> Credential kr era
scriptToCred MultiSig era
payScript) (StakeCredential era -> StakeReference era
forall era. StakeCredential era -> StakeReference era
StakeRefBase (StakeCredential era -> StakeReference era)
-> StakeCredential era -> StakeReference era
forall a b. (a -> b) -> a -> b
$ MultiSig era -> StakeCredential era
forall era (kr :: KeyRole).
Era era =>
MultiSig era -> Credential kr era
scriptToCred MultiSig era
stakeScript)

-- | Serialise an address to the external format.
serialiseAddr :: Addr era -> ByteString
serialiseAddr :: Addr era -> ByteString
serialiseAddr = ByteString -> ByteString
BSL.toStrict (ByteString -> ByteString)
-> (Addr era -> ByteString) -> Addr era -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Put -> ByteString
B.runPut (Put -> ByteString) -> (Addr era -> Put) -> Addr era -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Addr era -> Put
forall era. Addr era -> Put
putAddr

-- | Deserialise an address from the external format. This will fail if the
-- input data is not in the right format (or if there is trailing data).
deserialiseAddr :: Era era => ByteString -> Maybe (Addr era)
deserialiseAddr :: ByteString -> Maybe (Addr era)
deserialiseAddr ByteString
bs = case Get (Addr era)
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, Addr era)
forall a.
Get a
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
B.runGetOrFail Get (Addr era)
forall era. Era era => Get (Addr era)
getAddr (ByteString -> ByteString
BSL.fromStrict ByteString
bs) of
  Left (ByteString
_remaining, ByteOffset
_offset, String
_message) -> Maybe (Addr era)
forall a. Maybe a
Nothing
  Right (ByteString
_remaining, ByteOffset
_offset, Addr era
result) -> Addr era -> Maybe (Addr era)
forall a. a -> Maybe a
Just Addr era
result

-- | Deserialise a stake refence from a address. This will fail if this
-- is a Bootstrap address (or malformed).
deserialiseAddrStakeRef :: Era era => ByteString -> Maybe (StakeReference era)
deserialiseAddrStakeRef :: ByteString -> Maybe (StakeReference era)
deserialiseAddrStakeRef ByteString
bs = case Get (Maybe (StakeReference era))
-> ByteString
-> Either
     (ByteString, ByteOffset, String)
     (ByteString, ByteOffset, Maybe (StakeReference era))
forall a.
Get a
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
B.runGetOrFail Get (Maybe (StakeReference era))
forall era. Era era => Get (Maybe (StakeReference era))
getAddrStakeReference (ByteString -> ByteString
BSL.fromStrict ByteString
bs) of
  Left (ByteString
_remaining, ByteOffset
_offset, String
_message) -> Maybe (StakeReference era)
forall a. Maybe a
Nothing
  Right (ByteString
_remaining, ByteOffset
_offset, Maybe (StakeReference era)
result) -> Maybe (StakeReference era)
result

-- | Serialise a reward account to the external format.
serialiseRewardAcnt :: RewardAcnt era -> ByteString
serialiseRewardAcnt :: RewardAcnt era -> ByteString
serialiseRewardAcnt = ByteString -> ByteString
BSL.toStrict (ByteString -> ByteString)
-> (RewardAcnt era -> ByteString) -> RewardAcnt era -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Put -> ByteString
B.runPut (Put -> ByteString)
-> (RewardAcnt era -> Put) -> RewardAcnt era -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RewardAcnt era -> Put
forall era. RewardAcnt era -> Put
putRewardAcnt

-- | Deserialise an reward account from the external format. This will fail if the
-- input data is not in the right format (or if there is trailing data).
deserialiseRewardAcnt :: Era era => ByteString -> Maybe (RewardAcnt era)
deserialiseRewardAcnt :: ByteString -> Maybe (RewardAcnt era)
deserialiseRewardAcnt ByteString
bs = case Get (RewardAcnt era)
-> ByteString
-> Either
     (ByteString, ByteOffset, String)
     (ByteString, ByteOffset, RewardAcnt era)
forall a.
Get a
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
B.runGetOrFail Get (RewardAcnt era)
forall era. Era era => Get (RewardAcnt era)
getRewardAcnt (ByteString -> ByteString
BSL.fromStrict ByteString
bs) of
  Left (ByteString
_remaining, ByteOffset
_offset, String
_message) -> Maybe (RewardAcnt era)
forall a. Maybe a
Nothing
  Right (ByteString
_remaining, ByteOffset
_offset, RewardAcnt era
result) -> RewardAcnt era -> Maybe (RewardAcnt era)
forall a. a -> Maybe a
Just RewardAcnt era
result

-- | An address for UTxO.
data Addr era
  = Addr Network (PaymentCredential era) (StakeReference era)
  | AddrBootstrap (BootstrapAddress era)
  deriving (Int -> Addr era -> ShowS
[Addr era] -> ShowS
Addr era -> String
(Int -> Addr era -> ShowS)
-> (Addr era -> String) -> ([Addr era] -> ShowS) -> Show (Addr era)
forall era. Int -> Addr era -> ShowS
forall era. [Addr era] -> ShowS
forall era. Addr era -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Addr era] -> ShowS
$cshowList :: forall era. [Addr era] -> ShowS
show :: Addr era -> String
$cshow :: forall era. Addr era -> String
showsPrec :: Int -> Addr era -> ShowS
$cshowsPrec :: forall era. Int -> Addr era -> ShowS
Show, Addr era -> Addr era -> Bool
(Addr era -> Addr era -> Bool)
-> (Addr era -> Addr era -> Bool) -> Eq (Addr era)
forall era. Addr era -> Addr era -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Addr era -> Addr era -> Bool
$c/= :: forall era. Addr era -> Addr era -> Bool
== :: Addr era -> Addr era -> Bool
$c== :: forall era. Addr era -> Addr era -> Bool
Eq, (forall x. Addr era -> Rep (Addr era) x)
-> (forall x. Rep (Addr era) x -> Addr era) -> Generic (Addr era)
forall x. Rep (Addr era) x -> Addr era
forall x. Addr era -> Rep (Addr era) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall era x. Rep (Addr era) x -> Addr era
forall era x. Addr era -> Rep (Addr era) x
$cto :: forall era x. Rep (Addr era) x -> Addr era
$cfrom :: forall era x. Addr era -> Rep (Addr era) x
Generic, Addr era -> ()
(Addr era -> ()) -> NFData (Addr era)
forall era. Addr era -> ()
forall a. (a -> ()) -> NFData a
rnf :: Addr era -> ()
$crnf :: forall era. Addr era -> ()
NFData, Eq (Addr era)
Eq (Addr era)
-> (Addr era -> Addr era -> Ordering)
-> (Addr era -> Addr era -> Bool)
-> (Addr era -> Addr era -> Bool)
-> (Addr era -> Addr era -> Bool)
-> (Addr era -> Addr era -> Bool)
-> (Addr era -> Addr era -> Addr era)
-> (Addr era -> Addr era -> Addr era)
-> Ord (Addr era)
Addr era -> Addr era -> Bool
Addr era -> Addr era -> Ordering
Addr era -> Addr era -> Addr era
forall era. Eq (Addr era)
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall era. Addr era -> Addr era -> Bool
forall era. Addr era -> Addr era -> Ordering
forall era. Addr era -> Addr era -> Addr era
min :: Addr era -> Addr era -> Addr era
$cmin :: forall era. Addr era -> Addr era -> Addr era
max :: Addr era -> Addr era -> Addr era
$cmax :: forall era. Addr era -> Addr era -> Addr era
>= :: Addr era -> Addr era -> Bool
$c>= :: forall era. Addr era -> Addr era -> Bool
> :: Addr era -> Addr era -> Bool
$c> :: forall era. Addr era -> Addr era -> Bool
<= :: Addr era -> Addr era -> Bool
$c<= :: forall era. Addr era -> Addr era -> Bool
< :: Addr era -> Addr era -> Bool
$c< :: forall era. Addr era -> Addr era -> Bool
compare :: Addr era -> Addr era -> Ordering
$ccompare :: forall era. Addr era -> Addr era -> Ordering
$cp1Ord :: forall era. Eq (Addr era)
Ord)

getNetwork :: Addr era -> Network
getNetwork :: Addr era -> Network
getNetwork (Addr Network
n PaymentCredential era
_ StakeReference era
_) = Network
n
getNetwork (AddrBootstrap (BootstrapAddress Address
byronAddr)) =
  case AddrAttributes -> NetworkMagic
Byron.aaNetworkMagic (AddrAttributes -> NetworkMagic)
-> (Address -> AddrAttributes) -> Address -> NetworkMagic
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Attributes AddrAttributes -> AddrAttributes
forall h. Attributes h -> h
Byron.attrData (Attributes AddrAttributes -> AddrAttributes)
-> (Address -> Attributes AddrAttributes)
-> Address
-> AddrAttributes
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Address -> Attributes AddrAttributes
Byron.addrAttributes (Address -> NetworkMagic) -> Address -> NetworkMagic
forall a b. (a -> b) -> a -> b
$ Address
byronAddr of
    NetworkMagic
Byron.NetworkMainOrStage -> Network
Mainnet
    Byron.NetworkTestnet Word32
_ -> Network
Testnet

instance NoThunks (Addr era)

-- | An account based address for rewards
data RewardAcnt era = RewardAcnt
  { RewardAcnt era -> Network
getRwdNetwork :: !Network,
    RewardAcnt era -> Credential 'Staking era
getRwdCred :: !(Credential 'Staking era)
  }
  deriving (Int -> RewardAcnt era -> ShowS
[RewardAcnt era] -> ShowS
RewardAcnt era -> String
(Int -> RewardAcnt era -> ShowS)
-> (RewardAcnt era -> String)
-> ([RewardAcnt era] -> ShowS)
-> Show (RewardAcnt era)
forall era. Int -> RewardAcnt era -> ShowS
forall era. [RewardAcnt era] -> ShowS
forall era. RewardAcnt era -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RewardAcnt era] -> ShowS
$cshowList :: forall era. [RewardAcnt era] -> ShowS
show :: RewardAcnt era -> String
$cshow :: forall era. RewardAcnt era -> String
showsPrec :: Int -> RewardAcnt era -> ShowS
$cshowsPrec :: forall era. Int -> RewardAcnt era -> ShowS
Show, RewardAcnt era -> RewardAcnt era -> Bool
(RewardAcnt era -> RewardAcnt era -> Bool)
-> (RewardAcnt era -> RewardAcnt era -> Bool)
-> Eq (RewardAcnt era)
forall era. RewardAcnt era -> RewardAcnt era -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RewardAcnt era -> RewardAcnt era -> Bool
$c/= :: forall era. RewardAcnt era -> RewardAcnt era -> Bool
== :: RewardAcnt era -> RewardAcnt era -> Bool
$c== :: forall era. RewardAcnt era -> RewardAcnt era -> Bool
Eq, (forall x. RewardAcnt era -> Rep (RewardAcnt era) x)
-> (forall x. Rep (RewardAcnt era) x -> RewardAcnt era)
-> Generic (RewardAcnt era)
forall x. Rep (RewardAcnt era) x -> RewardAcnt era
forall x. RewardAcnt era -> Rep (RewardAcnt era) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall era x. Rep (RewardAcnt era) x -> RewardAcnt era
forall era x. RewardAcnt era -> Rep (RewardAcnt era) x
$cto :: forall era x. Rep (RewardAcnt era) x -> RewardAcnt era
$cfrom :: forall era x. RewardAcnt era -> Rep (RewardAcnt era) x
Generic, Eq (RewardAcnt era)
Eq (RewardAcnt era)
-> (RewardAcnt era -> RewardAcnt era -> Ordering)
-> (RewardAcnt era -> RewardAcnt era -> Bool)
-> (RewardAcnt era -> RewardAcnt era -> Bool)
-> (RewardAcnt era -> RewardAcnt era -> Bool)
-> (RewardAcnt era -> RewardAcnt era -> Bool)
-> (RewardAcnt era -> RewardAcnt era -> RewardAcnt era)
-> (RewardAcnt era -> RewardAcnt era -> RewardAcnt era)
-> Ord (RewardAcnt era)
RewardAcnt era -> RewardAcnt era -> Bool
RewardAcnt era -> RewardAcnt era -> Ordering
RewardAcnt era -> RewardAcnt era -> RewardAcnt era
forall era. Eq (RewardAcnt era)
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall era. RewardAcnt era -> RewardAcnt era -> Bool
forall era. RewardAcnt era -> RewardAcnt era -> Ordering
forall era. RewardAcnt era -> RewardAcnt era -> RewardAcnt era
min :: RewardAcnt era -> RewardAcnt era -> RewardAcnt era
$cmin :: forall era. RewardAcnt era -> RewardAcnt era -> RewardAcnt era
max :: RewardAcnt era -> RewardAcnt era -> RewardAcnt era
$cmax :: forall era. RewardAcnt era -> RewardAcnt era -> RewardAcnt era
>= :: RewardAcnt era -> RewardAcnt era -> Bool
$c>= :: forall era. RewardAcnt era -> RewardAcnt era -> Bool
> :: RewardAcnt era -> RewardAcnt era -> Bool
$c> :: forall era. RewardAcnt era -> RewardAcnt era -> Bool
<= :: RewardAcnt era -> RewardAcnt era -> Bool
$c<= :: forall era. RewardAcnt era -> RewardAcnt era -> Bool
< :: RewardAcnt era -> RewardAcnt era -> Bool
$c< :: forall era. RewardAcnt era -> RewardAcnt era -> Bool
compare :: RewardAcnt era -> RewardAcnt era -> Ordering
$ccompare :: forall era. RewardAcnt era -> RewardAcnt era -> Ordering
$cp1Ord :: forall era. Eq (RewardAcnt era)
Ord, RewardAcnt era -> ()
(RewardAcnt era -> ()) -> NFData (RewardAcnt era)
forall era. RewardAcnt era -> ()
forall a. (a -> ()) -> NFData a
rnf :: RewardAcnt era -> ()
$crnf :: forall era. RewardAcnt era -> ()
NFData, ToJSONKeyFunction [RewardAcnt era]
ToJSONKeyFunction (RewardAcnt era)
ToJSONKeyFunction (RewardAcnt era)
-> ToJSONKeyFunction [RewardAcnt era] -> ToJSONKey (RewardAcnt era)
forall era. Era era => ToJSONKeyFunction [RewardAcnt era]
forall era. Era era => ToJSONKeyFunction (RewardAcnt era)
forall a.
ToJSONKeyFunction a -> ToJSONKeyFunction [a] -> ToJSONKey a
toJSONKeyList :: ToJSONKeyFunction [RewardAcnt era]
$ctoJSONKeyList :: forall era. Era era => ToJSONKeyFunction [RewardAcnt era]
toJSONKey :: ToJSONKeyFunction (RewardAcnt era)
$ctoJSONKey :: forall era. Era era => ToJSONKeyFunction (RewardAcnt era)
ToJSONKey, FromJSONKeyFunction [RewardAcnt era]
FromJSONKeyFunction (RewardAcnt era)
FromJSONKeyFunction (RewardAcnt era)
-> FromJSONKeyFunction [RewardAcnt era]
-> FromJSONKey (RewardAcnt era)
forall era. Era era => FromJSONKeyFunction [RewardAcnt era]
forall era. Era era => FromJSONKeyFunction (RewardAcnt era)
forall a.
FromJSONKeyFunction a -> FromJSONKeyFunction [a] -> FromJSONKey a
fromJSONKeyList :: FromJSONKeyFunction [RewardAcnt era]
$cfromJSONKeyList :: forall era. Era era => FromJSONKeyFunction [RewardAcnt era]
fromJSONKey :: FromJSONKeyFunction (RewardAcnt era)
$cfromJSONKey :: forall era. Era era => FromJSONKeyFunction (RewardAcnt era)
FromJSONKey)

instance Era era => ToJSON (RewardAcnt era) where
  toJSON :: RewardAcnt era -> Value
toJSON RewardAcnt era
ra =
    [Pair] -> Value
Aeson.object
      [ Text
"network" Text -> Network -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= RewardAcnt era -> Network
forall era. RewardAcnt era -> Network
getRwdNetwork RewardAcnt era
ra,
        Text
"credential" Text -> Credential 'Staking era -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= RewardAcnt era -> Credential 'Staking era
forall era. RewardAcnt era -> Credential 'Staking era
getRwdCred RewardAcnt era
ra
      ]

instance Era era => FromJSON (RewardAcnt era) where
  parseJSON :: Value -> Parser (RewardAcnt era)
parseJSON =
    String
-> (Object -> Parser (RewardAcnt era))
-> Value
-> Parser (RewardAcnt era)
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Aeson.withObject String
"RewardAcnt" ((Object -> Parser (RewardAcnt era))
 -> Value -> Parser (RewardAcnt era))
-> (Object -> Parser (RewardAcnt era))
-> Value
-> Parser (RewardAcnt era)
forall a b. (a -> b) -> a -> b
$ \Object
obj ->
      Network -> Credential 'Staking era -> RewardAcnt era
forall era. Network -> Credential 'Staking era -> RewardAcnt era
RewardAcnt
        (Network -> Credential 'Staking era -> RewardAcnt era)
-> Parser Network
-> Parser (Credential 'Staking era -> RewardAcnt era)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
obj Object -> Text -> Parser Network
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"network"
        Parser (Credential 'Staking era -> RewardAcnt era)
-> Parser (Credential 'Staking era) -> Parser (RewardAcnt era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Text -> Parser (Credential 'Staking era)
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"credential"

instance NoThunks (RewardAcnt era)

instance Era era => ToJSONKey (Addr era) where
  toJSONKey :: ToJSONKeyFunction (Addr era)
toJSONKey = (Addr era -> Text)
-> (Addr era -> Encoding' Text) -> ToJSONKeyFunction (Addr era)
forall a.
(a -> Text) -> (a -> Encoding' Text) -> ToJSONKeyFunction a
Aeson.ToJSONKeyText Addr era -> Text
forall era. Addr era -> Text
addrToText (Text -> Encoding' Text
forall a. Text -> Encoding' a
Aeson.text (Text -> Encoding' Text)
-> (Addr era -> Text) -> Addr era -> Encoding' Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Addr era -> Text
forall era. Addr era -> Text
addrToText)

instance Era era => FromJSONKey (Addr era) where
  fromJSONKey :: FromJSONKeyFunction (Addr era)
fromJSONKey = (Text -> Parser (Addr era)) -> FromJSONKeyFunction (Addr era)
forall a. (Text -> Parser a) -> FromJSONKeyFunction a
Aeson.FromJSONKeyTextParser Text -> Parser (Addr era)
forall era. Era era => Text -> Parser (Addr era)
parseAddr

instance Era era => ToJSON (Addr era) where
  toJSON :: Addr era -> Value
toJSON = Text -> Value
forall a. ToJSON a => a -> Value
toJSON (Text -> Value) -> (Addr era -> Text) -> Addr era -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Addr era -> Text
forall era. Addr era -> Text
addrToText

instance Era era => FromJSON (Addr era) where
  parseJSON :: Value -> Parser (Addr era)
parseJSON = String -> (Text -> Parser (Addr era)) -> Value -> Parser (Addr era)
forall a. String -> (Text -> Parser a) -> Value -> Parser a
Aeson.withText String
"address" Text -> Parser (Addr era)
forall era. Era era => Text -> Parser (Addr era)
parseAddr

addrToText :: Addr era -> Text
addrToText :: Addr era -> Text
addrToText =
  ByteString -> Text
Text.decodeLatin1 (ByteString -> Text)
-> (Addr era -> ByteString) -> Addr era -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
Base16.encode (ByteString -> ByteString)
-> (Addr era -> ByteString) -> Addr era -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Addr era -> ByteString
forall era. Addr era -> ByteString
serialiseAddr

parseAddr :: Era era => Text -> Aeson.Parser (Addr era)
parseAddr :: Text -> Parser (Addr era)
parseAddr Text
t = do
  ByteString
bytes <- (Base16ParseError -> Parser ByteString)
-> (ByteString -> Parser ByteString)
-> Either Base16ParseError ByteString
-> Parser ByteString
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Base16ParseError -> Parser ByteString
forall (m :: * -> *) a a. (MonadFail m, Show a) => a -> m a
badHex ByteString -> Parser ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> Either Base16ParseError ByteString
parseBase16 Text
t)
  Parser (Addr era)
-> (Addr era -> Parser (Addr era))
-> Maybe (Addr era)
-> Parser (Addr era)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Parser (Addr era)
forall a. Parser a
badFormat Addr era -> Parser (Addr era)
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> Maybe (Addr era)
forall era. Era era => ByteString -> Maybe (Addr era)
deserialiseAddr ByteString
bytes)
  where
    badHex :: a -> m a
badHex a
h = String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m a) -> String -> m a
forall a b. (a -> b) -> a -> b
$ String
"Addresses are expected in hex encoding for now: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
h
    badFormat :: Parser a
badFormat = String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Address is not in the right format"

byron :: Int
byron :: Int
byron = Int
7

notBaseAddr :: Int
notBaseAddr :: Int
notBaseAddr = Int
6

isEnterpriseAddr :: Int
isEnterpriseAddr :: Int
isEnterpriseAddr = Int
5

stakeCredIsScript :: Int
stakeCredIsScript :: Int
stakeCredIsScript = Int
5

payCredIsScript :: Int
payCredIsScript :: Int
payCredIsScript = Int
4

rewardCredIsScript :: Int
rewardCredIsScript :: Int
rewardCredIsScript = Int
4

putAddr :: Addr era -> Put
putAddr :: Addr era -> Put
putAddr (AddrBootstrap (BootstrapAddress Address
byronAddr)) = ByteString -> Put
B.putLazyByteString (Address -> ByteString
forall a. ToCBOR a => a -> ByteString
serialize Address
byronAddr)
putAddr (Addr Network
network PaymentCredential era
pc StakeReference era
sr) =
  let setPayCredBit :: Word8 -> Word8
setPayCredBit = case PaymentCredential era
pc of
        ScriptHashObj ScriptHash era
_ -> (Word8 -> Int -> Word8) -> Int -> Word8 -> Word8
forall a b c. (a -> b -> c) -> b -> a -> c
flip Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
setBit Int
payCredIsScript
        KeyHashObj KeyHash 'Payment (Crypto era)
_ -> Word8 -> Word8
forall a. a -> a
id
      netId :: Word8
netId = Network -> Word8
networkToWord8 Network
network
   in case StakeReference era
sr of
        StakeRefBase StakeCredential era
sc -> do
          let setStakeCredBit :: Word8 -> Word8
setStakeCredBit = case StakeCredential era
sc of
                ScriptHashObj ScriptHash era
_ -> (Word8 -> Int -> Word8) -> Int -> Word8 -> Word8
forall a b c. (a -> b -> c) -> b -> a -> c
flip Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
setBit Int
stakeCredIsScript
                KeyHashObj KeyHash 'Staking (Crypto era)
_ -> Word8 -> Word8
forall a. a -> a
id
              header :: Word8
header = Word8 -> Word8
setStakeCredBit (Word8 -> Word8) -> (Word8 -> Word8) -> Word8 -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Word8
setPayCredBit (Word8 -> Word8) -> Word8 -> Word8
forall a b. (a -> b) -> a -> b
$ Word8
netId
          Word8 -> Put
B.putWord8 Word8
header
          PaymentCredential era -> Put
forall (kr :: KeyRole) era. Credential kr era -> Put
putCredential PaymentCredential era
pc
          StakeCredential era -> Put
forall (kr :: KeyRole) era. Credential kr era -> Put
putCredential StakeCredential era
sc
        StakeRefPtr Ptr
ptr -> do
          let header :: Word8
header = Word8 -> Word8
setPayCredBit (Word8 -> Word8) -> Word8 -> Word8
forall a b. (a -> b) -> a -> b
$ Word8
netId Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`setBit` Int
notBaseAddr
          Word8 -> Put
B.putWord8 Word8
header
          PaymentCredential era -> Put
forall (kr :: KeyRole) era. Credential kr era -> Put
putCredential PaymentCredential era
pc
          Ptr -> Put
putPtr Ptr
ptr
        StakeReference era
StakeRefNull -> do
          let header :: Word8
header = Word8 -> Word8
setPayCredBit (Word8 -> Word8) -> Word8 -> Word8
forall a b. (a -> b) -> a -> b
$ Word8
netId Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`setBit` Int
isEnterpriseAddr Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`setBit` Int
notBaseAddr
          Word8 -> Put
B.putWord8 Word8
header
          PaymentCredential era -> Put
forall (kr :: KeyRole) era. Credential kr era -> Put
putCredential PaymentCredential era
pc

getAddr :: forall era. Era era => Get (Addr era)
getAddr :: Get (Addr era)
getAddr = do
  Word8
header <- Get Word8 -> Get Word8
forall a. Get a -> Get a
B.lookAhead Get Word8
B.getWord8
  if Word8 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word8
header Int
byron
    then Get (Addr era)
forall era. Get (Addr era)
getByron
    else do
      Word8
_ <- Get Word8
B.getWord8 -- read past the header byte
      let addrNetId :: Word8
addrNetId = Word8
header Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x0F -- 0b00001111 is the mask for the network id
      case Word8 -> Maybe Network
word8ToNetwork Word8
addrNetId of
        Just Network
n -> Network -> PaymentCredential era -> StakeReference era -> Addr era
forall era.
Network -> PaymentCredential era -> StakeReference era -> Addr era
Addr Network
n (PaymentCredential era -> StakeReference era -> Addr era)
-> Get (PaymentCredential era)
-> Get (StakeReference era -> Addr era)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Word8 -> Get (PaymentCredential era)
forall era. Era era => Word8 -> Get (PaymentCredential era)
getPayCred Word8
header Get (StakeReference era -> Addr era)
-> Get (StakeReference era) -> Get (Addr era)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Word8 -> Get (StakeReference era)
forall era. Era era => Word8 -> Get (StakeReference era)
getStakeReference Word8
header
        Maybe Network
Nothing ->
          String -> Get (Addr era)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Get (Addr era)) -> String -> Get (Addr era)
forall a b. (a -> b) -> a -> b
$
            Context -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
              [String
"Address with unknown network Id. (", Word8 -> String
forall a. Show a => a -> String
show Word8
addrNetId, String
")"]

-- | We are "expecting" an address, but we are only interested in the StakeReference.
--   If the Addr is A Byron style address, there are no Stake References, return Nothing.
getAddrStakeReference :: forall era. Era era => Get (Maybe (StakeReference era))
getAddrStakeReference :: Get (Maybe (StakeReference era))
getAddrStakeReference = do
  Word8
header <- Get Word8
B.getWord8
  if Word8 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word8
header Int
byron
    then Maybe (StakeReference era) -> Get (Maybe (StakeReference era))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (StakeReference era)
forall a. Maybe a
Nothing
    else [ADDRHASH (Crypto era)] -> Get ()
forall (proxy :: * -> *) h. HashAlgorithm h => proxy h -> Get ()
skipHash ([] @(ADDRHASH (Crypto era))) Get ()
-> Get (Maybe (StakeReference era))
-> Get (Maybe (StakeReference era))
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> StakeReference era -> Maybe (StakeReference era)
forall a. a -> Maybe a
Just (StakeReference era -> Maybe (StakeReference era))
-> Get (StakeReference era) -> Get (Maybe (StakeReference era))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Word8 -> Get (StakeReference era)
forall era. Era era => Word8 -> Get (StakeReference era)
getStakeReference Word8
header

putRewardAcnt :: RewardAcnt era -> Put
putRewardAcnt :: RewardAcnt era -> Put
putRewardAcnt (RewardAcnt Network
network Credential 'Staking era
cred) = do
  let setPayCredBit :: Word8 -> Word8
setPayCredBit = case Credential 'Staking era
cred of
        ScriptHashObj ScriptHash era
_ -> (Word8 -> Int -> Word8) -> Int -> Word8 -> Word8
forall a b c. (a -> b -> c) -> b -> a -> c
flip Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
setBit Int
payCredIsScript
        KeyHashObj KeyHash 'Staking (Crypto era)
_ -> Word8 -> Word8
forall a. a -> a
id
      netId :: Word8
netId = Network -> Word8
networkToWord8 Network
network
      rewardAcntPrefix :: Word8
rewardAcntPrefix = Word8
0xE0 -- 0b1110000 are always set for reward accounts
      header :: Word8
header = Word8 -> Word8
setPayCredBit (Word8
netId Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
rewardAcntPrefix)
  Word8 -> Put
B.putWord8 Word8
header
  Credential 'Staking era -> Put
forall (kr :: KeyRole) era. Credential kr era -> Put
putCredential Credential 'Staking era
cred

getRewardAcnt :: forall era. Era era => Get (RewardAcnt era)
getRewardAcnt :: Get (RewardAcnt era)
getRewardAcnt = do
  Word8
header <- Get Word8
B.getWord8
  let rewardAcntPrefix :: Word8
rewardAcntPrefix = Word8
0xE0 -- 0b1110000 are always set for reward accounts
      isRewardAcnt :: Bool
isRewardAcnt = (Word8
header Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
rewardAcntPrefix) Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
rewardAcntPrefix
      netId :: Word8
netId = Word8
header Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x0F -- 0b00001111 is the mask for the network id
  case (Word8 -> Maybe Network
word8ToNetwork Word8
netId, Bool
isRewardAcnt) of
    (Maybe Network
Nothing, Bool
_) ->
      String -> Get (RewardAcnt era)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Get (RewardAcnt era)) -> String -> Get (RewardAcnt era)
forall a b. (a -> b) -> a -> b
$ Context -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [String
"Reward account with unknown network Id. (", Word8 -> String
forall a. Show a => a -> String
show Word8
netId, String
")"]
    (Maybe Network
_, Bool
False) ->
      String -> Get (RewardAcnt era)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Get (RewardAcnt era)) -> String -> Get (RewardAcnt era)
forall a b. (a -> b) -> a -> b
$ Context -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [String
"Expected reward account. Got account with header: ", Word8 -> String
forall a. Show a => a -> String
show Word8
header]
    (Just Network
network, Bool
True) -> do
      Credential 'Staking era
cred <- case Word8 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word8
header Int
rewardCredIsScript of
        Bool
True -> Get (Credential 'Staking era)
forall era (kr :: KeyRole). Era era => Get (Credential kr era)
getScriptHash
        Bool
False -> Get (Credential 'Staking era)
forall era (kr :: KeyRole). Era era => Get (Credential kr era)
getKeyHash
      RewardAcnt era -> Get (RewardAcnt era)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (RewardAcnt era -> Get (RewardAcnt era))
-> RewardAcnt era -> Get (RewardAcnt era)
forall a b. (a -> b) -> a -> b
$ Network -> Credential 'Staking era -> RewardAcnt era
forall era. Network -> Credential 'Staking era -> RewardAcnt era
RewardAcnt Network
network Credential 'Staking era
cred

skipHash :: forall proxy h. Hash.HashAlgorithm h => proxy h -> Get ()
skipHash :: proxy h -> Get ()
skipHash proxy h
p = Int -> Get ()
B.skip (Int -> Get ()) -> (Word -> Int) -> Word -> Get ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word -> Get ()) -> Word -> Get ()
forall a b. (a -> b) -> a -> b
$ proxy h -> Word
forall h (proxy :: * -> *). HashAlgorithm h => proxy h -> Word
Hash.sizeHash proxy h
p

getHash :: forall h a. Hash.HashAlgorithm h => Get (Hash.Hash h a)
getHash :: Get (Hash h a)
getHash = do
  ByteString
bytes <- Int -> Get ByteString
B.getByteString (Int -> Get ByteString) -> (Word -> Int) -> Word -> Get ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word -> Get ByteString) -> Word -> Get ByteString
forall a b. (a -> b) -> a -> b
$ [h] -> Word
forall h (proxy :: * -> *). HashAlgorithm h => proxy h -> Word
Hash.sizeHash ([] @h)
  case ByteString -> Maybe (Hash h a)
forall h a. HashAlgorithm h => ByteString -> Maybe (Hash h a)
Hash.hashFromBytes ByteString
bytes of
    Maybe (Hash h a)
Nothing -> String -> Get (Hash h a)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"getHash: implausible hash length mismatch"
    Just Hash h a
h -> Hash h a -> Get (Hash h a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Hash h a
h

putHash :: Hash.Hash h a -> Put
putHash :: Hash h a -> Put
putHash = ByteString -> Put
B.putByteString (ByteString -> Put) -> (Hash h a -> ByteString) -> Hash h a -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash h a -> ByteString
forall h a. Hash h a -> ByteString
Hash.hashToBytes

getPayCred :: Era era => Word8 -> Get (PaymentCredential era)
getPayCred :: Word8 -> Get (PaymentCredential era)
getPayCred Word8
header = case Word8 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word8
header Int
payCredIsScript of
  Bool
True -> Get (PaymentCredential era)
forall era (kr :: KeyRole). Era era => Get (Credential kr era)
getScriptHash
  Bool
False -> Get (PaymentCredential era)
forall era (kr :: KeyRole). Era era => Get (Credential kr era)
getKeyHash

getScriptHash :: Era era => Get (Credential kr era)
getScriptHash :: Get (Credential kr era)
getScriptHash = ScriptHash era -> Credential kr era
forall (kr :: KeyRole) era. ScriptHash era -> Credential kr era
ScriptHashObj (ScriptHash era -> Credential kr era)
-> (Hash (ADDRHASH (Crypto era)) (Script era) -> ScriptHash era)
-> Hash (ADDRHASH (Crypto era)) (Script era)
-> Credential kr era
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash (ADDRHASH (Crypto era)) (Script era) -> ScriptHash era
forall era.
Hash (ADDRHASH (Crypto era)) (Script era) -> ScriptHash era
ScriptHash (Hash (ADDRHASH (Crypto era)) (Script era) -> Credential kr era)
-> Get (Hash (ADDRHASH (Crypto era)) (Script era))
-> Get (Credential kr era)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get (Hash (ADDRHASH (Crypto era)) (Script era))
forall h a. HashAlgorithm h => Get (Hash h a)
getHash

getKeyHash :: (Era era) => Get (Credential kr era)
getKeyHash :: Get (Credential kr era)
getKeyHash = KeyHash kr (Crypto era) -> Credential kr era
forall (kr :: KeyRole) era.
KeyHash kr (Crypto era) -> Credential kr era
KeyHashObj (KeyHash kr (Crypto era) -> Credential kr era)
-> (Hash (ADDRHASH (Crypto era)) (VerKeyDSIGN (DSIGN (Crypto era)))
    -> KeyHash kr (Crypto era))
-> Hash (ADDRHASH (Crypto era)) (VerKeyDSIGN (DSIGN (Crypto era)))
-> Credential kr era
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash (ADDRHASH (Crypto era)) (VerKeyDSIGN (DSIGN (Crypto era)))
-> KeyHash kr (Crypto era)
forall (discriminator :: KeyRole) crypto.
Hash (ADDRHASH crypto) (VerKeyDSIGN (DSIGN crypto))
-> KeyHash discriminator crypto
KeyHash (Hash (ADDRHASH (Crypto era)) (VerKeyDSIGN (DSIGN (Crypto era)))
 -> Credential kr era)
-> Get
     (Hash (ADDRHASH (Crypto era)) (VerKeyDSIGN (DSIGN (Crypto era))))
-> Get (Credential kr era)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get
  (Hash (ADDRHASH (Crypto era)) (VerKeyDSIGN (DSIGN (Crypto era))))
forall h a. HashAlgorithm h => Get (Hash h a)
getHash

getStakeReference :: Era era => Word8 -> Get (StakeReference era)
getStakeReference :: Word8 -> Get (StakeReference era)
getStakeReference Word8
header = case Word8 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word8
header Int
notBaseAddr of
  Bool
True -> case Word8 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word8
header Int
isEnterpriseAddr of
    Bool
True -> StakeReference era -> Get (StakeReference era)
forall (f :: * -> *) a. Applicative f => a -> f a
pure StakeReference era
forall era. StakeReference era
StakeRefNull
    Bool
False -> Ptr -> StakeReference era
forall era. Ptr -> StakeReference era
StakeRefPtr (Ptr -> StakeReference era) -> Get Ptr -> Get (StakeReference era)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Ptr
getPtr
  Bool
False -> case Word8 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word8
header Int
stakeCredIsScript of
    Bool
True -> StakeCredential era -> StakeReference era
forall era. StakeCredential era -> StakeReference era
StakeRefBase (StakeCredential era -> StakeReference era)
-> Get (StakeCredential era) -> Get (StakeReference era)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get (StakeCredential era)
forall era (kr :: KeyRole). Era era => Get (Credential kr era)
getScriptHash
    Bool
False -> StakeCredential era -> StakeReference era
forall era. StakeCredential era -> StakeReference era
StakeRefBase (StakeCredential era -> StakeReference era)
-> Get (StakeCredential era) -> Get (StakeReference era)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get (StakeCredential era)
forall era (kr :: KeyRole). Era era => Get (Credential kr era)
getKeyHash

putCredential :: Credential kr era -> Put
putCredential :: Credential kr era -> Put
putCredential (ScriptHashObj (ScriptHash Hash (ADDRHASH (Crypto era)) (Script era)
h)) = Hash (ADDRHASH (Crypto era)) (Script era) -> Put
forall h a. Hash h a -> Put
putHash Hash (ADDRHASH (Crypto era)) (Script era)
h
putCredential (KeyHashObj (KeyHash Hash (ADDRHASH (Crypto era)) (VerKeyDSIGN (DSIGN (Crypto era)))
h)) = Hash (ADDRHASH (Crypto era)) (VerKeyDSIGN (DSIGN (Crypto era)))
-> Put
forall h a. Hash h a -> Put
putHash Hash (ADDRHASH (Crypto era)) (VerKeyDSIGN (DSIGN (Crypto era)))
h

getByron :: Get (Addr era)
getByron :: Get (Addr era)
getByron =
  ByteString -> Either DecoderError Address
forall a. FromCBOR a => ByteString -> Either DecoderError a
decodeFull (ByteString -> Either DecoderError Address)
-> Get ByteString -> Get (Either DecoderError Address)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get ByteString
B.getRemainingLazyByteString Get (Either DecoderError Address)
-> (Either DecoderError Address -> Get (Addr era))
-> Get (Addr era)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Left DecoderError
e -> String -> Get (Addr era)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (DecoderError -> String
forall a. Show a => a -> String
show DecoderError
e)
    Right Address
r -> Addr era -> Get (Addr era)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Addr era -> Get (Addr era)) -> Addr era -> Get (Addr era)
forall a b. (a -> b) -> a -> b
$ BootstrapAddress era -> Addr era
forall era. BootstrapAddress era -> Addr era
AddrBootstrap (BootstrapAddress era -> Addr era)
-> BootstrapAddress era -> Addr era
forall a b. (a -> b) -> a -> b
$ Address -> BootstrapAddress era
forall era. Address -> BootstrapAddress era
BootstrapAddress Address
r

-- | The size of the extra attributes in a bootstrp (ie Byron) address. Used
-- to help enforce that people do not post huge ones on the chain.
bootstrapAddressAttrsSize :: BootstrapAddress era -> Int
bootstrapAddressAttrsSize :: BootstrapAddress era -> Int
bootstrapAddressAttrsSize (BootstrapAddress Address
addr) =
  -- I'm sorry this code is formatted so weridly below.
  -- It is to apease the capricious god Ormolu. A sacrifice is demanded!
  Int -> (HDAddressPayload -> Int) -> Maybe HDAddressPayload -> Int
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
    Int
0
    (ByteString -> Int
BS.length (ByteString -> Int)
-> (HDAddressPayload -> ByteString) -> HDAddressPayload -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HDAddressPayload -> ByteString
Byron.getHDAddressPayload)
    (AddrAttributes -> Maybe HDAddressPayload
Byron.aaVKDerivationPath (Attributes AddrAttributes -> AddrAttributes
forall h. Attributes h -> h
Byron.attrData Attributes AddrAttributes
attrs))
    Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Attributes AddrAttributes -> Int
forall a. Attributes a -> Int
Byron.unknownAttributesLength Attributes AddrAttributes
attrs
  where
    attrs :: Attributes AddrAttributes
attrs = Address -> Attributes AddrAttributes
Byron.addrAttributes Address
addr

-- | Return True if a given address is a redeemer address from the Byron Era
isBootstrapRedeemer :: Addr era -> Bool
isBootstrapRedeemer :: Addr era -> Bool
isBootstrapRedeemer (AddrBootstrap (BootstrapAddress (Byron.Address AddressHash Address'
_ Attributes AddrAttributes
_ AddrType
Byron.ATRedeem))) = Bool
True
isBootstrapRedeemer Addr era
_ = Bool
False

putPtr :: Ptr -> Put
putPtr :: Ptr -> Put
putPtr (Ptr SlotNo
slot Ix
txIx Ix
certIx) = do
  SlotNo -> Put
putSlot SlotNo
slot
  Ix -> Put
putVariableLengthNat Ix
txIx
  Ix -> Put
putVariableLengthNat Ix
certIx
  where
    putSlot :: SlotNo -> Put
putSlot (SlotNo Word64
n) = Ix -> Put
putVariableLengthNat (Ix -> Put) -> (Word64 -> Ix) -> Word64 -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> Ix
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Put) -> Word64 -> Put
forall a b. (a -> b) -> a -> b
$ Word64
n

getPtr :: Get Ptr
getPtr :: Get Ptr
getPtr =
  SlotNo -> Ix -> Ix -> Ptr
Ptr (SlotNo -> Ix -> Ix -> Ptr) -> Get SlotNo -> Get (Ix -> Ix -> Ptr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Word64 -> SlotNo
SlotNo (Word64 -> SlotNo) -> (Ix -> Word64) -> Ix -> SlotNo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ix -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Ix -> SlotNo) -> Get Ix -> Get SlotNo
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Ix
getVariableLengthNat)
    Get (Ix -> Ix -> Ptr) -> Get Ix -> Get (Ix -> Ptr)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Ix
getVariableLengthNat
    Get (Ix -> Ptr) -> Get Ix -> Get Ptr
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Ix
getVariableLengthNat

newtype Word7 = Word7 Word8
  deriving (Word7 -> Word7 -> Bool
(Word7 -> Word7 -> Bool) -> (Word7 -> Word7 -> Bool) -> Eq Word7
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Word7 -> Word7 -> Bool
$c/= :: Word7 -> Word7 -> Bool
== :: Word7 -> Word7 -> Bool
$c== :: Word7 -> Word7 -> Bool
Eq, Int -> Word7 -> ShowS
[Word7] -> ShowS
Word7 -> String
(Int -> Word7 -> ShowS)
-> (Word7 -> String) -> ([Word7] -> ShowS) -> Show Word7
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Word7] -> ShowS
$cshowList :: [Word7] -> ShowS
show :: Word7 -> String
$cshow :: Word7 -> String
showsPrec :: Int -> Word7 -> ShowS
$cshowsPrec :: Int -> Word7 -> ShowS
Show)

toWord7 :: Word8 -> Word7
toWord7 :: Word8 -> Word7
toWord7 Word8
x = Word8 -> Word7
Word7 (Word8
x Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x7F) -- 0x7F = 0b01111111

putWord7s :: [Word7] -> Put
putWord7s :: [Word7] -> Put
putWord7s [] = () -> Put
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
putWord7s [Word7 Word8
x] = Word8 -> Put
B.putWord8 Word8
x
putWord7s (Word7 Word8
x : [Word7]
xs) = Word8 -> Put
B.putWord8 (Word8
x Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
0x80) Put -> Put -> Put
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [Word7] -> Put
putWord7s [Word7]
xs

getWord7s :: Get [Word7]
getWord7s :: Get [Word7]
getWord7s = do
  Word8
next <- Get Word8
B.getWord8
  case Word8
next Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x80 of
    Word8
0x80 -> (:) (Word8 -> Word7
toWord7 Word8
next) ([Word7] -> [Word7]) -> Get [Word7] -> Get [Word7]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get [Word7]
getWord7s
    Word8
_ -> [Word7] -> Get [Word7]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Word8 -> Word7
Word7 Word8
next]

natToWord7s :: Natural -> [Word7]
natToWord7s :: Ix -> [Word7]
natToWord7s = [Word7] -> [Word7]
forall a. [a] -> [a]
reverse ([Word7] -> [Word7]) -> (Ix -> [Word7]) -> Ix -> [Word7]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ix -> [Word7]
forall t. (Integral t, Bits t) => t -> [Word7]
go
  where
    go :: t -> [Word7]
go t
n
      | t
n t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
0x7F = [Word8 -> Word7
Word7 (Word8 -> Word7) -> (t -> Word8) -> t -> Word7
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (t -> Word7) -> t -> Word7
forall a b. (a -> b) -> a -> b
$ t
n]
      | Bool
otherwise = (Word8 -> Word7
toWord7 (Word8 -> Word7) -> (t -> Word8) -> t -> Word7
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral) t
n Word7 -> [Word7] -> [Word7]
forall a. a -> [a] -> [a]
: t -> [Word7]
go (t -> Int -> t
forall a. Bits a => a -> Int -> a
shiftR t
n Int
7)

putVariableLengthNat :: Natural -> Put
putVariableLengthNat :: Ix -> Put
putVariableLengthNat = [Word7] -> Put
putWord7s ([Word7] -> Put) -> (Ix -> [Word7]) -> Ix -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ix -> [Word7]
natToWord7s

word7sToNat :: [Word7] -> Natural
word7sToNat :: [Word7] -> Ix
word7sToNat = (Ix -> Word7 -> Ix) -> Ix -> [Word7] -> Ix
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Ix -> Word7 -> Ix
forall a. (Bits a, Num a) => a -> Word7 -> a
f Ix
0
  where
    f :: a -> Word7 -> a
f a
n (Word7 Word8
r) = a -> Int -> a
forall a. Bits a => a -> Int -> a
shiftL a
n Int
7 a -> a -> a
forall a. Bits a => a -> a -> a
.|. (Word8 -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
r)

getVariableLengthNat :: Get Natural
getVariableLengthNat :: Get Ix
getVariableLengthNat = [Word7] -> Ix
word7sToNat ([Word7] -> Ix) -> Get [Word7] -> Get Ix
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get [Word7]
getWord7s

decoderFromGet :: Text -> Get a -> Decoder s a
decoderFromGet :: Text -> Get a -> Decoder s a
decoderFromGet Text
name Get a
get = do
  ByteString
bytes <- Decoder s ByteString
forall a s. FromCBOR a => Decoder s a
fromCBOR
  case Get a
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
forall a.
Get a
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
B.runGetOrFail Get a
get ByteString
bytes of
    Right (ByteString
_remaining, ByteOffset
_offset, a
value) -> a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
value
    Left (ByteString
_remaining, ByteOffset
_offset, String
message) ->
      DecoderError -> Decoder s a
forall e s a. Buildable e => e -> Decoder s a
cborError (Text -> Text -> DecoderError
DecoderErrorCustom Text
name (Text -> DecoderError) -> Text -> DecoderError
forall a b. (a -> b) -> a -> b
$ String -> Text
forall a. IsString a => String -> a
fromString String
message)

instance Era era => ToCBOR (Addr era) where
  toCBOR :: Addr era -> Encoding
toCBOR = ByteString -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (ByteString -> Encoding)
-> (Addr era -> ByteString) -> Addr era -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Put -> ByteString
B.runPut (Put -> ByteString) -> (Addr era -> Put) -> Addr era -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Addr era -> Put
forall era. Addr era -> Put
putAddr

instance Era era => FromCBOR (Addr era) where
  fromCBOR :: Decoder s (Addr era)
fromCBOR = Text -> Get (Addr era) -> Decoder s (Addr era)
forall a s. Text -> Get a -> Decoder s a
decoderFromGet Text
"Addr" Get (Addr era)
forall era. Era era => Get (Addr era)
getAddr

instance Era era => ToCBOR (RewardAcnt era) where
  toCBOR :: RewardAcnt era -> Encoding
toCBOR = ByteString -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (ByteString -> Encoding)
-> (RewardAcnt era -> ByteString) -> RewardAcnt era -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Put -> ByteString
B.runPut (Put -> ByteString)
-> (RewardAcnt era -> Put) -> RewardAcnt era -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RewardAcnt era -> Put
forall era. RewardAcnt era -> Put
putRewardAcnt

instance Era era => FromCBOR (RewardAcnt era) where
  fromCBOR :: Decoder s (RewardAcnt era)
fromCBOR = Text -> Get (RewardAcnt era) -> Decoder s (RewardAcnt era)
forall a s. Text -> Get a -> Decoder s a
decoderFromGet Text
"RewardAcnt" Get (RewardAcnt era)
forall era. Era era => Get (RewardAcnt era)
getRewardAcnt

newtype BootstrapAddress era = BootstrapAddress
  { BootstrapAddress era -> Address
unBootstrapAddress :: Byron.Address
  }
  deriving (BootstrapAddress era -> BootstrapAddress era -> Bool
(BootstrapAddress era -> BootstrapAddress era -> Bool)
-> (BootstrapAddress era -> BootstrapAddress era -> Bool)
-> Eq (BootstrapAddress era)
forall era. BootstrapAddress era -> BootstrapAddress era -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BootstrapAddress era -> BootstrapAddress era -> Bool
$c/= :: forall era. BootstrapAddress era -> BootstrapAddress era -> Bool
== :: BootstrapAddress era -> BootstrapAddress era -> Bool
$c== :: forall era. BootstrapAddress era -> BootstrapAddress era -> Bool
Eq, (forall x. BootstrapAddress era -> Rep (BootstrapAddress era) x)
-> (forall x. Rep (BootstrapAddress era) x -> BootstrapAddress era)
-> Generic (BootstrapAddress era)
forall x. Rep (BootstrapAddress era) x -> BootstrapAddress era
forall x. BootstrapAddress era -> Rep (BootstrapAddress era) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall era x. Rep (BootstrapAddress era) x -> BootstrapAddress era
forall era x. BootstrapAddress era -> Rep (BootstrapAddress era) x
$cto :: forall era x. Rep (BootstrapAddress era) x -> BootstrapAddress era
$cfrom :: forall era x. BootstrapAddress era -> Rep (BootstrapAddress era) x
Generic)
  deriving newtype (BootstrapAddress era -> ()
(BootstrapAddress era -> ()) -> NFData (BootstrapAddress era)
forall era. BootstrapAddress era -> ()
forall a. (a -> ()) -> NFData a
rnf :: BootstrapAddress era -> ()
$crnf :: forall era. BootstrapAddress era -> ()
NFData, Eq (BootstrapAddress era)
Eq (BootstrapAddress era)
-> (BootstrapAddress era -> BootstrapAddress era -> Ordering)
-> (BootstrapAddress era -> BootstrapAddress era -> Bool)
-> (BootstrapAddress era -> BootstrapAddress era -> Bool)
-> (BootstrapAddress era -> BootstrapAddress era -> Bool)
-> (BootstrapAddress era -> BootstrapAddress era -> Bool)
-> (BootstrapAddress era
    -> BootstrapAddress era -> BootstrapAddress era)
-> (BootstrapAddress era
    -> BootstrapAddress era -> BootstrapAddress era)
-> Ord (BootstrapAddress era)
BootstrapAddress era -> BootstrapAddress era -> Bool
BootstrapAddress era -> BootstrapAddress era -> Ordering
BootstrapAddress era
-> BootstrapAddress era -> BootstrapAddress era
forall era. Eq (BootstrapAddress era)
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall era. BootstrapAddress era -> BootstrapAddress era -> Bool
forall era.
BootstrapAddress era -> BootstrapAddress era -> Ordering
forall era.
BootstrapAddress era
-> BootstrapAddress era -> BootstrapAddress era
min :: BootstrapAddress era
-> BootstrapAddress era -> BootstrapAddress era
$cmin :: forall era.
BootstrapAddress era
-> BootstrapAddress era -> BootstrapAddress era
max :: BootstrapAddress era
-> BootstrapAddress era -> BootstrapAddress era
$cmax :: forall era.
BootstrapAddress era
-> BootstrapAddress era -> BootstrapAddress era
>= :: BootstrapAddress era -> BootstrapAddress era -> Bool
$c>= :: forall era. BootstrapAddress era -> BootstrapAddress era -> Bool
> :: BootstrapAddress era -> BootstrapAddress era -> Bool
$c> :: forall era. BootstrapAddress era -> BootstrapAddress era -> Bool
<= :: BootstrapAddress era -> BootstrapAddress era -> Bool
$c<= :: forall era. BootstrapAddress era -> BootstrapAddress era -> Bool
< :: BootstrapAddress era -> BootstrapAddress era -> Bool
$c< :: forall era. BootstrapAddress era -> BootstrapAddress era -> Bool
compare :: BootstrapAddress era -> BootstrapAddress era -> Ordering
$ccompare :: forall era.
BootstrapAddress era -> BootstrapAddress era -> Ordering
$cp1Ord :: forall era. Eq (BootstrapAddress era)
Ord)
  deriving (Int -> BootstrapAddress era -> ShowS
[BootstrapAddress era] -> ShowS
BootstrapAddress era -> String
(Int -> BootstrapAddress era -> ShowS)
-> (BootstrapAddress era -> String)
-> ([BootstrapAddress era] -> ShowS)
-> Show (BootstrapAddress era)
forall era. Int -> BootstrapAddress era -> ShowS
forall era. [BootstrapAddress era] -> ShowS
forall era. BootstrapAddress era -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BootstrapAddress era] -> ShowS
$cshowList :: forall era. [BootstrapAddress era] -> ShowS
show :: BootstrapAddress era -> String
$cshow :: forall era. BootstrapAddress era -> String
showsPrec :: Int -> BootstrapAddress era -> ShowS
$cshowsPrec :: forall era. Int -> BootstrapAddress era -> ShowS
Show) via Quiet (BootstrapAddress era)

instance NoThunks (BootstrapAddress era)

bootstrapKeyHash ::
  forall era.
  -- TODO: enforce this constraint
  --(HASH era ~ Hash.Blake2b_224) =>
  Era era =>
  BootstrapAddress era ->
  KeyHash 'Payment (Crypto era)
bootstrapKeyHash :: BootstrapAddress era -> KeyHash 'Payment (Crypto era)
bootstrapKeyHash (BootstrapAddress Address
byronAddress) =
  let root :: AddressHash Address'
root = Address -> AddressHash Address'
Byron.addrRoot Address
byronAddress
      bytes :: ByteString
bytes = AddressHash Address' -> ByteString
forall algo a. AbstractHash algo a -> ByteString
Byron.abstractHashToBytes AddressHash Address'
root
      hash :: Hash (ADDRHASH (Crypto era)) (VerKeyDSIGN (DSIGN (Crypto era)))
hash =
        Hash (ADDRHASH (Crypto era)) (VerKeyDSIGN (DSIGN (Crypto era)))
-> Maybe
     (Hash (ADDRHASH (Crypto era)) (VerKeyDSIGN (DSIGN (Crypto era))))
-> Hash (ADDRHASH (Crypto era)) (VerKeyDSIGN (DSIGN (Crypto era)))
forall a. a -> Maybe a -> a
fromMaybe (Text
-> Hash (ADDRHASH (Crypto era)) (VerKeyDSIGN (DSIGN (Crypto era)))
forall a. HasCallStack => Text -> a
panic Text
"bootstrapKeyHash: incorrect hash length") (Maybe
   (Hash (ADDRHASH (Crypto era)) (VerKeyDSIGN (DSIGN (Crypto era))))
 -> Hash (ADDRHASH (Crypto era)) (VerKeyDSIGN (DSIGN (Crypto era))))
-> Maybe
     (Hash (ADDRHASH (Crypto era)) (VerKeyDSIGN (DSIGN (Crypto era))))
-> Hash (ADDRHASH (Crypto era)) (VerKeyDSIGN (DSIGN (Crypto era)))
forall a b. (a -> b) -> a -> b
$
          ByteString
-> Maybe
     (Hash (ADDRHASH (Crypto era)) (VerKeyDSIGN (DSIGN (Crypto era))))
forall h a. HashAlgorithm h => ByteString -> Maybe (Hash h a)
Hash.hashFromBytes ByteString
bytes
   in Hash (ADDRHASH (Crypto era)) (VerKeyDSIGN (DSIGN (Crypto era)))
-> KeyHash 'Payment (Crypto era)
forall (discriminator :: KeyRole) crypto.
Hash (ADDRHASH crypto) (VerKeyDSIGN (DSIGN crypto))
-> KeyHash discriminator crypto
KeyHash Hash (ADDRHASH (Crypto era)) (VerKeyDSIGN (DSIGN (Crypto era)))
hash