diff --git a/cardano-api/src/Cardano/Api.hs b/cardano-api/src/Cardano/Api.hs index 9e9f8f04c1c..3cc0fe22c2c 100644 --- a/cardano-api/src/Cardano/Api.hs +++ b/cardano-api/src/Cardano/Api.hs @@ -67,6 +67,7 @@ module Cardano.Api ( -- ** Byron addresses makeByronAddress, ByronKey, + ByronKeyLegacy, -- ** Shelley addresses makeShelleyAddress, @@ -422,6 +423,7 @@ module Cardano.Api ( -- ** Conversions --TODO: arrange not to export these toNetworkMagic, + toByronLovelace, ) where import Cardano.Api.Typed diff --git a/cardano-api/src/Cardano/Api/Byron.hs b/cardano-api/src/Cardano/Api/Byron.hs index 22cd7ee0442..3cf3177c308 100644 --- a/cardano-api/src/Cardano/Api/Byron.hs +++ b/cardano-api/src/Cardano/Api/Byron.hs @@ -81,9 +81,12 @@ module Cardano.Api.Byron toByronLedgertoByronVote, -- ** Conversions + fromByronTxIn, toByronNetworkMagic, toByronProtocolMagicId, toByronRequiresNetworkMagic, + + makeByronTransaction, ) where import Cardano.Api diff --git a/cardano-api/src/Cardano/Api/Tx.hs b/cardano-api/src/Cardano/Api/Tx.hs index 024ffa0dbe5..f218f0f07fe 100644 --- a/cardano-api/src/Cardano/Api/Tx.hs +++ b/cardano-api/src/Cardano/Api/Tx.hs @@ -73,11 +73,11 @@ import qualified Cardano.Crypto.Wallet as Crypto.HD -- -- Byron imports -- +import qualified Cardano.Chain.Common as Byron +import qualified Cardano.Chain.UTxO as Byron import qualified Cardano.Crypto.Hashing as Byron import qualified Cardano.Crypto.ProtocolMagic as Byron import qualified Cardano.Crypto.Signing as Byron -import qualified Cardano.Chain.Common as Byron -import qualified Cardano.Chain.UTxO as Byron -- -- Shelley imports @@ -521,9 +521,10 @@ makeSignedTransaction witnesses (ShelleyTxBody era txbody txmetadata) = (maybeToStrictMaybe txmetadata) -makeByronKeyWitness :: NetworkId +makeByronKeyWitness :: forall key. IsByronLegacyFormat key + => NetworkId -> TxBody ByronEra - -> SigningKey ByronKey + -> SigningKey key -> Witness ByronEra makeByronKeyWitness _ (ShelleyTxBody era _ _) = case era of {} makeByronKeyWitness nw (ByronTxBody txbody) = @@ -535,11 +536,18 @@ makeByronKeyWitness nw (ByronTxBody txbody) = -- To allow sharing of the txhash computation across many signatures we -- define and share the txhash outside the lambda for the signing key: - in \(ByronSigningKey sk) -> - ByronKeyWitness $ - Byron.VKWitness - (Byron.toVerification sk) - (Byron.sign pm Byron.SignTx sk (Byron.TxSigData txhash)) + in case isLegacyFormat :: ByronKeyFormat key of + ByronLegacyKeyFormat -> + \(ByronSigningKeyLegacy sk) -> witness sk pm txhash + ByronNonLegacyKeyFormat -> + \(ByronSigningKey sk) -> witness sk pm txhash + where + witness :: Byron.SigningKey -> Byron.ProtocolMagicId -> Byron.Hash Byron.Tx -> Witness ByronEra + witness sk pm txHash = + ByronKeyWitness $ + Byron.VKWitness + (Byron.toVerification sk) + (Byron.sign pm Byron.SignTx sk (Byron.TxSigData txHash)) -- | Either a network ID or a Byron address to be used in constructing a -- Shelley bootstrap witness. diff --git a/cardano-api/src/Cardano/Api/TxBody.hs b/cardano-api/src/Cardano/Api/TxBody.hs index 52ab7f9031c..56638c76ff9 100644 --- a/cardano-api/src/Cardano/Api/TxBody.hs +++ b/cardano-api/src/Cardano/Api/TxBody.hs @@ -86,6 +86,9 @@ module Cardano.Api.TxBody ( -- * Data family instances AsType(AsTxId, AsTxBody, AsByronTxBody, AsShelleyTxBody), + + -- * Conversion functions + fromByronTxIn, ) where import Prelude @@ -224,6 +227,13 @@ newtype TxIx = TxIx Word deriving stock (Eq, Ord, Show) deriving newtype (Enum) +fromByronTxIn :: Byron.TxIn -> TxIn +fromByronTxIn (Byron.TxInUtxo txId index) = + let shortBs = Byron.abstractHashToShort txId + mApiHash = Crypto.hashFromBytesShort shortBs + in case mApiHash of + Just apiHash -> TxIn (TxId apiHash) (TxIx . fromIntegral $ toInteger index) + Nothing -> error $ "Error converting Byron era TxId: " <> show txId toByronTxIn :: TxIn -> Byron.TxIn toByronTxIn (TxIn txid (TxIx txix)) = diff --git a/cardano-api/src/Cardano/Api/Typed.hs b/cardano-api/src/Cardano/Api/Typed.hs index ee953552099..e88ee5ff096 100644 --- a/cardano-api/src/Cardano/Api/Typed.hs +++ b/cardano-api/src/Cardano/Api/Typed.hs @@ -84,6 +84,8 @@ module Cardano.Api.Typed ( -- ** Byron addresses makeByronAddress, ByronKey, + ByronKeyLegacy, + -- ** Shelley addresses makeShelleyAddress, PaymentCredential(..), @@ -475,6 +477,8 @@ module Cardano.Api.Typed ( -- ** Conversions --TODO: arrange not to export these + fromByronTxIn, + toByronLovelace, toByronNetworkMagic, toByronProtocolMagicId, toByronRequiresNetworkMagic,