Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Crypto.Encoding.BIP39
Synopsis
- data Entropy (n :: Nat)
- type ValidEntropySize (n :: Nat) = (KnownNat n, NatWithinBound Int n, Elem n '[96, 128, 160, 192, 224, 256])
- data Checksum (bits :: Nat)
- type ValidChecksumSize (ent :: Nat) (csz :: Nat) = (KnownNat csz, NatWithinBound Int csz, Elem csz '[3, 4, 5, 6, 7, 8], CheckSumBits ent ~ csz)
- type family MnemonicWords (n :: Nat) :: Nat where ...
- type family EntropySize (n :: Nat) :: Nat where ...
- toEntropy :: forall n csz ba. (ValidEntropySize n, ValidChecksumSize n csz, ByteArrayAccess ba) => ba -> Either (EntropyError csz) (Entropy n)
- entropyRaw :: Entropy n -> ByteString
- entropyChecksum :: Entropy n -> Checksum (CheckSumBits n)
- entropyToWords :: forall n csz mw. ConsistentEntropy n mw csz => Entropy n -> MnemonicSentence mw
- wordsToEntropy :: forall ent csz mw. ConsistentEntropy ent mw csz => MnemonicSentence mw -> Either (EntropyError csz) (Entropy ent)
- data Seed
- type Passphrase = String
- sentenceToSeed :: ValidMnemonicSentence mw => MnemonicSentence mw -> Dictionary -> Passphrase -> Seed
- phraseToSeed :: ValidMnemonicSentence mw => MnemonicPhrase mw -> Dictionary -> Passphrase -> Seed
- data MnemonicSentence (mw :: Nat)
- data MnemonicPhrase (mw :: Nat)
- type ValidMnemonicSentence (mw :: Nat) = (KnownNat mw, NatWithinBound Int mw, Elem mw '[9, 12, 15, 18, 21, 24])
- mnemonicPhrase :: forall mw. ValidMnemonicSentence mw => [String] -> Either MnemonicWordsError (MnemonicPhrase mw)
- checkMnemonicPhrase :: forall mw. ValidMnemonicSentence mw => Dictionary -> MnemonicPhrase mw -> Bool
- mnemonicPhraseToMnemonicSentence :: forall mw. ValidMnemonicSentence mw => Dictionary -> MnemonicPhrase mw -> Either DictionaryError (MnemonicSentence mw)
- mnemonicSentenceToMnemonicPhrase :: forall mw. ValidMnemonicSentence mw => Dictionary -> MnemonicSentence mw -> MnemonicPhrase mw
- mnemonicSentenceToString :: forall mw. ValidMnemonicSentence mw => Dictionary -> MnemonicSentence mw -> String
- mnemonicSentenceToListN :: MnemonicSentence mw -> ListN mw WordIndex
- mnemonicPhraseToString :: forall mw. ValidMnemonicSentence mw => Dictionary -> MnemonicPhrase mw -> String
- translateTo :: forall mw. ValidMnemonicSentence mw => Dictionary -> Dictionary -> MnemonicPhrase mw -> Either DictionaryError (MnemonicPhrase mw)
- data Dictionary = Dictionary {}
- data WordIndex
- wordIndex :: Offset String -> WordIndex
- unWordIndex :: WordIndex -> Offset String
- type ConsistentEntropy ent mw csz = (ValidEntropySize ent, ValidChecksumSize ent csz, ValidMnemonicSentence mw, MnemonicWords ent ~ mw)
- type family CheckSumBits (n :: Nat) :: Nat where ...
- type family Elem (e :: Nat) (l :: [Nat]) :: Constraint where ...
- data DictionaryError = ErrInvalidDictionaryWord String
- data EntropyError csz
- = ErrInvalidEntropyLength Int Int
- | ErrInvalidEntropyChecksum (Checksum csz) (Checksum csz)
- data MnemonicWordsError = ErrWrongNumberOfWords Int Int
Entropy
data Entropy (n :: Nat) Source #
BIP39's entropy is a byte array of a given size (in bits, see
ValidEntropySize
for the valid size).
To it is associated
Instances
Show (Entropy n) Source # | |
NormalForm (Entropy n) Source # | |
Defined in Crypto.Encoding.BIP39 Methods toNormalForm :: Entropy n -> () # | |
Arbitrary (Entropy 96) Source # | |
Defined in Crypto.Encoding.BIP39 | |
Arbitrary (Entropy 128) Source # | |
Defined in Crypto.Encoding.BIP39 | |
Arbitrary (Entropy 160) Source # | |
Defined in Crypto.Encoding.BIP39 | |
Arbitrary (Entropy 192) Source # | |
Defined in Crypto.Encoding.BIP39 | |
Arbitrary (Entropy 224) Source # | |
Defined in Crypto.Encoding.BIP39 | |
Arbitrary (Entropy 256) Source # | |
Defined in Crypto.Encoding.BIP39 | |
Eq (Entropy n) Source # | |
type ValidEntropySize (n :: Nat) = (KnownNat n, NatWithinBound Int n, Elem n '[96, 128, 160, 192, 224, 256]) Source #
Type Constraint Alias to check a given Nat
is valid for an entropy size
i.e. it must be one of the following: 96, 128, 160, 192, 224, 256.
data Checksum (bits :: Nat) Source #
this is the Checksum
of a given Entropy
the Nat
type parameter represent the size, in bits, of this checksum.
type ValidChecksumSize (ent :: Nat) (csz :: Nat) = (KnownNat csz, NatWithinBound Int csz, Elem csz '[3, 4, 5, 6, 7, 8], CheckSumBits ent ~ csz) Source #
type family MnemonicWords (n :: Nat) :: Nat where ... Source #
Number of Words related to a specific entropy size in bits
Equations
MnemonicWords 96 = 9 | |
MnemonicWords 128 = 12 | |
MnemonicWords 160 = 15 | |
MnemonicWords 192 = 18 | |
MnemonicWords 224 = 21 | |
MnemonicWords 256 = 24 |
type family EntropySize (n :: Nat) :: Nat where ... Source #
Corresponding entropy size in bits for a given number of words
Equations
EntropySize 9 = 96 | |
EntropySize 12 = 128 | |
EntropySize 15 = 160 | |
EntropySize 18 = 192 | |
EntropySize 21 = 224 | |
EntropySize 24 = 256 |
toEntropy :: forall n csz ba. (ValidEntropySize n, ValidChecksumSize n csz, ByteArrayAccess ba) => ba -> Either (EntropyError csz) (Entropy n) Source #
Create a specific entropy type of known size from a raw bytestring
entropyRaw :: Entropy n -> ByteString Source #
Get the raw binary associated with the entropy
entropyChecksum :: Entropy n -> Checksum (CheckSumBits n) Source #
Get the checksum of the Entropy
entropyToWords :: forall n csz mw. ConsistentEntropy n mw csz => Entropy n -> MnemonicSentence mw Source #
Given an entropy of size n, Create a list
wordsToEntropy :: forall ent csz mw. ConsistentEntropy ent mw csz => MnemonicSentence mw -> Either (EntropyError csz) (Entropy ent) Source #
retrieve the initial entropy from a given MnemonicSentence
This function validate the retrieved Entropy
is valid, i.e. that the
checksum is correct.
This means you should not create a new Entropy
from a MnemonicSentence
,
instead, you should use a Random Number Generator to create a new Entropy
.
Seed
type Passphrase = String Source #
Arguments
:: ValidMnemonicSentence mw | |
=> MnemonicSentence mw |
|
-> Dictionary | Dictionary' of words/indexes |
-> Passphrase |
|
-> Seed |
Create a seed from MmemonicSentence
and Passphrase
using the BIP39
algorithm.
Arguments
:: ValidMnemonicSentence mw | |
=> MnemonicPhrase mw |
|
-> Dictionary | Dictionary' of words/indexes |
-> Passphrase |
|
-> Seed |
Create a seed from MmemonicPhrase
and Passphrase
using the BIP39
algorithm.
Mnemonic Sentence
data MnemonicSentence (mw :: Nat) Source #
Mnemonic Sentence is a list of WordIndex
.
This is the generic representation of a mnemonic phrase that can be used for transalating to a different dictionary (example: English to Japanese).
This is mainly used to convert from/to the Entropy
and for cardanoSlSeed
Instances
data MnemonicPhrase (mw :: Nat) Source #
Human readable representation of a MnemonicSentence
Instances
type ValidMnemonicSentence (mw :: Nat) = (KnownNat mw, NatWithinBound Int mw, Elem mw '[9, 12, 15, 18, 21, 24]) Source #
Type Constraint to validate the given Nat
is valid for the supported
MnemonicSentence
mnemonicPhrase :: forall mw. ValidMnemonicSentence mw => [String] -> Either MnemonicWordsError (MnemonicPhrase mw) Source #
checkMnemonicPhrase :: forall mw. ValidMnemonicSentence mw => Dictionary -> MnemonicPhrase mw -> Bool Source #
check a given MnemonicPhrase
is valid for the given Dictionary
mnemonicPhraseToMnemonicSentence :: forall mw. ValidMnemonicSentence mw => Dictionary -> MnemonicPhrase mw -> Either DictionaryError (MnemonicSentence mw) Source #
convert the given MnemonicPhrase
to a generic MnemonicSentence
with the given Dictionary
.
This function assumes the Dictionary
and the MnemonicPhrase
are
compatible (see checkMnemonicPhrase
).
mnemonicSentenceToMnemonicPhrase :: forall mw. ValidMnemonicSentence mw => Dictionary -> MnemonicSentence mw -> MnemonicPhrase mw Source #
convert the given generic MnemonicSentence
to a human readable
MnemonicPhrase
targetting the language of the given Dictionary
.
mnemonicSentenceToString :: forall mw. ValidMnemonicSentence mw => Dictionary -> MnemonicSentence mw -> String Source #
mnemonicSentenceToListN :: MnemonicSentence mw -> ListN mw WordIndex Source #
mnemonicPhraseToString :: forall mw. ValidMnemonicSentence mw => Dictionary -> MnemonicPhrase mw -> String Source #
Arguments
:: forall mw. ValidMnemonicSentence mw | |
=> Dictionary | source dictionary |
-> Dictionary | destination dictionary |
-> MnemonicPhrase mw | |
-> Either DictionaryError (MnemonicPhrase mw) |
translate the given MnemonicPhrase
from one dictionary into another.
This function assumes the source dictionary is compatible with the given
MnemonicPhrase
(see checkMnemonicPhrase
)
Dictionary
data Dictionary Source #
this discribe the property of the Dictionary and will alllow to
convert from a mnemonic phrase to MnemonicSentence
This is especially needed to build the BIP39 Seed
Constructors
Dictionary | |
Fields
|
Index of the mnemonic word in the Dictionary
WordIndex
are within range of [0..2047]
Instances
Bounded WordIndex Source # | |
Enum WordIndex Source # | |
Defined in Crypto.Encoding.BIP39.Dictionary Methods succ :: WordIndex -> WordIndex # pred :: WordIndex -> WordIndex # fromEnum :: WordIndex -> Int # enumFrom :: WordIndex -> [WordIndex] # enumFromThen :: WordIndex -> WordIndex -> [WordIndex] # enumFromTo :: WordIndex -> WordIndex -> [WordIndex] # enumFromThenTo :: WordIndex -> WordIndex -> WordIndex -> [WordIndex] # | |
Show WordIndex Source # | |
NormalForm WordIndex Source # | |
Defined in Crypto.Encoding.BIP39.Dictionary Methods toNormalForm :: WordIndex -> () # | |
Eq WordIndex Source # | |
Ord WordIndex Source # | |
Defined in Crypto.Encoding.BIP39.Dictionary | |
TryFrom Int WordIndex Source # | |
TryFrom (Offset String) WordIndex Source # | |
helpers
type ConsistentEntropy ent mw csz = (ValidEntropySize ent, ValidChecksumSize ent csz, ValidMnemonicSentence mw, MnemonicWords ent ~ mw) Source #
Type Constraint Alias to check the entropy size, the number of mnemonic words and the checksum size is consistent. i.e. that the following is true:
| entropysize | checksumsize | entropysize + checksumsize | mnemonicsize | +---------------+--------------+----------------------------+--------------+ | 96 | 3 | 99 | 9 | | 128 | 4 | 132 | 12 | | 160 | 5 | 165 | 15 | | 192 | 6 | 198 | 18 | | 224 | 7 | 231 | 21 | | 256 | 8 | 264 | 24 |
This type constraint alias also perform all the GHC's cumbersome type level literal handling.
type family CheckSumBits (n :: Nat) :: Nat where ... Source #
Number of bits of checksum related to a specific entropy size in bits
Equations
CheckSumBits 96 = 3 | |
CheckSumBits 128 = 4 | |
CheckSumBits 160 = 5 | |
CheckSumBits 192 = 6 | |
CheckSumBits 224 = 7 | |
CheckSumBits 256 = 8 |
Errors
data DictionaryError Source #
Constructors
ErrInvalidDictionaryWord String |
Instances
Show DictionaryError Source # | |
Defined in Crypto.Encoding.BIP39.Dictionary Methods showsPrec :: Int -> DictionaryError -> ShowS # show :: DictionaryError -> String # showList :: [DictionaryError] -> ShowS # |
data EntropyError csz Source #
Constructors
ErrInvalidEntropyLength Int Int | |
ErrInvalidEntropyChecksum (Checksum csz) (Checksum csz) |
Instances
Show (EntropyError csz) Source # | |
Defined in Crypto.Encoding.BIP39 Methods showsPrec :: Int -> EntropyError csz -> ShowS # show :: EntropyError csz -> String # showList :: [EntropyError csz] -> ShowS # |
data MnemonicWordsError Source #
Constructors
ErrWrongNumberOfWords Int Int |
Instances
Show MnemonicWordsError Source # | |
Defined in Crypto.Encoding.BIP39 Methods showsPrec :: Int -> MnemonicWordsError -> ShowS # show :: MnemonicWordsError -> String # showList :: [MnemonicWordsError] -> ShowS # |