Skip to content

Commit

Permalink
Split off Cardano.Wallet.Deposit.Write.Keys
Browse files Browse the repository at this point in the history
  • Loading branch information
HeinrichApfelmus committed Nov 27, 2024
1 parent f1f8390 commit 39897fd
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 37 deletions.
1 change: 1 addition & 0 deletions lib/customer-deposit-wallet/customer-deposit-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ library
Cardano.Wallet.Deposit.Testing.DSL.Types
Cardano.Wallet.Deposit.Time
Cardano.Wallet.Deposit.Write
Cardano.Wallet.Deposit.Write.Keys

test-suite scenario
import: language, opts-exe
Expand Down
41 changes: 4 additions & 37 deletions lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Write.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,11 @@ module Cardano.Wallet.Deposit.Write

import Prelude

import Cardano.Crypto.Wallet
( xpubPublicKey
)
import Cardano.Ledger.Keys
( SignedDSIGN
, VKey (..)
)
import Cardano.Read.Ledger.Tx.Output
( Output (..)
)
import Cardano.Wallet.Address.BIP32_Ed25519
( XPrv
, XPub
, XSignature
, rawSerialiseXSignature
, sign
, toXPub
)
Expand All @@ -80,6 +70,10 @@ import Cardano.Wallet.Deposit.Read
import Cardano.Wallet.Read.Tx
( toConwayOutput
)
import Cardano.Wallet.Deposit.Write.Keys
( vkeyFromXPub
, signedDSIGNfromXSignature
)
import Control.Lens
( Lens'
, lens
Expand All @@ -90,9 +84,6 @@ import Control.Lens
import Data.Map
( Map
)
import Data.Maybe
( fromMaybe
)
import Data.Maybe.Strict
( StrictMaybe (..)
, maybeToStrictMaybe
Expand All @@ -105,7 +96,6 @@ import Data.Set
( Set
)

import qualified Cardano.Crypto.DSIGN as DSIGN
import qualified Cardano.Ledger.Api as L
import qualified Cardano.Ledger.Api.Tx.In as L
import qualified Cardano.Ledger.Slot as L
Expand Down Expand Up @@ -139,29 +129,6 @@ addAddressWitness xprv tx@(Read.Tx ledgerTx) =
witnessVKey =
L.WitVKey (vkeyFromXPub xpub) (signedDSIGNfromXSignature xsign)

-- | Convert 'XPub' to a type that 'Cardano.Ledger' accepts.
vkeyFromXPub :: XPub -> VKey 'L.Witness L.StandardCrypto
vkeyFromXPub =
VKey
. fromMaybe impossible
. DSIGN.rawDeserialiseVerKeyDSIGN
. xpubPublicKey
where
impossible = error "impossible: Cannot convert XPub to VKey"

-- | Convert 'XSignature' to a type that 'Cardano.Ledger' accepts.
signedDSIGNfromXSignature
:: XSignature
-> SignedDSIGN L.StandardCrypto
(Hash.Hash Hash.Blake2b_256 Read.EraIndependentTxBody)
signedDSIGNfromXSignature =
DSIGN.SignedDSIGN
. fromMaybe impossible
. DSIGN.rawDeserialiseSigDSIGN
. rawSerialiseXSignature
where
impossible = error "impossible: Cannot convert XSignature to SignedDSIGN"

{-----------------------------------------------------------------------------
Convenience TxBody
------------------------------------------------------------------------------}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{-# LANGUAGE DataKinds #-}

-- | Module for converting key types from
-- @Cardano.Ledger@ with key types from @Cardano.Crypto.Wallet@.
--
-- TODO: Match this up with the @Write@ hierarchy.
module Cardano.Wallet.Deposit.Write.Keys
( enterpriseAddressFromVKey
, vkeyFromXPub
, signedDSIGNfromXSignature
) where

import Prelude

import Cardano.Crypto.Wallet
( xpubPublicKey
)
import Cardano.Ledger.Keys
( SignedDSIGN
, VKey (..)
)
import Cardano.Wallet.Address.BIP32_Ed25519
( XPub
, XSignature
, rawSerialiseXSignature
)
import Cardano.Wallet.Deposit.Read
( Address
)
import Data.Maybe
( fromMaybe
)

import qualified Cardano.Crypto.DSIGN as DSIGN
import qualified Cardano.Ledger.Address as L
import qualified Cardano.Ledger.Api as L
import qualified Cardano.Ledger.BaseTypes as L
import qualified Cardano.Ledger.Credential as L
import qualified Cardano.Ledger.Hashes as L
import qualified Cardano.Ledger.Keys as L

{-----------------------------------------------------------------------------
Key conversion
------------------------------------------------------------------------------}
-- | Create an enterprise address from a ledger 'VKey'.
enterpriseAddressFromVKey
:: L.Network
-> VKey 'L.Witness L.StandardCrypto
-> Address
enterpriseAddressFromVKey network =
mkEnterpriseAddress
. L.coerceKeyRole
. L.hashKey
where
mkEnterpriseAddress h =
L.compactAddr
$ L.Addr network (L.KeyHashObj h) L.StakeRefNull

-- | Convert 'XPub' to a ledger verification key.
vkeyFromXPub :: XPub -> VKey 'L.Witness L.StandardCrypto
vkeyFromXPub =
VKey
. fromMaybe impossible
. DSIGN.rawDeserialiseVerKeyDSIGN
. xpubPublicKey
where
impossible = error "impossible: Cannot convert XPub to VKey"

-- | Convert 'XSignature' to a ledger signature.
signedDSIGNfromXSignature
:: XSignature
-> SignedDSIGN L.StandardCrypto
(L.Hash L.StandardCrypto L.EraIndependentTxBody)
signedDSIGNfromXSignature =
DSIGN.SignedDSIGN
. fromMaybe impossible
. DSIGN.rawDeserialiseSigDSIGN
. rawSerialiseXSignature
where
impossible = error "impossible: Cannot convert XSignature to SignedDSIGN"

0 comments on commit 39897fd

Please sign in to comment.