コード例 #1
0
        public static async Task UpdateAccount(string address)
        {
            var acttoupd = (from a in Globals.DbContext.Accounts where a.Address == address select a).First();

            try
            {
                var actinf = await Globals.API.Accounts_GetAccount(acttoupd.Address);

                if (actinf == null || actinf.account == null || !actinf.success)
                {
                    return;
                }
                acttoupd.Balance    = LiskAPI.LSKLongToDecimal(actinf.account.balance);
                acttoupd.LastUpdate = DateTime.UtcNow;
                var dg = await Globals.API.Delegates_Get(acttoupd.PublicKey);

                if (dg != null && dg.success && dg.@delegate != null && !string.IsNullOrEmpty([email protected]))
                {
                    acttoupd.FriendlyName = [email protected];
                }
                await Globals.DbContext.SaveChangesAsync();

                Globals.AppViewModel.AccountsViewModel.RaisePropertyChanged("Accounts");
                Globals.AppViewModel.AccountsViewModel.RaisePropertyChanged("TotalBalance");
            }
            catch (Exception crap)
            {
                Console.WriteLine("AccountsViewModel.UpdateAccount threw an error: " + crap.Message);
            }
        }
コード例 #2
0
        public static async Task UpdateAccounts()
        {
            var cdt       = DateTime.UtcNow.AddMinutes(-2);
            var actstoupd = (from a in Globals.DbContext.Accounts where a.LastUpdate < cdt select a).Take(40);

            foreach (var a in actstoupd)
            {
                try
                {
                    var actinf = await Globals.API.Accounts_GetAccount(a.Address);

                    if (actinf == null || actinf.account == null || !actinf.success)
                    {
                        continue;
                    }
                    a.Balance    = LiskAPI.LSKLongToDecimal(actinf.account.balance);
                    a.LastUpdate = DateTime.UtcNow;
                    var dg = await Globals.API.Delegates_Get(a.PublicKey);

                    if (dg != null && dg.success && dg.@delegate != null && !string.IsNullOrEmpty([email protected]))
                    {
                        a.FriendlyName = [email protected];
                    }

                    await Globals.DbContext.SaveChangesAsync();

                    Globals.AppViewModel.AccountsViewModel.RaisePropertyChanged("Accounts");
                    Globals.AppViewModel.AccountsViewModel.RaisePropertyChanged("TotalBalance");
                }
                catch (Exception crap)
                {
                    Console.WriteLine("AccountsViewModel.UpdateAccounts threw an error: " + crap.Message);
                }
            }
        }
コード例 #3
0
        private async void AddServerButton_Click(object sender, RoutedEventArgs e)
        {
            var serverurl = NewServerTextBox.Text.ToLower().Trim();

            if (string.IsNullOrEmpty(serverurl))
            {
                return;
            }
            if (!serverurl.StartsWith("https://"))
            {
                var nd = new NoticeDialog("Server Address Error",
                                          "Error: Server must start with \"https://\".\r\nPlease correct the address and try again.");
                nd.ShowDialog();
                ;
                return;
            }
            var pingtime = AppHelpers.GetServerResponseTime(serverurl);

            if (pingtime <= 0 || pingtime > 120)
            {
                var nd = new NoticeDialog("Server Connection Error",
                                          "Error: Server connection failed or did not respond fast enough (" + pingtime +
                                          " ms).\r\nPlease try a different server.");
                nd.ShowDialog();
                return;
            }

            try
            {
                var api = new LiskAPI(serverurl);
                var ss  = await api.Loader_Status();

                if (!ss.loaded)
                {
                    var nd = new NoticeDialog("Server Sync Error",
                                              "Error: Server failed sync status test.\r\nPlease try a different server.");
                    nd.ShowDialog();
                    return;
                }
            }
            catch
            {
                var nd = new NoticeDialog("Server API Error",
                                          "Error: Server failed api test.\r\nPlease try a different server.");
                nd.ShowDialog();
                return;
            }

            Properties.Settings.Default.Servers.Add(serverurl);
            Properties.Settings.Default.Save();
            NewServerTextBox.Text = "";
            UpdateServersListView();
        }
コード例 #4
0
        private async void ModernButton_Click_1(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(AccountSecretTextBox.Text.Trim()))
            {
                return;
            }
            var sec        = AccountSecretTextBox.Text;
            var issecvalid = await AppHelpers.IsSecretValid(sec);

            if (!issecvalid)
            {
                var nd = new NoticeDialog("Invalid Account Secret",
                                          "Invalid account secret.\r\nPlease correct and try again.");
                nd.ShowDialog();
                return;
            }
            AccountSecretTextBox.Text = "";
            var act = await Globals.API.Accounts_Open(sec);

            var fn = AccountFriendlyNameTextBox.Text.Trim();

            AccountFriendlyNameTextBox.Text = "";
            if (string.IsNullOrEmpty(fn))
            {
                var dg = await Globals.API.Delegates_Get(act.account.publicKey);

                if (dg != null && dg.success && dg.@delegate != null && !string.IsNullOrEmpty([email protected]))
                {
                    fn = [email protected];
                }
            }

            var hasrecord = (from a in Globals.AppViewModel.AccountsViewModel.Accounts
                             where a.Address == act.account.address || a.FriendlyName == fn
                             select a).Any();

            if (hasrecord)
            {
                var nd = new NoticeDialog("Account Record Exists",
                                          "A record for this account secret or friendly name already exists.\r\nPlease use a different secret or friendly name and try again");
                nd.ShowDialog();
                return;
            }

            using (var avm = new AuthViewModel {
                ActionDescription = "Add / Import new account " + fn
            })
            {
                var rmpw = new AuthRequestDialog(avm);
                rmpw.ShowDialog();
                if (!avm.Accepted)
                {
                    return;
                }
                if (rmpw.DialogResult == null || rmpw.DialogResult == false || string.IsNullOrEmpty(avm.Password))
                {
                    return;
                }

                var ssece = AppHelpers.EncryptString(sec, avm.Password);
                var ni    = new Account
                {
                    Address      = act.account.address,
                    PublicKey    = act.account.publicKey,
                    FriendlyName = fn,
                    SecretHash   = ssece,
                    Balance      = LiskAPI.LSKLongToDecimal(act.account.balance)
                };
                await AccountsViewModel.AddAccountAsync(ni);

                NavigationCommands.BrowseBack.Execute(null, null);
            }
        }
コード例 #5
0
        private async void BulkImportButton_OnClick(object sender, RoutedEventArgs e)
        {
            var fd = new OpenFileDialog();

            fd.Multiselect = false;
            fd.Filter      = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*";
            var showDialog = fd.ShowDialog();
            var res        = showDialog != null && (bool)showDialog;

            if (!res || string.IsNullOrEmpty(fd.FileName) || !File.Exists(fd.FileName))
            {
                return;
            }

            string[] secrets;
            try
            {
                secrets = File.ReadAllText(fd.FileName).Split(',');
            }
            catch (Exception)
            {
                var nd = new NoticeDialog("Bulk Account Import",
                                          "Error: CSV file could not be parsed.\r\nPlease correct the file and try again.");
                nd.ShowDialog();
                return;
            }

            using (var avm = new AuthViewModel {
                ActionDescription = "Bulk Import Accounts "
            })
            {
                var rmpw = new AuthRequestDialog(avm);
                rmpw.ShowDialog();
                if (!avm.Accepted)
                {
                    return;
                }
                if (rmpw.DialogResult == null || rmpw.DialogResult == false || string.IsNullOrEmpty(avm.Password))
                {
                    return;
                }
                var i  = 0;
                var ii = 1;
                var pd = new ProcessingDialog("Importing Accounts",
                                              "Please wait while your accounts are imported. This may take some time depending on the number of accounts.");
                pd.Show();
                foreach (var sec in secrets)
                {
                    ii++;
                    pd.ProgressTextBlock.Text = ii + " of " + secrets.Length;
                    var act = await Globals.API.Accounts_Open(sec);

                    if (!act.success)
                    {
                        continue;
                    }

                    var hasrecord = (from a in Globals.AppViewModel.AccountsViewModel.Accounts
                                     where a.Address == act.account.address
                                     select a).Any();
                    if (hasrecord)
                    {
                        continue;
                    }

                    var ssece = AppHelpers.EncryptString(sec, avm.Password);
                    var bal   = LiskAPI.LSKLongToDecimal(act.account.balance);
                    var ni    = new Account
                    {
                        Address      = act.account.address,
                        PublicKey    = act.account.publicKey,
                        FriendlyName = act.account.address,
                        SecretHash   = ssece,
                        Balance      = bal,
                        LastUpdate   = DateTime.UtcNow
                    };
                    try
                    {
                        await AccountsViewModel.AddAccountAsync(ni);

                        i++;
                    }
                    catch (Exception crap)
                    {
                        Console.WriteLine("Error saving imported account " + act.account.address + " | " + crap.Message);
                    }
                }
                pd.Hide();
                var nd = new NoticeDialog("Bulk Account Import",
                                          "Bulk account import complete.\r\n" + i + " of " + secrets.Length + " accounts imported.");
                nd.ShowDialog();
                NavigationCommands.BrowseBack.Execute(null, null);
            }
        }
コード例 #6
0
        internal async Task UpdateTransactionsAsync()
        {
            try
            {
                var cdt      = DateTime.UtcNow.AddMinutes(-2);
                var accounts =
                    (from a in Globals.DbContext.Accounts where a.LastTransactionsUpdate < cdt select a.Address).Take(20);
                if (!accounts.Any())
                {
                    return;
                }
                foreach (var a in accounts)
                {
                    //TODO: the code in this loop should be put into a private method since it is duplicated
                    do
                    {
                        // just wait
                    } while (Globals.API == null);
                    var stx = await Globals.API.Transactions_GetList("", a, "", 50, 0, "t_timestamp:desc");

                    Thread.Sleep(300);
                    var rtx = await Globals.API.Transactions_GetList("", "", a, 50, 0, "t_timestamp:desc");

                    var trans = new List <Transaction_Object>();
                    if (stx != null && stx.transactions.Any())
                    {
                        trans.AddRange(stx.transactions);
                    }
                    if (rtx != null && rtx.transactions.Any())
                    {
                        trans.AddRange(rtx.transactions);
                    }

                    var newtrans = from t in trans select t;
                    foreach (var t in newtrans)
                    {
                        Globals.AppViewModel.TransactionsViewModel.Transactions =
                            new ObservableCollection <Transaction>(Globals.DbContext.Transactions);
                        if ((from c in Globals.DbContext.Transactions where c.Id == t.id select c).Any())
                        {
                            continue;
                        }
                        var ni = new Transaction
                        {
                            Id       = t.id,
                            Created  = AppHelpers.TimestampToDateTime(t.timestamp),
                            Amount   = LiskAPI.LSKLongToDecimal(t.amount),
                            Block    = (await Globals.API.Blocks_GetHeight()).height - long.Parse(t.confirmations),
                            Receiver = t.recipientId,
                            Sender   = t.senderId,
                            TType    = int.Parse(t.type),
                            Fee      = LiskAPI.LSKLongToDecimal(t.fee)
                        };
                        if (string.IsNullOrEmpty(ni.Receiver))
                        {
                            ni.Receiver = "";
                        }
                        try
                        {
                            Globals.AppViewModel.TransactionsViewModel.Transactions =
                                new ObservableCollection <Transaction>(Globals.DbContext.Transactions);
                            if ((from r in Globals.DbContext.Transactions where r.Id == ni.Id select r).Any())
                            {
                                continue;
                            }
                            await AddTransactionAsync(ni, false);
                        }
                        catch (Exception crap)
                        {
                            Console.WriteLine("TransactionsViewModel.UpdateTransactions Error saving transaction: " +
                                              crap.Message);
                        }
                    }
                    var act =
                        (from c in Globals.DbContext.Accounts where c.Address == a select c).First();
                    act.LastTransactionsUpdate = DateTime.UtcNow;
                    await Globals.DbContext.SaveChangesAsync();
                }
            }
            catch (Exception crap)
            {
                Console.WriteLine("TransactionsViewModel.UpdateTransactions threw an exception: " + crap.Message + " | " +
                                  crap.Source);
            }

            try
            {
                Globals.AppViewModel.TransactionsViewModel.Transactions =
                    new ObservableCollection <Transaction>(Globals.DbContext.Transactions);
                Globals.AppViewModel.TransactionsViewModel.RecentTransactions =
                    new ObservableCollection <Transaction>((from t in Globals.DbContext.Transactions
                                                            orderby t.Created descending
                                                            select t).Take(20));
                Globals.AppViewModel.TransactionsViewModel.RaisePropertyChanged("Transactions");
                Globals.AppViewModel.TransactionsViewModel.RaisePropertyChanged("RecentTransactions");
            }
            catch (Exception crap)
            {
                // the async thread is probably not caught up
                Console.WriteLine("TransactionsViewModel.UpdateTransactions threw an exception: " + crap.Message);
            }
        }
コード例 #7
0
        private async void SendLSKButton_OnClick(object sender, RoutedEventArgs e)
        {
            var act = (from a in Globals.AppViewModel.AccountsViewModel.Accounts
                       where a.FriendlyName == AppViewModel.SelectedAccountFriendlyName
                       select a).First();
            var     avail = act.Balance;
            decimal iamount;

            if (!decimal.TryParse(SendAmountTextBox.Text.Trim(), out iamount))
            {
                ShowNotice("Send LSK", "Please enter a valid send amount and try again.");
                return;
            }
            if (iamount + Properties.Settings.Default.SendFee > avail || iamount < 0.1m)
            {
                ShowNotice("Send LSK",
                           "Sorry the amount specified exceeds your available balance.\r\nPlease enter a valid send amount and try again.");
                return;
            }
            //verify the toaddress is valid by checking the length and format
            if (ToAddressTextBox.Text.Trim().Length < 20 || ToAddressTextBox.Text.Trim().Length > 21 ||
                !ToAddressTextBox.Text.Trim().EndsWith("l"))
            {
                ShowNotice("Send LSK",
                           "Sorry the address specified does not apear to be valid.\r\nPlease check the address for errors and try again.");
                return;
            }
            transactions_send_response res;

            using (
                var avm = new AuthViewModel
            {
                ActionDescription =
                    "Send " + iamount + " LSK from " + act.FriendlyName + " to " + ToAddressTextBox.Text.Trim()
            })
            {
                var rmpw = new AuthRequestDialog(avm);
                rmpw.ShowDialog();
                if (!avm.Accepted)
                {
                    return;
                }
                if (rmpw.DialogResult == null || rmpw.DialogResult == false || string.IsNullOrEmpty(avm.Password))
                {
                    return;
                }
                var actsec = AppHelpers.DecryptString(act.SecretHash, avm.Password);
                res = await Globals.API.Transactions_Send(actsec, (long)LiskAPI.LSKDecimalToLong(iamount),
                                                          ToAddressTextBox.Text.Trim(), act.PublicKey, "");
            }
            if (res == null || !res.success || string.IsNullOrEmpty(res.transactionId))
            {
                if (res != null && !string.IsNullOrEmpty(res.error))
                {
                    Console.WriteLine("Send transaction failed or did not return a transaction id, " + res.error);
                    var nd = new NoticeDialog("Send LSK Failed",
                                              "Sending of " + iamount + " LSK from " + act.FriendlyName + " to " +
                                              ToAddressTextBox.Text.Trim() + " failed.\r\nError: " + res.error);
                    nd.ShowDialog();
                }
                else
                {
                    Console.WriteLine("Send transaction failed or did not return a transaction id, no additional data");
                    var nd = new NoticeDialog("Send LSK Failed",
                                              "Sending of " + iamount + " LSK from " + act.FriendlyName + " to " +
                                              ToAddressTextBox.Text.Trim() + " failed.\r\nError: no error data available.");
                    nd.ShowDialog();
                }
            }
            else
            {
                Console.WriteLine("Send transaction id " + res.transactionId + " sent " + iamount + " LSK from " +
                                  act.FriendlyName + " to " + ToAddressTextBox.Text.Trim());
                var nd = new NoticeDialog("Send LSK",
                                          "Sent " + iamount + " LSK from " + act.FriendlyName + " to " + ToAddressTextBox.Text.Trim());
                nd.ShowDialog();
            }
            try
            {
                Send_OnLoaded(null, null);
            }
            catch
            {
            }
        }
コード例 #8
0
        internal static async Task SetupAPI()
        {
            WriteLine("Setting up API system");
            if (Settings.Default.Testnet)
            {
                Console.WriteLine("USING TESTNET");
            }
            // find the fastest server
            var fastserver     = "";
            var fastservertime = 0L;

            if (Settings.Default.AutoFindServer)
            {
                WriteLine("Finding best node");
                var servers = Settings.Default.Testnet ? Settings.Default.TestnetServers : Settings.Default.Servers;
                foreach (var s in servers)
                {
                    var rt = AppHelpers.GetServerResponseTime(s);
                    Console.WriteLine("tested server " + s + " response time " + rt + " ms");
                    if (rt < fastservertime || fastservertime <= 0)
                    {
                        try
                        {
                            Console.WriteLine("checking server " + s + " status");
                            // check that the server is synced
                            var _api = new LiskAPI(s);
                            var ls   = await _api.Loader_Status();

                            if (ls == null || !ls.success || !ls.loaded)
                            {
                                Console.WriteLine("server " + s + " is not synced, skipping");
                                continue;
                            }
                            var ss = await _api.Loader_SyncStatus();

                            if (ss == null || !ss.success || ss.syncing || ss.blocks != 0)
                            {
                                Console.WriteLine("server " + s + " is not synced, skipping");
                                continue;
                            }
                            fastservertime = rt;
                            fastserver     = s;
                        }
                        catch
                        {
                            Console.WriteLine("server " + s + " failed status query request, skipping");
                        }
                    }
                }

                if (fastservertime > 0 && !string.IsNullOrEmpty(fastserver))
                {
                    Console.WriteLine("Selected server " + fastserver + " response time " + fastservertime + " ms");
                    Globals.API = new LiskAPI(fastserver);
                    Globals.CurrentBlockHeight = (await Globals.API.Blocks_GetHeight()).height;
                    Console.WriteLine("API Server block height " + Globals.CurrentBlockHeight);
                    WriteLine("API system setup complete");
                }
                else
                {
                    WriteLine("API system setup failed, could not find a valid server.");
                    var mbr = MessageBox.Show("Error: Could not find a valid server node.\r\nWould you like retry?",
                                              "Server Error", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (mbr == MessageBoxResult.Yes)
                    {
                        await SetupAPI();
                    }
                    else
                    {
                        ForceExit();
                    }
                }
            }
            else
            {
                fastservertime = AppHelpers.GetServerResponseTime(Settings.Default.Servers[0]);
                WriteLine("Selected first server " + Settings.Default.Servers[0] + " response time " +
                          fastservertime + " ms");
                if (fastservertime <= 0)
                {
                    WriteLine("API server did not respond to ping request, status unknown, attempting test call");
                }
                Globals.API = new LiskAPI(Settings.Default.Servers[0]);
                try
                {
                    var res = await Globals.API.Loader_Status();

                    if (res == null || !res.loaded)
                    {
                        WriteLine("API server failed to respond, fatal exiting");
                        Console.WriteLine("Press any key to quit");
                        Console.ReadLine();
                        ForceExit();
                    }
                }
                catch (Exception crap)
                {
                    WriteLine("Error: " + crap.Message);
                    Console.WriteLine("Press any key to quit");
                    Console.ReadLine();
                    ForceExit();
                }
                Globals.CurrentBlockHeight = (await Globals.API.Blocks_GetHeight()).height;
                Console.WriteLine("API Server block height " + Globals.CurrentBlockHeight);
                WriteLine("API system setup complete");
            }
        }