This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
schema.py
132 lines (99 loc) · 4.46 KB
/
schema.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import json
import os
import requests
from django.conf import settings
from .agent import Issuer
from von_agent.util import encode
from . import eventloop
import logging
logger = logging.getLogger(__name__)
TOB_BASE_URL = os.getenv('THE_ORG_BOOK_BASE_URL')
def claim_value_pair(plain):
return [str(plain), encode(plain)]
class SchemaManager():
claim_def_json = None
def __init__(self):
schemas_path = os.path.abspath(settings.BASE_DIR + '/schemas.json')
try:
with open(schemas_path, 'r') as schemas_file:
schemas_json = schemas_file.read()
except FileNotFoundError as e:
logger.error('Could not find schemas.json. Exiting.')
raise
self.schemas = json.loads(schemas_json)
if os.getenv('PYTHON_ENV') == 'development':
for schema in self.schemas:
schema['version'] = '0.0.0'
def __log_json(self, heading, data):
logger.debug(
"\n============================================================================\n" +
"{0}\n".format(heading) +
"----------------------------------------------------------------------------\n" +
"{0}\n".format(json.dumps(data, indent=2)) +
"============================================================================\n")
return
def publish_schema(self, schema):
async def run(schema):
async with Issuer() as issuer:
# Check if schema exists on ledger
schema_json = await issuer.get_schema(
issuer.did, schema['name'], schema['version'])
# If not, send the schema to the ledger, then get result
if not json.loads(schema_json):
await issuer.send_schema(json.dumps(schema))
schema_json = await issuer.get_schema(
issuer.did, schema['name'], schema['version'])
schema = json.loads(schema_json)
# Check if claim definition has been published.
# If not then publish.
claim_def_json = await issuer.get_claim_def(
schema['seqNo'], issuer.did)
if not json.loads(claim_def_json):
await issuer.send_claim_def(schema_json)
return eventloop.do(run(schema))
def submit_claim(self, schema, claim):
async def run(schema, claim):
async with Issuer() as issuer:
for key, value in claim.items():
claim[key] = claim_value_pair(value) if value else \
claim_value_pair("")
self.__log_json('Claim:', claim)
self.__log_json('Schema:', schema)
# We need schema from ledger
schema_json = await issuer.get_schema(
issuer.did, schema['name'], schema['version'])
schema = json.loads(schema_json)
self.__log_json('Schema:', schema)
claim_def_json = await issuer.get_claim_def(
schema['seqNo'], issuer.did)
self.__log_json('Requesting Claim Request:',
{
'did': issuer.did,
'seqNo': schema['seqNo'],
'claim_def': json.loads(claim_def_json)
})
response = requests.post(
TOB_BASE_URL + '/bcovrin/generate-claim-request',
json={
'did': issuer.did,
'seqNo': schema['seqNo'],
'claim_def': claim_def_json
}
)
# Build claim
claim_request = response.json()
claim_request_json = json.dumps(claim_request)
self.__log_json('Claim Request Json:', claim_request)
(_, claim_json) = await issuer.create_claim(
claim_request_json, claim)
self.__log_json('Claim Json:', json.loads(claim_json))
# Send claim
response = requests.post(
TOB_BASE_URL + '/bcovrin/store-claim',
json={
'claim_type': schema['data']['name'],
'claim_data': json.loads(claim_json)
}
)
return response.json()
return eventloop.do(run(schema, claim))