Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
        }