-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdriver.py
57 lines (44 loc) · 1.47 KB
/
driver.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
#!/usr/bin/env python3
from ontology.sdk import Ontology
from ontology.vm import build_vm
from ontology.core.transaction import Transaction
import re
from aiohttp import web
import logging
__version__ = "0.1.0"
async def get_ddo(ont_id):
contract_address = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03'
version = b'\x00'
remote = 'http://dappnode1.ont.io:20336/'
sdk = Ontology(rpc_address=remote)
args = dict(ontid=ont_id.encode('utf-8'))
invoke_code = build_vm.build_native_invoke_code(
contract_address, version, 'getDocumentJson', args)
tx = Transaction(0, 0xd1, 0, 0, b'', invoke_code)
response = await sdk.aio_rpc.send_raw_transaction_pre_exec(tx)
ddo = response['Result']
return ddo
async def handle(request):
id = request.match_info.get('id', '')
if not re.match('^did:ont:(.*)$', id):
return web.Response()
ddo = await get_ddo(id)
if ddo:
res = bytes.fromhex(ddo).decode('utf-8')
else:
logging.info("cannot resolves %s", id)
res = None
# fix issue #1
headers = {'Content-Type': 'application/did+ld+json'}
return web.Response(text=res, headers=headers)
def run():
app = web.Application()
app.add_routes([
web.get('/1.0/identifiers/{id}', handle)
])
web.run_app(app)
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
logging.info("Start ontid driver")
run()