{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE DisambiguateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}

-- | Transaction bodies
module Cardano.Api.TxIn
  ( -- * Transaction inputs
    TxIn (..)
  , TxIx (..)

    -- * Transaction Ids
  , TxId (..)
  , parseTxId

    -- * Data family instances
  , AsType (AsTxId)

    -- * Internal conversion functions
  , toByronTxId
  , toShelleyTxId
  , fromShelleyTxId
  , toByronTxIn
  , fromByronTxIn
  , toShelleyTxIn
  , fromShelleyTxIn
  , renderTxIn
  )
where

import           Cardano.Api.Error
import           Cardano.Api.HasTypeProxy
import           Cardano.Api.Pretty
import           Cardano.Api.SerialiseJSON
import           Cardano.Api.SerialiseRaw
import           Cardano.Api.SerialiseUsing
import           Cardano.Api.Utils

import qualified Cardano.Chain.UTxO as Byron
import qualified Cardano.Crypto.Hash.Class as Crypto
import qualified Cardano.Crypto.Hashing as Byron
import qualified Cardano.Ledger.BaseTypes as Ledger
import           Cardano.Ledger.Crypto (StandardCrypto)
import qualified Cardano.Ledger.Keys as Shelley
import qualified Cardano.Ledger.SafeHash as SafeHash
import qualified Cardano.Ledger.Shelley.TxBody as Shelley
import qualified Cardano.Ledger.TxIn as Ledger

import           Control.Applicative (some)
import           Data.Aeson (withText)
import qualified Data.Aeson as Aeson
import           Data.Aeson.Types (ToJSONKey (..), toJSONKeyText)
import qualified Data.ByteString.Char8 as BSC
import           Data.String
import           Data.Text (Text)
import qualified Data.Text as Text
import qualified Text.Parsec as Parsec
import           Text.Parsec ((<?>))
import qualified Text.Parsec.Language as Parsec
import qualified Text.Parsec.String as Parsec
import qualified Text.Parsec.Token as Parsec

{- HLINT ignore "Redundant flip" -}
{- HLINT ignore "Use section" -}

-- ----------------------------------------------------------------------------
-- Transaction Ids
--

newtype TxId = TxId (Shelley.Hash StandardCrypto Shelley.EraIndependentTxBody)
  -- We use the Shelley representation and convert to/from the Byron one
  deriving stock (TxId -> TxId -> Bool
(TxId -> TxId -> Bool) -> (TxId -> TxId -> Bool) -> Eq TxId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TxId -> TxId -> Bool
== :: TxId -> TxId -> Bool
$c/= :: TxId -> TxId -> Bool
/= :: TxId -> TxId -> Bool
Eq, Eq TxId
Eq TxId =>
(TxId -> TxId -> Ordering)
-> (TxId -> TxId -> Bool)
-> (TxId -> TxId -> Bool)
-> (TxId -> TxId -> Bool)
-> (TxId -> TxId -> Bool)
-> (TxId -> TxId -> TxId)
-> (TxId -> TxId -> TxId)
-> Ord TxId
TxId -> TxId -> Bool
TxId -> TxId -> Ordering
TxId -> TxId -> TxId
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
$ccompare :: TxId -> TxId -> Ordering
compare :: TxId -> TxId -> Ordering
$c< :: TxId -> TxId -> Bool
< :: TxId -> TxId -> Bool
$c<= :: TxId -> TxId -> Bool
<= :: TxId -> TxId -> Bool
$c> :: TxId -> TxId -> Bool
> :: TxId -> TxId -> Bool
$c>= :: TxId -> TxId -> Bool
>= :: TxId -> TxId -> Bool
$cmax :: TxId -> TxId -> TxId
max :: TxId -> TxId -> TxId
$cmin :: TxId -> TxId -> TxId
min :: TxId -> TxId -> TxId
Ord)
  deriving (Int -> TxId -> ShowS
[TxId] -> ShowS
TxId -> [Char]
(Int -> TxId -> ShowS)
-> (TxId -> [Char]) -> ([TxId] -> ShowS) -> Show TxId
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TxId -> ShowS
showsPrec :: Int -> TxId -> ShowS
$cshow :: TxId -> [Char]
show :: TxId -> [Char]
$cshowList :: [TxId] -> ShowS
showList :: [TxId] -> ShowS
Show, [Char] -> TxId
([Char] -> TxId) -> IsString TxId
forall a. ([Char] -> a) -> IsString a
$cfromString :: [Char] -> TxId
fromString :: [Char] -> TxId
IsString) via UsingRawBytesHex TxId
  deriving ([TxId] -> Value
[TxId] -> Encoding
TxId -> Bool
TxId -> Value
TxId -> Encoding
(TxId -> Value)
-> (TxId -> Encoding)
-> ([TxId] -> Value)
-> ([TxId] -> Encoding)
-> (TxId -> Bool)
-> ToJSON TxId
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: TxId -> Value
toJSON :: TxId -> Value
$ctoEncoding :: TxId -> Encoding
toEncoding :: TxId -> Encoding
$ctoJSONList :: [TxId] -> Value
toJSONList :: [TxId] -> Value
$ctoEncodingList :: [TxId] -> Encoding
toEncodingList :: [TxId] -> Encoding
$comitField :: TxId -> Bool
omitField :: TxId -> Bool
ToJSON, Maybe TxId
Value -> Parser [TxId]
Value -> Parser TxId
(Value -> Parser TxId)
-> (Value -> Parser [TxId]) -> Maybe TxId -> FromJSON TxId
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
$cparseJSON :: Value -> Parser TxId
parseJSON :: Value -> Parser TxId
$cparseJSONList :: Value -> Parser [TxId]
parseJSONList :: Value -> Parser [TxId]
$comittedField :: Maybe TxId
omittedField :: Maybe TxId
FromJSON) via UsingRawBytesHex TxId
  deriving (ToJSONKeyFunction [TxId]
ToJSONKeyFunction TxId
ToJSONKeyFunction TxId
-> ToJSONKeyFunction [TxId] -> ToJSONKey TxId
forall a.
ToJSONKeyFunction a -> ToJSONKeyFunction [a] -> ToJSONKey a
$ctoJSONKey :: ToJSONKeyFunction TxId
toJSONKey :: ToJSONKeyFunction TxId
$ctoJSONKeyList :: ToJSONKeyFunction [TxId]
toJSONKeyList :: ToJSONKeyFunction [TxId]
ToJSONKey, FromJSONKeyFunction [TxId]
FromJSONKeyFunction TxId
FromJSONKeyFunction TxId
-> FromJSONKeyFunction [TxId] -> FromJSONKey TxId
forall a.
FromJSONKeyFunction a -> FromJSONKeyFunction [a] -> FromJSONKey a
$cfromJSONKey :: FromJSONKeyFunction TxId
fromJSONKey :: FromJSONKeyFunction TxId
$cfromJSONKeyList :: FromJSONKeyFunction [TxId]
fromJSONKeyList :: FromJSONKeyFunction [TxId]
FromJSONKey) via UsingRawBytesHex TxId

instance HasTypeProxy TxId where
  data AsType TxId = AsTxId
  proxyToAsType :: Proxy TxId -> AsType TxId
proxyToAsType Proxy TxId
_ = AsType TxId
AsTxId

instance SerialiseAsRawBytes TxId where
  serialiseToRawBytes :: TxId -> ByteString
serialiseToRawBytes (TxId Hash StandardCrypto EraIndependentTxBody
h) = Hash Blake2b_256 EraIndependentTxBody -> ByteString
forall h a. Hash h a -> ByteString
Crypto.hashToBytes Hash Blake2b_256 EraIndependentTxBody
Hash StandardCrypto EraIndependentTxBody
h
  deserialiseFromRawBytes :: AsType TxId -> ByteString -> Either SerialiseAsRawBytesError TxId
deserialiseFromRawBytes AsType TxId
R:AsTypeTxId
AsTxId ByteString
bs = case ByteString -> Maybe (Hash Blake2b_256 EraIndependentTxBody)
forall h a. HashAlgorithm h => ByteString -> Maybe (Hash h a)
Crypto.hashFromBytes ByteString
bs of
    Just Hash Blake2b_256 EraIndependentTxBody
a -> TxId -> Either SerialiseAsRawBytesError TxId
forall a b. b -> Either a b
Right (Hash StandardCrypto EraIndependentTxBody -> TxId
TxId Hash Blake2b_256 EraIndependentTxBody
Hash StandardCrypto EraIndependentTxBody
a)
    Maybe (Hash Blake2b_256 EraIndependentTxBody)
Nothing -> SerialiseAsRawBytesError -> Either SerialiseAsRawBytesError TxId
forall a b. a -> Either a b
Left (SerialiseAsRawBytesError -> Either SerialiseAsRawBytesError TxId)
-> SerialiseAsRawBytesError -> Either SerialiseAsRawBytesError TxId
forall a b. (a -> b) -> a -> b
$ [Char] -> SerialiseAsRawBytesError
SerialiseAsRawBytesError [Char]
"Unable to deserialise TxId"

toByronTxId :: TxId -> Byron.TxId
toByronTxId :: TxId -> TxId
toByronTxId (TxId Hash StandardCrypto EraIndependentTxBody
h) =
  ByteString -> TxId
forall a. ByteString -> Hash a
Byron.unsafeHashFromBytes (Hash Blake2b_256 EraIndependentTxBody -> ByteString
forall h a. Hash h a -> ByteString
Crypto.hashToBytes Hash Blake2b_256 EraIndependentTxBody
Hash StandardCrypto EraIndependentTxBody
h)

toShelleyTxId :: TxId -> Ledger.TxId StandardCrypto
toShelleyTxId :: TxId -> TxId StandardCrypto
toShelleyTxId (TxId Hash StandardCrypto EraIndependentTxBody
h) =
  SafeHash StandardCrypto EraIndependentTxBody -> TxId StandardCrypto
forall c. SafeHash c EraIndependentTxBody -> TxId c
Ledger.TxId (Hash StandardCrypto EraIndependentTxBody
-> SafeHash StandardCrypto EraIndependentTxBody
forall c index. Hash (HASH c) index -> SafeHash c index
SafeHash.unsafeMakeSafeHash (Hash Blake2b_256 EraIndependentTxBody
-> Hash Blake2b_256 EraIndependentTxBody
forall h a b. Hash h a -> Hash h b
Crypto.castHash Hash Blake2b_256 EraIndependentTxBody
Hash StandardCrypto EraIndependentTxBody
h))

fromShelleyTxId :: Ledger.TxId StandardCrypto -> TxId
fromShelleyTxId :: TxId StandardCrypto -> TxId
fromShelleyTxId (Ledger.TxId SafeHash StandardCrypto EraIndependentTxBody
h) =
  Hash StandardCrypto EraIndependentTxBody -> TxId
TxId (Hash Blake2b_256 EraIndependentTxBody
-> Hash Blake2b_256 EraIndependentTxBody
forall h a b. Hash h a -> Hash h b
Crypto.castHash (SafeHash StandardCrypto EraIndependentTxBody
-> Hash StandardCrypto EraIndependentTxBody
forall c i. SafeHash c i -> Hash (HASH c) i
SafeHash.extractHash SafeHash StandardCrypto EraIndependentTxBody
h))

-- ----------------------------------------------------------------------------
-- Transaction inputs
--

data TxIn = TxIn TxId TxIx
  deriving (TxIn -> TxIn -> Bool
(TxIn -> TxIn -> Bool) -> (TxIn -> TxIn -> Bool) -> Eq TxIn
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TxIn -> TxIn -> Bool
== :: TxIn -> TxIn -> Bool
$c/= :: TxIn -> TxIn -> Bool
/= :: TxIn -> TxIn -> Bool
Eq, Eq TxIn
Eq TxIn =>
(TxIn -> TxIn -> Ordering)
-> (TxIn -> TxIn -> Bool)
-> (TxIn -> TxIn -> Bool)
-> (TxIn -> TxIn -> Bool)
-> (TxIn -> TxIn -> Bool)
-> (TxIn -> TxIn -> TxIn)
-> (TxIn -> TxIn -> TxIn)
-> Ord TxIn
TxIn -> TxIn -> Bool
TxIn -> TxIn -> Ordering
TxIn -> TxIn -> TxIn
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
$ccompare :: TxIn -> TxIn -> Ordering
compare :: TxIn -> TxIn -> Ordering
$c< :: TxIn -> TxIn -> Bool
< :: TxIn -> TxIn -> Bool
$c<= :: TxIn -> TxIn -> Bool
<= :: TxIn -> TxIn -> Bool
$c> :: TxIn -> TxIn -> Bool
> :: TxIn -> TxIn -> Bool
$c>= :: TxIn -> TxIn -> Bool
>= :: TxIn -> TxIn -> Bool
$cmax :: TxIn -> TxIn -> TxIn
max :: TxIn -> TxIn -> TxIn
$cmin :: TxIn -> TxIn -> TxIn
min :: TxIn -> TxIn -> TxIn
Ord, Int -> TxIn -> ShowS
[TxIn] -> ShowS
TxIn -> [Char]
(Int -> TxIn -> ShowS)
-> (TxIn -> [Char]) -> ([TxIn] -> ShowS) -> Show TxIn
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TxIn -> ShowS
showsPrec :: Int -> TxIn -> ShowS
$cshow :: TxIn -> [Char]
show :: TxIn -> [Char]
$cshowList :: [TxIn] -> ShowS
showList :: [TxIn] -> ShowS
Show)

instance ToJSON TxIn where
  toJSON :: TxIn -> Value
toJSON TxIn
txIn = Text -> Value
Aeson.String (Text -> Value) -> Text -> Value
forall a b. (a -> b) -> a -> b
$ TxIn -> Text
renderTxIn TxIn
txIn

instance ToJSONKey TxIn where
  toJSONKey :: ToJSONKeyFunction TxIn
toJSONKey = (TxIn -> Text) -> ToJSONKeyFunction TxIn
forall a. (a -> Text) -> ToJSONKeyFunction a
toJSONKeyText TxIn -> Text
renderTxIn

instance FromJSON TxIn where
  parseJSON :: Value -> Parser TxIn
parseJSON = [Char] -> (Text -> Parser TxIn) -> Value -> Parser TxIn
forall a. [Char] -> (Text -> Parser a) -> Value -> Parser a
withText [Char]
"TxIn" ((Text -> Parser TxIn) -> Value -> Parser TxIn)
-> (Text -> Parser TxIn) -> Value -> Parser TxIn
forall a b. (a -> b) -> a -> b
$ Parser TxIn -> Text -> Parser TxIn
forall a. Parser a -> Text -> Parser a
runParsecParser Parser TxIn
parseTxIn

instance FromJSONKey TxIn where
  fromJSONKey :: FromJSONKeyFunction TxIn
fromJSONKey = (Text -> Parser TxIn) -> FromJSONKeyFunction TxIn
forall a. (Text -> Parser a) -> FromJSONKeyFunction a
Aeson.FromJSONKeyTextParser ((Text -> Parser TxIn) -> FromJSONKeyFunction TxIn)
-> (Text -> Parser TxIn) -> FromJSONKeyFunction TxIn
forall a b. (a -> b) -> a -> b
$ Parser TxIn -> Text -> Parser TxIn
forall a. Parser a -> Text -> Parser a
runParsecParser Parser TxIn
parseTxIn

deriving via (ShowOf TxIn) instance Pretty TxIn

parseTxId :: Parsec.Parser TxId
parseTxId :: Parser TxId
parseTxId = do
  [Char]
str <- ParsecT [Char] () Identity Char
-> ParsecT [Char] () Identity [Char]
forall a.
ParsecT [Char] () Identity a -> ParsecT [Char] () Identity [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
some ParsecT [Char] () Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
Parsec.hexDigit ParsecT [Char] () Identity [Char]
-> [Char] -> ParsecT [Char] () Identity [Char]
forall s u (m :: * -> *) a.
ParsecT s u m a -> [Char] -> ParsecT s u m a
<?> [Char]
"transaction id (hexadecimal)"
  (RawBytesHexError -> [Char])
-> Either RawBytesHexError TxId -> Parser TxId
forall (m :: * -> *) e a.
MonadFail m =>
(e -> [Char]) -> Either e a -> m a
failEitherWith
    (\RawBytesHexError
e -> Doc AnsiStyle -> [Char]
docToString (Doc AnsiStyle -> [Char]) -> Doc AnsiStyle -> [Char]
forall a b. (a -> b) -> a -> b
$ Doc AnsiStyle
"Incorrect transaction id format: " Doc AnsiStyle -> Doc AnsiStyle -> Doc AnsiStyle
forall a. Semigroup a => a -> a -> a
<> RawBytesHexError -> Doc AnsiStyle
forall e ann. Error e => e -> Doc ann
forall ann. RawBytesHexError -> Doc ann
prettyError RawBytesHexError
e)
    (AsType TxId -> ByteString -> Either RawBytesHexError TxId
forall a.
SerialiseAsRawBytes a =>
AsType a -> ByteString -> Either RawBytesHexError a
deserialiseFromRawBytesHex AsType TxId
AsTxId (ByteString -> Either RawBytesHexError TxId)
-> ByteString -> Either RawBytesHexError TxId
forall a b. (a -> b) -> a -> b
$ [Char] -> ByteString
BSC.pack [Char]
str)

parseTxIn :: Parsec.Parser TxIn
parseTxIn :: Parser TxIn
parseTxIn = TxId -> TxIx -> TxIn
TxIn (TxId -> TxIx -> TxIn)
-> Parser TxId -> ParsecT [Char] () Identity (TxIx -> TxIn)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser TxId
parseTxId ParsecT [Char] () Identity (TxIx -> TxIn)
-> ParsecT [Char] () Identity TxIx -> Parser TxIn
forall a b.
ParsecT [Char] () Identity (a -> b)
-> ParsecT [Char] () Identity a -> ParsecT [Char] () Identity b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Char -> ParsecT [Char] () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
Parsec.char Char
'#' ParsecT [Char] () Identity Char
-> ParsecT [Char] () Identity TxIx
-> ParsecT [Char] () Identity TxIx
forall a b.
ParsecT [Char] () Identity a
-> ParsecT [Char] () Identity b -> ParsecT [Char] () Identity b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT [Char] () Identity TxIx
parseTxIx)

parseTxIx :: Parsec.Parser TxIx
parseTxIx :: ParsecT [Char] () Identity TxIx
parseTxIx = Word -> TxIx
TxIx (Word -> TxIx) -> (Integer -> Word) -> Integer -> TxIx
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer -> TxIx)
-> ParsecT [Char] () Identity Integer
-> ParsecT [Char] () Identity TxIx
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Char] () Identity Integer
decimal

decimal :: Parsec.Parser Integer
Parsec.TokenParser{decimal :: forall s u (m :: * -> *).
GenTokenParser s u m -> ParsecT s u m Integer
Parsec.decimal = ParsecT [Char] () Identity Integer
decimal} = GenTokenParser [Char] () Identity
forall st. TokenParser st
Parsec.haskell

renderTxIn :: TxIn -> Text
renderTxIn :: TxIn -> Text
renderTxIn (TxIn TxId
txId (TxIx Word
ix)) =
  TxId -> Text
forall a. SerialiseAsRawBytes a => a -> Text
serialiseToRawBytesHexText TxId
txId Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"#" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
Text.pack (Word -> [Char]
forall a. Show a => a -> [Char]
show Word
ix)

newtype TxIx = TxIx Word
  deriving stock (TxIx -> TxIx -> Bool
(TxIx -> TxIx -> Bool) -> (TxIx -> TxIx -> Bool) -> Eq TxIx
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TxIx -> TxIx -> Bool
== :: TxIx -> TxIx -> Bool
$c/= :: TxIx -> TxIx -> Bool
/= :: TxIx -> TxIx -> Bool
Eq, Eq TxIx
Eq TxIx =>
(TxIx -> TxIx -> Ordering)
-> (TxIx -> TxIx -> Bool)
-> (TxIx -> TxIx -> Bool)
-> (TxIx -> TxIx -> Bool)
-> (TxIx -> TxIx -> Bool)
-> (TxIx -> TxIx -> TxIx)
-> (TxIx -> TxIx -> TxIx)
-> Ord TxIx
TxIx -> TxIx -> Bool
TxIx -> TxIx -> Ordering
TxIx -> TxIx -> TxIx
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
$ccompare :: TxIx -> TxIx -> Ordering
compare :: TxIx -> TxIx -> Ordering
$c< :: TxIx -> TxIx -> Bool
< :: TxIx -> TxIx -> Bool
$c<= :: TxIx -> TxIx -> Bool
<= :: TxIx -> TxIx -> Bool
$c> :: TxIx -> TxIx -> Bool
> :: TxIx -> TxIx -> Bool
$c>= :: TxIx -> TxIx -> Bool
>= :: TxIx -> TxIx -> Bool
$cmax :: TxIx -> TxIx -> TxIx
max :: TxIx -> TxIx -> TxIx
$cmin :: TxIx -> TxIx -> TxIx
min :: TxIx -> TxIx -> TxIx
Ord, Int -> TxIx -> ShowS
[TxIx] -> ShowS
TxIx -> [Char]
(Int -> TxIx -> ShowS)
-> (TxIx -> [Char]) -> ([TxIx] -> ShowS) -> Show TxIx
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TxIx -> ShowS
showsPrec :: Int -> TxIx -> ShowS
$cshow :: TxIx -> [Char]
show :: TxIx -> [Char]
$cshowList :: [TxIx] -> ShowS
showList :: [TxIx] -> ShowS
Show)
  deriving newtype Int -> TxIx
TxIx -> Int
TxIx -> [TxIx]
TxIx -> TxIx
TxIx -> TxIx -> [TxIx]
TxIx -> TxIx -> TxIx -> [TxIx]
(TxIx -> TxIx)
-> (TxIx -> TxIx)
-> (Int -> TxIx)
-> (TxIx -> Int)
-> (TxIx -> [TxIx])
-> (TxIx -> TxIx -> [TxIx])
-> (TxIx -> TxIx -> [TxIx])
-> (TxIx -> TxIx -> TxIx -> [TxIx])
-> Enum TxIx
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: TxIx -> TxIx
succ :: TxIx -> TxIx
$cpred :: TxIx -> TxIx
pred :: TxIx -> TxIx
$ctoEnum :: Int -> TxIx
toEnum :: Int -> TxIx
$cfromEnum :: TxIx -> Int
fromEnum :: TxIx -> Int
$cenumFrom :: TxIx -> [TxIx]
enumFrom :: TxIx -> [TxIx]
$cenumFromThen :: TxIx -> TxIx -> [TxIx]
enumFromThen :: TxIx -> TxIx -> [TxIx]
$cenumFromTo :: TxIx -> TxIx -> [TxIx]
enumFromTo :: TxIx -> TxIx -> [TxIx]
$cenumFromThenTo :: TxIx -> TxIx -> TxIx -> [TxIx]
enumFromThenTo :: TxIx -> TxIx -> TxIx -> [TxIx]
Enum
  deriving newtype ([TxIx] -> Value
[TxIx] -> Encoding
TxIx -> Bool
TxIx -> Value
TxIx -> Encoding
(TxIx -> Value)
-> (TxIx -> Encoding)
-> ([TxIx] -> Value)
-> ([TxIx] -> Encoding)
-> (TxIx -> Bool)
-> ToJSON TxIx
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: TxIx -> Value
toJSON :: TxIx -> Value
$ctoEncoding :: TxIx -> Encoding
toEncoding :: TxIx -> Encoding
$ctoJSONList :: [TxIx] -> Value
toJSONList :: [TxIx] -> Value
$ctoEncodingList :: [TxIx] -> Encoding
toEncodingList :: [TxIx] -> Encoding
$comitField :: TxIx -> Bool
omitField :: TxIx -> Bool
ToJSON, Maybe TxIx
Value -> Parser [TxIx]
Value -> Parser TxIx
(Value -> Parser TxIx)
-> (Value -> Parser [TxIx]) -> Maybe TxIx -> FromJSON TxIx
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
$cparseJSON :: Value -> Parser TxIx
parseJSON :: Value -> Parser TxIx
$cparseJSONList :: Value -> Parser [TxIx]
parseJSONList :: Value -> Parser [TxIx]
$comittedField :: Maybe TxIx
omittedField :: Maybe TxIx
FromJSON)

fromByronTxIn :: Byron.TxIn -> TxIn
fromByronTxIn :: TxIn -> TxIn
fromByronTxIn (Byron.TxInUtxo TxId
txId Word16
index) =
  let shortBs :: ShortByteString
shortBs = TxId -> ShortByteString
forall algo a. AbstractHash algo a -> ShortByteString
Byron.abstractHashToShort TxId
txId
      mApiHash :: Maybe (Hash Blake2b_256 EraIndependentTxBody)
mApiHash = ShortByteString -> Maybe (Hash Blake2b_256 EraIndependentTxBody)
forall h a. HashAlgorithm h => ShortByteString -> Maybe (Hash h a)
Crypto.hashFromBytesShort ShortByteString
shortBs
   in case Maybe (Hash Blake2b_256 EraIndependentTxBody)
mApiHash of
        Just Hash Blake2b_256 EraIndependentTxBody
apiHash -> TxId -> TxIx -> TxIn
TxIn (Hash StandardCrypto EraIndependentTxBody -> TxId
TxId Hash Blake2b_256 EraIndependentTxBody
Hash StandardCrypto EraIndependentTxBody
apiHash) (Word -> TxIx
TxIx (Word -> TxIx) -> (Integer -> Word) -> Integer -> TxIx
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer -> TxIx) -> Integer -> TxIx
forall a b. (a -> b) -> a -> b
$ Word16 -> Integer
forall a. Integral a => a -> Integer
toInteger Word16
index)
        Maybe (Hash Blake2b_256 EraIndependentTxBody)
Nothing -> [Char] -> TxIn
forall a. HasCallStack => [Char] -> a
error ([Char] -> TxIn) -> [Char] -> TxIn
forall a b. (a -> b) -> a -> b
$ [Char]
"Error converting Byron era TxId: " [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> TxId -> [Char]
forall a. Show a => a -> [Char]
show TxId
txId

toByronTxIn :: TxIn -> Byron.TxIn
toByronTxIn :: TxIn -> TxIn
toByronTxIn (TxIn TxId
txid (TxIx Word
txix)) =
  TxId -> Word16 -> TxIn
Byron.TxInUtxo (TxId -> TxId
toByronTxId TxId
txid) (Word -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
txix)

-- | This function may overflow on the transaction index. Call sites must ensure
-- that all uses of this function are appropriately guarded.
toShelleyTxIn :: TxIn -> Ledger.TxIn StandardCrypto
toShelleyTxIn :: TxIn -> TxIn StandardCrypto
toShelleyTxIn (TxIn TxId
txid (TxIx Word
txix)) =
  TxId StandardCrypto -> TxIx -> TxIn StandardCrypto
forall c. TxId c -> TxIx -> TxIn c
Ledger.TxIn (TxId -> TxId StandardCrypto
toShelleyTxId TxId
txid) (Word64 -> TxIx
Ledger.TxIx (Word64 -> TxIx) -> Word64 -> TxIx
forall a b. (a -> b) -> a -> b
$ Word -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
txix)

fromShelleyTxIn :: Ledger.TxIn StandardCrypto -> TxIn
fromShelleyTxIn :: TxIn StandardCrypto -> TxIn
fromShelleyTxIn (Ledger.TxIn TxId StandardCrypto
txid (Ledger.TxIx Word64
txix)) =
  TxId -> TxIx -> TxIn
TxIn (TxId StandardCrypto -> TxId
fromShelleyTxId TxId StandardCrypto
txid) (Word -> TxIx
TxIx (Word64 -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
txix))