예제 #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(64);
            }

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

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

            int cs = csArray[i];

            byte[]    checksum    = Crypto.sha512sqTrunc(entropy);
            BitWriter entcsResult = new BitWriter();

            entcsResult.Write(entropy);
            entcsResult.Write(checksum, cs);
            _Indices  = entcsResult.ToIntegers();
            _Words    = _WordList.GetWords(_Indices);
            _Mnemonic = _WordList.GetSentence(_Indices);
        }
예제 #2
0
        public static async Task <Wordlist> LoadWordList(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException("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);
        }
예제 #3
0
        public Mnemonic(string mnemonic, Wordlist wordlist = null)
        {
            if (mnemonic == null)
            {
                throw new ArgumentNullException("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 equals to 12,15,18,21 or 24");
            }
            _Words    = words;
            _WordList = wordlist;
            _Indices  = wordlist.ToIndices(words);
        }
예제 #4
0
 public Mnemonic(Wordlist wordList, WordCount wordCount)
     : this(wordList, GenerateEntropy(wordCount))
 {
 }
예제 #5
0
        public int[] ToIntegers()
        {
            var array = new BitArray(values.ToArray());

            return(Wordlist.ToIntegers(array));
        }