Skip to content

Commit

Permalink
[ADP-3215] Remove fromCardanoApiTx from read (#4721)
Browse files Browse the repository at this point in the history
This pull request removes the function `fromCardanoApiTx` from the
`read` package, which in turn allows us to remove the dependency on
`cardano-api`.

The function `fromCardanoApiTx` is mainly used in the context of
`SealedTx`, this pull request moves the function there.

### Issue Number

ADP-3215
  • Loading branch information
HeinrichApfelmus authored Aug 8, 2024
2 parents 17cc50f + a75e45b commit 5970083
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 69 deletions.
3 changes: 3 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ package cardano-wallet-read
package cardano-wallet-text-class
tests: True

package cardano-wallet-unit
tests: True

package cardano-numeric
tests: True

Expand Down
32 changes: 21 additions & 11 deletions lib/primitive/lib/Cardano/Wallet/Primitive/Ledger/Read/Tx/Sealed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,42 @@

module Cardano.Wallet.Primitive.Ledger.Read.Tx.Sealed
( fromSealedTx
, anythingFromSealedTx
) where

import Prelude

import Cardano.Api
( InAnyCardanoEra (..)
)
import Cardano.Read.Ledger.Tx.Cardano
( fromCardanoApiTx
)
import Cardano.Wallet.Primitive.Types.Tx.SealedTx
( SealedTx (unsafeCardanoTx)
)
import Cardano.Wallet.Read
( EraValue
, Tx
, applyEraFun
( EraValue (..)
, Tx (..)
)
import Cardano.Wallet.Read.Eras
( Allegra
, Alonzo
, Babbage
, Conway
, Mary
, Shelley
)

import qualified Cardano.Api as Cardano
import qualified Cardano.Api.Shelley as Cardano
import qualified Cardano.Wallet.Primitive.Types.Tx.SealedTx as W

fromSealedTx:: W.SealedTx -> EraValue Tx
fromSealedTx sealed =
case unsafeCardanoTx sealed of
InAnyCardanoEra _ce tx -> fromCardanoApiTx tx

anythingFromSealedTx :: (forall era . Tx era -> a) -> SealedTx -> a
anythingFromSealedTx f = applyEraFun f . fromSealedTx
fromCardanoApiTx :: Cardano.Tx era -> EraValue Tx
fromCardanoApiTx tx0 = case tx0 of
Cardano.ShelleyTx era tx -> case era of
Cardano.ShelleyBasedEraShelley -> EraValue (Tx tx :: Tx Shelley)
Cardano.ShelleyBasedEraAllegra -> EraValue (Tx tx :: Tx Allegra)
Cardano.ShelleyBasedEraMary -> EraValue (Tx tx :: Tx Mary)
Cardano.ShelleyBasedEraAlonzo -> EraValue (Tx tx :: Tx Alonzo)
Cardano.ShelleyBasedEraBabbage -> EraValue (Tx tx :: Tx Babbage)
Cardano.ShelleyBasedEraConway -> EraValue (Tx tx :: Tx Conway)
2 changes: 0 additions & 2 deletions lib/read/cardano-wallet-read.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ library
Cardano.Read.Ledger.Tx.Inputs
Cardano.Read.Ledger.Tx.Outputs
Cardano.Read.Ledger.Tx.TxId
Cardano.Read.Ledger.Tx.Cardano
Cardano.Read.Ledger.Tx.Certificates
Cardano.Read.Ledger.Tx.CBOR
Cardano.Read.Ledger.Tx.CollateralInputs
Expand Down Expand Up @@ -106,7 +105,6 @@ library
build-depends:
, base
, bytestring
, cardano-api
, cardano-crypto
, cardano-crypto-class
, cardano-crypto-praos
Expand Down
44 changes: 0 additions & 44 deletions lib/read/lib/Cardano/Read/Ledger/Tx/Cardano.hs

This file was deleted.

20 changes: 8 additions & 12 deletions lib/unit/test/unit/Cardano/Wallet/Shelley/TransactionSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ import Cardano.Api.Gen
import Cardano.Mnemonic
( SomeMnemonic (SomeMnemonic)
)
import Cardano.Read.Ledger.Tx.Cardano
( fromCardanoApiTx
)
import Cardano.Wallet
( Fee (..)
, Percentile (..)
Expand Down Expand Up @@ -93,6 +90,9 @@ import Cardano.Wallet.Primitive.Ledger.Convert
import Cardano.Wallet.Primitive.Ledger.Read.Tx.Features.Integrity
( txIntegrity
)
import Cardano.Wallet.Primitive.Ledger.Read.Tx.Sealed
( fromSealedTx
)
import Cardano.Wallet.Primitive.Ledger.Shelley
( toCardanoTxIn
)
Expand Down Expand Up @@ -926,18 +926,14 @@ prop_signTransaction_preservesScriptIntegrity
Nothing
sealedTx

txIntegrityCardanoApi = txIntegrity . fromCardanoApiTx

getScriptIntegrityHashInAnyCardanoEra
:: InAnyCardanoEra Cardano.Tx
-> Maybe ByteString
getScriptIntegrityHashInAnyCardanoEra (InAnyCardanoEra _ tx') =
getHash <$> txIntegrityCardanoApi tx'
getScriptIntegrityHash :: SealedTx -> Maybe ByteString
getScriptIntegrityHash =
fmap getHash . txIntegrity . fromSealedTx

scriptIntegrityHashBefore =
getScriptIntegrityHashInAnyCardanoEra $ cardanoTx sealedTx
getScriptIntegrityHash sealedTx
scriptIntegrityHashAfter =
getScriptIntegrityHashInAnyCardanoEra $ cardanoTx sealedTx'
getScriptIntegrityHash sealedTx'

checkCoverage
$ cover 30 (isJust scriptIntegrityHashBefore)
Expand Down

0 comments on commit 5970083

Please sign in to comment.