async def credential_definition(pool_, issuer, schema): # Define a credential definition: # Cred['def'] : - id # - json # - tag # - type # - config # 11. print_log( '\n11. Creating and storing CRED DEFINITION using anoncreds as Trust Anchor, for the given Schema\n' ) cred_def = {'Issuer': issuer['name']} cred_def['tag'] = 'cred_def_tag' cred_def['type'] = 'CL' cred_def['config'] = json.dumps({"support_revocation": False}) (cred_def['id'], cred_def['json'] ) = await anoncreds.issuer_create_and_store_credential_def( wallet_handle=issuer['wallet'], issuer_did=issuer['did'], schema_json=schema['json'], tag=cred_def['tag'], signature_type=cred_def['type'], config_json=cred_def['config']) print_log('Credential definition: ') pprint.pprint(json.loads(cred_def['json'])) return (cred_def)
async def create_credential(issuer, cred): # 16. Issuer creates credential: print_log( '\n Issuer (Trust Anchor) creates Credential for Credential Request\n') cred['values_json'] = json.dumps({ 'sex': [ 'male', '5944657099558967239210949258394887428692050081607692519917050011144233115103' ], 'name': ['Alex', '1139481716457488690172217916278103335'], 'height': ['175', '175'], 'age': ['28', '28'] }) (cred['json'], _, _) = await anoncreds.issuer_create_credential( wallet_handle=issuer['wallet'], cred_offer_json=cred['offer_json'], cred_req_json=cred['req_json'], cred_values_json=cred['values_json'], rev_reg_id=None, blob_storage_reader_handle=None) print_log('Credential: ') pprint.pprint(json.loads(cred['json'])) return (cred)
async def credential_definition(pool_,issuer,schema): # Define a credential definition: # Cred['def'] : - id # - json # - tag # - type # - config #Next, we create a credential definition. # This references the schema that we just added, and announces # who is going to be issuing credentials with that schema (our trust anchor identity, in this case), # what type of signature method they plan to use ("CL" = "Camenisch Lysyanskya", # the default method used for zero-knowledge proofs by indy), # how they plan to handle revocation, and so forth. # 11. print_log('\n Creating and storing CRED DEFINITION.\n') cred_def = {'Issuer':issuer['name']} cred_def['tag'] = 'cred_def_tag' cred_def['type'] = 'CL' cred_def['config'] = json.dumps({"support_revocation": False}) (cred_def['id'], cred_def['json']) = await anoncreds.issuer_create_and_store_credential_def(wallet_handle=issuer['wallet'], issuer_did=issuer['did'], schema_json=schema['json'], tag=cred_def['tag'], signature_type=cred_def['type'], config_json=cred_def['config']) print_log('Credential definition: ') pprint.pprint(json.loads(cred_def['json'])) return(cred_def)
async def prep(A, Bname, msg, crypt): AkeyB = 'key_for_' + Bname BkeyA = 'key_from_' + Bname if crypt == 2: encrypted = bytes(msg, 'utf-8') else: msg = bytes(msg, "utf-8") try: encrypted = await crypto.auth_crypt(A['wallet'], A[AkeyB], A[BkeyA], msg) # encrypted = await crypto.anon_crypt(their_vk, msg) print() print('encrypted = %s' % repr(encrypted)) print() except KeyError as e: print_log( 'Make sure you have retrieved the receivers Verkey from the ledger.' ) print() return with open('message.dat', 'wb') as f: f.write(encrypted) print('prepping %s' % msg) return encrypted
async def build_proof_request(issuer, schema): # 18. print_log('\n Prover gets Credentials for Proof Request\n') proof_request = { 'nonce': '123432421212', 'name': 'proof_req_1', 'version': '0.1', 'requested_attributes': { 'attr1_referent': { 'name': 'name', "restrictions": { "issuer_did": issuer['did'], "schema_id": schema['id'] } } }, 'requested_predicates': { 'predicate1_referent': { 'name': 'age', 'p_type': '>=', 'p_value': 18, "restrictions": { "issuer_did": issuer['did'] } } } } print_log('Proof Request: ') pprint.pprint(proof_request) return (proof_request)
async def verify_proof(proof_req, proof, cred, schema): # 21. print_log('\n Verifier is verifying proof from Prover\n') assert await anoncreds.verifier_verify_proof(proof_req['json'], proof['json'], schema['proof_json'], cred['def']['proof_json'], "{}", "{}")
async def offer_credential(issuer, cred): # 14. Issuer creates offer: print_log( '\n Issuer (Trust Anchor) is creating a Credential Offer for Prover\n') cred['offer_json'] = await anoncreds.issuer_create_credential_offer( wallet_handle=issuer['wallet'], cred_def_id=cred['def']['id']) print_log('Credential Offer: ') pprint.pprint(json.loads(cred['offer_json'])) return cred
async def process_and_store_credential(prover, cred): # 17. print_log('\n Prover processes and stores Credential\n') await anoncreds.prover_store_credential( wallet_handle=prover['wallet'], cred_id=None, cred_req_metadata_json=cred['req_metadata_json'], cred_json=cred['json'], cred_def_json=cred['def']['json'], rev_reg_def_json=None) return (cred)
async def prover_wallet_and_link_secret(prover): # prover = await create_wallet(prover) # prover['did'] = 'VsKV7grR1BUE29mG2Fm2kX' # prover['wallet'] = await wallet.open_wallet(prover['wallet_config'], prover['wallet_credentials']) # 13. print_log('\n Prover is creating Link Secret\n') prover['link_secret_name'] = 'link_secret' prover['link_secret_id'] = await anoncreds.prover_create_master_secret( prover['wallet'], prover['link_secret_name']) return (prover)
async def request_credential(prover, cred): # 15. Prover creates credential request: print_log( '\n Prover creates Credential Request for the given credential offer\n' ) (cred['req_json'], cred['req_metadata_json']) = await anoncreds.prover_create_credential_req( wallet_handle=prover['wallet'], prover_did=prover['did'], cred_offer_json=cred['offer_json'], cred_def_json=cred['def']['json'], master_secret_id=prover['link_secret_id']) print_log('Credential Request: ') pprint.pprint(json.loads(cred['req_json'])) return (cred)
async def fetch_credentials(prover, proof_req): # 19. print_log( '\n Prover gets Credentials for attr1_referent and predicate1_referent\n' ) proof_req['json'] = json.dumps(proof_req) prover['cred_search_handle'] = \ await anoncreds.prover_search_credentials_for_proof_req(prover['wallet'], proof_req['json'], None) creds_for_attr1 = await anoncreds.prover_fetch_credentials_for_proof_req( prover['cred_search_handle'], 'attr1_referent', 1) prover['cred_for_attr1'] = json.loads(creds_for_attr1)[0]['cred_info'] print_log('Prover credential for attr1_referent: ') pprint.pprint(prover['cred_for_attr1']) creds_for_predicate1 = await anoncreds.prover_fetch_credentials_for_proof_req( prover['cred_search_handle'], 'predicate1_referent', 1) prover['cred_for_predicate1'] = json.loads( creds_for_predicate1)[0]['cred_info'] print_log('Prover credential for predicate1_referent: ') pprint.pprint(prover['cred_for_predicate1']) await anoncreds.prover_close_credentials_search_for_proof_req( prover['cred_search_handle']) return prover, proof_req
async def create_proof(proof_req, prover, cred, schema): # 20. print_log('\n Prover creates Proof for Proof Request\n') prover['requested_creds'] = json.dumps({ 'self_attested_attributes': {}, 'requested_attributes': { 'attr1_referent': { 'cred_id': prover['cred_for_attr1']['referent'], 'revealed': True } }, 'requested_predicates': { 'predicate1_referent': { 'cred_id': prover['cred_for_predicate1']['referent'] } } }) print_log('Requested Credentials for Proving: ') pprint.pprint(json.loads(prover['requested_creds'])) prover['schema_id'] = json.loads(cred['offer_json'])['schema_id'] schema['proof_json'] = json.dumps( {prover['schema_id']: json.loads(schema['json'])}) cred['def']['proof_json'] = json.dumps( {cred['def']['id']: json.loads(cred['def']['json'])}) proof_json = await anoncreds.prover_create_proof( prover['wallet'], proof_req['json'], prover['requested_creds'], prover['link_secret_id'], schema['proof_json'], cred['def']['proof_json'], "{}") proof = json.loads(proof_json) proof['json'] = proof_json assert 'Alex' == proof['requested_proof']['revealed_attrs'][ 'attr1_referent']["raw"] return proof, prover
async def issue_credential(): try: # "Scaffolding:" # Step 1: Pool configuration: pool_ = await pool_configuration(pool_name) # Step 2: Create and open wallets: steward = await ID('steward') steward = await create_wallet(steward) trust_anchor = await ID('trust_anchor') trust_anchor = await create_wallet(trust_anchor) # Step 3: Create did and verkey: steward = await create_did_and_verkey(steward) trust_anchor = await create_did_and_verkey(trust_anchor) # Step 4: NYM request: nymrole = 'TRUST_ANCHOR' # Define role for NYM transaction await nym_request(pool_,steward,trust_anchor,nymrole) # Step 5: Rotate keys (skipped): # Step 3: Build and submit a schema request schema = await schema_request(pool_,steward) # Step 4: Create a Credential Definition: cred_def = await credential_definition(pool_,trust_anchor,schema) # Define the credential based on the credential definition: cred = {'def': cred_def} print_log('Credential:') pprint.pprint(cred) # Step 3: Create prover ID and link-secret: print_log('\n12. Creating Prover\n') prover = await ID('prover') prover = await create_wallet(prover) prover = await create_did_and_verkey(prover) prover = await prover_wallet_and_link_secret(prover) # Step 4 code goes here: cred = await offer_credential(trust_anchor,cred) print_log("Cred Definition") print(cred_def) cred = await request_credential(prover,cred) cred = await create_credential(trust_anchor, cred) cred = await process_and_store_credential(prover,cred) print_log('Credential:') pprint.pprint(cred) # Clean-up: await cleanup(pool_,trust_anchor) await cleanup(pool_,steward) await cleanup(pool_,prover) except IndyError as e: print('Error occurred: %s' % e)
async def schema_request(pool_,issuer): # 9. print_log('\n Build the SCHEMA request\n') name = input('Schema name?:') version = input('Version?:') A = [] # create list while True: for i in # Define Schema: schema = { 'name': 'gvt', 'version': '1.0', 'attributes': ["age", "sex", "height", "name"] } # This function call creates the schema and assumes the issuer of credentials using the schema to be the # submitter of the schema request: schema['id'],schema['json'] = await anoncreds.issuer_create_schema(issuer_did=issuer['did'], name=schema['name'], version=schema['version'], attrs=json.dumps(schema['attributes'])) print_log('Schema: ') pprint.pprint(schema) print_log('Schema json: ') pprint.pprint(json.loads(schema['json'])) # Create schema for an issuer: return issuer_schema_id, issuer_schema_json: schema_request = await ledger.build_schema_request(submitter['did'],schema['json']) print_log('Schema request: ') # form of a json string pprint.pprint(json.loads(schema_request)) # 10. print_log('\n Sending the SCHEMA request to the ledger\n') schema_response = await ledger.sign_and_submit_request(pool_handle=pool_['handle'], wallet_handle=submitter['wallet'], submitter_did=submitter['did'], request_json=schema_request) print_log('Schema response:') pprint.pprint(json.loads(schema_response)) schema_response_dict = json.loads(schema_response) if schema_response_dict['op'] == 'REJECT': print_log('\n Sending GET SCHEMA request to the ledger for existing Version Number\n') schema_response = await get_schema_request(pool_,submitter,schema) print_log('Schema response:') pprint.pprint(json.loads(schema_response)) return(schema)
async def write_nym_and_query_verkey(): try: # Step 1: Pool configuration pool_ = await pool_configuration(pool_name) # Step 2: Create and open wallets: steward = await ID('steward') steward = await create_wallet(steward) trust_anchor = await ID('trust_anchor') trust_anchor = await create_wallet(trust_anchor) # Step 3: Create did and verkey steward = await create_did_and_verkey(steward) trust_anchor = await create_did_and_verkey(trust_anchor) # Step 4: NYM request: nymrole = 'TRUST_ANCHOR' await nym_request(pool_, steward, trust_anchor, nymrole) # Step 5: Query DID (GET_NYM request): print_log( '\n9. Generating and storing DID and verkey representing a Client ' 'that wants to obtain Trust Anchor Verkey\n') client = await ID('client') client = await create_wallet(client) client['did'], client['verkey'] = await did.create_and_store_my_did( client['wallet'], "{}") print_log('Client DID: ', client['did']) print_log('Client Verkey: ', client['verkey']) get_nym_response = await query_did(pool_, steward, trust_anchor) # See whether we received the same info that we wrote the ledger in step 4. print_log( '\n12. Comparing Trust Anchor verkey as written by Steward and as retrieved in GET_NYM ' 'response submitted by Client\n') print_log('Written by Steward: ', trust_anchor['verkey']) verkey_from_ledger = json.loads( get_nym_response['result']['data'])['verkey'] print_log('Queried from ledger: ', verkey_from_ledger) print_log('Matching: ', verkey_from_ledger == trust_anchor['verkey']) # Close and delete pool and wallets: await cleanup(pool_, trust_anchor) await cleanup(pool_, steward) await cleanup(pool_, client) except IndyError as e: print('Error occurred: %s' % e)
from indy import pool, ledger, wallet, did from indy.error import IndyError, ErrorCode # Required functions: from identity import ID from identity import ID from write_did_functions import print_log, pool_configuration, create_wallet, create_did_and_verkey, nym_request, query_did,cleanup from save_schema_and_cred_def_functions import schema_request, credential_definition from issue_credential_functions import prover_wallet_and_link_secret, offer_credential,request_credential, create_credential, process_and_store_credential from negotiate_proof_functions import build_proof_request, fetch_credentials, create_proof, verify_proof print_log('\n Please specify by entering the appropriate number:\n') print_log('\n 1. Connect to the pool of genesis nodes on the ledger. \n') print_log('\n 2. Create identity owner. \n') print_log('\n 3. Create wallet for identity owner. \n') print_log('\n 4. Create DID and verkey for Identity or Connection. \n') print_log('\n 5. Create NYM_REQUEST. \n') print_log('\n 6. Query DID. \n') print_log('\n 7. Replace Keys. \n') print_log('\n 8. Create identity owner. \n') print_log('\n 9. Create identity owner. \n') print_log('\n 10. Create identity owner. \n') print_log('\n 11. Create identity owner. \n') print_log('\n 12. Create identity owner. \n') print_log('\n 13. Create identity owner. \n') print_log('\n 14. Create identity owner. \n') print_log('\n 15. Create identity owner. \n')
async def messenger(clientname, *args): if not args: A, Bname = await connect() else: for arg in args: Aname = arg A, Bname = await connect(Aname) while True: print('\n') print('Is this an existing secure pairwise connection?') print('1. Yes') print('2. No') crypt = (input('Please select a number:')) if crypt == '1' or crypt == '2': break else: print('Huh?') crypt = int(crypt) print_log('\n The Messenger recognizes the following commands:\n') print('prep: Sender prepares a text message.') print('send: Sender server listens for client response.') print('receive: Receiver client connects to server.') print('read: receiver reads the message.') print('request: Sender prepares a connection request.') print('response: Sender prepares a connection response.') print('verinym: Sender prepares a verinym request.') print('quit:Quit the messenger.') while True: argv = input('> ').strip().split(' ') cmd = argv[0].lower() rest = ' '.join(argv[1:]) if re.match(cmd, 'prep'): msg = await prep(A, Bname, rest, crypt) elif re.match(cmd, 'send'): await server(msg) elif re.match(cmd, 'receive'): await client(clientname) elif re.match(cmd, 'read'): msg = await read(A, Bname, crypt) elif re.match(cmd, 'save'): await save(A, Bname, msg) elif re.match(cmd, 'request'): # Connection request msg = await request(A, Bname) crypt = 2 msg = await prep(A, Bname, msg, crypt) elif re.match(cmd, 'response'): # Connection response msg = await response(A, Bname) crypt = 1 msg = await prep(A, Bname, msg, crypt) elif re.match(cmd, 'verinym'): # Request a verinym msg = await verinym_request(A, Bname) crypt = 1 msg = await prep(A, Bname, msg, crypt) elif re.match(cmd, 'quit'): return A['name'] break else: print('Huh?')
async def save(A, Bname, msg): BdidA = 'did_from_' + Bname BkeyA = 'key_from_' + Bname Bdid = Bname + '_did' Bkey = Bname + '_key' pickle_file = A['name'] + '.pickle' # Function for saving a received message: while True: print('Save as:') print('1. Connection request (DID for ' + Bname + ')') print('2. Connection response (DID and Verkey from ' + Bname + ')') print('3. Verinym request (' + Bname + ' DID and Verkey)') sel = input('Please select a number:') if sel == '1' or sel == '2' or sel == '3': break else: print('Huh?') sel = int(sel) if sel == 1: # Connection request connection_request = json.loads(msg) A[BdidA] = connection_request['did'] A['nonce'] = {Bname: connection_request['nonce']} print() print('Connection request information saved successfully.') elif sel == 2: # Connection response connection_response = json.loads(msg[1].decode('utf-8')) A[BdidA] = connection_response['did'] A[BkeyA] = connection_response['verkey'] A['nonce'] = {Bname: connection_response['nonce']} initial_request = json.loads(A['connection_requests'][Bname]) if initial_request['nonce'] == A['nonce'][Bname]: print_log('\n The Response is Nonce Authenticated \n') print() print('Connection response information is saved successfully.') else: print( 'The Nonce in the Response does not match the Nonce in the Request' ) else: # Verinym verinym_request = json.loads(msg[1].decode('utf-8')) A[Bdid] = verinym_request['did'] A[Bkey] = verinym_request['verkey'] if A[BkeyA] == msg[0]: print() print('Message sender verkey matches connection verkey:') print() print_log('Sender Verkey: ', msg[0]) print_log(Bname + ' Verkey: ', A[BkeyA]) print() print('Verinym request information is saved successfully') with open(pickle_file, 'wb') as f: pickle.dump(A, f)