Skip to content

Commit

Permalink
add addCosignerKey impl of DBLayer
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Apr 8, 2021
1 parent 9fdf947 commit b6e4017
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/core/src/Cardano/SharedWallet/DB.hs
Expand Up @@ -25,7 +25,7 @@ import Prelude
import Cardano.Address.Script
( Cosigner )
import Cardano.SharedWallet.Script
( CosignerInfo )
( CosignerInfo, CredentialType )
import Cardano.SharedWallet.SharedState
( SharedWalletInfo )
import Cardano.Wallet.Primitive.Types
Expand Down Expand Up @@ -115,5 +115,5 @@ newtype ErrSharedWalletAlreadyExists
-- | Forbidden operation was executed when adding the account key of cosigner
data ErrAddCosignerKey
= ErrAddCosignerKeyNoWallet ErrNoSuchSharedWallet
| ErrAddCosignerKeyNoCosigner Cosigner
| ErrAddCosignerKeyAlreadyExists WalletId Cosigner CredentialType
deriving (Eq, Show)
27 changes: 22 additions & 5 deletions lib/core/src/Cardano/SharedWallet/DB/Log.hs
Expand Up @@ -13,23 +13,30 @@ module Cardano.SharedWallet.DB.Log

import Prelude

import Cardano.Address.Script
( Cosigner (..) )
import Cardano.BM.Data.Severity
( Severity (..) )
import Cardano.BM.Data.Tracer
( HasPrivacyAnnotation (..), HasSeverityAnnotation (..) )
import Cardano.DB.Sqlite
( DBLog (..) )
import Cardano.SharedWallet.Script
( CredentialType )
import Cardano.Wallet.Primitive.Types
( WalletId )
import Data.Text
( Text )
import Data.Text.Class
( ToText (..), toText )

import qualified Data.Text as T

data SharedWalletDbLog
= MsgGeneric DBLog
| MsgParseFailure ParseFailure
| MsgCreatingSharedWallet WalletId
| MsgAddingCosigner WalletId Cosigner CredentialType
| MsgRemovingSharedWallet WalletId
deriving (Eq, Show)

Expand All @@ -51,6 +58,7 @@ instance HasSeverityAnnotation SharedWalletDbLog where
MsgParseFailure {} -> Error
MsgRemovingSharedWallet {} -> Notice
MsgCreatingSharedWallet {} -> Notice
MsgAddingCosigner {} -> Notice

instance ToText SharedWalletDbLog where
toText = \case
Expand All @@ -61,13 +69,22 @@ instance ToText SharedWalletDbLog where
, "'. Description of error: "
, parseFailure e
]
MsgRemovingSharedWallet p -> mconcat
MsgRemovingSharedWallet wid -> mconcat
[ "Removing the following shared wallet from the database: "
, toText p
, toText wid
, "."
]
MsgCreatingSharedWallet wid -> mconcat
[ "Creating the following shared wallet in the database: "
, toText wid
, "."
]
MsgCreatingSharedWallet p -> mconcat
[ "Creating the following pool in the database: "
, toText p
MsgAddingCosigner wid (Cosigner c) cred -> mconcat
[ "Adding the account public key for the cosigner#"
, T.pack (show c)
, " for "
, toText cred
, " credential for the following shared wallet in the database: "
, toText wid
, "."
]
36 changes: 33 additions & 3 deletions lib/core/src/Cardano/SharedWallet/DB/Sqlite.hs
Expand Up @@ -30,6 +30,8 @@ module Cardano.SharedWallet.DB.Sqlite

import Prelude

import Cardano.Address.Script
( Cosigner (..) )
import Cardano.DB.Sqlite
( DBLog (..)
, MigrationError
Expand All @@ -41,13 +43,16 @@ import Cardano.DB.Sqlite
)
import Cardano.SharedWallet.DB
( DBLayer (..)
, ErrAddCosignerKey (..)
, ErrNoSuchSharedWallet (..)
, ErrSharedWalletAlreadyExists (..)
)
import Cardano.SharedWallet.DB.Log
( SharedWalletDbLog (..) )
import Cardano.SharedWallet.DB.Sqlite.TH
( EntityField (..), SharedWallet (..), migrateAll )
( CosignerKey (..), EntityField (..), SharedWallet (..), migrateAll )
import Cardano.SharedWallet.Script
( CosignerInfo (..) )
import Cardano.SharedWallet.SharedState
( SharedWalletInfo (..) )
import Cardano.Wallet.DB.Sqlite.Types
Expand All @@ -66,6 +71,8 @@ import Data.Function
( (&) )
import Data.Generics.Internal.VL.Lens
( (^.) )
import Data.Time.Clock
( UTCTime )
import Database.Persist.Sql
( Entity (..), deleteWhere, insert_, selectFirst, (==.) )
import Database.Persist.Sqlite
Expand Down Expand Up @@ -151,6 +158,7 @@ newDBLayer tr SqliteContext{runQuery} =
Just _ -> Right <$> do
liftIO $ traceWith tr $ MsgRemovingSharedWallet wid
deleteWhere [SharedWalletWalletId ==. wid]
deleteWhere [CosignerKeyWalletId ==. wid]

readSharedWalletState wid = do
selectSharedWallet wid >>= \case
Expand All @@ -162,7 +170,14 @@ newDBLayer tr SqliteContext{runQuery} =
Nothing -> pure Nothing
Just _ -> selectSharedWalletMetadata wid

addCosignerKey _walId _utctime _cosignerInfo = undefined
addCosignerKey wid utctime info = ExceptT $ do
selectSharedWallet wid >>= \case
Nothing -> pure $ Left $ ErrAddCosignerKeyNoWallet $ ErrNoSuchSharedWallet wid
Just _ -> do
res <- handleConstraint (ErrAddCosignerKeyAlreadyExists wid (cosigner info) (credential info)) $
insert_ (mkCosignerKeyEntity wid utctime info)
liftIO $ traceWith tr $ MsgAddingCosigner wid (cosigner info) (credential info)
pure res

listCosignerKeys _walId = undefined

Expand Down Expand Up @@ -204,7 +219,6 @@ mkSharedWalletEntity
mkSharedWalletEntity wid state meta gp = SharedWallet
{ sharedWalletWalletId = wid
, sharedWalletCreationTime = meta ^. #creationTime
, sharedWalletUpdateTime = Nothing
, sharedWalletName = meta ^. #name . coerce
, sharedWalletAccountXPub = serializeXPub (state ^. #walletAccountKey)
, sharedWalletAccountIndex = getIndex (state ^. #accountIx)
Expand Down Expand Up @@ -259,3 +273,19 @@ stateFromEntity wal = SharedWalletInfo
, delegationScript = sharedWalletDelegationScript wal
, poolGap = sharedWalletScriptGap wal
}

mkCosignerKeyEntity
:: PersistPublicKey (k 'AccountK)
=> W.WalletId
-> UTCTime
-> CosignerInfo k
-> CosignerKey
mkCosignerKeyEntity wid utctime info = CosignerKey
{ cosignerKeyWalletId = wid
, cosignerKeyCreationTime = utctime
, cosignerKeyCredential = credential info
, cosignerKeyAccountXPub = serializeXPub (cosignerAccountKey info)
, cosignerKeyIndex = c
}
where
(Cosigner c) = cosigner info
1 change: 0 additions & 1 deletion lib/core/src/Cardano/SharedWallet/DB/Sqlite/TH.hs
Expand Up @@ -56,7 +56,6 @@ share
SharedWallet
sharedWalletWalletId W.WalletId sql=wallet_id
sharedWalletCreationTime UTCTime sql=creation_time
sharedWalletUpdateTime UTCTime Maybe sql=update_time
sharedWalletName Text sql=name
sharedWalletAccountXPub B8.ByteString sql=account_xpub
sharedWalletAccountIndex Word32 sql=account_ix
Expand Down

0 comments on commit b6e4017

Please sign in to comment.