Skip to content

Commit

Permalink
validate mnemonic in mnemonic_to_hex_seed (iotaledger#568)
Browse files Browse the repository at this point in the history
* validate mnemonic

* add changes file
  • Loading branch information
Thoralf-M authored May 18, 2021
1 parent 8188e45 commit 4b159da
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changes/mnemonic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nodejs-binding": patch
---

Validate mnemonic in mnemonicToHexSeed()
5 changes: 4 additions & 1 deletion iota-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use zeroize::Zeroize;
use crate::builder::TIPS_INTERVAL;
#[cfg(feature = "mqtt")]
use rumqttc::AsyncClient as MqttClient;
#[cfg(any(feature = "mqtt", not(feature = "wasm")))]
#[cfg(feature = "mqtt")]
use tokio::sync::watch::{Receiver as WatchReceiver, Sender as WatchSender};
#[cfg(not(feature = "wasm"))]
use tokio::{
Expand Down Expand Up @@ -558,6 +558,9 @@ impl Client {

/// Returns a hex encoded seed for a mnemonic.
pub fn mnemonic_to_hex_seed(mnemonic: &str) -> Result<String> {
// first we check if the mnemonic is valid to give meaningful errors
crypto::keys::bip39::wordlist::verify(mnemonic, &crypto::keys::bip39::wordlist::ENGLISH)
.map_err(|e| crate::Error::InvalidMnemonic(format!("{:?}", e)))?;
let mut mnemonic_seed = [0u8; 64];
mnemonic_to_seed(mnemonic, &"", &mut mnemonic_seed);
Ok(hex::encode(mnemonic_seed))
Expand Down
3 changes: 3 additions & 0 deletions iota-client/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,7 @@ pub enum Error {
/// Tokio task join error
#[error("{0}")]
TaskJoinError(#[from] tokio::task::JoinError),
/// Invalid mnemonic error
#[error("Invalid mnemonic {0}")]
InvalidMnemonic(String),
}
14 changes: 14 additions & 0 deletions iota-client/tests/mnemonic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use iota_client::{Client, Result};

#[tokio::test]
async fn mnemonic() -> Result<()> {
let mnemonic = Client::generate_mnemonic()?;
assert!(Client::mnemonic_to_hex_seed(&mnemonic).is_ok());
assert!(Client::mnemonic_to_hex_seed("until fire hat mountain zoo grocery real deny advance change marble taste goat ivory wheat bubble panic banner tattoo client ticket action race rocket").is_ok());
assert!(Client::mnemonic_to_hex_seed("fire until hat mountain zoo grocery real deny advance change marble taste goat ivory wheat bubble panic banner tattoo client ticket action race rocket").is_err());
assert!(Client::mnemonic_to_hex_seed("invalid mnemonic").is_err());
Ok(())
}

0 comments on commit 4b159da

Please sign in to comment.