Struct bip39::Mnemonic
[−]
[src]
pub struct Mnemonic { /* fields omitted */ }
The primary type in this crate, most tasks require creating or using one.
To create a new Mnemonic
from a randomly generated key, call Mnemonic::new()
.
To get a Mnemonic
instance for an existing mnemonic phrase, including
those generated by other software or hardware wallets, use Mnemonic::from_string()
.
You can get the HD wallet Seed
from a Mnemonic
by calling Mnemonic::get_seed()
,
from there you can either get the raw byte value with Seed::as_bytes()
, or the hex
representation with Seed::as_hex()
.
You can also get the original entropy value back from a Mnemonic
with Mnemonic::to_entropy()
,
but beware that the entropy value is not the same thing as an HD wallet seed, and should
never be used that way.
Methods
impl Mnemonic
[src]
fn new<S>(
mnemonic_type: MnemonicType,
lang: Language,
password: S
) -> Result<Mnemonic, Error> where
S: Into<String>,
[src]
mnemonic_type: MnemonicType,
lang: Language,
password: S
) -> Result<Mnemonic, Error> where
S: Into<String>,
Generates a new Mnemonic
Can be used to get the Seed
using Mnemonic::get_seed()
.
Can also be used to get the original entropy value. Use Mnemonic::as_entropy()
for a slice, or
Mnemonic::get_entropy() for an owned Vec<u8>
.
Example
use bip39::{Mnemonic, MnemonicType, Language}; let mnemonic_type = MnemonicType::for_word_count(12).unwrap(); let mnemonic = match Mnemonic::new(mnemonic_type, Language::English, "") { Ok(b) => b, Err(e) => { println!("e: {}", e); return } }; let phrase = mnemonic.get_string(); let seed = mnemonic.get_seed(); let seed_bytes: &[u8] = seed.as_bytes(); let seed_hex: &str = seed.as_hex(); println!("phrase: {}", phrase);
fn from_entropy<S>(
entropy: &[u8],
mnemonic_type: MnemonicType,
lang: Language,
password: S
) -> Result<Mnemonic, Error> where
S: Into<String>,
[src]
entropy: &[u8],
mnemonic_type: MnemonicType,
lang: Language,
password: S
) -> Result<Mnemonic, Error> where
S: Into<String>,
Create a Mnemonic
from generated entropy
Example
use bip39::{Mnemonic, MnemonicType, Language}; let entropy = &[0x33, 0xE4, 0x6B, 0xB1, 0x3A, 0x74, 0x6E, 0xA4, 0x1C, 0xDD, 0xE4, 0x5C, 0x90, 0x84, 0x6A, 0x79]; let mnemonic = Mnemonic::from_entropy(entropy, MnemonicType::for_key_size(128).unwrap(), Language::English, "").unwrap(); assert_eq!("crop cash unable insane eight faith inflict route frame loud box vibrant", mnemonic.as_str());
fn from_entropy_hex<S>(
entropy: &str,
mnemonic_type: MnemonicType,
lang: Language,
password: S
) -> Result<Mnemonic, Error> where
S: Into<String>,
[src]
entropy: &str,
mnemonic_type: MnemonicType,
lang: Language,
password: S
) -> Result<Mnemonic, Error> where
S: Into<String>,
Create a Mnemonic
from generated entropy hexadecimal representation
Example
use bip39::{Mnemonic, MnemonicType, Language}; let entropy = "33E46BB13A746EA41CDDE45C90846A79"; let mnemonic = Mnemonic::from_entropy_hex(entropy, MnemonicType::for_key_size(128).unwrap(), Language::English, "").unwrap(); assert_eq!("crop cash unable insane eight faith inflict route frame loud box vibrant", mnemonic.as_str());
fn from_string<S>(
string: S,
lang: Language,
password: S
) -> Result<Mnemonic, Error> where
S: Into<String>,
[src]
string: S,
lang: Language,
password: S
) -> Result<Mnemonic, Error> where
S: Into<String>,
Create a Mnemonic
from an existing mnemonic phrase
The phrase supplied will be checked for word length and validated according to the checksum specified in BIP0039
Example
use bip39::{Mnemonic, Language}; let test_mnemonic = "park remain person kitchen mule spell knee armed position rail grid ankle"; let mnemonic = Mnemonic::from_string(test_mnemonic, Language::English, "").unwrap();
fn validate<S>(string: S, lang: Language) -> Result<(), Error> where
S: Into<String>,
[src]
S: Into<String>,
Validate a mnemonic phrase
The phrase supplied will be checked for word length and validated according to the checksum specified in BIP0039
Note: you cannot use this function to determine anything more than whether the mnemonic
phrase itself is intact, it does not check the password or compute the seed value. For that,
you should use Mnemonic::from_string()
.
Example
use bip39::{Mnemonic, Language}; let test_mnemonic = "park remain person kitchen mule spell knee armed position rail grid ankle"; match Mnemonic::validate(test_mnemonic, Language::English) { Ok(_) => { println!("valid: {}", test_mnemonic); }, Err(e) => { println!("e: {}", e); return } }
fn get_entropy(&self) -> Vec<u8>
[src]
Get the original entropy value of the mnemonic phrase as an owned Vec
Example
use bip39::{Mnemonic, Language}; let test_mnemonic = "park remain person kitchen mule spell knee armed position rail grid ankle"; let mnemonic = Mnemonic::from_string(test_mnemonic, Language::English, "").unwrap(); let entropy: Vec<u8> = mnemonic.get_entropy();
Note: this function clones the internal entropy bytes
fn as_str(&self) -> &str
[src]
Get the mnemonic phrase as a string reference
fn get_string(&self) -> String
[src]
Get the mnemonic phrase as an owned string
Note: this clones the internal Mnemonic String instance
fn get_language(&self) -> Language
[src]
Get the Language
fn as_seed(&self) -> &Seed
[src]
Get a reference to the internal Seed
fn get_seed(&self) -> Seed
[src]
fn get_entropy_hex(&self) -> String
[src]
Get the original entropy used to create the Mnemonic as a hex string
Note: this allocates a new String
fn as_entropy(&self) -> &[u8]
[src]
Get the original entropy value of the mnemonic phrase as a slice
Example
use bip39::{Mnemonic, Language}; let test_mnemonic = "park remain person kitchen mule spell knee armed position rail grid ankle"; let mnemonic = Mnemonic::from_string(test_mnemonic, Language::English, "").unwrap(); let entropy: &[u8] = mnemonic.as_entropy();
Note: this function clones the internal entropy bytes
Trait Implementations
impl Debug for Mnemonic
[src]
impl Clone for Mnemonic
[src]
fn clone(&self) -> Mnemonic
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more