Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add certs support in compatible Tx building #691

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cardano-api/internal/Cardano/Api/Eon/BabbageEraOnwards.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ instance Inject (BabbageEraOnwards era) (MaryEraOnwards era) where
BabbageEraOnwardsBabbage -> MaryEraOnwardsBabbage
BabbageEraOnwardsConway -> MaryEraOnwardsConway

instance Inject (BabbageEraOnwards era) (AlonzoEraOnwards era) where
inject = \case
BabbageEraOnwardsBabbage -> AlonzoEraOnwardsBabbage
BabbageEraOnwardsConway -> AlonzoEraOnwardsConway

type BabbageEraOnwardsConstraints era =
( C.HashAlgorithm (L.HASH (L.EraCrypto (ShelleyLedgerEra era)))
, C.Signable (L.VRF (L.EraCrypto (ShelleyLedgerEra era))) L.Seed
Expand Down
5 changes: 5 additions & 0 deletions cardano-api/internal/Cardano/Api/Eon/ConwayEraOnwards.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module Cardano.Api.Eon.ConwayEraOnwards
)
where

import Cardano.Api.Eon.AllegraEraOnwards (AllegraEraOnwards (..))
import Cardano.Api.Eon.BabbageEraOnwards
import Cardano.Api.Eon.ShelleyBasedEra
import Cardano.Api.Eras.Core
Expand Down Expand Up @@ -74,6 +75,10 @@ instance Inject (ConwayEraOnwards era) (ShelleyBasedEra era) where
inject = \case
ConwayEraOnwardsConway -> ShelleyBasedEraConway

instance Inject (ConwayEraOnwards era) (AllegraEraOnwards era) where
inject = \case
ConwayEraOnwardsConway -> AllegraEraOnwardsConway

instance Inject (ConwayEraOnwards era) (BabbageEraOnwards era) where
inject = \case
ConwayEraOnwardsConway -> BabbageEraOnwardsConway
Expand Down
97 changes: 33 additions & 64 deletions cardano-api/internal/Cardano/Api/Fees.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,13 @@ substituteExecutionUnits
redeemer
exunits

adjustWitness
:: (ScriptWitness witctx era -> Either (TxBodyErrorAutoBalance era) (ScriptWitness witctx era))
-> Witness witctx era
-> Either (TxBodyErrorAutoBalance era) (Witness witctx era)
adjustWitness _ (KeyWitness ctx) = Right $ KeyWitness ctx
adjustWitness g (ScriptWitness ctx witness') = ScriptWitness ctx <$> g witness'

mapScriptWitnessesTxIns
:: [(TxIn, BuildTxWith BuildTx (Witness WitCtxTxIn era))]
-> Either (TxBodyErrorAutoBalance era) [(TxIn, BuildTxWith BuildTx (Witness WitCtxTxIn era))]
Expand All @@ -1460,27 +1467,18 @@ substituteExecutionUnits
]
mappedScriptWitnesses =
[ (txin, BuildTxWith <$> wit')
| -- The tx ins are indexed in the map order by txid
(ix, (txin, BuildTxWith wit)) <- zip [0 ..] (orderTxIns txins)
, let wit' = case wit of
KeyWitness{} -> Right wit
ScriptWitness ctx witness -> ScriptWitness ctx <$> witness'
where
witness' = substituteExecUnits (ScriptWitnessIndexTxIn ix) witness
| (ix, txin, wit) <- txInsToIndexed txins
, let wit' = adjustWitness (substituteExecUnits ix) wit
]
in traverse
( \(txIn, eWitness) ->
case eWitness of
Left e -> Left e
Right wit -> Right (txIn, wit)
)
(\(txIn, eWitness) -> (txIn,) <$> eWitness)
mappedScriptWitnesses

mapScriptWitnessesWithdrawals
:: TxWithdrawals BuildTx era
-> Either (TxBodyErrorAutoBalance era) (TxWithdrawals BuildTx era)
mapScriptWitnessesWithdrawals TxWithdrawalsNone = Right TxWithdrawalsNone
mapScriptWitnessesWithdrawals (TxWithdrawals supported withdrawals) =
mapScriptWitnessesWithdrawals txWithdrawals'@(TxWithdrawals supported _) =
let mappedWithdrawals
:: [ ( StakeAddress
, L.Coin
Expand All @@ -1489,55 +1487,30 @@ substituteExecutionUnits
]
mappedWithdrawals =
[ (addr, withdrawal, BuildTxWith <$> mappedWitness)
| -- The withdrawals are indexed in the map order by stake credential
(ix, (addr, withdrawal, BuildTxWith wit)) <- zip [0 ..] (orderStakeAddrs withdrawals)
, let mappedWitness = adjustWitness (substituteExecUnits (ScriptWitnessIndexWithdrawal ix)) wit
| (ix, addr, withdrawal, wit) <- txWithdrawalsToIndexed txWithdrawals'
, let mappedWitness = adjustWitness (substituteExecUnits ix) wit
]
in TxWithdrawals supported
<$> traverse
( \(sAddr, ll, eWitness) ->
case eWitness of
Left e -> Left e
Right wit -> Right (sAddr, ll, wit)
)
(\(sAddr, ll, eWitness) -> (sAddr,ll,) <$> eWitness)
mappedWithdrawals
where
adjustWitness
:: (ScriptWitness witctx era -> Either (TxBodyErrorAutoBalance era) (ScriptWitness witctx era))
-> Witness witctx era
-> Either (TxBodyErrorAutoBalance era) (Witness witctx era)
adjustWitness _ (KeyWitness ctx) = Right $ KeyWitness ctx
adjustWitness g (ScriptWitness ctx witness') = ScriptWitness ctx <$> g witness'

mapScriptWitnessesCertificates
:: TxCertificates BuildTx era
-> Either (TxBodyErrorAutoBalance era) (TxCertificates BuildTx era)
mapScriptWitnessesCertificates TxCertificatesNone = Right TxCertificatesNone
mapScriptWitnessesCertificates
( TxCertificates
supported
certs
(BuildTxWith witnesses)
) =
let mappedScriptWitnesses
:: [(StakeCredential, Either (TxBodyErrorAutoBalance era) (Witness WitCtxStake era))]
mappedScriptWitnesses =
[ (stakecred, ScriptWitness ctx <$> witness')
| -- The certs are indexed in list order
(ix, cert) <- zip [0 ..] certs
, stakecred <- maybeToList (selectStakeCredentialWitness cert)
, ScriptWitness ctx witness <-
maybeToList (List.lookup stakecred witnesses)
, let witness' = substituteExecUnits (ScriptWitnessIndexCertificate ix) witness
]
in TxCertificates supported certs . BuildTxWith
<$> traverse
( \(sCred, eScriptWitness) ->
case eScriptWitness of
Left e -> Left e
Right wit -> Right (sCred, wit)
)
mappedScriptWitnesses
mapScriptWitnessesCertificates txCertificates'@(TxCertificates supported certs _) =
let mappedScriptWitnesses
:: [(StakeCredential, Either (TxBodyErrorAutoBalance era) (Witness WitCtxStake era))]
mappedScriptWitnesses =
[ (stakeCred, witness')
| (ix, _, stakeCred, witness) <- txCertificatesToIndexed txCertificates'
, let witness' = adjustWitness (substituteExecUnits ix) witness
]
in TxCertificates supported certs . BuildTxWith
<$> traverse
(\(sCred, eScriptWitness) -> (sCred,) <$> eScriptWitness)
mappedScriptWitnesses

mapScriptWitnessesVotes
:: Maybe (Featured ConwayEraOnwards era (TxVotingProcedures build era))
Expand All @@ -1547,13 +1520,11 @@ substituteExecutionUnits
mapScriptWitnessesVotes Nothing = return Nothing
mapScriptWitnessesVotes (Just (Featured _ TxVotingProceduresNone)) = return Nothing
mapScriptWitnessesVotes (Just (Featured _ (TxVotingProcedures _ ViewTx))) = return Nothing
mapScriptWitnessesVotes (Just (Featured era (TxVotingProcedures vProcedures (BuildTxWith sWitMap)))) = do
mapScriptWitnessesVotes (Just (Featured era txVotingProcedures'@(TxVotingProcedures vProcedures (BuildTxWith _)))) = do
let eSubstitutedExecutionUnits =
[ (vote, updatedWitness)
| let allVoteMap = L.unVotingProcedures vProcedures
, (vote, scriptWitness) <- toList sWitMap
, index <- maybeToList $ Map.lookupIndex vote allVoteMap
, let updatedWitness = substituteExecUnits (ScriptWitnessIndexVoting $ fromIntegral index) scriptWitness
| (ix, vote, witness) <- txVotingProceduresToIndexed txVotingProcedures'
, let updatedWitness = substituteExecUnits ix witness
]

substitutedExecutionUnits <- traverseScriptWitnesses eSubstitutedExecutionUnits
Expand All @@ -1570,13 +1541,11 @@ substituteExecutionUnits
mapScriptWitnessesProposals Nothing = return Nothing
mapScriptWitnessesProposals (Just (Featured _ TxProposalProceduresNone)) = return Nothing
mapScriptWitnessesProposals (Just (Featured _ (TxProposalProcedures _ ViewTx))) = return Nothing
mapScriptWitnessesProposals (Just (Featured era txpp@(TxProposalProcedures osetProposalProcedures (BuildTxWith sWitMap)))) = do
let allProposalsList = toList $ convProposalProcedures txpp
eSubstitutedExecutionUnits =
mapScriptWitnessesProposals (Just (Featured era txpp@(TxProposalProcedures osetProposalProcedures (BuildTxWith _)))) = do
let eSubstitutedExecutionUnits =
[ (proposal, updatedWitness)
| (proposal, scriptWitness) <- toList sWitMap
, index <- maybeToList $ List.elemIndex proposal allProposalsList
, let updatedWitness = substituteExecUnits (ScriptWitnessIndexProposing $ fromIntegral index) scriptWitness
| (ix, proposal, scriptWitness) <- txProposalProceduresToIndexed txpp
, let updatedWitness = substituteExecUnits ix scriptWitness
]

substitutedExecutionUnits <- traverseScriptWitnesses eSubstitutedExecutionUnits
Expand Down
34 changes: 17 additions & 17 deletions cardano-api/internal/Cardano/Api/Script.hs
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,23 @@ data SimpleScriptOrReferenceInput lang
| SReferenceScript TxIn
deriving (Eq, Show)

-- ----------------------------------------------------------------------------
-- The kind of witness to use, key (signature) or script
--

data Witness witctx era where
KeyWitness
:: KeyWitnessInCtx witctx
-> Witness witctx era
ScriptWitness
:: ScriptWitnessInCtx witctx
-> ScriptWitness witctx era
-> Witness witctx era

deriving instance Eq (Witness witctx era)

deriving instance Show (Witness witctx era)

-- | A /use/ of a script within a transaction body to witness that something is
-- being used in an authorised manner. That can be
--
Expand Down Expand Up @@ -797,23 +814,6 @@ getScriptWitnessReferenceInputOrScript = \case
PlutusScriptWitness _ _ (PReferenceScript txIn) _ _ _ ->
Right txIn

-- ----------------------------------------------------------------------------
-- The kind of witness to use, key (signature) or script
--

data Witness witctx era where
KeyWitness
:: KeyWitnessInCtx witctx
-> Witness witctx era
ScriptWitness
:: ScriptWitnessInCtx witctx
-> ScriptWitness witctx era
-> Witness witctx era

deriving instance Eq (Witness witctx era)

deriving instance Show (Witness witctx era)

data KeyWitnessInCtx witctx where
KeyWitnessForSpending :: KeyWitnessInCtx WitCtxTxIn
KeyWitnessForStakeAddr :: KeyWitnessInCtx WitCtxStake
Expand Down
Loading