Esempio n. 1
0
 def test_l1block_signing_hashing(self):
     block = make_l1_block()
     sig = crypto.sign_l1_block(blake2b, secp256k1, key, block)
     self.assertEqual(
         sig,
         "MEUCIQD9CaNsrMVCF75kuZFLZxbLhfv+CPJm1VO/aMecBAL01AIgUotuERB38vNzM4A2emuWTwTAVPxoALngsrld1A2qEvk="
     )
     sig = crypto.sign_l1_block(sha256, secp256k1, key, block)
     self.assertEqual(
         sig,
         "MEUCIQDjh8wpWg7NzkfqfSfVaCITXBQWp9ZsYDz+JFqEumLgXwIgLeohzgIQF6rUDN3cT7ft4wB8LKyZMrzpV66907o7gdw="
     )
     sig = crypto.sign_l1_block(sha3_256, secp256k1, key, block)
     self.assertEqual(
         sig,
         "MEQCIHt4Pk7bv+b3B2byjFwHDg9QezFfEMfB97RheamjQZqeAiBjmCOseeIzvHYfObJB4NhLVxvt+oRV4vYwQV+a4ye+5w=="
     )
     block_hash, nonce = crypto.pow_l1_block(blake2b, block)
     self.assertEqual(block_hash,
                      "AByAk2FJJSyay0mb5jG6Zmtw08ZKN2M9TahZ95+6Ec0=")
     self.assertEqual(nonce, 328)
     block_hash, nonce = crypto.pow_l1_block(sha256, block)
     self.assertEqual(block_hash,
                      "AL+iuhHcC51x065CbYCR+0LchibuF8x98SgEi+WW52g=")
     self.assertEqual(nonce, 246)
     block_hash, nonce = crypto.pow_l1_block(sha3_256, block)
     self.assertEqual(block_hash,
                      "AG2Rlpw0bNSz3q5DJx/wOk4i3fiNfVi3P6D52sn7bhY=")
     self.assertEqual(nonce, 509)
Esempio 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")
Esempio n. 3
0
 def test_l1block_trust_verifying(self):
     block = make_l1_block()
     sig = crypto.sign_l1_block(blake2b, secp256k1, key, block)
     block.proof = sig
     block.scheme = "trust"
     self.assertTrue(crypto.verify_l1_block_trust(blake2b, secp256k1, key.pubkey, block))
     valid_block = copy.deepcopy(block)
     block.dc_id = "forged"
     self.assertFalse(crypto.verify_l1_block_trust(blake2b, secp256k1, key.pubkey, block))
     block = copy.deepcopy(valid_block)
     block.block_id = "forged"
     self.assertFalse(crypto.verify_l1_block_trust(blake2b, secp256k1, key.pubkey, block))
     block = copy.deepcopy(valid_block)
     block.timestamp = "forged"
     self.assertFalse(crypto.verify_l1_block_trust(blake2b, secp256k1, key.pubkey, block))
     block = copy.deepcopy(valid_block)
     block.prev_id = "forged"
     self.assertFalse(crypto.verify_l1_block_trust(blake2b, secp256k1, key.pubkey, block))
     block = copy.deepcopy(valid_block)
     block.prev_proof = "forged=="
     self.assertFalse(crypto.verify_l1_block_trust(blake2b, secp256k1, key.pubkey, block))
     block = copy.deepcopy(valid_block)
     block.stripped_transactions = ["some", "forged", "txns"]
     self.assertFalse(crypto.verify_l1_block_trust(blake2b, secp256k1, key.pubkey, block))
     block = copy.deepcopy(valid_block)
     block.proof = "MEQCIF+o3DHBiTbpw8X6W4/yOuPF/FfANIiNnG0mFLgjBAjuAiAe/QAHN8ufmHMeRvHFItdrzVHFORGED4/msipzFORGED=="
     self.assertFalse(crypto.verify_l1_block_trust(blake2b, secp256k1, key.pubkey, block))