public static async Task <RequestedCredentials> GetAutoRequestedCredentialsForProofCredentials(IAgentContext holderContext, IProofService proofService, ProofRequest proofRequest) { var requestedCredentials = new RequestedCredentials(); foreach (var requestedAttribute in proofRequest.RequestedAttributes) { var credentials = await proofService.ListCredentialsForProofRequestAsync(holderContext, proofRequest, requestedAttribute.Key); requestedCredentials.RequestedAttributes.Add(requestedAttribute.Key, new RequestedAttribute { CredentialId = credentials.First().CredentialInfo.Referent, Revealed = true }); } foreach (var requestedAttribute in proofRequest.RequestedPredicates) { var credentials = await proofService.ListCredentialsForProofRequestAsync(holderContext, proofRequest, requestedAttribute.Key); requestedCredentials.RequestedPredicates.Add(requestedAttribute.Key, new RequestedAttribute { CredentialId = credentials.First().CredentialInfo.Referent, Revealed = true }); } return(requestedCredentials); }
/// <summary> /// Reference https://github.com/hyperledger/aries-framework-dotnet/blob/master/test/Hyperledger.Aries.Tests/Protocols/ProofTests.cs#L644 /// </summary> public async Task GetRequestedAttributes() { try { RequestedAttributes.Clear(); if (ProofRequest.RequestedAttributes == null) { return; } var context = await agentContextProvider.GetContextAsync(); //Get any Available Credentials for each requested attribute foreach (var requestedAttribute in ProofRequest.RequestedAttributes) { List <Credential> attributeCredentials = await proofService.ListCredentialsForProofRequestAsync(context, ProofRequest, requestedAttribute.Key); var attribute = scope.Resolve <ProofRequestAttributeViewModel>( new NamedParameter("name", requestedAttribute.Value.Name ?? string.Join(", ", requestedAttribute.Value.Names)), new NamedParameter("isPredicate", false), new NamedParameter("attributeCredentials", attributeCredentials), new NamedParameter("referent", requestedAttribute.Key) ); RequestedAttributes.Add(attribute); } //TODO: Implement Predicate and Restrictions related functionlity } catch (Exception xx) { } }
public async Task <GetCredentialsForProofResponse> Handle ( GetCredentialsForProofRequest aGetCredentialsForProofRequest, CancellationToken aCancellationToken ) { IAgentContext agentContext = await AgentProvider.GetContextAsync(); ProofRecord proofRecord = await ProofService.GetAsync(agentContext, aGetCredentialsForProofRequest.ProofId); ProofRequest proofRequest = JsonConvert.DeserializeObject <ProofRequest>(proofRecord.RequestJson); //ProofServiceUtils.GetAutoRequestedCredentialsForProofCredentials List <Credential> credentials = await ProofService .ListCredentialsForProofRequestAsync ( agentContext, proofRequest, aGetCredentialsForProofRequest.Referent ); var response = new GetCredentialsForProofResponse(credentials, aGetCredentialsForProofRequest.CorrelationId); return(response); }
public async Task <IActionResult> SendProof(string proofRecordId) { var agentContext = await _agentProvider.GetContextAsync(); var proofRecord = await _proofService.GetAsync(agentContext, proofRecordId); var connectionRecord = await _connectionService.GetAsync(agentContext, proofRecord.ConnectionId); var request = JsonConvert.DeserializeObject <ProofRequest>(proofRecord.RequestJson); var requestedCredentials = new RequestedCredentials(); foreach (var requestedAttribute in request.RequestedAttributes) { var credentials = await _proofService.ListCredentialsForProofRequestAsync(agentContext, request, requestedAttribute.Key); requestedCredentials.RequestedAttributes.Add(requestedAttribute.Key, new RequestedAttribute { CredentialId = credentials.First().CredentialInfo.Referent, Revealed = true }); } foreach (var requestedAttribute in request.RequestedPredicates) { var credentials = await _proofService.ListCredentialsForProofRequestAsync(agentContext, request, requestedAttribute.Key); requestedCredentials.RequestedPredicates.Add(requestedAttribute.Key, new RequestedAttribute { CredentialId = credentials.First().CredentialInfo.Referent, Revealed = false }); } var(proofMsg, record) = await _proofService.CreatePresentationAsync(agentContext, proofRecordId, requestedCredentials); await _messageService.SendAsync(agentContext.Wallet, proofMsg, connectionRecord); return(RedirectToAction("Index")); }
public async Task ProcessProofInvalidState() { //Setup a connection and issue the credentials to the holder var(issuerConnection, holderConnection) = await Scenarios.EstablishConnectionAsync( _connectionService, _messages, _issuerWallet, _holderWallet); await Scenarios.IssueCredentialAsync( _schemaService, _credentialService, _messages, issuerConnection, holderConnection, _issuerWallet, _holderWallet, await _holderWallet.Pool, TestConstants.DefaultMasterSecret, true, new List <CredentialPreviewAttribute> { new CredentialPreviewAttribute("first_name", "Test"), new CredentialPreviewAttribute("last_name", "Holder") }); _messages.Clear(); //Requestor initialize a connection with the holder var(_, requestorConnection) = await Scenarios.EstablishConnectionAsync( _connectionService, _messages, _holderWallet, _requestorWallet); // Verifier sends a proof request to prover { var proofRequestObject = new ProofRequest { Name = "ProofReq", Version = "1.0", Nonce = await AnonCreds.GenerateNonceAsync(), RequestedAttributes = new Dictionary <string, ProofAttributeInfo> { { "first-name-requirement", new ProofAttributeInfo { Name = "first_name" } } } }; //Requestor sends a proof request var(message, _) = await _proofService.CreateRequestAsync(_requestorWallet, proofRequestObject, requestorConnection.Id); _messages.Add(message); } // Holder accepts the proof requests and builds a proof { //Holder retrives proof request message from their cloud agent var proofRequest = FindContentMessage <RequestPresentationMessage>(); Assert.NotNull(proofRequest); //Holder stores the proof request var holderProofRequestId = await _proofService.ProcessRequestAsync(_holderWallet, proofRequest, holderConnection); var holderProofRecord = await _proofService.GetAsync(_holderWallet, holderProofRequestId.Id); var holderProofObject = JsonConvert.DeserializeObject <ProofRequest>(holderProofRecord.RequestJson); var requestedCredentials = new RequestedCredentials(); foreach (var requestedAttribute in holderProofObject.RequestedAttributes) { var credentials = await _proofService.ListCredentialsForProofRequestAsync(_holderWallet, holderProofObject, requestedAttribute.Key); requestedCredentials.RequestedAttributes.Add(requestedAttribute.Key, new RequestedAttribute { CredentialId = credentials.First().CredentialInfo.Referent, Revealed = true }); } foreach (var requestedAttribute in holderProofObject.RequestedPredicates) { var credentials = await _proofService.ListCredentialsForProofRequestAsync(_holderWallet, holderProofObject, requestedAttribute.Key); requestedCredentials.RequestedPredicates.Add(requestedAttribute.Key, new RequestedAttribute { CredentialId = credentials.First().CredentialInfo.Referent, Revealed = true }); } //Holder accepts the proof request and sends a proof (var proofMessage, var _) = await _proofService.CreatePresentationAsync(_holderWallet, holderProofRequestId.Id, requestedCredentials); _messages.Add(proofMessage); } //Requestor retrives proof message from their cloud agent var proof = FindContentMessage <PresentationMessage>(); Assert.NotNull(proof); //Requestor stores proof await _proofService.ProcessPresentationAsync(_requestorWallet, proof); var ex = await Assert.ThrowsAsync <AriesFrameworkException>(async() => await _proofService.ProcessPresentationAsync(_requestorWallet, proof)); Assert.True(ex.ErrorCode == ErrorCode.RecordInInvalidState); }
public async Task CanConductChallengeFlow() { //Setup a connection and issue the credentials to the holder var(issuerConnection, holderConnection) = await Scenarios.EstablishConnectionAsync( _connectionService, _messages, _issuerWallet, _holderWallet); await Scenarios.IssueCredentialAsync( _schemaService, _credentialService, _messages, issuerConnection, holderConnection, _issuerWallet, _holderWallet, await _holderWallet.Pool, TestConstants.DefaultMasterSecret, true, new List <CredentialPreviewAttribute> { new CredentialPreviewAttribute("first_name", "Test"), new CredentialPreviewAttribute("last_name", "Holder") }); _messages.Clear(); // Challenger sends a challenge { var challengeConfig = new EphemeralChallengeConfiguration { Name = "Test", Type = ChallengeType.Proof, Contents = new ProofRequestConfiguration { RequestedAttributes = new Dictionary <string, ProofAttributeInfo> { { "first-name-requirement", new ProofAttributeInfo { Name = "first_name" } } } } }; var challengeConfigId = await _ephemeralChallengeService.CreateChallengeConfigAsync(_requestorWallet, challengeConfig); (var challenge, var record) = await _ephemeralChallengeService.CreateChallengeAsync(_requestorWallet, challengeConfigId); Assert.True(!string.IsNullOrEmpty(challenge.ChallengerName)); Assert.True(challenge.RecipientKeys.Count() == 1); Assert.True(challenge.RecipientKeys.First() == TestConstants.DefaultVerkey); Assert.True(challenge.ServiceEndpoint == TestConstants.DefaultMockUri); _messages.Add(challenge); var result = await _ephemeralChallengeService.GetChallengeStateAsync(_requestorWallet, record.Id); Assert.True(result == ChallengeState.Challenged); } //Challenge responder recieves challenge { var challengeMessage = _messages.OfType <EphemeralChallengeMessage>().First(); var proofRequest = challengeMessage.Challenge.Contents.ToObject <ProofRequest>(); var requestedCredentials = new RequestedCredentials(); foreach (var requestedAttribute in proofRequest.RequestedAttributes) { var credentials = await _proofService.ListCredentialsForProofRequestAsync(_holderWallet, proofRequest, requestedAttribute.Key); requestedCredentials.RequestedAttributes.Add(requestedAttribute.Key, new RequestedAttribute { CredentialId = credentials.First().CredentialInfo.Referent, Revealed = true }); } foreach (var requestedAttribute in proofRequest.RequestedPredicates) { var credentials = await _proofService.ListCredentialsForProofRequestAsync(_holderWallet, proofRequest, requestedAttribute.Key); requestedCredentials.RequestedPredicates.Add(requestedAttribute.Key, new RequestedAttribute { CredentialId = credentials.First().CredentialInfo.Referent, Revealed = true }); } var challenge = await _ephemeralChallengeService.CreateProofChallengeResponseAsync( _holderWallet, challengeMessage, requestedCredentials); _messages.Add(challenge); } //Challenger recieves challenge response and verifies it { var challengeResponseMessage = _messages.OfType <EphemeralChallengeResponseMessage>().First(); var id = await _ephemeralChallengeService.ProcessChallengeResponseAsync(_requestorWallet, challengeResponseMessage); var result = await _ephemeralChallengeService.GetChallengeStateAsync(_requestorWallet, id); Assert.True(result == ChallengeState.Accepted); } }
public async Task CredentialProofDemo() { int events = 0; _eventAggregator.GetEventByType <ServiceMessageProcessingEvent>() .Where(_ => (_.MessageType == MessageTypes.ProofRequest || _.MessageType == MessageTypes.DisclosedProof)) .Subscribe(_ => { events++; }); //Setup a connection and issue the credentials to the holder var(issuerConnection, holderConnection) = await Scenarios.EstablishConnectionAsync( _connectionService, _messages, _issuerWallet, _holderWallet); await Scenarios.IssueCredentialAsync( _schemaService, _credentialService, _messages, issuerConnection, holderConnection, _issuerWallet, _holderWallet, _pool, MasterSecretId, true); _messages.Clear(); //Requestor initialize a connection with the holder var(_, requestorConnection) = await Scenarios.EstablishConnectionAsync( _connectionService, _messages, _holderWallet, _requestorWallet); // Verifier sends a proof request to prover { var proofRequestObject = new ProofRequest { Name = "ProofReq", Version = "1.0", Nonce = "123", RequestedAttributes = new Dictionary <string, ProofAttributeInfo> { { "first-name-requirement", new ProofAttributeInfo { Name = "first_name" } } } }; //Requestor sends a proof request await _proofService.SendProofRequestAsync(_requestorWallet, requestorConnection.Id, proofRequestObject); } // Holder accepts the proof requests and builds a proof { //Holder retrives proof request message from their cloud agent var proofRequest = FindContentMessage <ProofRequestMessage>(); Assert.NotNull(proofRequest); _holderWallet.Connection = holderConnection; //Holder stores the proof request var holderProofRequestId = await _proofService.ProcessProofRequestAsync(_holderWallet, proofRequest); var holderProofRecord = await _proofService.GetAsync(_holderWallet, holderProofRequestId); var holderProofObject = JsonConvert.DeserializeObject <ProofRequest>(holderProofRecord.RequestJson); var requestedCredentials = new RequestedCredentials(); foreach (var requestedAttribute in holderProofObject.RequestedAttributes) { var credentials = await _proofService.ListCredentialsForProofRequestAsync(_holderWallet, holderProofObject, requestedAttribute.Key); requestedCredentials.RequestedAttributes.Add(requestedAttribute.Key, new RequestedAttribute { CredentialId = credentials.First().CredentialInfo.Referent, Revealed = true, Timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds() }); } foreach (var requestedAttribute in holderProofObject.RequestedPredicates) { var credentials = await _proofService.ListCredentialsForProofRequestAsync(_holderWallet, holderProofObject, requestedAttribute.Key); requestedCredentials.RequestedPredicates.Add(requestedAttribute.Key, new RequestedAttribute { CredentialId = credentials.First().CredentialInfo.Referent, Revealed = true }); } //Holder accepts the proof request and sends a proof var proofMessage = await _proofService.AcceptProofRequestAsync(_holderWallet, holderProofRequestId, requestedCredentials); _messages.Add(proofMessage); } //Requestor retrives proof message from their cloud agent var proof = FindContentMessage <ProofMessage>(); Assert.NotNull(proof); _requestorWallet.Connection = requestorConnection; //Requestor stores proof var requestorProofId = await _proofService.ProcessProofAsync(_requestorWallet, proof); //Requestor verifies proof var requestorVerifyResult = await _proofService.VerifyProofAsync(_requestorWallet, requestorProofId); ////Verify the proof is valid Assert.True(requestorVerifyResult); Assert.True(events == 2); ////Get the proof from both parties wallets //var requestorProof = await _proofService.GetProof(_requestorWallet, requestorProofId); //var holderProof = await _proofService.GetProof(_holderWallet, holderProofRequestId); ////Verify that both parties have a copy of the proof //Assert.Equal(requestorProof, holderProof); }
private async Task CreateRequestedCredential() { var requestedCredentials = new RequestedCredentials(); var context = await _agentProvider.GetContextAsync(); _proofRequestAndCredentialMaps.Clear(); RangeEnabledObservableCollection <ProofRequestAndCredentialMap> proofRequestMapList = new RangeEnabledObservableCollection <ProofRequestAndCredentialMap>(); foreach (var requestedAttribute in ProofRequestObject.RequestedAttributes) { ProofRequestAndCredentialMap proofCredMap = new ProofRequestAndCredentialMap(); proofCredMap.ProofKey = requestedAttribute.Key; var credentials = await _proofService.ListCredentialsForProofRequestAsync(context, _proofRequest, requestedAttribute.Key); if (credentials.Count != 0) { var firstSuitableCredential = credentials.First(); _isSatisfied = true; proofCredMap.Referent = firstSuitableCredential.CredentialInfo.Referent; var key = this.RemoveAllSpaceAndToLower(requestedAttribute.Value.Name); var proofKeyAndCredentialMap = firstSuitableCredential.CredentialInfo.Attributes.ToDictionary(k => RemoveAllSpaceAndToLower(k.Key), v => new KeyValuePair <string, string>(v.Key, v.Value)); if (proofKeyAndCredentialMap.ContainsKey(key)) { var value = proofKeyAndCredentialMap[key].Value; var credKey = proofKeyAndCredentialMap[key].Key; KeyValuePair <string, string> keyValuePair = new KeyValuePair <string, string>(credKey, value); proofCredMap.CredentialAttribute = keyValuePair; } requestedCredentials.RequestedAttributes.Add(requestedAttribute.Key, new RequestedAttribute { CredentialId = firstSuitableCredential.CredentialInfo.Referent, Revealed = true }); } else { _isSatisfied = false; proofCredMap.Referent = "Unavailable"; var key = requestedAttribute.Value.Name; var value = "Unavailable"; KeyValuePair <string, string> keyValuePair = new KeyValuePair <string, string>(key, value); proofCredMap.CredentialAttribute = keyValuePair; requestedCredentials.RequestedAttributes.Add(requestedAttribute.Key, new RequestedAttribute { CredentialId = "Unavailable", Revealed = true }); } proofRequestMapList.Add(proofCredMap); //requestedCredentials.RequestedAttributes. //proofAndCredentialAttributesMapping.Add(requestedAttribute, credentials.First().CredentialInfo.Attributes.) } foreach (var requestedAttribute in ProofRequestObject.RequestedPredicates) { var credentials = await _proofService.ListCredentialsForProofRequestAsync(context, ProofRequestObject, requestedAttribute.Key); ProofRequestAndCredentialMap proofCredMap = new ProofRequestAndCredentialMap(); if (credentials.Count != 0) { var firstSuitableCredential = credentials.First(); _isSatisfied = true; proofCredMap.Referent = firstSuitableCredential.CredentialInfo.Referent; var key = this.RemoveAllSpaceAndToLower(requestedAttribute.Value.Name); var proofKeyAndCredentialMap = firstSuitableCredential.CredentialInfo.Attributes.ToDictionary(k => RemoveAllSpaceAndToLower(k.Key), v => new KeyValuePair <string, string>(v.Key, v.Value)); if (proofKeyAndCredentialMap.ContainsKey(key)) { var value = proofKeyAndCredentialMap[key].Value; var credKey = proofKeyAndCredentialMap[key].Key; KeyValuePair <string, string> keyValuePair = new KeyValuePair <string, string>(credKey, value); proofCredMap.CredentialAttribute = keyValuePair; } requestedCredentials.RequestedPredicates.Add(requestedAttribute.Key, new RequestedAttribute { CredentialId = firstSuitableCredential.CredentialInfo.Referent, Revealed = true }); } else { _isSatisfied = false; proofCredMap.ProofKey = requestedAttribute.Key; proofCredMap.Referent = "Unavailable"; var key = requestedAttribute.Value.Name; var value = "Unavailable"; KeyValuePair <string, string> keyValuePair = new KeyValuePair <string, string>(key, value); proofCredMap.CredentialAttribute = keyValuePair; requestedCredentials.RequestedPredicates.Add(requestedAttribute.Key, new RequestedAttribute { CredentialId = "Unavailable", Revealed = true }); } proofRequestMapList.Add(proofCredMap); } ProofRequestAndCredentialMaps = proofRequestMapList; RequestedCredentials = requestedCredentials; }