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

Commit a385013

Browse files
committed
Backporting 'IsOurs' abstraction from cardano-wallet
1 parent c11c2ac commit a385013

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

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

+39-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ module Cardano.Wallet.Kernel.DB.HdWallet (
2323
, HdRoot(..)
2424
, HdAccount(..)
2525
, HdAddress(..)
26+
-- * IsOurs
27+
, IsOurs(..)
28+
, decryptHdLvl2DerivationPath
2629
-- * HD Wallet state
2730
, HdAccountState(..)
2831
, HdAccountUpToDate(..)
@@ -113,14 +116,15 @@ import Serokell.Util (listJson)
113116
import qualified Pos.Core as Core
114117
import Pos.Core.Chrono (NewestFirst (..))
115118
import Pos.Core.NetworkMagic (NetworkMagic (..))
119+
import Pos.Crypto (HDPassphrase)
116120
import qualified Pos.Crypto as Core
117121

118122
import Cardano.Wallet.API.V1.Types (V1 (..))
119123
import Cardano.Wallet.Kernel.DB.BlockContext
120124
import Cardano.Wallet.Kernel.DB.InDb
121125
import Cardano.Wallet.Kernel.DB.Spec
122126
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')
124128
import qualified Cardano.Wallet.Kernel.DB.Util.IxSet as IxSet hiding (Indexable)
125129
import qualified Cardano.Wallet.Kernel.DB.Util.Zoomable as Z
126130
import Cardano.Wallet.Kernel.NodeStateAdaptor (SecurityParameter (..))
@@ -210,6 +214,9 @@ deriveSafeCopy 1 'base ''HasSpendingPassword
210214
eskToHdRootId :: NetworkMagic -> Core.EncryptedSecretKey -> HdRootId
211215
eskToHdRootId nm = HdRootId . InDb . (Core.makePubKeyAddressBoot nm) . Core.encToPublic
212216

217+
eskToHdPassphrase :: Core.EncryptedSecretKey -> HDPassphrase
218+
eskToHdPassphrase = Core.deriveHDPassphrase . Core.encToPublic
219+
213220

214221
{-------------------------------------------------------------------------------
215222
HD wallets
@@ -504,6 +511,37 @@ hdAccountRestorationState a = case a ^. hdAccountState of
504511
( _hdIncompleteHistorical ^. currentCheckpoint . cpContext . lazy,
505512
_hdIncompleteCurrent ^. oldestCheckpoint . pcheckpointContext)
506513

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
507545

508546
{-------------------------------------------------------------------------------
509547
Unknown identifiers
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module Cardano.Wallet.Kernel.Decrypt
22
( decryptAddress
3-
, decryptHdLvl2DerivationPath
43
, keyToWalletDecrCredentials
54
, WalletDecrCredentials
65
, WalletDecrCredentialsKey(..)
@@ -12,17 +11,5 @@ import Data.List ((!!))
1211

1312
import Pos.Wallet.Web.Tracking.Decrypt
1413

15-
import Cardano.Wallet.Kernel.DB.HdWallet (HdAccountIx (..),
16-
HdAddressIx (..))
1714
import Pos.Core (Address, aaPkDerivationPath, addrAttributesUnwrapped)
1815
import Pos.Crypto (HDPassphrase, unpackHDAddressAttr)
19-
20-
21-
decryptHdLvl2DerivationPath :: HDPassphrase
22-
-> Address
23-
-> Maybe (HdAccountIx, HdAddressIx)
24-
decryptHdLvl2DerivationPath hdPass addr = do
25-
hdPayload <- aaPkDerivationPath $ addrAttributesUnwrapped addr
26-
derPath <- unpackHDAddressAttr hdPass hdPayload
27-
guard $ length derPath == 2
28-
pure (HdAccountIx (derPath !! 0), HdAddressIx (derPath !! 1))

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ import Cardano.Wallet.Kernel.DB.HdWallet (AssuranceLevel,
4141
import qualified Cardano.Wallet.Kernel.DB.HdWallet as HD
4242
import qualified Cardano.Wallet.Kernel.DB.HdWallet.Create as HD
4343
import Cardano.Wallet.Kernel.DB.InDb (InDb (..), fromDb)
44-
import Cardano.Wallet.Kernel.Decrypt (WalletDecrCredentialsKey (..),
45-
decryptHdLvl2DerivationPath, keyToWalletDecrCredentials)
44+
import Cardano.Wallet.Kernel.Decrypt (keyToWalletDecrCredentials)
4645
import Cardano.Wallet.Kernel.Internal (PassiveWallet, walletKeystore,
4746
walletProtocolMagic, wallets)
4847
import qualified Cardano.Wallet.Kernel.Keystore as Keystore
@@ -282,7 +281,7 @@ defaultHdAddressWith :: HDPassphrase
282281
-> Address
283282
-> Maybe HdAddress
284283
defaultHdAddressWith hdPass rootId cardanoAddress = do
285-
(hdAccountIx, hdAddressIx) <- decryptHdLvl2DerivationPath hdPass cardanoAddress
284+
(hdAccountIx, hdAddressIx) <- HD.decryptHdLvl2DerivationPath hdPass cardanoAddress
286285
let hdAccountId = HdAccountId rootId hdAccountIx
287286
hdAddressId = HdAddressId hdAccountId hdAddressIx
288287
pure $ HD.HdAddress hdAddressId (InDb cardanoAddress)

0 commit comments

Comments
 (0)