def test_proof_validation_for_empty_tree(tree): """Tests proof-validation for proofs provided by empty trees """ audit_proof = tree.auditProof(arg=0) consistency_proof = tree.consistencyProof(old_hash=tree.rootHash(), sublength=0) assert validateProof(target_hash=b'anything...', proof=audit_proof) is False and validateProof( target_hash=b'anything...', proof=consistency_proof) is True
def test_validation_get_receipt(): tree = MerkleTree(*['%d-th record' % _ for _ in range(5)]) audit_proof = tree.auditProof(b'2-th record') receipt = validateProof(target=tree.rootHash, proof=audit_proof, get_receipt=True, dirpath=os.path.join(os.path.dirname(__file__), 'receipts')) receipt_path = os.path.join(os.path.dirname(__file__), 'receipts', '%s.json' % receipt.header['uuid']) with open(receipt_path) as __file: clone = json.load(__file) assert receipt.serialize() == clone
def verify_proof(self, data, merkle_root=None): """Verify a proof Args: data (dict): A Merkle proof to verify. merkle_root (str): A merkle root to proof. Returns: bool. """ if not merkle_root: merkle_root = self.tree.rootHash else: if isinstance(merkle_root, str): merkle_root = merkle_root.encode() p = Proof(from_dict=data) return validateProof(target=merkle_root, proof=p)
def test_false_audit_validateProof(tree, proof): assert not validateProof(proof, tree.rootHash)
def test_validateResponse(challenge): proof = tree.merkleProof(challenge) commitment = proof.header['commitment'] assert validateProof(proof) is validateProof(proof, commitment)
def test_true_consistency_validateProof(tree, consistency_proof): assert validateProof(consistency_proof, tree.rootHash)
def test_consistency_proof_validation_with_sublength_equal_to_power_of_2( tree, later_state): assert validateProof(later_state.consistencyProof(tree.rootHash), later_state.rootHash)
def test_consistency_proof_validation_with_sublength_equal_to_power_of_2( tree, later_state): assert validateProof( target_hash=later_state.rootHash(), proof=later_state.consistencyProof(old_hash=tree.rootHash(), sublength=tree.length())) is True
"checksum": m.hexdigest() } } proof_payload = { "jsonrpc": "2.0", "id": 0, "method": "proof", "params": { "api_key": api_key, "checksum": m.hexdigest() } } # Submits a file by digest to the calendar server for timestamping. Caches the UUID response object to use later for requesting the proof r = requests.post(submit_route, data=json.dumps(submit_payload)) print(r.text) # Gets the Proof based on the UUID response proof_resp = requests.post(proof_route, data=json.dumps(proof_payload)) proof_json = json.loads(proof_resp.text)["result"] print(proof_json) # Reconstructs the tree from the recieved proof to check it is valid print("Validating proof...") Proof_obj = pymerkle.Proof.deserialize(proof_json) if pymerkle.validateProof(Proof_obj): print("Proof is valid given parameters") else: print("invalid proof")
def test_true_consistency_validateProof(_tree, _consistency_proof): assert validateProof(_tree.rootHash, _consistency_proof)
def test_true_audit_validateProof(_tree, _audit_proof): assert validateProof(_tree.rootHash, _audit_proof)
def test_consistency_proof_validation_for_non_empty_tree( consistency_proof, target_hash, expected): assert validateProof(target_hash=target_hash, proof=consistency_proof) is expected
def test_record_based_audit_proof_validation_for_non_empty_tree( audit_proof, expected): assert validateProof(target_hash=small_tree.rootHash(), proof=audit_proof) is expected
def test_index_based_audit_proof_validation_for_non_empty_tree( audit_proof, target_hash, expected): assert validateProof(target_hash=target_hash, proof=audit_proof) is expected
def test_true_audit_validateProof(tree, proof): assert validateProof(proof, tree.rootHash)
"""pymerkle demo""" from pymerkle import MerkleTree, validateProof if __name__ == '__main__': tree = MerkleTree(hash_type='sha256', encoding='utf-8', raw_bytes=True, security=True) for i in range(7): tree.encryptRecord('%d-th record' % i) print(repr(tree)) challenge = { 'checksum': '45c44059cf0f5a447933f57d851a6024ac78b44a41603738f563bcbf83f35d20' } proof = tree.merkleProof(challenge) print(proof) assert validateProof(proof) receipt = validateProof(proof, get_receipt=True) print(receipt)
def test_false_audit_validateProof(_tree, _audit_proof): assert not validateProof(_tree.rootHash, _audit_proof)