private void txtSeedToSplit_TextChanged(object sender, TextChangedEventArgs e)
 {
     try
     {
         clsSeed seed = new clsSeed(txtSeedToSplit.Text);
         txtSeedToSplit.ToolTip             = $"Count of words: {seed.iWordCount}";
         txtSeedToSplit.Background          = new SolidColorBrush(Colors.LightGreen);
         txtSeedBIP39HEX.Text               = seed.BIP39SeedHex;
         txtSeedHex.Text                    = seed.SeedBitsHex;
         btnDoSplit.IsEnabled               = true;
         btnCopySharesToClipboard.IsEnabled = false;
         txtSeedToSplit.IsReadOnly          = true;
         btnLockSeed.Visibility             = Visibility.Visible;
         btnLockSeedOpen.Visibility         = Visibility.Collapsed;
     }
     catch (Exception ex)
     {
         txtSeedToSplit.Background = new SolidColorBrush(Colors.LightSalmon);
         txtSeedToSplit.ToolTip    = $"Invalid seed";
         lstSplitShares.Items.Clear();
         txtSeedBIP39HEX.Text = "";
         txtSeedHex.Text      = "";
         btnDoSplit.IsEnabled = false;
         btnCopySharesToClipboard.IsEnabled = false;
         txtSeedToSplit.IsReadOnly          = false;
         btnLockSeed.Visibility             = Visibility.Collapsed;
         btnLockSeedOpen.Visibility         = Visibility.Visible;
     }
 }
        private void CalculateFromValidShares()
        {
            if (liCurrShares.Count < 3)
            {
                return;
            }

            clsBIP39Splitter merger = new clsBIP39Splitter();

            byte[] baSeed;
            string sRet = merger.JoinSecrets(liCurrShares, out baSeed, txtPassword.Text);

            if (sRet != "")
            {
                txtMerged.Text       = sRet;
                txtMerged.Background = new SolidColorBrush(Colors.LightSalmon);
                return;
            }
            try
            {
                clsSeed seed = new clsSeed(baSeed);
                txtMerged.Text       = seed.SeedString;
                txtMerged.Background = new SolidColorBrush(Colors.LightGreen);
            }
            catch (Exception ex)
            {
                txtMerged.Text       = $"Wrong password used ({ex.Message})";
                txtMerged.Background = new SolidColorBrush(Colors.LightSalmon);
            }
        }
        private void lstSplitShares_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            List <string> liSelShares = new List <string>();

            foreach (ucShare uc1 in lstSplitShares.SelectedItems)
            {
                liSelShares.Add(uc1.txtShare.Text);
            }

            if (liSelShares.Count < 2)
            {
                return;
            }

            clsBIP39Splitter merger = new clsBIP39Splitter();

            byte[] baSecret;
            string sRet = merger.JoinSecrets(liSelShares, out baSecret, txtPassword.Text);

            if (sRet != "")
            {
                txtTestMerged.Text       = sRet;
                txtTestMerged.Background = new SolidColorBrush(Colors.Yellow);
                return;
            }

            try
            {
                clsSeed seed = new clsSeed(baSecret);
                txtTestMerged.Text = seed.SeedString;

                if (txtSeedToSplit.Text == txtTestMerged.Text)
                {
                    txtTestMerged.Background = new SolidColorBrush(Colors.LightGreen);
                }
                else
                {
                    txtTestMerged.Background = new SolidColorBrush(Colors.LightSalmon);
                }
            }
            catch (Exception ex)
            {
                txtTestMerged.Text       = $"Wrong password used ({ex.Message})";
                txtTestMerged.Background = new SolidColorBrush(Colors.LightSalmon);
            }
        }
        private async void btnDoSplit_Click(object sender, RoutedEventArgs e)
        {
            btnDoSplit.Content  = "Calculating...";
            gridSplit.IsEnabled = false;
            lstSplitShares.Items.Clear();
            txtTestMerged.Text = "";

            clsSeed seed = new clsSeed(txtSeedToSplit.Text);

            byte[] baSeedHex = clsHelpers.StringToByteArray(seed.SeedBitsHex);

            List <string> liBestShares = new List <string>();
            int           iBestMaxLen  = int.MaxValue;
            Single        siBestAvg    = Single.MaxValue;
            string        sPassword    = txtPassword.Text;
            byte          bShareCount  = byte.Parse(txtShareCount.Text);
            byte          bThreshold   = byte.Parse(txtThreshold.Text);
            int           iTried       = 0;

            timerDoCounter          = new DispatcherTimer();
            timerDoCounter.Interval = new TimeSpan(0, 0, 1);
            timerDoCounter.Tick    += timerDoCounter_Tick;
            timerDoCounter.Start();

            dRunUntil = DateTime.Now.AddSeconds(Convert.ToInt32(txtSeconds.Text));
            await Task.Run(() =>
            {
                while (DateTime.Now < dRunUntil)
                {
                    iTried++;
                    clsBIP39Splitter splitter = new clsBIP39Splitter();
                    List <string> liShares    = splitter.SeedToSplitSecrets(seed.SeedBytes, bShareCount, bThreshold, sPassword);
                    int iCurrMax = 0, iCurrTotal = 0;

                    foreach (string s1 in liShares)
                    {
                        iCurrTotal += s1.Length;
                        if (iCurrMax < s1.Length)
                        {
                            iCurrMax = s1.Length;
                        }
                    }
                    Single siCurrAvg = iCurrTotal / liShares.Count;

                    if ((siCurrAvg < siBestAvg) && (iCurrMax <= iBestMaxLen))
                    {
                        siBestAvg    = siCurrAvg;
                        iBestMaxLen  = iCurrMax;
                        liBestShares = liShares;
                    }
                }
            });

            for (int i1 = 0; i1 < liBestShares.Count; i1++)
            {
                string  sShare = liBestShares[i1];
                ucShare uc1    = new ucShare(i1 + 1, sShare);
                lstSplitShares.Items.Add(uc1);
            }
            btnDoSplit.Content  = "Do Split";
            btnDoSplit.ToolTip  = $"Tried:{iTried} Avg:{siBestAvg} MaxLen={iBestMaxLen}";
            gridSplit.IsEnabled = true;
            btnCopySharesToClipboard.IsEnabled = true;
        }