Struct parity_bip39::Mnemonic
source · pub struct Mnemonic { /* private fields */ }
Expand description
A mnemonic code.
The core::str::FromStr implementation will try to determine the language of the mnemonic from all the supported languages. (Languages have to be explicitly enabled using the Cargo features.)
Supported number of words are 12, 15, 18, 21, and 24.
Implementations§
source§impl Mnemonic
impl Mnemonic
sourcepub fn normalize_utf8_cow<'a>(cow: &mut Cow<'a, str>)
Available on crate feature unicode-normalization
only.
pub fn normalize_utf8_cow<'a>(cow: &mut Cow<'a, str>)
unicode-normalization
only.sourcepub fn from_entropy_in(
language: Language,
entropy: &[u8]
) -> Result<Mnemonic, Error>
pub fn from_entropy_in( language: Language, entropy: &[u8] ) -> Result<Mnemonic, Error>
Create a new Mnemonic in the specified language from the given entropy. Entropy must be a multiple of 32 bits (4 bytes) and 128-256 bits in length.
sourcepub fn from_entropy(entropy: &[u8]) -> Result<Mnemonic, Error>
pub fn from_entropy(entropy: &[u8]) -> Result<Mnemonic, Error>
Create a new English Mnemonic from the given entropy. Entropy must be a multiple of 32 bits (4 bytes) and 128-256 bits in length.
sourcepub fn generate_in_with<R>(
rng: &mut R,
language: Language,
word_count: usize
) -> Result<Mnemonic, Error>
Available on crate feature rand_core
only.
pub fn generate_in_with<R>( rng: &mut R, language: Language, word_count: usize ) -> Result<Mnemonic, Error>
rand_core
only.Generate a new Mnemonic in the given language with the given randomness source. For the different supported word counts, see documentation on Mnemonic.
Example:
use parity_bip39::{Mnemonic, Language};
let mut rng = parity_bip39::rand::thread_rng();
let m = Mnemonic::generate_in_with(&mut rng, Language::English, 24).unwrap();
sourcepub fn generate_in(
language: Language,
word_count: usize
) -> Result<Mnemonic, Error>
Available on crate feature rand
only.
pub fn generate_in( language: Language, word_count: usize ) -> Result<Mnemonic, Error>
rand
only.sourcepub fn generate(word_count: usize) -> Result<Mnemonic, Error>
Available on crate feature rand
only.
pub fn generate(word_count: usize) -> Result<Mnemonic, Error>
rand
only.sourcepub fn word_iter(&self) -> impl Iterator<Item = &'static str> + Clone + '_
👎Deprecated: Use Mnemonic::words instead
pub fn word_iter(&self) -> impl Iterator<Item = &'static str> + Clone + '_
Returns an iterator over the words of the Mnemonic.
sourcepub fn word_indices(&self) -> impl Iterator<Item = usize> + Clone + '_
pub fn word_indices(&self) -> impl Iterator<Item = usize> + Clone + '_
sourcepub fn language_of<S: AsRef<str>>(mnemonic: S) -> Result<Language, Error>
pub fn language_of<S: AsRef<str>>(mnemonic: S) -> Result<Language, Error>
Determine the language of the mnemonic.
NOTE: This method only guarantees that the returned language is the correct language on the assumption that the mnemonic is valid. It does not itself validate the mnemonic.
Some word lists don’t guarantee that their words don’t occur in other word lists. In the extremely unlikely case that a word list can be interpreted in multiple languages, an Error::AmbiguousLanguages is returned, containing the possible languages.
sourcepub fn parse_in_normalized(
language: Language,
s: &str
) -> Result<Mnemonic, Error>
pub fn parse_in_normalized( language: Language, s: &str ) -> Result<Mnemonic, Error>
Parse a mnemonic in normalized UTF8 in the given language.
sourcepub fn parse_in_normalized_without_checksum_check(
language: Language,
s: &str
) -> Result<Mnemonic, Error>
pub fn parse_in_normalized_without_checksum_check( language: Language, s: &str ) -> Result<Mnemonic, Error>
Parse a mnemonic in normalized UTF8 in the given language without checksum check.
It is advised to use this method together with the utility methods
sourcepub fn parse_normalized(s: &str) -> Result<Mnemonic, Error>
pub fn parse_normalized(s: &str) -> Result<Mnemonic, Error>
Parse a mnemonic in normalized UTF8.
sourcepub fn parse_in<'a, S: Into<Cow<'a, str>>>(
language: Language,
s: S
) -> Result<Mnemonic, Error>
Available on crate feature unicode-normalization
only.
pub fn parse_in<'a, S: Into<Cow<'a, str>>>( language: Language, s: S ) -> Result<Mnemonic, Error>
unicode-normalization
only.Parse a mnemonic in the given language.
sourcepub fn parse<'a, S: Into<Cow<'a, str>>>(s: S) -> Result<Mnemonic, Error>
Available on crate feature unicode-normalization
only.
pub fn parse<'a, S: Into<Cow<'a, str>>>(s: S) -> Result<Mnemonic, Error>
unicode-normalization
only.Parse a mnemonic and detect the language from the enabled languages.
sourcepub fn word_count(&self) -> usize
pub fn word_count(&self) -> usize
Get the number of words in the mnemonic.
sourcepub fn to_seed_normalized(&self, normalized_passphrase: &str) -> [u8; 64]
pub fn to_seed_normalized(&self, normalized_passphrase: &str) -> [u8; 64]
Convert to seed bytes with a passphrase in normalized UTF8.
sourcepub fn to_seed<'a, P: Into<Cow<'a, str>>>(&self, passphrase: P) -> [u8; 64]
Available on crate feature unicode-normalization
only.
pub fn to_seed<'a, P: Into<Cow<'a, str>>>(&self, passphrase: P) -> [u8; 64]
unicode-normalization
only.Convert to seed bytes.
sourcepub fn to_entropy_array(&self) -> ([u8; 33], usize)
pub fn to_entropy_array(&self) -> ([u8; 33], usize)
Convert the mnemonic back to the entropy used to generate it.
The return value is a byte array and the size.
Use Mnemonic::to_entropy (needs std
) to get a Vec<u8>
.
sourcepub fn to_entropy(&self) -> Vec<u8>
Available on crate feature alloc
only.
pub fn to_entropy(&self) -> Vec<u8>
alloc
only.Convert the mnemonic back to the entropy used to generate it.
sourcepub fn checksum(&self) -> u8
pub fn checksum(&self) -> u8
Return checksum value for the Mnemonic.
The checksum value is the numerical value of the first self.word_count() / 3
bits of the
SHA256 digest of the Mnemonic’s entropy, and is
encoded by the last word of the mnemonic sentence.
This is useful for validating the integrity of a mnemonic: For a valid mnemonic m
, the
following assertion should hold:
let checksum_width = m.word_count() / 3;
let shift_width = 8 - checksum_width;
assert_eq!(sha256::Hash::hash(&m.to_entropy())[0] >> shift_width, m.checksum());
Note that since this library constrains initialization of Mnemonic
instances through an
API that guarantees validity, all Mnemonic
instances should be valid and the above
condition should hold.
Trait Implementations§
source§impl<'de> Deserialize<'de> for Mnemonic
Available on crate feature serde
only.
impl<'de> Deserialize<'de> for Mnemonic
serde
only.source§fn deserialize<D>(deserializer: D) -> Result<Mnemonic, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Mnemonic, D::Error>where
D: Deserializer<'de>,
source§impl Ord for Mnemonic
impl Ord for Mnemonic
source§impl PartialEq for Mnemonic
impl PartialEq for Mnemonic
source§impl PartialOrd for Mnemonic
impl PartialOrd for Mnemonic
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more