Skip to content

Commit

Permalink
Merge #2111
Browse files Browse the repository at this point in the history
2111: More API updates for the Allegra and Mary eras r=dcoutts a=dcoutts

Generalise most of the API tx construction functions over all the Shelley-based eras.

Co-authored-by: Duncan Coutts <duncan@well-typed.com>
  • Loading branch information
iohk-bors[bot] and dcoutts committed Nov 24, 2020
2 parents d5aaf0f + ee54b49 commit 58e7a04
Show file tree
Hide file tree
Showing 12 changed files with 907 additions and 373 deletions.
8 changes: 7 additions & 1 deletion cardano-api/src/Cardano/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,14 @@ module Cardano.API (
makeMIRCertificate,
makeGenesisKeyDelegationCertificate,

NetworkMagic,
-- * Protocol parameter updates
UpdateProposal(..),
ProtocolParametersUpdate(..),
makeShelleyUpdateProposal,
PraosNonce,
makePraosNonce,

NetworkMagic,
) where

import Cardano.Api.Typed
91 changes: 66 additions & 25 deletions cardano-api/src/Cardano/Api/Address.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ module Cardano.Api.Address (
toShelleyAddr,
toShelleyStakeAddr,
toShelleyStakeCredential,
fromShelleyAddr,
fromShelleyStakeAddr,
fromShelleyStakeCredential,

-- * Serialising addresses
SerialiseAddress(..),
Expand All @@ -67,7 +70,10 @@ import Control.Applicative

import qualified Cardano.Chain.Common as Byron

import qualified Cardano.Ledger.Era as Ledger
import Ouroboros.Consensus.Shelley.Eras (StandardShelley)
import Ouroboros.Consensus.Shelley.Protocol.Crypto (StandardCrypto)

import qualified Shelley.Spec.Ledger.Address as Shelley
import qualified Shelley.Spec.Ledger.BaseTypes as Shelley
import qualified Shelley.Spec.Ledger.Credential as Shelley
Expand Down Expand Up @@ -484,45 +490,80 @@ toShelleyAddr (AddressInEra (ShelleyAddressInEra _)
Shelley.Addr nw
(coerceShelleyPaymentCredential pc)
(coerceShelleyStakeReference scr)
where
-- The era parameter in these types is a phantom type so it is safe to cast.
-- We choose to cast because we need to use an era-independent address
-- representation, but have to produce an era-dependent format used by the
-- Shelley ledger lib.
coerceShelleyPaymentCredential :: Shelley.PaymentCredential eraA
-> Shelley.PaymentCredential eraB
coerceShelleyPaymentCredential = coerce

coerceShelleyStakeReference :: Shelley.StakeReference eraA
-> Shelley.StakeReference eraB
coerceShelleyStakeReference = coerce

toShelleyStakeAddr :: StakeAddress -> Shelley.RewardAcnt StandardShelley

toShelleyStakeAddr :: StakeAddress -> Shelley.RewardAcnt ledgerera
toShelleyStakeAddr (StakeAddress nw sc) =
Shelley.RewardAcnt {
Shelley.getRwdNetwork = nw,
Shelley.getRwdCred = sc
Shelley.getRwdCred = coerceShelleyStakeCredential sc
}

toShelleyPaymentCredential :: PaymentCredential
-> Shelley.PaymentCredential StandardShelley
toShelleyPaymentCredential :: Ledger.Crypto ledgerera ~ StandardCrypto
=> PaymentCredential
-> Shelley.PaymentCredential ledgerera
toShelleyPaymentCredential (PaymentCredentialByKey (PaymentKeyHash kh)) =
Shelley.KeyHashObj kh
toShelleyPaymentCredential (PaymentCredentialByScript (ScriptHash sh)) =
Shelley.ScriptHashObj sh
toShelleyPaymentCredential (PaymentCredentialByScript sh) =
Shelley.ScriptHashObj (toShelleyScriptHash sh)

toShelleyStakeCredential :: StakeCredential
-> Shelley.StakeCredential StandardShelley
toShelleyStakeCredential :: Ledger.Crypto ledgerera ~ StandardCrypto
=> StakeCredential
-> Shelley.StakeCredential ledgerera
toShelleyStakeCredential (StakeCredentialByKey (StakeKeyHash kh)) =
Shelley.KeyHashObj kh
toShelleyStakeCredential (StakeCredentialByScript (ScriptHash kh)) =
Shelley.ScriptHashObj kh
toShelleyStakeCredential (StakeCredentialByScript sh) =
Shelley.ScriptHashObj (toShelleyScriptHash sh)

toShelleyStakeReference :: StakeAddressReference
-> Shelley.StakeReference StandardShelley
toShelleyStakeReference :: Ledger.Crypto ledgerera ~ StandardCrypto
=> StakeAddressReference
-> Shelley.StakeReference ledgerera
toShelleyStakeReference (StakeAddressByValue stakecred) =
Shelley.StakeRefBase (toShelleyStakeCredential stakecred)
toShelleyStakeReference (StakeAddressByPointer ptr) =
Shelley.StakeRefPtr ptr
toShelleyStakeReference NoStakeAddress =
Shelley.StakeRefNull


fromShelleyAddr :: IsShelleyBasedEra era
=> Shelley.Addr (ShelleyLedgerEra era) -> AddressInEra era
fromShelleyAddr (Shelley.AddrBootstrap (Shelley.BootstrapAddress addr)) =
AddressInEra ByronAddressInAnyEra (ByronAddress addr)

fromShelleyAddr (Shelley.Addr nw pc scr) =
AddressInEra
(ShelleyAddressInEra shelleyBasedEra)
(ShelleyAddress
nw
(coerceShelleyPaymentCredential pc)
(coerceShelleyStakeReference scr))

fromShelleyStakeAddr :: Shelley.RewardAcnt ledgerera -> StakeAddress
fromShelleyStakeAddr (Shelley.RewardAcnt nw sc) =
StakeAddress nw (coerceShelleyStakeCredential sc)

fromShelleyStakeCredential :: Ledger.Crypto ledgerera ~ StandardCrypto
=> Shelley.StakeCredential ledgerera
-> StakeCredential
fromShelleyStakeCredential (Shelley.KeyHashObj kh) =
StakeCredentialByKey (StakeKeyHash kh)
fromShelleyStakeCredential (Shelley.ScriptHashObj sh) =
StakeCredentialByScript (fromShelleyScriptHash sh)


-- The era parameter in these types is a phantom type so it is safe to cast.
-- We choose to cast because we need to use an era-independent address
-- representation, but have to produce an era-dependent format used by the
-- Shelley ledger lib.
coerceShelleyPaymentCredential :: Shelley.PaymentCredential eraA
-> Shelley.PaymentCredential eraB
coerceShelleyPaymentCredential = coerce

coerceShelleyStakeCredential :: Shelley.StakeCredential eraA
-> Shelley.StakeCredential eraB
coerceShelleyStakeCredential = coerce

coerceShelleyStakeReference :: Shelley.StakeReference eraA
-> Shelley.StakeReference eraB
coerceShelleyStakeReference = coerce

Loading

0 comments on commit 58e7a04

Please sign in to comment.