Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of <see cref="ElectrumMnemonic"/> with the given mnemonic, world list and the passphrase.
        /// </summary>
        /// <exception cref="ArgumentException"/>
        /// <exception cref="ArgumentNullException"/>
        /// <exception cref="FormatException"/>
        /// <param name="mnemonic">Mnemonic (should be 12 words)</param>
        /// <param name="wl">[Defaultvalue = <see cref="BIP0039.WordLists.English"/> Word list to use</param>
        /// <param name="passPhrase">
        /// [Default value = null] Optional passphrase to use for computing <see cref="BIP0032"/> entropy
        /// </param>
        public ElectrumMnemonic(string mnemonic, BIP0039.WordLists wl = BIP0039.WordLists.English, string passPhrase = null)
        {
            if (string.IsNullOrWhiteSpace(mnemonic))
            {
                throw new ArgumentNullException(nameof(mnemonic), "Seed can not be null or empty!");
            }
            allWords = BIP0039.GetAllWords(wl);

            string[] words = mnemonic.Normalize(NormalizationForm.FormKD)
                             .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            if (words.Length != WordLen)
            {
                throw new FormatException("Invalid seed length. It should be 12.");
            }
            if (!words.All(x => allWords.Contains(x)))
            {
                throw new FormatException("Mnemonic has invalid words.");
            }

            wordIndexes = new int[words.Length];
            for (int i = 0; i < words.Length; i++)
            {
                wordIndexes[i] = Array.IndexOf(allWords, words[i]);
            }

            MnType = GetMnomonicType(Normalize(ToMnemonic()));
            if (MnType == MnemonicType.Undefined)
            {
                throw new FormatException("Invalid mnemonic (undefined version).");
            }

            SetBip32(passPhrase);
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of <see cref="ElectrumMnemonic"/> with a randomly generated entropy
        /// using the given <see cref="IRandomNumberGenerator"/> instance, world list and an the passphrase.
        /// </summary>
        /// <exception cref="ArgumentException"/>
        /// <exception cref="ArgumentNullException"/>
        /// <exception cref="ArgumentOutOfRangeException"/>
        /// <param name="rng">Random number generator to use</param>
        /// <param name="mnType">Type of the mnemonic to create (anything but <see cref="MnemonicType.Undefined"/>)</param>
        /// <param name="wl">[Defaultvalue = <see cref="BIP0039.WordLists.English"/> Word list to use</param>
        /// <param name="passPhrase">
        /// [Default value = null] Optional passphrase to use for computing <see cref="BIP0032"/> entropy
        /// </param>
        public ElectrumMnemonic(IRandomNumberGenerator rng, MnemonicType mnType,
                                BIP0039.WordLists wl = BIP0039.WordLists.English, string passPhrase = null)
        {
            if (rng is null)
            {
                throw new ArgumentNullException(nameof(rng), "Random number generator can not be null.");
            }
            if (!Enum.IsDefined(typeof(MnemonicType), mnType) || mnType == MnemonicType.Undefined)
            {
                throw new ArgumentException("Undefined mnemonic type.", nameof(mnType));
            }

            MnType   = mnType;
            allWords = BIP0039.GetAllWords(wl);

            byte[] entropy = new byte[EntropyByteLen];
            rng.GetBytes(entropy);
            SetWordsFromEntropy(entropy);
            SetBip32(passPhrase);
        }
Beispiel #3
0
        /// <summary>
        /// Initializes a new instance of <see cref="ElectrumMnemonic"/> with the given entropy, world list and the passphrase.
        /// </summary>
        /// <exception cref="ArgumentException"/>
        /// <exception cref="ArgumentNullException"/>
        /// <exception cref="ArgumentOutOfRangeException"/>
        /// <param name="entropy">Entropy to use (must be 17 bytes or 132 bits)</param>
        /// <param name="mnType">Type of the mnemonic to create (anything but <see cref="MnemonicType.Undefined"/>)</param>
        /// <param name="wl">[Defaultvalue = <see cref="BIP0039.WordLists.English"/> Word list to use</param>
        /// <param name="passPhrase">
        /// [Default value = null] Optional passphrase to use for computing <see cref="BIP0032"/> entropy
        /// </param>
        public ElectrumMnemonic(byte[] entropy, MnemonicType mnType,
                                BIP0039.WordLists wl = BIP0039.WordLists.English, string passPhrase = null)
        {
            if (entropy == null)
            {
                throw new ArgumentNullException(nameof(entropy), "Entropy can not be null.");
            }
            if (entropy.Length != EntropyByteLen)
            {
                throw new ArgumentOutOfRangeException(nameof(entropy), $"Entropy must be {EntropyByteLen} bytes or 132 bits.");
            }
            if (!Enum.IsDefined(typeof(MnemonicType), mnType) || mnType == MnemonicType.Undefined)
            {
                throw new ArgumentException("Undefined mnemonic type.", nameof(mnType));
            }

            MnType   = mnType;
            allWords = BIP0039.GetAllWords(wl);
            SetWordsFromEntropy(entropy);
            SetBip32(passPhrase);
        }