Kryptonite v0.1.8 Kryptonite.Bip39 View Source
This module allows for easy transformation between a binary and a mnemonic phrase.
Its main limitation is the fact that the data must have a byte size that is a multiple of 4 and comprised between 4 and 1024.
Link to this section Summary
Functions
Builds a Bip39 structure given some data
. Note that the data length must
follow the same rules as for generate/1
Builds a Bip39 structure given a wordlist. Each word is verified to exist and the checksum is verified against the rebuilt data
Generates random entropy of the given size_in_bytes
and its associated wording.
Note that the specified size must be a multiple of four
Link to this section Types
The t()
type exposes a mnemonic construct.
Link to this section Functions
Builds a Bip39 structure given some data
. Note that the data length must
follow the same rules as for generate/1
.
Returns a t()
or {:error, atom}
.
Examples
iex> seed = "Hey."
iex> %Kryptonite.Bip39{data: data} = from_data(seed)
iex> seed == data
true
iex> from_data(<<0, 0, 0, 0>>).words
"abandon abandon ability"
iex> from_data(<<255, 255, 255, 255, 255, 255, 255, 255>>).words
"zoo zoo zoo zoo zoo zebra"
iex> from_data(<<1, 2, 3>>)
{:error, :invalid_data_size}
iex> from_data(<<1, 2, 3, 4, 5>>)
{:error, :invalid_data_size}
Builds a Bip39 structure given a wordlist. Each word is verified to exist and the checksum is verified against the rebuilt data.
Returns t()
or {:error, atom}
.
Examples
iex> seed = "abandon abandon ability"
iex> %Kryptonite.Bip39{words: words, data: data} = from_words(seed)
iex> seed == words && data == <<0, 0, 0, 0>>
true
iex> seed = "zoo zoo zoo zoo zoo zebra"
iex> from_words(seed).data
<<255, 255, 255, 255, 255, 255, 255, 255>>
iex> from_words("foobar")
{:error, :invalid_word}
iex> from_words("random word nothing work")
{:error, :invalid_checksum}
generate(pos_integer()) :: t() | {:error, atom()}
Generates random entropy of the given size_in_bytes
and its associated wording.
Note that the specified size must be a multiple of four.
Returns a t()
or {:error, atom}
.
Examples
iex> %Kryptonite.Bip39{data: data, words: words} = generate(4)
iex> byte_size(data) == 4 && bit_size(data) == 32
true
iex> String.length(words) != 0
true
iex> generate(3)
{:error, :invalid_data_size}
iex> generate(11)
{:error, :invalid_data_size}