def verifyProof(self, proof: Proof) -> Record: if not Proof.isValid(proof): raise ProofVerificationException( 'Input Proof is not valid for a verification process.') leaves = proof.leaves hashes = proof.nodes depth_bytes = bytes.fromhex(proof.depth) depth = [ int.from_bytes(depth_bytes[i * 2:i * 2 + 2], 'big') for i in range(0, len(depth_bytes) // 2) ] bitmap = [b for b in bytes.fromhex(proof.bitmap)] it_leaves = 0 it_hashes = 0 stack = [] while it_hashes < len(hashes) or it_leaves < len(leaves): act_depth = depth[it_hashes + it_leaves] if (bitmap[int((it_hashes + it_leaves) / 8)] & (1 << (7 - ((it_hashes + it_leaves) % 8)))) > 0: act_hash = hashes[it_hashes] it_hashes += 1 else: act_hash = leaves[it_leaves] it_leaves += 1 while len(stack) > 0 and stack[len(stack) - 1][1] == act_depth: last_hash = stack.pop() act_hash = (Record.fromHex(last_hash[0] + act_hash).getHash()) act_depth -= 1 stack.append((act_hash, act_depth)) return Record.fromHash(stack[0][0])
def test_from_hash(self): self.assertEqual( Record.fromHash('test_hash')._Record__hash, Record('test_hash')._Record__hash, "Records do not match.")