Skip to content

Commit

Permalink
Introduce UnsignedTx
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimbo4350 committed Jul 15, 2024
1 parent 4491ed7 commit 23824d7
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 4 deletions.
71 changes: 67 additions & 4 deletions cardano-api/internal/Cardano/Api/Experimental/UnsignedTx.hs
Original file line number Diff line number Diff line change
@@ -1,13 +1,76 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}

module Cardano.Api.Experimental.UnsignedTx where

import Cardano.Api.Eras
import Cardano.Api.Experimental.Eras
import Cardano.Api.ReexposeLedger (strictMaybeToMaybe)
import Cardano.Api.Tx.Body

import qualified Cardano.Ledger.Core as Ledger

import Data.Bifunctor



-- A transaction that contains everything
-- except signing key witnesses
data UnsignedTx era
= UnsignedTx
(Ledger.Tx (ToConstrainedEra era))
(Ledger.TxWits (ToConstrainedEra era))
newtype UnsignedTx era
= UnsignedTx (Ledger.Tx (ToConstrainedEra era))


data UnsignedTxError
= UnsignedTxError TxBodyError

Check notice

Code scanning / HLint

Use newtype instead of data Note

cardano-api/internal/Cardano/Api/Experimental/UnsignedTx.hs:(23,1)-(24,31): Suggestion: Use newtype instead of data
  
Found:
  data UnsignedTxError = UnsignedTxError TxBodyError
  
Perhaps:
  newtype UnsignedTxError = UnsignedTxError TxBodyError
  
Note: decreases laziness

makeUnsignedTx
:: Era era
-> TxBodyContent BuildTx (AvailableErasToSbe era)
-> Either UnsignedTxError (UnsignedTx era)
makeUnsignedTx era bc = do
sbe <- maybe (Left $ error "") Right $ protocolVersionToSbe era

-- Construct tx body
let apiTxOuts = txOuts bc
apiScriptWitnesses = collectTxBodyScriptWitnesses sbe bc
apiScriptValidity = txScriptValidity bc
apiMintValue = txMintValue bc
apiProtocolParameters = txProtocolParams bc
apiCollateralTxIns = txInsCollateral bc
apiReferenceInputs = txInsReference bc
apiExtraKeyWitnesses = txExtraKeyWits bc
apiReturnCollateral = txReturnCollateral bc
apiTotalCollateral = txTotalCollateral bc

-- Ledger types
collTxIns = convCollateralTxIns apiCollateralTxIns
refTxIns = convReferenceInputs apiReferenceInputs
returnCollateral = convReturnCollateral sbe apiReturnCollateral
totalCollateral = convTotalCollateral apiTotalCollateral
certs = convCertificates sbe $ txCertificates bc
txAuxData = toAuxiliaryData sbe (txMetadata bc) (txAuxScripts bc)
scripts = convScripts apiScriptWitnesses
languages = convLanguages apiScriptWitnesses
sData = convScriptData sbe apiTxOuts apiScriptWitnesses

setUpdateProposal <- first UnsignedTxError $ convTxUpdateProposal sbe (txUpdateProposal bc)

setInvalidBefore <- maybe (Left $ error "TODO") Right $ convValidityLowerBound (txValidityLowerBound bc)

let setMint = convMintValue apiMintValue

setScriptIntegrityHash <- maybe (Left $ error "TODO") Right $ strictMaybeToMaybe $ getScriptIntegrityHash apiProtocolParameters languages sData

let setReqSignerHashes = convExtraKeyWitnesses apiExtraKeyWitnesses

let ledgerTxBody =
mkCommonTxBody sbe (txIns bc) (txOuts bc) (txFee bc) (txWithdrawals bc) txAuxData
& A.certsTxBodyL sbe .~ certs
& A.invalidHereAfterTxBodyL sbe .~ convValidityUpperBound sbe (txValidityUpperBound bc)

-- TODO: Left off here
undefined
-- Construct script witnesses

-- TODO: Left off here. Fill out this function and also
-- create a function for signing.
16 changes: 16 additions & 0 deletions cardano-api/internal/Cardano/Api/Tx/Body.hs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ module Cardano.Api.Tx.Body
, scriptDataToInlineDatum

-- * Internal conversion functions & types
, convCertificates
, convCollateralTxIns
, convExtraKeyWitnesses
, convLanguages
, convMintValue
, convReferenceInputs
, convReturnCollateral
, convScripts
, convScriptData
, convTotalCollateral
, convTxUpdateProposal
, convValidityLowerBound
, convValidityUpperBound
, getScriptIntegrityHash
, mkCommonTxBody
, toAuxiliaryData
, toByronTxId
, toShelleyTxId
, toShelleyTxIn
Expand Down

0 comments on commit 23824d7

Please sign in to comment.