public async Task RefreshExecute() { try { IsBusy = true; SentList = new ObservableCollection <Message>(); if (!string.IsNullOrEmpty(AuthenticationService.AuthenticatedUser.UserBox)) { var mailCount = await PhantasmaService.GetOutboxCount(); if (mailCount > 0) { await DeserializeOutboxMails(mailCount); } } } catch (Exception ex) { if (ex is RpcClientUnknownException || ex is RpcClientTimeoutException) //todo switch error message { AppSettings.ChangeRpcServer(); } await DialogService.ShowAlertAsync(ex.Message, AppResource.Alert_Error); } finally { IsBusy = false; } }
private async Task UnregisterExecute() { if (IsBusy) { return; } try { IsBusy = true; var confirmation = await DialogService.ShowConfirmAsync( "Are you sure you want to unregister? You won't be able to use your box again.", "Unregister", "Confirm", "Cancel"); if (confirmation) { var tx = await PhantasmaService.UnregisterMailbox(); if (!string.IsNullOrEmpty(tx)) { await NavigationService.NavigateToAsync <LoginViewModel>(); } } } catch (Exception ex) { await DialogService.ShowAlertAsync(ex.Message, AppResource.Alert_Error); } finally { IsBusy = false; } }
private async Task <string> SerializeAndHashMessage() { Message.Date = DateTime.UtcNow; Message.FromInbox = AuthenticationService.AuthenticatedUser.UserBox; Message.FromAddress = AuthenticationService.AuthenticatedUser.GetUserDefaultAddress(); var remotePubkey = await PhantasmaService.GetMailboxPublicKey(Message.ToInbox); // todo if (!string.IsNullOrEmpty(remotePubkey)) { var encryptedText = EncryptionUtils.Encrypt(Message.TextContent, AuthenticationService.AuthenticatedUser.GetPrivateKey(), remotePubkey.HexToBytes()); Message.TextContent = encryptedText.ToHexString(); } var json = JsonConvert.SerializeObject(Message, AppSettings.JsonSettings()); var bytes = Encoding.Default.GetBytes(json); json = Encoding.UTF8.GetString(bytes); return(json); }
private async Task DeserializeOutboxMails(int mailCount) { var index = 1; var emails = await PhantasmaService.GetAllOutboxMessages(mailCount); var storedEmails = await _db.GetSentMessages(AuthenticationService.AuthenticatedUser.UserBox); try { //deserialization foreach (var email in emails) { if (email.StartsWith("{") || email.StartsWith("[")) { string decryptedText = string.Empty; var mailObject = JsonConvert.DeserializeObject <Message>(email, AppSettings.JsonSettings()); if (mailObject?.TextContent != null) { if (MessageUtils.IsHex(mailObject.TextContent.ToCharArray())) { var encryptedText = mailObject.TextContent.HexToBytes(); var remotePub = await PhantasmaService.GetMailboxPublicKey(mailObject.ToInbox); decryptedText = EncryptionUtils.Decrypt(encryptedText, AuthenticationService.AuthenticatedUser.GetPrivateKey(), remotePub.HexToBytes()); } mailObject.ID = index; var hash = GetHashFromStoredMessage(storedEmails.ToList(), mailObject); if (!string.IsNullOrEmpty(hash)) { mailObject.Hash = hash; } if (!string.IsNullOrEmpty(decryptedText)) { mailObject.TextContent = decryptedText; } SentList.Add(mailObject); } } index++; } } catch (Exception e) { Debug.WriteLine(e); } SentList = new ObservableCollection <Message>(SentList.OrderByDescending(p => p.Date) .ThenByDescending(p => p.Date.Hour).ToList()); _fullSentList = SentList.ToList(); }
private async Task DeleteMessageExecute() { if (IsBusy) { return; } string tx = null; try { IsBusy = true; if (FromInbox) { tx = await PhantasmaService.RemoveInboxMessage(SelectedMessage.ID); } else { tx = await PhantasmaService.RemoveOutboxMessage(SelectedMessage.ID); } } catch (Exception ex) { if (ex is RpcClientUnknownException || ex is RpcClientTimeoutException) //todo switch error message { AppSettings.ChangeRpcServer(); } await DialogService.ShowAlertAsync(ex.Message, AppResource.Alert_Error); } finally { IsBusy = false; } if (!string.IsNullOrEmpty(tx)) { await _db.DeleteMessage(SelectedMessage.ToStoreMessage()); await DialogService.ShowAlertAsync("Message will be deleted in the next block", "Success"); //todo move to resources await NavigationService.NavigateBackAsync(); } else { await DialogService.ShowAlertAsync(AppResource.Alert_SomethingWrong, AppResource.Alert_Error); } }
private async Task DeleteSelectedMessagesExecute() { if (IsBusy) { return; } try { IsBusy = true; var indexes = new List <int>(); foreach (var message in SentList) { if (message.IsSelected) { indexes.Add(message.ID); } } var tx = await PhantasmaService.RemoveInboxMessages(indexes.ToArray()); if (string.IsNullOrEmpty(tx)) { await DialogService.ShowAlertAsync(AppResource.Alert_SomethingWrong, AppResource.Alert_Error); } else { _fullSentList.RemoveAll(msg => indexes.Contains(msg.ID)); SentList = new ObservableCollection <Message>(_fullSentList); } } catch (Exception ex) { if (ex is RpcClientUnknownException || ex is RpcClientTimeoutException) //todo switch error message { AppSettings.ChangeRpcServer(); } await DialogService.ShowAlertAsync(ex.Message, AppResource.Alert_Error); } finally { IsBusy = false; IsMultipleSelectionActive = false; DeselectAllMessages(); MessagingCenter.Send(this, "resetToolbar"); } }
private async Task DeserializeInboxMails(int mailCount) { var index = 1; var emails = await PhantasmaService.GetAllInboxMessages(mailCount); try { //deserialization foreach (var email in emails) { if (email.StartsWith("{") || email.StartsWith("[")) { var mailObject = JsonConvert.DeserializeObject <Message>(email, AppSettings.JsonSettings()); if (mailObject?.TextContent != null) { if (MessageUtils.IsHex(mailObject.TextContent.ToCharArray())) { var encryptedText = mailObject.TextContent.HexToBytes(); var remotePub = await PhantasmaService.GetMailboxPublicKey(mailObject.FromInbox); var decryptedText = EncryptionUtils.Decrypt(encryptedText, AuthenticationService.AuthenticatedUser.GetPrivateKey(), remotePub.HexToBytes()); mailObject.TextContent = decryptedText; } mailObject.ID = index; InboxList.Add(mailObject); } } index++; } } catch (Exception e) { Debug.WriteLine(e); } InboxList = new ObservableCollection <Message>(InboxList.OrderByDescending(p => p.Date) .ThenByDescending(p => p.Date.Hour).ToList()); _fullInboxList = InboxList.ToList(); }
private async Task DeleteMessageExecute(Message msg) { if (msg == null) { return; } if (IsBusy) { return; } try { IsBusy = true; var tx = await PhantasmaService.RemoveInboxMessage(msg.ID); if (string.IsNullOrEmpty(tx)) { await DialogService.ShowAlertAsync(AppResource.Alert_SomethingWrong, AppResource.Alert_Error); } else { _fullInboxList.Remove(msg); InboxList = new ObservableCollection <Message>(_fullInboxList); } } catch (Exception ex) { if (ex is RpcClientUnknownException || ex is RpcClientTimeoutException) //todo switch error message { AppSettings.ChangeRpcServer(); } await DialogService.ShowAlertAsync(ex.Message, AppResource.Alert_Error); } finally { IsBusy = false; } }
private async Task LoginExecute() { //if (isValid) TODO see if input is rigth try { if (IsBusy) { return; } IsBusy = true; // TODO LOGIN LOGIC await Task.Delay(1000); switch (LoginOption) { case LoginEnum.Wif: if (!await AuthenticationService.LoginAsync(Wif)) { await DialogService.ShowAlertAsync("Invalid WIF", AppResource.Alert_Error); } break; case LoginEnum.EncryptedKey: if (!await AuthenticationService.LoginAsync(EncryptedKey, Password)) { await DialogService.ShowAlertAsync("Invalid Encrypted Key/Password", AppResource.Alert_Error); } break; case LoginEnum.Username: //TODO await DialogService.ShowAlertAsync("This is the least secure method to login. Use this option just to experiment the app, we do not advise to secure your funds here", "Info"); if (!await AuthenticationService.LoginWithUsername(Username, UsernamePassword)) { await DialogService.ShowAlertAsync("Invalid Username/Password", AppResource.Alert_Error); } break; } await Task.Delay(500); if (AuthenticationService.IsAuthenticated) { var boxName = await PhantasmaService.GetMailboxFromAddress(); AuthenticationService.AuthenticatedUser.UserBox = boxName; if (string.IsNullOrEmpty(boxName)) { await NavigationService.NavigateToAsync <RegisterBoxViewModel>(); } else { await NavigationService.NavigateToAsync <MainViewModel>(); if (string.IsNullOrEmpty(await PhantasmaService.GetMailboxPublicKey(boxName))) { var registerPubKeyTx = await PhantasmaService.RegisterPublicKey(boxName, AuthenticationService.AuthenticatedUser.GetPublicKey().ToHexString()); } } } } catch (Exception ex) { if (ex is RpcClientUnknownException || ex is RpcClientTimeoutException) //todo switch error message { AppSettings.ChangeRpcServer(); } await DialogService.ShowAlertAsync(ex.Message, AppResource.Alert_Error); } finally { IsBusy = false; } }
private async Task SendMessageExecute() { if (IsBusy) { return; } if (string.IsNullOrEmpty(Message.Subject) || string.IsNullOrEmpty(Message.ToInbox) || string.IsNullOrEmpty(Message.TextContent)) { await DialogService.ShowAlertAsync("All fields are required", AppResource.Alert_Error); return; } var txHash = string.Empty; try { IsBusy = true; DialogService.ShowLoading(); var toAddress = await PhantasmaService.GetAddressFromMailbox(Message.ToInbox.ToLowerInvariant()); if (string.IsNullOrEmpty(toAddress)) { await DialogService.ShowAlertAsync("The specified inbox does not exist", AppResource.Alert_Error); return; } Message.ToAddress = toAddress; var hashedMessage = await SerializeAndHashMessage(); txHash = await PhantasmaService.SendMessage(Message.ToInbox.ToLowerInvariant(), hashedMessage); } catch (Exception ex) { await DialogService.ShowAlertAsync(ex.Message, AppResource.Alert_Error); } finally { UserDialogs.Instance.HideLoading(); IsBusy = false; } if (!string.IsNullOrEmpty(txHash)) { Message.Hash = txHash; //store to db var store = Message.ToStoreMessage(); if (store != null) { await _db.AddMessage(store); } if (_draftMessage != null) { await _db.DeleteMessage(_draftMessage); await DraftsVm.RefreshList(); } await DialogService.ShowAlertAsync( "Message sent! Use a block explorer to see your transaction: " + txHash, "Success"); await NavigationService.PopAllAsync(true); } else { await DialogService.ShowAlertAsync(AppResource.Alert_SomethingWrong, AppResource.Alert_Error); } }