-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #232 from blockchain-certificates/poc/proof-chain
Feat: proofChain with chainedProof2021
- Loading branch information
Showing
14 changed files
with
468 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = '3.0.2' | ||
__version__ = '3.1.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import copy | ||
CHAINED_PROOF_TYPE = 'ChainedProof2021' | ||
|
||
class ChainedProof2021: | ||
type = '' | ||
verificationMethod = '' | ||
chainedProofType = '' | ||
created = '' | ||
proofPurpose = '' | ||
previousProof = '' | ||
proofValue = '' | ||
|
||
def __init__(self, previous_proof, current_proof): | ||
self.type = CHAINED_PROOF_TYPE | ||
self.create_proof_object(current_proof) | ||
self.set_previous_proof(previous_proof) | ||
|
||
def create_proof_object(self, current_proof): | ||
self.verificationMethod = current_proof['verificationMethod'] | ||
self.chainedProofType = current_proof['type'] | ||
self.created = current_proof['created'] | ||
self.proofPurpose = current_proof['proofPurpose'] | ||
self.proofValue = current_proof['proofValue'] | ||
|
||
def set_previous_proof(self, previous_proof): | ||
if previous_proof['type'] == CHAINED_PROOF_TYPE: | ||
previous_proof_to_store = copy.deepcopy(previous_proof) | ||
previous_proof_to_store['type'] = previous_proof['chainedProofType'] | ||
del previous_proof_to_store['chainedProofType'] | ||
del previous_proof_to_store['previousProof'] | ||
self.previousProof = previous_proof_to_store | ||
else: | ||
self.previousProof = previous_proof | ||
|
||
def to_json_object(self): | ||
return self.__dict__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import json | ||
import os | ||
from cert_schema import normalize_jsonld, extend_preloaded_context | ||
|
||
from cert_issuer.config import CONFIG | ||
|
||
|
||
class JSONLDHandler: | ||
@staticmethod | ||
def normalize_to_utf8(certificate_json): | ||
JSONLDHandler.preload_contexts() | ||
normalized = normalize_jsonld(certificate_json, detect_unmapped_fields=False) | ||
return normalized.encode('utf-8') | ||
|
||
@staticmethod | ||
def preload_contexts(): | ||
if CONFIG.context_urls is None or CONFIG.context_file_paths is None: | ||
return | ||
for (url, path) in zip(CONFIG.context_urls, CONFIG.context_file_paths): | ||
with open(os.path.join(os.getcwd(), path)) as context_file: | ||
context_data = json.load(context_file) | ||
extend_preloaded_context(url, context_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from cert_issuer.chained_proof_2021 import ChainedProof2021 | ||
from cert_schema import ContextUrls | ||
from cert_issuer.utils import array_intersect | ||
|
||
class ProofHandler: | ||
def __init__(self): | ||
self.contextUrls = ContextUrls() | ||
|
||
def add_proof(self, certificate_json, merkle_proof): | ||
if 'proof' in certificate_json: | ||
if not isinstance(certificate_json['proof'], list): | ||
# convert proof to list | ||
initial_proof = certificate_json['proof'] | ||
certificate_json['proof'] = [initial_proof] | ||
previous_proof = certificate_json['proof'][-1] | ||
certificate_json['proof'].append(ChainedProof2021(previous_proof, merkle_proof).to_json_object()) | ||
self.update_context_for_chained_proof(certificate_json) | ||
else: | ||
certificate_json['proof'] = merkle_proof | ||
self.update_context_for_single_proof(certificate_json) | ||
return certificate_json | ||
|
||
def update_context_for_chained_proof(self, certificate_json): | ||
context = certificate_json['@context'] | ||
if self.contextUrls.merkle_proof_2019() not in context: | ||
context.append(self.contextUrls.merkle_proof_2019()) | ||
|
||
if self.contextUrls.chained_proof_2021() not in context: | ||
context.append(self.contextUrls.chained_proof_2021()) | ||
|
||
if array_intersect(self.contextUrls.v3_all(), context): | ||
for v3_context in self.contextUrls.v3_all(): | ||
if v3_context in context: | ||
index = context.index(v3_context) | ||
del context[index] | ||
context.append(self.contextUrls.v3_1_canonical()) | ||
|
||
def update_context_for_single_proof(self, certificate_json): | ||
context = certificate_json['@context'] | ||
if array_intersect(self.contextUrls.v3_1_all(), context): | ||
if self.contextUrls.merkle_proof_2019() not in context: | ||
context.append(self.contextUrls.merkle_proof_2019()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def array_intersect (a, b): | ||
return list(filter(lambda x: x in a, b)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
cert-core>=3.0.0b1 | ||
cert-schema>=3.0.3 | ||
cert-schema>=3.2.1 | ||
merkletools==1.0.3 | ||
configargparse==0.12.0 | ||
glob2==0.6 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.