Beispiel #1
0
        // Create an invitation to establish a connection between the agents.
        public async Task <JObject> CreateConnectionAsync(Enrollee enrollee)
        {
            var alias     = enrollee.Id.ToString();
            var issuerDid = await _verifiableCredentialClient.GetIssuerDidAsync();

            var schemaId = await _verifiableCredentialClient.GetSchemaId(issuerDid);

            var invitation = await _verifiableCredentialClient.CreateInvitationAsync(alias);

            var invitationUrl          = invitation.Value <string>("invitation_url");
            var credentialDefinitionId = await _verifiableCredentialClient.GetCredentialDefinitionIdAsync(schemaId);

            QRCodeGenerator qrGenerator         = new QRCodeGenerator();
            QRCodeData      qrCodeData          = qrGenerator.CreateQrCode(invitationUrl, QRCodeGenerator.ECCLevel.Q);
            Base64QRCode    qrCode              = new Base64QRCode(qrCodeData);
            string          qrCodeImageAsBase64 = qrCode.GetGraphic(20, "#003366", "#ffffff");

            enrollee.Credential = new Credential
            {
                SchemaId = schemaId,
                CredentialDefinitionId = credentialDefinitionId,
                Alias        = alias,
                Base64QRCode = qrCodeImageAsBase64
            };

            var created = await _context.SaveChangesAsync();

            if (created < 1)
            {
                throw new InvalidOperationException("Could not store connection invitation.");
            }

            // TODO after testing don't need to pass back the invitation
            return(invitation);
        }
        private async Task <Action> OnApplicationStartedAsync(IVerifiableCredentialClient _verifiableCredentialClient)
        {
            var issuerDid = await _verifiableCredentialClient.GetIssuerDidAsync();

            var schemaId = await _verifiableCredentialClient.GetSchemaId(issuerDid);

            if (schemaId == null)
            {
                schemaId = await _verifiableCredentialClient.CreateSchemaAsync();
            }

            var credentialDefinitionId = await _verifiableCredentialClient.GetCredentialDefinitionIdAsync(schemaId);

            if (credentialDefinitionId == null)
            {
                credentialDefinitionId = await _verifiableCredentialClient.CreateCredentialDefinitionAsync(schemaId);
            }

            return(null);
        }
Beispiel #3
0
        public async Task <string> IssueCredentialsAsync(Patient patient, List <Identifier> identifiers)
        {
            var connectionActive = true;
            var connection       = await _context.Connections
                                   .Where(c => c.AcceptedConnectionDate != null)
                                   .OrderByDescending(c => c.AcceptedConnectionDate)
                                   .FirstOrDefaultAsync(c => c.PatientId == patient.Id);

            if (connection == null)
            {
                // Create connection and wait for connection to be accepted before issuing credentials
                connection = await CreateConnectionAsync(patient);

                connectionActive = false;
            }

            var alias     = patient.Id.ToString();
            var issuerDid = await _verifiableCredentialClient.GetIssuerDidAsync();

            var schemaId = await _verifiableCredentialClient.GetSchemaId(issuerDid);

            if (schemaId == null)
            {
                schemaId = await _verifiableCredentialClient.CreateSchemaAsync();
            }
            var credentialDefinitionId = await _verifiableCredentialClient.GetCredentialDefinitionIdAsync(schemaId);

            if (credentialDefinitionId == null)
            {
                credentialDefinitionId = await _verifiableCredentialClient.CreateCredentialDefinitionAsync(schemaId);
            }
            var credentials = new List <Credential>();

            foreach (var identifier in identifiers)
            {
                var newCredential = new Credential
                {
                    ConnectionId           = connection.Id,
                    SchemaId               = schemaId,
                    CredentialDefinitionId = credentialDefinitionId,
                    Identifier             = new Identifier
                    {
                        Guid = identifier.Guid,
                        Uri  = identifier.Uri
                    }
                };

                credentials.Add(newCredential);
            }

            await _context.Credentials.AddRangeAsync(credentials);

            var created = await _context.SaveChangesAsync();

            if (created < 1)
            {
                throw new InvalidOperationException("Could not store credentials.");
            }

            if (connectionActive)
            {
                // Issue credentials if connection already active
                foreach (var credential in credentials)
                {
                    _logger.LogInformation("Issuing a credential with this connection_id: {connectionId}", connection.ConnectionId);
                    // Assumed that when a connection invitation has been sent and accepted
                    await IssueCredential(credential, connection.ConnectionId, credential.Identifier.Guid);

                    _logger.LogInformation("Credential has been issued for connection_id: {connectionId}", connection.ConnectionId);
                }

                return(null);
            }

            return(connection.Base64QRCode);
        }