Skip to content

Commit

Permalink
Fill in network magic in Alonzo tx body construction
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimbo4350 authored and newhoggy committed May 27, 2021
1 parent aab18d4 commit 03240fe
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
8 changes: 7 additions & 1 deletion cardano-api/src/Cardano/Api/Address.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module Cardano.Api.Address (
StakeExtendedKey,

-- * Internal conversion functions
getNetworkId,
toShelleyAddr,
toShelleyStakeAddr,
toShelleyStakeCredential,
Expand Down Expand Up @@ -81,8 +82,8 @@ import qualified Shelley.Spec.Ledger.BaseTypes as Shelley
import qualified Shelley.Spec.Ledger.Credential as Shelley

import Cardano.Api.Eras
import Cardano.Api.Hash
import Cardano.Api.HasTypeProxy
import Cardano.Api.Hash
import Cardano.Api.Key
import Cardano.Api.KeysByron
import Cardano.Api.KeysShelley
Expand Down Expand Up @@ -490,6 +491,11 @@ makeStakeAddress nw sc =
-- Internal conversion functions
--

getNetworkId :: Address addrtype -> Shelley.Network
getNetworkId (ByronAddress addr) =
Shelley.getNetwork . Shelley.AddrBootstrap $ Shelley.BootstrapAddress addr
getNetworkId (ShelleyAddress nid _ _ ) = nid

toShelleyAddr :: AddressInEra era -> Shelley.Addr StandardCrypto
toShelleyAddr (AddressInEra ByronAddressInAnyEra (ByronAddress addr)) =
Shelley.AddrBootstrap (Shelley.BootstrapAddress addr)
Expand Down
9 changes: 5 additions & 4 deletions cardano-api/src/Cardano/Api/NetworkId.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import Prelude

import Ouroboros.Network.Magic (NetworkMagic (..))

import qualified Cardano.Crypto.ProtocolMagic as Byron
(ProtocolMagicId(..), RequiresNetworkMagic(..))
import qualified Cardano.Chain.Common as Byron (NetworkMagic(..))
import qualified Cardano.Chain.Common as Byron (NetworkMagic (..))
import qualified Cardano.Chain.Genesis as Byron (mainnetProtocolMagicId)
import qualified Cardano.Crypto.ProtocolMagic as Byron (ProtocolMagicId (..),
RequiresNetworkMagic (..))

import qualified Shelley.Spec.Ledger.BaseTypes as Shelley (Network(..))
import qualified Shelley.Spec.Ledger.BaseTypes as Shelley (Network (..))


-- ----------------------------------------------------------------------------
Expand Down Expand Up @@ -76,3 +76,4 @@ fromShelleyNetwork Shelley.Mainnet nm
| nm == mainnetNetworkMagic = Mainnet
| otherwise = error "fromShelleyNetwork Mainnet: wrong mainnet network magic"


10 changes: 9 additions & 1 deletion cardano-api/src/Cardano/Api/TxBody.hs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ import qualified Cardano.Ledger.Alonzo.Tx as Alonzo
import qualified Cardano.Ledger.Alonzo.TxWitness as Alonzo
import qualified Shelley.Spec.Ledger.Address as Shelley
import Shelley.Spec.Ledger.BaseTypes (StrictMaybe (..), maybeToStrictMaybe)
import qualified Shelley.Spec.Ledger.BaseTypes as Shelley
import qualified Shelley.Spec.Ledger.Credential as Shelley
import qualified Shelley.Spec.Ledger.Genesis as Shelley
import qualified Shelley.Spec.Ledger.Keys as Shelley
Expand Down Expand Up @@ -1922,11 +1923,18 @@ makeShelleyTransactionBody era@ShelleyBasedEraAlonzo
redeemerPtrs)
(maybeToStrictMaybe
(Ledger.hashAuxiliaryData @StandardAlonzo <$> txAuxData))
(error "TODO alonzo: optional network"))
(maybeToStrictMaybe $ networkId txOuts))
(map toShelleySimpleScript (collectTxBodySimpleScripts txbodycontent))
txAuxData
redeemerPtrs
where
-- Necessary for hardware wallets constructing tx bodies.
networkId :: [TxOut era] -> Maybe Shelley.Network
networkId txouts =
case map (\(TxOut (AddressInEra _ addr) _ _) -> getNetworkId addr) txouts of
nid : _ -> Just nid
[] -> Nothing

redeemerPtrs :: Map Alonzo.RdmrPtr (Alonzo.Data ledgerera, Alonzo.ExUnits)
redeemerPtrs =
Map.fromList $ concat [ certifyingRedeemerPtr txCertificates
Expand Down

0 comments on commit 03240fe

Please sign in to comment.