コード例 #1
0
        private async Task AcknowledgeMessageAsync(
            OpenVaspPayload payload,
            string symKey,
            string asymKey,
            string returnTopic = null)
        {
            var envelopeId = Guid.NewGuid().ToString("N");
            var ackPayload = new OpenVaspPayload(
                Instruction.Ack,
                _vaspId, // receiver becomes a sender
                payload.ConnectionId,
                envelopeId)
            {
                EnvelopeAck = payload.EnvelopeId
            };

            var ackEnvelope = new MessageEnvelope
            {
                Topic          = returnTopic ?? payload.ReturnTopic,
                EncryptionKey  = symKey ?? asymKey,
                EncryptionType = symKey != null ? EncryptionType.Symmetric : EncryptionType.Asymmetric,
            };

            await _outboundEnvelopeService.AcknowledgeAsync(ackEnvelope, ackPayload.ToString());

            _logger?.LogInformation(
                $"Sent ACK to {payload.Instruction} with topic {ackEnvelope.Topic} for connection {payload.ConnectionId} from {_vaspId} to {payload.SenderVaspId}");
        }
コード例 #2
0
        public async Task SendAsync(string connectionId, string message, Instruction instruction, string receiverVaspId)
        {
            var connection = _connections[connectionId];

            var receiverVaspCode = (receiverVaspId ?? connection.CounterPartyVaspId).Substring(4, 8);

            var envelopeId = Guid.NewGuid().ToString("N");

            var payload = new OpenVaspPayload(
                instruction,
                _vaspId,
                connection.Id,
                envelopeId)
            {
                ReturnTopic = connection.InboundTopic,
                OvMessage   = message
            };

            if (instruction == Instruction.Invite || instruction == Instruction.Accept || instruction == Instruction.Deny)
            {
                payload.EcdhPk = ECDH_Key.ImportKey(connection.PrivateKey).PublicKey;
            }

            var topic = connection.OutboundTopic ?? receiverVaspCode;

            if (string.IsNullOrWhiteSpace(topic))
            {
                throw new InvalidOperationException($"Topic is empty for connection {connection.Id}");
            }

            var envelope = new MessageEnvelope
            {
                Topic = topic,
            };

            if (instruction == Instruction.Invite || instruction == Instruction.Close && string.IsNullOrWhiteSpace(connection.SymKeyId))
            {
                envelope.EncryptionType = EncryptionType.Asymmetric;

                var vaspTransportKey = await _vaspCodesService.GetTransportKeyAsync(receiverVaspCode);

                if (vaspTransportKey == null)
                {
                    throw new InvalidOperationException($"Transport key for vasp code {receiverVaspCode} cannot be found during message sending");
                }

                envelope.EncryptionKey = vaspTransportKey.DecompressPublicKey().ToHex(true);
            }
            else if (instruction == Instruction.Accept || instruction == Instruction.Deny ||
                     instruction == Instruction.Close && connection.Status == ConnectionStatus.PartiallyActive)
            {
                envelope.EncryptionType = EncryptionType.Asymmetric;
                envelope.EncryptionKey  = connection.CounterPartyPublicKey.DecompressPublicKey().ToHex(true);
            }
            else
            {
                envelope.EncryptionType = EncryptionType.Symmetric;
                envelope.EncryptionKey  = connection.SymKeyId;
            }

            var outboundEnvelope = new OutboundEnvelope
            {
                Id           = envelopeId,
                ConnectionId = connectionId,
                Envelope     = envelope,
                TotalResents = 0,
                Payload      = payload.ToString()
            };

            _openVaspPayloads[payload.EnvelopeId] = payload;

            await _outboundEnvelopeService.SendEnvelopeAsync(outboundEnvelope, instruction != Instruction.Deny);

            if (instruction == Instruction.Deny)
            {
                await DeactivateAsync(connectionId);
            }
        }