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

Commit 7a7c74d

Browse files
authored
Merge pull request #3128 from input-output-hk/Squad1/CBR-288/review-wallet-peripheral-utilities
[CBR-288] Review wallet peripheral utilities
2 parents 567c9af + 6a36e02 commit 7a7c74d

File tree

30 files changed

+712
-976
lines changed

30 files changed

+712
-976
lines changed

crypto/Pos/Crypto/Encryption.hs

+6-18
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
{-# LANGUAGE RankNTypes #-}
2-
31
-- | Wrapper over AES. `encode` and `decode` use AES256 CTR mode with
42
-- IV = 0.
53
-- Decryption functions are used in wallet. Encryption is not used anywhere.
64

75
module Pos.Crypto.Encryption
8-
( AesKey
9-
, deriveAesKey
10-
, deriveAesKeyBS
6+
( AesKey(..)
117
, aesEncrypt
128
, aesDecrypt
139
) where
@@ -17,9 +13,7 @@ import Universum hiding (init)
1713
import Crypto.Cipher.AES (AES256)
1814
import Crypto.Cipher.Types (BlockCipher (..), cipherInit, ctrCombine, nullIV)
1915
import Crypto.Error (CryptoError, eitherCryptoError)
20-
import Crypto.Hash (Blake2b_256, Digest, hash)
21-
import Data.ByteArray (convert)
22-
import qualified Data.Text.Encoding as TE
16+
2317

2418
----------------------------------------------------------------------------
2519
-- AES
@@ -30,17 +24,11 @@ newtype AesKey = AesKey
3024
{ fromAESKey :: ByteString
3125
} deriving (Show, Eq, Generic, Hashable)
3226

33-
deriveAesKey :: Text -> AesKey
34-
deriveAesKey = deriveAesKeyBS . TE.encodeUtf8
35-
36-
deriveAesKeyBS :: ByteString -> AesKey
37-
deriveAesKeyBS = AesKey . blake2b
38-
where
39-
blake2b :: ByteString -> ByteString
40-
blake2b = convert @(Digest Blake2b_256) . hash
41-
4227
aesEncrypt :: ByteString -> AesKey -> Either CryptoError ByteString
43-
aesEncrypt input (fromAESKey -> sk) = ctrCombine <$> init <*> pure nullIV <*> pure input
28+
aesEncrypt input (fromAESKey -> sk) = ctrCombine
29+
<$> init
30+
<*> pure nullIV
31+
<*> pure input
4432
where
4533
-- FIXME: return either here
4634
init :: Either CryptoError AES256

crypto/Pos/Crypto/Orphans.hs

+1-16
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@ module Pos.Crypto.Orphans
44
(
55
) where
66

7-
import Prelude (show)
8-
import Universum hiding (show)
7+
import Universum
98

10-
import qualified Cardano.Crypto.Wallet as CC
119
import qualified Crypto.SCRAPE as Scrape
1210
import Crypto.Scrypt (EncryptedPass (..))
1311
import qualified Crypto.Sign.Ed25519 as Ed25519
1412
import Data.Aeson (FromJSON (..), ToJSON (..))
1513
import Data.Hashable (Hashable)
16-
import qualified Data.Hashable as Hashable
1714
import Serokell.Util.Base64 (JsonByteString (..))
1815

1916
import Pos.Binary.Class (Bi (..), decodeBinary, encodeBinary)
@@ -50,18 +47,6 @@ instance Bi Ed25519.Signature where
5047
encode (Ed25519.Signature s) = encode s
5148
decode = Ed25519.Signature <$> decode
5249

53-
instance Eq CC.XPub where
54-
a == b = CC.unXPub a == CC.unXPub b
55-
56-
instance Ord CC.XPub where
57-
compare = comparing CC.unXPub
58-
59-
instance Show CC.XPub where
60-
show = show . CC.unXPub
61-
62-
instance Hashable CC.XPub where
63-
hashWithSalt n = Hashable.hashWithSalt n . CC.unXPub
64-
6550
----------------------------------------------------------------------------
6651
-- Bi instances for Scrape
6752
----------------------------------------------------------------------------

pkgs/default.nix

+20-3
Original file line numberDiff line numberDiff line change
@@ -14516,33 +14516,41 @@ license = stdenv.lib.licenses.mit;
1451614516
({
1451714517
mkDerivation
1451814518
, base
14519+
, basement
1451914520
, bytestring
1452014521
, cryptonite
1452114522
, cryptonite-openssl
1452214523
, deepseq
1452314524
, fetchgit
14525+
, foundation
1452414526
, hashable
14527+
, integer-gmp
1452514528
, memory
1452614529
, stdenv
1452714530
}:
1452814531
mkDerivation {
1452914532

1453014533
pname = "cardano-crypto";
14531-
version = "1.0.0";
14534+
version = "1.1.0";
1453214535
src = fetchgit {
1453314536

1453414537
url = "https://github.com/input-output-hk/cardano-crypto";
14535-
sha256 = "05i1z01fzg0la4hk2ff0l89sk0a5ada81w60kwj9i8ix74jchp94";
14536-
rev = "287cc575fafe86af9d24af9d012c47f9d3f04da0";
14538+
sha256 = "1v165n4cmp4g272406vaaan7bwvrw2m55jwcbz8qqjxslxv1l63p";
14539+
rev = "33c7ecc6e4bd71c3ea0195e9d796eeace7be22cf";
1453714540

1453814541
};
14542+
isLibrary = true;
14543+
isExecutable = true;
1453914544
libraryHaskellDepends = [
1454014545
base
14546+
basement
1454114547
bytestring
1454214548
cryptonite
1454314549
cryptonite-openssl
1454414550
deepseq
14551+
foundation
1454514552
hashable
14553+
integer-gmp
1454614554
memory
1454714555
];
1454814556
doHaddock = false;
@@ -17985,7 +17993,9 @@ license = stdenv.lib.licenses.mit;
1798517993
, async
1798617994
, base
1798717995
, base58-bytestring
17996+
, basement
1798817997
, bytestring
17998+
, cardano-crypto
1798917999
, cardano-sl
1799018000
, cardano-sl-block
1799118001
, cardano-sl-client
@@ -18073,7 +18083,9 @@ aeson
1807318083
async
1807418084
base
1807518085
base58-bytestring
18086+
basement
1807618087
bytestring
18088+
cardano-crypto
1807718089
cardano-sl
1807818090
cardano-sl-block
1807918091
cardano-sl-client
@@ -18141,8 +18153,10 @@ libraryToolDepends = [
1814118153
cpphs
1814218154
];
1814318155
testHaskellDepends = [
18156+
aeson
1814418157
base
1814518158
bytestring
18159+
cardano-crypto
1814618160
cardano-sl
1814718161
cardano-sl-block
1814818162
cardano-sl-client
@@ -18163,6 +18177,7 @@ cardano-sl-update
1816318177
cardano-sl-util
1816418178
cardano-sl-util-test
1816518179
containers
18180+
cryptonite
1816618181
data-default
1816718182
deepseq
1816818183
ekg-core
@@ -18171,9 +18186,11 @@ formatting
1817118186
hspec
1817218187
lens
1817318188
log-warper
18189+
memory
1817418190
MonadRandom
1817518191
mtl
1817618192
QuickCheck
18193+
quickcheck-instances
1817718194
safe-exceptions
1817818195
safecopy
1817918196
serokell-util

scripts/test/wallet/integration.sh

-79
This file was deleted.

scripts/test/wallet/multi-integration.sh

-14
This file was deleted.

stack.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ packages:
9191
extra-dep: true
9292
- location:
9393
git: https://github.com/input-output-hk/cardano-crypto
94-
commit: 287cc575fafe86af9d24af9d012c47f9d3f04da0
94+
commit: 33c7ecc6e4bd71c3ea0195e9d796eeace7be22cf
9595
extra-dep: true
9696
# Waiting on the flexible types PR to be merged:
9797
# https://github.com/acid-state/acid-state/pull/94

tools/src/dbgen/Lib.hs

+16-26
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE DataKinds #-}
12
{-# LANGUAGE DeriveGeneric #-}
23
{-# LANGUAGE FlexibleContexts #-}
34
{-# LANGUAGE GADTs #-}
@@ -19,23 +20,21 @@ import Data.String.Conv (toS)
1920
import Data.Time (diffUTCTime, getCurrentTime)
2021
import GHC.Generics (Generic)
2122

22-
import Crypto.Random.Entropy (getEntropy)
2323
import Pos.Client.Txp (TxHistoryEntry (..))
2424
import Pos.Core (Address, Coin, mkCoin)
2525
import Pos.Data.Attributes (mkAttributes)
2626
import Pos.DB.GState.Common (getTip)
2727
import Pos.Infra.StateLock (StateLock (..))
2828
import Pos.Txp (Tx (..), TxId, TxIn (..), TxOut (..), TxOutAux (..))
2929
import Pos.Txp.Toil.Types (utxoToModifier)
30-
import Pos.Util.BackupPhrase (BackupPhrase (..))
31-
import Pos.Util.Mnemonics (toMnemonic)
30+
import Pos.Util.Mnemonic (Mnemonic, entropyToMnemonic, genEntropy)
3231
import Pos.Util.Servant (decodeCType)
3332
import Pos.Util.Util (lensOf)
3433
import Pos.Wallet.Web.Account (GenSeed (..))
3534
import Pos.Wallet.Web.ClientTypes (AccountId (..), CAccount (..), CAccountInit (..),
36-
CAccountMeta (..), CAddress (..), CId (..),
37-
CWallet (..), CWalletAssurance (..), CWalletInit (..),
38-
CWalletMeta (..), Wal)
35+
CAccountMeta (..), CAddress (..), CBackupPhrase (..),
36+
CId (..), CWallet (..), CWalletAssurance (..),
37+
CWalletInit (..), CWalletMeta (..), Wal)
3938
import Pos.Wallet.Web.ClientTypes.Instances ()
4039
import Pos.Wallet.Web.Methods.Logic (getAccounts, newAccountIncludeUnready, newAddress)
4140
import Pos.Wallet.Web.Methods.Restore (newWallet)
@@ -215,7 +214,7 @@ generateWalletDB CLI{..} spec@GenSpec{..} = do
215214

216215
case addTo of
217216
Just accId ->
218-
if (checkIfAddTo fakeUtxoSpec fakeTxs) then
217+
if checkIfAddTo fakeUtxoSpec fakeTxs then
219218
addAddressesTo spec accId
220219
else do
221220
timed $ generateFakeUtxo fakeUtxoSpec accId
@@ -299,7 +298,7 @@ generateRealTxHistE outputAddresses = do
299298
fakeTime <- liftIO $ generate arbitrary
300299
fakeTx <- liftIO $ generate $ genTxs fakeTxOut
301300

302-
pure $ THEntry
301+
pure THEntry
303302
{ _thTxId = fakeTxIds
304303
, _thTx = fakeTx
305304
, _thDifficulty = fakeChain
@@ -330,7 +329,7 @@ generateRealTxHistE outputAddresses = do
330329
let _txOutputs = NE.fromList txOut
331330
let _txAttributes = mkAttributes ()
332331

333-
pure $ UnsafeTx {..}
332+
pure UnsafeTx {..}
334333

335334
-- | Generate sensible amount of coins.
336335
genCoins :: Gen Coin
@@ -375,7 +374,7 @@ unwrapCAddress = decodeCType . cadId
375374

376375

377376
addAddressesTo :: GenSpec -> AccountId -> UberMonad ()
378-
addAddressesTo spec cid = genAddresses spec cid
377+
addAddressesTo = genAddresses
379378

380379

381380
genAccounts :: GenSpec -> (Int, CWallet) -> UberMonad ()
@@ -404,35 +403,26 @@ genWallet walletNum = do
404403
mnemonic <- newRandomMnemonic
405404
newWallet mempty (walletInit mnemonic)
406405
where
407-
walletInit :: BackupPhrase -> CWalletInit
406+
walletInit :: Mnemonic 12 -> CWalletInit
408407
walletInit backupPhrase = CWalletInit {
409408
cwInitMeta = CWalletMeta
410409
{ cwName = "Wallet #" <> show walletNum
411410
, cwAssurance = CWANormal
412411
, cwUnit = 0
413412
}
414-
, cwBackupPhrase = backupPhrase
413+
, cwBackupPhrase = CBackupPhrase backupPhrase
415414
}
416415

417416

418-
-- | Generates a new 'BackupPhrase'.
419-
newRandomMnemonic :: WalletWebMode BackupPhrase
420-
newRandomMnemonic = do
421-
422-
-- The size 16 should give you 12 words after bip39 encoding.
423-
let mnemonic :: IO ByteString
424-
mnemonic = getEntropy 16
425-
426-
genMnemonic <- liftIO mnemonic
427-
428-
let newMnemonic = either (error . show) id (toMnemonic genMnemonic)
429-
430-
pure $ BackupPhrase $ words newMnemonic
417+
-- | Generates a new 'Mnemonic'.
418+
newRandomMnemonic :: WalletWebMode (Mnemonic 12)
419+
newRandomMnemonic =
420+
liftIO (entropyToMnemonic <$> genEntropy)
431421

432422

433423
-- | Creates a new 'CAccount'.
434424
genAccount :: CWallet -> Integer -> UberMonad CAccount
435-
genAccount CWallet{..} accountNum = do
425+
genAccount CWallet{..} accountNum =
436426
newAccountIncludeUnready True RandomSeed mempty accountInit
437427
where
438428
accountInit :: CAccountInit

0 commit comments

Comments
 (0)