Exemple #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);
        }
Exemple #2
0
        // Create the credential offer.
        private async Task <JObject> CreateCredentialOfferAsync(string connectionId, JArray attributes)
        {
            var issuerDid = await _verifiableCredentialClient.GetIssuerDidAsync();

            var schema = (await _verifiableCredentialClient.GetSchema(SCHEMA_ID)).Value <JObject>("schema");
            var credentialDefinitionId = await _verifiableCredentialClient.GetCredentialDefinitionIdAsync(SCHEMA_ID);

            JObject credentialOffer = new JObject
            {
                { "connection_id", connectionId },
                { "issuer_did", issuerDid },
                { "schema_id", SCHEMA_ID },
                { "schema_issuer_did", issuerDid },
                { "schema_name", schema.Value <string>("name") },
                { "schema_version", schema.Value <string>("version") },
                { "cred_def_id", credentialDefinitionId },
                { "comment", "PharmaNet GPID" },
                { "auto_remove", true },
                { "revoc_reg_id", null },
                { "credential_proposal", new JObject
                  {
                      { "@type", "did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/issue-credential/1.0/credential-preview" },
                      { "attributes", attributes }
                  } }
            };

            _logger.LogInformation("Credential offer for connection ID \"{connectionId}\" for {@JObject}", connectionId, JsonConvert.SerializeObject(credentialOffer));

            return(credentialOffer);
        }
        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);
        }
Exemple #4
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);
        }