Skip to content
Permalink
Browse files

[DEVOPS-1195] address review comments and clean up code

  • Loading branch information...
cleverca22 committed Mar 12, 2019
1 parent aa476e7 commit 1f11c329d33b06e613ef748b7741183a2221158d
Showing with 37 additions and 34 deletions.
  1. +8 −11 wallet/src/Cardano/Wallet/API/V1/Types.hs
  2. +29 −23 wallet/src/Cardano/Wallet/WalletLayer/Kernel.hs
@@ -1608,14 +1608,16 @@ instance ToSchema WalletBalance where
genericSchemaDroppingPrefix "wb" (\(--^) props -> props
& "balance"
--^ "the balance of a given public key"
& "walletId"
--^ "the walletid of the given public key"
)

instance Example WalletBalance
instance Arbitrary WalletBalance where
arbitrary = WalletBalance <$> arbitrary <*> arbitrary

instance Buildable WalletBalance where
build (WalletBalance bal walid) = "WalletBalance { ballence = " <> (show bal) <> ", walletid = " <> show walid <> " }"
build (WalletBalance bal walid) = "WalletBalance { balence = " <> (show bal) <> ", walletId = " <> show walid <> " }"

instance ToSchema EncryptedSecretKey where
declareNamedSchema _ =
@@ -1624,17 +1626,12 @@ instance ToSchema EncryptedSecretKey where
& format ?~ "hex|base16"

instance ToJSON EncryptedSecretKey where
toJSON _esk = "todo"
toJSON _esk = "<encrypted secret>"

mkEncryptedSecretKey :: Text -> Either Text EncryptedSecretKey
mkEncryptedSecretKey eskhex = join $
case Base16.decode eskhex of
Left e -> Left e
Right bs -> do
let
esk :: Either Text EncryptedSecretKey
esk = decodeFull' bs
pure esk
mkEncryptedSecretKey eskhex = case Base16.decode eskhex of
Left e -> Left e
Right bs -> decodeFull' bs

instance FromJSON EncryptedSecretKey where
parseJSON (String eskhex) = case mkEncryptedSecretKey eskhex of
@@ -1643,7 +1640,7 @@ instance FromJSON EncryptedSecretKey where
parseJSON x = typeMismatch "parseJSON failed for EncryptedSecretKey" x

instance Buildable (SecureLog EncryptedSecretKey) where
build _ = "todo"
build _ = "<encrypted secret>"

-- | A type encapsulating enough information to import a wallet from a
-- backup file.
@@ -14,7 +14,6 @@ import qualified Control.Concurrent.STM as STM
import Control.Monad.IO.Unlift (MonadUnliftIO)
import qualified Data.ByteString.Char8 as BS
import qualified Data.List.NonEmpty as NE
import qualified Data.Map as M (toList)
import qualified Data.Text as T

import Data.Foldable (for_)
@@ -24,13 +23,13 @@ import qualified Formatting as F
import Pos.Chain.Block (Blund, blockHeader, headerHash, prevBlockL)
import Pos.Chain.Genesis (Config (..))
import Pos.Chain.Txp (TxIn, TxOutAux)
import Pos.Chain.Txp (Utxo, toaOut, txOutAddress, txOutValue)
import Pos.Chain.Txp (toaOut, txOutAddress, txOutValue)
import Pos.Chain.Update (HasUpdateConfiguration)
import Pos.Core.Chrono (OldestFirst (..))
import Pos.Core.Common (Coin, addrToBase58, mkCoin, unsafeAddCoin)
import Pos.Core.Common (Address, Coin, addrToBase58, mkCoin,
unsafeAddCoin)
import Pos.Core.NetworkMagic (makeNetworkMagic)
import Pos.Crypto (EncryptedSecretKey, ProtocolMagic)
import Pos.DB.Txp.Utxo (getAllPotentiallyHugeUtxo)
import Pos.Infra.InjectFail (FInjects)
import Pos.Util.CompileInfo (HasCompileInfo)
import Pos.Util.Wlog (Severity (Debug, Warning))
@@ -40,22 +39,20 @@ import qualified Cardano.Wallet.Kernel as Kernel
import qualified Cardano.Wallet.Kernel.Actions as Actions
import qualified Cardano.Wallet.Kernel.BListener as Kernel
import Cardano.Wallet.Kernel.DB.AcidState (dbHdWallets)
import Cardano.Wallet.Kernel.DB.HdWallet (HdAddressId, eskToHdRootId,
getHdRootId, hdAccountRestorationState, hdRootId,
hdWalletsRoots)
import Cardano.Wallet.Kernel.DB.HdWallet (eskToHdRootId, getHdRootId,
hdAccountRestorationState, hdRootId, hdWalletsRoots)
import Cardano.Wallet.Kernel.DB.InDb (fromDb)
import qualified Cardano.Wallet.Kernel.DB.Read as Kernel
import qualified Cardano.Wallet.Kernel.DB.Util.IxSet as IxSet
import Cardano.Wallet.Kernel.Decrypt (eskToWalletDecrCredentials)
import Cardano.Wallet.Kernel.Decrypt (WalletDecrCredentials,
decryptAddress, eskToWalletDecrCredentials)
import Cardano.Wallet.Kernel.Diffusion (WalletDiffusion (..))
import Cardano.Wallet.Kernel.Internal (walletNode,
walletProtocolMagic)
import Cardano.Wallet.Kernel.Keystore (Keystore)
import Cardano.Wallet.Kernel.NodeStateAdaptor
import Cardano.Wallet.Kernel.PrefilterTx (WalletKey, filterOurs)
import qualified Cardano.Wallet.Kernel.Read as Kernel
import qualified Cardano.Wallet.Kernel.Restore as Kernel
import Cardano.Wallet.Kernel.Types (WalletId (WalletIdHdRnd))
import Cardano.Wallet.WalletLayer (ActiveWalletLayer (..),
PassiveWalletLayer (..))
import qualified Cardano.Wallet.WalletLayer.Kernel.Accounts as Accounts
@@ -173,25 +170,34 @@ bracketPassiveWallet pm mode logFunction keystore node fInjects f = do
invokeIO :: forall m'. MonadIO m' => Actions.WalletAction Blund -> m' ()
invokeIO = liftIO . STM.atomically . invoke

-- a variant of isOurs, that accepts a pre-derived WalletDecrCredentials, to save cpu cycles
fastIsOurs :: WalletDecrCredentials -> Address -> Bool
fastIsOurs wdc addr = case decryptAddress wdc addr of
Just _ -> True
Nothing -> False

-- takes a WalletDecrCredentials and transaction, and returns the Coin output, if its ours
maybeReadcoin :: WalletDecrCredentials -> (TxIn, TxOutAux) -> Maybe Coin
maybeReadcoin wdc (_, txout) = case fastIsOurs wdc (txOutAddress . toaOut $ txout) of
True -> Just $ (txOutValue . toaOut) txout
False -> Nothing

-- take a EncryptedSecretKey and return the sum of all utxo in the state, and the walletid
xqueryWalletBalance :: Kernel.PassiveWallet -> EncryptedSecretKey -> IO WalletBalance
xqueryWalletBalance w esk = do
let
nm = makeNetworkMagic (w ^. walletProtocolMagic)
wId = WalletIdHdRnd (eskToHdRootId nm esk)
wKey :: WalletKey
wKey = (wId, eskToWalletDecrCredentials nm esk)
withNode :: (HasCompileInfo, HasUpdateConfiguration) => Lock (WithNodeState IO) -> WithNodeState IO Utxo
withNode _lock = getAllPotentiallyHugeUtxo
--print wKey
all_utxo <- withNodeState (w ^. walletNode) withNode
--mapM_ print all_utxo
let
my_utxo = filterOurs wKey (txOutAddress . toaOut . snd) (M.toList all_utxo)
sumUtxo :: Coin -> ((TxIn, TxOutAux), HdAddressId) -> Coin
sumUtxo a b = unsafeAddCoin a ((txOutValue . toaOut . snd . fst) b)
rootid = eskToHdRootId nm esk
wdc = eskToWalletDecrCredentials nm esk
let
withNode :: (HasCompileInfo, HasUpdateConfiguration) => Lock (WithNodeState IO) -> WithNodeState IO [Coin]
withNode _lock = filterUtxo (maybeReadcoin wdc)
my_coins <- withNodeState (w ^. walletNode) withNode
let
balance :: Coin
balance = foldl' sumUtxo (mkCoin 0) my_utxo
walletid = addrToBase58 $ view fromDb (getHdRootId $ eskToHdRootId nm esk)
balance = foldl' unsafeAddCoin (mkCoin 0) my_coins
walletid = addrToBase58 $ view fromDb (getHdRootId rootid)
pure $ WalletBalance (V1 balance) (T.pack $ BS.unpack walletid)

-- | Initialize the active wallet.

0 comments on commit 1f11c32

Please sign in to comment.
You can’t perform that action at this time.