Esempio n. 1
0
        /// <inheritdoc />
        public virtual async Task <string> AcceptInvitationAsync(Wallet wallet, ConnectionInvitationMessage invitation)
        {
            Logger.LogInformation(LoggingEvents.AcceptInvitation, "Key {0}, Endpoint {1}",
                                  invitation.ConnectionKey, invitation.Endpoint.Uri);

            var my = await Did.CreateAndStoreMyDidAsync(wallet, "{}");

            var connection = new ConnectionRecord
            {
                Endpoint     = invitation.Endpoint,
                MyDid        = my.Did,
                MyVk         = my.VerKey,
                ConnectionId = Guid.NewGuid().ToString().ToLowerInvariant()
            };

            connection.Tags.Add(TagConstants.MyDid, my.Did);

            if (!string.IsNullOrEmpty(invitation.Name) || !string.IsNullOrEmpty(invitation.ImageUrl))
            {
                connection.Alias = new ConnectionAlias
                {
                    Name     = invitation.Name,
                    ImageUrl = invitation.ImageUrl
                };

                if (string.IsNullOrEmpty(invitation.Name))
                {
                    connection.Tags.Add(TagConstants.Alias, invitation.Name);
                }
            }

            await connection.TriggerAsync(ConnectionTrigger.InvitationAccept);

            await RecordService.AddAsync(wallet, connection);

            var provisioning = await ProvisioningService.GetProvisioningAsync(wallet);

            var connectionDetails = new ConnectionDetails
            {
                Did      = my.Did,
                Verkey   = my.VerKey,
                Endpoint = provisioning.Endpoint
            };

            var request = await MessageSerializer.PackSealedAsync <ConnectionRequestMessage>(connectionDetails, wallet,
                                                                                             my.VerKey, invitation.ConnectionKey);

            request.Key  = invitation.ConnectionKey;
            request.Type = MessageUtils.FormatKeyMessageType(invitation.ConnectionKey, MessageTypes.ConnectionRequest);

            var forwardMessage = new ForwardToKeyEnvelopeMessage
            {
                Type    = MessageUtils.FormatKeyMessageType(invitation.ConnectionKey, MessageTypes.ForwardToKey),
                Content = request.ToJson()
            };

            await RouterService.ForwardAsync(forwardMessage, invitation.Endpoint);

            return(connection.GetId());
        }
        /// <inheritdoc />
        public virtual async Task <ProofRequestMessage> CreateProofRequestAsync(Wallet wallet, string connectionId,
                                                                                string proofRequestJson)
        {
            Logger.LogInformation(LoggingEvents.CreateProofRequest, "ConnectionId {0}", connectionId);

            var connection = await ConnectionService.GetAsync(wallet, connectionId);

            var proofJobj = JObject.Parse(proofRequestJson);

            var proofRecord = new ProofRecord
            {
                Id           = Guid.NewGuid().ToString(),
                ConnectionId = connection.ConnectionId,
                RequestJson  = proofRequestJson
            };

            proofRecord.Tags[TagConstants.Nonce]        = proofJobj["nonce"].ToObject <string>();
            proofRecord.Tags[TagConstants.ConnectionId] = connection.GetId();
            proofRecord.Tags[TagConstants.Role]         = TagConstants.Requestor;

            await RecordService.AddAsync(wallet, proofRecord);

            var proofRequest = await MessageSerializer.PackSealedAsync <ProofRequestMessage>(
                new ProofRequestDetails { ProofRequestJson = proofRequestJson },
                wallet,
                connection.MyVk,
                connection.TheirVk);

            proofRequest.Type = MessageUtils.FormatDidMessageType(connection.TheirDid, MessageTypes.ProofRequest);

            return(proofRequest);
        }
Esempio n. 3
0
        /// <inheritdoc />
        public virtual async Task AcceptOfferAsync(Wallet wallet, Pool pool, string credentialId,
                                                   Dictionary <string, string> attributeValues = null)
        {
            var credential = await RecordService.GetAsync <CredentialRecord>(wallet, credentialId);

            var connection = await ConnectionService.GetAsync(wallet, credential.ConnectionId);

            var definition =
                await LedgerService.LookupDefinitionAsync(pool, connection.MyDid, credential.CredentialDefinitionId);

            var provisioning = await ProvisioningService.GetProvisioningAsync(wallet);

            var request = await AnonCreds.ProverCreateCredentialReqAsync(wallet, connection.MyDid, credential.OfferJson,
                                                                         definition.ObjectJson, provisioning.MasterSecretId);

            // Update local credential record with new info
            credential.CredentialRequestMetadataJson = request.CredentialRequestMetadataJson;

            var details = new CredentialRequestDetails
            {
                OfferJson             = credential.OfferJson,
                CredentialRequestJson = request.CredentialRequestJson,
                CredentialValuesJson  = CredentialUtils.FormatCredentialValues(attributeValues)
            };

            var requestMessage =
                await MessageSerializer.PackSealedAsync <CredentialRequestMessage>(details, wallet, connection.MyVk,
                                                                                   connection.TheirVk);

            requestMessage.Type =
                MessageUtils.FormatDidMessageType(connection.TheirDid, MessageTypes.CredentialRequest);

            await credential.TriggerAsync(CredentialTrigger.Request);

            await RecordService.UpdateAsync(wallet, credential);

            //TODO we need roll back, i.e if we fail to send the A2A message the credential record should revert to Offer phase
            //so the user can resend
            await RouterService.ForwardAsync(new ForwardEnvelopeMessage
            {
                Content = requestMessage.ToJson(),
                Type    = MessageUtils.FormatDidMessageType(connection.TheirDid, MessageTypes.Forward)
            }, connection.Endpoint);
        }