Skip to content
Permalink
Browse files

[DEVOPS-1195] allow importing a raw secret

  • Loading branch information...
cleverca22 committed Mar 13, 2019
1 parent 5a00ac5 commit b4be5cf965d8563e98f4e2f1c75b37a332f9d742
@@ -13,6 +13,7 @@ module Pos.Crypto.Signing.Types.Safe
, encToPublic
, noPassEncrypt
, checkPassMatches
, passScryptParam
) where

import qualified Cardano.Crypto.Wallet as CC
@@ -22,6 +22,7 @@ let
dist/build/cardano-node/cardano-node --configuration-file ${cfgFiles}/configuration.yaml --configuration-key ${cfg.key} \
--db-path states/${cfg.name}/DB --keyfile states/${cfg.name}/secret.key \
--wallet-db-path states/${cfg.name}/Wallet
--logs-prefix states/${cfg.name}/logs --topology ${../script-runner/. + "/topology-${cfg.name}.yaml"} \
--tlscert $STATE/tls/server/server.crt --tlskey $STATE/tls/server/server.key \
--tlsca $STATE/tls/server/ca.crt \
@@ -206,6 +206,9 @@ importWalletError e = case e of
ex@(ImportWalletNoWalletFoundInBackup _file) ->
V1.UnknownError (sformat build ex)

ex@(ImportWalletMissingField) ->
V1.UnknownError (sformat build ex)

(ImportWalletCreationFailed e') ->
createWalletError e'

@@ -159,14 +159,16 @@ import Cardano.Wallet.API.V1.Generic (jsendErrorGenericParseJSON,
import Cardano.Wallet.API.V1.Swagger.Example (Example, example)
import Cardano.Wallet.Types.UtxoStatistics
import Cardano.Wallet.Util (mkJsonKey, showApiUtcTime)
import qualified Pos.Crypto as S

import Cardano.Mnemonic (Mnemonic)
import Pos.Binary.Class (decodeFull')
import qualified Pos.Chain.Txp as Txp
import qualified Pos.Client.Txp.Util as Core
import qualified Pos.Core as Core
import Pos.Crypto (EncryptedSecretKey, PublicKey (..), decodeHash,
hashHexF)
import Pos.Crypto (EncryptedSecretKey,
EncryptedSecretKey (EncryptedSecretKey), PublicKey (..),
decodeHash, hashHexF)
import qualified Pos.Crypto.Signing as Core
import Pos.Infra.Communication.Types.Protocol ()
import Pos.Infra.Diffusion.Subscription.Status
@@ -1646,8 +1648,9 @@ instance Buildable (SecureLog EncryptedSecretKey) where
-- backup file.
data WalletImport = WalletImport
{ wiSpendingPassword :: !(Maybe SpendingPassword)
, wiFilePath :: !FilePath
} deriving (Show, Eq, Generic)
, wiFilePath :: !(Maybe FilePath)
, wiRawSecret :: !(Maybe EncryptedSecretKey)
} deriving (Show, Generic)

deriveJSON Aeson.defaultOptions ''WalletImport

@@ -1661,6 +1664,7 @@ instance ToSchema WalletImport where
instance Arbitrary WalletImport where
arbitrary = WalletImport <$> arbitrary
<*> arbitrary
<*> arbitrary

deriveSafeBuildable ''WalletImport
instance BuildableSafeGen WalletImport where
@@ -1882,7 +1886,13 @@ instance Example Redemption where

instance Example WalletImport where
example = WalletImport <$> example
<*> pure "/Users/foo/Documents/wallet_to_import.key"
<*> (pure $ Just "/Users/foo/Documents/wallet_to_import.key")
<*> (pure $ Just $ EncryptedSecretKey (CC.generate dummyKey dummyPassphrase) passphrase)
where
dummyPassphrase :: ByteString
dummyPassphrase = "passphrase"
Right dummyKey = Base16.decode "0000000000000000000000000000000000000000000000000000000000000000"
passphrase = S.encryptPassWithSalt S.passScryptParam S.emptySalt dummyPassphrase

--
-- Wallet Errors
@@ -357,6 +357,8 @@ data ImportWalletError =
-- didn't provide any.
| ImportWalletCreationFailed CreateWalletError
-- ^ When trying to import this wallet, the wallet creation failed.
| ImportWalletMissingField
-- ^ one of FilePath or RawSecret must be specified

-- | Unsound show instance needed for the 'Exception' instance.
instance Show ImportWalletError where
@@ -371,6 +373,7 @@ instance Buildable ImportWalletError where
bprint ("ImportWalletNoWalletFoundInBackup " % build) fp
build (ImportWalletCreationFailed err) =
bprint ("ImportWalletCreationFailed " % build) err
build (ImportWalletMissingField) = "ImportWalletMissingField"

------------------------------------------------------------
-- Errors when getting Transactions
@@ -1,3 +1,5 @@
{-# LANGUAGE NamedFieldPuns #-}

module Cardano.Wallet.WalletLayer.Kernel.Internal (
nextUpdate
, applyUpdate
@@ -110,18 +112,26 @@ importWallet :: MonadIO m
=> Kernel.PassiveWallet
-> WalletImport
-> m (Either ImportWalletError Wallet)
importWallet pw WalletImport{..} = liftIO $ do
secretE <- try $ Keystore.readWalletSecret wiFilePath
case secretE of
Left e ->
if isDoesNotExistError e
then return (Left $ ImportWalletFileNotFound wiFilePath)
else throwM e
Right mbEsk -> do
case mbEsk of
Nothing -> return (Left $ ImportWalletNoWalletFoundInBackup wiFilePath)
Just esk -> do
res <- liftIO $ createWallet pw (ImportWalletFromESK esk wiSpendingPassword)
return $ case res of
Left e -> Left (ImportWalletCreationFailed e)
Right importedWallet -> Right importedWallet
importWallet pw WalletImport{wiSpendingPassword,wiFilePath,wiRawSecret} = liftIO $ do
eskE <- case (wiFilePath, wiRawSecret) of
(Nothing, Nothing) -> return $ Left ImportWalletMissingField
(Just _, Just _) -> return $ Left ImportWalletMissingField
(Just filePath, Nothing) -> do
secretE <- try $ Keystore.readWalletSecret filePath
case secretE of
Left e ->
if isDoesNotExistError e
then return (Left $ ImportWalletFileNotFound filePath)
else throwM e
Right mbEsk -> do
case mbEsk of
Nothing -> return (Left $ ImportWalletNoWalletFoundInBackup filePath)
Just esk -> return $ Right esk
(Nothing, Just esk) -> return $ Right esk
case eskE of
Left err -> return $ Left err
Right esk -> do
res <- liftIO $ createWallet pw (ImportWalletFromESK esk wiSpendingPassword)
return $ case res of
Left e -> Left (ImportWalletCreationFailed e)
Right importedWallet -> Right importedWallet

0 comments on commit b4be5cf

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