/// <inheritdoc />
        public virtual async Task <string> CreateCredentialDefinitionAsync(Pool pool, Wallet wallet, string schemaId,
                                                                           string issuerDid, string tag, bool supportsRevocation, int maxCredentialCount, Uri tailsBaseUri)
        {
            var definitionRecord = new DefinitionRecord();
            var schema           = await LedgerService.LookupSchemaAsync(pool, schemaId);

            var credentialDefinition = await AnonCreds.IssuerCreateAndStoreCredentialDefAsync(wallet, issuerDid,
                                                                                              schema.ObjectJson, tag, null, new { support_revocation = supportsRevocation }.ToJson());

            await LedgerService.RegisterCredentialDefinitionAsync(wallet, pool, issuerDid,
                                                                  credentialDefinition.CredDefJson);

            definitionRecord.SupportsRevocation = supportsRevocation;
            definitionRecord.Id       = credentialDefinition.CredDefId;
            definitionRecord.SchemaId = schemaId;

            if (supportsRevocation)
            {
                definitionRecord.MaxCredentialCount = maxCredentialCount;
                var tailsHandle = await TailsService.CreateTailsAsync();

                var revRegDefConfig =
                    new { issuance_type = "ISSUANCE_ON_DEMAND", max_cred_num = maxCredentialCount }.ToJson();
                var revocationRegistry = await AnonCreds.IssuerCreateAndStoreRevocRegAsync(wallet, issuerDid, null,
                                                                                           "Tag2", credentialDefinition.CredDefId, revRegDefConfig, tailsHandle);

                // Update tails location URI
                var revocationDefinition = JObject.Parse(revocationRegistry.RevRegDefJson);
                var tailsfile            = Path.GetFileName(revocationDefinition["value"]["tailsLocation"].ToObject <string>());
                revocationDefinition["value"]["tailsLocation"] = new Uri(tailsBaseUri, tailsfile).ToString();

                await LedgerService.RegisterRevocationRegistryDefinitionAsync(wallet, pool, issuerDid,
                                                                              revocationDefinition.ToString());

                await LedgerService.SendRevocationRegistryEntryAsync(wallet, pool, issuerDid,
                                                                     revocationRegistry.RevRegId, "CL_ACCUM", revocationRegistry.RevRegEntryJson);

                var revocationRecord = new RevocationRegistryRecord
                {
                    Id        = revocationRegistry.RevRegId,
                    TailsFile = tailsfile,
                    CredentialDefinitionId = credentialDefinition.CredDefId
                };
                await RecordService.AddAsync(wallet, revocationRecord);
            }

            await RecordService.AddAsync(wallet, definitionRecord);

            return(credentialDefinition.CredDefId);
        }
Ejemplo n.º 2
0
        /// <inheritdoc />
        public async Task <string> CreateCredentialDefinitionAsync(IAgentContext context, CredentialDefinitionConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new AriesFrameworkException(ErrorCode.InvalidParameterFormat, "Configuration must be specified.");
            }
            if (configuration.SchemaId == null)
            {
                throw new AriesFrameworkException(ErrorCode.InvalidParameterFormat, "SchemaId must be specified.");
            }
            if (configuration.EnableRevocation &&
                configuration.RevocationRegistryBaseUri == null &&
                AgentOptions.RevocationRegistryUriPath == null)
            {
                throw new AriesFrameworkException(ErrorCode.InvalidParameterFormat, "RevocationRegistryBaseUri must be specified either in the configuration or the AgentOptions");
            }

            var schema = await LedgerService.LookupSchemaAsync(context, configuration.SchemaId);

            var provisioning = await ProvisioningService.GetProvisioningAsync(context.Wallet);

            configuration.IssuerDid ??= provisioning.IssuerDid;

            var credentialDefinition = await AnonCreds.IssuerCreateAndStoreCredentialDefAsync(
                wallet : context.Wallet,
                issuerDid : configuration.IssuerDid,
                schemaJson : schema.ObjectJson,
                tag : configuration.Tag,
                type : null,
                configJson : new { support_revocation = configuration.EnableRevocation }.ToJson());

            var definitionRecord = new DefinitionRecord();

            definitionRecord.IssuerDid = configuration.IssuerDid;

            //var paymentInfo = await paymentService.GetTransactionCostAsync(context, TransactionTypes.CRED_DEF);

            await LedgerService.RegisterCredentialDefinitionAsync(
                context : context,
                submitterDid : configuration.IssuerDid,
                data : credentialDefinition.CredDefJson,
                paymentInfo : null);

            definitionRecord.SupportsRevocation = configuration.EnableRevocation;
            definitionRecord.Id       = credentialDefinition.CredDefId;
            definitionRecord.SchemaId = configuration.SchemaId;

            if (configuration.EnableRevocation)
            {
                definitionRecord.MaxCredentialCount  = configuration.RevocationRegistrySize;
                definitionRecord.RevocationAutoScale = configuration.RevocationRegistryAutoScale;

                var(_, revocationRecord) = await CreateRevocationRegistryAsync(
                    context : context,
                    tag : $"1-{configuration.RevocationRegistrySize}",
                    definitionRecord : definitionRecord);

                definitionRecord.CurrentRevocationRegistryId = revocationRecord.Id;
            }

            await RecordService.AddAsync(context.Wallet, definitionRecord);

            return(credentialDefinition.CredDefId);
        }
        /// <inheritdoc />
        public virtual async Task <string> CreateCredentialDefinitionAsync(IAgentContext context, string schemaId,
                                                                           string issuerDid, string tag, bool supportsRevocation, int maxCredentialCount, Uri tailsBaseUri)
        {
            var definitionRecord = new DefinitionRecord();
            var schema           = await LedgerService.LookupSchemaAsync(await context.Pool, schemaId);

            var credentialDefinition = await AnonCreds.IssuerCreateAndStoreCredentialDefAsync(context.Wallet, issuerDid,
                                                                                              schema.ObjectJson, tag, null, new { support_revocation = supportsRevocation }.ToJson());

            var paymentInfo = await paymentService.GetTransactionCostAsync(context, TransactionTypes.CRED_DEF);

            await LedgerService.RegisterCredentialDefinitionAsync(context : context,
                                                                  submitterDid : issuerDid,
                                                                  data : credentialDefinition.CredDefJson,
                                                                  paymentInfo : paymentInfo);

            if (paymentInfo != null)
            {
                await RecordService.UpdateAsync(context.Wallet, paymentInfo.PaymentAddress);
            }

            definitionRecord.SupportsRevocation = supportsRevocation;
            definitionRecord.Id       = credentialDefinition.CredDefId;
            definitionRecord.SchemaId = schemaId;

            if (supportsRevocation)
            {
                definitionRecord.MaxCredentialCount = maxCredentialCount;
                var tailsHandle = await TailsService.CreateTailsAsync();

                var revRegDefConfig =
                    new { issuance_type = "ISSUANCE_ON_DEMAND", max_cred_num = maxCredentialCount }.ToJson();
                var revocationRegistry = await AnonCreds.IssuerCreateAndStoreRevocRegAsync(context.Wallet, issuerDid, null,
                                                                                           "Tag2", credentialDefinition.CredDefId, revRegDefConfig, tailsHandle);

                // Update tails location URI
                var revocationDefinition = JObject.Parse(revocationRegistry.RevRegDefJson);
                var tailsfile            = Path.GetFileName(revocationDefinition["value"]["tailsLocation"].ToObject <string>());
                revocationDefinition["value"]["tailsLocation"] = new Uri(tailsBaseUri, tailsfile).ToString();

                paymentInfo = await paymentService.GetTransactionCostAsync(context, TransactionTypes.REVOC_REG_DEF);

                await LedgerService.RegisterRevocationRegistryDefinitionAsync(context : context,
                                                                              submitterDid : issuerDid,
                                                                              data : revocationDefinition.ToString(),
                                                                              paymentInfo : paymentInfo);

                if (paymentInfo != null)
                {
                    await RecordService.UpdateAsync(context.Wallet, paymentInfo.PaymentAddress);
                }

                paymentInfo = await paymentService.GetTransactionCostAsync(context, TransactionTypes.REVOC_REG_ENTRY);

                await LedgerService.SendRevocationRegistryEntryAsync(context : context,
                                                                     issuerDid : issuerDid,
                                                                     revocationRegistryDefinitionId : revocationRegistry.RevRegId,
                                                                     revocationDefinitionType : "CL_ACCUM",
                                                                     value : revocationRegistry.RevRegEntryJson,
                                                                     paymentInfo : paymentInfo);

                if (paymentInfo != null)
                {
                    await RecordService.UpdateAsync(context.Wallet, paymentInfo.PaymentAddress);
                }

                var revocationRecord = new RevocationRegistryRecord
                {
                    Id        = revocationRegistry.RevRegId,
                    TailsFile = tailsfile,
                    CredentialDefinitionId = credentialDefinition.CredDefId
                };
                await RecordService.AddAsync(context.Wallet, revocationRecord);
            }

            await RecordService.AddAsync(context.Wallet, definitionRecord);

            return(credentialDefinition.CredDefId);
        }