public async Task ApplyClaimDefinitionAsync(string userId, Guid claimDefinitionId)
        {
            var claimDefinition = await _dbContext.ClaimDefinitions.Include(p => p.CredentialSchema).FirstOrDefaultAsync(p => p.Id == claimDefinitionId);

            var issuerWalletData = await GetDefaultWalletDataAsync(claimDefinition.UserId);

            var issuerWallet = await Wallet.OpenWalletAsync(issuerWalletData.Name, null, null);

            var userWalletData = await GetDefaultWalletDataAsync(userId);

            var userWallet = await Wallet.OpenWalletAsync(userWalletData.Name, null, null);

            var schemaCreatorWalletData = await GetDefaultWalletDataAsync(claimDefinition.CredentialSchema.UserId);


            try
            {
                using (var pool = await PoolUtils.CreateAndOpenPoolLedgerAsync())
                {
                    var userIssuerDidResult = await Did.CreateAndStoreMyDidAsync(userWallet, "{}");

                    var schema = await GetSchemaAsync(pool, issuerWalletData.Did, schemaCreatorWalletData.Did, claimDefinition.CredentialSchema.Name, claimDefinition.CredentialSchema.Version);

                    var transcriptClaimOfferJson = await AnonCreds.IssuerCreateClaimOfferAsync(issuerWallet, schema.GetValue("result").ToString(), issuerWalletData.Did, userIssuerDidResult.Did);

                    await AnonCreds.ProverStoreClaimOfferAsync(userWallet, transcriptClaimOfferJson);

                    var userMasterSecretName = RandomUtils.RandomString(8);
                    await AnonCreds.ProverCreateMasterSecretAsync(userWallet, userMasterSecretName);

                    var getClaimDefRequest = await Ledger.BuildGetClaimDefTxnAsync(userIssuerDidResult.Did, schema.GetValue("result").Value <int>("seqNo"), "CL", issuerWalletData.Did);

                    var getClaimDefResponse = await Ledger.SubmitRequestAsync(pool, getClaimDefRequest);

                    var transcriptClaimDef = JObject.Parse(getClaimDefResponse).GetValue("result");

                    var transcriptClaimRequestJson = await AnonCreds.ProverCreateAndStoreClaimReqAsync(userWallet, userIssuerDidResult.Did, transcriptClaimOfferJson, transcriptClaimDef.ToString(), userMasterSecretName);

                    var userIndyClaim = new UserIndyClaim
                    {
                        ClaimDefinitionId = claimDefinitionId,
                        ClaimRequest      = transcriptClaimRequestJson,
                        Id          = Guid.NewGuid(),
                        LastUpdated = DateTime.UtcNow,
                        Status      = UserIndyClaimStatus.Requested,
                        TimeCreated = DateTime.UtcNow,
                        UserId      = userId
                    };

                    _dbContext.UserIndyClaims.Add(userIndyClaim);
                    await _dbContext.SaveChangesAsync();
                }
            }
            finally
            {
                await userWallet.CloseAsync();

                await issuerWallet.CloseAsync();
            }
        }
        public async Task TestBuildGetClaimDefRequestWorks()
        {
            var expectedResult = string.Format("\"identifier\":\"{0}\"," +
                                               "\"operation\":{{" +
                                               "\"type\":\"108\"," +
                                               "\"ref\":{1}," +
                                               "\"signature_type\":\"{2}\"," +
                                               "\"origin\":\"{3}\"" +
                                               "}}", DID1, _seqNo, _signatureType, DID1);

            var getClaimDefRequest = await Ledger.BuildGetClaimDefTxnAsync(DID1, _seqNo, _signatureType, DID1);


            Assert.IsTrue(getClaimDefRequest.Replace("\\", "").Contains(expectedResult));
        }
        public async Task TestClaimDefRequestWorks()
        {
            var trusteeDidResult = await Did.CreateAndStoreMyDidAsync(wallet, TRUSTEE_IDENTITY_JSON);

            var trusteeDid = trusteeDidResult.Did;

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

            var myDid    = myDidResult.Did;
            var myVerkey = myDidResult.VerKey;

            var nymRequest = await Ledger.BuildNymRequestAsync(trusteeDid, myDid, myVerkey, null, null);

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

            var claimDef = string.Format(_claimDefTemplate, _seqNo, myDid);

            var claimDefObj = JObject.Parse(claimDef);

            var claimDefJson    = claimDefObj["data"].ToString();
            var claimDefRequest = await Ledger.BuildClaimDefTxnAsync(myDid, _seqNo, _signatureType, claimDefJson);

            await Ledger.SignAndSubmitRequestAsync(pool, wallet, myDid, claimDefRequest);

            var getClaimDefRequest = await Ledger.BuildGetClaimDefTxnAsync(myDid, _seqNo, _signatureType, claimDefObj["origin"].ToString());

            var getClaimDefResponse = await Ledger.SubmitRequestAsync(pool, getClaimDefRequest);

            var getClaimDefResponseObj = JObject.Parse(getClaimDefResponse);

            var expectedClaimDef = claimDefObj["data"]["primary"];
            var actualClaimDef   = getClaimDefResponseObj["result"]["data"]["primary"];

            Assert.AreEqual(expectedClaimDef["n"], actualClaimDef["n"]);
            Assert.AreEqual(expectedClaimDef["rms"], actualClaimDef["rms"]);
            Assert.AreEqual(expectedClaimDef["rctxt"], actualClaimDef["rctxt"]);
            Assert.AreEqual(expectedClaimDef["z"], actualClaimDef["z"]);
            Assert.AreEqual(expectedClaimDef["n"], actualClaimDef["n"]);

            var expectedR = (JObject)expectedClaimDef["r"];
            var actualR   = (JObject)actualClaimDef["r"];

            Assert.AreEqual(expectedR.Value <string>("age"), actualR.Value <string>("age"));
            Assert.AreEqual(expectedR.Value <string>("sex"), actualR.Value <string>("sex"));
            Assert.AreEqual(expectedR.Value <string>("height"), actualR.Value <string>("height"));
            Assert.AreEqual(expectedR.Value <string>("name"), actualR.Value <string>("name"));
        }
Esempio n. 4
0
        public async Task TestClaimDefRequestWorks()
        {
            var trusteeJson = "{\"seed\":\"000000000000000000000000Trustee1\"}";

            var trusteeDidResult = await Signus.CreateAndStoreMyDidAsync(_wallet, trusteeJson);

            var trusteeDid = trusteeDidResult.Did;

            var myJson = "{}";

            var myDidResult = await Signus.CreateAndStoreMyDidAsync(_wallet, myJson);

            var myDid    = myDidResult.Did;
            var myVerkey = myDidResult.VerKey;

            var nymRequest = await Ledger.BuildNymRequestAsync(trusteeDid, myDid, myVerkey, null, null);

            await Ledger.SignAndSubmitRequestAsync(_pool, _wallet, trusteeDid, nymRequest);

            var schemaData = "{\"name\":\"gvt2\",\"version\":\"2.0\",\"attr_names\": [\"name\", \"male\"]}";

            var schemaRequest = await Ledger.BuildSchemaRequestAsync(myDid, schemaData);

            await Ledger.SignAndSubmitRequestAsync(_pool, _wallet, myDid, schemaRequest);

            var getSchemaData    = "{\"name\":\"gvt2\",\"version\":\"2.0\"}";
            var getSchemaRequest = await Ledger.BuildGetSchemaRequestAsync(myDid, myDid, getSchemaData);

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

            var schemaObj = JObject.Parse(getSchemaResponse);

            var schemaSeqNo = (int)schemaObj["result"]["seqNo"];

            var claimDef = string.Format(_claimDefTemplate, schemaSeqNo, myDid);

            var claimDefObj = JObject.Parse(claimDef);

            var claimDefJson = claimDefObj["data"];

            var claimDefRequest = await Ledger.BuildClaimDefTxnAsync(myDid, schemaSeqNo, _signatureType, claimDefObj["data"].ToString());

            var claimDefResponse = await Ledger.SignAndSubmitRequestAsync(_pool, _wallet, myDid, claimDefRequest);

            var getClaimDefRequest = await Ledger.BuildGetClaimDefTxnAsync(myDid, schemaSeqNo, _signatureType, claimDefObj["origin"].ToString());

            var getClaimDefResponse = await Ledger.SubmitRequestAsync(_pool, getClaimDefRequest);

            var getClaimDefResponseObj = JObject.Parse(getClaimDefResponse);

            var expectedClaimDef = claimDefObj["data"]["primary"];
            var actualClaimDef   = getClaimDefResponseObj["result"]["data"]["primary"];

            Assert.AreEqual(expectedClaimDef["n"], actualClaimDef["n"]);
            Assert.AreEqual(expectedClaimDef["rms"], actualClaimDef["rms"]);
            Assert.AreEqual(expectedClaimDef["rctxt"], actualClaimDef["rctxt"]);
            Assert.AreEqual(expectedClaimDef["z"], actualClaimDef["z"]);
            Assert.AreEqual(expectedClaimDef["n"], actualClaimDef["n"]);

            //TODO: Check reworked asserts are correct.
            //JSON.NET does not guarantee the order of nodes in an object when serialized so the original test failed.

            var expectedR = (JObject)expectedClaimDef["r"];
            var actualR   = (JObject)actualClaimDef["r"];

            Assert.AreEqual(expectedR.Value <string>("age"), actualR.Value <string>("age"));
            Assert.AreEqual(expectedR.Value <string>("sex"), actualR.Value <string>("sex"));
            Assert.AreEqual(expectedR.Value <string>("height"), actualR.Value <string>("height"));
            Assert.AreEqual(expectedR.Value <string>("name"), actualR.Value <string>("name"));
        }