public static async Task <bool> verifyDoctorProof(string proofJson)
        {
            string proofReqJson = getProofRequest();

            proofReqJson = proofReqJson.Replace(" ", string.Empty);
            proofReqJson = proofReqJson.Replace(Environment.NewLine, string.Empty);
            try
            {
                EmergencyDoctorCredentialModel model =
                    EmergencyDoctorCredentialModel.importFromJsonFile();
                // IOFacilitator io = new IOFacilitator();
                // DoctorCredDefInfoModel model = JsonConvert.DeserializeObject
                //     <DoctorCredDefInfoModel>(File.ReadAllText(
                //         io.getDoctorCredDefConfigPathAbs()));
                string schemas = "{";
                schemas += "\"" + model.schema_id + "\":" + model.schema_json;
                schemas += "}";
                string credDefs = "{";
                credDefs += "\"" + model.cred_def_id + "\":" + model.cred_def_json;
                credDefs += "}";

                bool result = await AnonCreds.VerifierVerifyProofAsync(proofReqJson, proofJson,
                                                                       schemas, credDefs, "{}", "{}");

                return(result);
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error: {e.Message}");
                return(false);
            }
        }
        /// <inheritdoc />
        public virtual async Task <bool> VerifyProofAsync(Wallet wallet, Pool pool, string proofRecId)
        {
            var proofRecord = await GetAsync(wallet, proofRecId);

            var proofObject = JsonConvert.DeserializeObject <Proof>(proofRecord.ProofJson);

            var schemas = await BuildSchemasAsync(pool,
                                                  proofObject.Identifiers
                                                  .Select(x => x.SchemaId)
                                                  .Where(x => x != null)
                                                  .Distinct());

            var definitions = await BuildCredentialDefinitionsAsync(pool,
                                                                    proofObject.Identifiers
                                                                    .Select(x => x.CredentialDefintionId)
                                                                    .Where(x => x != null)
                                                                    .Distinct());

            var revocationDefinitions = await BuildRevocationRegistryDefinitionsAsync(pool,
                                                                                      proofObject.Identifiers
                                                                                      .Select(x => x.RevocationRegistryId)
                                                                                      .Where(x => x != null)
                                                                                      .Distinct());

            var revocationRegistries = await BuildRevocationRegistryDetlasAsync(pool,
                                                                                proofObject.Identifiers
                                                                                .Where(x => x.RevocationRegistryId != null));

            return(await AnonCreds.VerifierVerifyProofAsync(proofRecord.RequestJson, proofRecord.ProofJson, schemas,
                                                            definitions, revocationDefinitions, revocationRegistries));
        }
예제 #3
0
        /// <inheritdoc />
        public virtual async Task <bool> VerifyProofAsync(IAgentContext agentContext, string proofRequestJson, string proofJson)
        {
            var proof = JsonConvert.DeserializeObject <PartialProof>(proofJson);

            var schemas = await BuildSchemasAsync(await agentContext.Pool,
                                                  proof.Identifiers
                                                  .Select(x => x.SchemaId)
                                                  .Where(x => x != null)
                                                  .Distinct());

            var definitions = await BuildCredentialDefinitionsAsync(await agentContext.Pool,
                                                                    proof.Identifiers
                                                                    .Select(x => x.CredentialDefintionId)
                                                                    .Where(x => x != null)
                                                                    .Distinct());

            var revocationDefinitions = await BuildRevocationRegistryDefinitionsAsync(await agentContext.Pool,
                                                                                      proof.Identifiers
                                                                                      .Select(x => x.RevocationRegistryId)
                                                                                      .Where(x => x != null)
                                                                                      .Distinct());

            var revocationRegistries = await BuildRevocationRegistryDetlasAsync(await agentContext.Pool,
                                                                                proof.Identifiers
                                                                                .Where(x => x.RevocationRegistryId != null));

            return(await AnonCreds.VerifierVerifyProofAsync(proofRequestJson, proofJson, schemas,
                                                            definitions, revocationDefinitions, revocationRegistries));
        }
        public async Task testVerifierVerifyProofWorksForInvalidProofJson()
        {
            var schemasJson        = new JObject(new JProperty(gvtSchemaId, JObject.Parse(gvtSchema))).ToString();
            var credentialDefsJson = new JObject(new JProperty(issuer1gvtCredDefId, JObject.Parse(credentialDef))).ToString();
            var revocRegDefsJson   = "{}";
            var revocRegsJson      = "{}";

            var proofJson = string.Format("{{\"proofs\":{{\"issuer1GvtCredential::277478db-bf57-42c3-8530-b1b13cfe0bfd\":{{\"primary_proof\":{{\"eq_proof\":{{\"revealed_attrs\":{{\"name\":\"1139481716457488690172217916278103335\"}},\"a_prime\":\"80401564260558483983794628158664845806393125691167675024527906210615204776868092566789307767601325086260531777605457298059939671624755239928848057947875953445797869574854365751051663611984607735255307096920094357120779812375573500489773454634756645206823074153240319316758529163584251907107473703779754778699279153037094140428648169418133281187947677937472972061954089873405836249023133445286756991574802740614183730141450546881449500189789970102133738133443822618072337620343825908790734460412932921199267304555521397418007577171242880211812703320270140386219809818196744216958369397014610013338422295772654405475023\",\"e\":\"31151798717381512709903464053695613005379725796031086912986270617392167764097422442809244590980303622977555221812111085160553241592792901\",\"v\":\"524407431684833626723631303096063196973911986967748096669183384949467719053669910411426601230736351335262754473490498825342793551112426427823428399937548938048089615644972537564428344526295733169691240937176356626523864731701111189536269488496019586818879697981955044502664124964896796783428945944075084807859935155837238670987272778459356531608865162828109489758902085206073584532002909678902616210042778963974064479140826712481297584040209095459963718975102750913306565864485279810056629704077428898739021040190774575868853629858297299392839284660771662690107106553362040805152261505268111067408422298806905178826507224233050991301274817252924123120887017757639206512015559321675322509820081151404696713509158685022511201565062671933414307463988209696457343022378430051265752251403461414881325357657438328740471164157220698425309006894962942640219890219594168419276308074677144722217081026358892787770650248878952483621\",\"m\":{{\"age\":\"10477979077744818183854012231360633424177093192344587159214818537659704987539982653663361680650769087122324965941845552897155693994859927792964720675888893623940580527766661802170\",\"sex\":\"15368219775809326116045200104269422566086585069798988383076685221700842794654771075432385446820819836777771517356551059931242867733879324915651894894695726945279462946826404864068\",\"height\":\"268172143999991481637372321419290603042446269013750825098514042757459298040087626745653681785038933035820421862976371452111736537699176931068992453946771945552540798204580069806\"}},\"m1\":\"119095745403940293668103184388411799541118279558928018597628509118163496000813590825371995586347826189221837428823000332905316924389185590810015031744029496470545254805993327676570037596326743185389101389800942263689809725968264069601565478411709555274081560719927118853299543998608664701485475703881376151770\",\"m2\":\"3166313665375815600922385342096456465402430622944571045536207479553790085339726549928012930073803465171492637049498407367742103524723152099973753540483894420905314750248333232361\"}},\"ge_proofs\":[{{\"u\":{{\"2\":\"6494171529848192644197417834173236605253723188808961394289041396341136802965710957759175642924978223517091081898946519122412445399638640485278379079647638538597635045303985779767\",\"0\":\"7739508859260491061487569748588091139318989278758566530899756574128579312557203413565436003310787878172471425996601979342157451689172171025305431595131816910273398879776841751855\",\"3\":\"9424758820140378077609053635383940574362083113571024891496206162696034958494400871955445981458978146571146602763357500412840538526390475379772903513687358736287298159312524034159\",\"1\":\"9011979414559555265454106061917684716953356440811838475257096756618761731111646531136628099710567973381801256908067529269805992222342928842825929421929485785888403149296320711642\"}},\"r\":{{\"DELTA\":\"2119857977629302693157808821351328058251440215802746362450951329352726877165815663955490999790457576333458830301801261754696823614762123890412904169206391143688952648566814660498520188221060505840151491403269696751525874990487604723445355651918681212361562384420233903265612599812725766212744963540390806334870022328290970137051148373040320927100063898502086531019924715927190306801273252711777648467224661735618842887006436195147540705753550974655689586750013569294343535843195025962867299786380033532422131203367401906988124836294104501525520053613392691214421562815044433237816093079784307397782961917892254668290115653012265908717124278607660504580036193346698672079435538219972121355893074219968755049500875222141\",\"2\":\"879097501989202140886939888802566536179834329508897124489020677433754766947767937608431979796722207676629625451150104784909666168153917345813160237337412296010679353735699663083287427507870244565918756969618964144516025526404618052053542009438548457492400344119561349471929199757453154204191407620539220514897529346602664135146454509169680801061111878075145734123580343470361019624175036825631373890661124315134340427076598351080893567995392248394683875116715114577054906406649006122102488431184007790011073389768061904597267545895265921673106871142463561948479668876241841045522543174660428236658891636170119227855493059358614089146415798861053408542832475696099851160385105386001523305465829676723036394820593263477\",\"0\":\"1724016272047416140958096373304304971004826284109046259544344355102178044512441391364907122486655755929044720001281832600729467778103556397960700809066582436321515744527550472324028227472294258045699756170293405547851344921626775854114063087070898499913846456795761213291925373770081490280103876827479351849800210782799381740073719081199000612284788683993320623339686128531187019125095700122135094060470612862911102824801065698176788174959069186600426519872015152034176356923049531650418553748519941342115963599848111324793380438600664408464987023646615003553912544410140730587797458882329021327455905737414352355326238028222782957735440607899424838572541602600159016542488644761584240884783618700311735467659132540546\",\"3\":\"2317535203964314926167241523636020444600002667629517624482931328850422196008281300859516069440995466415138723103558631951648519232327284208990029010060986032518946759289078833125920310350676484457972303378558158127406345804560689086460633931717939234025886786468170219981598030245042011840614339386724945679531091642132820284896626191109974537171662283750959028046143650291367908660204201563611944187723824430780626387525165408619587771059635528553832034409311888615502905143628507219523591091412192645348525327725381323865648645828460581593542176351568614465903523790649219812666979685223535464526901006270478687017672202058914176692964406859722580270696925877498058525086810338471380117323227744481903228027847825795\",\"1\":\"1119193929864813751243160041764170298897380522230946444206167281178657213260394833843687899872857393015947283159245092452814155776571829885921814072299525859857844030379558685168895306445277750249341844789101670896570226707650318347992386244538723699686941887792682779028216548922683313576597384354842537728667739985216662699631842296096507821667149950956179957306177525178260912379909156360834120816956949271530622510333943914411903103069247646327625753995178999023427645468623522280255892736633780185163496867644317005801241786702434621502492159672660131289312665511793827552317714835658019088880972220344126692027952749318018900669839090109361161616086319604439015851316798257015063653414161203599184730094765941653\"}},\"mj\":\"10477979077744818183854012231360633424177093192344587159214818537659704987539982653663361680650769087122324965941845552897155693994859927792964720675888893623940580527766661802170\",\"alpha\":\"46280660038407959140964701167450659223532556136388451390393713283900546119670373626221864441898929302821705811144923685080534692512705456699843367809872982836890616398604933641265111106644805368974824737276965928297120628041257593166650593538539384316563258781595629888673792430276007730792093088812056156937735120078929629310611907731935101448992312370312134173482115524436767558802102266208152808607480693236511858269018733175523724309089010048330044458187371675333889670055578652283806685440133357512406700879353713629795062705271430695988191782837658895477702634883214188598350625843489120361660836956958750828038278027538830855628653513539929730230905015331221220017847248793929813230252015802389329428995718799619565984669228143200627972926117282688854152516298117476837960100343260648687249027349308513966440386556698667484082658689\",\"t\":{{\"DELTA\":\"46814992964714978733007076702016837564951956529003697497847838781899848384824991374342901164708655443686022921583406187082133141084994843502230809550055933825660668160300304112671478218513259983054489597176651737200716259733573469298437873515151377206364940530308167934399245072298875358347931404742292788785586833114480704138718996633638362933821933388459210678374952072108333767698704767907612549860590824123780096225591372365712106060039646448181221691765233478768574198237963457485496438076793333937013217675591500849193742006533651525421426481898699626618796271544860105422331629265388419155909716261466161258430\",\"2\":\"59423006413504086085782234600502410213751379553855471973440165009200961757474676407242673622935614782362911290590560535490636029324125251850583605745046201217673654522625983661578962623803698461459190578519097656221453474955879823750445359506290522280566225253310030053812918275525607874059407284653434046369835156477189219911810464401689041140506062300317020407969423270374033482533711564673658146930272487464489365713112043565257807490520178903336328210031106311280471651300486164966423437275272281777742004535722142265580037959473078313965482591454009972765788975683031385823798895914265841131145707278751512534120\",\"0\":\"56510878078818710798555570103060159621941668074271797077206591818472978018558098567975838757566260370093327989369045722406190165972775356924844244889146946158949660988214890388299203816110339909687790860564719380865809705044646711632599987968183128514431910561478715003212633874423067294596323864121737000450543142072142652163818450299889830999149821558252183477517484127000480272695698860647674027831262149565273068850774090998356019534296579838685977022988536930596918054160990243868372150609770079720240227817149126735182138479851227052696211125454858584118346950878092387488482897777914362341820607560926173967363\",\"3\":\"63511079416489489495396586813126304469185174450150717746314545118902972011091412254834718868134635251731510764117528579641756327883640004345178347120290107941107152421856942264968771810665927914509411385404403747487862696526824127219640807008235054362138760656969613951620938020257273816713908815343872804442748694361381399025862438391456307852482826748664499083370705834755863016895566228300904018909174673301643617543662527772400085378252706897979609427451977654028887889811453690146157824251379525221390697200211891556653698308665831075787991412401737090471273439878635073797691350863566834141222438011402987450926\",\"1\":\"30348838247529448929141877305241172943867610065951047292188826263950046630912426030349276970628525991007036685038199133783991618544554063310358191845473212966131475853690378885426974792306638181168558731807811629973716711132134244797541560013139884391800841941607502149630914097258613821336239993125960064136287579351403225717114920758719152701696123905042695943045383536065833292374624566478931465135875411483860059753175449604448434619593495399051968638830805689355610877075130302742512428461286121237297212174164897833936610857614962734658136750299346971377383141235020438750748045568800723867413392427848651081274\"}},\"predicate\":{{\"attr_name\":\"age\",\"p_type\":\"GE\",\"value\":18}}}}]}},\"non_revoc_proof\":null}}}},\"aggregated_proof\":{{\"c_hash\":\"81135772044295974649282368084258333955993271555081206390568996949836231116301\",\"c_list\":[[2,124,231,47,189,36,247,160,61,220,165,35,97,165,203,185,133,253,81,239,67,127,156,49,189,16,140,30,177,161,221,54,154,0,127,143,98,212,114,193,188,85,206,171,198,140,9,192,10,254,218,120,201,182,40,141,80,35,81,148,204,192,41,5,186,33,50,77,211,163,124,130,32,219,193,167,79,43,181,76,19,249,53,79,70,221,205,36,180,50,120,255,161,227,196,204,71,106,221,131,220,7,73,86,128,208,48,58,123,63,82,24,170,141,143,56,221,96,151,108,105,38,185,243,224,112,177,101,195,87,208,201,39,123,165,125,92,104,234,188,54,92,31,158,178,152,52,205,26,156,237,241,23,15,76,220,168,32,175,230,157,197,225,70,57,237,8,81,13,17,95,70,143,56,162,223,203,8,48,153,51,51,118,116,32,139,187,222,146,86,165,111,125,107,203,18,212,28,168,22,62,69,204,207,122,148,25,30,92,120,83,214,116,221,204,120,230,70,128,139,181,110,69,93,253,240,69,16,113,224,246,41,142,0,83,237,186,4,50,156,206,199,89,74,96,168,249,240,101,16,103,234,162,219,52,218,207],[1,191,167,2,151,36,61,136,184,172,120,86,127,88,109,119,56,21,167,171,217,221,24,64,246,237,255,152,81,183,201,191,59,234,213,101,254,91,33,205,120,71,215,144,160,243,145,109,19,151,241,46,135,132,50,143,219,207,197,35,89,103,83,212,96,83,222,101,55,57,220,161,252,115,39,62,46,160,30,138,221,89,125,66,114,150,5,95,63,10,55,107,102,73,40,69,41,6,57,0,64,226,152,66,181,149,251,50,28,53,18,26,221,5,188,67,125,184,190,200,56,92,132,201,242,211,37,2,43,6,146,88,228,120,204,190,4,118,134,106,118,110,249,145,175,165,116,197,200,183,207,215,197,79,207,203,29,182,231,151,248,233,107,41,79,234,250,27,33,33,107,102,240,47,37,230,243,185,93,192,52,31,73,211,11,173,150,92,194,154,172,247,221,206,129,85,193,105,172,140,201,40,240,200,28,94,1,96,204,175,113,170,46,134,229,111,215,208,237,252,84,50,249,41,214,79,38,194,23,212,7,164,153,217,23,252,32,114,145,58,189,118,104,131,84,184,115,175,199,227,219,117,23,113,113,180,3],[240,104,187,71,84,144,129,123,12,181,215,233,27,55,56,54,94,57,17,42,111,42,112,234,192,23,226,103,118,198,189,175,175,1,102,64,128,100,221,201,134,106,83,239,69,43,150,172,95,206,145,224,207,239,39,193,30,200,90,125,175,125,59,47,250,224,193,21,64,112,101,131,128,249,96,165,73,33,174,64,69,252,209,158,130,53,23,158,217,173,69,51,12,145,70,174,15,206,13,181,50,246,50,110,223,65,250,44,39,33,8,47,169,242,147,3,190,164,110,20,68,5,142,133,38,198,151,161,167,0,219,128,126,120,190,23,153,22,250,78,114,241,252,181,74,142,65,123,225,153,75,159,78,84,28,110,203,105,231,238,75,138,121,233,75,163,221,69,106,143,1,217,251,43,147,252,189,122,19,124,189,180,206,91,165,199,41,172,233,102,14,91,162,254,16,142,60,230,39,200,208,236,101,69,101,152,233,217,100,206,31,120,211,191,90,56,205,40,180,120,47,210,224,86,153,34,86,237,204,11,183,227,0,224,15,201,32,228,4,210,43,156,68,246,137,150,103,197,191,150,155,181,78,5,134,58],[1,214,184,139,205,251,132,131,8,186,140,58,211,242,134,120,121,253,128,192,10,252,172,101,44,26,119,56,212,8,248,71,19,96,59,12,233,191,63,187,217,35,191,160,127,247,189,247,229,111,252,101,126,10,142,252,238,215,211,137,137,164,114,186,255,199,183,50,103,9,158,63,134,140,162,154,188,109,52,31,92,78,38,228,0,60,225,100,239,88,114,95,48,71,7,117,168,45,45,177,178,62,87,197,98,174,123,249,26,237,179,12,63,182,46,218,183,148,163,222,179,159,146,56,142,190,122,100,211,6,86,237,10,7,111,186,27,66,95,252,108,247,203,1,111,60,13,218,104,63,128,125,197,11,201,138,33,122,37,31,163,123,120,132,65,122,208,60,80,87,113,183,28,31,74,106,18,79,52,245,113,184,94,202,72,223,8,128,209,43,77,237,119,208,255,144,26,76,223,77,177,131,237,49,150,251,53,150,115,33,254,237,185,15,140,234,205,99,248,252,171,245,192,104,151,194,190,186,249,180,246,9,169,165,0,221,7,107,39,67,58,178,176,99,212,40,247,49,127,7,94,5,170,65,154,28,104],[1,247,26,202,244,120,131,95,151,52,56,38,141,232,178,50,61,45,235,61,12,68,11,180,174,222,110,211,141,253,198,204,248,192,40,99,237,1,45,170,79,208,3,13,135,89,195,65,3,228,224,146,181,198,14,79,78,237,168,81,108,151,68,12,88,242,120,200,120,193,253,51,167,140,43,175,59,18,160,190,233,21,213,135,162,76,38,48,163,110,155,197,97,93,211,183,95,42,172,249,98,59,161,136,70,39,142,48,242,44,154,103,186,161,214,215,0,254,166,150,111,71,242,102,209,125,25,65,144,223,211,137,223,239,50,96,185,171,120,155,171,98,204,23,102,253,68,141,91,240,127,170,199,249,217,165,164,37,174,212,159,232,140,196,216,140,205,102,84,104,220,223,9,249,75,245,78,157,245,203,235,154,73,34,77,12,227,138,93,105,178,114,255,210,88,216,202,64,69,128,220,211,113,51,15,185,103,236,52,187,49,29,162,20,35,21,65,188,33,46,11,172,59,15,221,36,33,213,14,121,36,218,76,80,97,197,83,64,145,73,194,43,233,144,251,86,112,209,230,67,234,116,172,219,123,50,46],[1,114,216,159,37,214,198,117,230,153,15,176,95,20,29,134,179,207,209,35,101,193,47,54,130,141,78,213,54,167,31,73,105,177,129,135,6,135,45,107,103,16,133,187,74,217,42,40,1,214,60,70,78,245,86,82,150,75,91,235,181,249,129,147,202,15,86,250,222,240,203,236,102,39,53,147,79,178,124,184,97,73,65,136,74,29,219,182,83,167,221,203,32,200,243,130,65,234,133,181,203,35,86,21,123,170,74,174,5,132,1,149,77,141,158,193,249,130,37,53,253,234,228,144,66,152,232,246,26,193,6,53,139,45,231,173,115,87,89,61,197,9,96,73,229,189,49,44,203,214,156,139,58,153,77,13,90,35,157,130,184,150,161,69,145,157,4,206,52,216,227,233,113,202,54,154,153,100,83,97,135,88,197,227,42,52,28,221,91,117,56,183,198,102,231,37,232,226,136,142,115,218,175,45,221,143,130,215,184,39,102,172,126,253,152,108,254,241,17,98,70,223,191,138,251,227,243,32,180,190,223,69,135,0,97,105,115,189,221,134,26,159,32,210,172,233,7,65,238,77,203,159,181,188,203,159,190]]}}}},\"requested_proof\":{{\"revealed_attrs\":{{\"attr1_referent\":[\"issuer1GvtCredential::277478db-bf57-42c3-8530-b1b13cfe0bfd\",\"Alex\",\"1139481716457488690172217916278103335\"]}},\"unrevealed_attrs\":{{}},\"self_attested_attrs\":{{}},\"predicates\":{{\"predicate1_referent\":\"issuer1GvtCredential::277478db-bf57-42c3-8530-b1b13cfe0bfd\"}},\"identifiers\":{{\"issuer1GvtCredential::277478db-bf57-42c3-8530-b1b13cfe0bfd\":{{\"issuer_did\":\"NcYxiDXkpYi6ov5FcYDi1e\",\"schema_key\":{0}}}}}}}", gvtSchemaId);

            var ex = await Assert.ThrowsExceptionAsync <InvalidStructureException>(() =>
                                                                                   AnonCreds.VerifierVerifyProofAsync(verifierProofRequest, proofJson, schemasJson, credentialDefsJson, revocRegDefsJson, revocRegsJson)
                                                                                   );
        }
        /// <inheritdoc />
        public virtual async Task <bool> VerifyProofAsync(IAgentContext agentContext, string proofRequestJson, string proofJson, bool validateEncoding = true)
        {
            var proof        = JsonConvert.DeserializeObject <PartialProof>(proofJson);
            var proofRequest = proofRequestJson.ToObject <ProofRequest>();

            // If any values are revealed, validate encoding
            // against expected values
            if (validateEncoding && proof.RequestedProof.RevealedAttributes != null)
            {
                foreach (var attribute in proof.RequestedProof.RevealedAttributes)
                {
                    if (!CredentialUtils.CheckValidEncoding(attribute.Value.Raw, attribute.Value.Encoded))
                    {
                        throw new AriesFrameworkException(ErrorCode.InvalidProofEncoding,
                                                          $"The encoded value for '{attribute.Key}' is invalid. " +
                                                          $"Expected '{CredentialUtils.GetEncoded(attribute.Value.Raw)}'. " +
                                                          $"Actual '{attribute.Value.Encoded}'");
                    }
                }
            }

            var schemas = await BuildSchemasAsync(agentContext,
                                                  proof.Identifiers
                                                  .Select(x => x.SchemaId)
                                                  .Where(x => x != null)
                                                  .Distinct());

            var definitions = await BuildCredentialDefinitionsAsync(agentContext,
                                                                    proof.Identifiers
                                                                    .Select(x => x.CredentialDefintionId)
                                                                    .Where(x => x != null)
                                                                    .Distinct());

            var revocationDefinitions = await BuildRevocationRegistryDefinitionsAsync(agentContext,
                                                                                      proof.Identifiers
                                                                                      .Select(x => x.RevocationRegistryId)
                                                                                      .Where(x => x != null)
                                                                                      .Distinct());

            var revocationRegistries = await BuildRevocationRegistriesAsync(
                agentContext,
                proof.Identifiers.Where(x => x.RevocationRegistryId != null));

            return(await AnonCreds.VerifierVerifyProofAsync(
                       proofRequestJson,
                       proofJson,
                       schemas,
                       definitions,
                       revocationDefinitions,
                       revocationRegistries));
        }
        public async Task TestVerifierVerifyProofWorksForCorrectProof()
        {
            var schemasJson        = new JObject(new JProperty(gvtSchemaId, JObject.Parse(gvtSchema))).ToString();
            var credentialDefsJson = new JObject(new JProperty(issuer1gvtCredDefId, JObject.Parse(credentialDef))).ToString();
            var revocRegDefsJson   = "{}";
            var revocRegsJson      = "{}";

            proofJson = proofJson.Replace("\"schema_id\": \"\"", string.Format("\"schema_id\": \"{0}\"", gvtSchemaId));
            proofJson = proofJson.Replace("\"cred_def_id\": \"\"", string.Format("\"cred_def_id\": \"{0}\"", issuer1gvtCredDefId));

            var valid = await AnonCreds.VerifierVerifyProofAsync(verifierProofRequest, proofJson, schemasJson, credentialDefsJson, revocRegDefsJson, revocRegsJson);

            Assert.IsTrue(valid);
        }
예제 #7
0
        public async Task TestAnoncredsWorksForClaimRevokedAfterProofCreated()
        {
            //9. Prover gets Claims for Proof Request
            var claimsJson = await AnonCreds.ProverGetCredentialsForProofReqAsync(_issuerWallet, _proofReqJson);

            var claims         = JObject.Parse(claimsJson);
            var claimsForAttr1 = claims["attrs"]["attr1_referent"];
            var claim          = claimsForAttr1[0];
            var claimUuid      = claim.Value <string>("referent");

            //10. Prover create Proof
            var requestedClaimsJson = string.Format(_requestedClaimsJsonTemplate, claimUuid);

            var schemasJson    = string.Format("{{\"{0}\":{1}}}", claimUuid, schema);
            var claimDefsJson  = string.Format("{{\"{0}\":{1}}}", claimUuid, _claimDefJson);
            var revocRegsJsons = string.Format("{{\"{0}\":{1}}}", claimUuid, _claimResult.RevocRegDeltaJson);

            var proofJson = await AnonCreds.ProverCreateProofAsync(
                _issuerWallet,
                _proofReqJson,
                requestedClaimsJson,
                schemasJson,
                masterSecretName,
                claimDefsJson,
                revocRegsJsons);

            //11. Issuer revoke prover claim
            var revocRegUpdateJson = await AnonCreds.IssuerRevokeCredentialAsync(
                _issuerWallet,
                issuerDid,
                1,
                1);

            //12. Verifier verify proof
            var updatedRevocRegsJsons = string.Format("{{\"{0}\":{1}}}", claimUuid, revocRegUpdateJson);

            var valid = await AnonCreds.VerifierVerifyProofAsync(
                _proofReqJson,
                proofJson,
                schemasJson,
                claimDefsJson,
                updatedRevocRegsJsons);

            Assert.IsFalse(valid);
        }
        public async Task TestVerifierVerifyProofWorksForProofDoesNotCorrespondToProofRequest()
        {
            var schemasJson        = new JObject(new JProperty(gvtSchemaId, JObject.Parse(gvtSchema))).ToString();
            var credentialDefsJson = new JObject(new JProperty(issuer1gvtCredDefId, JObject.Parse(credentialDef))).ToString();
            var revocRegDefsJson   = "{}";
            var revocRegsJson      = "{}";

            proofJson = proofJson.Replace("\"schema_id\": \"\"", string.Format("\"schema_id\": \"{0}\"", gvtSchemaId));
            proofJson = proofJson.Replace("\"cred_def_id\": \"\"", string.Format("\"cred_def_id\": \"{0}\"", issuer1gvtCredDefId));

            var proofRequest = "{\"nonce\":\"123432421212\",\n" +
                               "               \"name\":\"proof_req_1\",\n" +
                               "               \"version\":\"0.1\",\n" +
                               "               \"requested_attributes\":{\"attr1_referent\":{\"name\":\"sex\"}},\n" +
                               "               \"requested_predicates\":{\"predicate1_referent\":{\"name\":\"height\",\"p_type\":\">=\",\"p_value\":180}}\n" +
                               "              }";

            var ex = await Assert.ThrowsExceptionAsync <InvalidStructureException>(() =>
                                                                                   AnonCreds.VerifierVerifyProofAsync(proofRequest, proofJson, schemasJson, credentialDefsJson, revocRegDefsJson, revocRegsJson)
                                                                                   );
        }
예제 #9
0
        /// <inheritdoc />
        public virtual async Task <bool> VerifyProofAsync(IAgentContext agentContext, string proofRecId)
        {
            var proofRecord = await GetAsync(agentContext, proofRecId);

            if (proofRecord.State != ProofState.Accepted)
            {
                throw new AgentFrameworkException(ErrorCode.RecordInInvalidState,
                                                  $"Proof record state was invalid. Expected '{ProofState.Accepted}', found '{proofRecord.State}'");
            }

            var proofObject = JsonConvert.DeserializeObject <Proof>(proofRecord.ProofJson);

            var schemas = await BuildSchemasAsync(agentContext.Pool,
                                                  proofObject.Identifiers
                                                  .Select(x => x.SchemaId)
                                                  .Where(x => x != null)
                                                  .Distinct());

            var definitions = await BuildCredentialDefinitionsAsync(agentContext.Pool,
                                                                    proofObject.Identifiers
                                                                    .Select(x => x.CredentialDefintionId)
                                                                    .Where(x => x != null)
                                                                    .Distinct());

            var revocationDefinitions = await BuildRevocationRegistryDefinitionsAsync(agentContext.Pool,
                                                                                      proofObject.Identifiers
                                                                                      .Select(x => x.RevocationRegistryId)
                                                                                      .Where(x => x != null)
                                                                                      .Distinct());

            var revocationRegistries = await BuildRevocationRegistryDetlasAsync(agentContext.Pool,
                                                                                proofObject.Identifiers
                                                                                .Where(x => x.RevocationRegistryId != null));

            return(await AnonCreds.VerifierVerifyProofAsync(proofRecord.RequestJson, proofRecord.ProofJson, schemas,
                                                            definitions, revocationDefinitions, revocationRegistries));
        }
예제 #10
0
        public async Task TestAnonCredsDemo()
        {
            //4. Issuer create ClaimDef
            var schemaJson = "{\n" +
                             "                    \"seqNo\":1,\n" +
                             "                    \"data\": {\n" +
                             "                        \"name\":\"gvt\",\n" +
                             "                        \"version\":\"1.0\",\n" +
                             "                        \"attr_names\":[\"age\",\"sex\",\"height\",\"name\"]\n" +
                             "                    }\n" +
                             "                }";
            var issuerDid = "NcYxiDXkpYi6ov5FcYDi1e";

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

            Assert.IsNotNull(claimDef);

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

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

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

            var claimOffersObject = JArray.Parse(claimOffersJson);

            Assert.AreEqual(claimOffersObject.Count, 1);

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

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

            Assert.IsNotNull(claimReq);

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

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

            Assert.IsNotNull(createClaimResult);
            var claimJson = createClaimResult.ClaimJson;

            //10. Prover store Claim
            await AnonCreds.ProverStoreClaimAsync(_proverWallet, claimJson);

            //11. Prover gets Claims for Proof Request
            var proofRequestJson = "{\n" +
                                   "                          \"nonce\":\"123432421212\",\n" +
                                   "                          \"name\":\"proof_req_1\",\n" +
                                   "                          \"version\":\"0.1\",\n" +
                                   "                          \"requested_attrs\":{\"attr1_referent\":{\"name\":\"name\",\"restrictions\":[{\"schema_seq_no\":1}]},\n" +
                                   "                                                \"attr2_referent\":{\"name\":\"sex\",\"restrictions\":[{\"schema_seq_no\":1}]}},\n" +
                                   "                          \"requested_predicates\":{\"predicate1_referent\":{\"attr_name\":\"age\",\"p_type\":\">=\",\"value\":18}}\n" +
                                   "                  }";

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

            Assert.IsNotNull(claimsForProofJson);

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

            Assert.AreEqual(claimsForAttribute1.Count, 1);
            Assert.AreEqual(claimsForAttribute2.Count, 1);
            Assert.AreEqual(claimsForPredicate.Count, 1);

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

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

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


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

            Assert.IsNotNull(proofJson);

            var proof = JObject.Parse(proofJson);

            //13. Verifier verify Proof
            Assert.AreEqual("Alex",
                            proof["requested_proof"]["revealed_attrs"]["attr1_referent"][1]);

            Assert.IsNotNull(proof["requested_proof"]["unrevealed_attrs"].Value <string>("attr2_referent"));

            Assert.AreEqual(selfAttestedValue, proof["requested_proof"]["self_attested_attrs"].Value <string>("self1"));

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

            Assert.IsTrue(valid);
        }
예제 #11
0
        public async Task TestAnonCredsWorksForSingleIssuerSingleProverMultipleClaims()
        {
            var issuerDid = "NcYxiDXkpYi6ov5FcYDi1e";

            //1. Issuer create ClaimDef
            var gvtSchemaJson = "{\n" +
                                "                    \"seqNo\":1,\n" +
                                "                    \"data\": {\n" +
                                "                        \"name\":\"gvt\",\n" +
                                "                        \"version\":\"1.0\",\n" +
                                "                        \"attr_names\":[\"age\",\"sex\",\"height\",\"name\"]\n" +
                                "                    }\n" +
                                "                }";

            var gvtClaimDef = await AnonCreds.IssuerCreateAndStoreClaimDefAsync(_issuerWallet, issuerDid, gvtSchemaJson, null, false);

            //2. Issuer create ClaimDef
            var xyzSchemaJson = "{\n" +
                                "                    \"seqNo\":2,\n" +
                                "                    \"data\": {\n" +
                                "                        \"name\":\"xyz\",\n" +
                                "                        \"version\":\"1.0\",\n" +
                                "                        \"attr_names\":[\"status\",\"period\"]\n" +
                                "                    }\n" +
                                "                }";

            var xyzClaimDef = await AnonCreds.IssuerCreateAndStoreClaimDefAsync(_issuerWallet, issuerDid, xyzSchemaJson, null, false);

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

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

            //5. Prover store Claim Offer received from Issuer
            var claimOffer2 = string.Format("{{\"issuer_did\":\"{0}\", \"schema_seq_no\":{1}}}", issuerDid, 2);
            await AnonCreds.ProverStoreClaimOfferAsync(_proverWallet, claimOffer2);

            //6. Prover get Claim Offers
            var claimOffersJson = await AnonCreds.ProverGetClaimOffersAsync(_proverWallet, "{}");

            var claimOffersObject = JArray.Parse(claimOffersJson);

            Assert.AreEqual(2, claimOffersObject.Count);

            var claimOfferObj1 = claimOffersObject[0];
            var claimOfferObj2 = claimOffersObject[1];

            var gvtClaimOffer = claimOfferObj1.Value <int>("schema_seq_no") == 1 ? claimOfferObj1.ToString() : claimOfferObj2.ToString();
            var xyzClaimOffer = claimOfferObj1.Value <int>("schema_seq_no") == 2 ? claimOfferObj1.ToString() : claimOfferObj2.ToString();


            //7. Prover create ClaimReq for GVT Claim Offer
            var proverDid   = "BzfFCYk";
            var gvtClaimReq = await AnonCreds.ProverCreateAndStoreClaimReqAsync(_proverWallet, proverDid, gvtClaimOffer, gvtClaimDef, masterSecret);

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

            var gvtCreateClaimResult = await AnonCreds.IssuerCreateClaimAsync(_issuerWallet, gvtClaimReq, gvtClaimAttributesJson, -1);

            var gvtClaimJson = gvtCreateClaimResult.ClaimJson;

            //9. Prover store Claim
            await AnonCreds.ProverStoreClaimAsync(_proverWallet, gvtClaimJson);

            //10. Prover create ClaimReq for GVT Claim Offer
            var xyzClaimReq = await AnonCreds.ProverCreateAndStoreClaimReqAsync(_proverWallet, proverDid, xyzClaimOffer, xyzClaimDef, masterSecret);

            //11. Issuer create Claim
            var xyzClaimAttributesJson = "{\n" +
                                         "               \"status\":[\"partial\",\"51792877103171595686471452153480627530895\"],\n" +
                                         "               \"period\":[\"8\",\"8\"]\n" +
                                         "        }";

            var xyzCreateClaimResult = await AnonCreds.IssuerCreateClaimAsync(_issuerWallet, xyzClaimReq, xyzClaimAttributesJson, -1);

            var xyzClaimJson = xyzCreateClaimResult.ClaimJson;

            //12. Prover store Claim
            await AnonCreds.ProverStoreClaimAsync(_proverWallet, xyzClaimJson);

            //13. Prover gets Claims for Proof Request
            var proofRequestJson = "{\n" +
                                   "                          \"nonce\":\"123432421212\",\n" +
                                   "                          \"name\":\"proof_req_1\",\n" +
                                   "                          \"version\":\"0.1\",\n" +
                                   "                          \"requested_attrs\":{\"attr1_referent\":{\"name\":\"name\",\"restrictions\":[{\"schema_seq_no\":1}]}},\n" +
                                   "                          \"requested_predicates\":{\"predicate1_referent\":{\"attr_name\":\"age\",\"p_type\":\">=\",\"value\":18}," +
                                   "                                                    \"predicate2_referent\":{\"attr_name\":\"period\",\"p_type\":\">=\",\"value\":5}}\n" +
                                   "                  }";


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

            Assert.IsNotNull(claimsForProofJson);

            var claimsForProof      = JObject.Parse(claimsForProofJson);
            var claimsForAttribute1 = (JArray)claimsForProof["attrs"]["attr1_referent"];
            var claimsForPredicate1 = (JArray)claimsForProof["predicates"]["predicate1_referent"];
            var claimsForPredicate2 = (JArray)claimsForProof["predicates"]["predicate2_referent"];

            Assert.AreEqual(claimsForAttribute1.Count, 1);
            Assert.AreEqual(claimsForPredicate1.Count, 1);
            Assert.AreEqual(claimsForPredicate2.Count, 1);

            var claimUuidForAttr1      = claimsForAttribute1[0].Value <string>("referent");
            var claimUuidForPredicate1 = claimsForPredicate1[0].Value <string>("referent");
            var claimUuidForPredicate2 = claimsForPredicate2[0].Value <string>("referent");

            //14. Prover create Proof
            var requestedClaimsJson = string.Format("{{\n" +
                                                    "                                          \"self_attested_attributes\":{{}},\n" +
                                                    "                                          \"requested_attrs\":{{\"attr1_referent\":[\"{0}\", true]}},\n" +
                                                    "                                          \"requested_predicates\":{{\"predicate1_referent\":\"{1}\"," +
                                                    "                                                                    \"predicate2_referent\":\"{2}\"}}\n" +
                                                    "                                        }}", claimUuidForAttr1, claimUuidForPredicate1, claimUuidForPredicate2);

            var schemasJson   = string.Format("{{\"{0}\":{1}, \"{2}\":{3}}}", claimUuidForAttr1, gvtSchemaJson, claimUuidForPredicate2, xyzSchemaJson);
            var claimDefsJson = string.Format("{{\"{0}\":{1}, \"{2}\":{3}}}", claimUuidForAttr1, gvtClaimDef, claimUuidForPredicate2, xyzClaimDef);

            var revocRegsJson = "{}";

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

            Assert.IsNotNull(proofJson);

            var proof = JObject.Parse(proofJson);

            //15. Verifier verify Proof
            Assert.AreEqual("Alex",
                            proof["requested_proof"]["revealed_attrs"]["attr1_referent"][1]);

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

            Assert.IsTrue(valid);
        }
예제 #12
0
        public async Task TestVerifyProofWorksForProofDoesNotCorrespondToProofRequest()
        {
            // 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)
                                                                                   );
        }
예제 #13
0
        public static async Task Execute()
        {
            Console.WriteLine("Anoncreds sample -> started");

            var issuerWalletName = "issuerWallet";
            var proverWalletName = "proverWallet";

            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(PoolUtils.DEFAULT_POOL_NAME, issuerWalletName, "default", null, issuerWalletCredentials);

                //3. Prover Create and Open Wallet
                await WalletUtils.CreateWalletAsync(PoolUtils.DEFAULT_POOL_NAME, proverWalletName, "default", null, 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(issuerWalletName, null, issuerWalletCredentials))
                        using (var proverWallet = await Wallet.OpenWalletAsync(proverWalletName, null, 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\":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, issuerWalletCredentials);

                await WalletUtils.DeleteWalletAsync(proverWalletName, proverWalletCredentials);

                await PoolUtils.DeletePoolLedgerConfigAsync(PoolUtils.DEFAULT_POOL_NAME);
            }

            Console.WriteLine("Anoncreds sample -> completed");
        }
        public async Task TestIssuerRevokeProofWorks()
        {
            // Create wallet, get wallet handle
            var walletConfig = JsonConvert.SerializeObject(new { id = "revocationWallet" });
            await Wallet.CreateWalletAsync(walletConfig, CREDENTIALS);

            var wallet = await Wallet.OpenWalletAsync(walletConfig, CREDENTIALS);

            // Issuer create Schema
            var createSchemaResult = await AnonCreds.IssuerCreateSchemaAsync(issuerDid, gvtSchemaName, schemaVersion, gvtSchemaAttributes);

            var schemaId   = createSchemaResult.SchemaId;
            var schemaJson = createSchemaResult.SchemaJson;

            // Issuer create issuer1GvtCredential definition
            var revocationCredentialDefConfig = "{\"support_revocation\":true}";
            var createCredentialDefResult     = await AnonCreds.IssuerCreateAndStoreCredentialDefAsync(wallet, issuerDid, schemaJson, tag, null, revocationCredentialDefConfig);

            var credDefId   = createCredentialDefResult.CredDefId;
            var credDefJson = createCredentialDefResult.CredDefJson;

            // Issuer create revocation registry
            var tailsWriter = await BlobStorage.OpenWriterAsync("default", tailsWriterConfig);

            var revRegConfig       = "{\"issuance_type\":null,\"max_cred_num\":5}";
            var createRevRegResult = await AnonCreds.IssuerCreateAndStoreRevocRegAsync(wallet, issuerDid, null, tag, credDefId, revRegConfig, tailsWriter);

            var revRegId  = createRevRegResult.RevRegId;
            var revRegDef = createRevRegResult.RevRegDefJson;

            // Prover create Master Secret
            await AnonCreds.ProverCreateMasterSecretAsync(wallet, masterSecretId);

            // Issuer create Credential Offer
            var credOfferJson = await AnonCreds.IssuerCreateCredentialOfferAsync(wallet, credDefId);

            // Prover create Credential Request
            var createCredReqResult =
                await AnonCreds.ProverCreateCredentialReqAsync(wallet, proverDid, credOfferJson, credDefJson, masterSecretId);

            var credentialReqJson         = createCredReqResult.CredentialRequestJson;
            var credentialReqMetadataJson = createCredReqResult.CredentialRequestMetadataJson;

            // Issuer open TailsReader
            var blobReader = await BlobStorage.OpenReaderAsync("default", tailsWriterConfig);

            //9. Issuer create Credential
            var createCredentialResult = await AnonCreds.IssuerCreateCredentialAsync(wallet, credOfferJson, credentialReqJson, gvtCredentialValuesJson, revRegId, blobReader);

            var credJson    = createCredentialResult.CredentialJson;
            var credRevocId = createCredentialResult.RevocId;
            var revRegDelta = createCredentialResult.RevocRegDeltaJson;

            // Prover create RevocationState
            var timestamp    = 100;
            var revStateJson = await AnonCreds.CreateRevocationStateAsync(blobReader, revRegDef, revRegDelta, timestamp, credRevocId);

            // Prover store received Credential
            await AnonCreds.ProverStoreCredentialAsync(wallet, credentialId1, credentialReqMetadataJson, credJson, credDefJson, revRegDef);

            // Prover gets Credentials for Proof Request
            var credentialsJson = await AnonCreds.ProverGetCredentialsForProofReqAsync(wallet, proofRequest);

            var credentials         = JObject.Parse(credentialsJson);
            var credentialsForAttr1 = credentials["attrs"]["attr1_referent"];

            var credentialUuid = credentialsForAttr1[0]["cred_info"]["referent"];

            // Prover create 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}}}}}" +
                                                         "}}", credentialUuid, timestamp, credentialUuid, timestamp);

            var schemasJson        = JObject.Parse(string.Format("{{\"{0}\":{1}}}", schemaId, schemaJson)).ToString();
            var credentialDefsJson = JObject.Parse(string.Format("{{\"{0}\":{1}}}", credDefId, credDefJson)).ToString();
            var revStatesJson      = JObject.Parse(string.Format("{{\"{0}\":{{\"{1}\":{2}}}}}", revRegId, timestamp, revStateJson)).ToString();

            var proofJson = await AnonCreds.ProverCreateProofAsync(wallet, proofRequest, requestedCredentialsJson, masterSecretId, schemasJson, credentialDefsJson, revStatesJson);

            var proof = JObject.Parse(proofJson);

            // Issuer revoke Credential
            revRegDelta = await AnonCreds.IssuerRevokeCredentialAsync(wallet, blobReader, revRegId, credRevocId);

            // Verifier verify proof
            var revealedAttr1 = proof["requested_proof"]["revealed_attrs"]["attr1_referent"];

            Assert.AreEqual("Alex", revealedAttr1["raw"]);

            var revRegDefsJson = JObject.Parse(string.Format("{{\"{0}\":{1}}}", revRegId, revRegDef)).ToString();
            var revRegs        = JObject.Parse(string.Format("{{\"{0}\":{{\"{1}\":{2}}}}}", revRegId, timestamp, revRegDelta)).ToString();

            var valid = await AnonCreds.VerifierVerifyProofAsync(proofRequest, proofJson, schemasJson, credentialDefsJson, revRegDefsJson, revRegs);

            Assert.IsFalse(valid);

            //// Close and Delete Wallet
            await wallet.CloseAsync();

            await Wallet.DeleteWalletAsync(walletConfig, CREDENTIALS);
        }
예제 #15
0
        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);
        }
예제 #16
0
        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);
        }
예제 #17
0
        public async Task TestVerifyProofWorksForProofDoesNotCorrespondToProofRequest()
        {
            //1. Issuer create ClaimDef
            var schemaJson = "{\n" +
                             "                    \"seqNo\":1,\n" +
                             "                    \"data\": {\n" +
                             "                        \"name\":\"gvt\",\n" +
                             "                        \"version\":\"1.0\",\n" +
                             "                        \"keys\":[\"age\",\"sex\",\"height\",\"name\"]\n" +
                             "                    }\n" +
                             "                }";
            var issuerDid = "NcYxiDXkpYi6ov5FcYDi1e";

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

            Assert.IsNotNull(claimDef);

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

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

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

            var claimOffersObject = JArray.Parse(claimOffersJson);

            Assert.AreEqual(claimOffersObject.Count, 1);

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

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

            Assert.IsNotNull(claimReq);

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

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

            Assert.IsNotNull(createClaimResult);
            var claimJson = createClaimResult.ClaimJson;

            //7. Prover store Claim
            await AnonCreds.ProverStoreClaimAsync(_proverWallet, claimJson);

            //8. Prover gets Claims for Proof Request
            var proofRequestJson = "{\n" +
                                   "                          \"nonce\":\"123432421212\",\n" +
                                   "                          \"name\":\"proof_req_1\",\n" +
                                   "                          \"version\":\"0.1\",\n" +
                                   "                          \"requested_attrs\":{\"attr1_uuid\":{\"schema_seq_no\":1,\"name\":\"name\"}},\n" +
                                   "                          \"requested_predicates\":{}\n" +
                                   "                  }";

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

            Assert.IsNotNull(claimsForProofJson);

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


            Assert.AreEqual(claimsForAttribute1.Count, 1);

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

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

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

            //TODO: Not sure why this call is failing...
            var proofJson = await AnonCreds.ProverCreateProofAsync(_proverWallet, proofRequestJson, requestedClaimsJson, schemasJson,
                                                                   masterSecret, claimDefsJson, revocRegsJson);

            Assert.IsNotNull(proofJson);

            var proof = JObject.Parse(proofJson);

            //10. Verifier verify Proof
            Assert.AreEqual("Alex",
                            proof["requested_proof"]["revealed_attrs"]["attr1_uuid"][1]);


            Assert.AreEqual(selfAttestedValue, proof["requested_proof"]["self_attested_attrs"].Value <string>("self1"));

            proofRequestJson = "{\n" +
                               "                            \"nonce\":\"123432421212\",\n" +
                               "                        \"name\":\"proof_req_1\",\n" +
                               "                        \"version\":\"0.1\",\n" +
                               "                    \"requested_attrs\":{\"attr1_uuid\":{\"schema_seq_no\":1,\"name\":\"name\"}},\n" +
                               "                    \"requested_predicates\":{\"predicate1_uuid\":{\"attr_name\":\"age\",\"p_type\":\"GE\",\"value\":18}\n" +
                               "           }";

            var ex = await Assert.ThrowsExceptionAsync <IndyException>(() =>
                                                                       AnonCreds.VerifierVerifyProofAsync(proofRequestJson, proofJson, schemasJson, claimDefsJson, revocRegsJson)
                                                                       );

            Assert.AreEqual(ErrorCode.CommonInvalidStructure, ex.ErrorCode);
        }
예제 #18
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");
        }
예제 #19
0
        public async Task testAnoncredsRevocationInteractionIssuanceByDefault()
        {
            // Issuer create DID
            var trusteeDidInfo = await Did.CreateAndStoreMyDidAsync(this.wallet, JsonConvert.SerializeObject(new { seed = TRUSTEE_SEED }));

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

            var nymRequest = await Ledger.BuildNymRequestAsync(trusteeDidInfo.Did, issuerDidInfo.Did,
                                                               issuerDidInfo.VerKey, null, "TRUSTEE");

            await Ledger.SignAndSubmitRequestAsync(pool, wallet, trusteeDidInfo.Did, nymRequest);

            var issuerDid = issuerDidInfo.Did;

            // Prover create DID
            var proverDidInfo = await Did.CreateAndStoreMyDidAsync(proverWallet, "{}");

            var proverDid    = proverDidInfo.Did;
            var proverVerkey = proverDidInfo.VerKey;

            // Issuer publish Prover DID
            nymRequest = await Ledger.BuildNymRequestAsync(issuerDid, proverDid, proverVerkey, null, null);

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

            // ISSUER post to Ledger Schema, CredentialDefinition, RevocationRegistry

            // Issuer creates Schema
            var schemaInfo =
                await AnonCreds.IssuerCreateSchemaAsync(issuerDidInfo.Did, GVT_SCHEMA_NAME,
                                                        SCHEMA_VERSION, GVT_SCHEMA_ATTRIBUTES);

            var schemaJson = schemaInfo.SchemaJson;

            // Issuer posts Schema to Ledger
            var schemaRequest = await Ledger.BuildSchemaRequestAsync(issuerDid, schemaJson);

            await Ledger.SignAndSubmitRequestAsync(pool, wallet, issuerDid, schemaRequest);

            // Issuer get Schema from Ledger
            var getSchemaRequest = await Ledger.BuildGetSchemaRequestAsync(issuerDid, schemaInfo.SchemaId);

            var getSchemaResponse = await PoolUtils.EnsurePreviousRequestAppliedAsync(pool, getSchemaRequest, response => {
                var getSchemaResponseObject = JObject.Parse(response);
                return(getSchemaResponseObject["result"]["seqNo"] != null);
            });

            // !!IMPORTANT!!
            // It is important to get Schema from Ledger and parse it to get the correct schema JSON and correspondent id in Ledger
            // After that we can create CredentialDefinition for received Schema(not for result of indy_issuer_create_schema)

            ParseResponseResult schemaInfo1 = await Ledger.ParseGetSchemaResponseAsync(getSchemaResponse);

            schemaJson = schemaInfo1.ObjectJson;

            // Issuer creates CredentialDefinition

            var credDefInfo =
                await AnonCreds.IssuerCreateAndStoreCredentialDefAsync(wallet, issuerDid, schemaJson,
                                                                       TAG, null, JsonConvert.SerializeObject(new { support_revocation = true }));

            var credDefId   = credDefInfo.CredDefId;
            var credDefJson = credDefInfo.CredDefJson;

            // Issuer post CredentialDefinition to Ledger
            var credDefRequest = await Ledger.BuildCredDefRequestAsync(issuerDid, credDefJson);

            await Ledger.SignAndSubmitRequestAsync(pool, wallet, issuerDid, credDefRequest);

            // Issuer creates RevocationRegistry
            /* FIXME: getIndyHomePath hard coded forward slash "/". It will not work for Windows. */
            var tailsWriterConfig = JsonConvert.SerializeObject(
                new
            {
                base_dir    = EnvironmentUtils.GetIndyHomePath("tails"),
                uri_pattern = string.Empty
            }
                );

            var tailsWriterHandle = await BlobStorage.OpenWriterAsync("default", tailsWriterConfig);

            var revRegInfo =
                await AnonCreds.IssuerCreateAndStoreRevocRegAsync(wallet, issuerDid, null, TAG,
                                                                  credDefId,
                                                                  JsonConvert.SerializeObject(new { max_cred_num = 5, issuance_type = "ISSUANCE_BY_DEFAULT" }),
                                                                  tailsWriterHandle);

            var revRegId        = revRegInfo.RevRegId;
            var revRegDefJson   = revRegInfo.RevRegDefJson;
            var revRegEntryJson = revRegInfo.RevRegEntryJson;

            // Issuer posts RevocationRegistryDefinition to Ledger
            var revRegDefRequest = await Ledger.BuildRevocRegDefRequestAsync(issuerDid, revRegDefJson);

            await Ledger.SignAndSubmitRequestAsync(pool, wallet, issuerDid, revRegDefRequest);

            // Issuer posts RevocationRegistryEntry to Ledger
            var revRegEntryRequest = await Ledger.BuildRevocRegEntryRequestAsync(issuerDid, revRegId, REVOC_REG_TYPE, revRegEntryJson);

            await Ledger.SignAndSubmitRequestAsync(pool, wallet, issuerDid, revRegEntryRequest);

            // Issuance Credential for Prover

            // Prover creates Master Secret
            await AnonCreds.ProverCreateMasterSecretAsync(proverWallet, COMMON_MASTER_SECRET);

            // Issuer creates Credential Offer
            var credOfferJson = await AnonCreds.IssuerCreateCredentialOfferAsync(wallet, credDefId);

            // Prover gets CredentialDefinition from Ledger
            var getCredDefRequest = await Ledger.BuildGetCredDefRequestAsync(proverDid, credDefInfo.CredDefId);

            var getCredDefResponse = await Ledger.SubmitRequestAsync(pool, getCredDefRequest);

            ParseResponseResult credDefIdInfo = await Ledger.ParseGetCredDefResponseAsync(getCredDefResponse);

            credDefId   = credDefIdInfo.Id;
            credDefJson = credDefIdInfo.ObjectJson;

            // Prover creates Credential Request
            var credReqInfo =
                await AnonCreds.ProverCreateCredentialReqAsync(proverWallet, proverDid, credOfferJson,
                                                               credDefJson, COMMON_MASTER_SECRET);

            var credReqJson         = credReqInfo.CredentialRequestJson;
            var credReqMetadataJson = credReqInfo.CredentialRequestMetadataJson;

            // Issuer creates TailsReader
            BlobStorageReader blobStorageReader = await BlobStorage.OpenReaderAsync(TYPE, tailsWriterConfig);

            // Issuer creates Credential
            // Issuer must not post rev_reg_delta to ledger for ISSUANCE_BY_DEFAULT strategy

            var credRegInfo =
                await AnonCreds.IssuerCreateCredentialAsync(wallet, credOfferJson, credReqJson,
                                                            GVT_CRED_VALUES, revRegId,
                                                            blobStorageReader);

            var credJson  = credRegInfo.CredentialJson;
            var credRevId = credRegInfo.RevocId;

            // Prover gets RevocationRegistryDefinition
            var getRevRegDefRequest = await Ledger.BuildGetRevocRegDefRequestAsync(proverDid, revRegId);

            var getRevRegDefResponse = await Ledger.SubmitRequestAsync(pool, getRevRegDefRequest);

            ParseResponseResult revRegInfo1 = await Ledger.ParseGetRevocRegDefResponseAsync(getRevRegDefResponse);

            revRegId = revRegInfo1.Id;
            var revocRegDefJson = revRegInfo1.ObjectJson;

            // Prover store received Credential

            await AnonCreds.ProverStoreCredentialAsync(proverWallet, "credential1_id",
                                                       credReqMetadataJson, credJson, credDefJson,
                                                       revocRegDefJson);

            // Verifying Prover Credential
            Thread.Sleep(3000);

            long to = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds / 1000;

            var proofRequest = 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
                    }
                },
                non_revoked = new
                {
                    to = to
                }
            }
                );

            // Prover gets Claims for Proof Request

            var credentialsJson = await AnonCreds.ProverGetCredentialsForProofReqAsync(proverWallet, proofRequest);

            var credentials      = JObject.Parse(credentialsJson);
            var credsForReferent = (JArray)credentials["attrs"]["attr1_referent"];
            var credential       = credsForReferent[0]["cred_info"];

            // Prover gets RevocationRegistryDelta from Ledger

            /* FIXME */
            var getRevRegDeltaRequest = await Ledger.BuildGetRevocRegDeltaRequestAsync(proverDid, revRegId, -1, (int)to);

            var getRevRegDeltaResponse = await Ledger.SubmitRequestAsync(pool, getRevRegDeltaRequest);

            var revRegInfo2 = await Ledger.ParseGetRevocRegDeltaResponseAsync(getRevRegDeltaResponse);

            revRegId = revRegInfo2.Id;
            var revocRegDeltaJson = revRegInfo2.ObjectJson;

            // Prover creates RevocationState
            var timestamp = to;

            var revStateJson = await AnonCreds.CreateRevocationStateAsync(blobStorageReader,
                                                                          revocRegDefJson, revocRegDeltaJson, (int)timestamp, credRevId);

            // Prover gets Schema from Ledger
            getSchemaRequest = await Ledger.BuildGetSchemaRequestAsync(proverDid, schemaInfo1.Id);

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

            ParseResponseResult schemaInfo2 = await Ledger.ParseGetSchemaResponseAsync(getSchemaResponse);

            var schemaId = schemaInfo2.Id;

            schemaJson = schemaInfo2.ObjectJson;

            // Prover creates Proof
            var requestedCredentialsJson = JsonConvert.SerializeObject(
                new
            {
                self_attested_attributes = new { },
                requested_attributes     = new
                {
                    attr1_referent = new
                    {
                        cred_id   = credential["referent"],
                        timestamp = timestamp,
                        revealed  = true
                    }
                },
                requested_predicates = new
                {
                    predicate1_referent = new
                    {
                        cred_id   = credential["referent"],
                        timestamp = timestamp
                    }
                }
            }
                );


            var schemasJson   = new JObject(new JProperty(schemaId, JObject.Parse(schemaJson))).ToString();
            var credDefsJson  = new JObject(new JProperty(credDefId, JObject.Parse(credDefJson))).ToString();
            var revStatesJson = new JObject(new JProperty(revRegId, new JObject(new JProperty(timestamp.ToString(), JObject.Parse(revStateJson))))).ToString();

            var proofJson = await AnonCreds.ProverCreateProofAsync(proverWallet, proofRequest,
                                                                   requestedCredentialsJson, COMMON_MASTER_SECRET,
                                                                   schemasJson, credDefsJson, revStatesJson);


            var proof = JObject.Parse(proofJson);

            // Verifier gets RevocationRegistry from Ledger

            var getRevRegReq = await Ledger.BuildGetRevocRegRequestAsync(DID_MY1, revRegId, (int)timestamp);

            var getRevRegResp = await Ledger.SubmitRequestAsync(pool, getRevRegReq);

            var revRegInfo3 = await Ledger.ParseGetRevocRegResponseAsync(getRevRegResp);

            revRegId = revRegInfo3.Id;
            var revRegJson = revRegInfo3.ObjectJson;

            // Verifier verifies proof
            Assert.AreNotEqual("Alex", proof["requested_proof"]["revealed_attrs"]["attr1_referent"].ToString());

            var revRegDefsJson = new JObject(new JProperty(revRegId, JObject.Parse(revRegDefJson))).ToString();
            var revRegsJson    = new JObject(new JProperty(revRegId, new JObject(new JProperty(timestamp.ToString(), JObject.Parse(revRegJson))))).ToString();

            var valid = await AnonCreds.VerifierVerifyProofAsync(proofRequest,
                                                                 proofJson,
                                                                 schemasJson,
                                                                 credDefsJson,
                                                                 revRegDefsJson,
                                                                 revRegsJson);

            Assert.IsTrue(valid);

            // Issuer revokes credential
            var revRegDeltaJson = await AnonCreds.IssuerRevokeCredentialAsync(wallet,
                                                                              blobStorageReader,
                                                                              revRegId, credRevId);

            // Issuer post RevocationRegistryDelta to Ledger
            revRegEntryRequest = await Ledger.BuildRevocRegEntryRequestAsync(issuerDid, revRegId, REVOC_REG_TYPE, revRegDeltaJson);

            await Ledger.SignAndSubmitRequestAsync(pool, wallet, issuerDid, revRegEntryRequest);

            // Verifying Prover Credential after Revocation
            Thread.Sleep(3000);

            long from = to;

            to = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds / 1000;

            // Prover gets RevocationRegistryDelta from Ledger
            getRevRegDeltaRequest = await Ledger.BuildGetRevocRegDeltaRequestAsync(proverDid, revRegId, (int)from, (int)to);

            getRevRegDeltaResponse = await Ledger.SubmitRequestAsync(pool, getRevRegDeltaRequest);

            var revRegInfo4 = await Ledger.ParseGetRevocRegDeltaResponseAsync(getRevRegDeltaResponse);

            revRegId          = revRegInfo4.Id;
            revocRegDeltaJson = revRegInfo4.ObjectJson;
            timestamp         = (long)revRegInfo4.Timestamp;

            // Prover creates RevocationState
            revStateJson = await AnonCreds.CreateRevocationStateAsync(blobStorageReader,
                                                                      revocRegDefJson, revocRegDeltaJson, (int)timestamp, credRevId);

            requestedCredentialsJson = JsonConvert.SerializeObject(
                new
            {
                self_attested_attributes = new { },
                requested_attributes     = new
                {
                    attr1_referent = new
                    {
                        cred_id   = credential["referent"],
                        timestamp = timestamp,
                        revealed  = true
                    }
                },
                requested_predicates = new
                {
                    predicate1_referent = new
                    {
                        cred_id   = credential["referent"],
                        timestamp = timestamp
                    }
                }
            }
                );

            revStatesJson = new JObject(new JProperty(revRegId, new JObject(new JProperty(timestamp.ToString(), JObject.Parse(revStateJson))))).ToString();

            proofJson = await AnonCreds.ProverCreateProofAsync(proverWallet,
                                                               proofRequest,
                                                               requestedCredentialsJson,
                                                               COMMON_MASTER_SECRET,
                                                               schemasJson,
                                                               credDefsJson,
                                                               revStatesJson);

            // Verifier gets RevocationRegistry from Ledger
            getRevRegReq = await Ledger.BuildGetRevocRegRequestAsync(DID_MY1, revRegId, (int)timestamp);

            getRevRegResp = await Ledger.SubmitRequestAsync(pool, getRevRegReq);

            var revRegInfo5 = await Ledger.ParseGetRevocRegResponseAsync(getRevRegResp);

            revRegId   = revRegInfo5.Id;
            revRegJson = revRegInfo5.ObjectJson;
            timestamp  = (long)revRegInfo5.Timestamp;

            revRegsJson = new JObject(new JProperty(revRegId, new JObject(new JProperty(timestamp.ToString(), JObject.Parse(revRegJson))))).ToString();

            valid = await AnonCreds.VerifierVerifyProofAsync(proofRequest,
                                                             proofJson,
                                                             schemasJson,
                                                             credDefsJson,
                                                             revRegDefsJson,
                                                             revRegsJson);

            Assert.IsFalse(valid);
        }
예제 #20
0
        public static async Task Execute()
        {
            Console.WriteLine("Anoncreds Revocation sample -> started");

            var issuerWalletName = "issuerWallet";
            var proverWalletName = "proverWallet";

            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(PoolUtils.DEFAULT_POOL_NAME, issuerWalletName, "default", null, issuerWalletCredentials);

                //3. Prover Create and Open Wallet
                await WalletUtils.CreateWalletAsync(PoolUtils.DEFAULT_POOL_NAME, proverWalletName, "default", null, 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(issuerWalletName, null, issuerWalletCredentials))
                        using (var proverWallet = await Wallet.OpenWalletAsync(proverWalletName, null, 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(issuerWalletName, issuerWalletCredentials);

                await WalletUtils.DeleteWalletAsync(proverWalletName, proverWalletCredentials);

                await PoolUtils.DeletePoolLedgerConfigAsync(PoolUtils.DEFAULT_POOL_NAME);
            }

            Console.WriteLine("Anoncreds Revocation sample -> completed");
        }
예제 #21
0
        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);
        }