public virtual async Task ProvisionAgentAsync(Wallet wallet, ProvisioningConfiguration provisioningConfiguration) { if (provisioningConfiguration == null) { throw new ArgumentNullException(nameof(provisioningConfiguration)); } if (provisioningConfiguration.EndpointUri == null) { throw new ArgumentNullException(nameof(provisioningConfiguration.EndpointUri)); } ProvisioningRecord record = null; try { record = await GetProvisioningAsync(wallet); } catch (AgentFrameworkException e) when(e.ErrorCode == ErrorCode.RecordNotFound) { } if (record != null) { throw new AgentFrameworkException(ErrorCode.WalletAlreadyProvisioned); } var agent = await Did.CreateAndStoreMyDidAsync(wallet, provisioningConfiguration.AgentSeed != null ?new { seed = provisioningConfiguration.AgentSeed }.ToJson() : "{}"); var masterSecretId = await AnonCreds.ProverCreateMasterSecretAsync(wallet, null); record = new ProvisioningRecord { MasterSecretId = masterSecretId, Endpoint = { Uri = provisioningConfiguration.EndpointUri.ToString(), Did = agent.Did, Verkey = agent.VerKey }, Owner = { Name = provisioningConfiguration.OwnerName, ImageUrl = provisioningConfiguration.OwnerImageUrl } }; if (provisioningConfiguration.CreateIssuer) { var issuer = await Did.CreateAndStoreMyDidAsync(wallet, provisioningConfiguration.IssuerSeed != null ?new { seed = provisioningConfiguration.IssuerSeed }.ToJson() : "{}"); record.IssuerDid = issuer.Did; record.IssuerVerkey = issuer.VerKey; record.TailsBaseUri = provisioningConfiguration.TailsBaseUri?.ToString(); } await RecordService.AddAsync(wallet, record); }
internal static async Task <(CredentialRecord issuerCredential, CredentialRecord holderCredential)> IssueCredentialAsync( ISchemaService schemaService, ICredentialService credentialService, IProducerConsumerCollection <IEnvelopeMessage> messages, string issuerConnectionId, Wallet issuerWallet, Wallet holderWallet, Pool pool, string proverMasterSecretId, bool revocable) { // Create an issuer DID/VK. Can also be created during provisioning var issuer = await Did.CreateAndStoreMyDidAsync(issuerWallet, new { seed = "000000000000000000000000Steward1" }.ToJson()); // Creata a schema and credential definition for this issuer var schemaId = await schemaService.CreateSchemaAsync(pool, issuerWallet, issuer.Did, $"Test-Schema-{Guid.NewGuid().ToString()}", "1.0", new[] { "first_name", "last_name" }); var definitionId = await schemaService.CreateCredentialDefinitionAsync(pool, issuerWallet, schemaId, issuer.Did, revocable, 100, new Uri("http://mock/tails")); var offerConfig = new DefaultCreateOfferConfiguration() { ConnectionId = issuerConnectionId, IssuerDid = issuer.Did, CredentialDefinitionId = definitionId }; // Send an offer to the holder using the established connection channel await credentialService.SendOfferAsync(issuerWallet, offerConfig); // Holder retrives message from their cloud agent var credentialOffer = FindContentMessage <CredentialOfferMessage>(messages); // Holder processes the credential offer by storing it var holderCredentialId = await credentialService.ProcessOfferAsync(holderWallet, credentialOffer); // Holder creates master secret. Will also be created during wallet agent provisioning await AnonCreds.ProverCreateMasterSecretAsync(holderWallet, proverMasterSecretId); // Holder accepts the credential offer and sends a credential request await credentialService.AcceptOfferAsync(holderWallet, pool, holderCredentialId, new Dictionary <string, string> { { "first_name", "Jane" }, { "last_name", "Doe" } }); // Issuer retrieves credential request from cloud agent var credentialRequest = FindContentMessage <CredentialRequestMessage>(messages); Assert.NotNull(credentialRequest); // Issuer processes the credential request by storing it var issuerCredentialId = await credentialService.ProcessCredentialRequestAsync(issuerWallet, credentialRequest); // Issuer accepts the credential requests and issues a credential await credentialService.IssueCredentialAsync(pool, issuerWallet, issuer.Did, issuerCredentialId); // Holder retrieves the credential from their cloud agent var credential = FindContentMessage <CredentialMessage>(messages); Assert.NotNull(credential); // Holder processes the credential by storing it in their wallet await credentialService.ProcessCredentialAsync(pool, holderWallet, credential); // Verify states of both credential records are set to 'Issued' var issuerCredential = await credentialService.GetAsync(issuerWallet, issuerCredentialId); var holderCredential = await credentialService.GetAsync(holderWallet, holderCredentialId); return(issuerCredential, holderCredential); }
public static async Task Execute() { Console.WriteLine("Anoncreds sample -> started"); var issuerWalletName = "issuerWallet"; var proverWalletName = "proverWallet"; 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, null); //3. Prover Create and Open Wallet await WalletUtils.CreateWalletAsync(PoolUtils.DEFAULT_POOL_NAME, proverWalletName, "default", null, null); // 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)) { //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, null); await WalletUtils.DeleteWalletAsync(proverWalletName, null); await PoolUtils.DeletePoolLedgerConfigAsync(PoolUtils.DEFAULT_POOL_NAME); } Console.WriteLine("Anoncreds sample -> completed"); }
/// <inheritdoc /> public virtual async Task ProvisionAgentAsync(ProvisioningConfiguration configuration) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if (configuration.WalletConfiguration == null || configuration.WalletCredentials == null) { throw new ArgumentNullException(nameof(configuration), "Wallet configuration and credentials must be specified"); } // Create agent wallet await WalletService.CreateWalletAsync(configuration.WalletConfiguration, configuration.WalletCredentials); var wallet = await WalletService.GetWalletAsync(configuration.WalletConfiguration, configuration.WalletCredentials); // Configure agent endpoint AgentEndpoint endpoint = null; if (configuration.EndpointUri != null) { endpoint = new AgentEndpoint { Uri = configuration.EndpointUri?.ToString() }; if (configuration.AgentSeed != null) { var agent = await Did.CreateAndStoreMyDidAsync(wallet, new { seed = configuration.AgentSeed }.ToJson()); endpoint.Did = agent.Did; endpoint.Verkey = agent.VerKey; } else if (configuration.AgentDid != null && configuration.AgentVerkey != null) { endpoint.Did = configuration.AgentDid; endpoint.Verkey = configuration.AgentVerkey; } else { var agent = await Did.CreateAndStoreMyDidAsync(wallet, "{}"); endpoint.Did = agent.Did; endpoint.Verkey = agent.VerKey; } } var masterSecretId = await AnonCreds.ProverCreateMasterSecretAsync(wallet, null); var record = new ProvisioningRecord { MasterSecretId = masterSecretId, Endpoint = endpoint, Owner = { Name = configuration.OwnerName, ImageUrl = configuration.OwnerImageUrl } }; // Populate initial tags if any passed if (configuration.Tags != null && configuration.Tags.Any()) { foreach (var item in configuration.Tags) { record.Tags.Add(item.Key, item.Value); } } // Create issuer if (configuration.CreateIssuer) { var issuer = await Did.CreateAndStoreMyDidAsync(wallet, configuration.IssuerSeed != null ?new { seed = configuration.IssuerSeed }.ToJson() : "{}"); record.IssuerDid = issuer.Did; record.IssuerVerkey = issuer.VerKey; record.TailsBaseUri = configuration.TailsBaseUri?.ToString(); } await RecordService.AddAsync(wallet, record); }
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_referent\":{\"schema_seq_no\":[1],\"name\":\"name\"},\n" + " \"attr2_referent\":{\"schema_seq_no\":[1],\"name\":\"sex\"}},\n" + " \"requested_predicates\":{\"predicate1_referent\":{\"attr_name\":\"age\",\"p_type\":\">=\",\"value\":18}}\n" + " }"; var claimsForProofJson = await AnonCreds.ProverGetClaimsForProofReqAsync(proverWallet, proofRequestJson); 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"]; Debug.Assert(claimsForAttribute1.Count == 1); Debug.Assert(claimsForAttribute2.Count == 1); Debug.Assert(claimsForPredicate.Count == 1); var claimUuid = claimsForAttribute1[0].Value <string>("referent"); //13. 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); var proof = JObject.Parse(proofJson); //14. Verifier verify Proof Debug.Assert(string.Equals("Alex", proof["requested_proof"]["revealed_attrs"]["attr1_referent"][1].ToString())); Debug.Assert(proof["requested_proof"]["unrevealed_attrs"].Value <string>("attr2_referent") != 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"); }
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); }
public async Task TestVerifyProofWorksForProofDoesNotCorrespondToProofRequest() { //1. 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); //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.ProverStoreCredentialOfferAsync(_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, createClaimResult.RevocRegUpdateJson); //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_referent\":{\"name\":\"name\",\"restrictions\":[{\"schema_seq_no\":1}]}, \"attr2_referent\":{\"name\":\"phone\"}},\n" + " \"requested_predicates\":{}\n" + " }"; var claimsForProofJson = await AnonCreds.ProverGetCredentialsForProofReqAsync(_proverWallet, proofRequestJson); Assert.IsNotNull(claimsForProofJson); var claimsForProof = JObject.Parse(claimsForProofJson); var claimsForAttribute1 = (JArray)claimsForProof["attrs"]["attr1_referent"]; Assert.AreEqual(claimsForAttribute1.Count, 1); var claimUuid = claimsForAttribute1[0].Value <string>("referent"); //9. Prover create Proof var selfAttestedValue = "yes"; var requestedClaimsJson = string.Format("{{\n" + " \"self_attested_attributes\":{{\"self1\":\"{0}\"}},\n" + " \"requested_attrs\":{{\"attr1_referent\":[\"{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_referent"][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_referent\":{\"name\":\"name\",\"restrictions\":[{\"schema_seq_no\":1}]}},\n" + " \"requested_predicates\":{\"predicate1_referent\":{\"attr_name\":\"age\",\"p_type\":\">=\",\"value\":18}\n" + " }"; var ex = await Assert.ThrowsExceptionAsync <InvalidStructureException>(() => AnonCreds.VerifierVerifyProofAsync(proofRequestJson, proofJson, schemasJson, claimDefsJson, revocRegsJson) ); }
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" + " \"keys\":[\"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" + " \"keys\":[\"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_uuid\":{\"schema_seq_no\":1,\"name\":\"name\"}},\n" + " \"requested_predicates\":{\"predicate1_uuid\":{\"attr_name\":\"age\",\"p_type\":\"GE\",\"value\":18}," + " \"predicate2_uuid\":{\"attr_name\":\"period\",\"p_type\":\"GE\",\"value\":5}}\n" + " }"; var claimsForProofJson = await AnonCreds.ProverGetClaimsForProofReqAsync(_proverWallet, proofRequestJson); Assert.IsNotNull(claimsForProofJson); var claimsForProof = JObject.Parse(claimsForProofJson); var claimsForAttribute1 = (JArray)claimsForProof["attrs"]["attr1_uuid"]; var claimsForPredicate1 = (JArray)claimsForProof["predicates"]["predicate1_uuid"]; var claimsForPredicate2 = (JArray)claimsForProof["predicates"]["predicate2_uuid"]; Assert.AreEqual(claimsForAttribute1.Count, 1); Assert.AreEqual(claimsForPredicate1.Count, 1); Assert.AreEqual(claimsForPredicate2.Count, 1); var claimUuidForAttr1 = claimsForAttribute1[0].Value <string>("claim_uuid"); var claimUuidForPredicate1 = claimsForPredicate1[0].Value <string>("claim_uuid"); var claimUuidForPredicate2 = claimsForPredicate2[0].Value <string>("claim_uuid"); //14. Prover create Proof var requestedClaimsJson = string.Format("{{\n" + " \"self_attested_attributes\":{{}},\n" + " \"requested_attrs\":{{\"attr1_uuid\":[\"{0}\", true]}},\n" + " \"requested_predicates\":{{\"predicate1_uuid\":\"{1}\"," + " \"predicate2_uuid\":\"{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_uuid"][1]); var valid = await AnonCreds.VerifierVerifyProofAsync(proofRequestJson, proofJson, schemasJson, claimDefsJson, revocRegsJson); Assert.IsTrue(valid); }
protected async Task InitCommonWallet() { if (_walletOpened) { return; } var walletConfig = JsonConvert.SerializeObject(new { id = Guid.NewGuid() }); await Wallet.CreateWalletAsync(walletConfig, CREDENTIALS); wallet = await Wallet.OpenWalletAsync(walletConfig, CREDENTIALS); var createSchemaResult = await AnonCreds.IssuerCreateSchemaAsync(issuerDid, gvtSchemaName, schemaVersion, gvtSchemaAttributes); gvtSchemaId = createSchemaResult.SchemaId; gvtSchema = createSchemaResult.SchemaJson; var xyzSchemaAttributes = "[\"status\", \"period\"]"; var xyzSchemaName = "xyz"; createSchemaResult = await AnonCreds.IssuerCreateSchemaAsync(issuerDid, xyzSchemaName, schemaVersion, xyzSchemaAttributes); xyzSchemaId = createSchemaResult.SchemaId; xyzSchema = createSchemaResult.SchemaJson; //Issue GVT issuer1GvtCredential by Issuer1 var issuer1CreateGvtCredDefResult = await AnonCreds.IssuerCreateAndStoreCredentialDefAsync(wallet, issuerDid, gvtSchema, tag, null, defaultCredentialDefinitionConfig); issuer1gvtCredDefId = issuer1CreateGvtCredDefResult.CredDefId; issuer1gvtCredDef = issuer1CreateGvtCredDefResult.CredDefJson; //Issue XYZ issuer1GvtCredential by Issuer1 var issuer1CreateXyzCredDefResult = await AnonCreds.IssuerCreateAndStoreCredentialDefAsync(wallet, issuerDid, xyzSchema, tag, null, defaultCredentialDefinitionConfig); var issuer1xyzCredDefId = issuer1CreateXyzCredDefResult.CredDefId; issuer1xyzCredDef = issuer1CreateXyzCredDefResult.CredDefJson; //Issue GVT issuer1GvtCredential by Issuer2 var issuerDid2 = "VsKV7grR1BUE29mG2Fm2kX"; var issuer2CreateGvtCredDefResult = await AnonCreds.IssuerCreateAndStoreCredentialDefAsync(wallet, issuerDid2, gvtSchema, tag, null, defaultCredentialDefinitionConfig); var issuer2gvtCredDefId = issuer2CreateGvtCredDefResult.CredDefId; var issuer2gvtCredDef = issuer2CreateGvtCredDefResult.CredDefJson; issuer1GvtCredOffer = await AnonCreds.IssuerCreateCredentialOfferAsync(wallet, issuer1gvtCredDefId); var issuer1XyzCredOffer = await AnonCreds.IssuerCreateCredentialOfferAsync(wallet, issuer1xyzCredDefId); issuer2GvtCredOffer = await AnonCreds.IssuerCreateCredentialOfferAsync(wallet, issuer2gvtCredDefId); await AnonCreds.ProverCreateMasterSecretAsync(wallet, masterSecretId); var createCredReqResult = await AnonCreds.ProverCreateCredentialReqAsync(wallet, proverDid, issuer1GvtCredOffer, issuer1gvtCredDef, masterSecretId); issuer1GvtCredReq = createCredReqResult.CredentialRequestJson; issuer1GvtCredReqMetadata = createCredReqResult.CredentialRequestMetadataJson; var createCredResult = await AnonCreds.IssuerCreateCredentialAsync(wallet, issuer1GvtCredOffer, issuer1GvtCredReq, gvtCredentialValuesJson, null, null); var issuer1GvtCredential = createCredResult.CredentialJson; await AnonCreds.ProverStoreCredentialAsync(wallet, credentialId1, issuer1GvtCredReqMetadata, issuer1GvtCredential, issuer1gvtCredDef, null); createCredReqResult = await AnonCreds.ProverCreateCredentialReqAsync(wallet, proverDid, issuer1XyzCredOffer, issuer1xyzCredDef, masterSecretId); var issuer1XyzCredReq = createCredReqResult.CredentialRequestJson; var issuer1XyzCredReqMetadata = createCredReqResult.CredentialRequestMetadataJson; createCredResult = await AnonCreds.IssuerCreateCredentialAsync(wallet, issuer1XyzCredOffer, issuer1XyzCredReq, xyzCredentialValuesJson, null, null); var issuer1XyzCredential = createCredResult.CredentialJson; await AnonCreds.ProverStoreCredentialAsync(wallet, credentialId2, issuer1XyzCredReqMetadata, issuer1XyzCredential, issuer1xyzCredDef, null); createCredReqResult = await AnonCreds.ProverCreateCredentialReqAsync(wallet, proverDid, issuer2GvtCredOffer, issuer2gvtCredDef, masterSecretId); var issuer2GvtCredReq = createCredReqResult.CredentialRequestJson; var issuer2GvtCredReqMetadata = createCredReqResult.CredentialRequestMetadataJson; var gvt2CredValues = "{" + " \"sex\":{\"raw\":\"male\",\"encoded\":\"2142657394558967239210949258394838228692050081607692519917028371144233115103\"},\n" + " \"name\":{\"raw\":\"Alexander\",\"encoded\":\"21332817548165488690172217217278169335\"},\n" + " \"height\":{\"raw\":\"170\",\"encoded\":\"170\"},\n" + " \"age\":{\"raw\":\"28\",\"encoded\":\"28\"}\n" + " }"; createCredResult = await AnonCreds.IssuerCreateCredentialAsync(wallet, issuer2GvtCredOffer, issuer2GvtCredReq, gvt2CredValues, null, null); var issuer2GvtCredential = createCredResult.CredentialJson; var credentialId3 = "id3"; await AnonCreds.ProverStoreCredentialAsync(wallet, credentialId3, issuer2GvtCredReqMetadata, issuer2GvtCredential, issuer2gvtCredDef, null); _walletOpened = true; }
public async Task TestAnonCredsWorksForMultipleIssuerSingleProver() { var issuerDid = "NcYxiDXkpYi6ov5FcYDi1e"; var issuerDid2 = "CnEDk9HrMnmiHXEV1WFgbVCRteYnPqsJwrTdcZaNhFVW"; var issuerGvtWallet = _issuerWallet; //1. Issuer2 Create and Open Wallet await Wallet.CreateWalletAsync(_poolName, "issuer2Wallet", TYPE, null, null); var issuerXyzWallet = await Wallet.OpenWalletAsync("issuer2Wallet", null, null); //2. 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(issuerGvtWallet, issuerDid, gvtSchemaJson, null, false); //3. 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(issuerXyzWallet, issuerDid2, xyzSchemaJson, null, false); //4. Prover create Master Secret var masterSecret = "masterSecretName"; await AnonCreds.ProverCreateMasterSecretAsync(_proverWallet, masterSecret); //5. Prover store Claim Offer received from Issuer1 var claimOffer = string.Format("{{\"issuer_did\":\"{0}\", \"schema_seq_no\":{1}}}", issuerDid, 1); await AnonCreds.ProverStoreCredentialOfferAsync(_proverWallet, claimOffer); //6. Prover store Claim Offer received from Issuer2 var claimOffer2 = string.Format("{{\"issuer_did\":\"{0}\", \"schema_seq_no\":{1}}}", issuerDid2, 2); await AnonCreds.ProverStoreCredentialOfferAsync(_proverWallet, claimOffer2); //7. 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 <string>("issuer_did").Equals(issuerDid) ? claimOfferObj1.ToString() : claimOfferObj2.ToString(); var xyzClaimOffer = claimOfferObj1.Value <string>("issuer_did").Equals(issuerDid2) ? claimOfferObj1.ToString() : claimOfferObj2.ToString(); //8. Prover create ClaimReq for GVT Claim Offer var proverDid = "BzfFCYk"; var gvtClaimReq = await AnonCreds.ProverCreateAndStoreClaimReqAsync(_proverWallet, proverDid, gvtClaimOffer, gvtClaimDef, masterSecret); //9. 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(issuerGvtWallet, gvtClaimReq, gvtClaimAttributesJson, -1); var gvtClaimJson = gvtCreateClaimResult.ClaimJson; //10. Prover store Claim await AnonCreds.ProverStoreClaimAsync(_proverWallet, gvtClaimJson, gvtCreateClaimResult.RevocRegUpdateJson); //11. Prover create ClaimReq for GVT Claim Offer var xyzClaimReq = await AnonCreds.ProverCreateAndStoreClaimReqAsync(_proverWallet, proverDid, xyzClaimOffer, xyzClaimDef, masterSecret); //12. Issuer create Claim var xyzClaimAttributesJson = "{\n" + " \"status\":[\"partial\",\"51792877103171595686471452153480627530895\"],\n" + " \"period\":[\"8\",\"8\"]\n" + " }"; var xyzCreateClaimResult = await AnonCreds.IssuerCreateClaimAsync(issuerXyzWallet, xyzClaimReq, xyzClaimAttributesJson, -1); var xyzClaimJson = xyzCreateClaimResult.ClaimJson; //13. Prover store Claim await AnonCreds.ProverStoreClaimAsync(_proverWallet, xyzClaimJson, xyzCreateClaimResult.RevocRegUpdateJson); //14. 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\":\"status\",\"restrictions\":[{\"schema_seq_no\":2}]}},\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.ProverGetCredentialsForProofReqAsync(_proverWallet, proofRequestJson); Assert.IsNotNull(claimsForProofJson); var claimsForProof = JObject.Parse(claimsForProofJson); var claimsForAttribute1 = (JArray)claimsForProof["attrs"]["attr1_referent"]; var claimsForAttribute2 = (JArray)claimsForProof["attrs"]["attr2_referent"]; var claimsForPredicate1 = (JArray)claimsForProof["predicates"]["predicate1_referent"]; var claimsForPredicate2 = (JArray)claimsForProof["predicates"]["predicate2_referent"]; Assert.AreEqual(claimsForAttribute1.Count, 1); Assert.AreEqual(claimsForAttribute2.Count, 1); Assert.AreEqual(claimsForPredicate1.Count, 1); Assert.AreEqual(claimsForPredicate2.Count, 1); var claimUuidForAttr1 = claimsForAttribute1[0].Value <string>("referent"); var claimUuidForAttr2 = claimsForAttribute2[0].Value <string>("referent"); var claimUuidForPredicate1 = claimsForPredicate1[0].Value <string>("referent"); var claimUuidForPredicate2 = claimsForPredicate2[0].Value <string>("referent"); //15. Prover create Proof var requestedClaimsJson = string.Format("{{\n" + " \"self_attested_attributes\":{{}},\n" + " \"requested_attrs\":{{\"attr1_referent\":[\"{0}\", true],\n" + " \"attr2_referent\":[\"{1}\", true]}},\n" + " \"requested_predicates\":{{\"predicate1_referent\":\"{2}\"," + " \"predicate2_referent\":\"{3}\"}}\n" + " }}", claimUuidForAttr1, claimUuidForAttr2, claimUuidForPredicate1, claimUuidForPredicate2); var schemasJson = string.Format("{{\"{0}\":{1}, \"{2}\":{3}}}", claimUuidForAttr1, gvtSchemaJson, claimUuidForAttr2, xyzSchemaJson); var claimDefsJson = string.Format("{{\"{0}\":{1}, \"{2}\":{3}}}", claimUuidForAttr1, gvtClaimDef, claimUuidForAttr2, xyzClaimDef); var revocRegsJson = "{}"; var proofJson = await AnonCreds.ProverCreateProofAsync(_proverWallet, proofRequestJson, requestedClaimsJson, schemasJson, masterSecret, claimDefsJson, revocRegsJson); Assert.IsNotNull(proofJson); var proof = JObject.Parse(proofJson); //16. Verifier verify Proof Assert.AreEqual("Alex", proof["requested_proof"]["revealed_attrs"]["attr1_referent"].Value <string>(1)); Assert.AreEqual("partial", proof["requested_proof"]["revealed_attrs"]["attr2_referent"].Value <string>(1)); Boolean valid = await AnonCreds.VerifierVerifyProofAsync(proofRequestJson, proofJson, schemasJson, claimDefsJson, revocRegsJson); Assert.IsTrue(valid); //18. Close and delete Issuer2 Wallet await issuerXyzWallet.CloseAsync(); await Wallet.DeleteWalletAsync("issuer2Wallet", null); }
/// <inheritdoc /> public async Task ProvisionAgentAsync(AgentOptions agentOptions) { if (agentOptions is null) { throw new ArgumentNullException(nameof(agentOptions)); } // Create agent wallet await WalletService.CreateWalletAsync( configuration : agentOptions.WalletConfiguration, credentials : agentOptions.WalletCredentials); var wallet = await WalletService.GetWalletAsync( configuration : agentOptions.WalletConfiguration, credentials : agentOptions.WalletCredentials); // Configure agent endpoint AgentEndpoint endpoint = null; if (agentOptions.EndpointUri != null) { endpoint = new AgentEndpoint { Uri = agentOptions.EndpointUri.ToString() }; if (agentOptions.AgentKeySeed != null) { var agent = await Did.CreateAndStoreMyDidAsync(wallet, new { seed = agentOptions.AgentKeySeed }.ToJson()); endpoint.Did = agent.Did; endpoint.Verkey = agent.VerKey; } else if (agentOptions.AgentKey != null) { endpoint.Did = agentOptions.AgentDid; endpoint.Verkey = agentOptions.AgentKey; } else { var agent = await Did.CreateAndStoreMyDidAsync(wallet, "{}"); endpoint.Did = agent.Did; endpoint.Verkey = agent.VerKey; } } var masterSecretId = await AnonCreds.ProverCreateMasterSecretAsync(wallet, null); var record = new ProvisioningRecord { MasterSecretId = masterSecretId, Endpoint = endpoint, Owner = { Name = agentOptions.AgentName, ImageUrl = agentOptions.AgentImageUri } }; // Issuer Configuration if (agentOptions.IssuerKeySeed == null) { agentOptions.IssuerKeySeed = CryptoUtils.GetUniqueKey(32); } var issuer = await Did.CreateAndStoreMyDidAsync( wallet : wallet, didJson : new { did = agentOptions.IssuerDid, seed = agentOptions.IssuerKeySeed }.ToJson()); record.IssuerSeed = agentOptions.IssuerKeySeed; record.IssuerDid = issuer.Did; record.IssuerVerkey = issuer.VerKey; record.TailsBaseUri = agentOptions.EndpointUri != null ? new Uri(new Uri(agentOptions.EndpointUri), "tails/").ToString() : null; record.SetTag("AgentKeySeed", agentOptions.AgentKeySeed); record.SetTag("IssuerKeySeed", agentOptions.IssuerKeySeed); // Add record to wallet await RecordService.AddAsync(wallet, record); }
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); }
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) ); }
public async Task TestProverCreateMasterSecretWorksForDuplicate() { var ex = await Assert.ThrowsExceptionAsync <DuplicateMasterSecretNameException>(() => AnonCreds.ProverCreateMasterSecretAsync(wallet, masterSecretId) ); }
public static async Task <(CredentialRecord issuerCredential, CredentialRecord holderCredential)> IssueCredentialAsync( ISchemaService schemaService, ICredentialService credentialService, IProducerConsumerCollection <AgentMessage> messages, ConnectionRecord issuerConnection, ConnectionRecord holderConnection, IAgentContext issuerContext, IAgentContext holderContext, Pool pool, string proverMasterSecretId, bool revocable, List <CredentialPreviewAttribute> credentialAttributes, OfferConfiguration offerConfiguration = null) { // Create an issuer DID/VK. Can also be created during provisioning var issuer = await Did.CreateAndStoreMyDidAsync(issuerContext.Wallet, new { seed = TestConstants.StewardSeed }.ToJson()); // Create a schema and credential definition for this issuer var(definitionId, _) = await CreateDummySchemaAndNonRevokableCredDef(issuerContext, schemaService, issuer.Did, credentialAttributes.Select(_ => _.Name).ToArray()); var offerConfig = offerConfiguration ?? new OfferConfiguration { IssuerDid = issuer.Did, CredentialDefinitionId = definitionId }; // Send an offer to the holder using the established connection channel var(offerMessage, _) = await credentialService.CreateOfferAsync( agentContext : issuerContext, config : offerConfig, connectionId : issuerConnection.Id); messages.TryAdd(offerMessage); // Holder retrieves message from their cloud agent var credentialOffer = FindContentMessage <CredentialOfferMessage>(messages); // Holder processes the credential offer by storing it var holderCredentialId = await credentialService.ProcessOfferAsync(holderContext, credentialOffer, holderConnection); // Holder creates master secret. Will also be created during wallet agent provisioning await AnonCreds.ProverCreateMasterSecretAsync(holderContext.Wallet, proverMasterSecretId); // Holder accepts the credential offer and sends a credential request var(request, _) = await credentialService.CreateRequestAsync(holderContext, holderCredentialId); messages.TryAdd(request); // Issuer retrieves credential request from cloud agent var credentialRequest = FindContentMessage <CredentialRequestMessage>(messages); Assert.NotNull(credentialRequest); // Issuer processes the credential request by storing it var issuerCredentialId = await credentialService.ProcessCredentialRequestAsync(issuerContext, credentialRequest, issuerConnection); // Issuer accepts the credential requests and issues a credential var(credentialMessage, _) = await credentialService.CreateCredentialAsync( agentContext : issuerContext, credentialId : issuerCredentialId, values : credentialAttributes); messages.TryAdd(credentialMessage); // Holder retrieves the credential from their cloud agent var credential = FindContentMessage <CredentialIssueMessage>(messages); Assert.NotNull(credential); // Holder processes the credential by storing it in their wallet await credentialService.ProcessCredentialAsync(holderContext, credential, holderConnection); // Verify states of both credential records are set to 'Issued' var issuerCredential = await credentialService.GetAsync(issuerContext, issuerCredentialId); var holderCredential = await credentialService.GetAsync(holderContext, holderCredentialId); return(issuerCredential, holderCredential); }
public async Task TestProverCreateMasterSecretWorksForEmptyName() { await AnonCreds.ProverCreateMasterSecretAsync(wallet, null); }
internal static async Task <(CredentialRecord issuerCredential, CredentialRecord holderCredential)> IssueCredentialAsync( ISchemaService schemaService, ICredentialService credentialService, IProducerConsumerCollection <IAgentMessage> messages, ConnectionRecord issuerConnection, ConnectionRecord holderConnection, IAgentContext issuerContext, IAgentContext holderContext, Pool pool, string proverMasterSecretId, bool revocable, OfferConfiguration offerConfiguration = null) { // Create an issuer DID/VK. Can also be created during provisioning var issuer = await Did.CreateAndStoreMyDidAsync(issuerContext.Wallet, new { seed = "000000000000000000000000Steward1" }.ToJson()); // Creata a schema and credential definition for this issuer (string definitionId, _) = await CreateDummySchemaAndNonRevokableCredDef(issuerContext, schemaService, issuer.Did, new[] { "first_name", "last_name" }); var offerConfig = offerConfiguration ?? new OfferConfiguration { IssuerDid = issuer.Did, CredentialDefinitionId = definitionId }; // Send an offer to the holder using the established connection channel await credentialService.SendOfferAsync(issuerContext, issuerConnection.Id, offerConfig); // Holder retrives message from their cloud agent var credentialOffer = FindContentMessage <CredentialOfferMessage>(messages); // Holder processes the credential offer by storing it var holderCredentialId = await credentialService.ProcessOfferAsync(holderContext, credentialOffer, holderConnection); // Holder creates master secret. Will also be created during wallet agent provisioning await AnonCreds.ProverCreateMasterSecretAsync(holderContext.Wallet, proverMasterSecretId); // Holder accepts the credential offer and sends a credential request await credentialService.AcceptOfferAsync(holderContext, holderCredentialId, new Dictionary <string, string> { { "first_name", "Jane" }, { "last_name", "Doe" } }); // Issuer retrieves credential request from cloud agent var credentialRequest = FindContentMessage <CredentialRequestMessage>(messages); Assert.NotNull(credentialRequest); // Issuer processes the credential request by storing it var issuerCredentialId = await credentialService.ProcessCredentialRequestAsync(issuerContext, credentialRequest, issuerConnection); // Issuer accepts the credential requests and issues a credential await credentialService.IssueCredentialAsync(issuerContext, issuer.Did, issuerCredentialId); // Holder retrieves the credential from their cloud agent var credential = FindContentMessage <CredentialMessage>(messages); Assert.NotNull(credential); // Holder processes the credential by storing it in their wallet await credentialService.ProcessCredentialAsync(holderContext, credential, holderConnection); // Verify states of both credential records are set to 'Issued' var issuerCredential = await credentialService.GetAsync(issuerContext, issuerCredentialId); var holderCredential = await credentialService.GetAsync(holderContext, holderCredentialId); return(issuerCredential, holderCredential); }
public async Task TestAnonCredsDemo() { //4. 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); //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_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); Assert.IsNotNull(claimsForProofJson); 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"]; Assert.AreEqual(claimsForAttribute1.Count, 1); Assert.AreEqual(claimsForAttribute2.Count, 1); Assert.AreEqual(claimsForPredicate.Count, 1); var claimUuid = claimsForAttribute1[0].Value <string>("claim_uuid"); //12. 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); Assert.IsNotNull(proofJson); var proof = JObject.Parse(proofJson); //13. Verifier verify Proof Assert.AreEqual("Alex", proof["requested_proof"]["revealed_attrs"]["attr1_uuid"][1]); Assert.IsNotNull(proof["requested_proof"]["unrevealed_attrs"].Value <string>("attr2_uuid")); 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); }
public async Task IssueCredentialThrowsExceptionCredentialInvalidState() { //Establish a connection between the two parties var(issuerConnection, holderConnection) = await Scenarios.EstablishConnectionAsync( _connectionService, _messages, _issuerWallet, _holderWallet); // Create an issuer DID/VK. Can also be created during provisioning var issuer = await Did.CreateAndStoreMyDidAsync(_issuerWallet.Wallet, new { seed = TestConstants.StewartDid }.ToJson()); // Create a schema and credential definition for this issuer var(definitionId, _) = await Scenarios.CreateDummySchemaAndNonRevokableCredDef(_issuerWallet, _schemaService, issuer.Did, new[] { "dummy_attr" }); var offerConfig = new OfferConfiguration { IssuerDid = issuer.Did, CredentialDefinitionId = definitionId }; // Send an offer to the holder using the established connection channel var(message, _) = await _credentialService.CreateOfferAsync(_issuerWallet, offerConfig, issuerConnection.Id); _messages.Add(message); // Holder retrieves message from their cloud agent var credentialOffer = FindContentMessage <CredentialOfferMessage>(_messages); // Holder processes the credential offer by storing it var holderCredentialId = await _credentialService.ProcessOfferAsync(_holderWallet, credentialOffer, holderConnection); // Holder creates master secret. Will also be created during wallet agent provisioning await AnonCreds.ProverCreateMasterSecretAsync(_holderWallet.Wallet, TestConstants.DefaultMasterSecret); // Holder accepts the credential offer and sends a credential request var(request, _) = await _credentialService.CreateCredentialRequestAsync(_holderWallet, holderCredentialId); _messages.Add(request); // Issuer retrieves credential request from cloud agent var credentialRequest = FindContentMessage <CredentialRequestMessage>(_messages); Assert.NotNull(credentialRequest); // Issuer processes the credential request by storing it var issuerCredentialId = await _credentialService.ProcessCredentialRequestAsync(_issuerWallet, credentialRequest, issuerConnection); // Issuer accepts the credential requests and issues a credential var(credential, _) = await _credentialService.CreateCredentialAsync(_issuerWallet, issuer.Did, issuerCredentialId, new List <CredentialPreviewAttribute> { new CredentialPreviewAttribute("dummy_attr", "dummyVal") }); _messages.Add(credential); //Try issue the credential again var ex = await Assert.ThrowsAsync <AgentFrameworkException>(async() => await _credentialService.CreateCredentialAsync(_issuerWallet, issuer.Did, issuerCredentialId)); Assert.True(ex.ErrorCode == ErrorCode.RecordInInvalidState); }
public static async Task Execute() { Console.WriteLine("Anoncreds Revocation sample -> started"); var proverWalletConfig = "{\"id\":\"prover_wallet\"}"; var issuerWalletConfig = "{\"id\":\"issuer_wallet\"}"; 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(issuerWalletConfig, issuerWalletCredentials); //3. Prover Create and Open Wallet await WalletUtils.CreateWalletAsync(proverWalletConfig, 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(issuerWalletConfig, issuerWalletCredentials)) using (var proverWallet = await Wallet.OpenWalletAsync(proverWalletConfig, 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(issuerWalletConfig, issuerWalletCredentials); await WalletUtils.DeleteWalletAsync(proverWalletConfig, proverWalletCredentials); await PoolUtils.DeletePoolLedgerConfigAsync(PoolUtils.DEFAULT_POOL_NAME); } Console.WriteLine("Anoncreds Revocation sample -> completed"); }
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); }