public static async Task ProofProtocolAsync(MockAgent requestor, MockAgent holder, ConnectionRecord requestorConnection, ConnectionRecord holderConnection, ProofRequest proofRequest) { var proofService = requestor.GetService <IProofService>(); var messageService = requestor.GetService <IMessageService>(); // Hook into message event var requestSlim = new SemaphoreSlim(0, 1); holder.GetService <IEventAggregator>().GetEventByType <ServiceMessageProcessingEvent>() .Where(x => x.MessageType == MessageTypes.PresentProofNames.RequestPresentation) .Subscribe(x => requestSlim.Release()); var(requestMsg, requestorRecord) = await proofService.CreateRequestAsync(requestor.Context, proofRequest, requestorConnection.Id); await messageService.SendAsync(requestor.Context, requestMsg, requestorConnection); await requestSlim.WaitAsync(TimeSpan.FromSeconds(30)); var holderRequests = await proofService.ListRequestedAsync(holder.Context); Assert.NotNull(holderRequests); Assert.True(holderRequests.Count > 0); // Hook into message event var proofSlim = new SemaphoreSlim(0, 1); requestor.GetService <IEventAggregator>().GetEventByType <ServiceMessageProcessingEvent>() .Where(x => x.MessageType == MessageTypes.PresentProofNames.Presentation) .Subscribe(x => requestSlim.Release()); var record = holderRequests.FirstOrDefault(); var request = JsonConvert.DeserializeObject <ProofRequest>(record.RequestJson); var requestedCredentials = await ProofServiceUtils.GetAutoRequestedCredentialsForProofCredentials(holder.Context, proofService, request); var(proofMsg, holderRecord) = await proofService.CreatePresentationAsync(holder.Context, record.Id, requestedCredentials); await messageService.SendAsync(holder.Context, proofMsg, holderConnection); await proofSlim.WaitAsync(TimeSpan.FromSeconds(30)); var requestorProofRecord = await proofService.GetAsync(requestor.Context, requestorRecord.Id); var holderProofRecord = await proofService.GetAsync(holder.Context, holderRecord.Id); Assert.True(requestorProofRecord.State == ProofState.Accepted); Assert.True(holderProofRecord.State == ProofState.Accepted); var isProofValid = await proofService.VerifyProofAsync(requestor.Context, requestorProofRecord.Id); Assert.True(isProofValid); }
public static async Task ProofProtocolConnectionlessAsync(MockAgent requestor, MockAgent holder, ProofRequest proofRequest, bool useDidKeyFormat) { var proofService = requestor.GetService <IProofService>(); var(requestMsg, requestorRecord) = await proofService.CreateRequestAsync(requestor.Context, proofRequest, useDidKeyFormat : useDidKeyFormat); var requestAttachment = requestMsg.Requests.FirstOrDefault(x => x.Id == "libindy-request-presentation-0") ?? throw new ArgumentException("Presentation request attachment not found."); var requestJson = requestAttachment.Data.Base64.GetBytesFromBase64().GetUTF8String(); var request = JsonConvert.DeserializeObject <ProofRequest>(requestJson); var requestedCredentials = await ProofServiceUtils.GetAutoRequestedCredentialsForProofCredentials(holder.Context, proofService, request); var holderRecord = await proofService.CreatePresentationAsync(holder.Context, requestMsg, requestedCredentials); var requestorProofRecord = await proofService.GetAsync(requestor.Context, requestorRecord.Id); var holderProofRecord = await proofService.GetAsync(holder.Context, holderRecord.Id); Assert.True(requestorProofRecord.State == ProofState.Accepted); Assert.True(holderProofRecord.State == ProofState.Accepted); var isProofValid = await proofService.VerifyProofAsync(requestor.Context, requestorProofRecord.Id); Assert.True(isProofValid); }
public static async Task <(ConnectionRecord inviteeConnection, ConnectionRecord inviterConnection)> EstablishConnectionWithReturnRoutingAsync(MockAgent invitee, MockAgent inviter, bool useDidKeyFormat = false) { var slim = new SemaphoreSlim(0, 1); var connectionService = inviter.GetService <IConnectionService>(); var messageService = inviter.GetService <IMessageService>(); // Hook into response message event of second runtime to release semaphore invitee.GetService <IEventAggregator>().GetEventByType <ServiceMessageProcessingEvent>() .Where(x => x.MessageType == MessageTypes.ConnectionResponse) .Subscribe(x => slim.Release()); var(invitation, inviteeConnection) = await connectionService.CreateInvitationAsync(inviter.Context, new InviteConfiguration { AutoAcceptConnection = true, UseDidKeyFormat = useDidKeyFormat }); var(request, inviterConnection) = await connectionService.CreateRequestAsync(invitee.Context, invitation); var response = await messageService.SendReceiveAsync <ConnectionResponseMessage>(invitee.Context, request, inviterConnection); Assert.NotNull(response); await connectionService.ProcessResponseAsync(invitee.Context, response, inviterConnection); await slim.WaitAsync(TimeSpan.FromSeconds(30)); var ackMessage = await connectionService.CreateAcknowledgementMessageAsync(invitee.Context, inviterConnection.Id); await messageService.SendAsync(invitee.Context, ackMessage, inviterConnection); await slim.WaitAsync(TimeSpan.FromSeconds(30)); var inviteeConnectionRecord = await connectionService.GetAsync(inviter.Context, inviteeConnection.Id); var inviterConnectionRecord = await connectionService.GetAsync(invitee.Context, inviterConnection.Id); Assert.Equal(ConnectionState.Connected, inviteeConnectionRecord.State); Assert.Equal(ConnectionState.Connected, inviterConnectionRecord.State); Assert.Equal(inviteeConnectionRecord.MyDid, inviterConnectionRecord.TheirDid); Assert.Equal(inviteeConnectionRecord.TheirDid, inviterConnectionRecord.MyDid); Assert.Equal( inviteeConnectionRecord.GetTag(TagConstants.LastThreadId), inviterConnectionRecord.GetTag(TagConstants.LastThreadId)); return(inviteeConnectionRecord, inviterConnectionRecord); }
public static async Task <DiscoveryDiscloseMessage> DiscoveryProtocolWithReturnRoutingAsync(MockAgent requestor, MockAgent holder, ConnectionRecord requestorConnection, ConnectionRecord holderConnection) { var discoveryService = requestor.GetService <IDiscoveryService>(); var messageService = requestor.GetService <IMessageService>(); //Ask for all protocols var msg = discoveryService.CreateQuery(requestor.Context, "*"); var rsp = await messageService.SendAsync(requestor.Context.Wallet, msg, requestorConnection, null, true); Assert.NotNull(rsp); var discoveryMsg = rsp.GetMessage <DiscoveryDiscloseMessage>(); Assert.NotNull(discoveryMsg); return(discoveryMsg); }
public static async Task <(ConnectionRecord inviteeConnection, ConnectionRecord inviterConnection)> EstablishConnectionWithReturnRoutingAsync(MockAgent invitee, MockAgent inviter) { var slim = new SemaphoreSlim(0, 1); var connectionService = invitee.GetService <IConnectionService>(); var messsageService = invitee.GetService <IMessageService>(); // Hook into response message event of second runtime to release semaphore inviter.GetService <IEventAggregator>().GetEventByType <ServiceMessageProcessingEvent>() .Where(x => x.MessageType == MessageTypes.ConnectionResponse) .Subscribe(x => slim.Release()); (var invitation, var inviterConnection) = await connectionService.CreateInvitationAsync(invitee.Context, new InviteConfiguration { AutoAcceptConnection = true }); (var request, var inviteeConnection) = await connectionService.CreateRequestAsync(inviter.Context, invitation); var response = await messsageService.SendAsync(inviter.Context.Wallet, request, inviteeConnection, invitation.RecipientKeys.First(), true); Assert.NotNull(response); await inviter.HandleInboundAsync(response); await slim.WaitAsync(TimeSpan.FromSeconds(30)); var connectionRecord1 = await connectionService.GetAsync(invitee.Context, inviterConnection.Id); var connectionRecord2 = await connectionService.GetAsync(inviter.Context, inviteeConnection.Id); Assert.Equal(ConnectionState.Connected, connectionRecord1.State); Assert.Equal(ConnectionState.Connected, connectionRecord2.State); Assert.Equal(connectionRecord1.MyDid, connectionRecord2.TheirDid); Assert.Equal(connectionRecord1.TheirDid, connectionRecord2.MyDid); Assert.Equal( connectionRecord1.GetTag(TagConstants.LastThreadId), connectionRecord2.GetTag(TagConstants.LastThreadId)); return(connectionRecord1, connectionRecord2); }
public static async Task <DiscoveryDiscloseMessage> DiscoveryProtocolWithReturnRoutingAsync(MockAgent requestor, MockAgent holder, ConnectionRecord requestorConnection, ConnectionRecord holderConnection) { var discoveryService = requestor.GetService <IDiscoveryService>(); var messageService = requestor.GetService <IMessageService>(); //Ask for all protocols var msg = discoveryService.CreateQuery(requestor.Context, "*"); var rsp = await messageService.SendReceiveAsync(requestor.Context, msg, requestorConnection); Assert.NotNull(rsp); if (rsp is UnpackedMessageContext messageContext) { var discoveryMsg = messageContext.GetMessage <DiscoveryDiscloseMessage>(); Assert.NotNull(discoveryMsg); return(discoveryMsg); } throw new InvalidOperationException("The response was not of the expected type"); }
public static async Task <(ConnectionRecord inviteeConnection, ConnectionRecord inviterConnection)> EstablishConnectionAsync(MockAgent invitee, MockAgent inviter, bool useDidKeyFormat = false) { var slim = new SemaphoreSlim(0, 1); var connectionService = invitee.GetService <IConnectionService>(); var messageService = invitee.GetService <IMessageService>(); // Hook into response message event of second runtime to release semaphore inviter.GetService <IEventAggregator>().GetEventByType <ServiceMessageProcessingEvent>() .Where(x => x.MessageType == MessageTypes.ConnectionResponse) .Subscribe(x => slim.Release()); (var invitation, var inviterConnection) = await connectionService.CreateInvitationAsync(invitee.Context, new InviteConfiguration { AutoAcceptConnection = true, UseDidKeyFormat = useDidKeyFormat }); var(request, inviteeConnection) = await connectionService.CreateRequestAsync(inviter.Context, invitation); await messageService.SendAsync(inviter.Context, request, inviteeConnection); // Wait for connection to be established or continue after 30 sec timeout await slim.WaitAsync(TimeSpan.FromSeconds(30)); var connectionRecord1 = await connectionService.GetAsync(invitee.Context, inviterConnection.Id); var connectionRecord2 = await connectionService.GetAsync(inviter.Context, inviteeConnection.Id); Assert.Equal(ConnectionState.Connected, connectionRecord1.State); Assert.Equal(ConnectionState.Connected, connectionRecord2.State); Assert.Equal(connectionRecord1.MyDid, connectionRecord2.TheirDid); Assert.Equal(connectionRecord1.TheirDid, connectionRecord2.MyDid); Assert.Equal( connectionRecord1.GetTag(TagConstants.LastThreadId), connectionRecord2.GetTag(TagConstants.LastThreadId)); return(connectionRecord1, connectionRecord2); }
public static async Task IssueCredentialConnectionlessAsync(MockAgent issuer, MockAgent holder, List <CredentialPreviewAttribute> credentialAttributes, bool useDidKeyFormat) { var credentialService = issuer.GetService <ICredentialService>(); var schemaService = issuer.GetService <ISchemaService>(); var provisionService = issuer.GetService <IProvisioningService>(); var issuerProv = await provisionService.GetProvisioningAsync(issuer.Context.Wallet); var(definitionId, _) = await Scenarios.CreateDummySchemaAndNonRevokableCredDef(issuer.Context, schemaService, issuerProv.IssuerDid, credentialAttributes.Select(_ => _.Name).ToArray()); (var offer, var issuerCredentialRecord) = await credentialService.CreateOfferAsync( agentContext : issuer.Context, config : new OfferConfiguration { IssuerDid = issuerProv.IssuerDid, CredentialDefinitionId = definitionId, CredentialAttributeValues = credentialAttributes, UseDidKeyFormat = useDidKeyFormat }); var holderCredentialRecord = await credentialService.CreateCredentialAsync(holder.Context, offer); Assert.NotNull(holderCredentialRecord.CredentialAttributesValues); Assert.True(holderCredentialRecord.CredentialAttributesValues.Count() == 2); var issuerCredRecord = await credentialService.GetAsync(issuer.Context, issuerCredentialRecord.Id); var holderCredRecord = await credentialService.GetAsync(holder.Context, holderCredentialRecord.Id); Assert.Equal(CredentialState.Issued, issuerCredRecord.State); Assert.Equal(CredentialState.Issued, holderCredRecord.State); Assert.Equal( issuerCredRecord.GetTag(TagConstants.LastThreadId), holderCredRecord.GetTag(TagConstants.LastThreadId)); }
public static async Task IssueCredentialAsync(MockAgent issuer, MockAgent holder, ConnectionRecord issuerConnection, ConnectionRecord holderConnection, List <CredentialPreviewAttribute> credentialAttributes) { var credentialService = issuer.GetService <ICredentialService>(); var messageService = issuer.GetService <IMessageService>(); var schemaService = issuer.GetService <ISchemaService>(); var provisionService = issuer.GetService <IProvisioningService>(); // Hook into message event var offerSlim = new SemaphoreSlim(0, 1); holder.GetService <IEventAggregator>().GetEventByType <ServiceMessageProcessingEvent>() .Where(x => x.MessageType == MessageTypes.IssueCredentialNames.OfferCredential) .Subscribe(x => offerSlim.Release()); var issuerProv = await provisionService.GetProvisioningAsync(issuer.Context.Wallet); var(definitionId, _) = await Scenarios.CreateDummySchemaAndNonRevokableCredDef(issuer.Context, schemaService, issuerProv.IssuerDid, credentialAttributes.Select(_ => _.Name).ToArray()); var(offer, issuerCredentialRecord) = await credentialService.CreateOfferAsync( agentContext : issuer.Context, config : new OfferConfiguration { IssuerDid = issuerProv.IssuerDid, CredentialDefinitionId = definitionId, CredentialAttributeValues = credentialAttributes, }, connectionId : issuerConnection.Id); await messageService.SendAsync(issuer.Context, offer, issuerConnection); await offerSlim.WaitAsync(TimeSpan.FromSeconds(30)); var offers = await credentialService.ListOffersAsync(holder.Context); Assert.NotNull(offers); Assert.True(offers.Count > 0); // Hook into message event var requestSlim = new SemaphoreSlim(0, 1); issuer.GetService <IEventAggregator>().GetEventByType <ServiceMessageProcessingEvent>() .Where(x => x.MessageType == MessageTypes.IssueCredentialNames.RequestCredential) .Subscribe(x => requestSlim.Release()); var(request, holderCredentialRecord) = await credentialService.CreateRequestAsync(holder.Context, offers[0].Id); Assert.NotNull(holderCredentialRecord.CredentialAttributesValues); Assert.True(holderCredentialRecord.CredentialAttributesValues.Count() == 2); await messageService.SendAsync(holder.Context, request, holderConnection); await requestSlim.WaitAsync(TimeSpan.FromSeconds(30)); // Hook into message event var credentialSlim = new SemaphoreSlim(0, 1); holder.GetService <IEventAggregator>().GetEventByType <ServiceMessageProcessingEvent>() .Where(x => x.MessageType == MessageTypes.IssueCredentialNames.IssueCredential) .Subscribe(x => credentialSlim.Release()); var(cred, _) = await credentialService.CreateCredentialAsync( agentContext : issuer.Context, credentialId : issuerCredentialRecord.Id); await messageService.SendAsync(issuer.Context, cred, issuerConnection); await credentialSlim.WaitAsync(TimeSpan.FromSeconds(30)); var issuerCredRecord = await credentialService.GetAsync(issuer.Context, issuerCredentialRecord.Id); var holderCredRecord = await credentialService.GetAsync(holder.Context, holderCredentialRecord.Id); Assert.Equal(CredentialState.Issued, issuerCredRecord.State); Assert.Equal(CredentialState.Issued, holderCredRecord.State); Assert.Equal( issuerCredRecord.GetTag(TagConstants.LastThreadId), holderCredRecord.GetTag(TagConstants.LastThreadId)); var ackSlim = new SemaphoreSlim(0, 1); holder.GetService <IEventAggregator>().GetEventByType <ServiceMessageProcessingEvent>() .Where(x => x.MessageType == MessageTypes.IssueCredentialNames.AcknowledgeCredential) .Subscribe(x => ackSlim.Release()); var ackMessage = await credentialService.CreateAcknowledgementMessageAsync(holder.Context, holderCredentialRecord.Id); await messageService.SendAsync(holder.Context, ackMessage, holderConnection); await ackSlim.WaitAsync(TimeSpan.FromSeconds(30)); Assert.Equal(ackMessage.Id, issuerCredRecord.GetTag(TagConstants.LastThreadId)); }
public async Task CanExchangeDid() { var slim = new SemaphoreSlim(0, 1); var didExchangeService = _requester.GetService <IDidExchangeService>(); var connectionService = _requester.GetService <IConnectionService>(); var messsageService = _requester.GetService <IMessageService>(); // Did Exchange Request _responder.GetService <IEventAggregator>().GetEventByType <ServiceMessageProcessingEvent>() .Where(x => x.MessageType == MessageTypesHttps.DidExchange.Request) .Subscribe(x => slim.Release()); (var request, var requesterConnection) = await didExchangeService.CreateRequestAsync(_requester.Context, TestConstants.StewardDid); await messsageService.SendAsync(_requester.Context, request, requesterConnection); await slim.WaitAsync(TimeSpan.FromSeconds(30)); var requesterRecord = await connectionService.GetAsync(_requester.Context, requesterConnection.Id); var responderRecord = (await connectionService.ListAsync(_responder.Context, SearchQuery.Equal(nameof(ConnectionRecord.TheirDid), requesterConnection.MyDid))).First(); Assert.Equal(ConnectionState.Negotiating, responderRecord.State); Assert.Equal(ConnectionState.Negotiating, requesterRecord.State); Assert.Equal(requesterRecord.TheirDid, TestConstants.StewardDid); Assert.Equal(responderRecord.TheirDid, requesterRecord.MyDid); Assert.Equal( requesterRecord.GetTag(TagConstants.LastThreadId), responderRecord.GetTag(TagConstants.LastThreadId)); // Did Exchange Response _requester.GetService <IEventAggregator>().GetEventByType <ServiceMessageProcessingEvent>() .Where(x => x.MessageType == MessageTypesHttps.DidExchange.Response) .Subscribe(x => slim.Release()); var(response, newResponderRecord) = await didExchangeService.CreateResponseAsync(_responder.Context, responderRecord); await messsageService.SendAsync(_responder.Context, response, newResponderRecord); await slim.WaitAsync(TimeSpan.FromSeconds(30)); var newRequesterRecord = (await connectionService.ListAsync(_requester.Context, SearchQuery.Equal(nameof(ConnectionRecord.TheirDid), newResponderRecord.MyDid))).First(); Assert.Equal(ConnectionState.Connected, newResponderRecord.State); Assert.Equal(ConnectionState.Connected, newRequesterRecord.State); Assert.Equal(newRequesterRecord.TheirDid, newResponderRecord.MyDid); Assert.Equal(newResponderRecord.TheirDid, newRequesterRecord.MyDid); Assert.Equal( newRequesterRecord.GetTag(TagConstants.LastThreadId), newResponderRecord.GetTag(TagConstants.LastThreadId)); // Did Exchange Complete _responder.GetService <IEventAggregator>().GetEventByType <ServiceMessageProcessingEvent>() .Where(x => x.MessageType == MessageTypesHttps.DidExchange.Complete) .Subscribe(x => slim.Release()); var(completeMessage, finalRequesterRecord) = await didExchangeService.CreateComplete(_requester.Context, newRequesterRecord); await messsageService.SendAsync(_requester.Context, completeMessage, finalRequesterRecord); await slim.WaitAsync(TimeSpan.FromSeconds(30)); var finalResponderRecord = (await connectionService.ListAsync(_responder.Context, SearchQuery.Equal(nameof(ConnectionRecord.TheirDid), finalRequesterRecord.MyDid))).First(); Assert.Equal(ConnectionState.Connected, finalResponderRecord.State); Assert.Equal(ConnectionState.Connected, finalRequesterRecord.State); Assert.Equal(finalRequesterRecord.TheirDid, finalResponderRecord.MyDid); Assert.Equal(finalResponderRecord.TheirDid, finalRequesterRecord.MyDid); Assert.Equal( finalRequesterRecord.GetTag(TagConstants.LastThreadId), finalResponderRecord.GetTag(TagConstants.LastThreadId)); }