Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit a02e5b9

Browse files
committed
[CO-407] Remove legacy-wallet dependency from new wallet Migration
1 parent 537260a commit a02e5b9

File tree

10 files changed

+122
-255
lines changed

10 files changed

+122
-255
lines changed

wallet-new/src/Cardano/Wallet/Kernel/DB/AcidState.hs

+18-18
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,8 @@ observableRollbackUseInTestsOnly = runUpdateDiscardSnapshot $
501501
-- 'createHdWallet'/'createWalletHdRnd' in "Cardano.Wallet.Kernel.Wallets".
502502
--
503503
-- INVARIANT: Creating a new wallet always come with a fresh HdAccount and
504-
-- a fresh 'HdAddress' attached to it, so we have to pass these two extra
505-
-- piece of into to the update function. We do @not@ build these inside the
504+
-- an optional 'HdAddress' attached to it, so we have to pass these two extra
505+
-- pieces of info to the update function. We do @not@ build these inside the
506506
-- update function because derivation requires an 'EncryptedSecretKey' and
507507
-- definitely we do not want it to show up in our acid-state logs.
508508
--
@@ -511,8 +511,8 @@ createHdWallet :: HdRoot
511511
-- ^ The default HdAccountId to go with this HdRoot. This
512512
-- function will take responsibility of creating the associated
513513
-- 'HdAccount'.
514-
-> HdAddress
515-
-- ^ The default HdAddress to go with this HdRoot.
514+
-> Maybe HdAddress
515+
-- ^ An optional default HdAddress to go with this HdRoot.
516516
-> Map HdAccountId (Utxo,[AddrWithId])
517517
-> Update DB (Either HD.CreateHdRootError ())
518518
createHdWallet newRoot defaultHdAccountId defaultHdAddress utxoByAccount =
@@ -532,14 +532,12 @@ createHdWallet newRoot defaultHdAccountId defaultHdAddress utxoByAccount =
532532
insertDefault :: Map HdAccountId (Utxo, [AddrWithId])
533533
-> Map HdAccountId (Utxo, [AddrWithId])
534534
insertDefault m =
535-
let defaultAddr = ( defaultHdAddress ^. hdAddressId
536-
, defaultHdAddress ^. hdAddressAddress . fromDb
537-
)
535+
let addrWithId = fmap toAddrWithId defaultHdAddress
538536
in case Map.lookup defaultHdAccountId m of
539537
Just (utxo, addrs) ->
540-
Map.insert defaultHdAccountId (utxo, defaultAddr : addrs) m
538+
Map.insert defaultHdAccountId (utxo, maybe addrs (: addrs) addrWithId) m
541539
Nothing ->
542-
Map.insert defaultHdAccountId (mempty, [defaultAddr]) m
540+
Map.insert defaultHdAccountId (mempty, maybeToList addrWithId) m
543541

544542
-- | Create an HdWallet with HdRoot, without defaultHdAddress. Since we use this function
545543
-- for external wallets only, we don't have defaultHdAddress here (because in the current
@@ -578,8 +576,8 @@ createHdExternalWallet newRoot defaultHdAccountId utxoByAccount =
578576
-- starting from the 'HdAccountOutsideK' state.
579577
--
580578
-- INVARIANT: Creating a new wallet always come with a fresh HdAccount and
581-
-- a fresh 'HdAddress' attached to it, so we have to pass these two extra
582-
-- piece of into to the update function. We do @not@ build these inside the
579+
-- an optional 'HdAddress' attached to it, so we have to pass these two extra
580+
-- pieces of info to the update function. We do @not@ build these inside the
583581
-- update function because derivation requires an 'EncryptedSecretKey' and
584582
-- definitely we do not want it to show up in our acid-state logs.
585583
--
@@ -588,8 +586,8 @@ restoreHdWallet :: HdRoot
588586
-- ^ The default HdAccountId to go with this HdRoot. This
589587
-- function will take responsibility of creating the associated
590588
-- 'HdAccount'.
591-
-> HdAddress
592-
-- ^ The default HdAddress to go with this HdRoot
589+
-> Maybe HdAddress
590+
-- ^ Optional default HdAddress to go with this HdRoot
593591
-> BlockContext
594592
-- ^ The initial block context for restorations
595593
-> Map HdAccountId (Utxo, Utxo, [AddrWithId])
@@ -613,15 +611,17 @@ restoreHdWallet newRoot defaultHdAccountId defaultHdAddress ctx utxoByAccount =
613611
insertDefault :: Map HdAccountId (Utxo, Utxo, [AddrWithId])
614612
-> Map HdAccountId (Utxo, Utxo, [AddrWithId])
615613
insertDefault m =
616-
let defaultAddr = ( defaultHdAddress ^. hdAddressId
617-
, defaultHdAddress ^. hdAddressAddress . fromDb
618-
)
614+
let addrWithId = fmap toAddrWithId defaultHdAddress
619615
in case Map.lookup defaultHdAccountId m of
620616
Just (utxo, utxo', addrs) ->
621-
Map.insert defaultHdAccountId (utxo, utxo', defaultAddr : addrs) m
617+
Map.insert defaultHdAccountId (utxo, utxo', maybe addrs (: addrs) addrWithId) m
622618
Nothing ->
623-
Map.insert defaultHdAccountId (mempty, mempty, [defaultAddr]) m
619+
Map.insert defaultHdAccountId (mempty, mempty, maybeToList addrWithId) m
624620

621+
toAddrWithId :: HdAddress -> AddrWithId
622+
toAddrWithId addr = ( addr ^. hdAddressId
623+
, addr ^. hdAddressAddress . fromDb
624+
)
625625
{-------------------------------------------------------------------------------
626626
Internal: support for updating accounts
627627
-------------------------------------------------------------------------------}

wallet-new/src/Cardano/Wallet/Kernel/Keystore.hs

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module Cardano.Wallet.Kernel.Keystore (
2525
-- * Deleting values
2626
, delete
2727
-- * Queries on a keystore
28+
, getKeys
2829
, lookup
2930
-- * Tests handy functions
3031
, bracketTestKeystore
@@ -268,6 +269,11 @@ lookupKey :: NetworkMagic -> UserSecret -> WalletId -> Maybe EncryptedSecretKey
268269
lookupKey nm us (WalletIdHdRnd walletId) =
269270
Data.List.find (\k -> eskToHdRootId nm k == walletId) (us ^. usKeys)
270271

272+
-- | Return all Keystore 'usKeys'
273+
getKeys :: Keystore -> IO [EncryptedSecretKey]
274+
getKeys (Keystore ks) =
275+
Strict.withMVar ks $ \(InternalStorage us) -> return $ us ^. usKeys
276+
271277
{-------------------------------------------------------------------------------
272278
Deleting things from the keystore
273279
-------------------------------------------------------------------------------}

0 commit comments

Comments
 (0)