private async Task <UnpackedMessageContext> UnpackAsync(Wallet wallet, PackedMessageContext message, string senderKey)
        {
            UnpackResult unpacked;

            try
            {
                unpacked = await CryptoUtils.UnpackAsync(wallet, message.Payload);
            }
            catch (Exception e)
            {
                Logger.LogError("Failed to un-pack message", e);
                throw new AriesFrameworkException(ErrorCode.InvalidMessage, "Failed to un-pack message", e);
            }
            return(new UnpackedMessageContext(unpacked.Message, senderKey));
        }
        /// <inheritdoc />
        public async Task <PackedMessageContext> DispatchAsync(Uri endpointUri, PackedMessageContext message)
        {
            var request = new HttpRequestMessage
            {
                RequestUri = endpointUri,
                Method     = HttpMethod.Post,
                Content    = new ByteArrayContent(message.Payload)
            };

            var agentContentType = new MediaTypeHeaderValue(DefaultMessageService.AgentWireMessageMimeType);

            request.Content.Headers.ContentType = agentContentType;

            var response = await HttpClient.SendAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                var responseBody = await response.Content.ReadAsStringAsync();

                throw new AriesFrameworkException(
                          ErrorCode.A2AMessageTransmissionError, $"Dispatch Failure. Endpoint:{endpointUri} Status: {response.StatusCode} Content: {responseBody}");
            }

            if (response.Content?.Headers.ContentType?.Equals(agentContentType) ?? false)
            {
                var rawContent = await response.Content.ReadAsByteArrayAsync();

                //TODO this assumes all messages are packed
                if (rawContent.Length > 0)
                {
                    return(new PackedMessageContext(rawContent));
                }
            }

            return(null);
        }
Exemplo n.º 3
0
        private async Task <(UnpackedMessageContext, UnpackResult)> UnpackAsync(IAgentContext agentContext, PackedMessageContext message)
        {
            UnpackResult unpacked;

            try
            {
                unpacked = await CryptoUtils.UnpackAsync(agentContext.Wallet, message.Payload);
            }
            catch (Exception e)
            {
                Logger.LogError("Failed to un-pack message", e);
                throw new AriesFrameworkException(ErrorCode.InvalidMessage, "Failed to un-pack message", e);
            }

            UnpackedMessageContext result = null;

            if (unpacked.SenderVerkey != null && message.Connection == null)
            {
                try
                {
                    if (await ConnectionService.ResolveByMyKeyAsync(agentContext, unpacked.RecipientVerkey) is ConnectionRecord connection)
                    {
                        result = new UnpackedMessageContext(unpacked.Message, connection);
                    }
                    else
                    {
                        result = new UnpackedMessageContext(unpacked.Message, unpacked.SenderVerkey);
                    }
                }
                catch (AriesFrameworkException ex) when(ex.ErrorCode == ErrorCode.RecordNotFound)
                {
                    // OK if not resolved. Example: authpacked forward message in routing agent.
                    // Downstream consumers should throw if Connection is required
                }
            }
            else
            {
                if (message.Connection != null)
                {
                    result = new UnpackedMessageContext(unpacked.Message, message.Connection);
                }
                else
                {
                    result = new UnpackedMessageContext(unpacked.Message, unpacked.SenderVerkey);
                }
            }

            return(result, unpacked);
        }