Skip to content

Commit

Permalink
store private key in DB & provide an accessor to read it back later
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ authored and Anviking committed Apr 26, 2019
1 parent cc79fcc commit 40ca2c2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import Cardano.Wallet.Primitive.AddressDerivation
, XPrv
, deriveAccountPrivateKey
, digest
, encryptPassphrase
, generateKeyFromSeed
, publicKey
)
Expand Down Expand Up @@ -252,7 +253,9 @@ mkWalletLayer db network = WalletLayer
, status = Restoring minBound
, delegation = NotDelegating
}
DB.createWallet db (PrimaryKey wid) checkpoint metadata $> wid
hpwd <- liftIO $ encryptPassphrase (passphrase w)
let creds = ( rootXPrv, hpwd )
DB.createWallet db (PrimaryKey wid) checkpoint metadata creds $> wid

, readWallet = _readWallet

Expand Down
9 changes: 9 additions & 0 deletions src/Cardano/Wallet/DB.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ module Cardano.Wallet.DB

import Prelude

import Cardano.Wallet.Primitive.AddressDerivation
( Depth (..), Key, XPrv )
import Cardano.Wallet.Primitive.Model
( Wallet )
import Cardano.Wallet.Primitive.Types
Expand All @@ -38,6 +40,7 @@ data DBLayer m s = DBLayer
:: PrimaryKey WalletId
-> Wallet s
-> WalletMetadata
-> (Key 'RootK XPrv, Hash "encryption")
-> ExceptT ErrWalletAlreadyExists m ()
-- ^ Initialize a database entry for a given wallet. 'putCheckpoint',
-- 'putWalletMeta' or 'putTxHistory' will actually all fail if they are
Expand Down Expand Up @@ -103,6 +106,12 @@ data DBLayer m s = DBLayer
--
-- Returns an empty map if the wallet isn't found.

, readPrivateKey
:: PrimaryKey WalletId
-> m (Maybe (Key 'RootK XPrv, Hash "encryption"))
-- ^ Read a previously stored private key and its associated passphrase
-- hash.

, withLock
:: forall e a. ()
=> ExceptT e m a
Expand Down
26 changes: 18 additions & 8 deletions src/Cardano/Wallet/DB/MVar.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import Cardano.Wallet.DB
, ErrWalletAlreadyExists (..)
, PrimaryKey (..)
)
import Cardano.Wallet.Primitive.AddressDerivation
( Depth (..), Key, XPrv )
import Cardano.Wallet.Primitive.Model
( Wallet )
import Cardano.Wallet.Primitive.Types
Expand All @@ -41,6 +43,7 @@ data Database s = Database
{ wallet :: Wallet s
, metadata :: WalletMetadata
, txHistory :: Map (Hash "Tx") (Tx, TxMeta)
, xprv :: (Key 'RootK XPrv, Hash "encryption")
}

-- | Instantiate a new in-memory "database" layer that simply stores data in
Expand All @@ -55,10 +58,10 @@ newDBLayer = do
Wallets
-----------------------------------------------------------------------}

{ createWallet = \key@(PrimaryKey wid) cp meta -> ExceptT $ do
{ createWallet = \key@(PrimaryKey wid) cp meta k -> ExceptT $ do
let alter = \case
Nothing ->
Right $ Just $ Database cp meta mempty
Right $ Just $ Database cp meta mempty k
Just _ ->
Left (ErrWalletAlreadyExists wid)
cp `deepseq` meta `deepseq` alterMVar db alter key
Expand All @@ -82,8 +85,8 @@ newDBLayer = do
let alter = \case
Nothing ->
Left (ErrNoSuchWallet wid)
Just (Database _ meta history) ->
Right $ Just $ Database cp meta history
Just (Database _ meta history k) ->
Right $ Just $ Database cp meta history k
cp `deepseq` alterMVar db alter key

, readCheckpoint = \key ->
Expand All @@ -97,8 +100,8 @@ newDBLayer = do
let alter = \case
Nothing ->
Left (ErrNoSuchWallet wid)
Just (Database cp _ history) ->
Right $ Just $ Database cp meta history
Just (Database cp _ history k) ->
Right $ Just $ Database cp meta history k
meta `deepseq` alterMVar db alter key

, readWalletMeta = \key -> do
Expand All @@ -112,13 +115,20 @@ newDBLayer = do
let alter = \case
Nothing ->
Left (ErrNoSuchWallet wid)
Just (Database cp meta txs) ->
Right $ Just $ Database cp meta (txs' <> txs)
Just (Database cp meta txs k) ->
Right $ Just $ Database cp meta (txs' <> txs) k
txs' `deepseq` alterMVar db alter key

, readTxHistory = \key ->
maybe mempty txHistory . Map.lookup key <$> readMVar db

{-----------------------------------------------------------------------
Keystore
-----------------------------------------------------------------------}

, readPrivateKey = \key ->
fmap xprv . Map.lookup key <$> readMVar db

{-----------------------------------------------------------------------
Lock
-----------------------------------------------------------------------}
Expand Down

0 comments on commit 40ca2c2

Please sign in to comment.