Skip to content

Commit

Permalink
Make passphase info a 'Maybe', and correctly set them within 'attachP…
Browse files Browse the repository at this point in the history
…rivateKey'
  • Loading branch information
KtorZ committed May 10, 2019
1 parent bb4395c commit 64c94cc
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 28 deletions.
22 changes: 9 additions & 13 deletions lib/core/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ import Control.Monad.Trans.Maybe
( MaybeT (..), maybeToExceptT )
import Control.Monad.Trans.State
( runState, state )
import Data.Bifunctor
( bimap )
import Data.Coerce
( coerce )
import Data.Functor
Expand Down Expand Up @@ -286,10 +284,9 @@ mkWalletLayer db nw tl = WalletLayer

{ createWallet = \wid wname s -> do
let checkpoint = initWallet s
now <- liftIO getCurrentTime
let metadata = WalletMetadata
{ name = wname
, passphraseInfo = WalletPassphraseInfo now
, passphraseInfo = Nothing
, status = Restoring minBound
, delegation = NotDelegating
}
Expand All @@ -301,16 +298,10 @@ mkWalletLayer db nw tl = WalletLayer
meta <- _readWalletMeta wid
DB.putWalletMeta db (PrimaryKey wid) (modify meta)

, updateWalletPassphrase = \wid pwds -> DB.withLock db $ do
meta <- withExceptT ErrUpdatePassphraseNoSuchWallet $ _readWalletMeta wid
let (old, new) = bimap coerce coerce pwds
withRootKey wid old ErrUpdatePassphraseWithRootKey $ \xprv ->
, updateWalletPassphrase = \wid (old, new) -> do
withRootKey wid (coerce old) ErrUpdatePassphraseWithRootKey $ \xprv ->
withExceptT ErrUpdatePassphraseNoSuchWallet $
_attachPrivateKey wid (xprv, new)
now <- liftIO getCurrentTime
let modify x = x { passphraseInfo = WalletPassphraseInfo now }
withExceptT ErrUpdatePassphraseNoSuchWallet $
DB.putWalletMeta db (PrimaryKey wid) (modify meta)
_attachPrivateKey wid (xprv, coerce new)

, listWallets = fmap (\(PrimaryKey wid) -> wid) <$> DB.listWallets db

Expand Down Expand Up @@ -412,6 +403,11 @@ mkWalletLayer db nw tl = WalletLayer
_attachPrivateKey wid (xprv, pwd) = do
hpwd <- liftIO $ encryptPassphrase pwd
DB.putPrivateKey db (PrimaryKey wid) (xprv, hpwd)
DB.withLock db $ do
meta <- _readWalletMeta wid
now <- liftIO getCurrentTime
let modify x = x { passphraseInfo = Just (WalletPassphraseInfo now) }
DB.putWalletMeta db (PrimaryKey wid) (modify meta)

-- | Execute an action which requires holding a root XPrv
withRootKey
Expand Down
2 changes: 1 addition & 1 deletion lib/core/src/Cardano/Wallet/Api/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ getWallet w (ApiT wid) = do
, name =
ApiT $ meta ^. #name
, passphrase =
ApiT $ meta ^. #passphraseInfo
ApiT <$> meta ^. #passphraseInfo
, state =
ApiT $ meta ^. #status
}
Expand Down
2 changes: 1 addition & 1 deletion lib/core/src/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ data ApiWallet = ApiWallet
, balance :: !(ApiT WalletBalance)
, delegation :: !(ApiT (WalletDelegation (ApiT PoolId)))
, name :: !(ApiT WalletName)
, passphrase :: !(ApiT WalletPassphraseInfo)
, passphrase :: !(Maybe (ApiT WalletPassphraseInfo))
, state :: !(ApiT WalletState)
} deriving (Eq, Generic, Show)

Expand Down
2 changes: 1 addition & 1 deletion lib/core/src/Cardano/Wallet/Primitive/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ data WalletMetadata = WalletMetadata
{ name
:: !WalletName
, passphraseInfo
:: !WalletPassphraseInfo
:: !(Maybe WalletPassphraseInfo)
, status
:: !WalletState
, delegation
Expand Down
2 changes: 1 addition & 1 deletion lib/core/test/unit/Cardano/Wallet/DB/MVarSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ instance Arbitrary WalletMetadata where
shrink _ = []
arbitrary = WalletMetadata
<$> (WalletName <$> elements ["bulbazaur", "charmander", "squirtle"])
<*> (WalletPassphraseInfo <$> arbitrary)
<*> (fmap WalletPassphraseInfo <$> arbitrary)
<*> oneof [pure Ready, Restoring . Quantity <$> arbitraryBoundedEnum]
<*> pure NotDelegating

Expand Down
2 changes: 1 addition & 1 deletion lib/core/test/unit/Cardano/WalletSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ walletUpdatePassphraseNoSuchWallet wallet@(wid', _, _) wid (old, new) =
wid /= wid' ==> monadicIO $ liftIO $ do
(WalletLayerFixture _ wl _) <- liftIO $ setupFixture wallet
attempt <- runExceptT $ updateWalletPassphrase wl wid (old, new)
let err = ErrUpdatePassphraseNoSuchWallet (ErrNoSuchWallet wid)
let err = ErrUpdatePassphraseWithRootKey (ErrWithRootKeyNoRootKey wid)
attempt `shouldBe` Left err

{-------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ import Data.Quantity
( Quantity (..) )
import Data.Text
( Text )
import Data.Time
( UTCTime )
import GHC.TypeLits
( Symbol )
import Language.Haskell.TH.Quote
Expand Down Expand Up @@ -260,15 +258,16 @@ delegation =
-> s
_set (s, v) = set typed (ApiT v ) s

passphraseLastUpdate :: HasType (ApiT WalletPassphraseInfo) s => Lens' s Text
passphraseLastUpdate
:: HasType (Maybe (ApiT WalletPassphraseInfo)) s
=> Lens' s (Maybe Text)
passphraseLastUpdate =
lens _get _set
where
_get :: HasType (ApiT WalletPassphraseInfo) s => s -> Text
_get = T.pack . show . lastUpdatedAt . getApiT . view typed
_set :: HasType (ApiT WalletPassphraseInfo) s => (s, Text) -> s
_set (s, v) =
set typed (ApiT $ WalletPassphraseInfo ((read $ T.unpack v) :: UTCTime)) s
_get :: HasType (Maybe (ApiT WalletPassphraseInfo)) s => s -> Maybe Text
_get = fmap (T.pack . show . lastUpdatedAt . getApiT) . view typed
_set :: HasType (Maybe (ApiT WalletPassphraseInfo)) s => (s, Maybe Text) -> s
_set (s, v) = set typed (ApiT . WalletPassphraseInfo . read . T.unpack <$> v) s

state :: HasType (ApiT WalletState) s => Lens' s WalletState
state =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ spec = do
, expectFieldEqual state (Restoring (Quantity minBound))
, expectFieldEqual delegation (NotDelegating)
, expectFieldEqual walletId "2cf060fe53e4e0593f145f22b858dfc60676d4ab"
, expectFieldNotEqual passphraseLastUpdate "2019-04-12 07:57:28.439742724 UTC"
, expectFieldNotEqual passphraseLastUpdate Nothing
]

it "WALLETS_CREATE_03,09 - Cannot create wallet that exists" $ \ctx -> do
Expand Down
1 change: 0 additions & 1 deletion specifications/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,6 @@ definitions:
- balance
- delegation
- name
- passphrase
- state
properties:
id: *walletId
Expand Down

0 comments on commit 64c94cc

Please sign in to comment.