def isValid(proof) -> bool: ''' Checks whether the Proof was build with valid parameters or not. Parameters ---------- proof: Proof Proof to validate. Returns ------- bool A Boolean that returns True if the proof is valid, False if not. ''' if isinstance(proof, Proof): try: for l in proof.leaves: if not Utils.isHex(l) or len(l) != 64: return False for n in proof.nodes: if not Utils.isHex(n) or len(n) != 64: return False if (len(proof.depth) != (len(proof.leaves)+len(proof.nodes))*4) \ and Utils.isHex(proof.depth): return False n_elements = len(proof.leaves) + len(proof.nodes) if len(proof.depth) != (n_elements) * 4: return False offset = 1 if n_elements % 8 == 0: offset = 0 if len(proof.bitmap) // 2 < ( (n_elements + 8 * offset - n_elements % 8) // 8): return False return True except Exception: return False return False
def isValid(record) -> bool: ''' Given a Record returns True if its contents are valid to be sent to Bloocks's API or False otherwise. Parameters ---------- record : Record Record object. Returns ------- bool Boolean indicating if the Record is susceptible to be sent (True) or not (False). ''' if isinstance(record, Record): _record = record.getHash() if (len(_record) == 64 and Utils.isHex(_record)): return True return False
def test_is_hex_invalid_input(self): with self.assertRaises(TypeError): Utils.isHex(34)
def test_is_hex_invalid_char(self): self.assertFalse(Utils.isHex('gg'), 'The input was a hex, hence should be True.')
def test_is_hex_okay(self): self.assertTrue(Utils.isHex('0123456789abcdef'), 'The input was a hex, hence should be True.')