def test_valid_sender(self): # Confirm no error when correct public key is used msg = b'this is a pretend merkle tree hash' sk, vk = ED25519Wallet.new() signature = ED25519Wallet.sign(sk, msg) timestamp = 'now' MerkleSignature.create(sig_hex=signature, timestamp=timestamp, sender=vk) # no error thrown
def test_invalid_signature_bad_length(self): # Test signature incorrect length (but valid hex) sk, vk = ED25519Wallet.new() timestamp = 'now' sig = ''.join(('A' for _ in range(100))) wrong_len = MerkleSignature.create(sig_hex=sig, timestamp=timestamp, sender=vk, validate=False) self.assertRaises(Exception, wrong_len.validate)
def test_verify_wrong_ms(self): # Test merkle tree validation returns false for incorrect verifying (public) key msg = b'this is a pretend merkle tree hash' timestamp = 'now' sk, vk = ED25519Wallet.new() signature = ED25519Wallet.sign(sk, msg) ms = MerkleSignature.create(sig_hex=signature, timestamp=timestamp, sender=vk) sk1, vk1 = ED25519Wallet.new() self.assertFalse(ms.verify(msg, vk1))
def test_verify_valid_ms(self): """ Tests that MerkleSignature.verify(...) returns true given a proper msg and vk """ # Test merkle tree verify() validates correct verifying (public) keys msg = b'this is a pretend merkle tree hash' timestamp = 'now' sk, vk = ED25519Wallet.new() signature = ED25519Wallet.sign(sk, msg) ms = MerkleSignature.create(sig_hex=signature, timestamp=timestamp, sender=vk) self.assertTrue(ms.verify(msg))
def test_invalid_timestamp(self): """ Test that if the timestamp field is not formatted as expected an error will be thrown """ msg = b'this is a pretend merkle tree hash' sk, vk = ED25519Wallet.new() signature = ED25519Wallet.sign(sk, msg) timestamp = 99 self.assertRaises( TypeError, MerkleSignature.create(sig_hex=signature, timestamp=timestamp, sender=vk))
def _create_merkle_sig(self, msg: bytes): """ Helper method to create a MerkleSignature and wallet keys :return: A tuple container (MerkleSignature, signing_key, verifying_key) """ assert type(msg) == bytes, "Must pass in bytes" sk, vk = ED25519Wallet.new() signature = ED25519Wallet.sign(sk, msg) ms = MerkleSignature.create(sig_hex=signature, timestamp=TIMESTAMP, sender=vk) return ms, sk, vk
def test_invalid_sender_bad_hash(self): # Test an error is thrown when created with a sender of not valid hash msg = b'this is a pretend merkle tree hash' sk, vk = ED25519Wallet.new() signature = ED25519Wallet.sign(sk, msg) timestamp = 'now' vk_bad_hash = ''.join( 'Z' for _ in range(64)) # verifying (public) key with bad hash self.assertRaises( Exception, MerkleSignature.create(sig_hex=signature, timestamp=timestamp, sender=vk_bad_hash, validate=False))
def test_valid_creation(self): """ Tests that a MerkleSignature created with some argument has the expected properties """ msg = b'this is a pretend merkle tree hash' timestamp = 'now' sk, vk = ED25519Wallet.new() signature = ED25519Wallet.sign(sk, msg) ms = MerkleSignature.create(sig_hex=signature, timestamp=timestamp, sender=vk) self.assertEqual(ms.signature, signature) self.assertEqual(ms.timestamp, timestamp) self.assertEqual(ms.sender, vk)
def test_invalid_signature_nonhex(self): """ Tests that an error is raised during validation if an invalid signature is passed (nonhex, or length != 128) """ msg = b'this is a pretend merkle tree hash' timestamp = 'now' sk, vk = ED25519Wallet.new() # Test nonhex signature (but valid length) sig = ''.join(('X' for _ in range(128))) nonhex = MerkleSignature.create(sig_hex=sig, timestamp=timestamp, sender=vk, validate=False) self.assertRaises(Exception, nonhex.validate)
def test_invalid_sender_wrong_sender(self): """ Tests that an error is raised during creation if an invalid sender field is passed in. A sender should be a 64 character hex string verifying key. """ # Test an error is thrown when MerkleSignature created with a sender that is not the correct public key msg = b'this is a pretend merkle tree hash' sk, vk = ED25519Wallet.new() signature = ED25519Wallet.sign(sk, msg) timestamp = 'now' vk_bad = ED25519Wallet.new()[1] # different verifying (public) key bad_public_key = MerkleSignature.create(sig_hex=signature, timestamp=timestamp, sender=vk_bad) self.assertRaises(Exception, bad_public_key)
def test_serialization(self): """ Tests that a created block data reply successfully serializes and deserializes. The deserialized object should have the same properties as the original one before it was serialized. """ msg = b'this is a pretend merkle tree hash' sk, vk = ED25519Wallet.new() signature = ED25519Wallet.sign(sk, msg) timestamp = 'now' valid_merkle_sig = MerkleSignature.create(sig_hex=signature, timestamp=timestamp, sender=vk) valid_merkle_sig_serialized = valid_merkle_sig.serialize() clone = MerkleSignature.from_bytes(valid_merkle_sig_serialized) self.assertEqual(valid_merkle_sig.signature, clone.signature) self.assertEqual(valid_merkle_sig.timestamp, clone.timestamp) self.assertEqual(valid_merkle_sig.sender, clone.sender)