Skip to content

Commit

Permalink
extend DLLayer interface for shared wallet - part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Apr 15, 2021
1 parent 25c5a5d commit db53ccc
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 12 deletions.
4 changes: 2 additions & 2 deletions lib/core/cardano-wallet-core.cabal
Expand Up @@ -146,7 +146,7 @@ library
Cardano.SharedWallet.DB
Cardano.SharedWallet.DB.Sqlite.TH
Cardano.SharedWallet.Script
Cardano.SharedWallet.Shared
Cardano.SharedWallet.SharedState
Cardano.Wallet
Cardano.Wallet.Api
Cardano.Wallet.Api.Client
Expand Down Expand Up @@ -333,7 +333,7 @@ test-suite unit
Cardano.Pool.DB.MVarSpec
Cardano.Pool.DB.Properties
Cardano.Pool.DB.SqliteSpec
Cardano.SharedWallet.SharedSpec
Cardano.SharedWallet.SharedStateSpec
Cardano.Wallet.Api.Malformed
Cardano.Wallet.Api.Server.TlsSpec
Cardano.Wallet.Api.ServerSpec
Expand Down
27 changes: 26 additions & 1 deletion lib/core/src/Cardano/SharedWallet/DB.hs
Expand Up @@ -17,11 +17,16 @@ module Cardano.SharedWallet.DB
-- * Errors
, ErrNoSuchSharedWallet (..)
, ErrSharedWalletAlreadyExists (..)
, ErrAddCosignerKey (..)
) where

import Prelude

import Cardano.SharedWallet.Shared
import Cardano.Address.Script
( Cosigner )
import Cardano.SharedWallet.Script
( CosignerInfo )
import Cardano.SharedWallet.SharedState
( SharedWallet )
import Cardano.Wallet.Primitive.Types
( GenesisParameters, WalletId, WalletMetadata )
Expand All @@ -31,6 +36,8 @@ import Control.Monad.IO.Class
( MonadIO )
import Control.Monad.Trans.Except
( ExceptT )
import Data.Time.Clock
( UTCTime )

-- | A Database interface for storing shared wallet state in DB.
--
Expand Down Expand Up @@ -72,6 +79,18 @@ data DBLayer m k = forall stm. (MonadFail stm, MonadIO stm) => DBLayer
--
-- Return 'Nothing' if there's no such wallet.

, addCosignerKey
:: WalletId
-> UTCTime
-> CosignerInfo k
-> ExceptT ErrAddCosignerKey stm ()
-- ^ Adding cosigner key to the shared wallet.

, listCosignerKeys
:: WalletId
-> stm [(UTCTime, CosignerInfo k)]
-- ^ Get the list of keys all known cosigners in the DB for a given shared wallet, possibly empty.

, cleanDB
:: stm ()
-- ^ Clean a database
Expand All @@ -92,3 +111,9 @@ newtype ErrNoSuchSharedWallet
newtype ErrSharedWalletAlreadyExists
= ErrSharedWalletAlreadyExists WalletId -- Wallet already exists in db
deriving (Eq, Show)

-- | Forbidden operation was executed when adding the account key of cosigner
data ErrAddCosignerKey
= ErrAddCosignerKeyNoWallet ErrNoSuchSharedWallet
| ErrAddCosignerKeyNoCosigner Cosigner
deriving (Eq, Show)
2 changes: 1 addition & 1 deletion lib/core/src/Cardano/SharedWallet/DB/Sqlite/TH.hs
Expand Up @@ -24,7 +24,7 @@ import Cardano.Address.Script
( Cosigner, Script )
import Cardano.SharedWallet.Script
( CredentialType )
import Cardano.SharedWallet.Shared
import Cardano.SharedWallet.SharedState
( SharedWalletState )
import Cardano.Wallet.DB.Sqlite.Types
( BlockId, sqlSettings' )
Expand Down
30 changes: 29 additions & 1 deletion lib/core/src/Cardano/SharedWallet/Script.hs
@@ -1,11 +1,14 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
Expand All @@ -22,6 +25,8 @@
module Cardano.SharedWallet.Script
(
CredentialType (..)
, CosignerInfo (..)

, keyHashFromAccXPubIx
, constructAddressFromIx
, toNetworkTag
Expand Down Expand Up @@ -67,6 +72,8 @@ import Cardano.Wallet.Primitive.Types
( invariant )
import Cardano.Wallet.Primitive.Types.Address
( Address (..) )
import Control.DeepSeq
( NFData )
import Data.Either.Combinators
( fromRight', rightToMaybe )
import Data.Kind
Expand All @@ -78,6 +85,8 @@ import Data.Text.Class
( FromText (..), TextDecodingError (..), ToText (..) )
import Data.Type.Equality
( (:~:) (..), testEquality )
import GHC.Generics
( Generic )
import Type.Reflection
( Typeable, typeRep )

Expand All @@ -87,7 +96,8 @@ import qualified Cardano.Address.Style.Shelley as CA
import qualified Data.Map.Strict as Map

data CredentialType = Payment | Delegation
deriving (Eq, Show)
deriving (Eq, Show, Generic)
deriving anyclass NFData

instance ToText CredentialType where
toText Payment = "payment"
Expand All @@ -102,6 +112,24 @@ instance FromText CredentialType where
, "'payment', 'delegation'."
]

data CosignerInfo k = CosignerInfo
{ cosignerAccountKey :: !(k 'AccountK XPub)
, cosigner :: !Cosigner
, credential :: !CredentialType
} deriving (Generic)

deriving instance
( Show (k 'AccountK XPub)
) => Show (CosignerInfo k)

deriving instance
( Eq (k 'AccountK XPub)
) => Eq (CosignerInfo k)

instance
( NFData (k 'AccountK XPub)
) => NFData (CosignerInfo k)

keyHashFromAccXPubIx
:: (SoftDerivation k, WalletKey k)
=> k 'AccountK XPub
Expand Down
Expand Up @@ -22,12 +22,12 @@
-- An implementation of shared script state using
-- scheme specified in CIP-1854 Multi-signature Wallets.

module Cardano.SharedWallet.Shared
module Cardano.SharedWallet.SharedState
(
-- ** State
SharedState (..)
, SharedWalletState (..)
, SharedWallet (..)
, SharedWalletState (..)
, unsafePendingSharedState
, newSharedState
, addCosignerAccXPub
Expand Down Expand Up @@ -197,7 +197,7 @@ instance FromText SharedWalletState where

data SharedWallet k = SharedWallet
{ walletState :: !SharedWalletState
, accountKey :: !(k 'AccountK XPub)
, walletAccountKey :: !(k 'AccountK XPub)
, accountIx :: !(Index 'Hardened 'AccountK)
, paymentScript :: !(Script Cosigner)
, delegationScript :: !(Script Cosigner)
Expand Down
2 changes: 1 addition & 1 deletion lib/core/src/Cardano/Wallet/DB/Sqlite/Types.hs
Expand Up @@ -32,7 +32,7 @@ import Cardano.Api.Typed
)
import Cardano.SharedWallet.Script
( CredentialType )
import Cardano.SharedWallet.Shared
import Cardano.SharedWallet.SharedState
( SharedWalletState )
import Cardano.Slotting.Slot
( SlotNo (..) )
Expand Down
Expand Up @@ -9,7 +9,7 @@

{-# OPTIONS_GHC -fno-warn-orphans #-}

module Cardano.SharedWallet.SharedSpec
module Cardano.SharedWallet.SharedStateSpec
( spec
) where

Expand All @@ -30,7 +30,7 @@ import Cardano.SharedWallet.Script
, liftDelegationAddress
, liftPaymentAddress
)
import Cardano.SharedWallet.Shared
import Cardano.SharedWallet.SharedState
( SharedState (..), isShared, newSharedState )
import Cardano.Wallet.Gen
( genNatural, genScript )
Expand Down
2 changes: 1 addition & 1 deletion lib/core/test/unit/Cardano/Wallet/Api/TypesSpec.hs
Expand Up @@ -47,7 +47,7 @@ import Cardano.Mnemonic
, entropyToMnemonic
, mkEntropy
)
import Cardano.SharedWallet.Shared
import Cardano.SharedWallet.SharedState
( retrieveAllCosigners )
import Cardano.Wallet.Api
( Api )
Expand Down

0 comments on commit db53ccc

Please sign in to comment.