Beispiel #1
0
        async Task <bool> Poll(TransactionDownload <Transaction> download, PollResult result)
        {
            var downloadResult = await download.DownloadTransactions();

            if (downloadResult.Ok)
            {
                foreach (var transactionDownload in downloadResult.Transactions)
                {
                    var transaction = transactionDownload.Transaction;
                    var type        = (transaction as DataTransaction).TransactionType;

                    if (transaction.HasFeatureRequest(FriendRequest.FriendRequestId))
                    {
                        result.UpdateFriends = true;
                    }
                    else if (transaction.TryGetFeature <SharedAccountIndex>(SharedAccountIndex.FeatureId, out var sharedAccountIndex))
                    {
                        var index = sharedAccountIndex.Index;
                        if (MessageServiceInfo.IsValidConversationIndex(index))
                        {
                            result.Indices.Add(index);
                        }
                    }
                }

                return(true);
            }

            return(false);
        }
Beispiel #2
0
        public bool GenerateSubmitAccounts(long friendAccountId, short friendKeyIndex)
        {
            foreach (var account in ServiceNode.ServiceAccounts.Values)
            {
                if (!account.IsDecrypted)
                {
                    continue;
                }

                var index         = MessageServiceInfo.GetConversationIndex(account.AccountId, account.KeyIndex, friendAccountId, friendKeyIndex);
                var submitAccount = ServiceNode.GetSubmitAccount <MessageSubmitAccount>(account.KeyIndex, index);
                if (submitAccount == null)
                {
                    submitAccount = new MessageSubmitAccount(account, this, friendAccountId, friendKeyIndex, account.KeyIndex, index, true);
                    ServiceNode.AddSubmitAccount(submitAccount);
                }
            }

            return(true);
        }
Beispiel #3
0
        public async Task <InboxRenameEvent> RenameInbox(short keyIndex, string title)
        {
            var submitAccount = ServiceNode.GetSubmitAccounts <SubmitAccount>(MessageServiceInfo.SubmitAccountIndex).FirstOrDefault();
            var result        = await SetSubmitAccount(submitAccount);

            if (result != null)
            {
                goto end;
            }

            if (string.IsNullOrEmpty(title) || title.Length > MessageServiceInfo.MaxInboxNameLength)
            {
                result = new HeleusClientResponse(HeleusClientResultTypes.Ok, (long)ServiceUserCodes.InboxNameInvalid);
                goto end;
            }

            var info = new DataTransaction(AccountId, ChainId, MessageServiceInfo.MessageDataChainIndex);

            info.PrivacyType = DataTransactionPrivacyType.PublicData;

            info.EnableFeature <AccountIndex>(AccountIndex.FeatureId).Index = MessageServiceInfo.GetInboxIndex(keyIndex);
            info.EnableFeature <Data>(Data.FeatureId).AddBinary(MessageServiceInfo.MessageDataIndex, new InboxNameRecord(true, title).ToByteArray());

            result = await _client.SendDataTransaction(info, true);

            if (result.TransactionResult == TransactionResultTypes.Ok)
            {
                UIApp.Run(() => DownloadInboxRecords(AccountId));
            }

end:

            var @event = new InboxRenameEvent(title, keyIndex, this, result);
            await UIApp.PubSub.PublishAsync(@event);

            return(@event);
        }
Beispiel #4
0
        async Task <bool> GenerateSecretExchangeKey(MessageSubmitAccount submitAccount, long friendAccountId, short friendKeyIndex)
        {
            if (submitAccount == null)
            {
                return(false);
            }

            //await GenerateSubmitAccounts(friendAccountId, friendKeyIndex, false);

            var index = MessageServiceInfo.GetConversationIndex(submitAccount.AccountId, submitAccount.KeyIndex, friendAccountId, friendKeyIndex);

            var keyManager = submitAccount.SecretKeyManager;

            if (!keyManager.HasSecretKeyType(index, SecretKeyInfoTypes.KeyExchange))
            {
                var friend = GetFriend(friendAccountId);
                if (friend != null)
                {
                    var key = await friend.GetSignedPublicKey(friendKeyIndex);

                    if (key != null)
                    {
                        var exchangeKey = Key.KeyExchange(KeyTypes.Ed25519, submitAccount.Account.DecryptedKey, key.PublicKey);
                        var secretKey   = await KeyExchageSecretKeyInfo.NewKeyExchangeSecetKey(submitAccount.AccountId, submitAccount.KeyIndex, friendAccountId, friendKeyIndex, submitAccount.ChainId, exchangeKey);

                        keyManager.AddSecretKey(index, secretKey);

                        return(true);
                    }
                }

                return(false);
            }

            return(true);
        }
Beispiel #5
0
        public async Task <InboxRecordDownloadEvent> DownloadInboxRecords(long accountId)
        {
            var items  = new List <InboxNameRecordInfo>();
            var result = MessageNodeEventResultTypes.Unknown;

            if (!ServiceNode.HasUnlockedServiceAccount)
            {
                result = MessageNodeEventResultTypes.NoUnlockedAccount;
                goto end;
            }

            var next = (await _client.DownloadNextServiceAccountKeyIndex(accountId, ChainId)).Data;

            if (next == null || !next.IsValid)
            {
                result = MessageNodeEventResultTypes.DownloadFailed;
                goto end;
            }

            var keyCount = next.Item - 1;
            var indices  = new List <Chain.Index>();

            for (short i = 0; i <= keyCount; i++)
            {
                indices.Add(MessageServiceInfo.GetInboxIndex(i));
            }

            var data = await AccountIndex.DownloadLastTransactionInfoIndicesBatch(_client, ChainType.Data, ChainId, MessageServiceInfo.MessageDataChainIndex, accountId, indices);

            if (data == null)
            {
                result = MessageNodeEventResultTypes.DownloadFailed;
                goto end;
            }

            if (data.ResultType != ResultTypes.Ok)
            {
                result = MessageNodeEventResultTypes.InvalidAccount;
                goto end;
            }

            var batch = data.Item;
            var count = batch.Count;

            for (short i = 0; i < count; i++)
            {
                InboxNameRecord record = null;
                (var success, _, var last) = batch.GetInfo(i);
                if (success)
                {
                    try
                    {
                        var transactionData = await ServiceNode.GetTransactionDownloadManager(MessageServiceInfo.MessageDataChainIndex).DownloadTransaction(last.TransactionId);

                        if (transactionData.Ok && transactionData.Count == 1 && transactionData.Transactions[0].Transaction.TryGetFeature <Data>(Data.FeatureId, out var featureData))
                        {
                            var recordData = featureData.Items[0].BinaryValue;
                            record = new InboxNameRecord(new Unpacker(recordData));
                        }
                    }
                    catch { }
                }

                items.Add(new InboxNameRecordInfo(i, record));
            }

            if (items.Count > 0)
            {
                result = MessageNodeEventResultTypes.Ok;
                if (accountId == AccountId)
                {
                    InboxNameRecords = items;
                }
            }
            else
            {
                result = MessageNodeEventResultTypes.InvalidAccount;
            }

end:

            var @event = new InboxRecordDownloadEvent(accountId, items, this, result);
            await UIApp.PubSub.PublishAsync(@event);

            return(@event);
        }