Exemplo n.º 1
0
        /// <summary>
        /// Takes in the words of a mnemonic sentence and it rebuilds the word index, having the valid index allows us to hot swap between languages/word lists :)
        /// </summary>
        /// <param name="wordsInMnemonicSentence">
        /// a string array containing each word in the mnemonic sentence
        /// </param>
        /// <returns>
        /// The word index that can be used to build the mnemonic sentence
        /// </returns>
        private List <int> RebuildWordIndexes(string[] wordsInMnemonicSentence)
        {
            List <int> wordIndexList = new List <int>();
            string     langName      = CEmptyString;

            Wordlist wordlist;

            switch (this.language)
            {
            case Language.English:
                wordlist = new English();
                langName = "English";
                break;

            case Language.Japanese:
                wordlist = new Japanese();
                langName = "Japanese";
                break;

            case Language.Spanish:
                wordlist = new Spanish();
                langName = "Spanish";
                break;

            case Language.ChineseSimplified:
                wordlist = new ChineseSimplified();
                langName = "Chinese Simplified";
                break;

            case Language.ChineseTraditional:
                wordlist = new ChineseTraditional();
                langName = "Chinese Traditional";
                break;

            case Language.French:
                wordlist = new French();
                langName = "French";
                break;

            default:
                wordlist = new English();
                langName = "English";
                break;
            }

            foreach (string s in wordsInMnemonicSentence)
            {
                int idx = -1;

                if (!wordlist.WordExists(s, out idx))
                {
                    throw new MnemonicException("Word " + s + " is not in the wordlist for language " + langName + " cannot continue to rebuild entropy from wordlist");
                }

                wordIndexList.Add(idx);
            }

            return(wordIndexList);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Uses the Wordlist Index to create a sentence ow words provided by the wordlist of this objects language attribute
        /// </summary>
        /// <returns>A sentence of words</returns>
        private string GetMnemonicSentence()
        {
            // trap for words that were not in the word list when built. If custom words were used, we will not support the rebuild as we don't have the words
            if (this.wordIndexList.Contains(-1))
            {
                throw new MnemonicException("the wordlist index contains -1 which means words were used in the mnemonic sentence that cannot be found in the wordlist and the index to sentence feature cannot be used. Perhaps a different language wordlist is needed?");
            }

            string   mSentence = CEmptyString;
            Wordlist wordlist;

            switch (this.language)
            {
            case Language.English:
                wordlist = new English();
                break;

            case Language.Japanese:
                wordlist = new Japanese();
                break;

            case Language.Spanish:
                wordlist = new Spanish();
                break;

            case Language.ChineseSimplified:
                wordlist = new ChineseSimplified();
                break;

            case Language.ChineseTraditional:
                wordlist = new ChineseTraditional();
                break;

            case Language.French:
                wordlist = new French();
                break;

            default:
                wordlist = new English();
                break;
            }

            for (int i = 0; i < this.wordIndexList.Count; i++)
            {
                mSentence += wordlist.GetWordAtIndex(this.wordIndexList[i]);
                if (i + 1 < this.wordIndexList.Count)
                {
                    mSentence += " ";
                }
            }

            return(mSentence);
        }
Exemplo n.º 3
0
        public static Language AutoDetectLanguage(string[] words)
        {
            var languageCount = new List <int>(new[] { 0, 0, 0, 0, 0, 0, 0 });
            int index;

            foreach (var s in words)
            {
                if (English.WordExists(s, out index))
                {
                    //english is at 0
                    languageCount[0]++;
                }

                if (Japanese.WordExists(s, out index))
                {
                    //japanese is at 1
                    languageCount[1]++;
                }

                if (Spanish.WordExists(s, out index))
                {
                    //spanish is at 2
                    languageCount[2]++;
                }

                if (ChineseSimplified.WordExists(s, out index))
                {
                    //chinese simplified is at 3
                    languageCount[3]++;
                }

                if (ChineseTraditional.WordExists(s, out index) && !ChineseSimplified.WordExists(s, out index))
                {
                    //chinese traditional is at 4
                    languageCount[4]++;
                }

                if (French.WordExists(s, out index))
                {
                    languageCount[5]++;
                }

                if (PortugueseBrazil.WordExists(s, out index))
                {
                    //portuguese_brazil is at 6
                    languageCount[6]++;
                }
            }

            //no hits found for any language unknown
            if (languageCount.Max() == 0)
            {
                return(Language.Unknown);
            }

            if (languageCount.IndexOf(languageCount.Max()) == 0)
            {
                return(Language.English);
            }

            if (languageCount.IndexOf(languageCount.Max()) == 1)
            {
                return(Language.Japanese);
            }

            if (languageCount.IndexOf(languageCount.Max()) == 2)
            {
                return(Language.Spanish);
            }

            if (languageCount.IndexOf(languageCount.Max()) == 3)
            {
                if (languageCount[4] > 0)
                {
                    //has traditional characters so not simplified but instead traditional
                    return(Language.ChineseTraditional);
                }

                return(Language.ChineseSimplified);
            }

            if (languageCount.IndexOf(languageCount.Max()) == 4)
            {
                return(Language.ChineseTraditional);
            }

            if (languageCount.IndexOf(languageCount.Max()) == 5)
            {
                return(Language.French);
            }

            if (languageCount.IndexOf(languageCount.Max()) == 6)
            {
                return(Language.PortugueseBrazil);
            }

            return(Language.Unknown);
        }
Exemplo n.º 4
0
        /////// <summary>
        /////// An asynchronous static method to create a new BIP39 from random entropy. The random entropy creation is CPU intensive so is run in its own Task and we await as per async pattern.
        /////// </summary>
        /////// <param name="entropySize">The size in bits of the entropy to be created</param>
        /////// <param name="passphrase">The optional passphrase. Please ensure NFKD Normalized, Empty string will be used if not provided as per spec</param>
        /////// <param name="language">The optional language. If no language is provided English will be used</param>
        /////// <returns>A BIP39 object</returns>
        ////public static async Task<BIP39> GetBIP39Async(int entropySize = cMinimumEntropyBits, string passphrase = cEmptyString, Language language = Language.English)
        ////{
        ////    byte[] entropyBytes = await Utilities.GetRandomBytesAsync(entropySize / cBitsInByte);
        ////    return new BIP39(entropyBytes, passphrase, language);
        ////}

        /// <summary>
        /// Takes in a string[] of words and detects the language that has the highest number of matching words.
        /// </summary>
        /// <param name="words">
        /// The words of which you wish to derive a language
        /// </param>
        /// <returns>
        /// The best attempt at a guessed Language
        /// </returns>
        public static Language AutoDetectLanguageOfWords(string[] words)
        {
            English            eng = new English();
            Japanese           jp  = new Japanese();
            Spanish            es  = new Spanish();
            French             fr  = new French();
            ChineseSimplified  cnS = new ChineseSimplified();
            ChineseTraditional cnT = new ChineseTraditional();

            List <int> languageCount = new List <int>(new[] { 0, 0, 0, 0, 0, 0 });
            int        index;

            foreach (string s in words)
            {
                if (eng.WordExists(s, out index))
                {
                    // english is at 0
                    languageCount[0]++;
                }

                if (jp.WordExists(s, out index))
                {
                    // japanese is at 1
                    languageCount[1]++;
                }

                if (es.WordExists(s, out index))
                {
                    // spanish is at 2
                    languageCount[2]++;
                }

                if (cnS.WordExists(s, out index))
                {
                    // chinese simplified is at 3
                    languageCount[3]++;
                }

                if (cnT.WordExists(s, out index) && !cnS.WordExists(s, out index))
                {
                    // chinese traditional is at 4
                    languageCount[4]++;
                }

                if (fr.WordExists(s, out index))
                {
                    // french is at 5
                    languageCount[5]++;
                }
            }

            // no hits found for any language unknown
            if (languageCount.Max() == 0)
            {
                return(Language.Unknown);
            }

            if (languageCount.IndexOf(languageCount.Max()) == 0)
            {
                return(Language.English);
            }

            if (languageCount.IndexOf(languageCount.Max()) == 1)
            {
                return(Language.Japanese);
            }

            if (languageCount.IndexOf(languageCount.Max()) == 2)
            {
                return(Language.Spanish);
            }

            if (languageCount.IndexOf(languageCount.Max()) == 3)
            {
                if (languageCount[4] > 0)
                {
                    // has traditional characters so not simplified but instead traditional
                    return(Language.ChineseTraditional);
                }

                return(Language.ChineseSimplified);
            }

            if (languageCount.IndexOf(languageCount.Max()) == 4)
            {
                return(Language.ChineseTraditional);
            }

            if (languageCount.IndexOf(languageCount.Max()) == 5)
            {
                return(Language.French);
            }

            return(Language.Unknown);
        }
Exemplo n.º 5
0
        public static Language AutoDetectLanguage(string[] words)
        {
            List <int> languageCount = new List <int>(new int[] { 0, 0, 0, 0, 0, 0, 0 });
            int        index;

            foreach (string s in words)
            {
                if (Japanese.WordExists(s, out index))
                {
                    languageCount[1]++;
                }
                if (Spanish.WordExists(s, out index))
                {
                    //spanish is at 2
                    languageCount[2]++;
                }
                if (ChineseSimplified.WordExists(s, out index))
                {
                    languageCount[3]++;
                }
                if (French.WordExists(s, out index))
                {
                    languageCount[5]++;
                }
            }
            if (languageCount.Max() == 0)
            {
                return(Language.Unknown);
            }
            if (languageCount.IndexOf(languageCount.Max()) == 0)
            {
                return(Language.English);
            }
            else if (languageCount.IndexOf(languageCount.Max()) == 1)
            {
                return(Language.Japanese);
            }
            else if (languageCount.IndexOf(languageCount.Max()) == 2)
            {
                return(Language.Spanish);
            }
            else if (languageCount.IndexOf(languageCount.Max()) == 3)
            {
                if (languageCount[4] > 0)
                {
                    return(Language.ChineseTraditional);
                }
                return(Language.ChineseSimplified);
            }
            else if (languageCount.IndexOf(languageCount.Max()) == 4)
            {
                return(Language.ChineseTraditional);
            }
            else if (languageCount.IndexOf(languageCount.Max()) == 5)
            {
                return(Language.French);
            }
            else if (languageCount.IndexOf(languageCount.Max()) == 6)
            {
                return(Language.PortugueseBrazil);
            }
            return(Language.Unknown);
        }