Skip to content

Commit

Permalink
refactor(Proof): abstract proof handler to centralize proof logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lemoustachiste committed May 9, 2022
1 parent 1d2e678 commit 4866a3b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 15 deletions.
17 changes: 3 additions & 14 deletions cert_issuer/certificate_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
import logging

from cert_schema import normalize_jsonld
from cert_schema import validate_v2
from cert_issuer import helpers
from cert_issuer.chained_proof_2021 import ChainedProof2021
from cert_issuer.proof_handler import ProofHandler
from pycoin.serialize import b2h
from cert_issuer.models import CertificateHandler, BatchHandler

Expand All @@ -23,12 +22,7 @@ def add_proof(self, certificate_metadata, merkle_proof):
:return:
"""
certificate_json = self._get_certificate_to_issue(certificate_metadata)
if 'proof' in certificate_json:
initial_proof = certificate_json['proof']
certificate_json['proof'] = [initial_proof, ChainedProof2021(initial_proof, merkle_proof).toJsonObject()]
else:
certificate_json['proof'] = merkle_proof
print(certificate_json['proof'])
certificate_json = ProofHandler().add_proof(certificate_json, merkle_proof)

with open(certificate_metadata.blockchain_cert_file_name, 'w') as out_file:
out_file.write(json.dumps(certificate_json))
Expand All @@ -44,12 +38,7 @@ def get_byte_array_to_issue(self, certificate_json):
return normalized.encode('utf-8')

def add_proof(self, certificate_json, merkle_proof):
"""
:param certificate_metadata:
:param merkle_proof:
:return:
"""
certificate_json['signature'] = merkle_proof
certificate_json = ProofHandler().add_proof(certificate_json, merkle_proof)
return certificate_json

class CertificateBatchWebHandler(BatchHandler):
Expand Down
10 changes: 10 additions & 0 deletions cert_issuer/proof_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from cert_issuer.chained_proof_2021 import ChainedProof2021

class ProofHandler:
def add_proof (self, certificate_json, merkle_proof):
if 'proof' in certificate_json:
initial_proof = certificate_json['proof']
certificate_json['proof'] = [initial_proof, ChainedProof2021(initial_proof, merkle_proof).toJsonObject()]
else:
certificate_json['proof'] = merkle_proof
return certificate_json
2 changes: 1 addition & 1 deletion tests/test_certificate_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def test_web_add_proof(self):
certificate_json = {'kek': 'kek'}

return_cert = handler.add_proof(certificate_json, proof)
self.assertEqual(return_cert, {'kek':'kek', 'signature': {'a': 'merkel'}})
self.assertEqual(return_cert, {'kek':'kek', 'proof': {'a': 'merkel'}})

class DummyCertificateHandler(CertificateHandler):
def __init__(self):
Expand Down
40 changes: 40 additions & 0 deletions tests/test_proof_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import unittest
from cert_issuer.proof_handler import ProofHandler

class TestProofHandler(unittest.TestCase):
def multiple_two_chained_signature(self):
handler = ProofHandler()
fixture_initial_proof = {
'type': 'Ed25519Signature2020',
'created': '2022-05-02T16:36:22.933Z',
'verificationMethod': 'did:key:z6MkjHnntGvtLjwfAMHWTAXXGJHhVL3DPtaT9BHmyTjWpjqs#z6MkjHnntGvtLjwfAMHWTAXXGJHhVL3DPtaT9BHmyTjWpjqs',
'proofPurpose': 'assertionMethod',
'proofValue': 'zAvFt59599JweBZ4zPP6Ge8LhKgECtBvDRmjG5VQbgEkPCiyMcM9QAPanJgSCs6RRGcKu96qNpfmpe9eTygpFZP6'
}
fixture_certificate_json = {
'kek': 'kek',
'proof': fixture_initial_proof
}
fixture_proof = {
'type': 'MerkleProof2019',
'created': '2022-05-05T08:05:14.912828',
'proofValue': 'zMcm4LfQFUZkWZyLJp1bqtXF8vkZZwp79x7Nvt5BmN2XV4usLLtDoeqiq3et923mcWfXde4a3m4f57yUZcATCbBXV1byb5AXbV8EzT6E8B9JKf3scvxxZCBVePtV4SrhYysAiLNJ9N2R8LgnpJ47wnQHkaTB1AMxrcLEHUTxm4zJTtQqf9orDLf3L4VoLzmST7ZzsDjuX9cw2hZ3Aazhhjy7swG44xfF1PC73SyCv77pDnJ6BSHm3azmbVG6BXv1EPtwF4J1YRqwojBEWk9nDgduACR7b9qNhQ46ND4B5vL8p3LkqTh',
'proofPurpose': 'assertionMethod',
'verificationMethod': 'did:ion:EiA_Z6LQILbB2zj_eVrqfQ2xDm4HNqeJUw5Kj2Z7bFOOeQ#key-1'
}
output = handler.add_proof(fixture_certificate_json, fixture_proof)
self.assertEqual(output, {
'kek': 'kek',
'proof': [
fixture_initial_proof,
{
'type': 'ChainedProof2021',
'chainedProofType': 'MerkleProof2019',
'created': '2022-05-05T08:05:14.912828',
'previousProof': fixture_initial_proof,
'proofValue': 'zMcm4LfQFUZkWZyLJp1bqtXF8vkZZwp79x7Nvt5BmN2XV4usLLtDoeqiq3et923mcWfXde4a3m4f57yUZcATCbBXV1byb5AXbV8EzT6E8B9JKf3scvxxZCBVePtV4SrhYysAiLNJ9N2R8LgnpJ47wnQHkaTB1AMxrcLEHUTxm4zJTtQqf9orDLf3L4VoLzmST7ZzsDjuX9cw2hZ3Aazhhjy7swG44xfF1PC73SyCv77pDnJ6BSHm3azmbVG6BXv1EPtwF4J1YRqwojBEWk9nDgduACR7b9qNhQ46ND4B5vL8p3LkqTh',
'proofPurpose': 'assertionMethod',
'verificationMethod': 'did:ion:EiA_Z6LQILbB2zj_eVrqfQ2xDm4HNqeJUw5Kj2Z7bFOOeQ#key-1'
}
]
})

0 comments on commit 4866a3b

Please sign in to comment.