Safe Haskell | None |
---|---|
Language | Haskell2010 |
Cardano.Api.Experimental.Tx
Synopsis
- newtype UnsignedTx era = UnsignedTx (Tx (LedgerEra era))
- newtype UnsignedTxError = UnsignedTxError TxBodyError
- makeUnsignedTx :: Era era -> TxBodyContent BuildTx era -> Either TxBodyError (UnsignedTx era)
- makeKeyWitness :: Era era -> UnsignedTx era -> ShelleyWitnessSigningKey -> WitVKey 'Witness
- signTx :: Era era -> [BootstrapWitness] -> [WitVKey 'Witness] -> UnsignedTx era -> Tx (LedgerEra era)
- convertTxBodyToUnsignedTx :: HasCallStack => ShelleyBasedEra era -> TxBody era -> UnsignedTx era
- hashTxBody :: HashAnnotated (TxBody era) EraIndependentTxBody => TxBody era -> Hash HASH EraIndependentTxBody
- data AnyWitness era where
- AnyKeyWitnessPlaceholder :: forall era. AnyWitness era
- AnySimpleScriptWitness :: forall era. SimpleScriptOrReferenceInput era -> AnyWitness era
- AnyPlutusScriptWitness :: forall (lang :: Language) (purpose :: PlutusScriptPurpose) era. PlutusScriptWitness lang purpose era -> AnyWitness era
- getAnyWitnessScript :: ShelleyBasedEra era -> AnyWitness (ShelleyLedgerEra era) -> Maybe (Script (ShelleyLedgerEra era))
- getAnyWitnessPlutusLanguage :: AnyWitness era -> Maybe Language
- getAnyWitnessScriptData :: AlonzoEraOnwards era -> AnyWitness (ShelleyLedgerEra era) -> TxDats (ShelleyLedgerEra era)
- data TxScriptWitnessRequirements era = TxScriptWitnessRequirements (Set Language) [Script era] (TxDats era) (Redeemers era)
- getTxScriptWitnessesRequirements :: forall era (witnessable :: WitnessableItem). AlonzoEraOnwards era -> [(Witnessable witnessable (ShelleyLedgerEra era), AnyWitness (ShelleyLedgerEra era))] -> TxScriptWitnessRequirements (ShelleyLedgerEra era)
- obtainMonoidConstraint :: AlonzoEraOnwards era -> (Monoid (TxScriptWitnessRequirements (ShelleyLedgerEra era)) => a) -> a
- extractExecutionUnits :: TxScriptWitnessRequirements era -> [ExecutionUnits]
- getTxScriptWitnessRequirements :: forall era (witnessable :: WitnessableItem). AlonzoEraOnwards era -> [(Witnessable witnessable (ShelleyLedgerEra era), AnyWitness (ShelleyLedgerEra era))] -> TxScriptWitnessRequirements (ShelleyLedgerEra era)
Creating transactions using the new API
Both the old and new APIs can be used to create transactions, and it is possible to transform a transaction from one format to the other as they share the same representation. However, the focus will shift towards using the new API, while the old API will be deprecated to ensure simplicity, closer alignment with the ledger, and easier maintenance.
In both the new and old APIs, constructing a transaction requires creating
a TxBodyContent
, along with at least one witness (for example, a
ShelleyWitnessSigningKey
) to sign the transaction.
This process remains unchanged.
To learn how to create a transaction using the old API, see the Cardano.Api.Tx.Internal.Body documentation.
In the examples below, the following qualified modules are used:
import qualified Cardano.Api as Api -- the general `cardano-api` exports (including the old API) import qualified Cardano.Api.Script as Script -- types related to scripts (Plutus and native) import qualified Cardano.Api.Ledger as Ledger -- cardano-ledger re-exports import qualified Cardano.Api.Experimental as Exp -- the experimental API
For instructions on how to do this, refer to the Test.Cardano.Api.Experimental
documentation.
Creating a TxBodyContent
Regardless of whether the experimental or the traditional API is used, creating a TxBodyContent
is necessary.
You can see how to do this in the documentation of the Cardano.Api.Tx.Internal.Body module.
Balancing a transaction
If a UTXO has exactly 12 ada, the transaction could be constructed as described in Cardano.Api.Tx.Internal.Body, and it would be valid. However:
- Ada may be wasted
- The UTXO that we intend to spend may not contain exactly 12 ada
- The transaction may not be this simple.
For these reasons, it is recommended to balance the transaction before proceeding with signing and submitting.
For instructions on how to balance a transaction, refer to the Cardano.Api.Tx.Internal.Fee documentation.
Creating a ShelleyWitnessSigningKey
Signing a transaction requires a witness, such as a ShelleyWitnessSigningKey
.
For instructions on creating a ShelleyWitnessSigningKey
refer to the Cardano.Api.Tx.Internal.Sign documentation.
Creating a transaction using the new API
This section outlines how to create a transaction using the new API. First,
create an UnsignedTx
using the makeUnsignedTx
function and the Era
and
TxBodyContent
that we defined earlier:
let (Right unsignedTx) = Exp.makeUnsignedTx era txBodyContent
Next, use the key witness to sign the unsigned transaction with the makeKeyWitness
function:
let transactionWitness = Exp.makeKeyWitness era unsignedTx (Api.WitnessPaymentKey signingKey)
Finally, sign the transaction using the signTx
function:
let newApiSignedTx :: Ledger.Tx (Exp.LedgerEra Exp.ConwayEra) = Exp.signTx era [] [transactionWitness] unsignedTx
The empty list represents the bootstrap witnesses, which are not needed in this case.
The transaction is now signed.
Converting a transaction from the new API to the old API
A transaction created with the new API can be easily converted to the old API by
wrapping it with the ShelleyTx
constructor:
let oldStyleTx :: Api.Tx Api.ConwayEra = ShelleyTx sbe newApiSignedTx
Inspecting transactions
When using a Tx
created with the experimental API, the TxBody
and
TxWits
can be extracted using the txBody
and txWits
lenses from
Cardano.Api.Ledger respectively.
Contents
newtype UnsignedTx era Source #
A transaction that can contain everything except key witnesses.
Constructors
UnsignedTx (Tx (LedgerEra era)) |
Instances
IsEra era => Show (UnsignedTx era) Source # | |
Defined in Cardano.Api.Experimental.Tx |
newtype UnsignedTxError Source #
Constructors
UnsignedTxError TxBodyError |
makeUnsignedTx :: Era era -> TxBodyContent BuildTx era -> Either TxBodyError (UnsignedTx era) Source #
makeKeyWitness :: Era era -> UnsignedTx era -> ShelleyWitnessSigningKey -> WitVKey 'Witness Source #
signTx :: Era era -> [BootstrapWitness] -> [WitVKey 'Witness] -> UnsignedTx era -> Tx (LedgerEra era) Source #
convertTxBodyToUnsignedTx :: HasCallStack => ShelleyBasedEra era -> TxBody era -> UnsignedTx era Source #
hashTxBody :: HashAnnotated (TxBody era) EraIndependentTxBody => TxBody era -> Hash HASH EraIndependentTxBody Source #
Witness
Any witness (key, simple script, plutus script).
data AnyWitness era where Source #
Here we consider three types of witnesses in Cardano: * key witnesses * simple script witnesses * Plutus script witnesses
Note that AnyKeyWitnessPlaceholder
does not contain the actual key witness. This is because
key witnesses are provided in the signing stage of the transaction. However we need this constuctor
to index the witnessable things correctly when plutus scripts are being used within the transaction.
AnyWitness is solely used to contruct the transaction body.
Constructors
AnyKeyWitnessPlaceholder :: forall era. AnyWitness era | |
AnySimpleScriptWitness :: forall era. SimpleScriptOrReferenceInput era -> AnyWitness era | |
AnyPlutusScriptWitness :: forall (lang :: Language) (purpose :: PlutusScriptPurpose) era. PlutusScriptWitness lang purpose era -> AnyWitness era |
getAnyWitnessScript :: ShelleyBasedEra era -> AnyWitness (ShelleyLedgerEra era) -> Maybe (Script (ShelleyLedgerEra era)) Source #
getAnyWitnessPlutusLanguage :: AnyWitness era -> Maybe Language Source #
getAnyWitnessScriptData :: AlonzoEraOnwards era -> AnyWitness (ShelleyLedgerEra era) -> TxDats (ShelleyLedgerEra era) Source #
NB this does not include datums from inline datums existing at tx outputs!
All the parts that constitute a plutus script witness but also including simple scripts
data TxScriptWitnessRequirements era Source #
This type collects all the requirements for script witnesses in a transaction.
Instances
Collecting plutus script witness related transaction requirements.
getTxScriptWitnessesRequirements :: forall era (witnessable :: WitnessableItem). AlonzoEraOnwards era -> [(Witnessable witnessable (ShelleyLedgerEra era), AnyWitness (ShelleyLedgerEra era))] -> TxScriptWitnessRequirements (ShelleyLedgerEra era) Source #
obtainMonoidConstraint :: AlonzoEraOnwards era -> (Monoid (TxScriptWitnessRequirements (ShelleyLedgerEra era)) => a) -> a Source #
Internal functions
getTxScriptWitnessRequirements :: forall era (witnessable :: WitnessableItem). AlonzoEraOnwards era -> [(Witnessable witnessable (ShelleyLedgerEra era), AnyWitness (ShelleyLedgerEra era))] -> TxScriptWitnessRequirements (ShelleyLedgerEra era) Source #