Struct iop_keyvault::Bip39
source · [−]pub struct Bip39 { /* private fields */ }
Expand description
Tool for generating, validating and parsing BIP39 phrases in different supported languages.
Implementations
sourceimpl Bip39
impl Bip39
sourcepub const MNEMONIC_WORDS: usize = 24usize
pub const MNEMONIC_WORDS: usize = 24usize
Number of words in the BIP39 mnemonics we accept and generate
sourcepub fn generate_entropy() -> Result<[u8; 32]>
pub fn generate_entropy() -> Result<[u8; 32]>
Creates the right sized entropy using the CSPRNG available on the platform.
sourcepub fn language(lang: Bip39Language) -> Self
pub fn language(lang: Bip39Language) -> Self
Creates a new BIP39 phrase handler with the given language
sourcepub fn language_code(code: impl AsRef<str>) -> Result<Self>
pub fn language_code(code: impl AsRef<str>) -> Result<Self>
Creates a new BIP39 phrase handler with the given language code
Example
let bip39 = Bip39::language_code("fr").unwrap();
assert!(bip39.check_word("aménager"));
sourcepub fn generate(self) -> Bip39Phrase
pub fn generate(self) -> Bip39Phrase
Creates a new phrase from hardware entropy. Cannot be used from WASM because it uses the system random generator.
Example
let phrase: &str = Bip39::language_code("fr").unwrap().generate().as_phrase();
let seed: Seed = Bip39::new().generate().password("");
sourcepub fn check_word(self, word: &str) -> bool
pub fn check_word(self, word: &str) -> bool
Checks if a word is present in the BIP39 dictionary
Example
let bip39 = Bip39::new();
assert!(bip39.check_word("abandon"));
assert!(!bip39.check_word("Abandon"));
assert!(!bip39.check_word("avalon"));
sourcepub fn list_words(self, prefix: impl AsRef<str>) -> &'static [&'static str]
pub fn list_words(self, prefix: impl AsRef<str>) -> &'static [&'static str]
Lists all words in the BIP39 dictionary, which start with the given prefix.
Can be used in 3 different ways:
- When the prefix is empty, the sorted list of all words are returned
- When the prefix is a partial word, the returned list can be used for auto-completion
- When the returned list is empty, the prefix is not a valid word in the dictionary
Example
let bip39 = Bip39::language(Bip39Language::English);
assert_eq!(bip39.list_words("").len(), 2048);
assert_eq!(bip39.list_words("woo"), ["wood", "wool"]);
assert!(bip39.list_words("woof").is_empty());
sourcepub fn validate(self, phrase: impl AsRef<str>) -> Result<()>
pub fn validate(self, phrase: impl AsRef<str>) -> Result<()>
Validates a whole BIP39 mnemonic phrase. Because the phrase contains some checksum, the whole phrase can be invalid even when each word itself is valid.
Example
let bip39 = Bip39::new();
assert!(bip39.validate("type shield target dream feature surface search flee tenant cake taxi shrug").is_ok());
assert!(bip39.validate("abandon abandon about").unwrap_err().to_string().contains("invalid number of words"));
sourcepub fn phrase(self, phrase: impl AsRef<str>) -> Result<Bip39Phrase>
pub fn phrase(self, phrase: impl AsRef<str>) -> Result<Bip39Phrase>
Validates a whole BIP39 mnemonic phrase and returns an intermediate object that can be
later converted into a Seed
with an optional password.
sourcepub fn short_phrase(self, phrase: impl AsRef<str>) -> Result<Bip39Phrase>
pub fn short_phrase(self, phrase: impl AsRef<str>) -> Result<Bip39Phrase>
Use the Self::phrase()
method whenever possible. This method allows some legacy use-cases to
provide mnemonics shorter than Self::MNEMONIC_WORDS
words.
sourcepub fn entropy(self, entropy: impl AsRef<[u8]>) -> Result<Bip39Phrase>
pub fn entropy(self, entropy: impl AsRef<[u8]>) -> Result<Bip39Phrase>
Creates a BIP39 phrase based on the 256 bits of entropy provided. This method is needed from WASM
because Self::generate()
uses system resources unavailable from WASM.
sourcepub fn short_entropy(self, entropy: impl AsRef<[u8]>) -> Result<Bip39Phrase>
pub fn short_entropy(self, entropy: impl AsRef<[u8]>) -> Result<Bip39Phrase>
Use the [‘entropy()’] method whenever possible. This method allows some legacy use-cases to provide mnemonics with low entropy.
Trait Implementations
impl Copy for Bip39
Auto Trait Implementations
impl RefUnwindSafe for Bip39
impl Send for Bip39
impl Sync for Bip39
impl Unpin for Bip39
impl UnwindSafe for Bip39
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more