Mnemonic

Struct Mnemonic 

Source
pub struct Mnemonic { /* private fields */ }
Expand description

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_phrase().

You can get the HD wallet Seed from a Mnemonic by calling Seed::new(). From there you can either get the raw byte value with Seed::as_bytes(), or the hex representation using Rust formatting: format!("{:X}", seed).

You can also get the original entropy value back from a Mnemonic with Mnemonic::entropy(), but beware that the entropy value is not the same thing as an HD wallet seed, and should never be used that way.

Implementations§

Source§

impl Mnemonic

Source

pub fn new( mtype: MnemonicType, lang: Language, ) -> Result<Mnemonic, UnspecifiedRandError>

Generates a new Mnemonic

Use Mnemonic::phrase() to get an str slice of the generated phrase.

§Example
use dup_crypto::mnemonic::{Mnemonic, MnemonicType, Language};

let mnemonic = Mnemonic::new(MnemonicType::Words12, Language::English).expect("fail to generate random bytes");
let phrase = mnemonic.phrase();

println!("phrase: {}", phrase);

assert_eq!(phrase.split(" ").count(), 12);
Source

pub fn from_entropy( entropy: &[u8], lang: Language, ) -> Result<Mnemonic, MnemonicError>

Create a Mnemonic from pre-generated entropy

§Example
use dup_crypto::mnemonic::{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, Language::English).unwrap();

assert_eq!("crop cash unable insane eight faith inflict route frame loud box vibrant", mnemonic.phrase());
assert_eq!("33E46BB13A746EA41CDDE45C90846A79", format!("{:X}", mnemonic));
Source

pub fn from_phrase<S>( phrase: S, lang: Language, ) -> Result<Mnemonic, MnemonicError>
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 dup_crypto::mnemonic::{Mnemonic, Language};

let phrase = "park remain person kitchen mule spell knee armed position rail grid ankle";
let mnemonic = Mnemonic::from_phrase(phrase, Language::English).unwrap();

assert_eq!(phrase, mnemonic.phrase());
Source

pub fn validate(phrase: &str, lang: Language) -> Result<(), MnemonicError>

Validate a mnemonic phrase

The phrase supplied will be checked for word length and validated according to the checksum specified in BIP0039.

§Example
use dup_crypto::mnemonic::{Mnemonic, Language};

let test_mnemonic = "park remain person kitchen mule spell knee armed position rail grid ankle";

assert!(Mnemonic::validate(test_mnemonic, Language::English).is_ok());
Source

pub fn phrase(&self) -> &str

Get the mnemonic phrase as a string reference.

Source

pub fn entropy(&self) -> &[u8]

Get the original entropy value of the mnemonic phrase as a slice.

§Example
use dup_crypto::mnemonic::{Mnemonic, Language};

let phrase = "park remain person kitchen mule spell knee armed position rail grid ankle";

let mnemonic = Mnemonic::from_phrase(phrase, Language::English).unwrap();

let entropy: &[u8] = mnemonic.entropy();

Note: You shouldn’t use the generated entropy as secrets, for that generate a new Seed from the Mnemonic.

Source

pub fn language(&self) -> Language

Get the Language

Trait Implementations§

Source§

impl AsRef<str> for Mnemonic

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Debug for Mnemonic

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Mnemonic

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<Mnemonic> for String

Source§

fn from(val: Mnemonic) -> String

Converts to this type from the input type.
Source§

impl LowerHex for Mnemonic

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Mnemonic

Source§

fn eq(&self, other: &Mnemonic) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl UpperHex for Mnemonic

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl StructuralPartialEq for Mnemonic

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.