Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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();
        }
Exemplo n.º 3
0
        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();
        }
Exemplo n.º 4
0
        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;
            }
        }