Skip to content

Commit

Permalink
adding getSharedWallet impl and integration testing
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Apr 6, 2021
1 parent cd10d26 commit 07eebe9
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 23 deletions.
16 changes: 16 additions & 0 deletions lib/core-integration/src/Test/Integration/Framework/DSL.hs
Expand Up @@ -76,6 +76,7 @@ module Test.Integration.Framework.DSL
, rewardWallet
, postSharedWallet
, deleteSharedWallet
, getSharedWallet

-- * Wallet helpers
, listFilteredWallets
Expand Down Expand Up @@ -1374,6 +1375,21 @@ deleteSharedWallet ctx (ApiSharedWallet (Right w)) =
request @Aeson.Value ctx
(Link.deleteSharedWallet w) Default Empty

getSharedWallet
:: forall m.
( MonadIO m
, MonadUnliftIO m
)
=> Context
-> ApiSharedWallet
-> m (HTTP.Status, Either RequestException ApiSharedWallet)
getSharedWallet ctx (ApiSharedWallet (Left w)) = do
let link = Link.getSharedWallet w
request @ApiSharedWallet ctx link Default Empty
getSharedWallet ctx (ApiSharedWallet (Right w)) = do
let link = Link.getSharedWallet w
request @ApiSharedWallet ctx link Default Empty

fixtureRawTx
:: Context
-> (Address, Natural)
Expand Down
Expand Up @@ -26,10 +26,14 @@ import Cardano.Wallet.Primitive.AddressDerivation
( DerivationIndex (..), PaymentAddress )
import Cardano.Wallet.Primitive.AddressDerivation.Shelley
( ShelleyKey )
import Cardano.Wallet.Primitive.SyncProgress
( SyncProgress (..) )
import Control.Monad.IO.Class
( liftIO )
import Control.Monad.Trans.Resource
( runResourceT )
import Data.Bifunctor
( second )
import Data.Quantity
( Quantity (..) )
import Test.Hspec
Expand All @@ -42,11 +46,13 @@ import Test.Integration.Framework.DSL
, MnemonicLength (..)
, Payload (..)
, deleteSharedWallet
, eventually
, expectField
, expectResponseCode
, fixturePassphrase
, genMnemonics
, getFromResponse
, getSharedWallet
, json
, notDelegating
, postSharedWallet
Expand Down Expand Up @@ -83,7 +89,7 @@ spec = describe "SHARED_WALLETS" $ do
}
} |]
r <- postSharedWallet ctx Default payload
verify (fst r, (\(Right (ApiSharedWallet (Right res))) -> Right res) $ snd r)
verify (second (\(Right (ApiSharedWallet (Right res))) -> Right res) r)
[ expectResponseCode HTTP.status201
, expectField
(#name . #getApiT . #getWalletName) (`shouldBe` "Shared Wallet")
Expand Down Expand Up @@ -122,7 +128,7 @@ spec = describe "SHARED_WALLETS" $ do
}
} |]
r <- postSharedWallet ctx Default payload
verify (fst r, (\(Right (ApiSharedWallet (Left res))) -> Right res) $ snd r)
verify (second (\(Right (ApiSharedWallet (Left res))) -> Right res) r)
[ expectResponseCode HTTP.status201
, expectField
(#name . #getApiT . #getWalletName) (`shouldBe` "Shared Wallet")
Expand All @@ -149,7 +155,7 @@ spec = describe "SHARED_WALLETS" $ do
}
} |]
r <- postSharedWallet ctx Default payload
verify (fst r, (\(Right (ApiSharedWallet (Right res))) -> Right res) $ snd r)
verify (second (\(Right (ApiSharedWallet (Right res))) -> Right res) r)
[ expectResponseCode HTTP.status201
, expectField
(#name . #getApiT . #getWalletName) (`shouldBe` "Shared Wallet")
Expand Down Expand Up @@ -184,7 +190,7 @@ spec = describe "SHARED_WALLETS" $ do
}
} |]
r <- postSharedWallet ctx Default payload
verify (fst r, (\(Right (ApiSharedWallet (Left res))) -> Right res) $ snd r)
verify (second (\(Right (ApiSharedWallet (Left res))) -> Right res) r)
[ expectResponseCode HTTP.status201
, expectField
(#name . #getApiT . #getWalletName) (`shouldBe` "Shared Wallet")
Expand Down Expand Up @@ -215,5 +221,10 @@ spec = describe "SHARED_WALLETS" $ do
[ expectResponseCode HTTP.status201 ]
let wal = getFromResponse id rInit

eventually "Wallet state = Ready" $ do
r <- getSharedWallet ctx wal
verify (second (\(Right (ApiSharedWallet (Right res))) -> Right res) r)
[ expectField (#state . #getApiT) (`shouldBe` Ready) ]

rDel <- deleteSharedWallet ctx wal
expectResponseCode HTTP.status204 rDel
12 changes: 12 additions & 0 deletions lib/core/src/Cardano/Wallet/Api/Link.hs
Expand Up @@ -107,6 +107,7 @@ module Cardano.Wallet.Api.Link
-- * Shared Wallets
, postSharedWallet
, deleteSharedWallet
, getSharedWallet

, PostWallet
, Discriminate
Expand Down Expand Up @@ -698,6 +699,17 @@ deleteSharedWallet w =
where
wid = w ^. typed @(ApiT WalletId)

getSharedWallet
:: forall w.
( HasType (ApiT WalletId) w
)
=> w
-> (Method, Text)
getSharedWallet w =
endpoint @Api.GetSharedWallet (wid &)
where
wid = w ^. typed @(ApiT WalletId)

--
-- Internals
--
Expand Down
10 changes: 5 additions & 5 deletions lib/core/src/Cardano/Wallet/Api/Server.hs
Expand Up @@ -82,6 +82,7 @@ module Cardano.Wallet.Api.Server
, signMetadata
, postAccountPublicKey
, postSharedWallet
, mkSharedWallet

-- * Internals
, LiftHandler(..)
Expand Down Expand Up @@ -867,7 +868,7 @@ postSharedWalletFromRootXPrv ctx generateKey body = do
(\wrk -> W.manageRewardBalance @(WorkerCtx ctx) @s @k (Proxy @n) wrk wid)
withWorkerCtx @_ @s @k ctx wid liftE liftE $ \wrk -> liftHandler $
W.attachPrivateKeyFromPwd @_ @s @k wrk wid (rootXPrv, pwd)
fst <$> getWallet ctx (mkSharedWallet @_ @s @k state) (ApiT wid)
fst <$> getWallet ctx (mkSharedWallet @_ @s @k) (ApiT wid)
where
seed = getApiMnemonicT (body ^. #mnemonicSentence)
secondFactor = getApiMnemonicT <$> (body ^. #mnemonicSecondFactor)
Expand Down Expand Up @@ -902,7 +903,7 @@ postSharedWalletFromAccountXPub ctx liftKey body = do
(\wrk -> W.createWallet @(WorkerCtx ctx) @s @k wrk wid wName state)
(\wrk -> W.restoreWallet @(WorkerCtx ctx) @s @k wrk wid)
(`idleWorker` wid)
fst <$> getWallet ctx (mkSharedWallet @_ @s @k state) (ApiT wid)
fst <$> getWallet ctx (mkSharedWallet @_ @s @k) (ApiT wid)
where
g = defaultAddressPoolGap
accIx = Index $ getDerivationIndex $ getApiT (body ^. #accountIndex)
Expand All @@ -919,9 +920,8 @@ mkSharedWallet
, s ~ SharedState n k
, HasWorkerRegistry s k ctx
)
=> SharedState n k
-> MkApiWallet ctx s ApiSharedWallet
mkSharedWallet sharedState ctx wid cp meta pending progress = case sharedState of
=> MkApiWallet ctx s ApiSharedWallet
mkSharedWallet ctx wid cp meta pending progress = case getState cp of
PendingSharedState (DerivationPrefix (_,_,accIx)) _ pTemplate dTemplateM g ->
pure $ ApiSharedWallet $ Left $ ApiPendingSharedWallet
{ id = ApiT wid
Expand Down
4 changes: 3 additions & 1 deletion lib/core/src/Cardano/Wallet/DB/Sqlite.hs
Expand Up @@ -162,6 +162,8 @@ import Control.Monad.Trans.Maybe
( MaybeT (..) )
import Control.Tracer
( Tracer, contramap, traceWith )
import Data.Bifunctor
( second )
import Data.Coerce
( coerce )
import Data.Either
Expand Down Expand Up @@ -2470,7 +2472,7 @@ instance
let SharedState _ accountBytes g pScript dScriptM prefix = entityVal st
let accXPub = unsafeDeserializeXPub accountBytes
pCosigners <- lift $ selectCosigners @k wid Payment
let prepareKeys = map (\(c,k) -> (c,getRawKey k))
let prepareKeys = map (second getRawKey)
let pTemplate = ScriptTemplate (Map.fromList $ prepareKeys pCosigners) pScript
dCosigners <- lift $ selectCosigners @k wid Delegation
let dTemplateM = ScriptTemplate (Map.fromList $ prepareKeys dCosigners) <$> dScriptM
Expand Down
4 changes: 2 additions & 2 deletions lib/core/src/Cardano/Wallet/DB/Sqlite/Types.hs
Expand Up @@ -30,12 +30,12 @@ import Cardano.Api.Typed
, metadataFromJson
, metadataToJson
)
import Cardano.Wallet.Primitive.AddressDiscovery.Script
( CredentialType )
import Cardano.Slotting.Slot
( SlotNo (..) )
import Cardano.Wallet.Primitive.AddressDerivation
( Passphrase (..), PassphraseScheme (..), Role (..) )
import Cardano.Wallet.Primitive.AddressDiscovery.Script
( CredentialType )
import Cardano.Wallet.Primitive.AddressDiscovery.Sequential
( AddressPoolGap (..)
, DerivationPrefix
Expand Down
Expand Up @@ -412,7 +412,7 @@ instance PersistPublicKey (ByronKey 'AccountK) where
hex (unXPub k) <> ":" <> hex p

unsafeDeserializeXPub kp =
either err id $ fmap mkKey (deserializeKey kp)
either err (id . mkKey) (deserializeKey kp)
where
err _ = error "unsafeDeserializeXPub: unable to deserialize ByronKey"
mkKey (key, pwd) = ByronKey key (Index 0) pwd
Expand Down
@@ -1,6 +1,5 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
Expand Down
2 changes: 1 addition & 1 deletion lib/core/test/unit/Cardano/Wallet/DB/Arbitrary.hs
Expand Up @@ -587,7 +587,7 @@ rootKeysRnd = unsafePerformIO $ generate (vectorOf 10 genRootKeysRnd)

instance Arbitrary (SharedState 'Mainnet ShelleyKey) where
shrink (SharedState prefix pool) =
(\p -> SharedState prefix p) <$> shrink pool
SharedState prefix <$> shrink pool
shrink _ = []
arbitrary = do
let activeWallet = SharedState
Expand Down
15 changes: 9 additions & 6 deletions lib/core/test/unit/Cardano/Wallet/Gen.hs
Expand Up @@ -42,9 +42,12 @@ import Cardano.Api.Typed
, metadataFromJson
)
import Cardano.Mnemonic
( SomeMnemonic (..) )
import Cardano.Mnemonic
( ConsistentEntropy, EntropySize, Mnemonic, entropyToMnemonic )
( ConsistentEntropy
, EntropySize
, Mnemonic
, SomeMnemonic (..)
, entropyToMnemonic
)
import Cardano.Wallet.Primitive.AddressDerivation
( Passphrase (..), WalletKey (..) )
import Cardano.Wallet.Primitive.AddressDerivation.Shelley
Expand Down Expand Up @@ -314,15 +317,15 @@ genScriptCosigners = do

genScriptTemplate :: Gen ScriptTemplate
genScriptTemplate = do
script <- genScriptCosigners `suchThat` (\s -> not (null (retrieveAllCosigners s)))
script <- genScriptCosigners `suchThat` (not . null . retrieveAllCosigners)
let scriptCosigners = retrieveAllCosigners script
cosignersSubset <- sublistOf scriptCosigners `suchThat` (\cs -> not (null cs))
cosignersSubset <- sublistOf scriptCosigners `suchThat` (not . null)
xpubs <- vectorOf (length cosignersSubset) genXPub
pure $ ScriptTemplate (Map.fromList $ zip cosignersSubset xpubs) script

genScriptTemplateComplete :: Gen ScriptTemplate
genScriptTemplateComplete = do
script <- genScriptCosigners `suchThat` (\s -> not (null (retrieveAllCosigners s)))
script <- genScriptCosigners `suchThat` (not . null . retrieveAllCosigners)
let scriptCosigners = retrieveAllCosigners script
xpubs <- vectorOf (length scriptCosigners) genXPub
pure $ ScriptTemplate (Map.fromList $ zip scriptCosigners xpubs) script
Expand Down
4 changes: 2 additions & 2 deletions lib/shelley/src/Cardano/Wallet/Shelley/Api/Server.hs
Expand Up @@ -83,6 +83,7 @@ import Cardano.Wallet.Api.Server
, listWallets
, migrateWallet
, mkLegacyWallet
, mkSharedWallet
, mkShelleyWallet
, postAccountPublicKey
, postAccountWallet
Expand Down Expand Up @@ -495,12 +496,11 @@ server byron icarus shelley multisig spl ntp =
sharedWallets :: Server SharedWallets
sharedWallets =
postSharedWallet multisig generateKeyFromSeed ShelleyKey
:<|> getSharedWallet
:<|> (fmap fst . getWallet multisig mkSharedWallet)
:<|> patchSharedWalletInPayment
:<|> patchSharedWalletInDelegation
:<|> deleteWallet multisig
where
getSharedWallet = pure $ throwError err501
patchSharedWalletInPayment _ = pure $ throwError err501
patchSharedWalletInDelegation _ = pure $ throwError err501

Expand Down

0 comments on commit 07eebe9

Please sign in to comment.