Skip to content

Commit

Permalink
Merge #2547
Browse files Browse the repository at this point in the history
2547: API refactoring to move scripts into the txbody r=Jimbo4350 a=dcoutts

REMOVE: Merge #2498 after this PR has been merged

This is the first step in a refactoring that we will need for Alonzo
support. The refactoring will put the scripts within the tx body at the
sites where they are used. For Shelley era scripts we can do it either
way round, but for Alonzo it makes much more sense to place the script
at the use sites. So to break the Alonzo changes into steps we do the
refactoring for the existing Shelley-based eras first.
    
The ideas here have been co-authored with Jordan Millar and most of the
code changes here appeared first in his Alonzo branch.

- [x] Update the API tests for these API changes
- [x] Update the CLI for these API changes

There are a couple breaking changes:
- You must specify auxiliary scripts with `--auxiliary-script-file` instead of `--script-file`
- Scripts  witnessing txins, certificates, withdrawals and minting must be paired with the thing they are witnessing. 
E.g:
```
--certificate-file  $certfile --certificate-script-file $scriptfile
--tx-out $txout --minting-script-file $scriptfile
--withdrawal $withdrawal --withdrawal-script-file $scriptfile
--tx-in $txin --txin-script-file $scriptfile
```
- Scripts are no longer specified at the tx signing stage. They are specified at the txbody stage.

Co-authored-by: Duncan Coutts <duncan@well-typed.com>
Co-authored-by: Jordan Millar <jordan.millar@iohk.io>
  • Loading branch information
3 people committed Apr 19, 2021
2 parents 09077b0 + aa43c7f commit 28b934f
Show file tree
Hide file tree
Showing 34 changed files with 859 additions and 631 deletions.
15 changes: 13 additions & 2 deletions cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ module Cardano.Api (
TxUpdateProposal(..),
TxMintValue(..),

-- ** Building vs viewing transactions
BuildTxWith(..),
BuildTx,
ViewTx,

-- ** Era-dependent transaction body features
MultiAssetSupportedInEra(..),
OnlyAdaSupportedInEra(..),
Expand Down Expand Up @@ -214,13 +219,12 @@ module Cardano.Api (

-- ** Incremental signing and separate witnesses
makeSignedTransaction,
Witness,
KeyWitness,
makeByronKeyWitness,
makeByronTransaction,
ShelleyWitnessSigningKey(..),
makeShelleyKeyWitness,
makeShelleyBootstrapWitness,
makeScriptWitness,

-- * Fee calculation
transactionFee,
Expand Down Expand Up @@ -296,6 +300,13 @@ module Cardano.Api (
toScriptInEra,
eraOfScriptInEra,

-- * Use of a script in an era as a witness
WitCtxTxIn, WitCtxMint, WitCtxStake,
ScriptWitness(..),
Witness(..),
KeyWitnessInCtx(..),
ScriptWitnessInCtx(..),

-- *** Languages supported in each era
ScriptLanguageInEra(..),
scriptLanguageSupportedInEra,
Expand Down
2 changes: 1 addition & 1 deletion cardano-api/src/Cardano/Api/Byron.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module Cardano.Api.Byron
Tx(ByronTx),

-- ** Incremental signing and separate witnesses
Witness (ByronKeyWitness),
KeyWitness (ByronKeyWitness),
WitnessNetworkIdOrByronAddress
( WitnessNetworkId
, WitnessByronAddress
Expand Down
96 changes: 96 additions & 0 deletions cardano-api/src/Cardano/Api/Script.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ module Cardano.Api.Script (
toScriptInEra,
eraOfScriptInEra,

-- * Use of a script in an era as a witness
WitCtxTxIn, WitCtxMint, WitCtxStake,
ScriptWitness(..),
Witness(..),
KeyWitnessInCtx(..),
ScriptWitnessInCtx(..),

-- ** Languages supported in each era
ScriptLanguageInEra(..),
scriptLanguageSupportedInEra,
Expand Down Expand Up @@ -518,6 +525,95 @@ eraOfScriptInEra :: ScriptInEra era -> ShelleyBasedEra era
eraOfScriptInEra (ScriptInEra langInEra _) = eraOfScriptLanguageInEra langInEra


-- ----------------------------------------------------------------------------
-- Scripts used in a transaction (in an era) to witness authorised use
--

-- | A tag type for the context in which a script is used in a transaction.
--
-- This type tags the context as being to witness a transaction input.
--
data WitCtxTxIn

-- | A tag type for the context in which a script is used in a transaction.
--
-- This type tags the context as being to witness minting.
--
data WitCtxMint

-- | A tag type for the context in which a script is used in a transaction.
--
-- This type tags the context as being to witness the use of stake addresses in
-- both certificates and withdrawals.
--
data WitCtxStake

-- | A /use/ of a script within a transaction body to witness that something is
-- being used in an authorised manner. That can be
--
-- * spending a transaction input
-- * minting tokens
-- * using a certificate (stake address certs specifically)
-- * withdrawing from a reward account
--
-- For simple script languages, the use of the script is the same in all
-- contexts. For Plutus scripts, using a script involves supplying a redeemer.
-- In addition, Plutus scripts used for spending inputs must also supply the
-- datum value used when originally creating the TxOut that is now being spent.
--
data ScriptWitness witctx era where

SimpleScriptWitness :: ScriptLanguageInEra lang era
-> SimpleScriptVersion lang
-> SimpleScript lang
-> ScriptWitness witctx era

deriving instance Show (ScriptWitness witctx era)

-- The GADT in the SimpleScriptWitness constructor requires a custom instance
instance Eq (ScriptWitness witctx era) where
(==) (SimpleScriptWitness langInEra version script)
(SimpleScriptWitness langInEra' version' script') =
case testEquality (languageOfScriptLanguageInEra langInEra)
(languageOfScriptLanguageInEra langInEra') of
Nothing -> False
Just Refl -> version == version' && script == script'


-- ----------------------------------------------------------------------------
-- 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

data ScriptWitnessInCtx witctx where

ScriptWitnessForSpending :: ScriptWitnessInCtx WitCtxTxIn
ScriptWitnessForMinting :: ScriptWitnessInCtx WitCtxMint
ScriptWitnessForStakeAddr :: ScriptWitnessInCtx WitCtxStake

deriving instance Eq (KeyWitnessInCtx witctx)
deriving instance Show (KeyWitnessInCtx witctx)

deriving instance Eq (ScriptWitnessInCtx witctx)
deriving instance Show (ScriptWitnessInCtx witctx)


-- ----------------------------------------------------------------------------
-- Script Hash
--
Expand Down
3 changes: 1 addition & 2 deletions cardano-api/src/Cardano/Api/Shelley.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ module Cardano.Api.Shelley
Tx(ShelleyTx),

-- ** Incremental signing and separate witnesses
Witness
KeyWitness
( ShelleyBootstrapWitness
, ShelleyKeyWitness
, ShelleyScriptWitness
),
ShelleyWitnessSigningKey
( WitnessPaymentKey
Expand Down

0 comments on commit 28b934f

Please sign in to comment.