Exemplo n.º 1
0
 def test_l2block_signing_hashing(self):
     block = make_l2_block()
     sig = crypto.sign_l2_block(blake2b, secp256k1, key, block)
     self.assertEqual(
         sig,
         "MEQCIAOSqqOeS8Wdor6AN5gvvqhdh+acAumfq7ImQglllCDbAiBEKs1ZShUUGWipcGB3hpCQVLpqYV3nY0ZufyrNT6j8JQ=="
     )
     sig = crypto.sign_l2_block(sha256, secp256k1, key, block)
     self.assertEqual(
         sig,
         "MEQCIBsQGOpY1JHOhWbBpXmmhOjIwG3h9e6qUu7O6XkznbkFAiBbhfNiW7AwVf4Nx+I0SVnvGP83IJ61UeW5OQIlEnPbXw=="
     )
     sig = crypto.sign_l2_block(sha3_256, secp256k1, key, block)
     self.assertEqual(
         sig,
         "MEQCIAnruzXCTx9AYT6+I+GrMLIWXkIIlg2sN3YVThgDrTgnAiACwUgzj3e6Ata6spob0WZNNfAP0uVumLHDZLEqq0sgqw=="
     )
     block_hash, nonce = crypto.pow_l2_block(blake2b, block)
     self.assertEqual(block_hash,
                      "AIqD5gtCg5/dgZBV105dz+dX2R7gxdw3QzsTeBV/6O8=")
     self.assertEqual(nonce, 661)
     block_hash, nonce = crypto.pow_l2_block(sha256, block)
     self.assertEqual(block_hash,
                      "ANCH7glM8nNFL96qsIuIXPvEv91T9GX66gS7wwpNEjw=")
     self.assertEqual(nonce, 16)
     block_hash, nonce = crypto.pow_l2_block(sha3_256, block)
     self.assertEqual(block_hash,
                      "AFt3k9xMCZbxb+itQxxd71Dk9vyde5Svvgbm5uA4gyk=")
     self.assertEqual(nonce, 254)
Exemplo n.º 2
0
 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")
Exemplo n.º 3
0
 def test_l2block_trust_verifying(self):
     block = make_l2_block()
     sig = crypto.sign_l2_block(blake2b, secp256k1, key, block)
     block.proof = sig
     block.scheme = "trust"
     self.assertTrue(crypto.verify_l2_block_trust(blake2b, secp256k1, key.pubkey, block))
     valid_block = copy.deepcopy(block)
     block.dc_id = "forged"
     self.assertFalse(crypto.verify_l2_block_trust(blake2b, secp256k1, key.pubkey, block))
     block = copy.deepcopy(valid_block)
     block.block_id = "forged"
     self.assertFalse(crypto.verify_l2_block_trust(blake2b, secp256k1, key.pubkey, block))
     block = copy.deepcopy(valid_block)
     block.timestamp = "forged"
     self.assertFalse(crypto.verify_l2_block_trust(blake2b, secp256k1, key.pubkey, block))
     block = copy.deepcopy(valid_block)
     block.prev_proof = "forged=="
     self.assertFalse(crypto.verify_l2_block_trust(blake2b, secp256k1, key.pubkey, block))
     block = copy.deepcopy(valid_block)
     block.validations_str = '{"forged": true}'
     self.assertFalse(crypto.verify_l2_block_trust(blake2b, secp256k1, key.pubkey, block))
     block = copy.deepcopy(valid_block)
     block.l1_dc_id = "forged"
     self.assertFalse(crypto.verify_l2_block_trust(blake2b, secp256k1, key.pubkey, block))
     block = copy.deepcopy(valid_block)
     block.l1_block_id = "forged"
     self.assertFalse(crypto.verify_l2_block_trust(blake2b, secp256k1, key.pubkey, block))
     block = copy.deepcopy(valid_block)
     block.l1_proof = "forged"
     self.assertFalse(crypto.verify_l2_block_trust(blake2b, secp256k1, key.pubkey, block))
     block = copy.deepcopy(valid_block)
     block.proof = "MEQCIF+o3DHBiTbpw8X6W4/yOuPF/FfANIiNnG0mFLgjBAjuAiAe/QAHN8ufmHMeRvHFItdrzVHFORGED4/msipzFORGED=="
     self.assertFalse(crypto.verify_l2_block_trust(blake2b, secp256k1, key.pubkey, block))