Example #1
0
        /// <summary>
        /// Generate a mnemonic
        /// </summary>
        /// <param name="wordList"></param>
        /// <param name="entropy"></param>
        public Mnemonic(Wordlist wordList, byte[] entropy = null)
        {
            wordList  = wordList ?? Wordlist.English;
            _WordList = wordList;
            if (entropy == null)
            {
                entropy = RandomUtils.GetBytes(32);
            }

            var i = Array.IndexOf(entArray, entropy.Length * 8);

            if (i == -1)
            {
                throw new ArgumentException("The length for entropy should be : " + String.Join(",", entArray), "entropy");
            }

            int cs = csArray[i];

            byte[]    checksum    = Hashes.SHA256(entropy);
            BitWriter entcsResult = new BitWriter();

            entcsResult.Write(entropy);
            entcsResult.Write(checksum, cs);
            _Indices  = entcsResult.ToIntegers();
            _Words    = _WordList.GetWords(_Indices);
            _Mnemonic = _WordList.GetSentence(_Indices);
        }
        public static async Task <Wordlist> LoadWordList(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            Wordlist result = null;

            lock (_LoadedLists)
            {
                _LoadedLists.TryGetValue(name, out result);
            }
            if (result != null)
            {
                return(await Task.FromResult <Wordlist>(result).ConfigureAwait(false));
            }


            if (WordlistSource == null)
            {
                throw new InvalidOperationException("Wordlist.WordlistSource is not set, impossible to fetch word list.");
            }
            result = await WordlistSource.Load(name).ConfigureAwait(false);

            if (result != null)
            {
                lock (_LoadedLists)
                {
                    _LoadedLists.AddOrReplace(name, result);
                }
            }
            return(result);
        }
Example #3
0
        public Mnemonic(string mnemonic, Wordlist wordlist = null)
        {
            if (mnemonic == null)
            {
                throw new ArgumentNullException(nameof(mnemonic));
            }
            _Mnemonic = mnemonic.Trim();

            if (wordlist == null)
            {
                wordlist = Wordlist.AutoDetect(mnemonic) ?? Wordlist.English;
            }

            var words = mnemonic.Split(new char[] { ' ', ' ' }, StringSplitOptions.RemoveEmptyEntries);

            //if the sentence is not at least 12 characters or cleanly divisible by 3, it is bad!
            if (!CorrectWordCount(words.Length))
            {
                throw new FormatException("Word count should be 12,15,18,21 or 24");
            }
            _Words    = words;
            _WordList = wordlist;
            _Indices  = wordlist.ToIndices(words);
        }
Example #4
0
 public Mnemonic(Wordlist wordList, WordCount wordCount)
     : this(wordList, GenerateEntropy(wordCount))
 {
 }