コード例 #1
0
        public async Task TestProverCreateProofWorksForInvalidSchemas()
        {
            await InitCommonWallet();

            var proofRequest = "{\"nonce\":\"123432421212\",\n" +
                               "                                \"name\":\"proof_req_1\",\n" +
                               "                                \"version\":\"0.1\",\n" +
                               "                                \"requested_attrs\":{\"attr1_referent\":{\"name\":\"name\",\"restrictions\":[{\"schema_seq_no\":1}]}},\n" +
                               "                                \"requested_predicates\":{\"predicate1_referent\":{\"attr_name\":\"age\",\"p_type\":\">=\",\"value\":18}}\n" +
                               "                              }";

            var claimsJson = await AnonCreds.ProverGetCredentialsForProofReqAsync(commonWallet, proofRequest);

            var claims = JObject.Parse(claimsJson);

            var claimForAttribute = claims["attrs"]["attr1_referent"][0];

            var claimUuid = claimForAttribute.Value <string>("referent");

            var requestedClaimsJson = string.Format("{{\n" +
                                                    "                                          \"self_attested_attributes\":{{}},\n" +
                                                    "                                          \"requested_attrs\":{{\"attr1_referent\":[\"{0}\", true]}},\n" +
                                                    "                                          \"requested_predicates\":{{\"predicate1_referent\":\"{1}\"}}\n" +
                                                    "                                        }}", claimUuid, claimUuid);

            var schemasJson   = "{}";
            var claimDefsJson = string.Format("{{\"{0}\":{1}}}", claimUuid, claimDef);
            var revocRegsJson = "{}";

            var ex = await Assert.ThrowsExceptionAsync <InvalidStructureException>(() =>
                                                                                   AnonCreds.ProverCreateProofAsync(commonWallet, proofRequest, requestedClaimsJson, schemasJson, masterSecretName, claimDefsJson, revocRegsJson)

                                                                                   );
        }
コード例 #2
0
        public async Task TestProverCreateProofWorks()
        {
            await InitCommonWallet();

            var proofRequest = "{\"nonce\":\"123432421212\",\n" +
                               "                                \"name\":\"proof_req_1\",\n" +
                               "                                \"version\":\"0.1\",\n" +
                               "                                \"requested_attrs\":{\"attr1_uuid\":{\"schema_seq_no\":1, \"name\":\"name\"}},\n" +
                               "                                \"requested_predicates\":{\"predicate1_uuid\":{\"attr_name\":\"age\",\"p_type\":\"GE\",\"value\":18}}\n" +
                               "                              }";

            var claimsJson = await AnonCreds.ProverGetClaimsForProofReqAsync(commonWallet, proofRequest);

            var claims = JObject.Parse(claimsJson);

            var claimForAttribute = claims["attrs"]["attr1_uuid"][0];

            var claimUuid = claimForAttribute.Value <string>("claim_uuid");

            var requestedClaimsJson = string.Format("{{\n" +
                                                    "                                          \"self_attested_attributes\":{{}},\n" +
                                                    "                                          \"requested_attrs\":{{\"attr1_uuid\":[\"{0}\", true]}},\n" +
                                                    "                                          \"requested_predicates\":{{\"predicate1_uuid\":\"{1}\"}}\n" +
                                                    "                                        }}", claimUuid, claimUuid);

            var schemasJson   = string.Format("{{\"{0}\":{1}}}", claimUuid, schema);
            var claimDefsJson = string.Format("{{\"{0}\":{1}}}", claimUuid, claimDef);
            var revocRegsJson = "{}";

            var proofJson = await AnonCreds.ProverCreateProofAsync(commonWallet, proofRequest, requestedClaimsJson, schemasJson,
                                                                   masterSecretName, claimDefsJson, revocRegsJson);

            Assert.IsNotNull(proofJson);
        }
コード例 #3
0
        public async Task TestProverCreateProofWorksForInvalidRequestedClaimsJson()
        {
            await InitCommonWallet();

            var proofRequest = "{\"nonce\":\"123432421212\",\n" +
                               "                                \"name\":\"proof_req_1\",\n" +
                               "                                \"version\":\"0.1\",\n" +
                               "                                \"requested_attrs\":{\"attr1_uuid\":{\"schema_seq_no\":1, \"name\":\"name\"}},\n" +
                               "                                \"requested_predicates\":{\"predicate1_uuid\":{\"attr_name\":\"age\",\"p_type\":\"GE\",\"value\":18}}\n" +
                               "                              }";

            var claimsJson = await AnonCreds.ProverGetClaimsForProofReqAsync(commonWallet, proofRequest);

            var claims = JObject.Parse(claimsJson);

            var claimForAttribute = claims["attrs"]["attr1_uuid"][0];

            var claimUuid = claimForAttribute.Value <string>("claim_uuid");

            String requestedClaimsJson = "{\"self_attested_attributes\":{},\n" +
                                         "                      \"requested_predicates\":{}\n" +
                                         "                    }";

            var schemasJson   = string.Format("{{\"{0}\":{1}}}", claimUuid, schema);
            var claimDefsJson = string.Format("{{\"{0}\":{1}}}", claimUuid, claimDef);
            var revocRegsJson = "{}";

            var ex = await Assert.ThrowsExceptionAsync <InvalidStructureException>(() =>
                                                                                   AnonCreds.ProverCreateProofAsync(commonWallet, proofRequest, requestedClaimsJson, schemasJson, "wrong_master_secret", claimDefsJson, revocRegsJson)

                                                                                   );
        }
コード例 #4
0
        public async Task TestProverCreateProofWorksForUsingNotSatisfyClaim()
        {
            await InitCommonWallet();

            var claimsJson = await AnonCreds.ProverGetClaimsAsync(commonWallet, "{}");

            var claims = JArray.Parse(claimsJson);

            var claimUuid = claims[0].Value <string>("claim_uuid");

            var proofRequest = "{\"nonce\":\"123432421212\",\n" +
                               "               \"name\":\"proof_req_1\",\n" +
                               "               \"version\":\"0.1\",\n" +
                               "               \"requested_attrs\":{\"attr1_uuid\":{\"schema_seq_no\":1, \"name\":\"some_attr\"}},\n" +
                               "               \"requested_predicates\":{}\n" +
                               "              }";

            var requestedClaimsJson = string.Format("{{\"self_attested_attributes\":{{}},\n" +
                                                    "                                    \"requested_attrs\":{{\"attr1_uuid\":[\"{0}\", true]}},\n" +
                                                    "                                    \"requested_predicates\":{{}}\n" +
                                                    "                                   }}", claimUuid);

            var schemasJson   = string.Format("{{\"{0}\":{1}}}", claimUuid, schema);
            var claimDefsJson = string.Format("{{\"{0}\":{1}}}", claimUuid, claimDef);
            var revocRegsJson = "{}";

            var ex = await Assert.ThrowsExceptionAsync <InvalidStructureException>(() =>
                                                                                   AnonCreds.ProverCreateProofAsync(commonWallet, proofRequest, requestedClaimsJson, schemasJson,
                                                                                                                    masterSecretName, claimDefsJson, revocRegsJson)
                                                                                   );
        }
コード例 #5
0
        public async Task TestAnoncredsWorksForClaimRevokedBeforeProofCreated()
        {
            //9. Issuer revoke claim
            var revocRegUpdateJson = await AnonCreds.IssuerRevokeCredentialAsync(
                _issuerWallet,
                issuerDid,
                1,
                1);

            //10. Prover gets Claims for Proof Request
            var claimsJson = await AnonCreds.ProverGetCredentialsForProofReqAsync(_issuerWallet, _proofReqJson);

            var claims         = JObject.Parse(claimsJson);
            var claimsForAttr1 = claims["attrs"]["attr1_referent"];
            var claim          = claimsForAttr1[0];
            var claimUuid      = claim.Value <string>("referent");

            //11. Prover create Proof
            var requestedClaimsJson = string.Format(_requestedClaimsJsonTemplate, claimUuid);

            var schemasJson    = string.Format("{{\"{0}\":{1}}}", claimUuid, schema);
            var claimDefsJson  = string.Format("{{\"{0}\":{1}}}", claimUuid, _claimDefJson);
            var revocRegsJsons = string.Format("{{\"{0}\":{1}}}", claimUuid, revocRegUpdateJson);

            var ex = await Assert.ThrowsExceptionAsync <ClaimRevokedException>(() =>
                                                                               AnonCreds.ProverCreateProofAsync(
                                                                                   _issuerWallet,
                                                                                   _proofReqJson,
                                                                                   requestedClaimsJson,
                                                                                   schemasJson,
                                                                                   masterSecretName,
                                                                                   claimDefsJson,
                                                                                   revocRegsJsons)
                                                                               );
        }
コード例 #6
0
        /// <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);
        }
コード例 #7
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);
        }
コード例 #8
0
        public async Task TestProverCreateProofWorksForInvalidMasterSecret()
        {
            var schemasJson        = new JObject(new JProperty(gvtSchemaId, JObject.Parse(gvtSchema))).ToString();
            var credentialDefsJson = new JObject(new JProperty(issuer1gvtCredDefId, JObject.Parse(issuer1gvtCredDef))).ToString();
            var revocStatesJson    = "{}";

            var ex = await Assert.ThrowsExceptionAsync <WalletItemNotFoundException>(() =>
                                                                                     AnonCreds.ProverCreateProofAsync(wallet, proofRequest, requestedCredentialsJson, "wrong_master_secret", schemasJson, credentialDefsJson, revocStatesJson)
                                                                                     );
        }
コード例 #9
0
        public async Task TestProverCreateProofWorksForInvalidSchemas()
        {
            var schemasJson        = "{}";
            var credentialDefsJson = new JObject(new JProperty(issuer1gvtCredDefId, issuer1gvtCredDef)).ToString();
            var revocStatesJson    = "{}";

            var ex = await Assert.ThrowsExceptionAsync <InvalidStructureException>(() =>
                                                                                   AnonCreds.ProverCreateProofAsync(wallet, proofRequest, requestedCredentialsJson, masterSecretId, schemasJson, credentialDefsJson, revocStatesJson)

                                                                                   );
        }
コード例 #10
0
        /// <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);
        }
コード例 #11
0
        public async Task TestProverCreateProofWorksForInvalidRequestedClaimsJson()
        {
            var schemasJson              = new JObject(new JProperty(gvtSchemaId, gvtSchema)).ToString();
            var credentialDefsJson       = new JObject(new JProperty(issuer1gvtCredDefId, issuer1gvtCredDef)).ToString();
            var revocStatesJson          = "{}";
            var requestedCredentialsJson = JsonConvert.SerializeObject(
                new { self_attested_attributes = new JObject(), requested_predicates = new JObject() }
                );


            var ex = await Assert.ThrowsExceptionAsync <InvalidStructureException>(() =>
                                                                                   AnonCreds.ProverCreateProofAsync(wallet, proofRequest, requestedCredentialsJson, masterSecretId, schemasJson, credentialDefsJson, revocStatesJson)

                                                                                   );
        }
コード例 #12
0
        public async Task TestProverCreateProofWorksForUsingNotSatisfyClaim()
        {
            var requestedCredentialsJson = string.Format("{{\"self_attested_attributes\":{{}},\n" +
                                                         "                                    \"requested_attributes\":{{\"attr1_referent\":{{\"cred_id\":\"{0}\", \"revealed\":true}}}},\n" +
                                                         "                                    \"requested_predicates\":{{}}\n" +
                                                         "                                   }}", credentialId2);

            var schemasJson        = new JObject(new JProperty(xyzSchemaId, xyzSchema)).ToString();
            var credentialDefsJson = new JObject(new JProperty(issuer1xyzCredDef, issuer1xyzCredDef)).ToString();
            var revocStatesJson    = "{}";

            await Assert.ThrowsExceptionAsync <InvalidStructureException>(() =>
                                                                          AnonCreds.ProverCreateProofAsync(wallet, proofRequest, requestedCredentialsJson,
                                                                                                           masterSecretId, schemasJson, credentialDefsJson, revocStatesJson)
                                                                          );
        }
コード例 #13
0
        public async Task TestAnoncredsWorksForClaimRevokedAfterProofCreated()
        {
            //9. Prover gets Claims for Proof Request
            var claimsJson = await AnonCreds.ProverGetCredentialsForProofReqAsync(_issuerWallet, _proofReqJson);

            var claims         = JObject.Parse(claimsJson);
            var claimsForAttr1 = claims["attrs"]["attr1_referent"];
            var claim          = claimsForAttr1[0];
            var claimUuid      = claim.Value <string>("referent");

            //10. Prover create Proof
            var requestedClaimsJson = string.Format(_requestedClaimsJsonTemplate, claimUuid);

            var schemasJson    = string.Format("{{\"{0}\":{1}}}", claimUuid, schema);
            var claimDefsJson  = string.Format("{{\"{0}\":{1}}}", claimUuid, _claimDefJson);
            var revocRegsJsons = string.Format("{{\"{0}\":{1}}}", claimUuid, _claimResult.RevocRegDeltaJson);

            var proofJson = await AnonCreds.ProverCreateProofAsync(
                _issuerWallet,
                _proofReqJson,
                requestedClaimsJson,
                schemasJson,
                masterSecretName,
                claimDefsJson,
                revocRegsJsons);

            //11. Issuer revoke prover claim
            // TODO
            //var revocRegUpdateJson = await AnonCreds.IssuerRevokeCredentialAsync(
            //    _issuerWallet,
            //    issuerDid,
            //    1,
            //    1);

            ////12. Verifier verify proof
            //var updatedRevocRegsJsons = string.Format("{{\"{0}\":{1}}}", claimUuid, revocRegUpdateJson);

            var valid = false; // TODO await AnonCreds.VerifierVerifyProofAsync(

            //_proofReqJson,
            //proofJson,
            //schemasJson,
            //claimDefsJson,
            //updatedRevocRegsJsons);

            Assert.IsFalse(valid);
        }
コード例 #14
0
        public async Task TestProverCreateProofWorks()
        {
            var schemasJson        = new JObject(new JProperty(gvtSchemaId, JObject.Parse(gvtSchema))).ToString();
            var credentialDefsJson = new JObject(new JProperty(issuer1gvtCredDefId, JObject.Parse(issuer1gvtCredDef))).ToString();
            var revocStatesJson    = "{}";

            var proofJson = await AnonCreds.ProverCreateProofAsync(
                wallet,
                proofRequest,
                requestedCredentialsJson,
                masterSecretId,
                schemasJson,
                credentialDefsJson,
                revocStatesJson);

            Assert.IsNotNull(proofJson);
        }
コード例 #15
0
        public async Task TestAnonCredsWorksForSingleIssuerSingleProverMultipleClaims()
        {
            var issuerDid = "NcYxiDXkpYi6ov5FcYDi1e";

            //1. Issuer create ClaimDef
            var gvtSchemaJson = "{\n" +
                                "                    \"seqNo\":1,\n" +
                                "                    \"data\": {\n" +
                                "                        \"name\":\"gvt\",\n" +
                                "                        \"version\":\"1.0\",\n" +
                                "                        \"attr_names\":[\"age\",\"sex\",\"height\",\"name\"]\n" +
                                "                    }\n" +
                                "                }";

            var gvtClaimDef = await AnonCreds.IssuerCreateAndStoreClaimDefAsync(_issuerWallet, issuerDid, gvtSchemaJson, null, false);

            //2. Issuer create ClaimDef
            var xyzSchemaJson = "{\n" +
                                "                    \"seqNo\":2,\n" +
                                "                    \"data\": {\n" +
                                "                        \"name\":\"xyz\",\n" +
                                "                        \"version\":\"1.0\",\n" +
                                "                        \"attr_names\":[\"status\",\"period\"]\n" +
                                "                    }\n" +
                                "                }";

            var xyzClaimDef = await AnonCreds.IssuerCreateAndStoreClaimDefAsync(_issuerWallet, issuerDid, xyzSchemaJson, null, false);

            //3. Prover create Master Secret
            var masterSecret = "masterSecretName";
            await AnonCreds.ProverCreateMasterSecretAsync(_proverWallet, masterSecret);

            //4. Prover store Claim Offer received from Issuer
            var claimOffer = string.Format("{{\"issuer_did\":\"{0}\", \"schema_seq_no\":{1}}}", issuerDid, 1);
            await AnonCreds.ProverStoreClaimOfferAsync(_proverWallet, claimOffer);

            //5. Prover store Claim Offer received from Issuer
            var claimOffer2 = string.Format("{{\"issuer_did\":\"{0}\", \"schema_seq_no\":{1}}}", issuerDid, 2);
            await AnonCreds.ProverStoreClaimOfferAsync(_proverWallet, claimOffer2);

            //6. Prover get Claim Offers
            var claimOffersJson = await AnonCreds.ProverGetClaimOffersAsync(_proverWallet, "{}");

            var claimOffersObject = JArray.Parse(claimOffersJson);

            Assert.AreEqual(2, claimOffersObject.Count);

            var claimOfferObj1 = claimOffersObject[0];
            var claimOfferObj2 = claimOffersObject[1];

            var gvtClaimOffer = claimOfferObj1.Value <int>("schema_seq_no") == 1 ? claimOfferObj1.ToString() : claimOfferObj2.ToString();
            var xyzClaimOffer = claimOfferObj1.Value <int>("schema_seq_no") == 2 ? claimOfferObj1.ToString() : claimOfferObj2.ToString();


            //7. Prover create ClaimReq for GVT Claim Offer
            var proverDid   = "BzfFCYk";
            var gvtClaimReq = await AnonCreds.ProverCreateAndStoreClaimReqAsync(_proverWallet, proverDid, gvtClaimOffer, gvtClaimDef, masterSecret);

            //8. Issuer create Claim
            var gvtClaimAttributesJson = "{\n" +
                                         "               \"sex\":[\"male\",\"5944657099558967239210949258394887428692050081607692519917050011144233115103\"],\n" +
                                         "               \"name\":[\"Alex\",\"1139481716457488690172217916278103335\"],\n" +
                                         "               \"height\":[\"175\",\"175\"],\n" +
                                         "               \"age\":[\"28\",\"28\"]\n" +
                                         "        }";

            var gvtCreateClaimResult = await AnonCreds.IssuerCreateClaimAsync(_issuerWallet, gvtClaimReq, gvtClaimAttributesJson, -1);

            var gvtClaimJson = gvtCreateClaimResult.ClaimJson;

            //9. Prover store Claim
            await AnonCreds.ProverStoreClaimAsync(_proverWallet, gvtClaimJson);

            //10. Prover create ClaimReq for GVT Claim Offer
            var xyzClaimReq = await AnonCreds.ProverCreateAndStoreClaimReqAsync(_proverWallet, proverDid, xyzClaimOffer, xyzClaimDef, masterSecret);

            //11. Issuer create Claim
            var xyzClaimAttributesJson = "{\n" +
                                         "               \"status\":[\"partial\",\"51792877103171595686471452153480627530895\"],\n" +
                                         "               \"period\":[\"8\",\"8\"]\n" +
                                         "        }";

            var xyzCreateClaimResult = await AnonCreds.IssuerCreateClaimAsync(_issuerWallet, xyzClaimReq, xyzClaimAttributesJson, -1);

            var xyzClaimJson = xyzCreateClaimResult.ClaimJson;

            //12. Prover store Claim
            await AnonCreds.ProverStoreClaimAsync(_proverWallet, xyzClaimJson);

            //13. Prover gets Claims for Proof Request
            var proofRequestJson = "{\n" +
                                   "                          \"nonce\":\"123432421212\",\n" +
                                   "                          \"name\":\"proof_req_1\",\n" +
                                   "                          \"version\":\"0.1\",\n" +
                                   "                          \"requested_attrs\":{\"attr1_referent\":{\"name\":\"name\",\"restrictions\":[{\"schema_seq_no\":1}]}},\n" +
                                   "                          \"requested_predicates\":{\"predicate1_referent\":{\"attr_name\":\"age\",\"p_type\":\">=\",\"value\":18}," +
                                   "                                                    \"predicate2_referent\":{\"attr_name\":\"period\",\"p_type\":\">=\",\"value\":5}}\n" +
                                   "                  }";


            var claimsForProofJson = await AnonCreds.ProverGetClaimsForProofReqAsync(_proverWallet, proofRequestJson);

            Assert.IsNotNull(claimsForProofJson);

            var claimsForProof      = JObject.Parse(claimsForProofJson);
            var claimsForAttribute1 = (JArray)claimsForProof["attrs"]["attr1_referent"];
            var claimsForPredicate1 = (JArray)claimsForProof["predicates"]["predicate1_referent"];
            var claimsForPredicate2 = (JArray)claimsForProof["predicates"]["predicate2_referent"];

            Assert.AreEqual(claimsForAttribute1.Count, 1);
            Assert.AreEqual(claimsForPredicate1.Count, 1);
            Assert.AreEqual(claimsForPredicate2.Count, 1);

            var claimUuidForAttr1      = claimsForAttribute1[0].Value <string>("referent");
            var claimUuidForPredicate1 = claimsForPredicate1[0].Value <string>("referent");
            var claimUuidForPredicate2 = claimsForPredicate2[0].Value <string>("referent");

            //14. Prover create Proof
            var requestedClaimsJson = string.Format("{{\n" +
                                                    "                                          \"self_attested_attributes\":{{}},\n" +
                                                    "                                          \"requested_attrs\":{{\"attr1_referent\":[\"{0}\", true]}},\n" +
                                                    "                                          \"requested_predicates\":{{\"predicate1_referent\":\"{1}\"," +
                                                    "                                                                    \"predicate2_referent\":\"{2}\"}}\n" +
                                                    "                                        }}", claimUuidForAttr1, claimUuidForPredicate1, claimUuidForPredicate2);

            var schemasJson   = string.Format("{{\"{0}\":{1}, \"{2}\":{3}}}", claimUuidForAttr1, gvtSchemaJson, claimUuidForPredicate2, xyzSchemaJson);
            var claimDefsJson = string.Format("{{\"{0}\":{1}, \"{2}\":{3}}}", claimUuidForAttr1, gvtClaimDef, claimUuidForPredicate2, xyzClaimDef);

            var revocRegsJson = "{}";

            var proofJson = await AnonCreds.ProverCreateProofAsync(_proverWallet, proofRequestJson, requestedClaimsJson, schemasJson,
                                                                   masterSecret, claimDefsJson, revocRegsJson);

            Assert.IsNotNull(proofJson);

            var proof = JObject.Parse(proofJson);

            //15. Verifier verify Proof
            Assert.AreEqual("Alex",
                            proof["requested_proof"]["revealed_attrs"]["attr1_referent"][1]);

            var valid = await AnonCreds.VerifierVerifyProofAsync(proofRequestJson, proofJson, schemasJson, claimDefsJson, revocRegsJson);

            Assert.IsTrue(valid);
        }
コード例 #16
0
        public static async Task Execute()
        {
            Console.WriteLine("Anoncreds sample -> started");

            var issuerWalletName = "issuerWallet";
            var proverWalletName = "proverWallet";

            var issuerWalletCredentials = "{\"key\":\"issuer_wallet_key\"}";
            var proverWalletCredentials = "{\"key\":\"prover_wallet_key\"}";


            var issuerDid = "NcYxiDXkpYi6ov5FcYDi1e";
            var proverDid = "VsKV7grR1BUE29mG2Fm2kX";

            try
            {
                //1. Create and Open Pool
                await PoolUtils.CreatePoolLedgerConfig();

                //2. Issuer Create and Open Wallet
                await WalletUtils.CreateWalletAsync(PoolUtils.DEFAULT_POOL_NAME, issuerWalletName, "default", null, issuerWalletCredentials);

                //3. Prover Create and Open Wallet
                await WalletUtils.CreateWalletAsync(PoolUtils.DEFAULT_POOL_NAME, proverWalletName, "default", null, proverWalletCredentials);

                // Open pool and wallets in using statements to ensure they are closed when finished.
                using (var pool = await Pool.OpenPoolLedgerAsync(PoolUtils.DEFAULT_POOL_NAME, "{}"))
                    using (var issuerWallet = await Wallet.OpenWalletAsync(issuerWalletName, null, issuerWalletCredentials))
                        using (var proverWallet = await Wallet.OpenWalletAsync(proverWalletName, null, proverWalletCredentials))
                        {
                            //4. Issuer Creates Credential Schema
                            var schemaName         = "gvt";
                            var schemaVersion      = "1.0";
                            var schemaAttributes   = "[\"name\", \"age\", \"sex\", \"height\"]";
                            var createSchemaResult = await AnonCreds.IssuerCreateSchemaAsync(issuerDid, schemaName, schemaVersion, schemaAttributes);

                            var schemaId   = createSchemaResult.SchemaId;
                            var schemaJson = createSchemaResult.SchemaJson;

                            //5. Issuer create Credential Definition
                            var credDefTag          = "Tag1";
                            var credDefConfigJson   = "{\"support_revocation\":false}";
                            var createCredDefResult = await AnonCreds.IssuerCreateAndStoreCredentialDefAsync(issuerWallet, issuerDid, schemaJson, credDefTag, null, credDefConfigJson);

                            var credDefId   = createCredDefResult.CredDefId;
                            var credDefJson = createCredDefResult.CredDefJson;

                            //6. Prover create Master Secret
                            var masterSecretId = await AnonCreds.ProverCreateMasterSecretAsync(proverWallet, null);

                            //7. Issuer Creates Credential Offer
                            var credOffer = await AnonCreds.IssuerCreateCredentialOfferAsync(issuerWallet, credDefId);

                            //8. Prover Creates Credential Request
                            var createCredReqResult = await AnonCreds.ProverCreateCredentialReqAsync(proverWallet, proverDid, credOffer, credDefJson, masterSecretId);

                            var credReqJson         = createCredReqResult.CredentialRequestJson;
                            var credReqMetadataJson = createCredReqResult.CredentialRequestMetadataJson;

                            //9. Issuer create Credential
                            var credValuesJson = "{\n" +
                                                 "        \"sex\": {\"raw\": \"male\", \"encoded\": \"594465709955896723921094925839488742869205008160769251991705001\"},\n" +
                                                 "        \"name\": {\"raw\": \"Alex\", \"encoded\": \"1139481716457488690172217916278103335\"},\n" +
                                                 "        \"height\": {\"raw\": \"175\", \"encoded\": \"175\"},\n" +
                                                 "        \"age\": {\"raw\": \"28\", \"encoded\": \"28\"}\n" +
                                                 "    }";

                            var createCredentialResult = await AnonCreds.IssuerCreateCredentialAsync(issuerWallet, credOffer, credReqJson, credValuesJson, null, null);

                            var credential = createCredentialResult.CredentialJson;

                            //10. Prover Stores Credential
                            await AnonCreds.ProverStoreCredentialAsync(proverWallet, null, credReqJson, credReqMetadataJson, credential, credDefJson, null);

                            //11. Prover Gets Credentials for Proof Request
                            var proofRequestJson = "{" +
                                                   "                    \"nonce\":\"123432421212\",\n" +
                                                   "                    \"name\":\"proof_req_1\",\n" +
                                                   "                    \"version\":\"0.1\", " +
                                                   "                    \"requested_attributes\": {" +
                                                   "                          \"attr1_referent\":{\"name\":\"name\"}," +
                                                   "                          \"attr2_referent\":{\"name\":\"sex\"}," +
                                                   "                          \"attr3_referent\":{\"name\":\"phone\"}" +
                                                   "                     }," +
                                                   "                    \"requested_predicates\":{" +
                                                   "                         \"predicate1_referent\":{\"name\":\"age\",\"p_type\":\">=\",\"p_value\":18}" +
                                                   "                    }" +
                                                   "                  }";

                            var credentialsForProofJson = await AnonCreds.ProverGetCredentialsForProofReqAsync(proverWallet, proofRequestJson);

                            var credentialsForProof      = JObject.Parse(credentialsForProofJson);
                            var credentialsForAttribute1 = (JArray)credentialsForProof["attrs"]["attr1_referent"];
                            var credentialsForAttribute2 = (JArray)credentialsForProof["attrs"]["attr2_referent"];
                            var credentialsForAttribute3 = (JArray)credentialsForProof["attrs"]["attr3_referent"];
                            var credentialsForPredicate  = (JArray)credentialsForProof["predicates"]["predicate1_referent"];

                            Debug.Assert(credentialsForAttribute1.Count == 1);
                            Debug.Assert(credentialsForAttribute2.Count == 1);
                            Debug.Assert(credentialsForAttribute3.Count == 0);
                            Debug.Assert(credentialsForPredicate.Count == 1);

                            var credentialId = credentialsForAttribute1[0]["cred_info"]["referent"].ToObject <string>();

                            //12. Prover Creates Proof
                            var selfAttestedValue        = "8-800-300";
                            var requestedCredentialsJson = string.Format(
                                "{{\n" +
                                "                                          \"self_attested_attributes\":{{\"attr3_referent\":\"{0}\"}},\n" +
                                "                                          \"requested_attributes\":{{\"attr1_referent\":{{\"cred_id\":\"{1}\", \"revealed\":true}},\n" +
                                "                                                                    \"attr2_referent\":{{\"cred_id\":\"{2}\", \"revealed\":false}}}},\n" +
                                "                                          \"requested_predicates\":{{\"predicate1_referent\":{{\"cred_id\":\"{3}\"}}}}\n" +
                                "                                        }}", selfAttestedValue, credentialId, credentialId, credentialId);

                            var schemas        = string.Format("{{\"{0}\":{1}}}", schemaId, schemaJson);
                            var credentialDefs = string.Format("{{\"{0}\":{1}}}", credDefId, credDefJson);
                            var revocStates    = "{}";

                            var proofJson = await AnonCreds.ProverCreateProofAsync(proverWallet, proofRequestJson, requestedCredentialsJson, masterSecretId, schemas, credentialDefs, revocStates);

                            var proof = JObject.Parse(proofJson);

                            //13. Verifier verify Proof
                            var revealedAttr1 = proof["requested_proof"]["revealed_attrs"]["attr1_referent"];
                            Debug.Assert("Alex" == revealedAttr1["raw"].ToObject <string>());

                            Debug.Assert(null != proof["requested_proof"]["unrevealed_attrs"]["attr2_referent"]["sub_proof_index"]);

                            Debug.Assert(selfAttestedValue == proof["requested_proof"]["self_attested_attrs"]["attr3_referent"].ToObject <string>());

                            var revocRegDefs = "{}";
                            var revocRegs    = "{}";

                            var valid = await AnonCreds.VerifierVerifyProofAsync(proofRequestJson, proofJson, schemas, credentialDefs, revocRegDefs, revocRegs);

                            Debug.Assert(valid);

                            //14. Close wallets and pool
                            await issuerWallet.CloseAsync();

                            await proverWallet.CloseAsync();

                            await pool.CloseAsync();
                        }
            }
            finally
            {
                //15. Delete wallets and Pool ledger config
                await WalletUtils.DeleteWalletAsync(issuerWalletName, issuerWalletCredentials);

                await WalletUtils.DeleteWalletAsync(proverWalletName, proverWalletCredentials);

                await PoolUtils.DeletePoolLedgerConfigAsync(PoolUtils.DEFAULT_POOL_NAME);
            }

            Console.WriteLine("Anoncreds sample -> completed");
        }
コード例 #17
0
        public async Task TestVerifyProofWorksForProofDoesNotCorrespondToProofRequest()
        {
            // Issuer create Schema
            var createSchemaResult = await AnonCreds.IssuerCreateSchemaAsync(issuerDid, GVT_SCHEMA_NAME, SCHEMA_VERSION, GVT_SCHEMA_ATTRIBUTES);

            var gvtSchemaId = createSchemaResult.SchemaId;
            var gvtSchema   = createSchemaResult.SchemaJson;

            // Issuer create CredentialDef
            var createCredDefResult = await AnonCreds.IssuerCreateAndStoreCredentialDefAsync(issuerWallet, issuerDid, gvtSchema, TAG, null, DEFAULT_CRED_DEF_CONFIG);

            var credDefId = createCredDefResult.CredDefId;
            var credDef   = createCredDefResult.CredDefJson;

            // Prover create Master Secret
            await AnonCreds.ProverCreateMasterSecretAsync(proverWallet, masterSecretId);

            // Issuer create Credential Offer
            var credOffer = await AnonCreds.IssuerCreateCredentialOfferAsync(issuerWallet, credDefId);

            // Prover create CredentialReq
            var createCredReqResult = await AnonCreds.ProverCreateCredentialReqAsync(proverWallet, proverDid, credOffer, credDef, masterSecretId);

            var credReq         = createCredReqResult.CredentialRequestJson;
            var credReqMetadata = createCredReqResult.CredentialRequestMetadataJson;

            // Issuer create Credential
            var createCredentialResult = await AnonCreds.IssuerCreateCredentialAsync(issuerWallet, credOffer, credReq, gvtCredentialValues, null, null);

            var credential = createCredentialResult.CredentialJson;

            // Prover store Credential
            await AnonCreds.ProverStoreCredentialAsync(proverWallet, credentialId1, credReqMetadata, credential, credDef, null);

            // Prover gets Credentials for Proof Request
            var proofRequestJson = JsonConvert.SerializeObject(
                new
            {
                nonce   = "123432421212",
                name    = "proof_req_1",
                version = "0.1",
                requested_attributes = new
                {
                    attr1_referent = new
                    {
                        name         = "name",
                        restrictions = new[] { new { schema_id = gvtSchemaId } }
                    },
                    attr2_referent = new { name = "phone" }
                },
                requested_predicates = new {}
            }
                );

            var credentialsForProofJson = await AnonCreds.ProverGetCredentialsForProofReqAsync(proverWallet, proofRequestJson);

            Assert.IsNotNull(credentialsForProofJson);

            var credentialsForProof      = JObject.Parse(credentialsForProofJson);
            var credentialsForAttribute1 = (JArray)credentialsForProof["attrs"]["attr1_referent"];

            Assert.AreEqual(1, credentialsForAttribute1.Count);

            var credentialUuid = credentialsForAttribute1[0]["cred_info"]["referent"];

            // Prover create Proof
            var selfAttestedValue        = "8-800-300";
            var requestedCredentialsJson = JsonConvert.SerializeObject(
                new
            {
                self_attested_attributes = new { attr3_referent = selfAttestedValue },
                requested_attributes     = new
                {
                    attr1_referent = new { cred_id = credentialUuid, revealed = true },
                },
                requested_predicates = new { }
            }
                );

            var schemas        = new JObject(new JProperty(gvtSchemaId, JObject.Parse(gvtSchema))).ToString();
            var credentialDefs = new JObject(new JProperty(credDefId, JObject.Parse(credDef))).ToString();
            var revocInfos     = "{}";

            var proofJson = await AnonCreds.ProverCreateProofAsync(proverWallet, proofRequestJson, requestedCredentialsJson,
                                                                   masterSecretId, schemas, credentialDefs, revocInfos);

            var proof = JObject.Parse(proofJson);

            // Verifier verify Proof
            var revealedAttr1 = proof["requested_proof"]["revealed_attrs"]["attr1_referent"];

            Assert.AreEqual("Alex", revealedAttr1["raw"]);

            Assert.AreEqual(selfAttestedValue, proof["requested_proof"]["self_attested_attrs"]["attr3_referent"]);

            var revocRegDefs = "{}";
            var revocRegs    = "{}";

            proofRequestJson = JsonConvert.SerializeObject(
                new
            {
                nonce   = "123432421212",
                name    = "proof_req_1",
                version = "0.1",
                requested_attributes = new
                {
                    attr1_referent = new
                    {
                        name         = "name",
                        restrictions = new[] { new { schema_id = gvtSchemaId } }
                    },
                    attr2_referent = new { name = "phone" }
                },
                requested_predicates = new
                {
                    predicate1_referent = new
                    {
                        name    = "age",
                        p_type  = ">=",
                        p_value = 18
                    }
                }
            }
                );

            var ex = await Assert.ThrowsExceptionAsync <InvalidStructureException>(() =>
                                                                                   AnonCreds.VerifierVerifyProofAsync(proofRequestJson, proofJson, schemas, credentialDefs, revocRegDefs, revocRegs)
                                                                                   );
        }
コード例 #18
0
        public async Task TestAnonCredsDemo()
        {
            var createSchemaResult = await AnonCreds.IssuerCreateSchemaAsync(issuerDid, GVT_SCHEMA_NAME, SCHEMA_VERSION, GVT_SCHEMA_ATTRIBUTES);

            var gvtSchemaId = createSchemaResult.SchemaId;
            var gvtSchema   = createSchemaResult.SchemaJson;

            // Issuer create CredentialDef
            var createCredDefResult = await AnonCreds.IssuerCreateAndStoreCredentialDefAsync(issuerWallet, issuerDid, gvtSchema, TAG, null, DEFAULT_CRED_DEF_CONFIG);

            var credDefId = createCredDefResult.CredDefId;
            var credDef   = createCredDefResult.CredDefJson;

            // Prover create Master Secret
            await AnonCreds.ProverCreateMasterSecretAsync(proverWallet, masterSecretId);

            // Issuer create Credential Offer
            var credOffer = await AnonCreds.IssuerCreateCredentialOfferAsync(issuerWallet, credDefId);

            // Prover create CredentialReq
            var createCredReqResult = await AnonCreds.ProverCreateCredentialReqAsync(proverWallet, proverDid, credOffer, credDef, masterSecretId);

            var credReq         = createCredReqResult.CredentialRequestJson;
            var credReqMetadata = createCredReqResult.CredentialRequestMetadataJson;

            // Issuer create Credential
            var createCredentialResult = await AnonCreds.IssuerCreateCredentialAsync(issuerWallet, credOffer, credReq, gvtCredentialValues, null, null);

            var credential = createCredentialResult.CredentialJson;

            // Prover store Credential
            await AnonCreds.ProverStoreCredentialAsync(proverWallet, credentialId1, credReqMetadata, credential, credDef, null);

            // Prover gets Credentials for Proof Request
            var proofRequestJson = "{" +
                                   "                    \"nonce\":\"123432421212\",\n" +
                                   "                    \"name\":\"proof_req_1\",\n" +
                                   "                    \"version\":\"0.1\", " +
                                   "                    \"requested_attributes\": {" +
                                   "                          \"attr1_referent\":{\"name\":\"name\"}," +
                                   "                          \"attr2_referent\":{\"name\":\"sex\"}," +
                                   "                          \"attr3_referent\":{\"name\":\"phone\"}" +
                                   "                     }," +
                                   "                    \"requested_predicates\":{" +
                                   "                         \"predicate1_referent\":{\"name\":\"age\",\"p_type\":\">=\",\"p_value\":18}" +
                                   "                    }" +
                                   "                  }";

            var credentialsForProofJson = await AnonCreds.ProverGetCredentialsForProofReqAsync(proverWallet, proofRequestJson);

            var credentialsForProof      = JObject.Parse(credentialsForProofJson);
            var credentialsForAttribute1 = (JArray)credentialsForProof["attrs"]["attr1_referent"];
            var credentialsForAttribute2 = (JArray)credentialsForProof["attrs"]["attr2_referent"];
            var credentialsForAttribute3 = (JArray)credentialsForProof["attrs"]["attr3_referent"];
            var credentialsForPredicate  = (JArray)credentialsForProof["predicates"]["predicate1_referent"];

            Assert.AreEqual(1, credentialsForAttribute1.Count);
            Assert.AreEqual(1, credentialsForAttribute2.Count);
            Assert.AreEqual(0, credentialsForAttribute3.Count);
            Assert.AreEqual(1, credentialsForPredicate.Count);

            var credentialUuid = credentialsForAttribute1[0]["cred_info"]["referent"];

            // Prover create Proof
            var selfAttestedValue        = "8-800-300";
            var requestedCredentialsJson = JsonConvert.SerializeObject(
                new
            {
                self_attested_attributes = new
                {
                    attr3_referent = selfAttestedValue
                },
                requested_attributes = new
                {
                    attr1_referent = new { cred_id = credentialUuid, revealed = true },
                    attr2_referent = new { cred_id = credentialUuid, revealed = false }
                },
                requested_predicates = new
                {
                    predicate1_referent = new { cred_id = credentialUuid }
                }
            }
                );

            var schemas        = new JObject(new JProperty(gvtSchemaId, JObject.Parse(gvtSchema))).ToString();
            var credentialDefs = new JObject(new JProperty(credDefId, JObject.Parse(credDef))).ToString();
            var revocStates    = "{}";

            var proofJson = await AnonCreds.ProverCreateProofAsync(proverWallet, proofRequestJson, requestedCredentialsJson,
                                                                   masterSecretId, schemas, credentialDefs, revocStates);

            var proof = JObject.Parse(proofJson);

            // Verifier verify Proof
            var revealedAttr1 = proof["requested_proof"]["revealed_attrs"]["attr1_referent"];

            Assert.AreEqual("Alex", revealedAttr1["raw"]);
            Assert.IsNotNull(proof["requested_proof"]["unrevealed_attrs"]["attr2_referent"]["sub_proof_index"]);
            Assert.AreEqual(selfAttestedValue, proof["requested_proof"]["self_attested_attrs"]["attr3_referent"]);

            var revocRegDefs = "{}";
            var revocRegs    = "{}";

            var valid = await AnonCreds.VerifierVerifyProofAsync(proofRequestJson, proofJson, schemas, credentialDefs, revocRegDefs, revocRegs);

            Assert.IsTrue(valid);
        }
コード例 #19
0
        /*
         * Proofs the holder of the open wallet is a doctor by using the first
         * credential that meets the docotr proof requirements.
         */
        public static async Task <string> createDoctorProof(
            Wallet wallet,
            string masterKey = "doctor-certificate")
        {
            string proofReqJson = getProofRequest();

            proofReqJson = proofReqJson.Replace(" ", string.Empty);
            proofReqJson = proofReqJson.Replace(Environment.NewLine, string.Empty);
            try
            {
                var credList =
                    await AnonCreds.ProverSearchCredentialsForProofRequestAsync(
                        wallet, proofReqJson);

                string attr1Cred = await getCredentialforRequest(
                    credList, "attr1_referent");

                string attr2Cred = await getCredentialforRequest(
                    credList, "attr2_referent");

                string predicate1Cred = await getCredentialforRequest(
                    credList, "predicate1_referent");

                string requestedCreds = proverDoctorRequestCreds(
                    getReferentFromCredential(attr1Cred),
                    getReferentFromCredential(attr2Cred),
                    getReferentFromCredential(predicate1Cred));

                EmergencyDoctorCredentialModel model =
                    EmergencyDoctorCredentialModel.importFromJsonFile();

                // IOFacilitator io = new IOFacilitator();
                // DoctorCredDefInfoModel model = JsonConvert.DeserializeObject
                //     <DoctorCredDefInfoModel>(File.ReadAllText(
                //         io.getDoctorCredDefConfigPathAbs()));
                string schemas = "{";
                schemas += "\"" + model.schema_id + "\":" + model.schema_json;
                schemas += "}";
                string credDefs = "{";
                credDefs += "\"" + model.cred_def_id + "\":" + model.cred_def_json;
                credDefs += "}";

                string res = await AnonCreds.ProverCreateProofAsync(
                    wallet,
                    proofReqJson,
                    requestedCreds,
                    masterKey,
                    schemas,
                    credDefs,
                    "{}"
                    );

                return(res);
            }
            catch (InvalidOperationException e)
            {
                return(e.Message);
            }
            catch (Exception e)
            {
                return($"Error: {e.Message}");
            }
        }
コード例 #20
0
        public async Task TestVerifyProofWorksForProofDoesNotCorrespondToProofRequest()
        {
            //1. Issuer create ClaimDef
            var schemaJson = "{\n" +
                             "                    \"seqNo\":1,\n" +
                             "                    \"data\": {\n" +
                             "                        \"name\":\"gvt\",\n" +
                             "                        \"version\":\"1.0\",\n" +
                             "                        \"keys\":[\"age\",\"sex\",\"height\",\"name\"]\n" +
                             "                    }\n" +
                             "                }";
            var issuerDid = "NcYxiDXkpYi6ov5FcYDi1e";

            var claimDef = await AnonCreds.IssuerCreateAndStoreClaimDefAsync(_issuerWallet, issuerDid, schemaJson, null, false);

            Assert.IsNotNull(claimDef);

            //2. Prover create Master Secret
            var masterSecret = "masterSecretName";
            await AnonCreds.ProverCreateMasterSecretAsync(_proverWallet, masterSecret);

            //3. Prover store Claim Offer
            var claimOffer = string.Format("{{\"issuer_did\":\"{0}\", \"schema_seq_no\":{1}}}", issuerDid, 1);
            await AnonCreds.ProverStoreClaimOfferAsync(_proverWallet, claimOffer);

            //4. Prover get Claim Offers
            var claimOfferFilter = string.Format("{{\"issuer_did\":\"{0}\"}}", issuerDid);
            var claimOffersJson  = await AnonCreds.ProverGetClaimOffersAsync(_proverWallet, claimOfferFilter);

            var claimOffersObject = JArray.Parse(claimOffersJson);

            Assert.AreEqual(claimOffersObject.Count, 1);

            var claimOfferObject = claimOffersObject[0];
            var claimOfferJson   = claimOfferObject.ToString();

            //5. Prover create ClaimReq
            var proverDid = "BzfFCYk";
            var claimReq  = await AnonCreds.ProverCreateAndStoreClaimReqAsync(_proverWallet, proverDid, claimOfferJson, claimDef, masterSecret);

            Assert.IsNotNull(claimReq);

            //6. Issuer create Claim
            var claimAttributesJson = "{\n" +
                                      "               \"sex\":[\"male\",\"5944657099558967239210949258394887428692050081607692519917050011144233115103\"],\n" +
                                      "               \"name\":[\"Alex\",\"1139481716457488690172217916278103335\"],\n" +
                                      "               \"height\":[\"175\",\"175\"],\n" +
                                      "               \"age\":[\"28\",\"28\"]\n" +
                                      "        }";

            var createClaimResult = await AnonCreds.IssuerCreateClaimAsync(_issuerWallet, claimReq, claimAttributesJson, -1);

            Assert.IsNotNull(createClaimResult);
            var claimJson = createClaimResult.ClaimJson;

            //7. Prover store Claim
            await AnonCreds.ProverStoreClaimAsync(_proverWallet, claimJson);

            //8. Prover gets Claims for Proof Request
            var proofRequestJson = "{\n" +
                                   "                          \"nonce\":\"123432421212\",\n" +
                                   "                          \"name\":\"proof_req_1\",\n" +
                                   "                          \"version\":\"0.1\",\n" +
                                   "                          \"requested_attrs\":{\"attr1_uuid\":{\"schema_seq_no\":1,\"name\":\"name\"}},\n" +
                                   "                          \"requested_predicates\":{}\n" +
                                   "                  }";

            var claimsForProofJson = await AnonCreds.ProverGetClaimsForProofReqAsync(_proverWallet, proofRequestJson);

            Assert.IsNotNull(claimsForProofJson);

            var claimsForProof      = JObject.Parse(claimsForProofJson);
            var claimsForAttribute1 = (JArray)claimsForProof["attrs"]["attr1_uuid"];


            Assert.AreEqual(claimsForAttribute1.Count, 1);

            var claimUuid = claimsForAttribute1[0].Value <string>("claim_uuid");

            //9. Prover create Proof
            var selfAttestedValue   = "yes";
            var requestedClaimsJson = string.Format("{{\n" +
                                                    "                                          \"self_attested_attributes\":{{\"self1\":\"{0}\"}},\n" +
                                                    "                                          \"requested_attrs\":{{\"attr1_uuid\":[\"{1}\", true]}},\n" +
                                                    "                                          \"requested_predicates\":{{}}\n" +
                                                    "                                        }}", selfAttestedValue, claimUuid);

            var schemasJson   = string.Format("{{\"{0}\":{1}}}", claimUuid, schemaJson);
            var claimDefsJson = string.Format("{{\"{0}\":{1}}}", claimUuid, claimDef);
            var revocRegsJson = "{}";

            //TODO: Not sure why this call is failing...
            var proofJson = await AnonCreds.ProverCreateProofAsync(_proverWallet, proofRequestJson, requestedClaimsJson, schemasJson,
                                                                   masterSecret, claimDefsJson, revocRegsJson);

            Assert.IsNotNull(proofJson);

            var proof = JObject.Parse(proofJson);

            //10. Verifier verify Proof
            Assert.AreEqual("Alex",
                            proof["requested_proof"]["revealed_attrs"]["attr1_uuid"][1]);


            Assert.AreEqual(selfAttestedValue, proof["requested_proof"]["self_attested_attrs"].Value <string>("self1"));

            proofRequestJson = "{\n" +
                               "                            \"nonce\":\"123432421212\",\n" +
                               "                        \"name\":\"proof_req_1\",\n" +
                               "                        \"version\":\"0.1\",\n" +
                               "                    \"requested_attrs\":{\"attr1_uuid\":{\"schema_seq_no\":1,\"name\":\"name\"}},\n" +
                               "                    \"requested_predicates\":{\"predicate1_uuid\":{\"attr_name\":\"age\",\"p_type\":\"GE\",\"value\":18}\n" +
                               "           }";

            var ex = await Assert.ThrowsExceptionAsync <IndyException>(() =>
                                                                       AnonCreds.VerifierVerifyProofAsync(proofRequestJson, proofJson, schemasJson, claimDefsJson, revocRegsJson)
                                                                       );

            Assert.AreEqual(ErrorCode.CommonInvalidStructure, ex.ErrorCode);
        }
コード例 #21
0
        public async Task TestAnonCredsWorksForRevocationProof()
        {
            // Issuer create Schema
            var createSchemaResult = await AnonCreds.IssuerCreateSchemaAsync(issuerDid, GVT_SCHEMA_NAME, SCHEMA_VERSION, GVT_SCHEMA_ATTRIBUTES);

            var gvtSchemaId = createSchemaResult.SchemaId;
            var schemaJson  = createSchemaResult.SchemaJson;

            // Issuer create credential definition
            var revocationCredentialDefConfig = JsonConvert.SerializeObject(new { support_revocation = true });
            var createCredentialDefResult     = await AnonCreds.IssuerCreateAndStoreCredentialDefAsync(issuerWallet, issuerDid, schemaJson, TAG, null, revocationCredentialDefConfig);

            var credDefId = createCredentialDefResult.CredDefId;
            var credDef   = createCredentialDefResult.CredDefJson;

            // Issuer create revocation registry
            var revRegConfig      = JsonConvert.SerializeObject(new { issuance_type = (object)null, max_cred_num = 5 });
            var tailsWriterConfig = JsonConvert.SerializeObject(
                new
            {
                base_dir    = EnvironmentUtils.GetIndyHomePath("tails"),
                uri_pattern = string.Empty
            }
                );
            var tailsWriter = await BlobStorage.OpenWriterAsync("default", tailsWriterConfig);

            var createRevRegResult = await AnonCreds.IssuerCreateAndStoreRevocRegAsync(issuerWallet, issuerDid, null, TAG, credDefId, revRegConfig, tailsWriter);

            var revRegId  = createRevRegResult.RevRegId;
            var revRegDef = createRevRegResult.RevRegDefJson;

            // Prover create Master Secret
            await AnonCreds.ProverCreateMasterSecretAsync(proverWallet, masterSecretId);

            // Issuer create Credential Offer
            var credOffer = await AnonCreds.IssuerCreateCredentialOfferAsync(issuerWallet, credDefId);

            // Prover create Credential Request
            var createCredReqResult = await AnonCreds.ProverCreateCredentialReqAsync(proverWallet, proverDid, credOffer, credDef, masterSecretId);

            var credReq         = createCredReqResult.CredentialRequestJson;
            var credReqMetadata = createCredReqResult.CredentialRequestMetadataJson;

            // Issuer open TailsReader
            var blobStorageReader = await BlobStorage.OpenReaderAsync("default", tailsWriterConfig);

            // Issuer create Credential
            var createCredentialResult = await AnonCreds.IssuerCreateCredentialAsync(issuerWallet, credOffer, credReq, gvtCredentialValues, revRegId, blobStorageReader);

            var credential  = createCredentialResult.CredentialJson;
            var revRegDelta = createCredentialResult.RevocRegDeltaJson;
            var credRevId   = createCredentialResult.RevocId;

            // Prover store received Credential
            await AnonCreds.ProverStoreCredentialAsync(proverWallet, credentialId1, credReqMetadata, credential, credDef, revRegDef);

            // Prover gets Credentials for Proof Request
            var proofRequestJson = JsonConvert.SerializeObject(
                new
            {
                nonce   = "123432421212",
                name    = "proof_req_1",
                version = "0.1",
                requested_attributes = new
                {
                    attr1_referent = new { name = "name" },
                },
                requested_predicates = new
                {
                    predicate1_referent = new { name = "age", p_type = ">=", p_value = 18 },
                }
            }
                );

            var credentialsJson = await AnonCreds.ProverGetCredentialsForProofReqAsync(proverWallet, proofRequestJson);

            var credentials         = JObject.Parse(credentialsJson);
            var credentialsForAttr1 = (JArray)credentials["attrs"]["attr1_referent"];

            var credentialUuid = credentialsForAttr1[0]["cred_info"]["referent"];

            // Prover create RevocationState
            int timestamp    = 100;
            var revStateJson = await AnonCreds.CreateRevocationStateAsync(blobStorageReader, revRegDef, revRegDelta, timestamp, credRevId);


            // Prover create Proof
            var requestedCredentialsJson = JsonConvert.SerializeObject(
                new
            {
                self_attested_attributes = new { },
                requested_attributes     = new
                {
                    attr1_referent = new { cred_id = credentialUuid, revealed = true, timestamp = timestamp },
                },
                requested_predicates = new
                {
                    predicate1_referent = new { cred_id = credentialUuid, timestamp = timestamp },
                }
            }
                );

            var schemas        = new JObject(new JProperty(gvtSchemaId, JObject.Parse(schemaJson))).ToString();
            var credentialDefs = new JObject(new JProperty(credDefId, JObject.Parse(credDef))).ToString();
            var revStates      = new JObject(new JProperty(revRegId, new JObject(new JProperty(timestamp.ToString(), JObject.Parse(revStateJson))))).ToString();

            var proofJson = await AnonCreds.ProverCreateProofAsync(proverWallet, proofRequestJson, requestedCredentialsJson, masterSecretId, schemas,
                                                                   credentialDefs, revStates);

            var proof = JObject.Parse(proofJson);

            // Verifier verify proof
            var revealedAttr1 = proof["requested_proof"]["revealed_attrs"]["attr1_referent"];

            Assert.AreEqual("Alex", revealedAttr1["raw"]);

            var revRegDefs = new JObject(new JProperty(revRegId, JObject.Parse(revRegDef))).ToString();
            var revRegs    = new JObject(new JProperty(revRegId, new JObject(new JProperty(timestamp.ToString(), JObject.Parse(revRegDelta))))).ToString();

            var valid = await AnonCreds.VerifierVerifyProofAsync(proofRequestJson, proofJson, schemas, credentialDefs, revRegDefs, revRegs);

            Assert.IsTrue(valid);
        }
コード例 #22
0
        public async Task testAnoncredsRevocationInteractionIssuanceByDefault()
        {
            // Issuer create DID
            var trusteeDidInfo = await Did.CreateAndStoreMyDidAsync(this.wallet, JsonConvert.SerializeObject(new { seed = TRUSTEE_SEED }));

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

            var nymRequest = await Ledger.BuildNymRequestAsync(trusteeDidInfo.Did, issuerDidInfo.Did,
                                                               issuerDidInfo.VerKey, null, "TRUSTEE");

            await Ledger.SignAndSubmitRequestAsync(pool, wallet, trusteeDidInfo.Did, nymRequest);

            var issuerDid = issuerDidInfo.Did;

            // Prover create DID
            var proverDidInfo = await Did.CreateAndStoreMyDidAsync(proverWallet, "{}");

            var proverDid    = proverDidInfo.Did;
            var proverVerkey = proverDidInfo.VerKey;

            // Issuer publish Prover DID
            nymRequest = await Ledger.BuildNymRequestAsync(issuerDid, proverDid, proverVerkey, null, null);

            await Ledger.SignAndSubmitRequestAsync(pool, wallet, issuerDid, nymRequest);

            // ISSUER post to Ledger Schema, CredentialDefinition, RevocationRegistry

            // Issuer creates Schema
            var schemaInfo =
                await AnonCreds.IssuerCreateSchemaAsync(issuerDidInfo.Did, GVT_SCHEMA_NAME,
                                                        SCHEMA_VERSION, GVT_SCHEMA_ATTRIBUTES);

            var schemaJson = schemaInfo.SchemaJson;

            // Issuer posts Schema to Ledger
            var schemaRequest = await Ledger.BuildSchemaRequestAsync(issuerDid, schemaJson);

            await Ledger.SignAndSubmitRequestAsync(pool, wallet, issuerDid, schemaRequest);

            // Issuer get Schema from Ledger
            var getSchemaRequest = await Ledger.BuildGetSchemaRequestAsync(issuerDid, schemaInfo.SchemaId);

            var getSchemaResponse = await PoolUtils.EnsurePreviousRequestAppliedAsync(pool, getSchemaRequest, response => {
                var getSchemaResponseObject = JObject.Parse(response);
                return(getSchemaResponseObject["result"]["seqNo"] != null);
            });

            // !!IMPORTANT!!
            // It is important to get Schema from Ledger and parse it to get the correct schema JSON and correspondent id in Ledger
            // After that we can create CredentialDefinition for received Schema(not for result of indy_issuer_create_schema)

            ParseResponseResult schemaInfo1 = await Ledger.ParseGetSchemaResponseAsync(getSchemaResponse);

            schemaJson = schemaInfo1.ObjectJson;

            // Issuer creates CredentialDefinition

            var credDefInfo =
                await AnonCreds.IssuerCreateAndStoreCredentialDefAsync(wallet, issuerDid, schemaJson,
                                                                       TAG, null, JsonConvert.SerializeObject(new { support_revocation = true }));

            var credDefId   = credDefInfo.CredDefId;
            var credDefJson = credDefInfo.CredDefJson;

            // Issuer post CredentialDefinition to Ledger
            var credDefRequest = await Ledger.BuildCredDefRequestAsync(issuerDid, credDefJson);

            await Ledger.SignAndSubmitRequestAsync(pool, wallet, issuerDid, credDefRequest);

            // Issuer creates RevocationRegistry
            /* FIXME: getIndyHomePath hard coded forward slash "/". It will not work for Windows. */
            var tailsWriterConfig = JsonConvert.SerializeObject(
                new
            {
                base_dir    = EnvironmentUtils.GetIndyHomePath("tails"),
                uri_pattern = string.Empty
            }
                );

            var tailsWriterHandle = await BlobStorage.OpenWriterAsync("default", tailsWriterConfig);

            var revRegInfo =
                await AnonCreds.IssuerCreateAndStoreRevocRegAsync(wallet, issuerDid, null, TAG,
                                                                  credDefId,
                                                                  JsonConvert.SerializeObject(new { max_cred_num = 5, issuance_type = "ISSUANCE_BY_DEFAULT" }),
                                                                  tailsWriterHandle);

            var revRegId        = revRegInfo.RevRegId;
            var revRegDefJson   = revRegInfo.RevRegDefJson;
            var revRegEntryJson = revRegInfo.RevRegEntryJson;

            // Issuer posts RevocationRegistryDefinition to Ledger
            var revRegDefRequest = await Ledger.BuildRevocRegDefRequestAsync(issuerDid, revRegDefJson);

            await Ledger.SignAndSubmitRequestAsync(pool, wallet, issuerDid, revRegDefRequest);

            // Issuer posts RevocationRegistryEntry to Ledger
            var revRegEntryRequest = await Ledger.BuildRevocRegEntryRequestAsync(issuerDid, revRegId, REVOC_REG_TYPE, revRegEntryJson);

            await Ledger.SignAndSubmitRequestAsync(pool, wallet, issuerDid, revRegEntryRequest);

            // Issuance Credential for Prover

            // Prover creates Master Secret
            await AnonCreds.ProverCreateMasterSecretAsync(proverWallet, COMMON_MASTER_SECRET);

            // Issuer creates Credential Offer
            var credOfferJson = await AnonCreds.IssuerCreateCredentialOfferAsync(wallet, credDefId);

            // Prover gets CredentialDefinition from Ledger
            var getCredDefRequest = await Ledger.BuildGetCredDefRequestAsync(proverDid, credDefInfo.CredDefId);

            var getCredDefResponse = await Ledger.SubmitRequestAsync(pool, getCredDefRequest);

            ParseResponseResult credDefIdInfo = await Ledger.ParseGetCredDefResponseAsync(getCredDefResponse);

            credDefId   = credDefIdInfo.Id;
            credDefJson = credDefIdInfo.ObjectJson;

            // Prover creates Credential Request
            var credReqInfo =
                await AnonCreds.ProverCreateCredentialReqAsync(proverWallet, proverDid, credOfferJson,
                                                               credDefJson, COMMON_MASTER_SECRET);

            var credReqJson         = credReqInfo.CredentialRequestJson;
            var credReqMetadataJson = credReqInfo.CredentialRequestMetadataJson;

            // Issuer creates TailsReader
            BlobStorageReader blobStorageReader = await BlobStorage.OpenReaderAsync(TYPE, tailsWriterConfig);

            // Issuer creates Credential
            // Issuer must not post rev_reg_delta to ledger for ISSUANCE_BY_DEFAULT strategy

            var credRegInfo =
                await AnonCreds.IssuerCreateCredentialAsync(wallet, credOfferJson, credReqJson,
                                                            GVT_CRED_VALUES, revRegId,
                                                            blobStorageReader);

            var credJson  = credRegInfo.CredentialJson;
            var credRevId = credRegInfo.RevocId;

            // Prover gets RevocationRegistryDefinition
            var getRevRegDefRequest = await Ledger.BuildGetRevocRegDefRequestAsync(proverDid, revRegId);

            var getRevRegDefResponse = await Ledger.SubmitRequestAsync(pool, getRevRegDefRequest);

            ParseResponseResult revRegInfo1 = await Ledger.ParseGetRevocRegDefResponseAsync(getRevRegDefResponse);

            revRegId = revRegInfo1.Id;
            var revocRegDefJson = revRegInfo1.ObjectJson;

            // Prover store received Credential

            await AnonCreds.ProverStoreCredentialAsync(proverWallet, "credential1_id",
                                                       credReqMetadataJson, credJson, credDefJson,
                                                       revocRegDefJson);

            // Verifying Prover Credential
            Thread.Sleep(3000);

            long to = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds / 1000;

            var proofRequest = JsonConvert.SerializeObject(
                new
            {
                nonce   = "123432421212",
                name    = "proof_req_1",
                version = "0.1",
                requested_attributes = new
                {
                    attr1_referent = new
                    {
                        name = "name"
                    }
                },
                requested_predicates = new
                {
                    predicate1_referent = new
                    {
                        name    = "age",
                        p_type  = ">=",
                        p_value = 18
                    }
                },
                non_revoked = new
                {
                    to = to
                }
            }
                );

            // Prover gets Claims for Proof Request

            var credentialsJson = await AnonCreds.ProverGetCredentialsForProofReqAsync(proverWallet, proofRequest);

            var credentials      = JObject.Parse(credentialsJson);
            var credsForReferent = (JArray)credentials["attrs"]["attr1_referent"];
            var credential       = credsForReferent[0]["cred_info"];

            // Prover gets RevocationRegistryDelta from Ledger

            /* FIXME */
            var getRevRegDeltaRequest = await Ledger.BuildGetRevocRegDeltaRequestAsync(proverDid, revRegId, -1, (int)to);

            var getRevRegDeltaResponse = await Ledger.SubmitRequestAsync(pool, getRevRegDeltaRequest);

            var revRegInfo2 = await Ledger.ParseGetRevocRegDeltaResponseAsync(getRevRegDeltaResponse);

            revRegId = revRegInfo2.Id;
            var revocRegDeltaJson = revRegInfo2.ObjectJson;

            // Prover creates RevocationState
            var timestamp = to;

            var revStateJson = await AnonCreds.CreateRevocationStateAsync(blobStorageReader,
                                                                          revocRegDefJson, revocRegDeltaJson, (int)timestamp, credRevId);

            // Prover gets Schema from Ledger
            getSchemaRequest = await Ledger.BuildGetSchemaRequestAsync(proverDid, schemaInfo1.Id);

            getSchemaResponse = await Ledger.SubmitRequestAsync(pool, getSchemaRequest);

            ParseResponseResult schemaInfo2 = await Ledger.ParseGetSchemaResponseAsync(getSchemaResponse);

            var schemaId = schemaInfo2.Id;

            schemaJson = schemaInfo2.ObjectJson;

            // Prover creates Proof
            var requestedCredentialsJson = JsonConvert.SerializeObject(
                new
            {
                self_attested_attributes = new { },
                requested_attributes     = new
                {
                    attr1_referent = new
                    {
                        cred_id   = credential["referent"],
                        timestamp = timestamp,
                        revealed  = true
                    }
                },
                requested_predicates = new
                {
                    predicate1_referent = new
                    {
                        cred_id   = credential["referent"],
                        timestamp = timestamp
                    }
                }
            }
                );


            var schemasJson   = new JObject(new JProperty(schemaId, JObject.Parse(schemaJson))).ToString();
            var credDefsJson  = new JObject(new JProperty(credDefId, JObject.Parse(credDefJson))).ToString();
            var revStatesJson = new JObject(new JProperty(revRegId, new JObject(new JProperty(timestamp.ToString(), JObject.Parse(revStateJson))))).ToString();

            var proofJson = await AnonCreds.ProverCreateProofAsync(proverWallet, proofRequest,
                                                                   requestedCredentialsJson, COMMON_MASTER_SECRET,
                                                                   schemasJson, credDefsJson, revStatesJson);


            var proof = JObject.Parse(proofJson);

            // Verifier gets RevocationRegistry from Ledger

            var getRevRegReq = await Ledger.BuildGetRevocRegRequestAsync(DID_MY1, revRegId, (int)timestamp);

            var getRevRegResp = await Ledger.SubmitRequestAsync(pool, getRevRegReq);

            var revRegInfo3 = await Ledger.ParseGetRevocRegResponseAsync(getRevRegResp);

            revRegId = revRegInfo3.Id;
            var revRegJson = revRegInfo3.ObjectJson;

            // Verifier verifies proof
            Assert.AreNotEqual("Alex", proof["requested_proof"]["revealed_attrs"]["attr1_referent"].ToString());

            var revRegDefsJson = new JObject(new JProperty(revRegId, JObject.Parse(revRegDefJson))).ToString();
            var revRegsJson    = new JObject(new JProperty(revRegId, new JObject(new JProperty(timestamp.ToString(), JObject.Parse(revRegJson))))).ToString();

            var valid = await AnonCreds.VerifierVerifyProofAsync(proofRequest,
                                                                 proofJson,
                                                                 schemasJson,
                                                                 credDefsJson,
                                                                 revRegDefsJson,
                                                                 revRegsJson);

            Assert.IsTrue(valid);

            // Issuer revokes credential
            var revRegDeltaJson = await AnonCreds.IssuerRevokeCredentialAsync(wallet,
                                                                              blobStorageReader,
                                                                              revRegId, credRevId);

            // Issuer post RevocationRegistryDelta to Ledger
            revRegEntryRequest = await Ledger.BuildRevocRegEntryRequestAsync(issuerDid, revRegId, REVOC_REG_TYPE, revRegDeltaJson);

            await Ledger.SignAndSubmitRequestAsync(pool, wallet, issuerDid, revRegEntryRequest);

            // Verifying Prover Credential after Revocation
            Thread.Sleep(3000);

            long from = to;

            to = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds / 1000;

            // Prover gets RevocationRegistryDelta from Ledger
            getRevRegDeltaRequest = await Ledger.BuildGetRevocRegDeltaRequestAsync(proverDid, revRegId, (int)from, (int)to);

            getRevRegDeltaResponse = await Ledger.SubmitRequestAsync(pool, getRevRegDeltaRequest);

            var revRegInfo4 = await Ledger.ParseGetRevocRegDeltaResponseAsync(getRevRegDeltaResponse);

            revRegId          = revRegInfo4.Id;
            revocRegDeltaJson = revRegInfo4.ObjectJson;
            timestamp         = (long)revRegInfo4.Timestamp;

            // Prover creates RevocationState
            revStateJson = await AnonCreds.CreateRevocationStateAsync(blobStorageReader,
                                                                      revocRegDefJson, revocRegDeltaJson, (int)timestamp, credRevId);

            requestedCredentialsJson = JsonConvert.SerializeObject(
                new
            {
                self_attested_attributes = new { },
                requested_attributes     = new
                {
                    attr1_referent = new
                    {
                        cred_id   = credential["referent"],
                        timestamp = timestamp,
                        revealed  = true
                    }
                },
                requested_predicates = new
                {
                    predicate1_referent = new
                    {
                        cred_id   = credential["referent"],
                        timestamp = timestamp
                    }
                }
            }
                );

            revStatesJson = new JObject(new JProperty(revRegId, new JObject(new JProperty(timestamp.ToString(), JObject.Parse(revStateJson))))).ToString();

            proofJson = await AnonCreds.ProverCreateProofAsync(proverWallet,
                                                               proofRequest,
                                                               requestedCredentialsJson,
                                                               COMMON_MASTER_SECRET,
                                                               schemasJson,
                                                               credDefsJson,
                                                               revStatesJson);

            // Verifier gets RevocationRegistry from Ledger
            getRevRegReq = await Ledger.BuildGetRevocRegRequestAsync(DID_MY1, revRegId, (int)timestamp);

            getRevRegResp = await Ledger.SubmitRequestAsync(pool, getRevRegReq);

            var revRegInfo5 = await Ledger.ParseGetRevocRegResponseAsync(getRevRegResp);

            revRegId   = revRegInfo5.Id;
            revRegJson = revRegInfo5.ObjectJson;
            timestamp  = (long)revRegInfo5.Timestamp;

            revRegsJson = new JObject(new JProperty(revRegId, new JObject(new JProperty(timestamp.ToString(), JObject.Parse(revRegJson))))).ToString();

            valid = await AnonCreds.VerifierVerifyProofAsync(proofRequest,
                                                             proofJson,
                                                             schemasJson,
                                                             credDefsJson,
                                                             revRegDefsJson,
                                                             revRegsJson);

            Assert.IsFalse(valid);
        }
コード例 #23
0
        public async Task TestAnonCredsDemo()
        {
            //4. Issuer create ClaimDef
            var schemaJson = "{\n" +
                             "                    \"seqNo\":1,\n" +
                             "                    \"data\": {\n" +
                             "                        \"name\":\"gvt\",\n" +
                             "                        \"version\":\"1.0\",\n" +
                             "                        \"attr_names\":[\"age\",\"sex\",\"height\",\"name\"]\n" +
                             "                    }\n" +
                             "                }";
            var issuerDid = "NcYxiDXkpYi6ov5FcYDi1e";

            var claimDef = await AnonCreds.IssuerCreateAndStoreClaimDefAsync(_issuerWallet, issuerDid, schemaJson, null, false);

            Assert.IsNotNull(claimDef);

            //5. Prover create Master Secret
            var masterSecret = "masterSecretName";
            await AnonCreds.ProverCreateMasterSecretAsync(_proverWallet, masterSecret);

            //6. Prover store Claim Offer
            var claimOffer = string.Format("{{\"issuer_did\":\"{0}\", \"schema_seq_no\":{1}}}", issuerDid, 1);
            await AnonCreds.ProverStoreClaimOfferAsync(_proverWallet, claimOffer);

            //7. Prover get Claim Offers
            var claimOfferFilter = string.Format("{{\"issuer_did\":\"{0}\"}}", issuerDid);
            var claimOffersJson  = await AnonCreds.ProverGetClaimOffersAsync(_proverWallet, claimOfferFilter);

            var claimOffersObject = JArray.Parse(claimOffersJson);

            Assert.AreEqual(claimOffersObject.Count, 1);

            var claimOfferObject = (JObject)claimOffersObject[0];
            var claimOfferJson   = claimOfferObject.ToString();

            //8. Prover create ClaimReq
            var proverDid = "BzfFCYk";
            var claimReq  = await AnonCreds.ProverCreateAndStoreClaimReqAsync(_proverWallet, proverDid, claimOfferJson, claimDef, masterSecret);

            Assert.IsNotNull(claimReq);

            //9. Issuer create Claim
            var claimAttributesJson = "{\n" +
                                      "               \"sex\":[\"male\",\"5944657099558967239210949258394887428692050081607692519917050011144233115103\"],\n" +
                                      "               \"name\":[\"Alex\",\"1139481716457488690172217916278103335\"],\n" +
                                      "               \"height\":[\"175\",\"175\"],\n" +
                                      "               \"age\":[\"28\",\"28\"]\n" +
                                      "        }";

            var createClaimResult = await AnonCreds.IssuerCreateClaimAsync(_issuerWallet, claimReq, claimAttributesJson, -1);

            Assert.IsNotNull(createClaimResult);
            var claimJson = createClaimResult.ClaimJson;

            //10. Prover store Claim
            await AnonCreds.ProverStoreClaimAsync(_proverWallet, claimJson);

            //11. Prover gets Claims for Proof Request
            var proofRequestJson = "{\n" +
                                   "                          \"nonce\":\"123432421212\",\n" +
                                   "                          \"name\":\"proof_req_1\",\n" +
                                   "                          \"version\":\"0.1\",\n" +
                                   "                          \"requested_attrs\":{\"attr1_referent\":{\"name\":\"name\",\"restrictions\":[{\"schema_seq_no\":1}]},\n" +
                                   "                                                \"attr2_referent\":{\"name\":\"sex\",\"restrictions\":[{\"schema_seq_no\":1}]}},\n" +
                                   "                          \"requested_predicates\":{\"predicate1_referent\":{\"attr_name\":\"age\",\"p_type\":\">=\",\"value\":18}}\n" +
                                   "                  }";

            var claimsForProofJson = await AnonCreds.ProverGetClaimsForProofReqAsync(_proverWallet, proofRequestJson);

            Assert.IsNotNull(claimsForProofJson);

            var claimsForProof      = JObject.Parse(claimsForProofJson);
            var claimsForAttribute1 = (JArray)claimsForProof["attrs"]["attr1_referent"];
            var claimsForAttribute2 = (JArray)claimsForProof["attrs"]["attr1_referent"];
            var claimsForPredicate  = (JArray)claimsForProof["predicates"]["predicate1_referent"];

            Assert.AreEqual(claimsForAttribute1.Count, 1);
            Assert.AreEqual(claimsForAttribute2.Count, 1);
            Assert.AreEqual(claimsForPredicate.Count, 1);

            var claimUuid = claimsForAttribute1[0].Value <string>("referent");

            //12. Prover create Proof
            var selfAttestedValue   = "yes";
            var requestedClaimsJson = string.Format("{{\n" +
                                                    "                                          \"self_attested_attributes\":{{\"self1\":\"{0}\"}},\n" +
                                                    "                                          \"requested_attrs\":{{\"attr1_referent\":[\"{1}\", true],\n" +
                                                    "                                                               \"attr2_referent\":[\"{2}\", false]}},\n" +
                                                    "                                          \"requested_predicates\":{{\"predicate1_referent\":\"{3}\"}}\n" +
                                                    "                                        }}", selfAttestedValue, claimUuid, claimUuid, claimUuid);

            var schemasJson   = string.Format("{{\"{0}\":{1}}}", claimUuid, schemaJson);
            var claimDefsJson = string.Format("{{\"{0}\":{1}}}", claimUuid, claimDef);
            var revocRegsJson = "{}";


            var proofJson = await AnonCreds.ProverCreateProofAsync(_proverWallet, proofRequestJson, requestedClaimsJson, schemasJson,
                                                                   masterSecret, claimDefsJson, revocRegsJson);

            Assert.IsNotNull(proofJson);

            var proof = JObject.Parse(proofJson);

            //13. Verifier verify Proof
            Assert.AreEqual("Alex",
                            proof["requested_proof"]["revealed_attrs"]["attr1_referent"][1]);

            Assert.IsNotNull(proof["requested_proof"]["unrevealed_attrs"].Value <string>("attr2_referent"));

            Assert.AreEqual(selfAttestedValue, proof["requested_proof"]["self_attested_attrs"].Value <string>("self1"));

            Boolean valid = await AnonCreds.VerifierVerifyProofAsync(proofRequestJson, proofJson, schemasJson, claimDefsJson, revocRegsJson);

            Assert.IsTrue(valid);
        }
コード例 #24
0
        public async Task TestIssuerRevokeProofWorks()
        {
            // Create wallet, get wallet handle
            var walletConfig = JsonConvert.SerializeObject(new { id = "revocationWallet" });
            await Wallet.CreateWalletAsync(walletConfig, CREDENTIALS);

            var wallet = await Wallet.OpenWalletAsync(walletConfig, CREDENTIALS);

            // Issuer create Schema
            var createSchemaResult = await AnonCreds.IssuerCreateSchemaAsync(issuerDid, gvtSchemaName, schemaVersion, gvtSchemaAttributes);

            var schemaId   = createSchemaResult.SchemaId;
            var schemaJson = createSchemaResult.SchemaJson;

            // Issuer create issuer1GvtCredential definition
            var revocationCredentialDefConfig = "{\"support_revocation\":true}";
            var createCredentialDefResult     = await AnonCreds.IssuerCreateAndStoreCredentialDefAsync(wallet, issuerDid, schemaJson, tag, null, revocationCredentialDefConfig);

            var credDefId   = createCredentialDefResult.CredDefId;
            var credDefJson = createCredentialDefResult.CredDefJson;

            // Issuer create revocation registry
            var tailsWriter = await BlobStorage.OpenWriterAsync("default", tailsWriterConfig);

            var revRegConfig       = "{\"issuance_type\":null,\"max_cred_num\":5}";
            var createRevRegResult = await AnonCreds.IssuerCreateAndStoreRevocRegAsync(wallet, issuerDid, null, tag, credDefId, revRegConfig, tailsWriter);

            var revRegId  = createRevRegResult.RevRegId;
            var revRegDef = createRevRegResult.RevRegDefJson;

            // Prover create Master Secret
            await AnonCreds.ProverCreateMasterSecretAsync(wallet, masterSecretId);

            // Issuer create Credential Offer
            var credOfferJson = await AnonCreds.IssuerCreateCredentialOfferAsync(wallet, credDefId);

            // Prover create Credential Request
            var createCredReqResult =
                await AnonCreds.ProverCreateCredentialReqAsync(wallet, proverDid, credOfferJson, credDefJson, masterSecretId);

            var credentialReqJson         = createCredReqResult.CredentialRequestJson;
            var credentialReqMetadataJson = createCredReqResult.CredentialRequestMetadataJson;

            // Issuer open TailsReader
            var blobReader = await BlobStorage.OpenReaderAsync("default", tailsWriterConfig);

            //9. Issuer create Credential
            var createCredentialResult = await AnonCreds.IssuerCreateCredentialAsync(wallet, credOfferJson, credentialReqJson, gvtCredentialValuesJson, revRegId, blobReader);

            var credJson    = createCredentialResult.CredentialJson;
            var credRevocId = createCredentialResult.RevocId;
            var revRegDelta = createCredentialResult.RevocRegDeltaJson;

            // Prover create RevocationState
            var timestamp    = 100;
            var revStateJson = await AnonCreds.CreateRevocationStateAsync(blobReader, revRegDef, revRegDelta, timestamp, credRevocId);

            // Prover store received Credential
            await AnonCreds.ProverStoreCredentialAsync(wallet, credentialId1, credentialReqMetadataJson, credJson, credDefJson, revRegDef);

            // Prover gets Credentials for Proof Request
            var credentialsJson = await AnonCreds.ProverGetCredentialsForProofReqAsync(wallet, proofRequest);

            var credentials         = JObject.Parse(credentialsJson);
            var credentialsForAttr1 = credentials["attrs"]["attr1_referent"];

            var credentialUuid = credentialsForAttr1[0]["cred_info"]["referent"];

            // Prover create Proof
            var requestedCredentialsJson = string.Format("{{" +
                                                         "\"self_attested_attributes\":{{}}," +
                                                         "\"requested_attributes\":{{\"attr1_referent\":{{\"cred_id\":\"{0}\", \"revealed\":true, \"timestamp\":{1} }}}}," +
                                                         "\"requested_predicates\":{{\"predicate1_referent\":{{\"cred_id\":\"{2}\", \"timestamp\":{3}}}}}" +
                                                         "}}", credentialUuid, timestamp, credentialUuid, timestamp);

            var schemasJson        = JObject.Parse(string.Format("{{\"{0}\":{1}}}", schemaId, schemaJson)).ToString();
            var credentialDefsJson = JObject.Parse(string.Format("{{\"{0}\":{1}}}", credDefId, credDefJson)).ToString();
            var revStatesJson      = JObject.Parse(string.Format("{{\"{0}\":{{\"{1}\":{2}}}}}", revRegId, timestamp, revStateJson)).ToString();

            var proofJson = await AnonCreds.ProverCreateProofAsync(wallet, proofRequest, requestedCredentialsJson, masterSecretId, schemasJson, credentialDefsJson, revStatesJson);

            var proof = JObject.Parse(proofJson);

            // Issuer revoke Credential
            revRegDelta = await AnonCreds.IssuerRevokeCredentialAsync(wallet, blobReader, revRegId, credRevocId);

            // Verifier verify proof
            var revealedAttr1 = proof["requested_proof"]["revealed_attrs"]["attr1_referent"];

            Assert.AreEqual("Alex", revealedAttr1["raw"]);

            var revRegDefsJson = JObject.Parse(string.Format("{{\"{0}\":{1}}}", revRegId, revRegDef)).ToString();
            var revRegs        = JObject.Parse(string.Format("{{\"{0}\":{{\"{1}\":{2}}}}}", revRegId, timestamp, revRegDelta)).ToString();

            var valid = await AnonCreds.VerifierVerifyProofAsync(proofRequest, proofJson, schemasJson, credentialDefsJson, revRegDefsJson, revRegs);

            Assert.IsFalse(valid);

            //// Close and Delete Wallet
            await wallet.CloseAsync();

            await Wallet.DeleteWalletAsync(walletConfig, CREDENTIALS);
        }
コード例 #25
0
        public static async Task Execute()
        {
            var issuerWalletName = "issuerWallet";
            var proverWalletName = "proverWallet";

            try
            {
                //1. Create and Open Pool
                await PoolUtils.CreatePoolLedgerConfig();

                //2. Issuer Create and Open Wallet
                await WalletUtils.CreateWalleatAsync(PoolUtils.DEFAULT_POOL_NAME, issuerWalletName, "default", null, null);

                //3. Prover Create and Open Wallet
                await WalletUtils.CreateWalleatAsync(PoolUtils.DEFAULT_POOL_NAME, proverWalletName, "default", null, null);

                //4. Open pool and wallets in using statements to ensure they are closed when finished.
                using (var pool = await Pool.OpenPoolLedgerAsync(PoolUtils.DEFAULT_POOL_NAME, "{}"))
                    using (var issuerWallet = await Wallet.OpenWalletAsync(issuerWalletName, null, null))
                        using (var proverWallet = await Wallet.OpenWalletAsync(proverWalletName, null, null))
                        {
                            //5. Issuer create ClaimDef
                            var schemaJson = "{\n" +
                                             "   \"seqNo\":1,\n" +
                                             "   \"data\": {\n" +
                                             "       \"name\":\"gvt\",\n" +
                                             "       \"version\":\"1.0\",\n" +
                                             "       \"attr_names\":[\"age\",\"sex\",\"height\",\"name\"]\n" +
                                             "   }\n" +
                                             "}";
                            var issuerDid = "NcYxiDXkpYi6ov5FcYDi1e";

                            var claimDef = await AnonCreds.IssuerCreateAndStoreClaimDefAsync(issuerWallet, issuerDid, schemaJson, null, false);

                            //6. Prover create Master Secret
                            var masterSecret = "masterSecretName";
                            await AnonCreds.ProverCreateMasterSecretAsync(proverWallet, masterSecret);

                            //7. Prover store Claim Offer
                            var claimOffer = string.Format("{{\"issuer_did\":\"{0}\", \"schema_seq_no\":{1}}}", issuerDid, 1);
                            await AnonCreds.ProverStoreClaimOfferAsync(proverWallet, claimOffer);

                            //8. Prover get Claim Offers
                            var claimOfferFilter = string.Format("{{\"issuer_did\":\"{0}\"}}", issuerDid);
                            var claimOffersJson  = await AnonCreds.ProverGetClaimOffersAsync(proverWallet, claimOfferFilter);

                            var claimOffersObject = JArray.Parse(claimOffersJson);
                            Debug.Assert(claimOffersObject.Count == 1);

                            var claimOfferObject = (JObject)claimOffersObject[0];
                            var claimOfferJson   = claimOfferObject.ToString();

                            //9. Prover create ClaimReq
                            var proverDid = "BzfFCYk";
                            var claimReq  = await AnonCreds.ProverCreateAndStoreClaimReqAsync(proverWallet, proverDid, claimOfferJson, claimDef, masterSecret);

                            Debug.Assert(claimReq != null);

                            //10. Issuer create Claim
                            var claimAttributesJson = "{\n" +
                                                      "   \"sex\":[\"male\",\"5944657099558967239210949258394887428692050081607692519917050011144233115103\"],\n" +
                                                      "   \"name\":[\"Alex\",\"1139481716457488690172217916278103335\"],\n" +
                                                      "   \"height\":[\"175\",\"175\"],\n" +
                                                      "   \"age\":[\"28\",\"28\"]\n" +
                                                      "}";

                            var createClaimResult = await AnonCreds.IssuerCreateClaimAsync(issuerWallet, claimReq, claimAttributesJson, -1);

                            var claimJson = createClaimResult.ClaimJson;

                            //11. Prover store Claim
                            await AnonCreds.ProverStoreClaimAsync(proverWallet, claimJson);

                            //12. Prover gets Claims for Proof Request
                            var proofRequestJson = "{\n" +
                                                   "   \"nonce\":\"123432421212\",\n" +
                                                   "   \"name\":\"proof_req_1\",\n" +
                                                   "   \"version\":\"0.1\",\n" +
                                                   "   \"requested_attrs\":{\"attr1_uuid\":{\"schema_seq_no\":1,\"name\":\"name\"},\n" +
                                                   "       \"attr2_uuid\":{\"schema_seq_no\":1,\"name\":\"sex\"}},\n" +
                                                   "   \"requested_predicates\":{\"predicate1_uuid\":{\"attr_name\":\"age\",\"p_type\":\"GE\",\"value\":18}}\n" +
                                                   "   }";

                            var claimsForProofJson = await AnonCreds.ProverGetClaimsForProofReqAsync(proverWallet, proofRequestJson);

                            var claimsForProof      = JObject.Parse(claimsForProofJson);
                            var claimsForAttribute1 = (JArray)claimsForProof["attrs"]["attr1_uuid"];
                            var claimsForAttribute2 = (JArray)claimsForProof["attrs"]["attr1_uuid"];
                            var claimsForPredicate  = (JArray)claimsForProof["predicates"]["predicate1_uuid"];

                            Debug.Assert(claimsForAttribute1.Count == 1);
                            Debug.Assert(claimsForAttribute2.Count == 1);
                            Debug.Assert(claimsForPredicate.Count == 1);

                            var claimUuid = claimsForAttribute1[0].Value <string>("claim_uuid");

                            //13. Prover create Proof
                            var selfAttestedValue   = "yes";
                            var requestedClaimsJson = string.Format("{{\n" +
                                                                    "   \"self_attested_attributes\":{{\"self1\":\"{0}\"}},\n" +
                                                                    "   \"requested_attrs\":{{\"attr1_uuid\":[\"{1}\", true],\n" +
                                                                    "   \"attr2_uuid\":[\"{2}\", false]}},\n" +
                                                                    "   \"requested_predicates\":{{\"predicate1_uuid\":\"{3}\"}}\n" +
                                                                    "}}", selfAttestedValue, claimUuid, claimUuid, claimUuid);

                            var schemasJson   = string.Format("{{\"{0}\":{1}}}", claimUuid, schemaJson);
                            var claimDefsJson = string.Format("{{\"{0}\":{1}}}", claimUuid, claimDef);
                            var revocRegsJson = "{}";


                            var proofJson = await AnonCreds.ProverCreateProofAsync(proverWallet, proofRequestJson, requestedClaimsJson, schemasJson,
                                                                                   masterSecret, claimDefsJson, revocRegsJson);

                            var proof = JObject.Parse(proofJson);

                            //14. Verifier verify Proof
                            Debug.Assert(string.Equals("Alex", proof["requested_proof"]["revealed_attrs"]["attr1_uuid"][1].ToString()));
                            Debug.Assert(proof["requested_proof"]["unrevealed_attrs"].Value <string>("attr2_uuid") != null);
                            Debug.Assert(string.Equals(selfAttestedValue, proof["requested_proof"]["self_attested_attrs"].Value <string>("self1")));

                            var valid = await AnonCreds.VerifierVerifyProofAsync(proofRequestJson, proofJson, schemasJson, claimDefsJson, revocRegsJson);

                            Debug.Assert(valid == true);

                            //15. Close wallets and pool
                            await issuerWallet.CloseAsync();

                            await proverWallet.CloseAsync();

                            await pool.CloseAsync();
                        }
            }
            finally
            {
                //16. Delete wallets and Pool ledger config
                await WalletUtils.DeleteWalletAsync(issuerWalletName, null);

                await WalletUtils.DeleteWalletAsync(proverWalletName, null);

                await PoolUtils.DeletePoolLedgerConfigAsync(PoolUtils.DEFAULT_POOL_NAME);
            }

            Console.WriteLine("Anoncreds sample -> completed");
        }
コード例 #26
0
        public static async Task Execute()
        {
            Console.WriteLine("Anoncreds Revocation sample -> started");

            var issuerWalletName = "issuerWallet";
            var proverWalletName = "proverWallet";

            var issuerWalletCredentials = "{\"key\":\"issuer_wallet_key\"}";
            var proverWalletCredentials = "{\"key\":\"prover_wallet_key\"}";

            var issuerDid = "NcYxiDXkpYi6ov5FcYDi1e";
            var proverDid = "VsKV7grR1BUE29mG2Fm2kX";

            try
            {
                //1. Create and Open Pool
                await PoolUtils.CreatePoolLedgerConfig();

                //2. Issuer Create and Open Wallet
                await WalletUtils.CreateWalletAsync(PoolUtils.DEFAULT_POOL_NAME, issuerWalletName, "default", null, issuerWalletCredentials);

                //3. Prover Create and Open Wallet
                await WalletUtils.CreateWalletAsync(PoolUtils.DEFAULT_POOL_NAME, proverWalletName, "default", null, proverWalletCredentials);

                // Open pool and wallets in using statements to ensure they are closed when finished.
                using (var pool = await Pool.OpenPoolLedgerAsync(PoolUtils.DEFAULT_POOL_NAME, "{}"))
                    using (var issuerWallet = await Wallet.OpenWalletAsync(issuerWalletName, null, issuerWalletCredentials))
                        using (var proverWallet = await Wallet.OpenWalletAsync(proverWalletName, null, proverWalletCredentials))
                        {
                            //4. Issuer Creates Credential Schema
                            var schemaName         = "gvt";
                            var schemaVersion      = "1.0";
                            var schemaAttributes   = "[\"name\", \"age\", \"sex\", \"height\"]";
                            var createSchemaResult = await AnonCreds.IssuerCreateSchemaAsync(issuerDid, schemaName, schemaVersion, schemaAttributes);

                            var schemaId   = createSchemaResult.SchemaId;
                            var schemaJson = createSchemaResult.SchemaJson;

                            //5. Issuer create Credential Definition
                            var credDefTag          = "Tag1";
                            var credDefConfigJson   = "{\"support_revocation\":true}";
                            var createCredDefResult = await AnonCreds.IssuerCreateAndStoreCredentialDefAsync(issuerWallet, issuerDid, schemaJson, credDefTag, null, credDefConfigJson);

                            var credDefId   = createCredDefResult.CredDefId;
                            var credDefJson = createCredDefResult.CredDefJson;

                            //6. Issuer create Revocation Registry
                            var revRegDefConfig   = "{\"issuance_type\":\"ISSUANCE_ON_DEMAND\",\"max_cred_num\":5}";
                            var tailsWriterConfig = string.Format("{{\"base_dir\":\"{0}\", \"uri_pattern\":\"\"}}", EnvironmentUtils.GetIndyHomePath("tails")).Replace('\\', '/');
                            var tailsWriter       = await BlobStorage.OpenWriterAsync("default", tailsWriterConfig);

                            var revRegDefTag       = "Tag2";
                            var createRevRegResult = await AnonCreds.IssuerCreateAndStoreRevocRegAsync(issuerWallet, issuerDid, null, revRegDefTag, credDefId, revRegDefConfig, tailsWriter);

                            var revRegId      = createRevRegResult.RevRegId;
                            var revRegDefJson = createRevRegResult.RevRegDefJson;

                            //7. Prover create Master Secret
                            var masterSecretId = await AnonCreds.ProverCreateMasterSecretAsync(proverWallet, null);

                            //8. Issuer Creates Credential Offer
                            var credOffer = await AnonCreds.IssuerCreateCredentialOfferAsync(issuerWallet, credDefId);

                            //9. Prover Creates Credential Request
                            var createCredReqResult = await AnonCreds.ProverCreateCredentialReqAsync(proverWallet, proverDid, credOffer, credDefJson, masterSecretId);

                            var credReqJson         = createCredReqResult.CredentialRequestJson;
                            var credReqMetadataJson = createCredReqResult.CredentialRequestMetadataJson;

                            //10. Issuer open Tails Reader
                            var blobStorageReaderCfg = await BlobStorage.OpenReaderAsync("default", tailsWriterConfig);

                            //11. Issuer create Credential
                            var credValuesJson = "{\n" +
                                                 "        \"sex\": {\"raw\": \"male\", \"encoded\": \"594465709955896723921094925839488742869205008160769251991705001\"},\n" +
                                                 "        \"name\": {\"raw\": \"Alex\", \"encoded\": \"1139481716457488690172217916278103335\"},\n" +
                                                 "        \"height\": {\"raw\": \"175\", \"encoded\": \"175\"},\n" +
                                                 "        \"age\": {\"raw\": \"28\", \"encoded\": \"28\"}\n" +
                                                 "    }";

                            var createCredentialResult = await AnonCreds.IssuerCreateCredentialAsync(issuerWallet, credOffer, credReqJson, credValuesJson, revRegId, blobStorageReaderCfg);

                            var credential      = createCredentialResult.CredentialJson;
                            var revRegDeltaJson = createCredentialResult.RevocRegDeltaJson;
                            var credRevId       = createCredentialResult.RevocId;

                            //12. Prover Stores Credential
                            await AnonCreds.ProverStoreCredentialAsync(proverWallet, null, credReqMetadataJson, credential, credDefJson, revRegDefJson);

                            //13. Prover Gets Credentials for Proof Request
                            var proofRequestJson = "{\n" +
                                                   "                   \"nonce\":\"123432421212\",\n" +
                                                   "                   \"name\":\"proof_req_1\",\n" +
                                                   "                   \"version\":\"0.1\", " +
                                                   "                   \"requested_attributes\":{" +
                                                   "                          \"attr1_referent\":{\"name\":\"name\"}" +
                                                   "                    },\n" +
                                                   "                    \"requested_predicates\":{" +
                                                   "                          \"predicate1_referent\":{\"name\":\"age\",\"p_type\":\">=\",\"p_value\":18}" +
                                                   "                    }" +
                                                   "               }";

                            var credentialsForProofJson = await AnonCreds.ProverGetCredentialsForProofReqAsync(proverWallet, proofRequestJson);

                            var credentials              = JObject.Parse(credentialsForProofJson);
                            var credentialsForAttr1      = (JArray)credentials["attrs"]["attr1_referent"];
                            var credentialsForPredicate1 = (JArray)credentials["predicates"]["predicate1_referent"];

                            var credIdForAttr1 = credentialsForAttr1[0]["cred_info"]["referent"].ToObject <string>();
                            var credIdForPred1 = credentialsForPredicate1[0]["cred_info"]["referent"].ToObject <string>();

                            //14. Prover create RevocationState
                            long timestamp    = 100;
                            var  revStateJson = await AnonCreds.CreateRevocationStateAsync(blobStorageReaderCfg, revRegDefJson, revRegDeltaJson, timestamp, credRevId);

                            //15. Prover Creates Proof
                            var requestedCredentialsJson = string.Format("{{" +
                                                                         "\"self_attested_attributes\":{{}}," +
                                                                         "\"requested_attributes\":{{\"attr1_referent\":{{\"cred_id\":\"{0}\", \"revealed\":true, \"timestamp\":{1} }}}}," +
                                                                         "\"requested_predicates\":{{\"predicate1_referent\":{{\"cred_id\":\"{2}\", \"timestamp\":{3}}}}}" +
                                                                         "}}", credIdForAttr1, timestamp, credIdForPred1, timestamp);

                            var schemas        = string.Format("{{\"{0}\":{1}}}", schemaId, schemaJson);
                            var credentialDefs = string.Format("{{\"{0}\":{1}}}", credDefId, credDefJson);
                            var revStates      = string.Format("{{\"{0}\": {{ \"{1}\":{2} }}}}", revRegId, timestamp, revStateJson);

                            var proofJson = await AnonCreds.ProverCreateProofAsync(proverWallet, proofRequestJson, requestedCredentialsJson, masterSecretId, schemas, credentialDefs, revStates);

                            var proof = JObject.Parse(proofJson);

                            //16. Verifier verify Proof
                            var revealedAttr1 = proof["requested_proof"]["revealed_attrs"]["attr1_referent"];
                            Debug.Assert("Alex" == revealedAttr1["raw"].ToObject <string>());

                            var revRegDefs = string.Format("{{\"{0}\":{1}}}", revRegId, revRegDefJson);
                            var revRegs    = string.Format("{{\"{0}\": {{ \"{1}\":{2} }}}}", revRegId, timestamp, revRegDeltaJson);

                            var valid = await AnonCreds.VerifierVerifyProofAsync(proofRequestJson, proofJson, schemas, credentialDefs, revRegDefs, revRegs);

                            Debug.Assert(valid);

                            await issuerWallet.CloseAsync();

                            await proverWallet.CloseAsync();

                            await pool.CloseAsync();
                        }
            }
            finally
            {
                //17. Delete wallets and Pool ledger config
                await WalletUtils.DeleteWalletAsync(issuerWalletName, issuerWalletCredentials);

                await WalletUtils.DeleteWalletAsync(proverWalletName, proverWalletCredentials);

                await PoolUtils.DeletePoolLedgerConfigAsync(PoolUtils.DEFAULT_POOL_NAME);
            }

            Console.WriteLine("Anoncreds Revocation sample -> completed");
        }
コード例 #27
0
        public async Task TestAnonCredsWorksForSingleIssuerSingleProverMultipleCredentials()
        {
            // Issuer create GVT Schema
            var createSchemaResult = await AnonCreds.IssuerCreateSchemaAsync(issuerDid, GVT_SCHEMA_NAME, SCHEMA_VERSION, GVT_SCHEMA_ATTRIBUTES);

            var gvtSchemaId = createSchemaResult.SchemaId;
            var gvtSchema   = createSchemaResult.SchemaJson;

            // Issuer create CredentialDef
            var createCredDefResult = await AnonCreds.IssuerCreateAndStoreCredentialDefAsync(issuerWallet, issuerDid, gvtSchema, TAG, null, DEFAULT_CRED_DEF_CONFIG);

            var gvtCredDefId = createCredDefResult.CredDefId;
            var gvtCredDef   = createCredDefResult.CredDefJson;

            // Issuer create XYZ Schema
            createSchemaResult = await AnonCreds.IssuerCreateSchemaAsync(issuerDid, XYZ_SCHEMA_NAME, SCHEMA_VERSION, XYZ_SCHEMA_ATTRIBUTES);

            var xyzSchemaId = createSchemaResult.SchemaId;
            var xyzSchema   = createSchemaResult.SchemaJson;

            // Issuer create CredentialDef
            createCredDefResult = await AnonCreds.IssuerCreateAndStoreCredentialDefAsync(issuerWallet, issuerDid, xyzSchema, TAG, null, DEFAULT_CRED_DEF_CONFIG);

            var xyzCredDefId = createCredDefResult.CredDefId;
            var xyzCredDef   = createCredDefResult.CredDefJson;

            // Prover create Master Secret
            await AnonCreds.ProverCreateMasterSecretAsync(proverWallet, masterSecretId);

            // Issuer create GVT Credential Offer
            var gvtCredOffer = await AnonCreds.IssuerCreateCredentialOfferAsync(issuerWallet, gvtCredDefId);

            // Issuer create XYZ Credential Offer
            var xyzCredOffer = await AnonCreds.IssuerCreateCredentialOfferAsync(issuerWallet, xyzCredDefId);

            // Prover create CredentialReq for GVT Credential Offer
            var createCredReqResult = await AnonCreds.ProverCreateCredentialReqAsync(proverWallet, proverDid, gvtCredOffer, gvtCredDef, masterSecretId);

            var gvtCredReq         = createCredReqResult.CredentialRequestJson;
            var gvtCredReqMetadata = createCredReqResult.CredentialRequestMetadataJson;

            // Issuer create GVT Credential
            var gvtCreateCredentialResult =
                await AnonCreds.IssuerCreateCredentialAsync(issuerWallet, gvtCredOffer, gvtCredReq, gvtCredentialValues, null, null);

            var gvtCredential = gvtCreateCredentialResult.CredentialJson;

            // Prover store GVT Credential
            await AnonCreds.ProverStoreCredentialAsync(proverWallet, credentialId1, gvtCredReqMetadata, gvtCredential, gvtCredDef, null);

            // Prover create CredentialReq for XYZ Credential Offer
            createCredReqResult = await AnonCreds.ProverCreateCredentialReqAsync(proverWallet, proverDid, xyzCredOffer, xyzCredDef, masterSecretId);

            var xyzCredReq         = createCredReqResult.CredentialRequestJson;
            var xyzCredReqMetadata = createCredReqResult.CredentialRequestMetadataJson;

            // Issuer create XYZ Credential
            var xyzCreateCredentialResult =
                await AnonCreds.IssuerCreateCredentialAsync(issuerWallet, xyzCredOffer, xyzCredReq, xyzCredentialValues, null, null);

            var xyzCredential = xyzCreateCredentialResult.CredentialJson;

            // Prover store XYZ Credential
            await AnonCreds.ProverStoreCredentialAsync(proverWallet, credentialId2, xyzCredReqMetadata, xyzCredential, xyzCredDef, null);

            // Prover gets Credentials for Proof Request
            var proofRequestJson = JsonConvert.SerializeObject(
                new
            {
                nonce   = "123432421212",
                name    = "proof_req_1",
                version = "0.1",
                requested_attributes = new
                {
                    attr1_referent = new { name = "name" },
                    attr2_referent = new { name = "status" }
                },
                requested_predicates = new
                {
                    predicate1_referent = new { name = "age", p_type = ">=", p_value = 18 },
                    predicate2_referent = new { name = "period", p_type = ">=", p_value = 5 },
                }
            }
                );

            var credentialsForProofJson = await AnonCreds.ProverGetCredentialsForProofReqAsync(proverWallet, proofRequestJson);

            Assert.IsNotNull(credentialsForProofJson);

            var credentialsForProof      = JObject.Parse(credentialsForProofJson);
            var credentialsForAttribute1 = (JArray)credentialsForProof["attrs"]["attr1_referent"];
            var credentialsForAttribute2 = (JArray)credentialsForProof["attrs"]["attr2_referent"];
            var credentialsForPredicate1 = (JArray)credentialsForProof["predicates"]["predicate1_referent"];
            var credentialsForPredicate2 = (JArray)credentialsForProof["predicates"]["predicate2_referent"];

            Assert.AreEqual(1, credentialsForAttribute1.Count);
            Assert.AreEqual(1, credentialsForAttribute2.Count);
            Assert.AreEqual(1, credentialsForPredicate1.Count);
            Assert.AreEqual(1, credentialsForPredicate2.Count);

            var credentialUuidForAttr1      = credentialsForAttribute1[0]["cred_info"]["referent"];
            var credentialUuidForAttr2      = credentialsForAttribute2[0]["cred_info"]["referent"];
            var credentialUuidForPredicate1 = credentialsForPredicate1[0]["cred_info"]["referent"];
            var credentialUuidForPredicate2 = credentialsForPredicate2[0]["cred_info"]["referent"];

            // Prover create Proof
            var requestedCredentialsJson = JsonConvert.SerializeObject(
                new
            {
                self_attested_attributes = new { },
                requested_attributes     = new
                {
                    attr1_referent = new { cred_id = credentialUuidForAttr1, revealed = true },
                    attr2_referent = new { cred_id = credentialUuidForAttr2, revealed = true }
                },
                requested_predicates = new
                {
                    predicate1_referent = new { cred_id = credentialUuidForPredicate1 },
                    predicate2_referent = new { cred_id = credentialUuidForPredicate2 }
                }
            }
                );

            var schemas        = new JObject(new JProperty(gvtSchemaId, JObject.Parse(gvtSchema)), new JProperty(xyzSchemaId, JObject.Parse(xyzSchema))).ToString();
            var credentialDefs = new JObject(new JProperty(gvtCredDefId, JObject.Parse(gvtCredDef)), new JProperty(xyzCredDefId, JObject.Parse(xyzCredDef))).ToString();
            var revocStates    = "{}";

            var proofJson = await AnonCreds.ProverCreateProofAsync(proverWallet, proofRequestJson, requestedCredentialsJson,
                                                                   masterSecretId, schemas, credentialDefs, revocStates);

            var proof = JObject.Parse(proofJson);

            // Verifier verify Proof
            var revealedAttr1 = proof["requested_proof"]["revealed_attrs"]["attr1_referent"];

            Assert.AreEqual("Alex", revealedAttr1["raw"]);

            var revealedAttr2 = proof["requested_proof"]["revealed_attrs"]["attr2_referent"];

            Assert.AreEqual("partial", revealedAttr2["raw"]);

            var revocRegDefs = "{}";
            var revocRegs    = "{}";

            Boolean valid = await AnonCreds.VerifierVerifyProofAsync(proofRequestJson, proofJson, schemas, credentialDefs, revocRegDefs, revocRegs);

            Assert.IsTrue(valid);
        }