cic-cli/cic/proof.py

128 lines
3.1 KiB
Python
Raw Normal View History

2021-10-09 19:56:29 +02:00
# standard imports
import os
import json
2021-10-09 20:37:54 +02:00
import logging
2021-10-10 21:30:18 +02:00
import tempfile
import cbor2
# external imports
from hexathon import strip_0x
2021-10-19 11:18:22 +02:00
from cic_types import MetadataPointer
from cic_types.processor import generate_metadata_pointer
from cic_types.ext.metadata import MetadataRequestsHandler
2021-10-09 19:56:29 +02:00
# local imports
from .base import *
2021-10-19 11:18:22 +02:00
from cic.output import OutputWriter
2021-10-09 19:56:29 +02:00
2021-10-09 20:37:54 +02:00
logg = logging.getLogger(__name__)
2021-10-09 19:56:29 +02:00
class Proof(Data):
def __init__(self, path='.', attachments=None, writer=None):
2021-10-09 19:56:29 +02:00
super(Proof, self).__init__()
self.namespace = 'ge'
self.description = None
self.path = path
self.writer = writer
2021-10-10 21:30:18 +02:00
self.extra_attachments = attachments
self.attachments = {}
2021-10-09 19:56:29 +02:00
self.proof_path = os.path.join(self.path, 'proof.json')
2021-10-10 21:30:18 +02:00
self.temp_proof_path = tempfile.mkstemp()[1]
2021-10-09 19:56:29 +02:00
def load(self):
2021-10-09 20:37:54 +02:00
super(Proof, self).load()
2021-10-09 19:56:29 +02:00
2021-10-09 20:37:54 +02:00
f = open(self.proof_path, 'r')
o = json.load(f)
2021-10-09 19:56:29 +02:00
f.close()
2021-10-09 20:37:54 +02:00
self.set_version(o['version'])
2021-10-09 19:56:29 +02:00
self.description = o['description']
self.namespace = o['namespace']
2021-10-14 15:43:23 +02:00
self.issuer = o['issuer']
2021-10-09 19:56:29 +02:00
self.inited = True
def start(self):
super(Proof, self).start()
proof_template_file_path = os.path.join(data_dir, 'proof_template_v{}.json'.format(self.version()))
f = open(proof_template_file_path)
o = json.load(f)
f.close()
f = open(self.proof_path, 'w')
2021-10-18 10:47:48 +02:00
json.dump(o, f, sort_keys=True, indent="\t")
2021-10-09 19:56:29 +02:00
f.close()
2021-10-10 21:30:18 +02:00
def asdict(self):
return {
'version': self.version(),
'namespace': self.namespace,
'description': self.description,
2021-10-14 15:43:23 +02:00
'issuer': self.issuer,
2021-10-10 21:30:18 +02:00
}
def __get_ordered_hashes(self):
ks = list(self.attachments.keys())
ks.sort()
return ks
def get(self):
v = self.asdict()
b = cbor2.dumps(v)
f = open(self.temp_proof_path, 'wb')
f.write(b)
f.close()
hsh = self.hash(b).hex()
self.attachments[hsh] = self.temp_proof_path
2021-10-11 19:02:42 +02:00
logg.debug('cbor of {} is {} hashes to {}'.format(v, b.hex(), hsh))
2021-10-10 21:30:18 +02:00
if self.extra_attachments != None:
a = self.extra_attachments.asdict()
for k in a.keys():
self.attachments[k] = a[k]
hshs = self.__get_ordered_hashes()
2021-10-11 19:02:42 +02:00
return (hsh, hshs)
def process(self, token_address=None, writer=None):
if writer == None:
writer = self.writer
(hsh, hshs) = self.get()
hshs = list(map(strip_0x, hshs))
hshs_bin = list(map(bytes.fromhex, hshs))
hshs_cat = b''.join(hshs_bin)
2021-10-12 14:26:57 +02:00
f = open(self.temp_proof_path, 'rb')
v = f.read()
f.close()
writer.write(hsh, v)
2021-10-11 19:02:42 +02:00
r = self.hash(hshs_cat)
r_hex = r.hex()
logg.debug('generated proof {} for hashes {}'.format(r_hex, hshs))
writer.write(r_hex, hshs_cat)
2021-10-10 21:30:18 +02:00
2021-10-11 19:02:42 +02:00
return r_hex
2021-10-10 21:30:18 +02:00
2021-10-09 20:37:54 +02:00
def __str__(self):
2021-10-10 14:49:22 +02:00
return "description = {}\n".format(self.description)