Skip to content

Commit

Permalink
Move BIP39 module to separate package
Browse files Browse the repository at this point in the history
From [input-ouput-hk/cardano-wallet#55](cardano-foundation/cardano-wallet#55):
> We've moved the wallet into its own repository as a first step towards decoupling. Now, the cardano-wallet uses cardano-sl as a peer dependency and rely on it for quite a few things. However, a few modules in cardano-sl also relies on code inside the wallet. This creates some circular dependencies between both repo.

> One of those dependencies is actually the BIP39-ish implementation from the cardano-wallet used in a few scripts and executable currently located on cardano-sl.

1. Creates new package `mnemonic`
2. Moves `BIP39` module & spec from `wallet-new` to `mnemonic`
3. Point BIP39 imports from faucet and wallet-new to the new location
4. Make scripts/generate-mnemonic.hs an executable instead.
  • Loading branch information
Anviking committed Nov 15, 2018
1 parent ad7dc40 commit 7468886
Show file tree
Hide file tree
Showing 19 changed files with 275 additions and 31 deletions.
1 change: 1 addition & 0 deletions faucet/cardano-sl-faucet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ library
, cardano-sl-core
, cardano-sl-crypto
, cardano-sl-util
, cardano-sl-mnemonic
, cardano-sl-wallet-new
, connection
, cryptonite
Expand Down
3 changes: 1 addition & 2 deletions faucet/src/Cardano/Faucet/Init.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import qualified System.Metrics.Gauge as Gauge
import System.Wlog (CanLog, HasLoggerName, LoggerNameBox (..),
liftLogIO, logDebug, logError, logInfo, withSublogger)

import Cardano.Mnemonic (Mnemonic, entropyToMnemonic, genEntropy)
import Cardano.Wallet.API.V1.Types (Account (..), Address,
AssuranceLevel (NormalAssurance), BackupPhrase (..),
ForceNtpCheck (..), NewWallet (..), NodeInfo (..),
Expand All @@ -60,8 +61,6 @@ import Cardano.Wallet.API.V1.Types (Account (..), Address,
import Cardano.Wallet.Client (ClientError (..), WalletClient (..),
WalletResponse (..), liftClient)
import Cardano.Wallet.Client.Http (mkHttpClient)
import Cardano.Wallet.Kernel.BIP39 (Mnemonic, entropyToMnemonic,
genEntropy)
import Pos.Core (Coin (..))
import Universum

Expand Down
2 changes: 1 addition & 1 deletion faucet/src/Cardano/Faucet/Types/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ import System.Metrics.Counter (Counter)
import System.Metrics.Gauge (Gauge)
import System.Remote.Monitoring.Statsd (StatsdOptions (..))

import Cardano.Mnemonic (Mnemonic)
import Cardano.Wallet.API.V1.Types (AccountIndex, Payment,
PaymentSource (..), V1, WalletId (..))
import Cardano.Wallet.Client (ClientError (..), WalletClient (..))
import Cardano.Wallet.Kernel.BIP39 (Mnemonic)
import Pos.Core (Address (..))
import Test.QuickCheck (Arbitrary (..), choose)
import Test.QuickCheck.Arbitrary.Generic
Expand Down
20 changes: 20 additions & 0 deletions mnemonic/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2018 IOHK

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to
do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

8 changes: 8 additions & 0 deletions mnemonic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Cardano-SL-Mnemonic
Custom Mnemonic module, previously located in cardano-sl-wallet-new (now cardano-wallet).

It includes an executable too:
```shell
$ stack exec cardano-sl-generate-mnemonic
["squirrel","material","silly","twice","direct","slush","pistol","razor","become","junk","kingdom","flee"]
```
91 changes: 91 additions & 0 deletions mnemonic/cardano-sl-mnemonic.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: cardano-sl-mnemonic
version: 2.0.0
synopsis: TODO
description: See README
homepage: https://github.com/input-output-hk/cardano-sl/mnemonic/README.md
author: IOHK Engineering Team
maintainer: operations@iohk.io
copyright: 2018 IOHK
license: MIT
license-file: LICENSE
category: Data
build-type: Simple
extra-source-files: README.md
cabal-version: >=1.10

library
default-language: Haskell2010
default-extensions: DeriveGeneric
LambdaCase
NoImplicitPrelude
OverloadedStrings
TupleSections
TypeApplications
ScopedTypeVariables

hs-source-dirs: src

build-depends: base >=4.7 && <5
, QuickCheck
, aeson
, basement
, bytestring
, cardano-crypto
, cardano-sl
, cardano-sl-core
, cardano-sl-crypto
, cardano-sl-infra
, cryptonite
, data-default
, formatting
, lens
, memory
, swagger2
, text
, time
, universum

exposed-modules: Cardano.Mnemonic


test-suite cardano-sl-mnemonic-test
default-language: Haskell2010

type: exitcode-stdio-1.0
default-extensions: DeriveGeneric
LambdaCase
NoImplicitPrelude
OverloadedStrings
TupleSections
TypeApplications
ScopedTypeVariables

hs-source-dirs: test
main-is: Main.hs
other-modules: Cardano.MnemonicSpec

build-depends: base
, hspec
, universum
, cardano-sl-mnemonic
, bytestring
, QuickCheck
, cardano-sl-crypto
, data-default
, aeson
, cardano-crypto


executable cardano-generate-mnemonic
main-is: Main.hs
hs-source-dirs: generate-mnemonic
ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall
build-depends:
base
, aeson
, cardano-sl-mnemonic
, bytestring
, text
, universum
default-language: Haskell2010
default-extensions: NoImplicitPrelude
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#!/usr/bin/env nix-shell
#!nix-shell -i runhaskell ../wallet-new/default.nix
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeApplications #-}
import Cardano.Wallet.Kernel.BIP39 (EntropySize, Mnemonic,
entropyToMnemonic, genEntropy)

import Universum

import Cardano.Mnemonic (Mnemonic, entropyToMnemonic, genEntropy)
import Data.Aeson (encode)
import Data.ByteString.Lazy.Char8 (unpack)

main :: IO ()
main = do
backupPhrase <- generateBackupPhrase
let backupPhraseString = backupPhraseToString backupPhrase
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
-- | Module providing restoring from backup phrase functionality
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Cardano.Wallet.Kernel.BIP39
module Cardano.Mnemonic
(
-- * Types
Mnemonic
Expand Down Expand Up @@ -41,16 +45,16 @@ import Control.Lens ((?~))
import Crypto.Encoding.BIP39
import Crypto.Hash (Blake2b_256, Digest, hash)
import Data.Aeson (FromJSON (..), ToJSON (..))
import Data.Aeson.Types (Parser)
import Data.ByteArray (constEq, convert)
import Data.ByteString (ByteString)
import Data.Default (Default (def))
import Data.Swagger (NamedSchema (..), ToSchema (..), maxItems,
minItems)
import Formatting (bprint, build, (%))
import Formatting (bprint, build, formatToString, (%))
import Test.QuickCheck (Arbitrary (..))
import Test.QuickCheck.Gen (vectorOf)

import Cardano.Wallet.Util (eitherToParser)
import Pos.Binary (serialize')
import Pos.Crypto (AesKey (..))
import Pos.Infra.Util.LogSafe (SecureLog)
Expand Down Expand Up @@ -374,3 +378,13 @@ instance (KnownNat mw) => ToSchema (Mnemonic mw) where
return $ NamedSchema (Just "Mnemonic") schema
& minItems ?~ fromIntegral mw
& maxItems ?~ fromIntegral mw


--
-- Miscellaneous
--

-- | Convert a given Either to an Aeson Parser
eitherToParser :: Buildable a => Either a b -> Parser b
eitherToParser =
either (fail . formatToString build) pure
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Cardano.Wallet.Kernel.BIP39Spec where
module Cardano.MnemonicSpec where

import Universum

import Cardano.Wallet.Kernel.BIP39
import Cardano.Mnemonic
import Crypto.Encoding.BIP39 (toEntropy)
import Data.Default (def)
import Pos.Crypto (AesKey (..))
Expand Down
12 changes: 12 additions & 0 deletions mnemonic/test/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Main where

import Universum

import Test.Hspec

import qualified Cardano.MnemonicSpec


main :: IO ()
main = hspec $ do
Cardano.MnemonicSpec.spec
Loading

0 comments on commit 7468886

Please sign in to comment.