Skip to content

Instantly share code, notes, and snippets.

@burdettadam
Created April 29, 2019 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save burdettadam/ac8ddf07a845d2d083df849072530c5c to your computer and use it in GitHub Desktop.
Save burdettadam/ac8ddf07a845d2d083df849072530c5c to your computer and use it in GitHub Desktop.
libvcx example code?
import asyncio
import json
import random
from ctypes import cdll
from time import sleep
import logging
import pyqrcode
from vcx.api.vcx_init import vcx_init_with_config, vcx_init
from vcx.api.connection import Connection
from vcx.api.credential_def import CredentialDef
from vcx.api.issuer_credential import IssuerCredential
from vcx.api.proof import Proof
from vcx.api.schema import Schema
from vcx.api.utils import vcx_agent_provision
from vcx.api.vcx_init import vcx_init_with_config
from vcx.state import State, ProofState
from indy import wallet
# logging.basicConfig(level=logging.DEBUG) uncomment to get logs
# 'agency_url': URL of the agency
# 'agency_did': public DID of the agency
# 'agency_verkey': public verkey of the agency
# 'wallet_name': name for newly created encrypted wallet
# 'wallet_key': encryption key for encoding wallet
# 'payment_method': method that will be used for payments
provisionConfig = {
#'agency_url':'http://localhost:8080',
'agency_url':'http://18.191.209.54:30800',
'agency_did':'VsKV7grR1BUE29mG2Fm2kX',
'agency_verkey':'Hezce2UWMZ3wUhVkh2LfKSs8nDzWwzs2Win7EzNN3YaR',
'wallet_name':'sovrin_wallet',
'wallet_key':'123',
'enterprise_seed':'000000000000000000000000Trustee1',
'institution_name':'Sovrin',
#'institution_logo_url': screensaver("adventure time")["original"]["url"],
'payment_method':'sov'
}
cred_attrs = {
'name': 'Adam Burdett',
'date': '2/15/2019',
'Certificate': 'Of Excellence In Sovrin Table Tennis'
}
async def main():
libsovtoken = cdll.LoadLibrary('/c/Users/burdettadam/Documents/GitHub/libsovtoken/libsovtoken/target/debug/libsovtoken.so')
libsovtoken.sovtoken_init()
# clean up code
'''print("#1 Provision an agent and wallet, get back configuration details")
await wallet.delete_wallet(json.dumps({"id": provisionConfig["wallet_name"]}), json.dumps({"key": provisionConfig["wallet_key"]}))
config = await vcx_agent_provision(json.dumps(provisionConfig))
config = json.loads(config)
# Set some additional configuration options specific to faber
config['institution_name'] = provisionConfig["institution_name"]
config['institution_logo_url'] = "https://pbs.twimg.com/profile_images/1034794321751330816/nBXwd5Wg_400x400.jpg" #screensaver("adventure time")["original"]["url"]
config['genesis_path'] = 'docker.txn'
config['payment_method'] = provisionConfig['payment_method']
f = open("faberVcxConfig.json", "w")
f.write(json.dumps(config))
print("#2 Initialize libvcx with new configuration")
await vcx_init_with_config(json.dumps(config))
'''
print("#2 Initialize libvcx with new configuration")
with open("faberVcxConfig.json") as f:
config = json.load(f)
await vcx_init("faberVcxConfig.json")
print("#3 Create a new schema on the ledger")
version = format("%d.%d.%d" % (random.randint(1, 101), random.randint(1, 101), random.randint(1, 101)))
schema = await Schema.create('schema_uuid', 'degree schema', version, [*cred_attrs.keys()], 0)
schema_id = await schema.get_schema_id()
print("#4 Create a new credential definition on the ledger")
cred_def = await CredentialDef.create('credef_uuid', 'degree', schema_id, 0)
cred_def_id = await cred_def.get_cred_def_id()
print("#5 Create a connection and write QR code invite")
connection_to_alice = await Connection.create('alice')
await connection_to_alice.connect(None)
await connection_to_alice.update_state()
details = await connection_to_alice.invite_details(False)
offer = convertInvite(details)
offer["s"]["n"] = config["institution_name"]
offer["t"] = cred_attrs["name"]
offer["s"]["l"] = config['institution_logo_url']
pyqrcode.create(json.dumps(offer)).png('qr-code-connection-offer.png', scale=1, module_color=[0, 0, 0, 128], background=[0xff, 0xff, 0xcc])
print("#6 Poll agency and wait for connection invitation to be accepted (scan QR code now)")
connection_state = await connection_to_alice.get_state()
while connection_state != State.Accepted:
sleep(10)
await connection_to_alice.update_state()
connection_state = await connection_to_alice.get_state()
print("#12 Create an IssuerCredential object using the schema and credential definition")
credential = await IssuerCredential.create('alice_degree', cred_attrs, cred_def_id, 'cred', '0')
#credential = await IssuerCredential.create('alice_degree', cred_attrs, cred_def_id, 'cred', '1000000')
print("#13 Issue credential offer over connection")
await credential.send_offer(connection_to_alice)
await credential.update_state()
print("#14 Poll agency and wait for a credential request")
credential_state = await credential.get_state()
while credential_state != State.RequestReceived:
sleep(10)
await credential.update_state()
credential_state = await credential.get_state()
print("#17 Issue credential ")
await credential.send_credential(connection_to_alice)
print("#18 Wait for credential to be accepted")
await credential.update_state()
credential_state = await credential.get_state()
while credential_state != State.Accepted:
sleep(10)
await credential.update_state()
credential_state = await credential.get_state()
proof_attrs = proof_request(cred_attrs,config['institution_did'])
print("#19 Create a Proof object")
proof = await Proof.create('proof_uuid', 'Sovrin Proof Request', proof_attrs)
print("#20 Request proof of degree from alice")
await proof.request_proof(connection_to_alice)
print("#21 Poll agency and wait for alice to provide proof")
proof_state = await proof.get_state()
while proof_state != State.Accepted:
sleep(10)
await proof.update_state()
proof_state = await proof.get_state()
print("#27 Process the proof provided by alice")
await proof.get_proof(connection_to_alice)
print("#28 Check if proof is valid")
if proof.proof_state == ProofState.Verified:
print("proof is verified!!")
else:
print("could not verify proof :(")
def convertInvite(invite):
return{ "id": invite["connReqId"],
"s" :{"d" :invite["senderDetail"]["DID"],
"dp":{"d":invite["senderDetail"]["agentKeyDlgProof"]["agentDID"],
"k":invite["senderDetail"]["agentKeyDlgProof"]["agentDelegatedKey"],
"s":invite["senderDetail"]["agentKeyDlgProof"]["signature"]
},
"l" :invite["senderDetail"]["logoUrl"],
"n" :invite["senderDetail"]["name"],
"v" :invite["senderDetail"]["verKey"]
},
"sa":{"d":invite["senderAgencyDetail"]["DID"],
"e":invite["senderAgencyDetail"]["endpoint"],
"v":invite["senderAgencyDetail"]["verKey"]
},
"sc":invite["statusCode"],
"sm":invite["statusMsg"],
"t" :invite["targetName"]
}
def proof_request(cred,institution_did):
proof_request = []
for key,val in cred.items():
proof_request.append({'name': key,'restrictions': [{'issuer_did':institution_did}]})
#print(json.dumps(proof_request))
return proof_request
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment