/// <inheritdoc />
        public async Task <string> ProcessResponseAsync(IAgentContext agentContext, ConnectionResponseMessage response)
        {
            Logger.LogInformation(LoggingEvents.AcceptConnectionResponse, "To {1}", agentContext.Connection.MyDid);

            await Did.StoreTheirDidAsync(agentContext.Wallet,
                                         new { did = response.Did, verkey = response.Verkey }.ToJson());

            await Pairwise.CreateAsync(agentContext.Wallet, response.Did, agentContext.Connection.MyDid,
                                       response.Endpoint.ToJson());

            agentContext.Connection.TheirDid = response.Did;
            agentContext.Connection.TheirVk  = response.Verkey;

            if (response.Endpoint != null)
            {
                agentContext.Connection.Endpoint = response.Endpoint;
            }

            await agentContext.Connection.TriggerAsync(ConnectionTrigger.Response);

            await RecordService.UpdateAsync(agentContext.Wallet, agentContext.Connection);

            EventAggregator.Publish(new ServiceMessageProcessingEvent
            {
                RecordId    = agentContext.Connection.Id,
                MessageType = response.Type,
            });

            return(agentContext.Connection.Id);
        }
        /// <inheritdoc />
        public virtual async Task <ConnectionResponseMessage> AcceptRequestAsync(IAgentContext agentContext, string connectionId)
        {
            Logger.LogInformation(LoggingEvents.AcceptConnectionRequest, "ConnectionId {0}", connectionId);

            var connection = await GetAsync(agentContext, connectionId);

            if (connection.State != ConnectionState.Negotiating)
            {
                throw new AgentFrameworkException(ErrorCode.RecordInInvalidState,
                                                  $"Connection state was invalid. Expected '{ConnectionState.Negotiating}', found '{connection.State}'");
            }

            var connectionCopy = connection.DeepCopy();

            await Pairwise.CreateAsync(agentContext.Wallet, connection.TheirDid, connection.MyDid, connection.Endpoint.ToJson());

            await connection.TriggerAsync(ConnectionTrigger.Request);

            await RecordService.UpdateAsync(agentContext.Wallet, connection);

            // Send back response message
            var provisioning = await ProvisioningService.GetProvisioningAsync(agentContext.Wallet);

            return(new ConnectionResponseMessage
            {
                Did = connection.MyDid,
                Endpoint = provisioning.Endpoint,
                Verkey = connection.MyVk
            });
        }
Beispiel #3
0
        /// <inheritdoc />
        public async Task ProcessResponseAsync(Wallet wallet, ConnectionResponseMessage response)
        {
            Logger.LogInformation(LoggingEvents.AcceptConnectionResponse, "To {0}", response.To);

            var(didOrKey, _) = MessageUtils.ParseMessageType(response.Type);

            var connectionSearch = await RecordService.SearchAsync <ConnectionRecord>(wallet,
                                                                                      new SearchRecordQuery { { TagConstants.MyDid, didOrKey } }, null, 1);

            var connection = connectionSearch.Single();
            await connection.TriggerAsync(ConnectionTrigger.Response);

            var(connectionDetails, _) = await MessageSerializer.UnpackSealedAsync <ConnectionDetails>(response.Content,
                                                                                                      wallet, connection.MyVk);

            await Did.StoreTheirDidAsync(wallet,
                                         new { did = connectionDetails.Did, verkey = connectionDetails.Verkey }.ToJson());

            await Pairwise.CreateAsync(wallet, connectionDetails.Did, connection.MyDid,
                                       connectionDetails.Endpoint.ToJson());

            connection.TheirDid = connectionDetails.Did;
            connection.TheirVk  = connectionDetails.Verkey;

            if (connectionDetails.Endpoint != null)
            {
                connection.Endpoint = connectionDetails.Endpoint;
            }

            connection.Tags.Add(TagConstants.TheirDid, connectionDetails.Did);
            await RecordService.UpdateAsync(wallet, connection);
        }
Beispiel #4
0
        public async Task TestListPairwiseWorksForEmptyResult()
        {
            var listPairwise = await Pairwise.ListAsync(wallet);

            var listPairwiseArray = JArray.Parse(listPairwise);

            Assert.AreEqual(0, listPairwiseArray.Count);
        }
Beispiel #5
0
        public async Task TestListPairwiseWorks()
        {
            await Pairwise.CreateAsync(wallet, theirDid, myDid, null);

            var listPairwise = await Pairwise.ListAsync(wallet);

            var listPairwiseArray = JArray.Parse(listPairwise);

            Assert.AreEqual(1, listPairwiseArray.Count);
            Assert.AreEqual(listPairwiseArray[0].ToString(), string.Format(PAIR_TEMPLATE, myDid, theirDid));
        }
Beispiel #6
0
        public async Task TestGetPairwiseWorks()
        {
            await Pairwise.CreateAsync(wallet, theirDid, myDid, METADATA);

            var pairwiseInfoJson = await Pairwise.GetAsync(wallet, theirDid);

            var pairwiseInfo = JObject.Parse(pairwiseInfoJson);

            Assert.AreEqual(myDid, pairwiseInfo.Value <string>("my_did"));
            Assert.AreEqual(METADATA, pairwiseInfo.Value <string>("metadata"));
        }
Beispiel #7
0
        public async Task TestGetPairwiseWorksWhenNoMetadataIsPresent()
        {
            await Pairwise.CreateAsync(wallet, theirDid, myDid, null);

            var pairwiseInfoJson = await Pairwise.GetAsync(wallet, theirDid);

            var pairwiseInfo = JObject.Parse(pairwiseInfoJson);

            Assert.AreEqual(myDid, pairwiseInfo.Value <string>("my_did"));
            Assert.IsNull(pairwiseInfo["metadata"]);
        }
Beispiel #8
0
        public async Task TestSetPairwiseMetadataWorks()
        {
            await Pairwise.CreateAsync(wallet, theirDid, myDid, null);

            var pairwiseWithoutMetadata = await Pairwise.GetAsync(wallet, theirDid);

            await Pairwise.SetMetadataAsync(wallet, theirDid, METADATA);

            var pairwiseWithMetadata = await Pairwise.GetAsync(wallet, theirDid);

            Assert.AreNotEqual(pairwiseWithoutMetadata, pairwiseWithMetadata);
            Assert.AreEqual(string.Format(PAIRWISE_TEMPLATE, myDid, METADATA), pairwiseWithMetadata);
        }
Beispiel #9
0
        [Ignore] //Bug in SDK?
        public async Task TestSetPairwiseMetadataWorksWithEmptyString()
        {
            await Pairwise.CreateAsync(wallet, theirDid, myDid, METADATA);

            var pairwiseWithMetadata = await Pairwise.GetAsync(wallet, theirDid);

            await Pairwise.SetMetadataAsync(wallet, theirDid, string.Empty);

            var pairwiseWithoutMetadata = await Pairwise.GetAsync(wallet, theirDid);

            var pairwiseInfo = JObject.Parse(pairwiseWithoutMetadata);

            Assert.AreNotEqual(pairwiseWithoutMetadata, pairwiseWithMetadata);
            Assert.AreEqual(string.Empty, pairwiseInfo.Value <string>("metadata"));
        }
Beispiel #10
0
        [Ignore] //Bug in SDK?
        public async Task TestSetPairwiseMetadataWorksWithNull()
        {
            await Pairwise.CreateAsync(wallet, theirDid, myDid, METADATA);

            var pairwiseWithMetadata = await Pairwise.GetAsync(wallet, theirDid);

            await Pairwise.SetMetadataAsync(wallet, theirDid, null);

            var pairwiseWithoutMetadata = await Pairwise.GetAsync(wallet, theirDid);

            var pairwiseInfo = JObject.Parse(pairwiseWithoutMetadata);

            Assert.AreNotEqual(pairwiseWithoutMetadata, pairwiseWithMetadata);
            Assert.IsNull(pairwiseInfo["metadata"]);
        }
        /// <inheritdoc />
        public virtual async Task <(ConnectionResponseMessage, ConnectionRecord)> CreateResponseAsync(IAgentContext agentContext, string connectionId)
        {
            Logger.LogInformation(LoggingEvents.AcceptConnectionRequest, "ConnectionId {0}", connectionId);

            var connection = await GetAsync(agentContext, connectionId);

            if (connection.State != ConnectionState.Negotiating)
            {
                throw new AgentFrameworkException(ErrorCode.RecordInInvalidState,
                                                  $"Connection state was invalid. Expected '{ConnectionState.Negotiating}', found '{connection.State}'");
            }

            await Pairwise.CreateAsync(agentContext.Wallet, connection.TheirDid, connection.MyDid, connection.Endpoint.ToJson());

            await connection.TriggerAsync(ConnectionTrigger.Request);

            await RecordService.UpdateAsync(agentContext.Wallet, connection);

            // Send back response message
            var provisioning = await ProvisioningService.GetProvisioningAsync(agentContext.Wallet);

            string responseEndpoint = string.Empty;
            var    records          = await CloudAgentRegistrationService.GetAllCloudAgentAsync(agentContext.Wallet);

            if (records.Count > 0)
            {
                var record = CloudAgentRegistrationService.getRandomCloudAgent(records);
                responseEndpoint = record.Endpoint.ResponseEndpoint + "/" + record.MyConsumerId;
            }

            var connectionData = new Connection
            {
                Did    = connection.MyDid,
                DidDoc = connection.MyDidDoc(provisioning, responseEndpoint)
            };

            var sigData = await SignatureUtils.SignData(agentContext, connectionData, connection.GetTag(TagConstants.ConnectionKey));

            var threadId = connection.GetTag(TagConstants.LastThreadId);

            var response = new ConnectionResponseMessage {
                ConnectionSig = sigData
            };

            response.ThreadFrom(threadId);

            return(response, connection);
        }
Beispiel #12
0
        /// <inheritdoc />
        public virtual async Task AcceptRequestAsync(Wallet wallet, string connectionId)
        {
            Logger.LogInformation(LoggingEvents.AcceptConnectionRequest, "ConnectionId {0}", connectionId);

            var connection = await GetAsync(wallet, connectionId);

            await connection.TriggerAsync(ConnectionTrigger.Request);

            await Pairwise.CreateAsync(wallet, connection.TheirDid, connection.MyDid, connection.Endpoint.ToJson());

            await RecordService.UpdateAsync(wallet, connection);

            // Send back response message
            var provisioning = await ProvisioningService.GetProvisioningAsync(wallet);

            var response = new ConnectionDetails
            {
                Did      = connection.MyDid,
                Endpoint = provisioning.Endpoint,
                Verkey   = connection.MyVk
            };

            var responseMessage =
                await MessageSerializer.PackSealedAsync <ConnectionResponseMessage>(response, wallet, connection.MyVk,
                                                                                    connection.TheirVk);

            responseMessage.Type =
                MessageUtils.FormatDidMessageType(connection.TheirDid, MessageTypes.ConnectionResponse);
            responseMessage.To = connection.TheirDid;

            var forwardMessage = new ForwardEnvelopeMessage
            {
                Type    = MessageUtils.FormatDidMessageType(connection.TheirDid, MessageTypes.Forward),
                Content = responseMessage.ToJson()
            };

            await RouterService.ForwardAsync(forwardMessage, connection.Endpoint);
        }
        /// <inheritdoc />
        public virtual async Task <string> ProcessResponseAsync(IAgentContext agentContext, ConnectionResponseMessage response, ConnectionRecord connection)
        {
            Logger.LogInformation(LoggingEvents.AcceptConnectionResponse, "To {1}", connection.MyDid);

            //TODO throw exception or a problem report if the connection request features a did doc that has no indy agent did doc convention featured
            //i.e there is no way for this agent to respond to messages. And or no keys specified
            var connectionObj = SignatureUtils.UnpackAndVerifyData <Connection>(response.ConnectionSig);

            await Did.StoreTheirDidAsync(agentContext.Wallet,
                                         new { did = connectionObj.Did, verkey = connectionObj.DidDoc.Keys[0].PublicKeyBase58 }.ToJson());

            await Pairwise.CreateAsync(agentContext.Wallet, connectionObj.Did, connection.MyDid,
                                       connectionObj.DidDoc.Services[0].ServiceEndpoint);

            connection.TheirDid = connectionObj.Did;
            connection.TheirVk  = connectionObj.DidDoc.Keys[0].PublicKeyBase58;

            connection.SetTag(TagConstants.LastThreadId, response.GetThreadId());

            if (connectionObj.DidDoc.Services[0] is IndyAgentDidDocService service)
            {
                connection.Endpoint = new AgentEndpoint(service.ServiceEndpoint, null, service.RoutingKeys != null && service.RoutingKeys.Count > 0 ? service.RoutingKeys[0] : null);
            }

            await connection.TriggerAsync(ConnectionTrigger.Response);

            await RecordService.UpdateAsync(agentContext.Wallet, connection);

            EventAggregator.Publish(new ServiceMessageProcessingEvent
            {
                RecordId    = connection.Id,
                MessageType = response.Type,
                ThreadId    = response.GetThreadId()
            });

            return(connection.Id);
        }
        async Task CheckCloudMessages()
        {
            // Check my cloud for new messages
            var messages = await SendToCloudAgent(StreetcredMessages.GET_MESSAGES, new GetMessages());

            var getMessages = new GetMessagesResponse();

            getMessages.MergeFrom(messages.Content);

            foreach (var message in getMessages.Messages)
            {
                switch (message.Type)
                {
                case SovrinMessages.CONNECTION_RESPONSE:
                {
                    var decrypted = await Crypto.AnonDecryptAsync(wallet, myVk, message.Content.ToByteArray());

                    var theirKey = await Did.KeyForDidAsync(pool, wallet, message.Origin);

                    await Did.StoreTheirDidAsync(wallet, JsonConvert.SerializeObject(new { did = message.Origin, verkey = theirKey }));

                    await Pairwise.CreateAsync(wallet, message.Origin, myDid, null);

                    var ack = new ConnectionAcknowledgement();
                    ack.Message = "Success";

                    var encrypted = await Crypto.AuthCryptAsync(wallet, myVk, theirKey, ack.ToByteArray());

                    Console.WriteLine($"Sending connection acknowledgement to {agentDid} for {message.Origin} connection");

                    await SendToOrganizationAgent(SovrinMessages.CONNECTION_ACKONOWLEDGEMENT, message.Origin, agentDid, encrypted);

                    break;
                }
                }
            }
        }
 public async Task TestPairwiseExistsWorksForNotCreated()
 {
     Assert.IsFalse(await Pairwise.IsExistsAsync(wallet, theirDid));
 }
        public async Task TestPairwiseExistsWorks()
        {
            await Pairwise.CreateAsync(wallet, theirDid, myDid, null);

            Assert.IsTrue(await Pairwise.IsExistsAsync(wallet, theirDid));
        }
Beispiel #17
0
 public async Task TestCreatePairwiseWorksForNotFoundTheirDid()
 {
     var ex = await Assert.ThrowsExceptionAsync <WalletValueNotFoundException>(() =>
                                                                               Pairwise.CreateAsync(wallet, DID1, myDid, null)
                                                                               );
 }
Beispiel #18
0
 public async Task TestCreatePairwiseWorksForEmptyMetadata()
 {
     await Pairwise.CreateAsync(wallet, theirDid, myDid, null);
 }
Beispiel #19
0
 public async Task TestCreatePairwiseWorks()
 {
     await Pairwise.CreateAsync(wallet, theirDid, myDid, METADATA);
 }
Beispiel #20
0
 public async Task TestGetPairwiseWorksForNotCreated()
 {
     var ex = await Assert.ThrowsExceptionAsync <WalletValueNotFoundException>(() =>
                                                                               Pairwise.GetAsync(wallet, theirDid)
                                                                               );
 }
Beispiel #21
0
 public async Task TestGetPairwiseWalletItemNotFoundExceptionForNotCreated()
 {
     var ex = await Assert.ThrowsExceptionAsync <WalletItemNotFoundException>(() =>
                                                                              Pairwise.GetAsync(wallet, theirVerkey)
                                                                              );
 }
Beispiel #22
0
 public async Task TestSetPairwiseMetadataWorksForNotCreatedPairwise()
 {
     var ex = await Assert.ThrowsExceptionAsync <WalletItemNotFoundException>(() =>
                                                                              Pairwise.SetMetadataAsync(wallet, theirVerkey, METADATA)
                                                                              );
 }
 public async Task TestCreatePairwiseWalletItemNotFoundExceptionForNotFoundMyDid()
 {
     var ex = await Assert.ThrowsExceptionAsync <WalletItemNotFoundException>(() =>
                                                                              Pairwise.CreateAsync(wallet, theirVerkey, DID1, null)
                                                                              );
 }