def requestCredDef(self, credDefKey, sender): name, version, origin = credDefKey credDef = CredDef(name=name, version=version, origin=origin) self._credDefs[credDefKey] = credDef req = credDef.getRequest(sender) if req: return self.prepReq(req)
def addCredDef(self, credDef: CredDef): """ :param credDef: credDef to add :return: number of pending txns """ self._credDefs[credDef.key()] = credDef req = credDef.request if req: self.pendRequest(req, credDef.key()) return len(self._pending)
def addClaimDefs(self, name, version, attrNames, staticPrime, credDefSeqNo, issuerKeySeqNo): csk = CredDefSecretKey(*staticPrime) sid = self.wallet.addCredDefSk(str(csk)) # Need to modify the claim definition. We do not support types yet claimDef = { NAME: name, VERSION: version, TYPE: "CL", ATTR_NAMES: attrNames } wallet = self.wallet credDef = CredDef(seqNo=credDefSeqNo, attrNames=claimDef[ATTR_NAMES], name=claimDef[NAME], version=claimDef[VERSION], origin=wallet.defaultId, typ=claimDef[TYPE], secretKey=sid) wallet._credDefs[(name, version, wallet.defaultId)] = credDef isk = IssuerSecretKey(credDef, csk, uid=str(uuid.uuid4())) self.wallet.addIssuerSecretKey(isk) ipk = IssuerPubKey(N=isk.PK.N, R=isk.PK.R, S=isk.PK.S, Z=isk.PK.Z, claimDefSeqNo=credDef.seqNo, secretKeyUid=isk.uid, origin=wallet.defaultId, seqNo=issuerKeySeqNo) key = (wallet.defaultId, credDefSeqNo) wallet._issuerPks[key] = ipk
def credentialDefinitionAdded(genned, updatedSteward, addedSponsor, sponsor, sponsorWallet, looper, tdir, nodeSet, attrNames, credDef, credDefSecretKeyAdded): old = sponsorWallet.pendingCount data = credDef.get(serFmt=SerFmt.base58) credDef = CredDef(seqNo=None, attrNames=attrNames, name=data[NAME], version=data[VERSION], origin=sponsorWallet.defaultId, typ=data[TYPE], secretKey=credDefSecretKeyAdded) pending = sponsorWallet.addCredDef(credDef) assert pending == old + 1 reqs = sponsorWallet.preparePending() sponsor.submitReqs(*reqs) key = credDef.key() def chk(): assert sponsorWallet.getCredDef(key).seqNo is not None looper.run(eventually(chk, retryWait=1, timeout=30)) return sponsorWallet.getCredDef(key).seqNo
def _getCredDefReply(self, result, preparedReq): data = json.loads(result.get(DATA)) credDef = self.getCredDef( (data.get(NAME), data.get(VERSION), data.get(ORIGIN))) if credDef: if not credDef.seqNo: credDef.seqNo = data.get(F.seqNo.name) else: credDef = CredDef(seqNo=data.get(F.seqNo.name), attrNames=data.get(ATTR_NAMES).split(","), name=data[NAME], version=data[VERSION], origin=data[ORIGIN], typ=data[TYPE]) self.addCredDef(credDef)
def faberAddedClaimDefAndIssuerKeys(faberAddedByPhil, faberIsRunning, staticPrimes, looper): faber, faberWallet = faberIsRunning csk = CredDefSecretKey(*staticPrimes.get("prime1")) sid = faberWallet.addCredDefSk(str(csk)) # Need to modify the claim definition. We do not support types yet claimDef = { "name": "Transcript", "version": "1.2", "type": "CL", "attr_names": ["student_name", "ssn", "degree", "year", "status"] } credDef = CredDef(seqNo=None, attrNames=claimDef[ATTR_NAMES], name=claimDef[NAME], version=claimDef[VERSION], origin=faberWallet.defaultId, typ=claimDef[TYPE], secretKey=sid) faberWallet.addCredDef(credDef) reqs = faberWallet.preparePending() faber.client.submitReqs(*reqs) def chk(): assert credDef.seqNo is not None looper.run(eventually(chk, retryWait=1, timeout=10)) isk = IssuerSecretKey(credDef, csk, uid=str(uuid.uuid4())) faberWallet.addIssuerSecretKey(isk) ipk = IssuerPubKey(N=isk.PK.N, R=isk.PK.R, S=isk.PK.S, Z=isk.PK.Z, claimDefSeqNo=credDef.seqNo, secretKeyUid=isk.uid, origin=faberWallet.defaultId) faberWallet.addIssuerPublicKey(ipk) reqs = faberWallet.preparePending() faber.client.submitReqs(*reqs) key = (faberWallet.defaultId, credDef.seqNo) def chk(): assert faberWallet.getIssuerPublicKey(key).seqNo is not None looper.run(eventually(chk, retryWait=1, timeout=10)) return ipk.seqNo
def testAnonCredFlow(genned, looper, tdir, nodeSet, issuerWallet: Wallet, proverWallet: Wallet, verifierWallet, addedIPV): # Don't move the following import outside of this method, otherwise that # client class doesn't gets reloaded and it doesn't get updated with the # correct plugin class/methods and it gives an error. # (for permanent solution bug is created: #130181205) from sovrin.test.helper import genTestClient BYU = IssuerModule.AttribDef('BYU', [ IssuerModule.AttribType("first_name", encode=True), IssuerModule.AttribType("last_name", encode=True), IssuerModule.AttribType("birth_date", encode=True), IssuerModule.AttribType("expire_date", encode=True), IssuerModule.AttribType("undergrad", encode=True), IssuerModule.AttribType("postgrad", encode=True) ]) setupLogging(DISPLAY_LOG_LEVEL, Console.Wordage.mute) logger = getlogger("test_anon_creds") logging.root.addHandler(DemoHandler(partial(out, logger))) logging.root.handlers = [] attributes = BYU.attribs(first_name="John", last_name="Doe", birth_date="1970-01-01", expire_date="2300-01-01", undergrad="True", postgrad="False") attrNames = tuple(attributes.keys()) # 3 Sovrin clients acting as Issuer, Signer and Verifier issuerC, _ = genTestClient(nodeSet, tmpdir=tdir, peerHA=genHa()) proverC, _ = genTestClient(nodeSet, tmpdir=tdir, peerHA=genHa()) verifierC, _ = genTestClient(nodeSet, tmpdir=tdir, peerHA=genHa()) looper.add(issuerC) looper.add(proverC) looper.add(verifierC) looper.run(issuerC.ensureConnectedToNodes(), proverC.ensureConnectedToNodes(), verifierC.ensureConnectedToNodes()) makePendingTxnsRequest(issuerC, issuerWallet) makePendingTxnsRequest(proverC, proverWallet) makePendingTxnsRequest(verifierC, verifierWallet) # Adding signers # issuer.signers[issuerSigner.identifier] = issuerSigner logger.display("Key pair for Issuer created \n" "Public key is {} \n" "Private key is stored on disk\n".format( issuerWallet.defaultId)) # prover.signers[proverSigner.identifier] = proverSigner logger.display("Key pair for Prover created \n" "Public key is {} \n" "Private key is stored on disk\n".format( proverWallet.defaultId)) # verifier.signers[verifierSigner.identifier] = verifierSigner logger.display("Key pair for Verifier created \n" "Public key is {} \n" "Private key is stored on disk\n".format( verifierWallet.defaultId)) # TODO BYU.name is used here instead of issuerSigner.identifier due to # tight coupling in Attribs.encoded() issuerId = BYU.name proverId = proverWallet.defaultId # Issuer's attribute repository attrRepo = IssuerModule.InMemoryAttrRepo() attrRepo.attributes = {proverId: attributes} name1 = "Qualifications" version1 = "1.0" ip = issuerC.peerHA[0] port = issuerC.peerHA[1] interactionId = 'LOGIN-1' # This is the issuer entity issuer = IssuerModule.Issuer(issuerId, attrRepo) # issuer.attributeRepo = attrRepo # Issuer publishes credential definition to Sovrin ledger csk = CredDefSecretKey(*staticPrimes().get("prime1")) cskId = issuerWallet.addCredDefSk(str(csk)) credDef = CredDef(seqNo=None, attrNames=attrNames, name=name1, version=version1, origin=issuerWallet.defaultId, secretKey=cskId) # credDef = issuer.addNewCredDef(attrNames, name1, version1, # p_prime="prime1", q_prime="prime1", ip=ip, # port=port) # issuer.credentialDefinitions = {(name1, version1): credDef} logger.display("Issuer: Creating version {} of credential definition" " for {}".format(version1, name1)) print("Credential definition: ") pprint.pprint(credDef.get()) # Pretty-printing the big object. pending = issuerWallet.addCredDef(credDef) reqs = issuerWallet.preparePending() # op = {TXN_TYPE: CRED_DEF, # DATA: getCredDefTxnData(credDef)} logger.display("Issuer: Writing credential definition to " "Sovrin Ledger...") issuerC.submitReqs(*reqs) def chk(): assert issuerWallet.getCredDef( (name1, version1, issuerWallet.defaultId)).seqNo is not None looper.run(eventually(chk, retryWait=.1, timeout=30)) # submitAndCheck(looper, issuerC, issuerWallet, op) # Prover requests Issuer for credential (out of band) logger.display("Prover: Requested credential from Issuer") # Issuer issues a credential for prover logger.display("Issuer: Creating credential for " "{}".format(proverWallet.defaultId)) encodedAttributes = attributes.encoded() revealedAttrs = ["undergrad"] prover = ProverModule.Prover(proverId) pk = {issuerId: prover.getPk(credDef)} proofBuilder = ProofBuilderModule.ProofBuilder(pk) proofId = proofBuilder.id prover.proofBuilders[proofId] = proofBuilder cred = issuer.createCred(proverId, name1, version1, proofBuilder.U[issuerId]) logger.display("Prover: Received credential from " "{}".format(issuerWallet.defaultId)) # Prover intends to prove certain attributes to a Verifier # Verifier issues a nonce logger.display("Prover: Requesting Nonce from verifier…") logger.display("Verifier: Nonce received from prover" " {}".format(proverId)) verifierId = verifierWallet.defaultId verifier = VerifierModule.Verifier(verifierId) nonce = verifier.generateNonce(interactionId) logger.display("Verifier: Nonce sent.") logger.display("Prover: Nonce received") presentationToken = { issuerId: (cred[0], cred[1], proofBuilder.vprime[issuerId] + cred[2]) } # Prover discovers Issuer's credential definition logger.display("Prover: Preparing proof for attributes: " "{}".format(revealedAttrs)) proofBuilder.setParams(presentationToken, revealedAttrs, nonce) prf = ProofBuilderModule.ProofBuilder.prepareProof( credDefPks=proofBuilder.credDefPks, masterSecret=proofBuilder.masterSecret, creds=presentationToken, encodedAttrs=encodedAttributes, revealedAttrs=revealedAttrs, nonce=nonce) logger.display("Prover: Proof prepared.") logger.display("Prover: Proof submitted") logger.display("Verifier: Proof received.") logger.display("Verifier: Looking up Credential Definition" " on Sovrin Ledger...") # Verifier fetches the credential definition from ledger verifier.credentialDefinitions = {(issuerId, name1, version1): credDef} verified = VerifierModule.Verifier.verifyProof(pk, prf, nonce, attributes.encoded(), revealedAttrs) # Verifier verifies proof logger.display("Verifier: Verifying proof...") logger.display("Verifier: Proof verified.") assert verified logger.display("Prover: Proof accepted.")