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();
            }
        }
Example #2
0
        public async Task TestRefreshPoolWorks()
        {
            var pool = await PoolUtils.CreateAndOpenPoolLedgerAsync();

            Assert.IsNotNull(pool);
            _openedPools.Add(pool);

            await pool.RefreshAsync();
        }
        public async Task TestRefreshPoolWorks()
        {
            Pool.SetProtocolVersionAsync(PoolUtils.PROTOCOL_VERSION).Wait();
            var pool = await PoolUtils.CreateAndOpenPoolLedgerAsync();

            Assert.IsNotNull(pool);
            openedPools.Add(pool);

            await pool.RefreshAsync();
        }
        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();
            }
        }
Example #5
0
        public async Task TestClosePoolWorks()
        {
            var pool = await PoolUtils.CreateAndOpenPoolLedgerAsync();

            Assert.IsNotNull(pool);
            openedPools.Add(pool);

            await pool.CloseAsync();

            openedPools.Remove(pool);
        }
Example #6
0
        public async Task TestClosePoolWorksForTwice()
        {
            var pool = await PoolUtils.CreateAndOpenPoolLedgerAsync();

            Assert.IsNotNull(pool);
            openedPools.Add(pool);

            await pool.CloseAsync();

            openedPools.Remove(pool);

            var ex = await Assert.ThrowsExceptionAsync <InvalidPoolException>(() =>
                                                                              pool.CloseAsync()
                                                                              );
        }
Example #7
0
        public async Task TestClosePoolWorksForTwice()
        {
            var pool = await PoolUtils.CreateAndOpenPoolLedgerAsync();

            Assert.IsNotNull(pool);
            _openedPools.Add(pool);

            await pool.CloseAsync();

            _openedPools.Remove(pool);

            var ex = await Assert.ThrowsExceptionAsync <IndyException>(() =>
                                                                       pool.CloseAsync()
                                                                       );

            Assert.AreEqual(ErrorCode.PoolLedgerInvalidPoolHandle, ex.ErrorCode);
        }
        public async Task CreateCredentialSchemaAsync(string userId, string name, string version, string[] attributes)
        {
            var walletData = await GetDefaultWalletDataAsync(userId);

            var schema = new { name, version, attr_names = attributes };
            var wallet = await Wallet.OpenWalletAsync(walletData.Name, null, null);

            try
            {
                using (var pool = await PoolUtils.CreateAndOpenPoolLedgerAsync())
                {
                    var schemaRequest = await Ledger.BuildSchemaRequestAsync(walletData.Did, JsonConvert.SerializeObject(schema));

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

                    Console.WriteLine("=============Schema Request Result===========");
                    Console.WriteLine(result);

                    var schemaData = new CredentialSchema
                    {
                        Id             = Guid.NewGuid(),
                        AttributeArray = attributes,
                        Name           = name,
                        Version        = version,
                        UserId         = userId
                    };

                    _dbContext.CredentialSchemas.Add(schemaData);
                    await _dbContext.SaveChangesAsync();
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                await wallet.CloseAsync();
            }
        }
Example #9
0
        public async Task CreateTrustAnchorWalletAsync(ApplicationUser user)
        {
            // Get steward wallet
            var stewardWalletData = await GetStewardWalletDataAsync();

            var stewardWallet = await Wallet.OpenWalletAsync(stewardWalletData.Name, null, null);

            Wallet trustAnchorWallet = null;

            try
            {
                // steward create new DID
                var stewardTrustAnchorDidResult = await Did.CreateAndStoreMyDidAsync(stewardWallet, "{}");

                using (var pool = await PoolUtils.CreateAndOpenPoolLedgerAsync())
                {
                    // steward send NYM transaction
                    var nymRequest = await Ledger.BuildNymRequestAsync(stewardWalletData.Did, stewardTrustAnchorDidResult.Did, stewardTrustAnchorDidResult.VerKey, null, "TRUST_ANCHOR");

                    await Ledger.SignAndSubmitRequestAsync(pool, stewardWallet, stewardWalletData.Did, nymRequest);

                    // steward creates the connection request
                    var nonce             = RandomUtils.RandomNumber(8);
                    var connectionRequest = new { did = stewardTrustAnchorDidResult.Did, nonce };

                    // trust anchor create wallet
                    var trustAnchorWalletName = Guid.NewGuid().ToString();
                    await Wallet.CreateWalletAsync(_configuration.GetValue("Indy.PoolName", "indy_pool"), trustAnchorWalletName, null, null, null);

                    trustAnchorWallet = await Wallet.OpenWalletAsync(trustAnchorWalletName, null, null);

                    var trustAnchorStewardWalletDidResult = await Did.CreateAndStoreMyDidAsync(trustAnchorWallet, "{}");

                    // trust anchor create connection response
                    var connectionResponse = new { did = trustAnchorStewardWalletDidResult.Did, verKey = trustAnchorStewardWalletDidResult.VerKey, connectionRequest.nonce };

                    // trust anchor asks the ledger for the verkey of the Steward's DID
                    var stewardTrustAnchorVerKey = await Did.KeyForDidAsync(pool, trustAnchorWallet, connectionRequest.did);

                    // trust anchor encrypts the connection response
                    var anoncryptedConnectionResponse = await Crypto.AnonCryptAsync(stewardTrustAnchorVerKey, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(connectionResponse)));

                    // steward decrypts the connection response
                    var decryptedConnectionResponse = await Crypto.AnonDecryptAsync(stewardWallet, stewardTrustAnchorDidResult.VerKey, anoncryptedConnectionResponse);

                    var decryptedConnectionResponseObj = JObject.Parse(Encoding.UTF8.GetString(decryptedConnectionResponse));
                    if (decryptedConnectionResponseObj.Value <int>("nonce") != nonce)
                    {
                        throw new Exception("Decrypt connection response failed, nonce not match");
                    }

                    // steward sends NYM transaction for Trust Anchor's DID to the ledger
                    nymRequest = await Ledger.BuildNymRequestAsync(stewardWalletData.Did, decryptedConnectionResponseObj.Value <string>("did"), decryptedConnectionResponseObj.Value <string>("verKey"), null, "TRUST_ANCHOR");

                    await Ledger.SignAndSubmitRequestAsync(pool, stewardWallet, stewardWalletData.Did, nymRequest);

                    // Getting verinym
                    // trust anchor creates new DID
                    var trustAnchorDidResult = await Did.CreateAndStoreMyDidAsync(trustAnchorWallet, "{}");

                    var authcryptedTrustAnchorDidInfoJson = await Crypto.AuthCryptAsync(trustAnchorWallet, trustAnchorStewardWalletDidResult.VerKey, stewardTrustAnchorVerKey, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new
                    {
                        did    = trustAnchorDidResult.Did,
                        verkey = trustAnchorDidResult.VerKey
                    })));

                    // steward decrypts the received message
                    var authDecryptResult = await Crypto.AuthDecryptAsync(stewardWallet, stewardTrustAnchorVerKey, authcryptedTrustAnchorDidInfoJson);

                    var authDecryptedDidInfo = JObject.Parse(Encoding.UTF8.GetString(authDecryptResult.MessageData));

                    // steward  asks the ledger for the verkey of trust anchor's DID
                    var trustAnchorVerKey = await Did.KeyForDidAsync(pool, stewardWallet, authDecryptedDidInfo.Value <string>("did"));

                    if (trustAnchorVerKey != authDecryptResult.TheirVk)
                    {
                        throw new Exception("Decrypt auth failed, VerKey not match");
                    }

                    // steward send NYM transaction for Trust Anchor's DID
                    nymRequest = await Ledger.BuildNymRequestAsync(stewardWalletData.Did, authDecryptedDidInfo.Value <string>("did"), authDecryptedDidInfo.Value <string>("verkey"), null, "TRUST_ANCHOR");

                    await Ledger.SignAndSubmitRequestAsync(pool, stewardWallet, stewardWalletData.Did, nymRequest);

                    await SaveWalletData(trustAnchorWalletName, user.Id, trustAnchorDidResult.Did, trustAnchorDidResult.VerKey, true);

                    await pool.CloseAsync();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                await stewardWallet.CloseAsync();

                await trustAnchorWallet.CloseAsync();
            }
        }
Example #10
0
        public async Task CreateWalletForBusinessAsync(ApplicationUser user)
        {
            // Get steward wallet
            var stewardWalletData = await GetStewardWalletDataAsync();

            var stewardWallet = await Wallet.OpenWalletAsync(stewardWalletData.Name, null, null);

            Wallet trustAnchorWallet = null;

            try
            {
                // steward create new DID
                var stewardTrustAnchorDidResult = await Did.CreateAndStoreMyDidAsync(stewardWallet, "{}");

                using (var pool = await PoolUtils.CreateAndOpenPoolLedgerAsync())
                {
                    // steward send NYM transaction
                    var nymRequest = await Ledger.BuildNymRequestAsync(stewardWalletData.Did, stewardTrustAnchorDidResult.Did, stewardTrustAnchorDidResult.VerKey, null, "TRUST_ANCHOR");

                    var a = await Ledger.SignAndSubmitRequestAsync(pool, stewardWallet, stewardWalletData.Did, nymRequest);

                    Console.WriteLine($"NYM 1: {a}");

                    // trust anchor create wallet
                    var trustAnchorWalletName = Guid.NewGuid().ToString();
                    await Wallet.CreateWalletAsync(_configuration.GetValue("Indy.PoolName", "indy_pool"), trustAnchorWalletName, null, null, null);

                    trustAnchorWallet = await Wallet.OpenWalletAsync(trustAnchorWalletName, null, null);

                    var trustAnchorStewardWalletDidResult = await Did.CreateAndStoreMyDidAsync(trustAnchorWallet, "{}");

                    // steward sends NYM transaction for Trust Anchor's DID to the ledger
                    nymRequest = await Ledger.BuildNymRequestAsync(stewardWalletData.Did, trustAnchorStewardWalletDidResult.Did, trustAnchorStewardWalletDidResult.VerKey, null, "TRUST_ANCHOR");

                    var b = await Ledger.SignAndSubmitRequestAsync(pool, stewardWallet, stewardWalletData.Did, nymRequest);

                    Console.WriteLine($"NYM 2: {b}");

                    // Getting verinym
                    // trust anchor creates new DID
                    var trustAnchorDidResult = await Did.CreateAndStoreMyDidAsync(trustAnchorWallet, "{}");

                    // steward send NYM transaction for Trust Anchor's DID
                    nymRequest = await Ledger.BuildNymRequestAsync(stewardWalletData.Did, trustAnchorDidResult.Did, trustAnchorDidResult.VerKey, null, "TRUST_ANCHOR");

                    var c = await Ledger.SignAndSubmitRequestAsync(pool, stewardWallet, stewardWalletData.Did, nymRequest);

                    Console.WriteLine($"NYM 3: {c}");

                    await SaveWalletData(trustAnchorWalletName, user.Id, trustAnchorDidResult.Did, trustAnchorDidResult.VerKey, true);

                    await pool.CloseAsync();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                await stewardWallet.CloseAsync();

                await trustAnchorWallet.CloseAsync();
            }
        }
Example #11
0
        public async Task CreateWalletAsync(ApplicationUser user, bool isTrustAnchor, string stewardId = null, string seed = null)
        {
            WalletData stewardWalletData = null;
            Wallet     stewardWallet     = null;

            if (!string.IsNullOrEmpty(stewardId))
            {
                stewardWalletData = await _dbContext.WalletDatas.FirstOrDefaultAsync(p => p.UserId == stewardId);

                if (stewardWalletData == null)
                {
                    throw new Exception("Agent Wallet not found.");
                }

                stewardWallet = await Wallet.OpenWalletAsync(stewardWalletData.Name, null, null);
            }

            var    walletName = $"{user.Id}";
            Wallet wallet     = null;

            try
            {
                await Wallet.CreateWalletAsync(_configuration.GetValue("Indy.PoolName", "indy_pool"), walletName, null, null, null);

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

                var didJson = "{}";
                if (!string.IsNullOrEmpty(seed))
                {
                    didJson = DidJson.GetJson(seed);
                }

                var didResult = await Did.CreateAndStoreMyDidAsync(wallet, didJson);

                var walletData = new WalletData
                {
                    UserId      = user.Id,
                    TimeCreated = DateTime.UtcNow,
                    Did         = didResult.Did,
                    Name        = walletName,
                    VerKey      = didResult.VerKey
                };

                _dbContext.WalletDatas.Add(walletData);

                if (stewardWalletData != null)
                {
                    if (isTrustAnchor)
                    {
                        var stewardAgentDidResult = await Did.CreateAndStoreMyDidAsync(stewardWallet, "{}");

                        var nymRequest = await Ledger.BuildNymRequestAsync(stewardWalletData.Did, stewardAgentDidResult.Did, stewardAgentDidResult.VerKey, null, "TRUST_ANCHOR");

                        using (var pool = await PoolUtils.CreateAndOpenPoolLedgerAsync())
                        {
                            var res = await Ledger.SignAndSubmitRequestAsync(pool, stewardWallet, stewardWalletData.Did, nymRequest);

                            var stewardAgentVerKey = await Did.KeyForDidAsync(pool, wallet, stewardAgentDidResult.Did);

                            var connectionResponse            = JsonConvert.SerializeObject(new { did = stewardAgentDidResult.Did, verkey = stewardAgentVerKey, nonce = 21314123141 });
                            var anoncryptedConnectionResponse = await Crypto.AnonCryptAsync(stewardAgentVerKey, Encoding.UTF8.GetBytes(connectionResponse));

                            var decryptedConnectionResponse = Encoding.UTF8.GetString(await Crypto.AnonDecryptAsync(stewardWallet, stewardAgentDidResult.VerKey, anoncryptedConnectionResponse));
                            var connectionResponseObj       = JObject.Parse(decryptedConnectionResponse);
                            nymRequest = await Ledger.BuildNymRequestAsync(stewardWalletData.Did, connectionResponseObj.Value <string>("did"), connectionResponseObj.Value <string>("verkey"), null, "TRUST_ANCHOR");

                            var signAndSubmitRequestResult = await Ledger.SignAndSubmitRequestAsync(pool, stewardWallet, stewardWalletData.Did, nymRequest);

                            // getting verinym
                        }
                    }
                }

                await _dbContext.SaveChangesAsync();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                await CloseWalletAsync(wallet);
                await CloseWalletAsync(stewardWallet);
            }
        }