Ejemplo n.º 1
0
        public static async Task <(ProofRecord holderProofRecord, ProofRecord requestorProofRecord)> ProposerInitiatedProofProtocolAsync(
            IProofService proofService,
            IProducerConsumerCollection <AgentMessage> messages,
            ConnectionRecord holderConnection, ConnectionRecord requestorConnection,
            IAgentContext holderContext, IAgentContext requestorContext,
            ProofProposal proofProposalObject)
        {
            // Holder sends a proof proposal
            var(holderProposalMessage, holderProofProposalRecord) = await proofService.CreateProposalAsync(holderContext, proofProposalObject, holderConnection.Id);

            messages.TryAdd(holderProposalMessage);
            Assert.True(holderProofProposalRecord.State == ProofState.Proposed);
            // Requestor accepts the proposal and builds a proofRequest
            var requestorProposalMessage = FindContentMessage <ProposePresentationMessage>(messages);

            Assert.NotNull(requestorProposalMessage);

            //Requestor stores the proof proposal
            var requestorProofProposalRecord = await proofService.ProcessProposalAsync(requestorContext, requestorProposalMessage, requestorConnection);

            Assert.Equal(ProofState.Proposed, requestorProofProposalRecord.State);
            var proposal = requestorProofProposalRecord.ProposalJson.ToObject <ProofProposal>();

            Assert.Equal("Hello, World", proposal.Comment);
            Assert.NotNull(proposal.ProposedAttributes);
            Console.WriteLine(requestorProofProposalRecord.ProposalJson);
            // Requestor sends a proof request
            var(requestorRequestMessage, requestorProofRequestRecord) = await proofService.CreateRequestFromProposalAsync(
                requestorContext,
                new ProofRequestParameters
            {
                Name    = "Test",
                Version = "1.0"
            },
                requestorProofProposalRecord.Id, requestorConnection.Id);

            messages.TryAdd(requestorRequestMessage);
            Assert.Equal(ProofState.Requested, requestorProofRequestRecord.State);

            return(await ProofProtocolAsync(proofService, messages, holderConnection, requestorConnection, holderContext, requestorContext, requestorProofRequestRecord));
        }
Ejemplo n.º 2
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);
        }