示例#1
0
 def test_dumps(self):
     ref = Entity()
     stream = StringIO()
     ref.dump(stream, SUPER_SECURE_PASSWORD)
     stream.seek(0)
     other = Entity.load(stream, SUPER_SECURE_PASSWORD)
     self.assertEqual(ref.private_key, other.private_key)
示例#2
0
    def test_hashing_not_equal(self):
        entity1 = Entity()
        entity2 = Entity()
        addr1 = Address(entity1)
        addr2 = Address(entity2)

        self.assertNotEqual(hash(addr1), hash(addr2))
示例#3
0
    def test_prompt_dump(self):
        ref = Entity()

        stream = StringIO()
        with patch('getpass.getpass') as mock_getpass:
            mock_getpass.side_effect = [
                WEAK_PASSWORD, SUPER_SECURE_PASSWORD, WEAK_PASSWORD,
                SUPER_SECURE_PASSWORD
            ]
            ref.prompt_dump(stream)

        stream.seek(0)
示例#4
0
    def test_construction_from_bytes(self):
        # create a reference and a copy of the entity
        reference = Entity()
        other = Entity(reference.private_key_bytes)

        self.assertEqual(reference.private_key_bytes, other.private_key_bytes)
        self.assertEqual(reference.private_key, other.private_key)
        self.assertEqual(reference.private_key_hex, other.private_key_hex)

        self.assertEqual(reference.public_key_bytes, other.public_key_bytes)
        self.assertEqual(reference.public_key, other.public_key)
        self.assertEqual(reference.public_key_hex, other.public_key_hex)
示例#5
0
    def test_prompt_load(self):
        ref = Entity()
        stream = StringIO()

        # write the entity to the stream
        ref.dump(stream, SUPER_SECURE_PASSWORD)

        # reset the stream
        stream.seek(0)
        with patch('getpass.getpass') as mock_getpass:
            mock_getpass.side_effect = [SUPER_SECURE_PASSWORD]

            other = Entity.prompt_load(stream)
            self.assertEqual(other.private_key, ref.private_key)
示例#6
0
    def test_generation(self):
        entity = Entity()

        self.assertEqual(32, len(entity.private_key_bytes))

        # check the other binary representations
        self.assertEqual(
            base64.b64encode(entity.private_key_bytes).decode(),
            entity.private_key)
        self.assertEqual(entity.private_key_bytes.hex(),
                         entity.private_key_hex)

        signing_key = ecdsa.SigningKey.from_string(
            entity.private_key_bytes,
            curve=Entity.curve,
            hashfunc=Entity.hash_function)
        verifying_key = signing_key.get_verifying_key()

        self.assertEqual(verifying_key.to_string(), entity.public_key_bytes)

        # check the other binary representations
        self.assertEqual(
            base64.b64encode(entity.public_key_bytes).decode(),
            entity.public_key)
        self.assertEqual(entity.public_key_bytes.hex(), entity.public_key_hex)
示例#7
0
    def test_hex_display(self):
        entity = Entity()
        address = Address(entity)

        address_bytes = bytes(address)
        hex_address = address_bytes.hex()

        self.assertEqual(hex_address, address.to_hex())
示例#8
0
    def test_hex_display(self):
        entity = Entity()
        address = Address(entity)

        address_bytes = bytes(address)
        hex_address = binascii.hexlify(address_bytes).decode()

        self.assertEqual(hex_address, address.to_hex())
示例#9
0
    def test_construction_from_strings(self):
        ref = Entity()

        test1 = Identity.from_hex(ref.public_key_hex)
        self.assertEqual(ref, test1)

        test2 = Identity.from_base64(ref.public_key)
        self.assertEqual(ref, test2)
示例#10
0
    def test_construction_from_verifying_key(self):
        entity = Entity()  # create a private / public key pair

        # create the identity from the identity (copy)
        identity = Identity(entity.verifying_key)

        # ensure the they are the same
        self.assertEqual(identity.public_key_bytes, entity.public_key_bytes)
示例#11
0
    def test_invalid_display(self):
        entity = Entity()
        address = Address(entity)
        address_bytes = bytes(address)
        invalid_checksum = bytes([0] * Address.CHECKSUM_SIZE)
        invalid_display = base58.b58encode(address_bytes + invalid_checksum).decode()

        with self.assertRaises(ValueError):
            _ = Address(invalid_display)
示例#12
0
    def test_construction_from_identity(self):
        entity = Entity()
        address = Address(entity)

        # manually compute the address value
        expected_address_bytes, expected_display = _calc_address(entity.public_key_bytes)

        self.assertEqual(expected_address_bytes, bytes(address))
        self.assertEqual(expected_display, str(address))
示例#13
0
    def test_construction_from_string(self):
        entity = Entity()

        # manually compute the address value
        expected_address_bytes, expected_display = _calc_address(entity.public_key_bytes)

        # re-create the address from the display string
        address = Address(expected_display)

        self.assertEqual(bytes(address), expected_address_bytes)
        self.assertEqual(str(address), expected_display)
示例#14
0
    def test_supports_collections(self):
        entity = Entity()

        # create a series of identities
        identity1 = Identity(entity)
        identity2 = Identity(entity)
        identity3 = Identity(identity2)

        identity_list = [identity1, identity2, identity3]
        self.assertEqual(len(identity_list), 3)
        self.assertIn(identity1, identity_list)
        self.assertIn(identity2, identity_list)
        self.assertIn(identity3, identity_list)

        # due to the fact that each identity is the name, the first element will always be matched
        self.assertEqual(identity_list.index(identity1), 0)
        self.assertEqual(identity_list.index(identity2), 0)
        self.assertEqual(identity_list.index(identity3), 0)

        identity_set = set(identity_list)
        self.assertEqual(len(identity_set), 1)
        self.assertIn(identity1, identity_set)
        self.assertIn(identity2, identity_set)
        self.assertIn(identity3, identity_set)
示例#15
0
    def test_signing_verifying_cycle(self):
        entity = Entity()

        payload = 'foo bar is a baz'

        # sign the payload
        signature = entity.sign(payload.encode())

        # verify the payload
        self.assertTrue(entity.verify(payload.encode(), signature))

        # modify the signature slightly
        bad_signature = bytes([(signature[0] + 1) & 0xff]) + signature[1:]

        self.assertFalse(entity.verify(payload.encode(), bad_signature))

        # also ensure a different payload is not verifiable
        self.assertFalse(
            entity.verify('foo bar is not a baz'.encode(), signature))
示例#16
0
    def test_construction_from_address(self):
        entity = Entity()
        address1 = Address(entity)
        address2 = Address(address1)

        self.assertEqual(bytes(address1), bytes(address2))
示例#17
0
    def test_equality(self):
        entity = Entity()
        addr1 = Address(entity)
        addr2 = Address(entity)

        self.assertEqual(addr1, addr2)
示例#18
0
 def test_not_equal(self):
     entity1 = Entity()
     entity2 = Entity()
     self.assertNotEqual(entity1, entity2)
示例#19
0
    def test_construction_from_identity(self):
        entity = Entity()  # create a private / public key pair
        identity = Identity(entity)

        self.assertEqual(entity.public_key_bytes, identity.public_key_bytes)
示例#20
0
from fetchai.ledger.crypto.address import Address
from fetchai.ledger.crypto.entity import Entity
from fetchai.ledger.crypto.identity import Identity
from fetchai.ledger.serialisation import transaction, bytearray
from fetchai.ledger.serialisation.sha256 import sha256_hex
from fetchai.ledger.transaction import Transaction

_PRIVATE_KEYS = (
    '1411d53f88e736eac7872430dbe5b55ac28c17a3e648c388e0bd1b161ab04427',
    '3436c184890d498b25bc2b5cb0afb6bad67379ebd778eae1de40b6e0f0763825',
    '4a56a19355f934174f6388b3c80598abb151af79c23d5a7af45a13357fb71253',
    'f9d67ec139eb7a1cb1f627357995847392035c1e633e8530de5ab5d04c6e9c33',
    '80f0e1c69e5f1216f32647c20d744c358e0894ebc855998159017a5acda208ba',
)

ENTITIES = [Entity.from_hex(x) for x in _PRIVATE_KEYS]
IDENTITIES = [Identity(x) for x in ENTITIES]


def _calculate_integer_stream_size(length: int) -> int:
    if length < 0x80:
        return 1
    elif length < 0x100:
        return 2
    elif length < 0x1000:
        return 4
    else:
        return 8


class TransactionSerialisation(unittest.TestCase):
示例#21
0
    def test_hashing_equal(self):
        entity = Entity()
        addr1 = Address(entity)
        addr2 = Address(entity)

        self.assertEqual(hash(addr1), hash(addr2))
示例#22
0
 def test_signing_key(self):
     entity = Entity()
     self.assertIsInstance(entity.signing_key, ecdsa.SigningKey)
示例#23
0
 def test_invalid_construction(self):
     with self.assertRaises(RuntimeError):
         _ = Entity(str())
示例#24
0
    def test_construction_from_base64(self):
        ref = Entity()
        ref_key = ref.private_key

        other = Entity.from_base64(ref_key)
        self.assertEqual(ref.private_key_bytes, other.private_key_bytes)
示例#25
0
 def test_dump_failure_on_weak_password(self):
     stream = StringIO()
     with self.assertRaises(RuntimeError):
         Entity().dump(stream, WEAK_PASSWORD)
示例#26
0
 def test_dumps_failure_on_weak_password(self):
     with self.assertRaises(RuntimeError):
         Entity().dumps(WEAK_PASSWORD)
示例#27
0
 def test_loads(self):
     ref = Entity()
     value = ref.dumps(SUPER_SECURE_PASSWORD)
     other = Entity.loads(value, SUPER_SECURE_PASSWORD)
     self.assertEqual(ref.private_key, other.private_key)
示例#28
0
 def test_conversion_from_hex(self):
     ref = Entity()
     other = Entity.from_hex(ref.private_key_hex)
     self.assertEqual(ref.private_key, other.private_key)