Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rvl committed Aug 1, 2021
1 parent ccfe475 commit 24da1b0
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 51 deletions.
17 changes: 6 additions & 11 deletions lib/core-integration/src/Test/Integration/Framework/DSL.hs
Expand Up @@ -271,8 +271,6 @@ import Cardano.Wallet.Primitive.AddressDerivation
, WalletKey (..)
, fromHex
, hex
import Cardano.Wallet.Primitive.Passphrase ( Passphrase (..)
, preparePassphrase
)
import Cardano.Wallet.Primitive.AddressDerivation.Byron
( ByronKey (..) )
Expand All @@ -284,6 +282,10 @@ import Cardano.Wallet.Primitive.AddressDiscovery.Sequential
( coinTypeAda )
import Cardano.Wallet.Primitive.AddressDiscovery.Shared
( CredentialType (..) )
import Cardano.Wallet.Primitive.Passphrase
( Passphrase (..), preparePassphrase )
import Cardano.Wallet.Primitive.Passphrase.Legacy
( encryptPassphraseTestingOnly )
import Cardano.Wallet.Primitive.SyncProgress
( SyncProgress (..) )
import Cardano.Wallet.Primitive.Types
Expand Down Expand Up @@ -327,14 +329,10 @@ import Control.Retry
( capDelay, constantDelay, retrying )
import Crypto.Hash
( Blake2b_160, Digest, digestFromByteString )
import Crypto.Random.Entropy
( getEntropy )
import Data.Aeson
( FromJSON, ToJSON, Value, (.=) )
import Data.Aeson.QQ
( aesonQQ )
import Data.ByteArray.Encoding
( Base (..), convertFromBase, convertToBase )
import Data.ByteString
( ByteString )
import Data.Either.Extra
Expand Down Expand Up @@ -437,14 +435,11 @@ import qualified Cardano.Wallet.Primitive.AddressDerivation.Byron as Byron
import qualified Cardano.Wallet.Primitive.AddressDerivation.Icarus as Icarus
import qualified Cardano.Wallet.Primitive.AddressDerivation.Shared as Shared
import qualified Cardano.Wallet.Primitive.AddressDerivation.Shelley as Shelley
import qualified Cardano.Wallet.Primitive.Types as W
import qualified Cardano.Wallet.Primitive.Passphrase.Types as W
import qualified Cardano.Wallet.Primitive.Types.TokenBundle as TokenBundle
import qualified Cardano.Wallet.Primitive.Types.TokenMap as TokenMap
import qualified Cardano.Wallet.Primitive.Types.TokenQuantity as TokenQuantity
import qualified Codec.Binary.Bech32 as Bech32
import qualified Codec.CBOR.Encoding as CBOR
import qualified Codec.CBOR.Write as CBOR
import qualified Crypto.KDF.Scrypt as Scrypt
import qualified Data.Aeson as Aeson
import qualified Data.ByteArray as BA
import qualified Data.ByteString as BS
Expand Down Expand Up @@ -1255,7 +1250,7 @@ emptyRandomWalletWithPasswd ctx rawPwd = do
$ hex
$ Byron.getKey
$ Byron.generateKeyFromSeed seed pwd
pwdH <- encryptPassphraseTestingOnly pwd
pwdH <- liftIO $ toText <$> encryptPassphraseTestingOnly pwd
emptyByronWalletFromXPrvWith ctx "random" ("Random Wallet", key, pwdH)

postWallet'
Expand Down
Expand Up @@ -26,11 +26,13 @@ import Cardano.Wallet.Api.Types
, WalletStyle (..)
)
import Cardano.Wallet.Primitive.AddressDerivation
( PassphraseMaxLength (..), PassphraseMinLength (..), PaymentAddress )
( PaymentAddress )
import Cardano.Wallet.Primitive.AddressDerivation.Byron
( ByronKey )
import Cardano.Wallet.Primitive.AddressDerivation.Icarus
( IcarusKey )
import Cardano.Wallet.Primitive.Passphrase
( PassphraseMaxLength (..), PassphraseMinLength (..) )
import Cardano.Wallet.Primitive.SyncProgress
( SyncProgress (..) )
import Control.Monad
Expand Down Expand Up @@ -63,7 +65,6 @@ import Test.Integration.Framework.DSL
, emptyIcarusWallet
, emptyRandomWallet
, emptyRandomWalletWithPasswd
, encryptWalletPasswordWithScrypt
, eventually
, expectErrorMessage
, expectField
Expand All @@ -79,7 +80,6 @@ import Test.Integration.Framework.DSL
, fixtureRandomWallet
, genMnemonics
, getFromResponse
, getSaltFromHexScryptPassword
, json
, listFilteredByronWallets
, postByronWallet
Expand Down
Expand Up @@ -37,9 +37,11 @@ import Cardano.Wallet.Api.Types
, WalletStyle (..)
)
import Cardano.Wallet.Primitive.AddressDerivation
( DerivationIndex (..), Passphrase (..), Role (..) )
( DerivationIndex (..), Role (..) )
import Cardano.Wallet.Primitive.AddressDiscovery.Shared
( CredentialType (..) )
import Cardano.Wallet.Primitive.Passphrase
( Passphrase (..) )
import Cardano.Wallet.Primitive.SyncProgress
( SyncProgress (..) )
import Control.Monad
Expand Down
18 changes: 10 additions & 8 deletions lib/core/test/unit/Cardano/Wallet/DB/Arbitrary.hs
Expand Up @@ -78,12 +78,12 @@ import Cardano.Wallet.Primitive.AddressDiscovery.Shared
( SharedState (..), SharedStateFields (..) )
import Cardano.Wallet.Primitive.Model
( Wallet, currentTip, getState, unsafeInitWallet, utxo )
import Cardano.Wallet.Primitive.Passphrase
( Passphrase (..) )
import Cardano.Wallet.Primitive.Passphrase.Types
( Passphrase (..), PassphraseScheme (..), WalletPassphraseInfo (..) )
import Cardano.Wallet.Primitive.Passphrase.Types
( PassphraseHash (..) )
( Passphrase (..)
, PassphraseHash (..)
, PassphraseScheme (..)
, WalletPassphraseInfo (..)
)
import Cardano.Wallet.Primitive.Types
( Block (..)
, BlockHeader (..)
Expand Down Expand Up @@ -691,8 +691,10 @@ instance Arbitrary RewardAccount where
RewardAccount . BS.pack <$> vector 28

instance Arbitrary (Hash purpose) where
arbitrary = do
Hash . convertToBase Base16 . BS.pack <$> vector 16
arbitrary = Hash . convertToBase Base16 . BS.pack <$> vector 16

instance Arbitrary PassphraseHash where
arbitrary = PassphraseHash . convertToBase Base16 . BS.pack <$> vector 16

instance Arbitrary PoolId where
arbitrary = do
Expand Down Expand Up @@ -729,7 +731,7 @@ instance Buildable (ShelleyKey depth XPrv, PassphraseHash) where
build (_, h) = tupleF (xprvF, prefixF 8 hF <> "..." <> suffixF 8 hF)
where
xprvF = "XPrv" :: Builder
hF = build (toText (Hash @"BlockHeader" (getPassphraseHash h)))
hF = build (toText (Hash @"BlockHeader" (BA.convert h)))

instance Buildable MockChain where
build (MockChain chain) = blockListF' mempty build chain
Expand Down
16 changes: 8 additions & 8 deletions lib/core/test/unit/Cardano/Wallet/DB/StateMachine.hs
Expand Up @@ -302,27 +302,27 @@ class PersistPrivateKey k => MockPrivKey k where
-> (k XPrv, PassphraseHash)

-- | Unstuff the DBLayer private key into the mock type.
toMockPrivKey
:: (k XPrv, PassphraseHash)
-> MPrivKey
toMockPrivKey (_, PassphraseHash h) =
B8.unpack h
toMockPrivKey :: (k XPrv, PassphraseHash) -> MPrivKey
toMockPrivKey (_, h) = B8.unpack (BA.convert h)

zeroes :: ByteString
zeroes = B8.replicate 256 '0'

instance MockPrivKey (ShelleyKey 'RootK) where
fromMockPrivKey s = (k, BA.convert (B8.pack s))
fromMockPrivKey s = (k, unMockPrivKeyHash s)
where (k, _) = unsafeDeserializeXPrv (zeroes, mempty)

instance MockPrivKey (SharedKey 'RootK) where
fromMockPrivKey s = (k, BA.convert (B8.pack s))
fromMockPrivKey s = (k, unMockPrivKeyHash s)
where (k, _) = unsafeDeserializeXPrv (zeroes, mempty)

instance MockPrivKey (ByronKey 'RootK) where
fromMockPrivKey s = (k, BA.convert (B8.pack s))
fromMockPrivKey s = (k, unMockPrivKeyHash s)
where (k, _) = unsafeDeserializeXPrv (zeroes <> ":", mempty)

unMockPrivKeyHash :: MPrivKey -> PassphraseHash
unMockPrivKeyHash = PassphraseHash . BA.convert . B8.pack

newtype MockSealedTx = MockSealedTx { mockSealedTxId :: Hash "Tx" }
deriving (Show, Eq, Generic, NFData)

Expand Down
Expand Up @@ -20,7 +20,6 @@ import Cardano.Wallet.Primitive.AddressDerivation
( Depth (..)
, DerivationType (..)
, Index (..)
, Passphrase
, WalletKey (publicKey)
, getRawKey
, hashVerificationKey
Expand All @@ -32,6 +31,8 @@ import Cardano.Wallet.Primitive.AddressDerivation.Shelley
( ShelleyKey )
import Cardano.Wallet.Primitive.AddressDerivationSpec
()
import Cardano.Wallet.Primitive.Passphrase
( Passphrase )
import Cardano.Wallet.Unsafe
( unsafeBech32Decode, unsafeFromHex, unsafeMkMnemonic, unsafeXPrv )
import Data.Function
Expand Down
Expand Up @@ -37,7 +37,11 @@ import Cardano.Wallet.Primitive.AddressDerivation.Icarus
import Cardano.Wallet.Primitive.AddressDerivation.Shelley
( ShelleyKey (..) )
import Cardano.Wallet.Primitive.Passphrase
( checkPassphrase, encryptPassphrase, preparePassphrase )
( PassphraseHash (..)
, checkPassphrase
, encryptPassphrase
, preparePassphrase
)
import Cardano.Wallet.Primitive.Passphrase.Types
( ErrWrongPassphrase (..)
, Passphrase (..)
Expand Down Expand Up @@ -280,12 +284,12 @@ instance Arbitrary (Index 'WholeDomain 'AccountK) where
shrink _ = []
arbitrary = arbitraryBoundedEnum

instance Arbitrary (Passphrase "raw") where
instance Arbitrary (Passphrase "user") where
arbitrary = do
n <- choose (passphraseMinLength p, passphraseMaxLength p)
bytes <- T.encodeUtf8 . T.pack <$> replicateM n arbitraryPrintableChar
return $ Passphrase $ BA.convert bytes
where p = Proxy :: Proxy "raw"
where p = Proxy :: Proxy "user"

shrink (Passphrase bytes)
| BA.length bytes <= passphraseMinLength p = []
Expand All @@ -295,11 +299,11 @@ instance Arbitrary (Passphrase "raw") where
$ B8.take (passphraseMinLength p)
$ BA.convert bytes
]
where p = Proxy :: Proxy "raw"
where p = Proxy :: Proxy "user"

instance Arbitrary (Passphrase "encryption") where
arbitrary = preparePassphrase EncryptWithPBKDF2
<$> arbitrary @(Passphrase "raw")
<$> arbitrary @(Passphrase "user")

instance {-# OVERLAPS #-} Arbitrary (Passphrase "generation") where
shrink (Passphrase "") = []
Expand All @@ -309,11 +313,11 @@ instance {-# OVERLAPS #-} Arbitrary (Passphrase "generation") where
InfiniteList bytes _ <- arbitrary
return $ Passphrase $ BA.convert $ BS.pack $ take n bytes

instance Arbitrary (Hash "encryption") where
instance Arbitrary PassphraseHash where
shrink _ = []
arbitrary = do
InfiniteList bytes _ <- arbitrary
return $ Hash $ BS.pack $ take 32 bytes
pure $ PassphraseHash $ BA.convert $ BS.pack $ take 32 bytes

instance Arbitrary PassphraseScheme where
arbitrary = genericArbitrary
Expand Down Expand Up @@ -422,7 +426,7 @@ instance Arbitrary SomeMnemonic where
-- p = 1
-- These parameters are in Scrypt.defaultParams
encryptPasswordWithScrypt
:: Passphrase "raw"
:: Passphrase "user"
-> IO (Hash "encryption")
encryptPasswordWithScrypt p = do
hashed <- Scrypt.encryptPassIO Scrypt.defaultParams
Expand Down
6 changes: 3 additions & 3 deletions lib/core/test/unit/Cardano/Wallet/Primitive/PassphraseSpec.hs
Expand Up @@ -102,7 +102,7 @@ spec = do
\414941366a515867386539493d"
checkPassphrase EncryptWithScrypt pwd hash `shouldBe` Right ()
it "compare new implementation with cardano-sl - normal password" $ do
let pwd = Passphrase @"raw" $ BA.convert $ T.encodeUtf8 "Secure Passphrase"
let pwd = Passphrase @"user" $ BA.convert $ T.encodeUtf8 "Secure Passphrase"
let hash = Hash $ unsafeFromHex
"31347c387c317c714968506842665966555a336f5156434c384449744b\
\677642417a6c584d62314d6d4267695433776a556f3d7c53672b436e30\
Expand All @@ -111,15 +111,15 @@ spec = do
\524331385958326c6863552f36687a38432f496172773d3d"
checkPassphrase EncryptWithScrypt pwd hash `shouldBe` Right ()
it "compare new implementation with cardano-sl - empty password" $ do
let pwd = Passphrase @"raw" $ BA.convert $ T.encodeUtf8 ""
let pwd = Passphrase @"user" $ BA.convert $ T.encodeUtf8 ""
let hash = Hash $ unsafeFromHex
"31347c387c317c5743424875746242496c6a66734d764934314a30727a7\
\9663076657375724954796376766a793150554e377452673d3d7c54753\
\434596d6e547957546c5759674a3164494f7974474a7842632b432f786\
\2507657382b5135356a38303d"
checkPassphrase EncryptWithScrypt pwd hash `shouldBe` Right ()
it "compare new implementation with cardano-sl - cardano-wallet password" $ do
let pwd = Passphrase @"raw" $ BA.convert $ T.encodeUtf8 "cardano-wallet"
let pwd = Passphrase @"user" $ BA.convert $ T.encodeUtf8 "cardano-wallet"
let hash = Hash $ unsafeFromHex
"31347c387c317c2b6a6f747446495a6a566d586f43374c6c54425a576c\
\597a425834515177666475467578436b4d485569733d7c78324d646738\
Expand Down
8 changes: 4 additions & 4 deletions lib/core/test/unit/Cardano/WalletSpec.hs
Expand Up @@ -537,7 +537,7 @@ walletUpdateNameNoSuchWallet wallet@(wid', _, _) wid wName =

walletUpdatePassphrase
:: (WalletId, WalletName, DummyState)
-> Passphrase "raw"
-> Passphrase "user"
-> Maybe (ShelleyKey 'RootK XPrv, Passphrase "encryption")
-> Property
walletUpdatePassphrase wallet new mxprv = monadicIO $ do
Expand All @@ -559,7 +559,7 @@ walletUpdatePassphrase wallet new mxprv = monadicIO $ do
walletUpdatePassphraseWrong
:: (WalletId, WalletName, DummyState)
-> (ShelleyKey 'RootK XPrv, Passphrase "encryption")
-> (Passphrase "raw", Passphrase "raw")
-> (Passphrase "user", Passphrase "user")
-> Property
walletUpdatePassphraseWrong wallet (xprv, pwd) (old, new) =
pwd /= coerce old ==> monadicIO $ do
Expand All @@ -575,7 +575,7 @@ walletUpdatePassphraseWrong wallet (xprv, pwd) (old, new) =
walletUpdatePassphraseNoSuchWallet
:: (WalletId, WalletName, DummyState)
-> WalletId
-> (Passphrase "raw", Passphrase "raw")
-> (Passphrase "user", Passphrase "user")
-> Property
walletUpdatePassphraseNoSuchWallet wallet@(wid', _, _) wid (old, new) =
wid /= wid' ==> monadicIO $ do
Expand Down Expand Up @@ -608,7 +608,7 @@ walletUpdatePassphraseDate wallet (xprv, pwd) = monadicIO $ liftIO $ do
walletKeyIsReencrypted
:: (WalletId, WalletName)
-> (ShelleyKey 'RootK XPrv, Passphrase "encryption")
-> Passphrase "raw"
-> Passphrase "user"
-> Property
walletKeyIsReencrypted (wid, wname) (xprv, pwd) newPwd =
monadicIO $ liftIO $ do
Expand Down
Expand Up @@ -814,12 +814,12 @@ instance Show XPrv where
instance Eq XPrv where
(==) = (==) `on` xprvToBytes

instance Arbitrary (Passphrase "raw") where
instance Arbitrary (Passphrase "user") where
arbitrary = do
n <- choose (passphraseMinLength p, passphraseMaxLength p)
bytes <- T.encodeUtf8 . T.pack <$> replicateM n arbitraryPrintableChar
return $ Passphrase $ BA.convert bytes
where p = Proxy :: Proxy "raw"
where p = Proxy :: Proxy "user"

shrink (Passphrase bytes)
| BA.length bytes <= passphraseMinLength p = []
Expand All @@ -829,11 +829,11 @@ instance Arbitrary (Passphrase "raw") where
$ B8.take (passphraseMinLength p)
$ BA.convert bytes
]
where p = Proxy :: Proxy "raw"
where p = Proxy :: Proxy "user"

instance Arbitrary (Passphrase "encryption") where
arbitrary = preparePassphrase EncryptWithPBKDF2
<$> arbitrary @(Passphrase "raw")
<$> arbitrary @(Passphrase "user")

instance Arbitrary (Quantity "byte" Word16) where
arbitrary = Quantity <$> choose (128, 2048)
Expand Down

0 comments on commit 24da1b0

Please sign in to comment.