@@ -23,6 +23,9 @@ module Cardano.Wallet.Kernel.DB.HdWallet (
23
23
, HdRoot (.. )
24
24
, HdAccount (.. )
25
25
, HdAddress (.. )
26
+ -- * IsOurs
27
+ , IsOurs (.. )
28
+ , decryptHdLvl2DerivationPath
26
29
-- * HD Wallet state
27
30
, HdAccountState (.. )
28
31
, HdAccountUpToDate (.. )
@@ -113,14 +116,15 @@ import Serokell.Util (listJson)
113
116
import qualified Pos.Core as Core
114
117
import Pos.Core.Chrono (NewestFirst (.. ))
115
118
import Pos.Core.NetworkMagic (NetworkMagic (.. ))
119
+ import Pos.Crypto (HDPassphrase )
116
120
import qualified Pos.Crypto as Core
117
121
118
122
import Cardano.Wallet.API.V1.Types (V1 (.. ))
119
123
import Cardano.Wallet.Kernel.DB.BlockContext
120
124
import Cardano.Wallet.Kernel.DB.InDb
121
125
import Cardano.Wallet.Kernel.DB.Spec
122
126
import Cardano.Wallet.Kernel.DB.Util.AcidState
123
- import Cardano.Wallet.Kernel.DB.Util.IxSet
127
+ import Cardano.Wallet.Kernel.DB.Util.IxSet hiding ( foldl' )
124
128
import qualified Cardano.Wallet.Kernel.DB.Util.IxSet as IxSet hiding (Indexable )
125
129
import qualified Cardano.Wallet.Kernel.DB.Util.Zoomable as Z
126
130
import Cardano.Wallet.Kernel.NodeStateAdaptor (SecurityParameter (.. ))
@@ -210,6 +214,9 @@ deriveSafeCopy 1 'base ''HasSpendingPassword
210
214
eskToHdRootId :: NetworkMagic -> Core. EncryptedSecretKey -> HdRootId
211
215
eskToHdRootId nm = HdRootId . InDb . (Core. makePubKeyAddressBoot nm) . Core. encToPublic
212
216
217
+ eskToHdPassphrase :: Core. EncryptedSecretKey -> HDPassphrase
218
+ eskToHdPassphrase = Core. deriveHDPassphrase . Core. encToPublic
219
+
213
220
214
221
{- ------------------------------------------------------------------------------
215
222
HD wallets
@@ -504,6 +511,37 @@ hdAccountRestorationState a = case a ^. hdAccountState of
504
511
( _hdIncompleteHistorical ^. currentCheckpoint . cpContext . lazy,
505
512
_hdIncompleteCurrent ^. oldestCheckpoint . pcheckpointContext)
506
513
514
+ {- ------------------------------------------------------------------------------
515
+ Address relationship: isOurs?
516
+ -------------------------------------------------------------------------------}
517
+
518
+ class IsOurs s where
519
+ isOurs :: Core. Address -> s -> (Maybe HdAddress , s )
520
+
521
+ {- ------------------------------------------------------------------------------
522
+ isOurs for Hd Random wallets
523
+ -------------------------------------------------------------------------------}
524
+
525
+ -- | NOTE: We could modify the given state here to actually store decrypted
526
+ -- addresses in a `Map` to trade a decryption against a map lookup for already
527
+ -- decrypted addresses.
528
+ instance IsOurs [(HdRootId , Core. EncryptedSecretKey )] where
529
+ isOurs addr s = (,s) $ foldl' (<|>) Nothing $ flip map s $ \ (rootId, esk) -> do
530
+ (accountIx, addressIx) <- decryptHdLvl2DerivationPath (eskToHdPassphrase esk) addr
531
+ let accId = HdAccountId rootId accountIx
532
+ let addrId = HdAddressId accId addressIx
533
+ return $ HdAddress addrId (InDb addr)
534
+
535
+ decryptHdLvl2DerivationPath
536
+ :: Core. HDPassphrase
537
+ -> Core. Address
538
+ -> Maybe (HdAccountIx , HdAddressIx )
539
+ decryptHdLvl2DerivationPath hdPass addr = do
540
+ hdPayload <- Core. aaPkDerivationPath $ Core. addrAttributesUnwrapped addr
541
+ derPath <- Core. unpackHDAddressAttr hdPass hdPayload
542
+ case derPath of
543
+ [a,b] -> Just (HdAccountIx a, HdAddressIx b)
544
+ _ -> Nothing
507
545
508
546
{- ------------------------------------------------------------------------------
509
547
Unknown identifiers
0 commit comments