-
Notifications
You must be signed in to change notification settings - Fork 211
/
Integrity.hs
65 lines (55 loc) · 1.82 KB
/
Integrity.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
-- |
-- Copyright: © 2020-2022 IOHK
-- License: Apache-2.0
--
module Cardano.Wallet.Read.Primitive.Tx.Features.Integrity
( integrity
, txIntegrity
)
where
import Prelude hiding
( (.) )
import Cardano.Ledger.Alonzo.Tx
( ScriptIntegrityHash )
import Cardano.Ledger.Crypto
( StandardCrypto )
import Cardano.Ledger.SafeHash
( SafeToHash (originalBytes) )
import Cardano.Wallet.Read.Eras
( EraFun (..), EraValue, K (..), applyEraFun, extractEraValue )
import Cardano.Wallet.Read.Tx
( Tx )
import Cardano.Wallet.Read.Tx.Integrity
( Integrity (..), getEraIntegrity )
import Control.Category
( (.) )
import Data.Maybe.Strict
( StrictMaybe, strictMaybeToMaybe )
import qualified Cardano.Wallet.Primitive.Types.Hash as W
integrity :: EraFun Integrity (K (Maybe (W.Hash "ScriptIntegrity")))
integrity = EraFun
{ byronFun = noIntegrity
, shelleyFun = noIntegrity
, allegraFun = noIntegrity
, maryFun = noIntegrity
, alonzoFun = yesIntegrity
, babbageFun = yesIntegrity
, conwayFun = yesIntegrity
}
where
noIntegrity = const $ K Nothing
yesIntegrity (Integrity es) = K $ getIntegrity es
getIntegrity
:: StrictMaybe (ScriptIntegrityHash StandardCrypto)
-> Maybe (W.Hash "ScriptIntegrity")
getIntegrity = strictMaybeToMaybe . fmap (W.Hash . originalBytes)
-- Era functions extract from Tx to primitive W.Hash.
-- Useful to cache this composition here, to be exported in case of reuse.
txIntegrityEraFun :: EraFun Tx (K (Maybe (W.Hash "ScriptIntegrity")))
txIntegrityEraFun = integrity . getEraIntegrity
-- | Extract from Tx in any era to primitive W.Hash.
txIntegrity :: EraValue Tx -> Maybe (W.Hash "ScriptIntegrity")
txIntegrity = extractEraValue . applyEraFun txIntegrityEraFun