/// <inheritdoc />
        public virtual async Task <ProofRequestMessage> CreateProofRequestAsync(Wallet wallet, string connectionId,
                                                                                ProofRequest proofRequest)
        {
            if (string.IsNullOrWhiteSpace(proofRequest.Nonce))
            {
                throw new ArgumentNullException(nameof(proofRequest.Nonce), "Nonce must be set.");
            }

            return(await CreateProofRequestAsync(wallet, connectionId, proofRequest.ToJson()));
        }
Example #2
0
        /// <inheritdoc />
        public virtual async Task <(ProofRequestMessage, ProofRecord)> CreateProofRequestAsync(
            IAgentContext agentContext, ProofRequest proofRequest,
            string connectionId)
        {
            if (string.IsNullOrWhiteSpace(proofRequest.Nonce))
            {
                throw new ArgumentNullException(nameof(proofRequest.Nonce), "Nonce must be set.");
            }

            return(await CreateProofRequestAsync(agentContext, proofRequest.ToJson(), connectionId));
        }
Example #3
0
        public async Task <IActionResult> SendPresentationRequestAsync(OperationBody body)
        {
            // NOTE: AATH can only start from presentation request, not respond to previous message
            var context = await _agentContextProvider.GetContextAsync();

            var presentationRequest        = body.Data;
            var connectionId               = (string)presentationRequest["connection_id"];
            var presentationRequestMessage = presentationRequest["presentation_proposal"]["request_presentations~attach"]["data"];

            var proofRequest = new ProofRequest
            {
                Name                = (string)presentationRequestMessage["name"] ?? "test proof",
                Version             = (string)presentationRequestMessage["version"] ?? "1.0",
                Nonce               = await AnonCreds.GenerateNonceAsync(),
                RequestedAttributes = presentationRequestMessage["requested_attributes"]?.ToObject <Dictionary <string, ProofAttributeInfo> >() ?? new Dictionary <string, ProofAttributeInfo>
                {
                },
                RequestedPredicates = presentationRequestMessage["requested_predicates"]?.ToObject <Dictionary <string, ProofPredicateInfo> >() ?? new Dictionary <string, ProofPredicateInfo> {
                }
            };

            _logger.LogInformation("SendPresentationRequest {proofRequest}", proofRequest.ToJson());


            var(requestPresentationMessage, proofRecord) = await _proofService.CreateRequestAsync(context, proofRequest, connectionId);

            var connection = await _connectionService.GetAsync(context, connectionId);

            var THPresentationExchange = new TestHarnessPresentationExchange
            {
                RecordId = proofRecord.Id,
                ThreadId = proofRecord.GetTag(TagConstants.LastThreadId),
                State    = TestHarnessPresentationExchangeState.RequestSent
            };

            _proofCache.Set(THPresentationExchange.ThreadId, THPresentationExchange);

            UpdateStateOnMessage(THPresentationExchange, TestHarnessPresentationExchangeState.PresentationReceived, _ => _.MessageType == MessageTypes.PresentProofNames.Presentation && _.ThreadId == THPresentationExchange.ThreadId);

            _logger.LogDebug("Send Presentation Request {requestPresentationMessage}", requestPresentationMessage.ToJson());

            await _messageService.SendAsync(context, requestPresentationMessage, connection);

            return(Ok(THPresentationExchange));
        }
        /// <inheritdoc />
        public virtual async Task <List <Credential> > ListCredentialsForProofRequestAsync(Wallet wallet,
                                                                                           ProofRequest proofRequest, string attributeReferent)
        {
            using (var search =
                       await AnonCreds.ProverSearchCredentialsForProofRequestAsync(wallet, proofRequest.ToJson()))
            {
                var searchResult = await search.NextAsync(attributeReferent, 100);

                return(JsonConvert.DeserializeObject <List <Credential> >(searchResult));
            }
        }
Example #5
0
        /// <inheritdoc />
        public virtual async Task <string> CreateProofAsync(IAgentContext agentContext,
                                                            ProofRequest proofRequest, RequestedCredentials requestedCredentials)
        {
            var provisioningRecord = await ProvisioningService.GetProvisioningAsync(agentContext.Wallet);

            var credentialObjects = new List <CredentialInfo>();

            foreach (var credId in requestedCredentials.GetCredentialIdentifiers())
            {
                credentialObjects.Add(
                    JsonConvert.DeserializeObject <CredentialInfo>(
                        await AnonCreds.ProverGetCredentialAsync(agentContext.Wallet, credId)));
            }

            var schemas = await BuildSchemasAsync(await agentContext.Pool,
                                                  credentialObjects
                                                  .Select(x => x.SchemaId)
                                                  .Distinct());

            var definitions = await BuildCredentialDefinitionsAsync(await agentContext.Pool,
                                                                    credentialObjects
                                                                    .Select(x => x.CredentialDefinitionId)
                                                                    .Distinct());

            var revocationStates = await BuildRevocationStatesAsync(await agentContext.Pool,
                                                                    credentialObjects,
                                                                    requestedCredentials);

            var proofJson = await AnonCreds.ProverCreateProofAsync(agentContext.Wallet, proofRequest.ToJson(),
                                                                   requestedCredentials.ToJson(), provisioningRecord.MasterSecretId, schemas, definitions,
                                                                   revocationStates);

            return(proofJson);
        }
Example #6
0
        public async Task CreateProofRequestFromProposal()
        {
            var events = 0;

            _eventAggregator.GetEventByType <ServiceMessageProcessingEvent>()
            .Where(_ => (_.MessageType == MessageTypes.PresentProofNames.ProposePresentation ||
                         _.MessageType == MessageTypes.PresentProofNames.RequestPresentation ||
                         _.MessageType == MessageTypes.IssueCredentialNames.RequestCredential ||
                         _.MessageType == MessageTypes.IssueCredentialNames.IssueCredential))
            .Subscribe(_ =>
            {
                events++;
            });

            // Setup secure connection between issuer and holder
            var(issuerConnection, holderConnection) = await Scenarios.EstablishConnectionAsync(
                _connectionService, _messages, _issuerWallet, _holderWallet);

            var(issuerCredential, holderCredential) = await Scenarios.IssueCredentialAsync(
                _schemaService, _credentialService, _messages, issuerConnection,
                holderConnection, _issuerWallet, _holderWallet, await _holderWallet.Pool, TestConstants.DefaultMasterSecret, false, new List <CredentialPreviewAttribute>
            {
                new CredentialPreviewAttribute("first_name", "Test"),
                new CredentialPreviewAttribute("last_name", "Test"),
                new CredentialPreviewAttribute("salary", "100000"),
                new CredentialPreviewAttribute("age", "25"),
                new CredentialPreviewAttribute("wellbeing", "100")
            });



            Assert.Equal(issuerCredential.State, holderCredential.State);
            Assert.Equal(CredentialState.Issued, issuerCredential.State);
            var(message, record) = await _proofService.CreateProposalAsync(_holderWallet, new ProofProposal
            {
                Comment            = "Hello, World",
                ProposedAttributes = new List <ProposedAttribute>
                {
                    new ProposedAttribute
                    {
                        Name = "first_name",
                        CredentialDefinitionId = holderCredential.CredentialDefinitionId,
                        Referent = "Proof of Name",
                        Value    = "Joe"
                    },
                    new ProposedAttribute
                    {
                        Name = "last_name",
                        CredentialDefinitionId = holderCredential.CredentialDefinitionId,
                        Referent = "Proof of Name",
                        Value    = "Shmoe"
                    },
                    new ProposedAttribute
                    {
                        Name = "age",
                        CredentialDefinitionId = holderCredential.CredentialDefinitionId,
                        Referent = "Proof of Age",
                        Value    = "Shmoe"
                    }
                },
                ProposedPredicates = new List <ProposedPredicate>
                {
                    new ProposedPredicate
                    {
                        Name = "salary",
                        CredentialDefinitionId = holderCredential.CredentialDefinitionId,
                        Predicate = ">",
                        Threshold = 99999,
                        Referent  = "Proof of Salary > $99,999"
                    },
                    new ProposedPredicate
                    {
                        Name = "wellbeing",
                        CredentialDefinitionId = holderCredential.CredentialDefinitionId,
                        Referent  = "Proof of Wellbeing",
                        Predicate = "<",
                        Threshold = 99999
                    }
                }
            }, holderConnection.Id);

            Assert.NotNull(message);

            // Process Proposal
            record = await _proofService.ProcessProposalAsync(_issuerWallet, message, issuerConnection);

            //
            RequestPresentationMessage requestMessage;

            (requestMessage, record) = await _proofService.CreateRequestFromProposalAsync(_issuerWallet, new ProofRequestParameters
            {
                Name       = "Test",
                Version    = "1.0",
                NonRevoked = null
            }, record.Id, issuerConnection.Id);

            Assert.NotNull(requestMessage);
            Assert.NotNull(record);

            var actualProofRequest   = record.RequestJson.ToObject <ProofRequest>();
            var expectedProofRequest = new ProofRequest
            {
                Name                = "Test",
                Version             = "1.0",
                Nonce               = actualProofRequest.Nonce,
                RequestedAttributes = new Dictionary <string, ProofAttributeInfo>
                {
                    {
                        "Proof of Name", new ProofAttributeInfo
                        {
                            Name         = null,
                            Names        = new string[] { "first_name", "last_name" },
                            NonRevoked   = null,
                            Restrictions = new List <AttributeFilter>
                            {
                                new AttributeFilter
                                {
                                    CredentialDefinitionId = holderCredential.CredentialDefinitionId
                                }
                            }
                        }
                    },
                    {
                        "Proof of Age", new ProofAttributeInfo
                        {
                            Name         = "age",
                            Names        = null,
                            NonRevoked   = null,
                            Restrictions = new List <AttributeFilter>
                            {
                                new AttributeFilter
                                {
                                    CredentialDefinitionId = holderCredential.CredentialDefinitionId
                                }
                            }
                        }
                    }
                },
                RequestedPredicates = new Dictionary <string, ProofPredicateInfo>
                {
                    {
                        "Proof of Salary > $99,999", new ProofPredicateInfo
                        {
                            Name           = "salary",
                            Names          = null,
                            NonRevoked     = null,
                            PredicateType  = ">",
                            PredicateValue = 99999,
                            Restrictions   = new List <AttributeFilter>
                            {
                                new AttributeFilter
                                {
                                    CredentialDefinitionId = holderCredential.CredentialDefinitionId
                                }
                            }
                        }
                    },
                    {
                        "Proof of Wellbeing", new ProofPredicateInfo
                        {
                            Name           = "wellbeing",
                            Names          = null,
                            NonRevoked     = null,
                            PredicateType  = "<",
                            PredicateValue = 99999,
                            Restrictions   = new List <AttributeFilter>
                            {
                                new AttributeFilter
                                {
                                    CredentialDefinitionId = holderCredential.CredentialDefinitionId
                                }
                            }
                        }
                    }
                }
            };
            var expectedProofRecord = new ProofRecord
            {
                State       = ProofState.Requested,
                RequestJson = expectedProofRequest.ToJson(),
            };

            actualProofRequest.Should().BeEquivalentTo(expectedProofRequest);
        }