Exemplo n.º 1
0
        private void Import_Wallet(object sender, EventArgs e)
        {
            Task.Run(async() =>
            {
                if (MainService.MyOpenOrderList.Count > 0)
                {
                    MainService.MessageBox("Notice!", "Cannot load wallet with open orders present.", "OK", false);
                    return;
                }

                if (MainService.CheckPendingPayment() == true)
                {
                    MainService.MessageBox("Notice", "There is at least one pending payment to this current address.", "OK", false);
                    return;
                }

                bool moveable = true;
                for (int i = 0; i < MainService.WalletList.Count; i++)
                {
                    if (MainService.WalletList[i].status != 0)
                    {
                        moveable = false; break;
                    }
                }
                if (moveable == false)
                {
                    MainService.MessageBox("Notice!", "There is at least one wallet unavailable to change the current address", "OK", false);
                    return;
                }

                bool result = MainService.PromptUser("Confirmation", "Importing a previous NebliDex wallet will replace the current wallet. Do you want to continue?", "Yes", "No");
                if (result == false)
                {
                    return;
                }

                //File Picker Dialog
                try
                {
                    //May need to run on UI thread
                    Plugin.FilePicker.Abstractions.FileData fileData = await CrossFilePicker.Current.PickFile();
                    if (fileData == null)
                    {
                        MainService.NebliDexNetLog("User canceled loading backup from file");
                        return; // user canceled file picking
                    }

                    Stream fileData_stream = fileData.GetStream(); //Get the stream referenced by the Picked file, we will write it to file

                    if (fileData_stream == null)
                    {
                        MainService.MessageBox("Notice!", "Unable to locate this file", "OK", false);
                        return;
                    }

                    if (File.Exists(MainService.App_Path + "/account_old.dat") != false)
                    {
                        File.Delete(MainService.App_Path + "/account_old.dat");
                    }
                    //Move the account.dat to the new location (old file) until the copy is complete
                    File.Move(MainService.App_Path + "/account.dat", MainService.App_Path + "/account_old.dat");

                    //Instead of File.Copy, we need to write the stream to the path at account.dat
                    //File.Copy may be unreliable if FilePath is null
                    using (FileStream account_stream = File.Create(MainService.App_Path + "/account.dat"))
                    {
                        fileData_stream.Seek(0, SeekOrigin.Begin);
                        fileData_stream.CopyTo(account_stream);
                        account_stream.Close();
                    }
                    fileData_stream.Close();

                    MainService.my_wallet_pass = ""; //Remove the wallet password
                    MainService.CheckWallet();       //Ran inline and load password if necessary
                    MainService.LoadWallet();
                    //Now delete the old wallet
                    File.Delete(MainService.App_Path + "/account_old.dat");
                    MainService.MessageBox("Notice!", "Imported the wallet successfully.", "OK", false);
                    MainService.NebliDex_Activity.RunOnUiThread(() =>
                    {
                        if (MainService.my_wallet_pass.Length > 0)
                        {
                            Toggle_Encryption_Label.Text = "Decrypt Wallet";
                        }
                        else
                        {
                            Toggle_Encryption_Label.Text = "Encrypt Wallet";
                        }
                    });
                }
                catch (Exception ex)
                {
                    MainService.NebliDexNetLog("Failed to load wallet, error: " + ex.ToString());
                    MainService.MessageBox("Notice!", "Failed to load imported NebliDex wallet. Reverting to old wallet.", "OK", false);
                    try
                    {
                        //Revert the file back to what is was
                        if (File.Exists(MainService.App_Path + "/account_old.dat") != false)
                        {
                            //Move the old wallet back to the current position
                            if (File.Exists(MainService.App_Path + "/account.dat") != false)
                            {
                                File.Delete(MainService.App_Path + "/account.dat");
                            }
                            File.Move(MainService.App_Path + "/account_old.dat", MainService.App_Path + "/account.dat");
                            MainService.my_wallet_pass = ""; //Remove the wallet password
                            MainService.CheckWallet();       //Ran inline and load password if necessary
                            MainService.LoadWallet();
                        }
                    }
                    catch (Exception) { }
                }
            });
        }