예제 #1
0
        public static async Task Execute()
        {
            Console.WriteLine("Ledger sample -> started");

            var myWalletName    = "myWallet";
            var theirWalletName = "theirWallet";

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

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

                // 3. Create and Open Trustee Wallet
                await WalletUtils.CreateWalleatAsync(PoolUtils.DEFAULT_POOL_NAME, theirWalletName, "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 myWallet = await Wallet.OpenWalletAsync(myWalletName, null, null))
                        using (var theirWallet = await Wallet.OpenWalletAsync(theirWalletName, null, null))
                        {
                            //5. Create My Did
                            var createMyDidResult = await Signus.CreateAndStoreMyDidAsync(myWallet, "{}");

                            //6. Create Their Did
                            var createTheirDidResult = await Signus.CreateAndStoreMyDidAsync(theirWallet, "{}");

                            var theirDid    = createTheirDidResult.Did;
                            var theirVerkey = createTheirDidResult.VerKey;

                            //7. Store Their DID
                            var identityJson = string.Format("{{\"did\":\"{0}\", \"verkey\":\"{1}\"}}", theirDid, theirVerkey);
                            await Signus.StoreTheirDidAsync(myWallet, identityJson);

                            //8. Their sign message
                            var msgBytes = Encoding.UTF8.GetBytes("{\n" +
                                                                  "   \"reqId\":1495034346617224651,\n" +
                                                                  "   \"identifier\":\"GJ1SzoWzavQYfNL9XkaJdrQejfztN4XqdsiV4ct3LXKL\",\n" +
                                                                  "   \"operation\":{\n" +
                                                                  "       \"type\":\"1\",\n" +
                                                                  "       \"dest\":\"4efZu2SXufS556yss7W5k6Po37jt4371RM4whbPKBKdB\"\n" +
                                                                  "   }\n" +
                                                                  "}");

                            var signatureBytes = await Signus.SignAsync(theirWallet, theirDid, msgBytes);

                            //9. Verify message
                            var valid = await Signus.VerifySignatureAsync(myWallet, pool, theirDid, msgBytes, signatureBytes);

                            Debug.Assert(valid == true);

                            //10. Close wallets and pool
                            await myWallet.CloseAsync();

                            await theirWallet.CloseAsync();

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

                await WalletUtils.DeleteWalletAsync(theirWalletName, null);

                await PoolUtils.DeletePoolLedgerConfigAsync(PoolUtils.DEFAULT_POOL_NAME);
            }

            Console.WriteLine("Ledger sample -> completed");
        }
예제 #2
0
        public static async Task Execute()
        {
            Console.WriteLine("Agent sample -> started");

            var listenerWalletName = "listenerWallet";
            var trusteeWalletName  = "trusteeWallet";
            var endpoint           = "127.0.0.1:9801";
            var message            = "test";
            var trusteeSeed        = "000000000000000000000000Trustee1";

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

                //2. Create Listener Wallet
                await WalletUtils.CreateWalleatAsync(PoolUtils.DEFAULT_POOL_NAME, listenerWalletName, "default", null, null);

                //3. Create Trustee Wallet
                await WalletUtils.CreateWalleatAsync(PoolUtils.DEFAULT_POOL_NAME, trusteeWalletName, "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 listenerWallet = await Wallet.OpenWalletAsync(listenerWalletName, null, null))
                        using (var trusteeWallet = await Wallet.OpenWalletAsync(trusteeWalletName, null, null))
                        {
                            var senderWallet = trusteeWallet;

                            //5. Create My Did
                            var createMyDidResult = await Signus.CreateAndStoreMyDidAsync(listenerWallet, "{}");

                            var listenerDid    = createMyDidResult.Did;
                            var listenerVerkey = createMyDidResult.VerKey;
                            var listenerPk     = createMyDidResult.Pk;

                            //6. Create Their Did from Trustee seed
                            var trusteeDidJson = string.Format("{{\"seed\":\"{0}\"}}", trusteeSeed);

                            var trusteeDidResult = await Signus.CreateAndStoreMyDidAsync(trusteeWallet, trusteeDidJson);

                            var trusteeDid = trusteeDidResult.Did;
                            var senderDid  = trusteeDid;

                            //7. Prepare and Send NYM request with signing
                            var nymRequest = await Ledger.BuildNymRequestAsync(trusteeDid, listenerDid, listenerVerkey, null, null);

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

                            //8. Prepare and Send Attrib for listener (will be requested from ledger and used by sender at start connection)
                            var rawAttribJson = string.Format("{{\"endpoint\":{{\"ha\":\"{0}\",\"verkey\":\"{1}\"}}}}", endpoint, listenerPk);
                            var attribRequest = await Ledger.BuildAttribRequestAsync(listenerDid, listenerDid, null, rawAttribJson, null);

                            await Ledger.SignAndSubmitRequestAsync(pool, listenerWallet, listenerDid, attribRequest);

                            //9. start listener on endpoint
                            var activeListener = await AgentListener.ListenAsync(endpoint);

                            //10. Allow listener accept incoming connection for specific DID (listener_did)
                            await activeListener.AddIdentityAsync(pool, listenerWallet, listenerDid);

                            //11. Initiate connection from sender to listener
                            var sendingConnection = await AgentConnection.ConnectAsync(pool, senderWallet, senderDid, listenerDid);

                            var connectionEvent = await activeListener.WaitForConnectionAsync();

                            var receivingConnection = connectionEvent.Connection;

                            //12. Send test message from sender to listener
                            await sendingConnection.SendAsync(message);

                            var messageEvent = await receivingConnection.WaitForMessageAsync();

                            Debug.Assert(string.Equals(message, messageEvent.Message));

                            //13. Close connection
                            await sendingConnection.CloseAsync();

                            //14. Close listener
                            await activeListener.CloseAsync();

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

                            await trusteeWallet.CloseAsync();

                            await pool.CloseAsync();
                        }
            }
            finally
            {
                // 16. Delete Pool ledger config and wallets
                await PoolUtils.DeletePoolLedgerConfigAsync(PoolUtils.DEFAULT_POOL_NAME);

                await WalletUtils.DeleteWalletAsync(listenerWalletName, null);

                await WalletUtils.DeleteWalletAsync(trusteeWalletName, null);
            }

            Console.WriteLine("Agent sample -> completed");
        }
예제 #3
0
        public static async Task Execute()
        {
            var issuerWalletName = "issuerWallet";
            var proverWalletName = "proverWallet";

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

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

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

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

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

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

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

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

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

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

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

                            Debug.Assert(claimReq != null);

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

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

                            var claimJson = createClaimResult.ClaimJson;

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

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

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

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

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

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

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

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


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

                            var proof = JObject.Parse(proofJson);

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

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

                            Debug.Assert(valid == true);

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

                            await proverWallet.CloseAsync();

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

                await WalletUtils.DeleteWalletAsync(proverWalletName, null);

                await PoolUtils.DeletePoolLedgerConfigAsync(PoolUtils.DEFAULT_POOL_NAME);
            }

            Console.WriteLine("Anoncreds sample -> completed");
        }
예제 #4
0
        public static async Task Execute()
        {
            Console.WriteLine("Ledger sample -> started");

            var myWalletName    = "myWallet";
            var theirWalletName = "theirWallet";
            var trusteeSeed     = "000000000000000000000000Trustee1";

            try
            {
                // 1. Create ledger config from genesis txn file
                await PoolUtils.CreatePoolLedgerConfig();

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

                // 3. Create and Open Trustee Wallet
                await WalletUtils.CreateWalleatAsync(PoolUtils.DEFAULT_POOL_NAME, theirWalletName, "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 myWallet = await Wallet.OpenWalletAsync(myWalletName, null, null))
                        using (var trusteeWallet = await Wallet.OpenWalletAsync(theirWalletName, null, null))
                        {
                            //5. Create My Did
                            var createMyDidResult = await Signus.CreateAndStoreMyDidAsync(myWallet, "{}");

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

                            //6. Create Did from Trustee1 seed
                            var theirDidJson = string.Format("{{\"seed\":\"{0}\"}}", trusteeSeed);

                            var createTheirDidResult = await Signus.CreateAndStoreMyDidAsync(trusteeWallet, theirDidJson);

                            var trusteeDid = createTheirDidResult.Did;

                            //7. Build Nym Request
                            var nymRequest = await Ledger.BuildNymRequestAsync(trusteeDid, myDid, myVerkey, null, null);

                            //8. Trustee Sign Nym Request
                            var nymResponseJson = await Ledger.SignAndSubmitRequestAsync(pool, trusteeWallet, trusteeDid, nymRequest);

                            var nymResponse = JObject.Parse(nymResponseJson);

                            Debug.Assert(string.Equals(myDid, nymResponse["result"].Value <string>("dest")));
                            Debug.Assert(string.Equals(myVerkey, nymResponse["result"].Value <string>("verkey")));

                            //9. Close wallets and pool
                            await myWallet.CloseAsync();

                            await trusteeWallet.CloseAsync();

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

                await WalletUtils.DeleteWalletAsync(theirWalletName, null);

                await PoolUtils.DeletePoolLedgerConfigAsync(PoolUtils.DEFAULT_POOL_NAME);
            }

            Console.WriteLine("Ledger sample -> completed");
        }