예제 #1
0
        async void OnBindButtonClicked(object sender, EventArgs args)
        {
            try
            {
                string qrCode = await CommonTasks.GetQRCodeAsync();

                if (qrCode == null)
                {
                    return;
                }
                else
                {
                    string publicKey;

                    Settings.BindControlPanel(qrCode);

                    Settings.GetPublicKey(out publicKey);

                    string fingerprint = Settings.PublicKeyToFingerprint(publicKey);

                    await DisplayAlert("Device bound", "Device with fingerprint " + fingerprint + " is successfully bound.", "OK");

                    Navigation.InsertPageBefore(new RemoteScreenPage(), this);
                    await Navigation.PopAsync(true);
                }
            }
            catch (Exception e)
            {
                await DisplayAlert("Error", "Binding failed", "OK");
            }
        }
예제 #2
0
        async void OnBindButtonClicked(object sender, EventArgs args)
        {
            try
            {
#if __ANDROID__
                var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera);

                if (status != PermissionStatus.Granted)
                {
                    var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Camera);

                    if (results.ContainsKey(Permission.Camera))
                    {
                        status = results[Permission.Camera];
                    }
                }

                if (status != PermissionStatus.Granted)
                {
                    await DisplayAlert("Camera permission denied", "Please enable camera permission for this app.", "OK");

                    return;
                }
#endif

                string qrCode = await CommonTasks.GetQRCodeAsync();

                if (qrCode == null)
                {
                    return;
                }
                else
                {
                    string publicKey;

                    Settings.BindControlPanel(qrCode);

                    Settings.GetPublicKey(out publicKey);

                    string fingerprint = Settings.PublicKeyToFingerprint(publicKey);

                    await DisplayAlert("Device bound", "Device with fingerprint " + fingerprint + " is successfully bound.", "OK");

                    Navigation.InsertPageBefore(new RemoteScreenPage(), this);
                    await Navigation.PopAsync(true);
                }
            }
            catch (Exception e)
            {
                await DisplayAlert("Error", "Binding failed", "OK");
            }
        }
        private async Task FindServer(CancellationToken token)
        {
            CommonTasks.ServerInfo serverInfo;
            int failedAttempts = 0;

            try
            {
                while (true)
                {
                    try
                    {
                        serverInfo = await CommonTasks.FindServerAsync(TimeSpan.FromSeconds(1), token);
                    }
                    catch (OperationCanceledException)
                    {
                        throw;
                    }
                    catch (Exception e)
                    {
                        failedAttempts++;

                        if (failedAttempts == 5)
                        {
                            ServerStatus             = "Searching for Secalot Control Panel";
                            ServerColor              = "lightYellow";
                            TransactionButtonEnabled = false;
                            failedAttempts           = 0;

                            await Task.Delay(5000, token);
                        }

                        continue;
                    }

                    ServerStatus             = "Secalot Control Panel found";
                    ServerColor              = "lightGreen";
                    TransactionButtonEnabled = true;
                    this.serverInfo          = serverInfo;
                    failedAttempts           = 0;

                    await Task.Delay(5000, token);
                }
            }
            catch (TaskCanceledException)
            {
                return;
            }
        }
        async void OnGetTransactionButtonClicked(object sender, EventArgs args)
        {
            uint timeout = 0;

            MainLayoutEnabled = false;

            try
            {
                CancellationTokenSource timeoutCTS = new CancellationTokenSource(TimeSpan.FromSeconds(5));

                using (CancellationTokenSource linkedCts = CancellationTokenSource.CreateLinkedTokenSource(timeoutCTS.Token, getTransactionTokenSource.Token))
                {
                    CommonTasks.ConnectionState connectionState = new CommonTasks.ConnectionState();

                    using (connectionState.client = new TcpClient())
                    {
                        await CommonTasks.ConnectToServerAsync(serverInfo, connectionState, TimeSpan.FromSeconds(1));

                        await CommonTasks.EstablishSRPChannelAsync(connectionState, linkedCts.Token);

                        var tls = await CommonTasks.PerformSSLHadshakeWithToken(connectionState, linkedCts.Token);

                        try
                        {
                            await CommonTasks.SelectBtcApp(connectionState, linkedCts.Token, tls);

                            var transactionDetails = await CommonTasks.GetBtcTransactionDetails(connectionState, linkedCts.Token, tls);

                            var inputAmounts = await CommonTasks.GetBtcInputAmounts(transactionDetails.numberOfInputs, connectionState, linkedCts.Token, tls);

                            var transaction = await CommonTasks.ReadBtcTransaction(transactionDetails.currentOffset, connectionState, linkedCts.Token, tls);

                            var parsedTransaction = BtcParser.ParseBitcoinTransaction(transactionDetails, transaction, inputAmounts, ref timeout);

                            await PopupNavigation.PushAsync(new TransactionPopup(parsedTransaction, timeout));
                        }
                        catch (TransactionNotActiveException)
                        {
                            try
                            {
                                await CommonTasks.SelectEthApp(connectionState, linkedCts.Token, tls);

                                var transactionDetails = await CommonTasks.GetEthTransactionDetails(connectionState, linkedCts.Token, tls);

                                var transaction = await CommonTasks.ReadEthTransaction(transactionDetails.currentOffset, connectionState, linkedCts.Token, tls);

                                string parsedTransaction = EthParser.ParseEthereumTransaction(transactionDetails, transaction, ref timeout);

                                await PopupNavigation.PushAsync(new TransactionPopup(parsedTransaction, timeout));
                            }
                            catch (TransactionNotActiveException)
                            {
                                try
                                {
                                    await CommonTasks.SelectXrpApp(connectionState, linkedCts.Token, tls);

                                    var transactionDetails = await CommonTasks.GetXrpTransactionDetails(connectionState, linkedCts.Token, tls);

                                    var transaction = await CommonTasks.ReadXrpTransaction(transactionDetails.currentOffset, connectionState, linkedCts.Token, tls);

                                    List <string> parsedTransaction = XrpParser.ParseRippleTransaction(transactionDetails, transaction, ref timeout);

                                    await PopupNavigation.PushAsync(new TransactionPopup(parsedTransaction[0], timeout, parsedTransaction[1]));
                                }
                                catch (TransactionNotActiveException)
                                {
                                    throw new RemoteProtocolException("There are no transactions active");
                                }
                                catch (AppNotPresentException)
                                {
                                    throw new RemoteProtocolException("There are no transactions active");
                                }
                            }
                        }
                    }
                }
            }
            catch (RemoteProtocolException e)
            {
                await DisplayAlert("Error", e.Message, "OK");
            }
            catch (ServerConnectionFailedException e)
            {
                await DisplayAlert("Error", "Failed to connect.", "OK");
            }
            catch (EthParserException e)
            {
                await DisplayAlert("Error", e.Message, "OK");
            }
            catch (BtcParserException e)
            {
                await DisplayAlert("Error", e.Message, "OK");
            }
            catch (XrpParserException e)
            {
                await DisplayAlert("Error", e.Message, "OK");
            }
            catch (Exception e)
            {
                string message = "An error has occurred";
                while (e.InnerException != null)
                {
                    if (e.InnerException.GetType() == typeof(InvalidServerCertificateException))
                    {
                        message = "This app is bound with a different Secalot device.";
                        break;
                    }

                    e = e.InnerException;
                }

                await DisplayAlert("Error", message, "OK");
            }
            finally
            {
                MainLayoutEnabled = true;
            }
        }