def test_l4block_signing_hashing(self): block = make_l4_block() sig = crypto.sign_l4_block(blake2b, secp256k1, key, block) self.assertEqual( sig, "MEUCIQDwOwwYlWivucunKJXm0KB+7BsEiHDmEKLg2wL6MKJIsQIgdtjZRBimLmsZjOcCkCmalLHf0sF2exAMsluuVxMaLvk=" ) sig = crypto.sign_l4_block(sha256, secp256k1, key, block) self.assertEqual( sig, "MEQCIHcVPm+NEIEJXCeoWgX94fo7Y+kMhZGKtroh94iNZTTbAiB7qWPI3B4vpj0Ir5+X0gHv/m9Xq6abQnWRagH64Xu4OA==" ) sig = crypto.sign_l4_block(sha3_256, secp256k1, key, block) self.assertEqual( sig, "MEQCIDGi9WC8097OtOUXahHm0/uJP5PUv6isk1NVvulbQ2c2AiBULyExoupsgJ/2KFKDQYHF1qQa5++cZxFxyqlXPpY0lA==" ) block_hash, nonce = crypto.pow_l4_block(blake2b, block) self.assertEqual(block_hash, "ACZ1yEGzRKKDxm9jqk5j8U6QRyeVHzqRjcS/G2gnEy8=") self.assertEqual(nonce, 82) block_hash, nonce = crypto.pow_l4_block(sha256, block) self.assertEqual(block_hash, "AEULvMdpuf6Sj7M3HxSCZLjQODViHxRRKMy4Ze0VrLY=") self.assertEqual(nonce, 3) block_hash, nonce = crypto.pow_l4_block(sha3_256, block) self.assertEqual(block_hash, "AOEPG+CTLsfaa92DpnOrkro+8WLGUKSKD28I+kqS9Lo=") self.assertEqual(nonce, 182)
def sign_block(self, signable_block: "model.BlockModel") -> str: """Sign a block with this class' keys Args: block: BlockModel to sign Returns: Base64 encoded string of the block signature Raises: exceptions.InvalidNodeLevel when invalid level on self RuntimeError when no private key is set on self """ if self.priv is None: raise RuntimeError("No private key has been set for signing") if self.level == 1: return crypto.sign_l1_block(self.hash, self.encryption, self.priv, signable_block) elif self.level == 2: return crypto.sign_l2_block(self.hash, self.encryption, self.priv, signable_block) elif self.level == 3: return crypto.sign_l3_block(self.hash, self.encryption, self.priv, signable_block) elif self.level == 4: return crypto.sign_l4_block(self.hash, self.encryption, self.priv, signable_block) elif self.level == 5: return crypto.sign_l5_block(self.hash, self.encryption, self.priv, signable_block) else: raise exceptions.InvalidNodeLevel( f"Node level {self.level} not implemented yet")
def test_l4block_trust_verifying(self): block = make_l4_block() sig = crypto.sign_l4_block(blake2b, secp256k1, key, block) block.proof = sig block.scheme = "trust" self.assertTrue(crypto.verify_l4_block_trust(blake2b, secp256k1, key.pubkey, block)) valid_block = copy.deepcopy(block) block.dc_id = "forged" self.assertFalse(crypto.verify_l4_block_trust(blake2b, secp256k1, key.pubkey, block)) block = copy.deepcopy(valid_block) block.block_id = "forged" self.assertFalse(crypto.verify_l4_block_trust(blake2b, secp256k1, key.pubkey, block)) block = copy.deepcopy(valid_block) block.timestamp = "forged" self.assertFalse(crypto.verify_l4_block_trust(blake2b, secp256k1, key.pubkey, block)) block = copy.deepcopy(valid_block) block.prev_proof = "forged==" self.assertFalse(crypto.verify_l4_block_trust(blake2b, secp256k1, key.pubkey, block)) block = copy.deepcopy(valid_block) block.l1_dc_id = "forged" self.assertFalse(crypto.verify_l4_block_trust(blake2b, secp256k1, key.pubkey, block)) block = copy.deepcopy(valid_block) block.l1_block_id = "forged" self.assertFalse(crypto.verify_l4_block_trust(blake2b, secp256k1, key.pubkey, block)) block = copy.deepcopy(valid_block) block.l1_proof = "forged" self.assertFalse(crypto.verify_l4_block_trust(blake2b, secp256k1, key.pubkey, block)) block = copy.deepcopy(valid_block) block.validations[0]["l3_dc_id"] = "forged" self.assertFalse(crypto.verify_l4_block_trust(blake2b, secp256k1, key.pubkey, block)) block = copy.deepcopy(valid_block) block.validations[0]["l3_block_id"] = "forged" self.assertFalse(crypto.verify_l4_block_trust(blake2b, secp256k1, key.pubkey, block)) block = copy.deepcopy(valid_block) block.validations[0]["l3_proof"] = "forged" self.assertFalse(crypto.verify_l4_block_trust(blake2b, secp256k1, key.pubkey, block)) block = copy.deepcopy(valid_block) block.validations[0]["valid"] = False self.assertFalse(crypto.verify_l4_block_trust(blake2b, secp256k1, key.pubkey, block)) block = copy.deepcopy(valid_block) block.proof = "MEQCIF+o3DHBiTbpw8X6W4/yOuPF/FfANIiNnG0mFLgjBAjuAiAe/QAHN8ufmHMeRvHFItdrzVHFORGED4/msipzFORGED==" self.assertFalse(crypto.verify_l4_block_trust(blake2b, secp256k1, key.pubkey, block))