Skip to content

Commit 9d4a145

Browse files
committed
Split compatible transaction building into separate building and witnessing functions
1 parent b61aa25 commit 9d4a145

File tree

5 files changed

+71
-48
lines changed

5 files changed

+71
-48
lines changed

cardano-api/cardano-api.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ library
179179
other-modules:
180180
Cardano.Api.Internal.Anchor
181181
Cardano.Api.Internal.Certificate
182+
Cardano.Api.Internal.Compatible.Tx
182183
Cardano.Api.Internal.Convenience.Construction
183184
Cardano.Api.Internal.Convenience.Query
184185
Cardano.Api.Internal.DeserialiseAnyOf
@@ -247,7 +248,6 @@ library
247248
Cardano.Api.Internal.SpecialByron
248249
Cardano.Api.Internal.StakePoolMetadata
249250
Cardano.Api.Internal.Tx.Body
250-
Cardano.Api.Internal.Tx.Compatible
251251
Cardano.Api.Internal.Tx.UTxO
252252
Cardano.Api.Internal.TxIn
253253
Cardano.Api.Internal.TxMetadata

cardano-api/src/Cardano/Api.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,9 @@ module Cardano.Api
461461
, makeByronKeyWitness
462462
, ShelleyWitnessSigningKey (..)
463463
, makeShelleyKeyWitness
464+
, makeShelleyKeyWitness'
464465
, makeShelleyBootstrapWitness
466+
, makeShelleyBasedBootstrapWitness
465467

466468
-- * Transaction metadata
467469

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Cardano.Api.Compatible
2-
( module Cardano.Api.Internal.Tx.Compatible
2+
( module Cardano.Api.Internal.Compatible.Tx
33
)
44
where
55

6-
import Cardano.Api.Internal.Tx.Compatible
6+
import Cardano.Api.Internal.Compatible.Tx

cardano-api/src/Cardano/Api/Internal/Tx/Compatible.hs renamed to cardano-api/src/Cardano/Api/Internal/Compatible/Tx.hs

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88
-- | This module provides a way to construct a simple transaction over all eras.
99
-- It is exposed for testing purposes only.
10-
module Cardano.Api.Internal.Tx.Compatible
10+
module Cardano.Api.Internal.Compatible.Tx
1111
( AnyProtocolUpdate (..)
1212
, AnyVote (..)
13-
, createCompatibleSignedTx
13+
, createCompatibleTx
14+
, addWitnesses
1415
)
1516
where
1617

@@ -60,19 +61,19 @@ data AnyVote era where
6061
-> AnyVote era
6162
NoVotes :: AnyVote era
6263

63-
createCompatibleSignedTx
64+
-- | Create a transaction in any shelley based era
65+
createCompatibleTx
6466
:: forall era
6567
. ShelleyBasedEra era
6668
-> [TxIn]
6769
-> [TxOut CtxTx era]
68-
-> [KeyWitness era]
6970
-> Lovelace
7071
-- ^ Fee
7172
-> AnyProtocolUpdate era
7273
-> AnyVote era
7374
-> TxCertificates BuildTx era
7475
-> Either ProtocolParametersConversionError (Tx era)
75-
createCompatibleSignedTx sbe ins outs witnesses txFee' anyProtocolUpdate anyVote txCertificates' =
76+
createCompatibleTx sbe ins outs txFee' anyProtocolUpdate anyVote txCertificates' =
7677
shelleyBasedEraConstraints sbe $ do
7778
(updateTxBody, extraScriptWitnesses) <-
7879
case anyProtocolUpdate of
@@ -125,7 +126,7 @@ createCompatibleSignedTx sbe ins outs witnesses txFee' anyProtocolUpdate anyVote
125126
. ShelleyTx sbe
126127
$ L.mkBasicTx txbody
127128
& L.witsTxL
128-
.~ allWitnesses (apiScriptWitnesses <> extraScriptWitnesses) allShelleyToBabbageWitnesses
129+
%~ setScriptWitnesses (apiScriptWitnesses <> extraScriptWitnesses)
129130
& updateVotingProcedures
130131
where
131132
era = toCardanoEra sbe
@@ -164,11 +165,11 @@ createCompatibleSignedTx sbe ins outs witnesses txFee' anyProtocolUpdate anyVote
164165
:: [(ScriptWitnessIndex, Certificate era, StakeCredential, Witness WitCtxStake era)]
165166
indexedTxCerts = indexTxCertificates txCertificates'
166167

167-
allWitnesses
168+
setScriptWitnesses
168169
:: [(ScriptWitnessIndex, AnyScriptWitness era)]
169170
-> L.TxWits (ShelleyLedgerEra era)
170171
-> L.TxWits (ShelleyLedgerEra era)
171-
allWitnesses scriptWitnesses =
172+
setScriptWitnesses scriptWitnesses =
172173
appEndos
173174
[ monoidForEraInEon
174175
era
@@ -191,21 +192,6 @@ createCompatibleSignedTx sbe ins outs witnesses txFee' anyProtocolUpdate anyVote
191192
)
192193
]
193194

194-
allShelleyToBabbageWitnesses
195-
:: L.EraTxWits (ShelleyLedgerEra era)
196-
=> L.EraCrypto (ShelleyLedgerEra era) ~ L.StandardCrypto
197-
=> L.TxWits (ShelleyLedgerEra era)
198-
allShelleyToBabbageWitnesses = do
199-
let shelleyKeywitnesses =
200-
fromList [w | ShelleyKeyWitness _ w <- witnesses]
201-
let shelleyBootstrapWitnesses =
202-
fromList [w | ShelleyBootstrapWitness _ w <- witnesses]
203-
L.mkBasicTxWits
204-
& L.addrTxWitsL
205-
.~ shelleyKeywitnesses
206-
& L.bootAddrTxWitsL
207-
.~ shelleyBootstrapWitnesses
208-
209195
createCommonTxBody
210196
:: HasCallStack
211197
=> ShelleyBasedEra era
@@ -224,3 +210,31 @@ createCommonTxBody era ins outs txFee' =
224210
.~ Seq.fromList txOuts'
225211
& L.feeTxBodyL
226212
.~ txFee'
213+
214+
-- | Add provided witnesses to the transaction
215+
addWitnesses
216+
:: forall era
217+
. [KeyWitness era]
218+
-> Tx era
219+
-> Tx era
220+
-- ^ a signed transaction
221+
addWitnesses witnesses (ShelleyTx sbe tx) =
222+
shelleyBasedEraConstraints sbe $
223+
ShelleyTx sbe txCommon
224+
where
225+
txCommon
226+
:: forall ledgerera
227+
. ShelleyLedgerEra era ~ ledgerera
228+
=> L.EraCrypto ledgerera ~ L.StandardCrypto
229+
=> L.EraTx ledgerera
230+
=> L.Tx ledgerera
231+
txCommon =
232+
tx
233+
& L.witsTxL
234+
%~ ( ( L.addrTxWitsL
235+
%~ (<> fromList [w | ShelleyKeyWitness _ w <- witnesses])
236+
)
237+
. ( L.bootAddrTxWitsL
238+
%~ (<> fromList [w | ShelleyBootstrapWitness _ w <- witnesses])
239+
)
240+
)

cardano-api/src/Cardano/Api/Internal/Tx/Sign.hs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
{-# LANGUAGE FlexibleContexts #-}
33
{-# LANGUAGE FlexibleInstances #-}
44
{-# LANGUAGE GADTs #-}
5-
{-# LANGUAGE LambdaCase #-}
65
{-# LANGUAGE PatternSynonyms #-}
76
{-# LANGUAGE ScopedTypeVariables #-}
87
{-# LANGUAGE StandaloneDeriving #-}
@@ -14,8 +13,6 @@
1413
-- not export any from this API. We also use them unticked as nature intended.
1514
{-# OPTIONS_GHC -Wno-unticked-promoted-constructors #-}
1615

17-
{- HLINT ignore "Avoid lambda using `infix`" -}
18-
1916
-- | Complete, signed transactions
2017
module Cardano.Api.Internal.Tx.Sign
2118
( -- * Signing transactions
@@ -43,8 +40,10 @@ module Cardano.Api.Internal.Tx.Sign
4340
, makeByronKeyWitness
4441
, ShelleyWitnessSigningKey (..)
4542
, makeShelleyKeyWitness
43+
, makeShelleyKeyWitness'
4644
, WitnessNetworkIdOrByronAddress (..)
4745
, makeShelleyBootstrapWitness
46+
, makeShelleyBasedBootstrapWitness
4847
, makeShelleySignature
4948
, getShelleyKeyWitnessVerificationKey
5049
, getTxBodyAndWitnesses
@@ -127,6 +126,12 @@ data Tx era where
127126
-> L.Tx (ShelleyLedgerEra era)
128127
-> Tx era
129128

129+
-- | This pattern will be deprecated in the future. We advise against introducing new usage of it.
130+
pattern Tx :: TxBody era -> [KeyWitness era] -> Tx era
131+
pattern Tx txbody ws <- (getTxBodyAndWitnesses -> (txbody, ws))
132+
where
133+
Tx txbody ws = makeSignedTransaction ws txbody
134+
130135
instance Show (InAnyCardanoEra Tx) where
131136
show (InAnyCardanoEra _ tx) = show tx
132137

@@ -749,12 +754,6 @@ instance IsCardanoEra era => HasTextEnvelope (KeyWitness era) where
749754
getTxBodyAndWitnesses :: Tx era -> (TxBody era, [KeyWitness era])
750755
getTxBodyAndWitnesses tx = (getTxBody tx, getTxWitnesses tx)
751756

752-
-- | This pattern will be deprecated in the future. We advise against introducing new usage of it.
753-
pattern Tx :: TxBody era -> [KeyWitness era] -> Tx era
754-
pattern Tx txbody ws <- (getTxBodyAndWitnesses -> (txbody, ws))
755-
where
756-
Tx txbody ws = makeSignedTransaction ws txbody
757-
758757
{-# COMPLETE Tx #-}
759758

760759
data ShelleyWitnessSigningKey
@@ -1106,19 +1105,27 @@ makeShelleyKeyWitness
11061105
-> TxBody era
11071106
-> ShelleyWitnessSigningKey
11081107
-> KeyWitness era
1109-
makeShelleyKeyWitness sbe = \case
1110-
ShelleyTxBody _ txbody _ _ _ _ ->
1111-
shelleyBasedEraConstraints sbe $
1112-
let txhash :: Shelley.Hash StandardCrypto Ledger.EraIndependentTxBody
1113-
txhash = Ledger.extractHash @StandardCrypto (Ledger.hashAnnotated txbody)
1114-
in -- To allow sharing of the txhash computation across many signatures we
1115-
-- define and share the txhash outside the lambda for the signing key:
1116-
\wsk ->
1117-
let sk = toShelleySigningKey wsk
1118-
vk = getShelleyKeyWitnessVerificationKey sk
1119-
signature = makeShelleySignature txhash sk
1120-
in ShelleyKeyWitness sbe $
1121-
L.WitVKey vk signature
1108+
makeShelleyKeyWitness sbe (ShelleyTxBody _ txBody _ _ _ _) =
1109+
makeShelleyKeyWitness' sbe txBody
1110+
1111+
makeShelleyKeyWitness'
1112+
:: forall era
1113+
. ()
1114+
=> ShelleyBasedEra era
1115+
-> L.TxBody (ShelleyLedgerEra era)
1116+
-> ShelleyWitnessSigningKey
1117+
-> KeyWitness era
1118+
makeShelleyKeyWitness' sbe txBody wsk =
1119+
shelleyBasedEraConstraints sbe $ do
1120+
let txhash :: Shelley.Hash StandardCrypto Ledger.EraIndependentTxBody
1121+
txhash = Ledger.extractHash @StandardCrypto (Ledger.hashAnnotated txBody)
1122+
-- To allow sharing of the txhash computation across many signatures we
1123+
-- define and share the txhash outside the lambda for the signing key:
1124+
sk = toShelleySigningKey wsk
1125+
vk = getShelleyKeyWitnessVerificationKey sk
1126+
signature = makeShelleySignature txhash sk
1127+
ShelleyKeyWitness sbe $
1128+
L.WitVKey vk signature
11221129

11231130
toShelleySigningKey :: ShelleyWitnessSigningKey -> ShelleySigningKey
11241131
toShelleySigningKey key = case key of

0 commit comments

Comments
 (0)