示例#1
0
        public async Task <HeleusClientResponse> UploadNote(SubmitAccount submitAccount, string text)
        {
            var serviceNode = submitAccount?.ServiceNode;
            var result      = await SetSubmitAccount(submitAccount, true);

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

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

            var secretKey    = submitAccount.DefaultSecretKey;
            var attachements = serviceNode.Client.NewAttachements(NoteServiceInfo.ChainIndex);
            var note         = await EncrytpedRecord <NoteRecord> .EncryptRecord(secretKey, new NoteRecord(text));

            attachements.AddBinaryAttachement(NoteServiceInfo.NoteFileName, note.ToByteArray());

            result = await serviceNode.Client.UploadAttachements(attachements, (transaction) =>
            {
                transaction.PrivacyType = DataTransactionPrivacyType.PrivateData;
                transaction.EnableFeature <AccountIndex>(AccountIndex.FeatureId).Index = NoteServiceInfo.NoteIndex;
            });

end:

            await UIApp.PubSub.PublishAsync(new NoteUploadedEvent(result));

            return(result);
        }
示例#2
0
        async Task <TodoRecordStorage <T> > GetDecryptedRecord <T>(Transaction transaction, long targetedTransactionId, long groupId, Unpacker unpacker) where T : Record
        {
            var encryptedRecord = new EncrytpedRecord <T>(unpacker);
            var secretKeyInfo   = encryptedRecord.KeyInfo;
            var secretKeys      = ServiceNode.GetSecretKeys(Index, secretKeyInfo.SecretId);

            foreach (var secretKey in secretKeys)
            {
                var record = await encryptedRecord.GetRecord(secretKey);

                if (record != null)
                {
                    LastUsedSecretKeyInfo = secretKeyInfo;
                    return(new TodoRecordStorage <T>(record, transaction.TransactionId, transaction.AccountId, transaction.Timestamp, targetedTransactionId, groupId));
                }
            }

            if (!MissingSecretKeys.ContainsKey(secretKeyInfo.SecretId))
            {
                MissingSecretKeys[secretKeyInfo.SecretId] = secretKeyInfo;
            }

            return(null);
        }
示例#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);
        }