/// <summary>
        /// Processes the agent message
        /// </summary>
        /// <param name="agentContext"></param>
        /// <param name="messageContext">The agent message agentContext.</param>
        /// <returns></returns>
        /// <exception cref="AriesFrameworkException">Unsupported message type {messageType}</exception>
        public async Task <AgentMessage> ProcessAsync(IAgentContext agentContext, UnpackedMessageContext messageContext)
        {
            switch (messageContext.GetMessageType())
            {
            // v1.0
            case MessageTypes.PresentProofNames.RequestPresentation:
            {
                var message = messageContext.GetMessage <RequestPresentationMessage>();
                var record  = await _proofService.ProcessRequestAsync(agentContext, message, messageContext.Connection);

                messageContext.ContextRecord = record;
                break;
            }

            case MessageTypes.PresentProofNames.Presentation:
            {
                var message = messageContext.GetMessage <PresentationMessage>();
                var record  = await _proofService.ProcessPresentationAsync(agentContext, message);

                messageContext.ContextRecord = record;
                break;
            }

            default:
                throw new AriesFrameworkException(ErrorCode.InvalidMessage,
                                                  $"Unsupported message type {messageContext.GetMessageType()}");
            }
            return(null);
        }
Example #2
0
        public static async Task <(ProofRecord holderProofRecord, ProofRecord RequestorProofRecord)> ProofProtocolAsync(
            IProofService proofService,
            IProducerConsumerCollection <AgentMessage> messages,
            ConnectionRecord holderConnection, ConnectionRecord requestorConnection,
            IAgentContext holderContext,
            IAgentContext requestorContext, ProofRequest proofRequestObject)
        {
            //Requestor sends a proof request
            var(message, requestorProofRecord) = await proofService.CreateRequestAsync(requestorContext, proofRequestObject, requestorConnection.Id);

            messages.TryAdd(message);

            // Holder accepts the proof requests and builds a proof
            var proofRequest = FindContentMessage <RequestPresentationMessage>(messages);

            Assert.NotNull(proofRequest);

            //Holder stores the proof request
            var holderProofRequestRecord = await proofService.ProcessRequestAsync(holderContext, proofRequest, holderConnection);

            var holderProofRecord = await proofService.GetAsync(holderContext, holderProofRequestRecord.Id);

            var holderProofRequest = JsonConvert.DeserializeObject <ProofRequest>(holderProofRecord.RequestJson);

            // Auto satify the proof with which ever credentials in the wallet are capable
            var requestedCredentials =
                await ProofServiceUtils.GetAutoRequestedCredentialsForProofCredentials(holderContext, proofService,
                                                                                       holderProofRequest);

            //Holder accepts the proof request and sends a proof
            (var proofMessage, _) = await proofService.CreatePresentationAsync(
                holderContext,
                holderProofRequestRecord.Id,
                requestedCredentials);

            messages.TryAdd(proofMessage);

            //Requestor retrives proof message from their cloud agent
            var proof = FindContentMessage <PresentationMessage>(messages);

            Assert.NotNull(proof);

            //Requestor stores proof
            requestorProofRecord = await proofService.ProcessPresentationAsync(requestorContext, proof);

            //Requestor verifies proof
            var requestorVerifyResult = await proofService.VerifyProofAsync(requestorContext, requestorProofRecord.Id);

            //Verify the proof is valid
            Assert.True(requestorVerifyResult);

            var requestorProofRecordResult = await proofService.GetAsync(requestorContext, requestorProofRecord.Id);

            var holderProofRecordResult = await proofService.GetAsync(holderContext, holderProofRecord.Id);

            return(holderProofRecordResult, requestorProofRecordResult);
        }
Example #3
0
        private async Task ScanInvite()
        {
            var expectedFormat = ZXing.BarcodeFormat.QR_CODE;

            var opts = new ZXing.Mobile.MobileBarcodeScanningOptions {
                PossibleFormats = new List <ZXing.BarcodeFormat> {
                    expectedFormat
                }
            };

            var context = await _agentContextProvider.GetContextAsync();

            var scanner = new ZXing.Mobile.MobileBarcodeScanner();

            var result = await scanner.Scan(opts);

            if (result == null)
            {
                return;
            }

            AgentMessage message = await MessageDecoder.ParseMessageAsync(result.Text);

            switch (message)
            {
            case ConnectionInvitationMessage invitation:
                break;

            case RequestPresentationMessage presentation:
                RequestPresentationMessage proofRequest = (RequestPresentationMessage)presentation;
                var         service     = message.GetDecorator <ServiceDecorator>(DecoratorNames.ServiceDecorator);
                ProofRecord proofRecord = await _proofService.ProcessRequestAsync(context, proofRequest, null);

                proofRecord.SetTag("RecipientKey", service.RecipientKeys.ToList()[0]);
                proofRecord.SetTag("ServiceEndpoint", service.ServiceEndpoint);
                await _recordService.UpdateAsync(context.Wallet, proofRecord);

                _eventAggregator.Publish(new ApplicationEvent {
                    Type = ApplicationEventType.ProofRequestUpdated
                });
                break;

            default:
                DialogService.Alert("Invalid invitation!");
                return;
            }

            Device.BeginInvokeOnMainThread(async() =>
            {
                if (message is ConnectionInvitationMessage)
                {
                    await NavigationService.NavigateToAsync <AcceptInviteViewModel>(message as ConnectionInvitationMessage, NavigationType.Modal);
                }
            });
        }
Example #4
0
        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);
        }