Exemple #1
0
        async Task OnAttachementsRequest(ClientAttachementsRequestMessage message, ClientConnection connection)
        {
            var result         = TransactionResultTypes.Unknown;
            var userCode       = 0L;
            var attachementKey = 0;

            var container    = _chainManager.GetContainer(message.ChainId);
            var dataChain    = container?.GetDataChain(message.ChainIndex);
            var serviceChain = container?.ServiceChain;
            var service      = container?.Service;

            if (container == null || dataChain == null || serviceChain == null)
            {
                result = TransactionResultTypes.ChainNodeInvalid;
                goto end;
            }

            if (service == null)
            {
                result = TransactionResultTypes.ChainServiceUnavailable;
                goto end;
            }

            attachementKey = dataChain.AttachementKey;
            if (attachementKey < 0)
            {
                result = TransactionResultTypes.AttachementsNotAllowed;
                goto end;
            }

            Key key = null;

            if (message.KeyIndex == Protocol.CoreAccountSignKeyIndex)
            {
                var coreAccount = _chainManager.CoreChain.GetCoreAccount(message.AccountId);
                key = coreAccount?.AccountKey;
            }
            else
            {
                var chainAccount = serviceChain.GetValidServiceAccountKey(message.AccountId, message.KeyIndex, Time.Timestamp);
                key = chainAccount?.PublicKey;
            }

            if (key == null)
            {
                result = TransactionResultTypes.InvalidServiceAccountKey;
                goto end;
            }

            var attachements = message.Attachements.GetSignedItem(key);

            if (attachements == null)
            {
                result = TransactionResultTypes.InvalidSignature;
                goto end;
            }

            if (!attachements.CheckAttachements() || AttachementInfo.IsExpired(attachements))
            {
                result = TransactionResultTypes.AttachementsInvalid;
                goto end;
            }

            var valid = await service.IsValidAttachementsRequest(attachements);

            userCode = valid.UserCode;
            if (!valid.IsOK)
            {
                result = TransactionResultTypes.ChainServiceErrorResponse;
                goto end;
            }

            if (!_attachementManager.AddAttachementsRequests(attachements))
            {
                result = TransactionResultTypes.AttachementsInvalid;
                goto end;
            }

            // everything seems to be OK
            result = TransactionResultTypes.Ok;

end:
            await connection.Send(new ClientAttachementsResponseMessage(message, result, userCode, attachementKey));
        }