Example #1
0
 def verify_block(self, block: "model.BlockModel") -> bool:  # noqa: C901
     """Verify a block with this class' keys
     Args:
         block: BlockModel to verify
     Returns:
         Boolean if valid block (according to these keys)
     Raises:
         NotImplementedError when invalid scheme on self
     """
     if self.scheme == SupportedSchemes.trust and self.pub is None:
         RuntimeError(
             "No public key has been set for verifying block signature")
     if self.level == 1:
         if self.scheme == SupportedSchemes.trust:
             return crypto.verify_l1_block_trust(
                 self.hash, self.encryption, self.pub,
                 cast("l1_block_model.L1BlockModel", block))
         elif self.scheme == SupportedSchemes.work:
             return crypto.verify_l1_block_pow(
                 self.hash, cast("l1_block_model.L1BlockModel", block))
         else:
             raise NotImplementedError(
                 f"Proof scheme {self.scheme} not implemented")
     elif self.level == 2:
         if self.scheme == SupportedSchemes.trust:
             return crypto.verify_l2_block_trust(
                 self.hash, self.encryption, self.pub,
                 cast("l2_block_model.L2BlockModel", block))
         elif self.scheme == SupportedSchemes.work:
             return crypto.verify_l2_block_pow(
                 self.hash, cast("l2_block_model.L2BlockModel", block))
         else:
             raise NotImplementedError(
                 f"Proof scheme {self.scheme} not implemented")
     elif self.level == 3:
         if self.scheme == SupportedSchemes.trust:
             return crypto.verify_l3_block_trust(
                 self.hash, self.encryption, self.pub,
                 cast("l3_block_model.L3BlockModel", block))
         elif self.scheme == SupportedSchemes.work:
             return crypto.verify_l3_block_pow(
                 self.hash, cast("l3_block_model.L3BlockModel", block))
         else:
             raise NotImplementedError(
                 f"Proof scheme {self.scheme} not implemented")
     elif self.level == 4:
         if self.scheme == SupportedSchemes.trust:
             return crypto.verify_l4_block_trust(
                 self.hash, self.encryption, self.pub,
                 cast("l4_block_model.L4BlockModel", block))
         elif self.scheme == SupportedSchemes.work:
             return crypto.verify_l4_block_pow(
                 self.hash, cast("l4_block_model.L4BlockModel", block))
         else:
             raise NotImplementedError(
                 f"Proof scheme {self.scheme} not implemented")
     elif self.level == 5:
         if self.scheme == SupportedSchemes.trust:
             return crypto.verify_l5_block_trust(
                 self.hash, self.encryption, self.pub,
                 cast("l5_block_model.L5BlockModel", block))
         else:
             raise NotImplementedError(
                 f"Proof scheme {self.scheme} not implemented")
     else:
         raise exceptions.InvalidNodeLevel(
             f"Node level {self.level} nonexistent")
Example #2
0
 def test_l3block_trust_verifying_v2(self):
     block = make_l3_block_v2()
     sig = crypto.sign_l3_block(blake2b, secp256k1, key, block)
     block.proof = sig
     block.scheme = "trust"
     self.assertTrue(
         crypto.verify_l3_block_trust(blake2b, secp256k1, key.pubkey,
                                      block))
     valid_block = copy.deepcopy(block)
     block.dc_id = "forged"
     self.assertFalse(
         crypto.verify_l3_block_trust(blake2b, secp256k1, key.pubkey,
                                      block))
     block = copy.deepcopy(valid_block)
     block.block_id = "forged"
     self.assertFalse(
         crypto.verify_l3_block_trust(blake2b, secp256k1, key.pubkey,
                                      block))
     block = copy.deepcopy(valid_block)
     block.timestamp = "forged"
     self.assertFalse(
         crypto.verify_l3_block_trust(blake2b, secp256k1, key.pubkey,
                                      block))
     block = copy.deepcopy(valid_block)
     block.prev_proof = "forged=="
     self.assertFalse(
         crypto.verify_l3_block_trust(blake2b, secp256k1, key.pubkey,
                                      block))
     block = copy.deepcopy(valid_block)
     block.l1_dc_id = "forged"
     self.assertFalse(
         crypto.verify_l3_block_trust(blake2b, secp256k1, key.pubkey,
                                      block))
     block = copy.deepcopy(valid_block)
     block.l1_block_id = "forged"
     self.assertFalse(
         crypto.verify_l3_block_trust(blake2b, secp256k1, key.pubkey,
                                      block))
     block = copy.deepcopy(valid_block)
     block.l1_proof = "forged"
     self.assertFalse(
         crypto.verify_l3_block_trust(blake2b, secp256k1, key.pubkey,
                                      block))
     block = copy.deepcopy(valid_block)
     block.ddss = "forged"
     self.assertFalse(
         crypto.verify_l3_block_trust(blake2b, secp256k1, key.pubkey,
                                      block))
     block = copy.deepcopy(valid_block)
     block.l2_count = "forged"
     self.assertFalse(
         crypto.verify_l3_block_trust(blake2b, secp256k1, key.pubkey,
                                      block))
     block = copy.deepcopy(valid_block)
     block.regions = ["forged", "stuff"]
     self.assertFalse(
         crypto.verify_l3_block_trust(blake2b, secp256k1, key.pubkey,
                                      block))
     block = copy.deepcopy(valid_block)
     block.clouds = ["forged"]
     self.assertFalse(
         crypto.verify_l3_block_trust(blake2b, secp256k1, key.pubkey,
                                      block))
     block = copy.deepcopy(valid_block)
     block.proof = "MEQCIF+o3DHBiTbpw8X6W4/yOuPF/FfANIiNnG0mFLgjBAjuAiAe/QAHN8ufmHMeRvHFItdrzVHFORGED4/msipzFORGED=="
     self.assertFalse(
         crypto.verify_l3_block_trust(blake2b, secp256k1, key.pubkey,
                                      block))