polkascan / py-bip39-bindings

Python bindings for tiny-bip39 RUST crate
Apache License 2.0
4 stars 8 forks source link

Add support for multi-language #6

Closed vtexier closed 2 years ago

vtexier commented 2 years ago

The library does only handle english mnemonic. When I supply a valid 128bits Bip39 french mnemonic of 12 words, the function bip39_to_mini_secret fails with "Invalid mnemonic".

Because it is hard coded with english:

    let mnemonic = match Mnemonic::from_phrase(phrase, Language::English) {
        Ok(some_mnemomic) => some_mnemomic,
        Err(err) => return Err(exceptions::ValueError::py_err(format!("Invalid mnemonic: {}", err.to_string())))
    };

Could it be possible to handle multi-language, adding an optional language argument with Language::English default to all functions?

arjanz commented 2 years ago

In the RUST-crate I see support for several languages (including French) and to retrieve it by a language-code string, so that would be possible:

pub fn from_language_code(language_code: &str) -> Option<Self> {
        match &language_code.to_ascii_lowercase()[..] {
            "en" => Some(Language::English),
            #[cfg(feature = "chinese-simplified")]
            "zh-hans" => Some(Language::ChineseSimplified),
            #[cfg(feature = "chinese-traditional")]
            "zh-hant" => Some(Language::ChineseTraditional),
            #[cfg(feature = "french")]
            "fr" => Some(Language::French),
            #[cfg(feature = "italian")]
            "it" => Some(Language::Italian),
            #[cfg(feature = "japanese")]
            "ja" => Some(Language::Japanese),
            #[cfg(feature = "korean")]
            "ko" => Some(Language::Korean),
            #[cfg(feature = "spanish")]
            "es" => Some(Language::Spanish),
            _ => None,
        }
    }

I'll look into it later this week, shouldn't be too much effort.