コード例 #1
0
        MessageSubmitAccount GenerateSubmitAccount(Chain.Index index)
        {
            (var friendAccountId, var friendKeyIndex, var accountId, var keyIndex) = GetFriendAccountId(index);

            if (accountId != AccountId)
            {
                return(null);
            }

            var submitAccount = ServiceNode.GetSubmitAccount <MessageSubmitAccount>(keyIndex, index);

            if (submitAccount != null)
            {
                return(submitAccount);
            }

            foreach (var account in ServiceNode.ServiceAccounts.Values)
            {
                if (!account.IsDecrypted)
                {
                    continue;
                }

                if (account.AccountId == accountId && account.KeyIndex == keyIndex)
                {
                    submitAccount = new MessageSubmitAccount(account, this, friendAccountId, friendKeyIndex, keyIndex, index, true);
                    ServiceNode.AddSubmitAccount(submitAccount);

                    return(submitAccount);
                }
            }

            return(null);
        }
コード例 #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);
        }
コード例 #3
0
        public async Task <MessageSentEvent> SendMessage(Chat chat, string text)
        {
            var index         = chat.Index;
            var submitAccount = ServiceNode.GetSubmitAccount <MessageSubmitAccount>(chat.KeyIndex, index);

            if (submitAccount == null)
            {
                submitAccount = GenerateSubmitAccount(index);
            }

            await GenerateSecretExchangeKey(submitAccount, chat.FriendAccountId, chat.FriendKeyIndex);

            var result = await SetSubmitAccount(submitAccount, true);

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

            if (string.IsNullOrWhiteSpace(text))
            {
                result = new HeleusClientResponse(HeleusClientResultTypes.Ok, (long)ServiceUserCodes.InvalidAttachement);
                goto end;
            }

            var secretKey = submitAccount.DefaultSecretKey;
            var record    = await EncrytpedRecord <MessageRecord> .EncryptRecord(secretKey, new MessageRecord(text));

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

            transaction.EnableFeature <PreviousAccountTransaction>(PreviousAccountTransaction.FeatureId);

            var sharedAccountIndex = transaction.EnableFeature <SharedAccountIndex>(SharedAccountIndex.FeatureId);

            sharedAccountIndex.Index = index;

            var data = transaction.EnableFeature <Data>(Data.FeatureId);

            data.AddBinary(MessageServiceInfo.MessageDataIndex, record.ToByteArray());

            var receiver = transaction.EnableFeature <Receiver>(Receiver.FeatureId);

            receiver.AddReceiver(submitAccount.FriendAccountId);

            transaction.EnableFeature(EnforceReceiverFriend.FeatureId);

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

            if (result.TransactionResult == TransactionResultTypes.Ok)
            {
                if (!_chats.ContainsKey(chat.Index))
                {
                    _chats[chat.Index] = chat;
                    await SaveAsync();
                }

                UIApp.Run(() => chat.DownloadMessages(false));
            }

end:

            var @event = new MessageSentEvent(text, index, this, result);
            await UIApp.PubSub.PublishAsync(@event);

            return(@event);
        }