def test_id():
    """Test get a random user_id"""
    user_id1 = helper.user.id()
    user_id2 = helper.user.id()
    assert isinstance(user_id1, str)
    assert isinstance(user_id2, str)
    assert PUBLIC_KEY_PATTERN.match(user_id1)
    assert PUBLIC_KEY_PATTERN.match(user_id2)
    assert user_id1 != user_id2
 def test_id(self):
     """Test get a random user_id"""
     self.assertTrue(callable(self.test.user.id))
     user_id1 = self.test.user.id()
     user_id2 = self.test.user.id()
     self.assertIsInstance(user_id1, str)
     self.assertIsInstance(user_id2, str)
     self.assertTrue(PUBLIC_KEY_PATTERN.match(user_id1))
     self.assertTrue(PUBLIC_KEY_PATTERN.match(user_id2))
     self.assertNotEqual(user_id1, user_id2)
def test_key():
    """Test get a generated keypair"""
    key1 = helper.user.key()
    key2 = helper.user.key()
    assert isinstance(key1, Key)
    assert isinstance(key2, Key)
    assert PUBLIC_KEY_PATTERN.match(key1.public_key)
    assert PUBLIC_KEY_PATTERN.match(key2.public_key)
    assert PRIVATE_KEY_PATTERN.match(key1.private_key)
    assert PRIVATE_KEY_PATTERN.match(key2.private_key)
    assert key1.public_key != key2.public_key
    assert key1.private_key != key2.private_key
Exemple #4
0
 def assertIsPublicKeyHex(self, key):
     """Sanity checks a hexidecimal string public key"""
     self.assertIsInstance(key, str)
     self.assertTrue(PUBLIC_KEY_PATTERN.match(key))
     key = Secp256k1PublicKey.from_hex(key)
     self.assertIsPublicKeySecp256k1(key)
     return key
 def base_validate_state(self, state, message, signer):
     if signer is None:
         raise ValueError("Signer is required")
     if message is None:
         raise ValueError("Message is required")
     if not isinstance(signer, str) and PUBLIC_KEY_PATTERN.match(signer):
         raise TypeError("Expected signer to be a public key")
     if state is None:
         raise ValueError("State is required")
Exemple #6
0
 def test_key_class_given_private_key(self):
     """Test initializing the key class with a given private key"""
     private_key = Secp256k1PrivateKey.new_random()
     txn_key = Key(private_key.as_hex())
     self.assertEqual(len(txn_key.private_key), PRIVATE_KEY_LENGTH * 2)
     self.assertTrue(PRIVATE_KEY_PATTERN.match(txn_key.private_key))
     self.assertEqual(txn_key.private_key, private_key.as_hex())
     self.assertEqual(len(txn_key.public_key), PUBLIC_KEY_LENGTH * 2)
     self.assertTrue(PUBLIC_KEY_PATTERN.match(txn_key.public_key))
 def base_validate(self, message, signer=None):
     if not isinstance(message, self.message_proto):
         raise TypeError("Expected message to be {}".format(
             self.message_proto))
     if (signer is not None and not isinstance(signer, Key)
             and not (isinstance(signer, str)
                      and PUBLIC_KEY_PATTERN.match(signer))):
         raise TypeError("Expected signer to be a keypair or a public key")
     if isinstance(signer, Key):
         signer = signer.public_key
     return signer
 def test_key_class_random_keys(self):
     value1 = Key()
     value2 = Key()
     self.assertTrue(isinstance(value1, Key))
     self.assertTrue(isinstance(value2, Key))
     self.assertNotEqual(value1, value2)
     self.assertIsInstance(value1.public_key, str)
     self.assertIsInstance(value2.public_key, str)
     self.assertIsInstance(value1.private_key, str)
     self.assertIsInstance(value2.private_key, str)
     self.assertEqual(len(value1.public_key), PUBLIC_KEY_LENGTH * 2)
     self.assertEqual(len(value2.public_key), PUBLIC_KEY_LENGTH * 2)
     self.assertEqual(len(value1.private_key), PRIVATE_KEY_LENGTH * 2)
     self.assertEqual(len(value2.private_key), PRIVATE_KEY_LENGTH * 2)
     self.assertTrue(PUBLIC_KEY_PATTERN.match(value1.public_key))
     self.assertTrue(PUBLIC_KEY_PATTERN.match(value2.public_key))
     self.assertTrue(PRIVATE_KEY_PATTERN.match(value1.private_key))
     self.assertTrue(PRIVATE_KEY_PATTERN.match(value2.private_key))
     self.assertNotEqual(value1.public_key, value2.public_key)
     self.assertNotEqual(value1.private_key, value2.private_key)
Exemple #9
0
 def test_key_class_random_keys(self):
     """Test the key class generates different keys each time initialized"""
     value1 = Key()
     value2 = Key()
     self.assertTrue(isinstance(value1, Key))
     self.assertTrue(isinstance(value2, Key))
     self.assertNotEqual(value1, value2)
     self.assertIsInstance(value1.public_key, str)
     self.assertIsInstance(value2.public_key, str)
     self.assertIsInstance(value1.private_key, str)
     self.assertIsInstance(value2.private_key, str)
     self.assertEqual(len(value1.public_key), PUBLIC_KEY_LENGTH * 2)
     self.assertEqual(len(value2.public_key), PUBLIC_KEY_LENGTH * 2)
     self.assertEqual(len(value1.private_key), PRIVATE_KEY_LENGTH * 2)
     self.assertEqual(len(value2.private_key), PRIVATE_KEY_LENGTH * 2)
     self.assertTrue(PUBLIC_KEY_PATTERN.match(value1.public_key))
     self.assertTrue(PUBLIC_KEY_PATTERN.match(value2.public_key))
     self.assertTrue(PRIVATE_KEY_PATTERN.match(value1.private_key))
     self.assertTrue(PRIVATE_KEY_PATTERN.match(value2.private_key))
     self.assertNotEqual(value1.public_key, value2.public_key)
     self.assertNotEqual(value1.private_key, value2.private_key)
Exemple #10
0
 def assertIsPublicKeySecp256k1(self, key):
     """Sanity checks a Secp256k1PublicKey public key"""
     self.assertIsInstance(key, Secp256k1PublicKey)
     self.assertIsInstance(key.as_hex(), str)
     self.assertEqual(len(key.as_hex()), PUBLIC_KEY_LENGTH * 2)
     self.assertTrue(PUBLIC_KEY_PATTERN.match(key.as_hex()))
     self.assertIsInstance(key.as_bytes(), bytes)
     self.assertEqual(len(key.as_bytes()), PUBLIC_KEY_LENGTH)
     self.assertEqual(key.as_hex(),
                      str(binascii.hexlify(key.as_bytes()), "ascii"))
     self.assertEqual(binascii.unhexlify(key.as_hex()), key.as_bytes())
     return key
Exemple #11
0
 def validate_state(self, context, message, inputs, input_state, store, signer):
     """Common state validation for all messages"""
     if signer is None:
         raise ValueError("Signer is required")
     if message is None:
         raise ValueError("Message is required")
     if not isinstance(inputs, list) and not isinstance(inputs, set):
         raise ValueError("Inputs is required and expected to be a list or a set")
     if not isinstance(input_state, dict):
         raise ValueError("Input state was expecte to be a dictionary")
     if not isinstance(signer, str) and PUBLIC_KEY_PATTERN.match(signer):
         raise TypeError("Expected signer to be a public key")
     if context is None:
         raise ValueError("State context is required")
 def validate_state(self, context, message, payload, input_state, store):
     """Common state validation for all messages"""
     if payload.signer.public_key is None:
         raise ValueError("Signer public key is required")
     if payload.signer.user_id is None:
         raise ValueError("Signer user id is required")
     if message is None:
         raise ValueError("Message is required")
     if not isinstance(payload.inputs, (list, set)):
         raise ValueError("Inputs is required and expected to be a list or a set")
     if not isinstance(input_state, dict):
         raise ValueError("Input state was expected to be a dictionary")
     if not isinstance(payload.signer.public_key, str) and PUBLIC_KEY_PATTERN.match(
         payload.signer.public_key
     ):
         raise TypeError("Expected signer public key to be a public key")
     if context is None:
         raise ValueError("State context is required")
 def test_key_class_public_key(self):
     private_key = Secp256k1PrivateKey.new_random()
     txn_key = Key(private_key.as_hex())
     self.assertEqual(len(txn_key.public_key), PUBLIC_KEY_LENGTH * 2)
     self.assertTrue(PUBLIC_KEY_PATTERN.match(txn_key.public_key))