public async Task TestIssuerCreateAndStoreClaimDefWorksForInvalidCryptoType()
        {
            var ex = await Assert.ThrowsExceptionAsync <IndyException>(() =>
                                                                       AnonCreds.IssuerCreateAndStoreClaimDefAsync(_wallet, _issuerDid, _gvtSchemaJson, "type", false)
                                                                       );

            Assert.AreEqual(ErrorCode.CommonInvalidStructure, ex.ErrorCode);
        }
        public async Task TestIssuerCreateAndStoreClaimDefWorksForInvalidSchemaJson()
        {
            var schema = "{\"seqNo\":1, \"name\":\"name\",\"version\":\"1.0\", \"attr_names\":[\"name\"]}";

            var ex = await Assert.ThrowsExceptionAsync <InvalidStructureException>(() =>
                                                                                   AnonCreds.IssuerCreateAndStoreClaimDefAsync(_wallet, _issuerDid, schema, null, false)
                                                                                   );
        }
        public async Task <ClaimDefinition> CreateClaimDefinitionAsync(string userId, Guid credentialSchemaId)
        {
            var schema = await _dbContext.CredentialSchemas.Include(p => p.User).FirstOrDefaultAsync(p => p.Id == credentialSchemaId);

            var schemaCreatorWalletData = await GetDefaultWalletDataAsync(schema.UserId);

            var walletData = await GetDefaultWalletDataAsync(userId);

            var wallet = await Wallet.OpenWalletAsync(walletData.Name, null, null);

            try
            {
                using (var pool = await PoolUtils.CreateAndOpenPoolLedgerAsync())
                {
                    var schemaData = JsonConvert.SerializeObject(new
                    {
                        name    = schema.Name,
                        version = schema.Version
                    });

                    var getSchemaRequest = await Ledger.BuildGetSchemaRequestAsync(walletData.Did, schemaCreatorWalletData.Did, schemaData);

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

                    var schemaValue  = JObject.Parse(getSchemaResponse).GetValue("result");
                    var claimDefJson = await AnonCreds.IssuerCreateAndStoreClaimDefAsync(wallet, walletData.Did, schemaValue.ToString(), "CL", false);

                    var claimDef        = JObject.Parse(claimDefJson);
                    var claimDefRequest = await Ledger.BuildClaimDefTxnAsync(walletData.Did, claimDef.Value <int>("ref"), claimDef.Value <string>("signature_type"), claimDef.GetValue("data").ToString());

                    var result = await Ledger.SignAndSubmitRequestAsync(pool, wallet, walletData.Did, claimDefRequest);

                    var claimDefinition = new ClaimDefinition
                    {
                        Id = Guid.NewGuid(),
                        CredentialSchemaId = credentialSchemaId,
                        UserId             = userId
                    };

                    _dbContext.ClaimDefinitions.Add(claimDefinition);
                    await _dbContext.SaveChangesAsync();

                    return(claimDefinition);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                await wallet.CloseAsync();
            }
        }
예제 #4
0
        public async Task TestExerciseCustomWallet()
        {
            StorageUtils.CleanupStorage();

            var walletName = "exerciseWalletTypeWorks";

            await Wallet.CreateWalletAsync("default", walletName, "inmem", null, null);

            var wallet = await Wallet.OpenWalletAsync(walletName, null, null);

            Assert.IsNotNull(wallet);

            var schema   = "{\"seqNo\":1,\"data\": {\"name\":\"gvt\",\"version\":\"1.0\",\"keys\":[\"age\",\"sex\",\"height\",\"name\"]}}";
            var claimDef = await AnonCreds.IssuerCreateAndStoreClaimDefAsync(wallet, DID1, schema, null, false);

            var claimOfferTemplate = "{{\"issuer_did\":\"{0}\",\"schema_seq_no\":{1}}}";

            await AnonCreds.ProverStoreClaimOfferAsync(wallet, string.Format(claimOfferTemplate, DID1, 1));

            await AnonCreds.ProverStoreClaimOfferAsync(wallet, string.Format(claimOfferTemplate, DID1, 2));

            var issuerDid2 = "CnEDk9HrMnmiHXEV1WFgbVCRteYnPqsJwrTdcZaNhFVW";
            await AnonCreds.ProverStoreClaimOfferAsync(wallet, string.Format(claimOfferTemplate, issuerDid2, 2));

            string masterSecretName = "master_secret_name";
            await AnonCreds.ProverCreateMasterSecretAsync(wallet, masterSecretName);

            var claimOffer = string.Format("{{\"issuer_did\":\"{0}\",\"schema_seq_no\":{1}}}", DID1, 1);

            var claimRequest = await AnonCreds.ProverCreateAndStoreClaimReqAsync(wallet, "CnEDk9HrMnmiHXEV1WFgbVCRteYnPqsJwrTdcZaNhFVW", claimOffer, claimDef, masterSecretName);

            var claim = "{\"sex\":[\"male\",\"5944657099558967239210949258394887428692050081607692519917050011144233115103\"],\n" +
                        "                 \"name\":[\"Alex\",\"1139481716457488690172217916278103335\"],\n" +
                        "                 \"height\":[\"175\",\"175\"],\n" +
                        "                 \"age\":[\"28\",\"28\"]\n" +
                        "        }";

            var createClaimResult = await AnonCreds.IssuerCreateClaimAsync(wallet, claimRequest, claim, -1);

            var claimJson = createClaimResult.ClaimJson;

            await AnonCreds.ProverStoreClaimAsync(wallet, claimJson);

            var filter = string.Format("{{\"issuer_did\":\"{0}\"}}", DID1);

            var claims = await AnonCreds.ProverGetClaimsAsync(wallet, filter);

            var claimsArray = JArray.Parse(claims);

            Assert.AreEqual(1, claimsArray.Count);

            await wallet.CloseAsync();
        }
        public async Task TestIssuerCreateAndStoreClaimDefWorksForEmptyKeys()
        {
            var schema = "{\n" +
                         "                    \"seqNo\":1,\n" +
                         "                    \"data\": {\n" +
                         "                        \"name\":\"gvt\",\n" +
                         "                        \"version\":\"1.0\",\n" +
                         "                        \"attr_names\":[]\n" +
                         "                    }\n" +
                         "                 }";

            var ex = await Assert.ThrowsExceptionAsync <InvalidStructureException>(() =>
                                                                                   AnonCreds.IssuerCreateAndStoreClaimDefAsync(_wallet, _issuerDid, schema, null, false)
                                                                                   );
        }
        public async Task TestIssuerCreateAndStoreClaimDefWorksForCorrectCryptoType()
        {
            var claimDef = await AnonCreds.IssuerCreateAndStoreClaimDefAsync(_wallet, _issuerDid, _gvtSchemaJson, "CL", false);

            Assert.IsNotNull(claimDef);

            var claimDefObject = JObject.Parse(claimDef);
            var primary        = claimDefObject["data"]["primary"];

            Assert.AreEqual(((JObject)primary["r"]).Count, 4);
            Assert.IsTrue(primary.Value <string>("n").Length > 0);
            Assert.IsTrue(primary.Value <string>("s").Length > 0);
            Assert.IsTrue(primary.Value <string>("z").Length > 0);
            Assert.IsTrue(primary.Value <string>("rms").Length > 0);
            Assert.IsTrue(primary.Value <string>("rctxt").Length > 0);
        }
예제 #7
0
        public async Task Before()
        {
            StorageUtils.CleanupStorage();

            //1. Create Issuer wallet, get wallet handle
            await Wallet.CreateWalletAsync("default", _walletName, "default", null, null);

            _issuerWallet = await Wallet.OpenWalletAsync(_walletName, null, null);

            //2. Issuer create claim definition
            _claimDefJson = await AnonCreds.IssuerCreateAndStoreClaimDefAsync(_issuerWallet, issuerDid, schema, null, true);

            //3. Issuer create revocation registry
            await AnonCreds.IssuerCreateAndStoreRevocRegAsync(_issuerWallet, issuerDid, 1, 5);

            //4. Prover create Master Secret
            await AnonCreds.ProverCreateMasterSecretAsync(_issuerWallet, masterSecretName);

            //5. Prover store Claim Offer received from Issuer
            var claimOfferJson = string.Format(claimOfferTemplate, issuerDid, 1);
            await AnonCreds.ProverStoreCredentialOfferAsync(_issuerWallet, claimOfferJson);

            //6. Prover create Claim Request
            var proverDid = "BzfFCYk";
            var claimReq  = await AnonCreds.ProverCreateCredentialReqAsync(
                _issuerWallet,
                proverDid,
                claimOfferJson,
                _claimDefJson,
                masterSecretName);

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


            _claimResult = await AnonCreds.IssuerCreateCredentialAsync(_issuerWallet, claimReq, claimJson, _userRevocIndex);

            //8. Prover store received Claim
            await AnonCreds.ProverStoreClaimAsync(_issuerWallet, _claimResult.CredentialJson, _claimResult.RevocRegDeltaJson);
        }
        protected async Task InitCommonWallet()
        {
            if (_walletOpened)
            {
                return;
            }

            StorageUtils.CleanupStorage();

            var walletName = "anoncredsCommonWallet";

            await Wallet.CreateWalletAsync("default", walletName, "default", null, null);

            commonWallet = await Wallet.OpenWalletAsync(walletName, null, null);

            claimDef = await AnonCreds.IssuerCreateAndStoreClaimDefAsync(commonWallet, issuerDid, schema, null, false);

            await AnonCreds.ProverStoreClaimOfferAsync(commonWallet, string.Format(claimOfferTemplate, issuerDid, 1));

            await AnonCreds.ProverStoreClaimOfferAsync(commonWallet, string.Format(claimOfferTemplate, issuerDid, 2));

            await AnonCreds.ProverStoreClaimOfferAsync(commonWallet, string.Format(claimOfferTemplate, issuerDid2, 2));

            await AnonCreds.ProverCreateMasterSecretAsync(commonWallet, masterSecretName);

            var claimOffer = string.Format("{{\"issuer_did\":\"{0}\",\"schema_seq_no\":{1}}}", issuerDid, 1);

            var claimRequest = await AnonCreds.ProverCreateAndStoreClaimReqAsync(commonWallet, "CnEDk9HrMnmiHXEV1WFgbVCRteYnPqsJwrTdcZaNhFVW", claimOffer, claimDef, masterSecretName);

            var claim = "{\"sex\":[\"male\",\"5944657099558967239210949258394887428692050081607692519917050011144233115103\"],\n" +
                        "                 \"name\":[\"Alex\",\"1139481716457488690172217916278103335\"],\n" +
                        "                 \"height\":[\"175\",\"175\"],\n" +
                        "                 \"age\":[\"28\",\"28\"]\n" +
                        "        }";

            var createClaimResult = await AnonCreds.IssuerCreateClaimAsync(commonWallet, claimRequest, claim, -1);

            var claimJson = createClaimResult.ClaimJson;

            await AnonCreds.ProverStoreClaimAsync(commonWallet, claimJson, createClaimResult.RevocRegUpdateJson);

            _walletOpened = true;
        }
        public async Task TestExerciseCustomWallet()
        {
            var walletName = "exerciseWalletTypeWorks";

            await Wallet.CreateWalletAsync("default", walletName, "inmem", null, null);

            var wallet = await Wallet.OpenWalletAsync(walletName, null, null);

            Assert.IsNotNull(wallet);

            var claimDef = await AnonCreds.IssuerCreateAndStoreClaimDefAsync(wallet, _issuerDid, _schema, null, false);

            await AnonCreds.ProverStoreClaimOfferAsync(wallet, string.Format(_claimOfferTemplate, _issuerDid, 1));

            await AnonCreds.ProverStoreClaimOfferAsync(wallet, string.Format(_claimOfferTemplate, _issuerDid, 2));

            await AnonCreds.ProverStoreClaimOfferAsync(wallet, string.Format(_claimOfferTemplate, _issuerDid2, 2));

            await AnonCreds.ProverCreateMasterSecretAsync(wallet, _masterSecretName);

            var claimOffer = string.Format("{{\"issuer_did\":\"{0}\",\"schema_seq_no\":{1}}}", _issuerDid, 1);

            var claimRequest = await AnonCreds.ProverCreateAndStoreClaimReqAsync(wallet, "CnEDk9HrMnmiHXEV1WFgbVCRteYnPqsJwrTdcZaNhFVW", claimOffer, claimDef, _masterSecretName);

            var claim = "{\"sex\":[\"male\",\"5944657099558967239210949258394887428692050081607692519917050011144233115103\"],\n" +
                        "                 \"name\":[\"Alex\",\"1139481716457488690172217916278103335\"],\n" +
                        "                 \"height\":[\"175\",\"175\"],\n" +
                        "                 \"age\":[\"28\",\"28\"]\n" +
                        "        }";

            var createClaimResult = await AnonCreds.IssuerCreateClaimAsync(wallet, claimRequest, claim, -1);

            var claimJson = createClaimResult.ClaimJson;

            await AnonCreds.ProverStoreClaimAsync(wallet, claimJson);

            var filter = string.Format("{{\"issuer_did\":\"{0}\"}}", _issuerDid);

            var claimOffers = await AnonCreds.ProverGetClaimOffersAsync(wallet, filter);

            await wallet.CloseAsync();
        }
예제 #10
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);
        }
예제 #11
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);
        }
예제 #12
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);
        }
예제 #13
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");
        }