/// <inheritdoc />
        public virtual async Task <string> CreateProofAsync(IAgentContext agentContext,
                                                            ProofRequest proofRequest, RequestedCredentials requestedCredentials)
        {
            var provisioningRecord = await ProvisioningService.GetProvisioningAsync(agentContext.Wallet);

            var credentialObjects = new List <CredentialInfo>();

            foreach (var credId in requestedCredentials.GetCredentialIdentifiers())
            {
                credentialObjects.Add(
                    JsonConvert.DeserializeObject <CredentialInfo>(
                        await AnonCreds.ProverGetCredentialAsync(agentContext.Wallet, credId)));
            }

            var schemas = await BuildSchemasAsync(await agentContext.Pool,
                                                  credentialObjects
                                                  .Select(x => x.SchemaId)
                                                  .Distinct());

            var definitions = await BuildCredentialDefinitionsAsync(await agentContext.Pool,
                                                                    credentialObjects
                                                                    .Select(x => x.CredentialDefinitionId)
                                                                    .Distinct());

            var revocationStates = await BuildRevocationStatesAsync(await agentContext.Pool,
                                                                    credentialObjects,
                                                                    requestedCredentials);

            var proofJson = await AnonCreds.ProverCreateProofAsync(agentContext.Wallet, proofRequest.ToJson(),
                                                                   requestedCredentials.ToJson(), provisioningRecord.MasterSecretId, schemas, definitions,
                                                                   revocationStates);

            return(proofJson);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> GetCredentialById([FromRoute] string credentialId)
        {
            var context = await _agentContextProvider.GetContextAsync();

            try
            {
                var credentialRecords = await _credentialService.ListAsync(context, SearchQuery.Equal(nameof(CredentialRecord.CredentialId), credentialId), 1);

                var credentialRecord = credentialRecords.First();

                var credential = await AnonCreds.ProverGetCredentialAsync(context.Wallet, credentialId);

                return(Ok(new
                {
                    referent = credentialRecord.CredentialId,
                    schema_id = credentialRecord.SchemaId,
                    cred_def_id = credentialRecord.CredentialDefinitionId,
                    credential = JsonConvert.DeserializeObject(credential)
                }));
            }
            catch
            {
                // Unable to retrieve credential record.
                return(NotFound());
            }
        }
Ejemplo n.º 3
0
        /// <inheritdoc />
        public virtual async Task <(ProofMessage, ProofRecord)> CreateProofAsync(IAgentContext agentContext,
                                                                                 string proofRequestId, RequestedCredentials requestedCredentials)
        {
            var record = await GetAsync(agentContext, proofRequestId);

            if (record.State != ProofState.Requested)
            {
                throw new AgentFrameworkException(ErrorCode.RecordInInvalidState,
                                                  $"Proof state was invalid. Expected '{ProofState.Requested}', found '{record.State}'");
            }

            var provisioningRecord = await ProvisioningService.GetProvisioningAsync(agentContext.Wallet);

            var credentialObjects = new List <CredentialInfo>();

            foreach (var credId in requestedCredentials.GetCredentialIdentifiers())
            {
                credentialObjects.Add(
                    JsonConvert.DeserializeObject <CredentialInfo>(
                        await AnonCreds.ProverGetCredentialAsync(agentContext.Wallet, credId)));
            }

            var schemas = await BuildSchemasAsync(await agentContext.Pool,
                                                  credentialObjects
                                                  .Select(x => x.SchemaId)
                                                  .Distinct());

            var definitions = await BuildCredentialDefinitionsAsync(await agentContext.Pool,
                                                                    credentialObjects
                                                                    .Select(x => x.CredentialDefinitionId)
                                                                    .Distinct());

            var revocationStates = await BuildRevocationStatesAsync(await agentContext.Pool,
                                                                    credentialObjects,
                                                                    requestedCredentials);

            var proofJson = await AnonCreds.ProverCreateProofAsync(agentContext.Wallet, record.RequestJson,
                                                                   requestedCredentials.ToJson(), provisioningRecord.MasterSecretId, schemas, definitions,
                                                                   revocationStates);

            record.ProofJson = proofJson;
            await record.TriggerAsync(ProofTrigger.Accept);

            await RecordService.UpdateAsync(agentContext.Wallet, record);

            var threadId = record.GetTag(TagConstants.LastThreadId);

            var proofMsg = new ProofMessage
            {
                ProofJson = proofJson
            };

            proofMsg.ThreadFrom(threadId);

            return(proofMsg, record);
        }
        /// <inheritdoc />
        public virtual async Task <ProofMessage> CreateProofAsync(Wallet wallet, Pool pool, string proofRequestId,
                                                                  RequestedCredentials requestedCredentials)
        {
            var record = await RecordService.GetAsync <ProofRecord>(wallet, proofRequestId);

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

            var provisioningRecord = await ProvisioningService.GetProvisioningAsync(wallet);

            var credentialObjects = new List <CredentialInfo>();

            foreach (var credId in requestedCredentials.GetCredentialIdentifiers())
            {
                credentialObjects.Add(
                    JsonConvert.DeserializeObject <CredentialInfo>(
                        await AnonCreds.ProverGetCredentialAsync(wallet, credId)));
            }

            var schemas = await BuildSchemasAsync(pool,
                                                  credentialObjects
                                                  .Select(x => x.SchemaId)
                                                  .Distinct());

            var definitions = await BuildCredentialDefinitionsAsync(pool,
                                                                    credentialObjects
                                                                    .Select(x => x.CredentialDefinitionId)
                                                                    .Distinct());

            var revocationStates = await BuildRevocationStatesAsync(pool,
                                                                    credentialObjects,
                                                                    requestedCredentials);

            var proofJson = await AnonCreds.ProverCreateProofAsync(wallet, record.RequestJson,
                                                                   requestedCredentials.ToJson(), provisioningRecord.MasterSecretId, schemas, definitions,
                                                                   revocationStates);

            record.ProofJson = proofJson;
            await record.TriggerAsync(ProofTrigger.Accept);

            await RecordService.UpdateAsync(wallet, record);

            var proof = await MessageSerializer.PackSealedAsync <ProofMessage>(
                new ProofDetails
            {
                ProofJson    = proofJson,
                RequestNonce = JsonConvert.DeserializeObject <ProofRequest>(record.RequestJson).Nonce
            }, wallet, connection.MyVk, connection.TheirVk);

            proof.Type = MessageUtils.FormatDidMessageType(connection.TheirDid, MessageTypes.DisclosedProof);

            return(proof);
        }
        public async Task TestProverGetCredentialWorks()
        {
            var credentialJson = await AnonCreds.ProverGetCredentialAsync(wallet, credentialId1);

            var credential = JObject.Parse(credentialJson);

            var expected = JObject.FromObject(new
            {
                schema_id   = gvtSchemaId,
                cred_def_id = issuer1gvtCredDefId,
                referent    = credentialId1,
                rev_reg_id  = (object)null,
                cred_rev_id = (object)null,
                attrs       = new
                {
                    sex    = "male",
                    name   = "Alex",
                    height = "175",
                    age    = "28",
                }
            });

            Assert.IsTrue(JToken.DeepEquals(expected, credential));
        }
 public async Task TestProverGetCredentialWorksForNotFound()
 {
     var ex = await Assert.ThrowsExceptionAsync <WalletItemNotFoundException>(() =>
                                                                              AnonCreds.ProverGetCredentialAsync(wallet, "other_cred_id")
                                                                              );
 }