Skip to content

Commit

Permalink
Propagate the updated functions throughout cardano-api
Browse files Browse the repository at this point in the history
Expose helper functions
  • Loading branch information
Jimbo4350 committed Jan 11, 2021
1 parent ddf0c88 commit 9620a55
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
2 changes: 2 additions & 0 deletions cardano-api/src/Cardano/Api.hs
Expand Up @@ -67,6 +67,7 @@ module Cardano.Api (
-- ** Byron addresses
makeByronAddress,
ByronKey,
ByronKeyLegacy,

-- ** Shelley addresses
makeShelleyAddress,
Expand Down Expand Up @@ -422,6 +423,7 @@ module Cardano.Api (
-- ** Conversions
--TODO: arrange not to export these
toNetworkMagic,
toByronLovelace,
) where

import Cardano.Api.Typed
3 changes: 3 additions & 0 deletions cardano-api/src/Cardano/Api/Byron.hs
Expand Up @@ -81,9 +81,12 @@ module Cardano.Api.Byron
toByronLedgertoByronVote,

-- ** Conversions
fromByronTxIn,
toByronNetworkMagic,
toByronProtocolMagicId,
toByronRequiresNetworkMagic,

makeByronTransaction,
) where

import Cardano.Api
Expand Down
26 changes: 17 additions & 9 deletions cardano-api/src/Cardano/Api/Tx.hs
Expand Up @@ -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
Expand Down Expand Up @@ -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) =
Expand All @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions cardano-api/src/Cardano/Api/TxBody.hs
Expand Up @@ -86,6 +86,9 @@ module Cardano.Api.TxBody (

-- * Data family instances
AsType(AsTxId, AsTxBody, AsByronTxBody, AsShelleyTxBody),

-- * Conversion functions
fromByronTxIn,
) where

import Prelude
Expand Down Expand Up @@ -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)) =
Expand Down
4 changes: 4 additions & 0 deletions cardano-api/src/Cardano/Api/Typed.hs
Expand Up @@ -84,6 +84,8 @@ module Cardano.Api.Typed (
-- ** Byron addresses
makeByronAddress,
ByronKey,
ByronKeyLegacy,

-- ** Shelley addresses
makeShelleyAddress,
PaymentCredential(..),
Expand Down Expand Up @@ -475,6 +477,8 @@ module Cardano.Api.Typed (

-- ** Conversions
--TODO: arrange not to export these
fromByronTxIn,
toByronLovelace,
toByronNetworkMagic,
toByronProtocolMagicId,
toByronRequiresNetworkMagic,
Expand Down

0 comments on commit 9620a55

Please sign in to comment.