Ejemplo n.º 1
0
        public void GetExtRootTest(string input, string expected)
        {
            var actual      = ElectrumMnemonic.GetExtRoot(input);
            var actualXpriv = actual.GetWif(Network.Main).ToString();

            Assert.Equal(expected, actualXpriv);
        }
Ejemplo n.º 2
0
        public void GetWalletTest(string seedPhrase, bool isChange, int walletId, string actualAddress)
        {
            // #1
            var wallet  = ElectrumMnemonic.GetWallet(seedPhrase, walletId, isChange);
            var actual1 = wallet
                          .GetPublicKey()
                          .GetAddress(ScriptPubKeyType.Legacy, Network.Main)
                          .ToString();

            Assert.Equal(actualAddress, actual1);

            // #2
            var actual2 = ElectrumMnemonic.GetWalletAddress(seedPhrase, walletId, isChange);

            Assert.Equal(actualAddress, actual2);

            // #3
            var root = ElectrumMnemonic.GetExtRoot(seedPhrase);

            var wallet3 = ElectrumMnemonic.GetWallet(root, walletId, isChange);
            var actual3 = wallet3
                          .GetPublicKey()
                          .GetAddress(ScriptPubKeyType.Legacy, Network.Main)
                          .ToString();

            Assert.Equal(actualAddress, actual3);

            // #4
            var actual4 = ElectrumMnemonic.GetWalletAddress(root, walletId, isChange);

            Assert.Equal(actualAddress, actual4);
        }
Ejemplo n.º 3
0
        public void ToMnemonic_DisoisedExceptionTest()
        {
            byte[] ent  = Helper.HexToBytes("0a0fecede9bf8a975eb6b4ef75bb799f00");
            var    elmn = new ElectrumMnemonic(ent, ElectrumMnemonic.MnemonicType.Standard, BIP0039.WordLists.Spanish);

            elmn.Dispose();

            Assert.Throws <ObjectDisposedException>(() => elmn.ToMnemonic());
        }
Ejemplo n.º 4
0
        public void Constructor_FromByte_IncrementTest()
        {
            byte[] ent = Helper.HexToBytes("0a0fecede9bf8a975eb6b4ef75bb799f00");
            using var elmn = new ElectrumMnemonic(ent, ElectrumMnemonic.MnemonicType.Standard, BIP0039.WordLists.Spanish);

            string actual   = elmn.ToMnemonic();
            string expected = "almíbar tibio superar vencer hacha peatón príncipe matar consejo polen vehículo odisea";

            Assert.Equal(expected, actual);
        }
Ejemplo n.º 5
0
        public async void FindPath(string input, SeedType inputType, BIP0039.WordLists wl, string pass,
                                   string extra, InputType extraType)
        {
            report.Init();

            BIP0032 bip32 = null;

            if (inputType == SeedType.BIP39)
            {
                try
                {
                    bip32 = new BIP0039(input, wl, pass);
                }
                catch (Exception ex)
                {
                    report.Fail($"Could not instantiate BIP-39 instance. Error: {ex.Message}");
                    return;
                }
            }
            else if (inputType == SeedType.Electrum)
            {
                try
                {
                    bip32 = new ElectrumMnemonic(input, wl, pass);
                }
                catch (Exception ex)
                {
                    report.Fail($"Could not instantiate ElectrumMnemonic instance. Error: {ex.Message}");
                    return;
                }
            }
            else if (inputType == SeedType.XPRV || inputType == SeedType.XPUB)
            {
                try
                {
                    bip32 = new BIP0032(input);
                    return;
                }
                catch (Exception ex)
                {
                    report.Fail($"Could not instantiate BIP-32 instance. Error: {ex.Message}");
                }
            }
            else
            {
                report.Fail("Undefined input type.");
                return;
            }

            Debug.Assert(bip32 is not null);

            // TODO: Derive child keys at different known paths to see which one matches the extra input

            report.Finalize();
        }
Ejemplo n.º 6
0
        public void Constructor_FromRngTest()
        {
            var rng = new MockRng(BigInteger.Parse("3423992296655289706780599506247192518735").ToByteArray(true, true));

            using var elmn = new ElectrumMnemonic(rng, ElectrumMnemonic.MnemonicType.Standard, BIP0039.WordLists.Spanish);

            string actual   = elmn.ToMnemonic();
            string expected = "almíbar tibio superar vencer hacha peatón príncipe matar consejo polen vehículo odisea";

            Assert.Equal(expected, actual);
        }
Ejemplo n.º 7
0
        public void ConstructorTest(BIP0039.WordLists wl, string mnemonic, byte[] entropy,
                                    ElectrumMnemonic.MnemonicType mnType, string pass, byte[] bip32Seed)
        {
            using var elmn = new ElectrumMnemonic(mnemonic, wl, pass);
            Assert.Equal(mnType, elmn.MnType);
            Assert.Equal(mnemonic, elmn.ToMnemonic());

            if (entropy != null)
            {
                using var fromEntropy = new ElectrumMnemonic(entropy, mnType, wl, pass);
                Assert.Equal(mnType, fromEntropy.MnType);
                Assert.Equal(mnemonic, fromEntropy.ToMnemonic());
            }

            if (bip32Seed != null)
            {
                string actualXprv   = elmn.ToBase58(false);
                string expectedXprv = new BIP0032(bip32Seed).ToBase58(false);
                Assert.Equal(expectedXprv, actualXprv);
            }
        }
Ejemplo n.º 8
0
        public bool TryDecodeMnemonic(string mnemonic, MnemonicTypes mnType, string[] allWords, out byte[] bytes)
        {
            bytes = null;
            if (string.IsNullOrWhiteSpace(mnemonic))
            {
                return(report.Fail("Mnemonic can not be null or empty."));
            }
            else
            {
                string[] words = mnemonic.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (!MnemonicSevice.allowedWordLengths.Contains(words.Length))
                {
                    return(report.Fail("Invalid mnemonic length."));
                }

                bool invalidWord = false;
                for (int i = 0; i < words.Length; i++)
                {
                    if (!allWords.Contains(words[i]))
                    {
                        invalidWord = true;
                        report.Fail($"Given mnemonic contains invalid word at index {i} ({words[i]}).");
                    }
                }
                if (invalidWord)
                {
                    return(false);
                }

                string temp       = string.Join(' ', words);
                string normalized = mnType == MnemonicTypes.Electrum ? ElectrumMnemonic.Normalize(temp) : temp;

                bytes = Encoding.UTF8.GetBytes(normalized);
                return(true);
            }
        }
Ejemplo n.º 9
0
        public async void FindPath(string input, SeedType inputType, BIP0039.WordLists wl, string pass,
                                   string extra, InputType extraType, uint count)
        {
            report.Init();

            if (!inputService.TryGetCompareService(extraType, extra, out comparer))
            {
                report.Fail($"Invalid extra input or extra input type: {extraType}");
                return;
            }

            BIP0032 bip32;

            if (inputType == SeedType.BIP39)
            {
                try
                {
                    bip32 = new BIP0039(input, wl, pass);
                }
                catch (Exception ex)
                {
                    report.Fail($"Could not instantiate BIP-39 instance. Error: {ex.Message}");
                    return;
                }
            }
            else if (inputType == SeedType.Electrum)
            {
                try
                {
                    bip32 = new ElectrumMnemonic(input, wl, pass);
                }
                catch (Exception ex)
                {
                    report.Fail($"Could not instantiate ElectrumMnemonic instance. Error: {ex.Message}");
                    return;
                }
            }
            else if (inputType == SeedType.XPRV)
            {
                try
                {
                    bip32 = new BIP0032(input);
                }
                catch (Exception ex)
                {
                    report.Fail($"Could not instantiate BIP-32 instance. Error: {ex.Message}");
                    return;
                }
            }
            else
            {
                report.Fail("Undefined input type.");
                return;
            }

            Debug.Assert(bip32 is not null);

            await Task.Run(() => Loop(bip32, count));

            report.Finalize();
        }
Ejemplo n.º 10
0
        public void GetSeedVersionTest(string input, int expected)
        {
            var actual = ElectrumMnemonic.GetSeedVersion(input);

            Assert.Equal(expected, actual);
        }
Ejemplo n.º 11
0
        public void NormalizeSeedPhraseTest(string input, string expected)
        {
            var actual = ElectrumMnemonic.NormalizeSeedPhrase(input);

            Assert.Equal(expected, actual);
        }